Modified editor and rename functions to go through the API when rolling back. This causes rollbacks to be logged.
authorDan
Mon, 11 Feb 2008 10:05:33 -0500
changeset 408 7ecbe721217c
parent 407 35d94240a197
child 409 267be9e394c2
Modified editor and rename functions to go through the API when rolling back. This causes rollbacks to be logged.
ajax.php
includes/clientside/static/editor.js
includes/pageutils.php
index.php
language/english/core.json
--- a/ajax.php	Sun Feb 10 19:35:06 2008 -0500
+++ b/ajax.php	Mon Feb 11 10:05:33 2008 -0500
@@ -98,7 +98,8 @@
     case "getsource":
       header('Content-type: application/json');
       $password = ( isset($_GET['pagepass']) ) ? $_GET['pagepass'] : false;
-      $page = new PageProcessor($paths->page_id, $paths->namespace);
+      $revid = ( isset($_GET['revid']) ) ? intval($_GET['revid']) : 0;
+      $page = new PageProcessor($paths->page_id, $paths->namespace, $revid);
       $page->password = $password;
       if ( $src = $page->fetch_source() )
       {
@@ -125,9 +126,39 @@
           'auth_edit' => $auth_edit,
           'time' => time(),
           'require_captcha' => false,
-          'allow_wysiwyg' => $auth_wysiwyg
+          'allow_wysiwyg' => $auth_wysiwyg,
+          'revid' => $revid
         );
       
+      if ( $revid > 0 )
+      {
+        // Retrieve information about this revision and the current one
+        $q = $db->sql_query('SELECT l1.author AS currentrev_author, l2.author AS oldrev_author FROM ' . table_prefix . 'logs AS l1
+  LEFT JOIN ' . table_prefix . 'logs AS l2
+    ON ( l2.time_id = ' . $revid . '
+         AND l2.log_type  = \'page\'
+         AND l2.action    = \'edit\'
+         AND l2.page_id   = \'ACL_Tests\'
+         AND l2.namespace = \'Article\'
+        )
+  WHERE l1.log_type  = \'page\'
+    AND l1.action    = \'edit\'
+    AND l1.page_id   = \'ACL_Tests\'
+    AND l1.namespace = \'Article\'
+    AND l1.time_id >= ' . $revid . '
+  ORDER BY l1.time_id DESC;');
+        if ( !$q )
+          $db->die_json();
+        
+        $rev_count = $db->numrows() - 1;
+        $row = $db->fetchrow();
+        $return['undo_info'] = array(
+          'old_author'     => $row['oldrev_author'],
+          'current_author' => $row['currentrev_author'],
+          'undo_count'     => $rev_count
+        );
+      }
+      
       if ( $auth_edit && !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
       {
         $return['require_captcha'] = true;
--- a/includes/clientside/static/editor.js	Sun Feb 10 19:35:06 2008 -0500
+++ b/includes/clientside/static/editor.js	Mon Feb 11 10:05:33 2008 -0500
@@ -43,16 +43,17 @@
 
 var editor_open = false;
 
-function ajaxEditor()
+function ajaxEditor(revid)
 {
   if ( KILL_SWITCH )
     return true;
   if ( editor_open )
     return true;
+  var rev_id_uri = ( revid ) ? '&revid=' + revid : '';
   selectButtonMinor('edit');
   selectButtonMajor('article');
   setAjaxLoading();
-  ajaxGet(stdAjaxPrefix + '&_mode=getsource', function()
+  ajaxGet(stdAjaxPrefix + '&_mode=getsource' + rev_id_uri, function()
     {
       if ( ajax.readyState == 4 && ajax.status == 200 )
       {
@@ -83,12 +84,12 @@
         // do we need to enter a captcha before saving the page?
         var captcha_hash = ( response.require_captcha ) ? response.captcha_id : false;
         
-        ajaxBuildEditor(response.src, (!response.auth_edit), response.time, response.allow_wysiwyg, captcha_hash);
+        ajaxBuildEditor(response.src, (!response.auth_edit), response.time, response.allow_wysiwyg, captcha_hash, response.revid, response.undo_info);
       }
     });
 }
 
-function ajaxBuildEditor(content, readonly, timestamp, allow_wysiwyg, captcha_hash)
+function ajaxBuildEditor(content, readonly, timestamp, allow_wysiwyg, captcha_hash, revid, undo_info)
 {
   // Set flags
   // We don't want the fancy confirmation framework to trigger if the user is only viewing the page source
@@ -173,6 +174,15 @@
     return false;
   }
   
+  // Old-revision notice
+  if ( revid > 0 )
+  {
+    var oldrev_box = document.createElement('div');
+    oldrev_box.className = 'usermessage';
+    oldrev_box.appendChild(document.createTextNode($lang.get('editor_msg_editing_old_revision')));
+    form.appendChild(oldrev_box);
+  }
+  
   // Preview holder
   var preview_anchor = document.createElement('a');
   preview_anchor.name = 'ajax_preview';
@@ -244,6 +254,13 @@
     field_es.type = 'text';
     field_es.size = '40';
     field_es.style.width = '96%';
+    
+    if ( revid > 0 )
+    {
+      undo_info.last_rev_id = revid;
+      field_es.value = $lang.get('editor_reversion_edit_summary', undo_info);
+    }
+    
     td1_2.appendChild(field_es);
     
     tr1.appendChild(td1_1);
--- a/includes/pageutils.php	Sun Feb 10 19:35:06 2008 -0500
+++ b/includes/pageutils.php	Mon Feb 11 10:05:33 2008 -0500
@@ -433,7 +433,7 @@
         // Actions!
         echo '<td class="' . $cls . '" style="text-align: center;"><a href="'.makeUrlNS($namespace, $page_id, 'oldid=' . $r['time_id']) . '" onclick="ajaxHistView(\'' . $r['time_id'] . '\'); return false;">' . $lang->get('history_action_view') . '</a></td>'."\n";
         echo '<td class="' . $cls . '" style="text-align: center;"><a href="'.makeUrl($paths->nslist['Special'].'Contributions/' . $r['author']) . '">' . $lang->get('history_action_contrib') . '</a></td>'."\n";
-        echo '<td class="' . $cls . '" style="text-align: center;"><a href="'.makeUrlNS($namespace, $page_id, 'do=rollback&amp;id=' . $r['time_id']) . '" onclick="ajaxRollback(\'' . $r['time_id'] . '\'); return false;">' . $lang->get('history_action_restore') . '</a></td>'."\n";
+        echo '<td class="' . $cls . '" style="text-align: center;"><a href="'.makeUrlNS($namespace, $page_id, 'do=edit&amp;revid=' . $r['time_id']) . '" onclick="ajaxEditor(\'' . $r['time_id'] . '\'); return false;">' . $lang->get('history_action_restore') . '</a></td>'."\n";
         
         echo '</tr>'."\n"."\n";
         
@@ -524,6 +524,10 @@
   public static function rollback($id)
   {
     global $db, $session, $paths, $template, $plugins; // Common objects
+    global $lang;
+    
+    // FIXME: l10n
+    
     if ( !$session->get_permissions('history_rollback') )
     {
       return('You are not authorized to perform rollbacks.');
@@ -584,25 +588,22 @@
       case "page":
         switch($rb['action'])
         {
-          case "edit":
-            if ( !$perms->get_permissions('edit_page') )
-              return "You don't have permission to edit pages, so rolling back edits can't be allowed either.";
-            $t = $db->escape($rb['page_text']);
-            $e = $db->sql_query('UPDATE ' . table_prefix.'page_text SET page_text=\'' . $t . '\',char_tag=\'' . $rb['char_tag'] . '\' WHERE page_id=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
-            if ( !$e )
-            {
-              return("An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace());
-            }
-            else
-            {
-              return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been rolled back to the state it was in on ' . enano_date('d M Y h:i a', intval($rb['time_id'])) . '.';
-            }
-            break;
+          // Support for rolling back edits removed in 1.1.2 - moved to page editor system
           case "rename":
             if ( !$perms->get_permissions('rename') )
               return "You don't have permission to rename pages, so rolling back renames can't be allowed either.";
-            $t = $db->escape($rb['edit_summary']);
-            $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET name=\'' . $t . '\' WHERE urlname=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
+            
+            $t = $rb['edit_summary'];
+            // result prediction
+            $subst = array(
+              'page_name_old' => get_page_title_ns($rb['page_id'], $rb['namespace']),
+              'page_name_new' => $t
+              );
+            
+            $e = PageUtils::rename($rb['page_id'], $rb['namespace'], $t);
+            
+            $e = ( $e == $lang->get('ajax_rename_success', $subst) );
+            
             if ( !$e )
             {
               return "An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace();
--- a/index.php	Sun Feb 10 19:35:06 2008 -0500
+++ b/index.php	Mon Feb 11 10:05:33 2008 -0500
@@ -153,12 +153,49 @@
       if(isset($_POST['_preview']))
       {
         $text = $_POST['page_text'];
+        $edsumm = $_POST['edit_summary'];
         echo PageUtils::genPreview($_POST['page_text']);
         $text = htmlspecialchars($text);
+        $revid = 0;
       }
       else
       {
-        $text = RenderMan::getPage($paths->cpage['urlname_nons'], $paths->namespace, 0, false, false, false, false);
+        $revid = ( isset($_GET['revid']) ) ? intval($_GET['revid']) : 0;
+        $page = new PageProcessor($paths->page_id, $paths->namespace, $revid);
+        $text = $page->fetch_source();
+        $edsumm = '';
+        // $text = RenderMan::getPage($paths->cpage['urlname_nons'], $paths->namespace, 0, false, false, false, false);
+      }
+      if ( $revid > 0 )
+      {
+        echo '<div class="usermessage">' . $lang->get('editor_msg_editing_old_revision') . '</div>';
+        // Retrieve information about this revision and the current one
+        $q = $db->sql_query('SELECT l1.author AS currentrev_author, l2.author AS oldrev_author FROM ' . table_prefix . 'logs AS l1
+  LEFT JOIN ' . table_prefix . 'logs AS l2
+    ON ( l2.time_id = ' . $revid . '
+         AND l2.log_type  = \'page\'
+         AND l2.action    = \'edit\'
+         AND l2.page_id   = \'ACL_Tests\'
+         AND l2.namespace = \'Article\'
+        )
+  WHERE l1.log_type  = \'page\'
+    AND l1.action    = \'edit\'
+    AND l1.page_id   = \'ACL_Tests\'
+    AND l1.namespace = \'Article\'
+    AND l1.time_id >= ' . $revid . '
+  ORDER BY l1.time_id DESC;');
+        if ( !$q )
+          $db->die_json();
+        
+        $rev_count = $db->numrows() - 1;
+        $row = $db->fetchrow();
+        $undo_info = array(
+          'old_author'     => $row['oldrev_author'],
+          'current_author' => $row['currentrev_author'],
+          'undo_count'     => $rev_count,
+          'last_rev_id'    => $revid
+        );
+        $db->free_result();
       }
       echo '
         <form action="'.makeUrl($paths->page, 'do=edit').'" method="post" enctype="multipart/form-data">
@@ -166,7 +203,8 @@
         <textarea name="page_text" rows="20" cols="60" style="width: 97%;">'.$text.'</textarea><br />
         <br />
         ';
-      echo $lang->get('editor_lbl_edit_summary') . ' <input name="edit_summary" type="text" size="40" /><br /><label><input type="checkbox" name="minor" /> ' . $lang->get('editor_lbl_minor_edit_field') . '</label><br />';
+      $edsumm = ( $revid > 0 ) ? $lang->get('editor_reversion_edit_summary', $undo_info) : $edsumm;
+      echo $lang->get('editor_lbl_edit_summary') . ' <input name="edit_summary" type="text" size="40" value="' . htmlspecialchars($edsumm) . '" /><br /><label><input type="checkbox" name="minor" /> ' . $lang->get('editor_lbl_minor_edit_field') . '</label><br />';
       if ( !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
       {
         echo '<br /><table border="0"><tr><td>';
--- a/language/english/core.json	Sun Feb 10 19:35:06 2008 -0500
+++ b/language/english/core.json	Mon Feb 11 10:05:33 2008 -0500
@@ -324,6 +324,7 @@
       msg_cancel_confirm_body: 'Are you sure you want to discard the changes you made? If you continue, your changes cannot be recovered.',
       msg_diff: 'Below is a summarization of the differences between the current revision of the page (left), and your version (right).',
       msg_diff_empty: 'There are no differences between the text in the editor and the current revision of the page.',
+      msg_editing_old_revision: 'You are editing an old revision of this page. If you click Save, newer revisions of this page will be undone.',
       btn_graphical: 'graphical editor',
       btn_wikitext: 'wikitext editor',
       lbl_edit_summary: 'Brief summary of your changes:',
@@ -340,6 +341,7 @@
       preview_blurb: '<b>Reminder:</b> This is only a preview - your changes to this page have not yet been saved.',
       msg_save_success_title: 'Changes saved',
       msg_save_success_body: 'Your changes to this page have been saved. Redirecting...',
+      reversion_edit_summary: 'Undid %undo_count% revision(s) by %current_author% to revision %last_rev_id% by %old_author%',
       
       msg_captcha_pleaseenter: 'Please enter the code shown in the image to the right into the text box. This process helps to ensure that this page is not being edited by an automated bot. If the image to the right is illegible, you can regenerate it by clicking on the image (only works if your browser supports Javascript).',
       msg_captcha_blind: 'If you are visually impaired or otherwise cannot read the text shown to the right, please contact the site management and they will be able to make your requested edits.',