Cleanup
[lhc/web/wiklou.git] / includes / Article.php
index 0b1b869..68e561a 100644 (file)
@@ -36,25 +36,12 @@ class Article {
        var $mUserText;                 //!<
        /**@}}*/
 
-       /**
-        * Constants used by internal components to get rollback results
-        */
-       const SUCCESS = 0;                      // Operation successful
-       const PERM_DENIED = 1;          // Permission denied
-       const BLOCKED = 2;                      // User has been blocked
-       const READONLY = 3;                     // Wiki is in read-only mode
-       const BAD_TOKEN = 4;            // Invalid token specified
-       const BAD_TITLE = 5;            // $this is not a valid Article
-       const ALREADY_ROLLED = 6;       // Someone else already rolled this back. $from and $summary will be set
-       const ONLY_AUTHOR = 7;          // User is the only author of the page
-       const RATE_LIMITED = 8;
        /**
         * Constructor and clear the article
         * @param $title Reference to a Title object.
         * @param $oldId Integer revision ID, null to fetch from request, zero for current
         */
-       function __construct( &$title, $oldId = null ) {
+       function __construct( Title $title, $oldId = null ) {
                $this->mTitle =& $title;
                $this->mOldId = $oldId;
                $this->clear();
@@ -626,6 +613,7 @@ class Article {
                global $wgUser, $wgOut, $wgRequest, $wgContLang;
                global $wgEnableParserCache, $wgStylePath, $wgParser;
                global $wgUseTrackbacks, $wgNamespaceRobotPolicies, $wgArticleRobotPolicies;
+               global $wgDefaultRobotPolicy;
                $sk = $wgUser->getSkin();
 
                wfProfileIn( __METHOD__ );
@@ -660,8 +648,7 @@ class Article {
                        # Honour customised robot policies for this namespace
                        $policy = $wgNamespaceRobotPolicies[$ns];
                } else {
-                       # Default to encourage indexing and following links
-                       $policy = 'index,follow';
+                       $policy = $wgDefaultRobotPolicy;
                }
                $wgOut->setRobotPolicy( $policy );
 
@@ -783,11 +770,11 @@ class Article {
                                        $this->setOldSubtitle( isset($this->mOldId) ? $this->mOldId : $oldid );
                                        if( $this->mRevision->isDeleted( Revision::DELETED_TEXT ) ) {
                                                if( !$this->mRevision->userCan( Revision::DELETED_TEXT ) ) {
-                                                       $wgOut->addWikiText( wfMsg( 'rev-deleted-text-permission' ) );
+                                                       $wgOut->addWikiMsg( 'rev-deleted-text-permission' );
                                                        $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
                                                        return;
                                                } else {
-                                                       $wgOut->addWikiText( wfMsg( 'rev-deleted-text-view' ) );
+                                                       $wgOut->addWikiMsg( 'rev-deleted-text-view' );
                                                        // and we are allowed to see...
                                                }
                                        }
@@ -865,7 +852,7 @@ class Article {
                # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
                if( $ns == NS_USER_TALK &&
                        User::isIP( $this->mTitle->getText() ) ) {
-                       $wgOut->addWikiText( wfMsg('anontalkpagetext') );
+                       $wgOut->addWikiMsg('anontalkpagetext');
                }
 
                # If we have been passed an &rcid= parameter, we want to give the user a
@@ -917,14 +904,14 @@ class Article {
                                        $o->tb_name,
                                        $rmvtxt);
                }
-               $wgOut->addWikitext(wfMsg('trackbackbox', $tbtext));
+               $wgOut->addWikiMsg( 'trackbackbox', $tbtext );
        }
 
        function deletetrackback() {
                global $wgUser, $wgRequest, $wgOut, $wgTitle;
 
                if (!$wgUser->matchEditToken($wgRequest->getVal('token'))) {
-                       $wgOut->addWikitext(wfMsg('sessionfailure'));
+                       $wgOut->addWikiMsg( 'sessionfailure' );
                        return;
                }
 
@@ -939,7 +926,7 @@ class Article {
                $db = wfGetDB(DB_MASTER);
                $db->delete('trackbacks', array('tb_id' => $wgRequest->getInt('tbid')));
                $wgTitle->invalidateCache();
-               $wgOut->addWikiText(wfMsg('trackbackdeleteok'));
+               $wgOut->addWikiMsg('trackbackdeleteok');
        }
 
        function render() {
@@ -991,6 +978,15 @@ class Article {
                        $update = SquidUpdate::newSimplePurge( $this->mTitle );
                        $update->doUpdate();
                }
+               if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
+                       global $wgMessageCache;
+                       if ( $this->getID() == 0 ) {
+                               $text = false;
+                       } else {
+                               $text = $this->getContent();
+                       }
+                       $wgMessageCache->replace( $this->mTitle->getDBkey(), $text );
+               }
                $this->view();
        }
 
@@ -1201,10 +1197,11 @@ class Article {
        /**
         * @deprecated use Article::doEdit()
         */
-       function insertNewArticle( $text, $summary, $isminor, $watchthis, $suppressRC=false, $comment=false ) {
+       function insertNewArticle( $text, $summary, $isminor, $watchthis, $suppressRC=false, $comment=false, $bot=false ) {
                $flags = EDIT_NEW | EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY |
                        ( $isminor ? EDIT_MINOR : 0 ) |
-                       ( $suppressRC ? EDIT_SUPPRESS_RC : 0 );
+                       ( $suppressRC ? EDIT_SUPPRESS_RC : 0 ) |
+                       ( $bot ? EDIT_FORCE_BOT : 0 );
 
                # If this is a comment, add the summary as headline
                if ( $comment && $summary != "" ) {
@@ -1323,7 +1320,7 @@ class Article {
 
                # Silently ignore EDIT_MINOR if not allowed
                $isminor = ( $flags & EDIT_MINOR ) && $wgUser->isAllowed('minoredit');
-               $bot = $wgUser->isAllowed( 'bot' ) || ( $flags & EDIT_FORCE_BOT );
+               $bot = $flags & EDIT_FORCE_BOT;
 
                $oldtext = $this->getContent();
                $oldsize = strlen( $oldtext );
@@ -1349,8 +1346,10 @@ class Article {
 
                        $lastRevision = 0;
                        $revisionId = 0;
+                       
+                       $changed = ( strcmp( $text, $oldtext ) != 0 );
 
-                       if ( 0 != strcmp( $text, $oldtext ) ) {
+                       if ( $changed ) {
                                $this->mGoodAdjustment = (int)$this->isCountable( $text )
                                  - (int)$this->isCountable( $oldtext );
                                $this->mTotalAdjustment = 0;
@@ -1415,9 +1414,8 @@ class Article {
                                # Invalidate cache of this article and all pages using this article
                                # as a template. Partly deferred.
                                Article::onArticleEdit( $this->mTitle );
-
+                               
                                # Update links tables, site stats, etc.
-                               $changed = ( strcmp( $oldtext, $text ) != 0 );
                                $this->editUpdates( $text, $summary, $isminor, $now, $revisionId, $changed );
                        }
                } else {
@@ -1571,7 +1569,7 @@ class Article {
                                # The user made this edit, and can't patrol it
                                # Tell them so, and then back off
                                $wgOut->setPageTitle( wfMsg( 'markedaspatrollederror' ) );
-                               $wgOut->addWikiText( wfMsgNoTrans( 'markedaspatrollederror-noautopatrol' ) );
+                               $wgOut->addWikiMsg( 'markedaspatrollederror-noautopatrol' );
                                $wgOut->returnToMain( false, $return );
                                return;
                        }
@@ -1584,7 +1582,7 @@ class Article {
 
                # Inform the user
                $wgOut->setPageTitle( wfMsg( 'markedaspatrolled' ) );
-               $wgOut->addWikiText( wfMsgNoTrans( 'markedaspatrolledtext' ) );
+               $wgOut->addWikiMsg( 'markedaspatrolledtext' );
                $wgOut->returnToMain( false, $return );
        }
 
@@ -1609,9 +1607,7 @@ class Article {
                        $wgOut->setPagetitle( wfMsg( 'addedwatch' ) );
                        $wgOut->setRobotpolicy( 'noindex,nofollow' );
 
-                       $link = wfEscapeWikiText( $this->mTitle->getPrefixedText() );
-                       $text = wfMsg( 'addedwatchtext', $link );
-                       $wgOut->addWikiText( $text );
+                       $wgOut->addWikiMsg( 'addedwatchtext', $this->mTitle->getPrefixedText() );
                }
 
                $wgOut->returnToMain( true, $this->mTitle->getPrefixedText() );
@@ -1656,9 +1652,7 @@ class Article {
                        $wgOut->setPagetitle( wfMsg( 'removedwatch' ) );
                        $wgOut->setRobotpolicy( 'noindex,nofollow' );
 
-                       $link = wfEscapeWikiText( $this->mTitle->getPrefixedText() );
-                       $text = wfMsg( 'removedwatchtext', $link );
-                       $wgOut->addWikiText( $text );
+                       $wgOut->addWikiMsg( 'removedwatchtext', $this->mTitle->getPrefixedText() );
                }
 
                $wgOut->returnToMain( true, $this->mTitle->getPrefixedText() );
@@ -1745,7 +1739,7 @@ class Article {
 
                                $expiry_description = '';
                                if ( $encodedExpiry != 'infinity' ) {
-                                       $expiry_description = ' (' . wfMsgForContent( 'protect-expiring', $wgContLang->timeanddate( $expiry ) ).')';
+                                       $expiry_description = ' (' . wfMsgForContent( 'protect-expiring', $wgContLang->timeanddate( $expiry, false, false ) ).')';
                                }
 
                                # Prepare a null revision to be added to the history
@@ -1776,10 +1770,7 @@ class Article {
                                        $comment .= "$expiry_description";
                                if ( $cascade )
                                        $comment .= "$cascade_description";
-
-                               $nullRevision = Revision::newNullRevision( $dbw, $id, $comment, true );
-                               $nullRevId = $nullRevision->insertOn( $dbw );
-
+                               
                                # Update restrictions table
                                foreach( $limit as $action => $restrictions ) {
                                        if ($restrictions != '' ) {
@@ -1793,6 +1784,10 @@ class Article {
                                        }
                                }
 
+                               # Insert a null revision
+                               $nullRevision = Revision::newNullRevision( $dbw, $id, $comment, true );
+                               $nullRevId = $nullRevision->insertOn( $dbw );
+
                                # Update page record
                                $dbw->update( 'page',
                                        array( /* SET */
@@ -1807,6 +1802,8 @@ class Article {
 
                                # Update the protection log
                                $log = new LogPage( 'protect' );
+                               
+                               
 
                                if( $protect ) {
                                        $log->addEntry( $modified ? 'modify' : 'protect', $this->mTitle, trim( $reason . " [$updated]$cascade_description$expiry_description" ) );
@@ -1883,21 +1880,20 @@ class Article {
                $row = $dbw->fetchObject($res);
                $onlyAuthor = $row->rev_user_text;
                // Try to find a second contributor
-               while(($row = $dbw->fetchObject($res)))
-                       if($row->rev_user_text != $onlyAuthor)
-                       {
+               while( $row = $dbw->fetchObject($res) ) {
+                       if($row->rev_user_text != $onlyAuthor) {
                                $onlyAuthor = false;
                                break;
                        }
+               }
                $dbw->freeResult($res);
 
                // Generate the summary with a '$1' placeholder
-               if($blank)
+               if($blank) {
                        // The current revision is blank and the one before is also
                        // blank. It's just not our lucky day
                        $reason = wfMsgForContent('exbeforeblank', '$1');
-               else
-               {
+               } else {
                        if($onlyAuthor)
                                $reason = wfMsgForContent('excontentauthor', '$1', $onlyAuthor);
                        else
@@ -1941,16 +1937,21 @@ class Article {
 
                # This code desperately needs to be totally rewritten
 
+               # Read-only check...
+               if ( wfReadOnly() ) {
+                       $wgOut->readOnlyPage();
+                       return;
+               }
+               
                # Check permissions
                $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgUser );
 
-               if (count($permission_errors)>0)
-               {
+               if (count($permission_errors)>0) {
                        $wgOut->showPermissionsErrorPage( $permission_errors );
                        return;
                }
 
-               $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
+               $wgOut->setPagetitle( wfMsg( 'delete-confirm', $this->mTitle->getPrefixedText() ) );
 
                # Better double-check that it hasn't been deleted yet!
                $dbw = wfGetDB( DB_MASTER );
@@ -1961,6 +1962,15 @@ class Article {
                        return;
                }
 
+               # Hack for big sites
+               $bigHistory = $this->isBigDeletion();
+               if( $bigHistory && !$this->mTitle->userCan( 'bigdelete' ) ) {
+                       global $wgLang, $wgDeleteRevisionsLimit;
+                       $wgOut->wrapWikiMsg( "<div class='error'>\n$1</div>\n",
+                               array( 'delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
+                       return;
+               }
+
                if( $confirm ) {
                        $this->doDelete( $reason );
                        if( $wgRequest->getCheck( 'wpWatch' ) ) {
@@ -1979,10 +1989,39 @@ class Article {
                if( $hasHistory && !$confirm ) {
                        $skin=$wgUser->getSkin();
                        $wgOut->addHTML( '<strong>' . wfMsg( 'historywarning' ) . ' ' . $skin->historyLink() . '</strong>' );
+                       if( $bigHistory ) {
+                               global $wgLang, $wgDeleteRevisionsLimit;
+                               $wgOut->wrapWikiMsg( "<div class='error'>\n$1</div>\n",
+                                       array( 'delete-warning-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
+                       }
                }
                
                return $this->confirmDelete( '', $reason );
        }
+       
+       /**
+        * @return bool whether or not the page surpasses $wgDeleteRevisionsLimit revisions
+        */
+       function isBigDeletion() {
+               global $wgDeleteRevisionsLimit;
+               if( $wgDeleteRevisionsLimit ) {
+                       $revCount = $this->estimateRevisionCount();
+                       return $revCount > $wgDeleteRevisionsLimit;
+               }
+               return false;
+       }
+       
+       /**
+        * @return int approximate revision count
+        */
+       function estimateRevisionCount() {
+               $dbr = wfGetDB();
+               // For an exact count...
+               //return $dbr->selectField( 'revision', 'COUNT(*)',
+               //      array( 'rev_page' => $this->getId() ), __METHOD__ );
+               return $dbr->estimateRowCount( 'revision', '*',
+                       array( 'rev_page' => $this->getId() ), __METHOD__ );
+       }
 
        /**
         * Get the last N authors
@@ -2032,96 +2071,59 @@ class Article {
 
        /**
         * Output deletion confirmation dialog
+        * @param $par string FIXME: do we need this parameter? One Call from Article::delete with '' only.
+        * @param $reason string Prefilled reason
         */
        function confirmDelete( $par, $reason ) {
-               global $wgOut, $wgUser;
+               global $wgOut, $wgUser, $wgContLang;
+               $align = $wgContLang->isRtl() ? 'left' : 'right';
 
                wfDebug( "Article::confirmDelete\n" );
 
-               $sub = htmlspecialchars( $this->mTitle->getPrefixedText() );
-               $wgOut->setSubtitle( wfMsg( 'deletesub', $sub ) );
+               $wgOut->setSubtitle( wfMsg( 'delete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->mTitle ) ) );
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
-               $wgOut->addWikiText( wfMsg( 'confirmdeletetext' ) );
-
-               $formaction = $this->mTitle->escapeLocalURL( 'action=delete' . $par );
-
-               $confirm = htmlspecialchars( wfMsg( 'deletepage' ) );
-               $delcom = Xml::label( wfMsg( 'deletecomment' ), 'wpDeleteReasonList' );
-               $token = htmlspecialchars( $wgUser->editToken() );
-               $watch = Xml::checkLabel( wfMsg( 'watchthis' ), 'wpWatch', 'wpWatch', $wgUser->getBoolOption( 'watchdeletion' ) || $this->mTitle->userIsWatching(), array( 'tabindex' => '2' ) );
-               
-               $mDeletereasonother = Xml::label( wfMsg( 'deleteotherreason' ), 'wpReason' );
-               $mDeletereasonotherlist = wfMsgHtml( 'deletereasonotherlist' );
-               $scDeleteReasonList = wfMsgForContent( 'deletereason-dropdown' );
-
-               $deleteReasonList = '';
-               if ( $scDeleteReasonList != '' && $scDeleteReasonList != '-' ) { 
-                       $deleteReasonList = "<option value=\"other\">$mDeletereasonotherlist</option>";
-                       $optgroup = "";
-                       foreach ( explode( "\n", $scDeleteReasonList ) as $option) {
-                               $value = trim( htmlspecialchars($option) );
-                               if ( $value == '' ) {
-                                       continue;
-                               } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
-                                       // A new group is starting ...
-                                       $value = trim( substr( $value, 1 ) );
-                                       $deleteReasonList .= "$optgroup<optgroup label=\"$value\">";
-                                       $optgroup = "</optgroup>";
-                               } elseif ( substr( $value, 0, 2) == '**' ) {
-                                       // groupmember
-                                       $selected = "";
-                                       $value = trim( substr( $value, 2 ) );
-                                       if ( $this->DeleteReasonList === $value)
-                                               $selected = ' selected="selected"';
-                                       $deleteReasonList .= "<option value=\"$value\"$selected>$value</option>";
-                               } else {
-                                       // groupless delete reason
-                                       $selected = "";
-                                       if ( $this->DeleteReasonList === $value)
-                                               $selected = ' selected="selected"';
-                                       $deleteReasonList .= "$optgroup<option value=\"$value\"$selected>$value</option>";
-                                       $optgroup = "";
-                               }
-                       }
-                       $deleteReasonList .= $optgroup;
-               }
-               $wgOut->addHTML( "
-<form id='deleteconfirm' method='post' action=\"{$formaction}\">
-       <table border='0'>
-               <tr id=\"wpDeleteReasonListRow\" name=\"wpDeleteReasonListRow\">
-                       <td align='right'>
-                               $delcom:
-                       </td>
-                       <td align='left'>
-                               <select tabindex='2' id='wpDeleteReasonList' name=\"wpDeleteReasonList\">
-                                       $deleteReasonList
-                               </select>
-                       </td>
-               </tr>
-               <tr id=\"wpDeleteReasonRow\" name=\"wpDeleteReasonRow\">
-                       <td>
-                               $mDeletereasonother
-                       </td>
-                       <td align='left'>
-                               <input type='text' maxlength='255' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" tabindex=\"1\" />
-                       </td>
-               </tr>
-               <tr>
-                       <td>&nbsp;</td>
-                       <td>$watch</td>
-               </tr>
-               <tr>
-                       <td>&nbsp;</td>
-                       <td>
-                               <input type='submit' name='wpConfirmB' id='wpConfirmB' value=\"{$confirm}\" tabindex=\"3\" />
-                       </td>
-               </tr>
-       </table>
-       <input type='hidden' name='wpEditToken' value=\"{$token}\" />
-</form>\n" );
-
-               $wgOut->returnToMain( false, $this->mTitle );
-
+               $wgOut->addWikiMsg( 'confirmdeletetext' );
+
+               $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->mTitle->getLocalURL( 'action=delete' . $par ), 'id' => 'deleteconfirm' ) ) .
+                       Xml::openElement( 'fieldset' ) .
+                       Xml::element( 'legend', array(), wfMsg( 'delete-legend' ) ) .
+                       Xml::openElement( 'table' ) .
+                       "<tr id=\"wpDeleteReasonListRow\">
+                               <td align='$align'>" .
+                                       Xml::label( wfMsg( 'deletecomment' ), 'wpDeleteReasonList' ) .
+                               "</td>
+                               <td>" .
+                                       Xml::listDropDown( 'wpDeleteReasonList',
+                                               wfMsgForContent( 'deletereason-dropdown' ), 
+                                               wfMsgForContent( 'deletereasonotherlist' ), '', 'wpReasonDropDown', 1 ) .
+                               "</td>
+                       </tr>
+                       <tr id=\"wpDeleteReasonRow\">
+                               <td align='$align'>" .
+                                       Xml::label( wfMsg( 'deleteotherreason' ), 'wpReason' ) .
+                               "</td>
+                               <td>" .
+                                       Xml::input( 'wpReason', 60, $reason, array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
+                               "</td>
+                       </tr>
+                       <tr>
+                               <td></td>
+                               <td>" .
+                                       Xml::checkLabel( wfMsg( 'watchthis' ), 'wpWatch', 'wpWatch', $wgUser->getBoolOption( 'watchdeletion' ) || $this->mTitle->userIsWatching(), array( 'tabindex' => '3' ) ) .
+                               "</td>
+                       </tr>
+                       <tr>
+                               <td></td>
+                               <td>" .
+                                       Xml::submitButton( wfMsg( 'deletepage' ), array( 'name' => 'wpConfirmB', 'id' => 'wpConfirmB', 'tabindex' => '4' ) ) .
+                               "</td>
+                       </tr>" .
+                       Xml::closeElement( 'table' ) .
+                       Xml::closeElement( 'fieldset' ) .
+                       Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
+                       Xml::closeElement( 'form' );
+
+               $wgOut->addHTML( $form );
                $this->showLogExtract( $wgOut );
        }
 
@@ -2149,15 +2151,14 @@ class Article {
 
                if (wfRunHooks('ArticleDelete', array(&$this, &$wgUser, &$reason))) {
                        if ( $this->doDeleteArticle( $reason ) ) {
-                               $deleted = wfEscapeWikiText( $this->mTitle->getPrefixedText() );
+                               $deleted = $this->mTitle->getPrefixedText();
 
                                $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
                                $wgOut->setRobotpolicy( 'noindex,nofollow' );
 
-                               $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
-                               $text = wfMsg( 'deletedtext', $deleted, $loglink );
+                               $loglink = '[[Special:Log/delete|' . wfMsgNoTrans( 'deletionlog' ) . ']]';
 
-                               $wgOut->addWikiText( $text );
+                               $wgOut->addWikiMsg( 'deletedtext', $deleted, $loglink );
                                $wgOut->returnToMain( false );
                                wfRunHooks('ArticleDeleteComplete', array(&$this, &$wgUser, $reason));
                        } else {
@@ -2267,57 +2268,76 @@ class Article {
        /**
         * Roll back the most recent consecutive set of edits to a page
         * from the same user; fails if there are no eligible edits to
-        * roll back to, e.g. user is the sole contributor
+        * roll back to, e.g. user is the sole contributor. This function
+        * performs permissions checks on $wgUser, then calls commitRollback()
+        * to do the dirty work
         *
         * @param string $fromP - Name of the user whose edits to rollback. 
         * @param string $summary - Custom summary. Set to default summary if empty.
         * @param string $token - Rollback token.
-        * @param bool $bot - If true, mark all reverted edits as bot.
+        * @param bool   $bot - If true, mark all reverted edits as bot.
         * 
-        * @param array $resultDetails contains result-specific dict of additional values
-        *    ALREADY_ROLLED : 'current' (rev)
-        *    SUCCESS        : 'summary' (str), 'current' (rev), 'target' (rev)
+        * @param array $resultDetails contains result-specific array of additional values
+        *    'alreadyrolled' : 'current' (rev)
+        *    success        : 'summary' (str), 'current' (rev), 'target' (rev)
         * 
-        * @return self::SUCCESS on succes, self::* on failure
+        * @return array of errors, each error formatted as
+        *   array(messagekey, param1, param2, ...).
+        * On success, the array is empty.  This array can also be passed to
+        * OutputPage::showPermissionsErrorPage().
         */
        public function doRollback( $fromP, $summary, $token, $bot, &$resultDetails ) {
-               global $wgUser, $wgUseRCPatrol;
+               global $wgUser;
                $resultDetails = null;
 
-               # Just in case it's being called from elsewhere         
-
-               if( $wgUser->isAllowed( 'rollback' ) && $this->mTitle->userCan( 'edit' ) ) {
-                       if( $wgUser->isBlocked() ) {
-                               return self::BLOCKED;
-                       }
-               } else {
-                       return self::PERM_DENIED;
-               }
-                       
-               if ( wfReadOnly() ) {
-                       return self::READONLY;
-               }
-
+               # Check permissions
+               $errors = array_merge( $this->mTitle->getUserPermissionsErrors( 'edit', $wgUser ),
+                                               $this->mTitle->getUserPermissionsErrors( 'rollback', $wgUser ) );
                if( !$wgUser->matchEditToken( $token, array( $this->mTitle->getPrefixedText(), $fromP ) ) )
-                       return self::BAD_TOKEN;
+                       $errors[] = array( 'sessionfailure' );
 
                if ( $wgUser->pingLimiter('rollback') || $wgUser->pingLimiter() ) {
-                       return self::RATE_LIMITED;
+                       $errors[] = array( 'actionthrottledtext' );
                }
-
+               # If there were errors, bail out now
+               if(!empty($errors))
+                       return $errors;
+               
+               return $this->commitRollback($fromP, $summary, $bot, $resultDetails);
+       }
+       
+       /**
+        * Backend implementation of doRollback(), please refer there for parameter
+        * and return value documentation
+        *
+        * NOTE: This function does NOT check ANY permissions, it just commits the
+        * rollback to the DB Therefore, you should only call this function direct-
+        * ly if you want to use custom permissions checks. If you don't, use
+        * doRollback() instead.
+        */     
+       public function commitRollback($fromP, $summary, $bot, &$resultDetails) {
+               global $wgUseRCPatrol, $wgUser;
                $dbw = wfGetDB( DB_MASTER );
 
+               if( wfReadOnly() ) {
+                       return array( array( 'readonlytext' ) );
+               }
+
                # Get the last editor
                $current = Revision::newFromTitle( $this->mTitle );
                if( is_null( $current ) ) {
                        # Something wrong... no page?
-                       return self::BAD_TITLE;
+                       return array(array('notanarticle'));
                }
 
                $from = str_replace( '_', ' ', $fromP );
                if( $from != $current->getUserText() ) {
                        $resultDetails = array( 'current' => $current );
-                       return self::ALREADY_ROLLED;
+                       return array(array('alreadyrolled',
+                               htmlspecialchars($this->mTitle->getPrefixedText()),
+                               htmlspecialchars($fromP),
+                               htmlspecialchars($current->getUserText())
+                       ));
                }
 
                # Get the last edit not by this guy
@@ -2325,17 +2345,15 @@ class Article {
                $user_text = $dbw->addQuotes( $current->getUserText() );
                $s = $dbw->selectRow( 'revision',
                        array( 'rev_id', 'rev_timestamp' ),
-                       array(
-                               'rev_page' => $current->getPage(),
+                       array(  'rev_page' => $current->getPage(),
                                "rev_user <> {$user} OR rev_user_text <> {$user_text}"
                        ), __METHOD__,
-                       array(
-                               'USE INDEX' => 'page_timestamp',
+                       array(  'USE INDEX' => 'page_timestamp',
                                'ORDER BY'  => 'rev_timestamp DESC' )
                        );
                if( $s === false ) {
-                       # Something wrong
-                       return self::ONLY_AUTHOR;
+                       # No one else ever edited this page
+                       return array(array('cantrollback'));
                }
        
                $set = array();
@@ -2358,14 +2376,25 @@ class Article {
                                );
                }
 
-               # Get the edit summary
+               # Generate the edit summary if necessary
                $target = Revision::newFromId( $s->rev_id );
                if( empty( $summary ) )
-                       $summary = wfMsgForContent( 'revertpage', $target->getUserText(), $from );
+               {
+                       global $wgLang;
+                       $summary = wfMsgForContent( 'revertpage',
+                                        $target->getUserText(), $from,
+                                        $s->rev_id, $wgLang->timeanddate(wfTimestamp(TS_MW, $s->rev_timestamp), true),
+                                        $current->getId(), $wgLang->timeanddate($current->getTimestamp())
+                       );
+               }
 
                # Save
-               $flags = EDIT_UPDATE | EDIT_MINOR;
-               if( $bot )
+               $flags = EDIT_UPDATE;
+
+               if ($wgUser->isAllowed('minoredit'))
+                       $flags |= EDIT_MINOR;
+
+               if( $bot && ($wgUser->isAllowed('markbotedits') || $wgUser->isAllowed('bot')) )
                        $flags |= EDIT_FORCE_BOT;
                $this->doEdit( $target->getText(), $summary, $flags );
 
@@ -2376,7 +2405,7 @@ class Article {
                        'current' => $current,
                        'target' => $target,
                );
-               return self::SUCCESS;
+               return array();
        }
 
        /**
@@ -2384,19 +2413,8 @@ class Article {
         */
        function rollback() {
                global $wgUser, $wgOut, $wgRequest, $wgUseRCPatrol;
-
                $details = null;
 
-               # Skip the permissions-checking in doRollback() itself, by checking permissions here.
-
-               $perm_errors = array_merge( $this->mTitle->getUserPermissionsErrors( 'edit', $wgUser ),
-                                               $this->mTitle->getUserPermissionsErrors( 'rollback', $wgUser ) );
-
-               if (count($perm_errors)) {
-                       $wgOut->showPermissionsErrorPage( $perm_errors );
-                       return;
-               }
-
                $result = $this->doRollback(
                        $wgRequest->getVal( 'from' ),
                        $wgRequest->getText( 'summary' ),
@@ -2405,61 +2423,44 @@ class Article {
                        $details
                );
 
-               switch( $result ) {
-                       case self::BLOCKED:
-                               $wgOut->blockedPage();
-                               break;
-                       case self::PERM_DENIED:
-                               $wgOut->permissionRequired( 'rollback' );
-                               break;
-                       case self::READONLY:
-                               $wgOut->readOnlyPage( $this->getContent() );
-                               break;
-                       case self::BAD_TOKEN:
-                               $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
-                               $wgOut->addWikiText( wfMsg( 'sessionfailure' ) );
-                               break;
-                       case self::BAD_TITLE:
-                               $wgOut->addHtml( wfMsg( 'notanarticle' ) );
-                               break;
-                       case self::ALREADY_ROLLED:
-                               $current = $details['current'];
-                               $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
-                               $wgOut->addWikiText(
-                                       wfMsg( 'alreadyrolled',
-                                               htmlspecialchars( $this->mTitle->getPrefixedText() ),
-                                               htmlspecialchars( $wgRequest->getVal( 'from' ) ),
-                                               htmlspecialchars( $current->getUserText() )
-                                       )
-                               );
-                               if( $current->getComment() != '' ) {
-                                       $wgOut->addHtml( wfMsg( 'editcomment',
-                                               $wgUser->getSkin()->formatComment( $current->getComment() ) ) );
+               if( in_array( array( 'blocked' ), $result ) ) {
+                       $wgOut->blockedPage();
+                       return;
+               }
+               if( in_array( array( 'actionthrottledtext' ), $result ) ) {
+                       $wgOut->rateLimited();
+                       return;
+               }
+               # Display permissions errors before read-only message -- there's no
+               # point in misleading the user into thinking the inability to rollback
+               # is only temporary.
+               if( !empty($result) && $result !== array( array('readonlytext') ) ) {
+                       # array_diff is completely broken for arrays of arrays, sigh.  Re-
+                       # move any 'readonlytext' error manually.
+                       $out = array();
+                       foreach( $result as $error ) {
+                               if( $error != array( 'readonlytext' ) ) {
+                                       $out []= $error;
                                }
-                               break;
-                       case self::ONLY_AUTHOR:
-                               $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
-                               $wgOut->addHtml( wfMsg( 'cantrollback' ) );
-                               break;
-                       case self::RATE_LIMITED:
-                               $wgOut->rateLimited();
-                               break;
-                       case self::SUCCESS:
-                               $current = $details['current'];
-                               $target = $details['target'];
-                               $wgOut->setPageTitle( wfMsg( 'actioncomplete' ) );
-                               $wgOut->setRobotPolicy( 'noindex,nofollow' );
-                               $old = $wgUser->getSkin()->userLink( $current->getUser(), $current->getUserText() )
-                                       . $wgUser->getSkin()->userToolLinks( $current->getUser(), $current->getUserText() );
-                               $new = $wgUser->getSkin()->userLink( $target->getUser(), $target->getUserText() )
-                                       . $wgUser->getSkin()->userToolLinks( $target->getUser(), $target->getUserText() );
-                               $wgOut->addHtml( wfMsgExt( 'rollback-success', array( 'parse', 'replaceafter' ), $old, $new ) );
-                               $wgOut->returnToMain( false, $this->mTitle );
-                               break;
-                       default:
-                               throw new MWException( __METHOD__ . ": Unknown return value `{$result}`" );
+                       }
+                       $wgOut->showPermissionsErrorPage( $out );
+                       return;
+               }
+               if( $result == array( array('readonlytext') ) ) {
+                       $wgOut->readOnlyPage();
+                       return;
                }
 
+               $current = $details['current'];
+               $target = $details['target'];
+               $wgOut->setPageTitle( wfMsg( 'actioncomplete' ) );
+               $wgOut->setRobotPolicy( 'noindex,nofollow' );
+               $old = $wgUser->getSkin()->userLink( $current->getUser(), $current->getUserText() )
+                       . $wgUser->getSkin()->userToolLinks( $current->getUser(), $current->getUserText() );
+               $new = $wgUser->getSkin()->userLink( $target->getUser(), $target->getUserText() )
+                       . $wgUser->getSkin()->userToolLinks( $target->getUser(), $target->getUserText() );
+               $wgOut->addHtml( wfMsgExt( 'rollback-success', array( 'parse', 'replaceafter' ), $old, $new ) );
+               $wgOut->returnToMain( false, $this->mTitle );
        }
 
 
@@ -2521,7 +2522,7 @@ class Article {
         * @param $changed Whether or not the content actually changed
         */
        function editUpdates( $text, $summary, $minoredit, $timestamp_of_pagechange, $newid, $changed = true ) {
-               global $wgDeferredUpdateList, $wgMessageCache, $wgUser, $wgParser;
+               global $wgDeferredUpdateList, $wgMessageCache, $wgUser, $wgParser, $wgEnableParserCache;
 
                wfProfileIn( __METHOD__ );
 
@@ -2536,8 +2537,10 @@ class Article {
                }
 
                # Save it to the parser cache
-               $parserCache =& ParserCache::singleton();
-               $parserCache->save( $editInfo->output, $this, $wgUser );
+               if ( $wgEnableParserCache ) {
+                       $parserCache =& ParserCache::singleton();
+                       $parserCache->save( $editInfo->output, $this, $wgUser );
+               }
 
                # Update the links tables
                $u = new LinksUpdate( $this->mTitle, $editInfo->output );
@@ -2894,6 +2897,7 @@ class Article {
 
                $title->touchLinks();
                $title->purgeSquid();
+               $title->deleteTitleProtection();
        }
 
        static function onArticleDelete( $title ) {
@@ -2911,6 +2915,10 @@ class Article {
                if( $title->getNamespace() == NS_MEDIAWIKI) {
                        $wgMessageCache->replace( $title->getDBkey(), false );
                }
+               if( $title->getNamespace() == NS_IMAGE ) {
+                       $update = new HTMLCacheUpdate( $title, 'imagelinks' );
+                       $update->doUpdate();
+               }
        }
 
        /**
@@ -2920,9 +2928,11 @@ class Article {
                global $wgDeferredUpdateList, $wgUseFileCache;
 
                // Invalidate caches of articles which include this page
-               $update = new HTMLCacheUpdate( $title, 'templatelinks' );
-               $wgDeferredUpdateList[] = $update;
+               $wgDeferredUpdateList[] = new HTMLCacheUpdate( $title, 'templatelinks' );
 
+               // Invalidate the caches of all pages which redirect here
+               $wgDeferredUpdateList[] = new HTMLCacheUpdate( $title, 'redirect' );
+               
                # Purge squid for this page only
                $title->purgeSquid();
 
@@ -3061,6 +3071,36 @@ class Article {
                return $result;
        }
 
+       /**
+        * Returns a list of hidden categories this page is a member of.
+        * Uses the page_props and categorylinks tables.
+        *
+        * @return array Array of Title objects
+        */
+       function getHiddenCategories() {
+               $result = array();
+               $id = $this->mTitle->getArticleID();
+               if( $id == 0 ) {
+                       return array();
+               }
+
+               $dbr = wfGetDB( DB_SLAVE );
+               $res = $dbr->select( array( 'categorylinks', 'page_props', 'page' ),
+                       array( 'cl_to' ),
+                       array( 'cl_from' => $id, 'pp_page=page_id', 'pp_propname' => 'hiddencat', 
+                               'page_namespace' => NS_CATEGORY, 'page_title=cl_to'),
+                       'Article:getHiddenCategories' );
+               if ( false !== $res ) {
+                       if ( $dbr->numRows( $res ) ) {
+                               while ( $row = $dbr->fetchObject( $res ) ) {
+                                       $result[] = Title::makeTitle( NS_CATEGORY, $row->cl_to );
+                               }
+                       }
+               }
+               $dbr->freeResult( $res );
+               return $result;
+       }
+
        /**
         * Return an auto-generated summary if the text provided is a redirect.
         *
@@ -3147,7 +3187,7 @@ class Article {
         * @param bool    $cache
         */
        public function outputWikiText( $text, $cache = true ) {
-               global $wgParser, $wgUser, $wgOut;
+               global $wgParser, $wgUser, $wgOut, $wgEnableParserCache;
 
                $popts = $wgOut->parserOptions();
                $popts->setTidy(true);
@@ -3156,7 +3196,7 @@ class Article {
                        $popts, true, true, $this->getRevIdFetched() );
                $popts->setTidy(false);
                $popts->enableLimitReport( false );
-               if ( $cache && $this && $parserOutput->getCacheTime() != -1 ) {
+               if ( $wgEnableParserCache && $cache && $this && $parserOutput->getCacheTime() != -1 ) {
                        $parserCache =& ParserCache::singleton();
                        $parserCache->save( $parserOutput, $this, $wgUser );
                }