* (bug 12574) Allow bots to specify whether an edit should be marked as a bot
[lhc/web/wiklou.git] / includes / Article.php
index 89dffe6..bca4461 100644 (file)
@@ -47,6 +47,7 @@ class Article {
        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
@@ -135,6 +136,7 @@ class Article {
                $this->mRevIdFetched = 0;
                $this->mRedirectUrl = false;
                $this->mLatest = false;
+               $this->mPreparedEdit = false;
        }
 
        /**
@@ -269,13 +271,16 @@ class Article {
                                'page_random',
                                'page_touched',
                                'page_latest',
-                               'page_len' ) ;
-               wfRunHooks( 'ArticlePageDataBefore', array( &$this , &$fields ) )       ;
-               $row = $dbr->selectRow( 'page',
+                               'page_len',
+               );
+               wfRunHooks( 'ArticlePageDataBefore', array( &$this, &$fields ) );
+               $row = $dbr->selectRow(
+                       'page',
                        $fields,
                        $conditions,
-                       'Article::pageData' );
-               wfRunHooks( 'ArticlePageDataAfter', array( &$this , &$row ) )   ;
+                       __METHOD__
+               );
+               wfRunHooks( 'ArticlePageDataAfter', array( &$this, &$row ) );
                return $row ;
        }
 
@@ -619,7 +624,7 @@ class Article {
        */
        function view() {
                global $wgUser, $wgOut, $wgRequest, $wgContLang;
-               global $wgEnableParserCache, $wgStylePath, $wgUseRCPatrol, $wgParser;
+               global $wgEnableParserCache, $wgStylePath, $wgParser;
                global $wgUseTrackbacks, $wgNamespaceRobotPolicies, $wgArticleRobotPolicies;
                $sk = $wgUser->getSkin();
 
@@ -642,6 +647,7 @@ class Article {
                $rcid = $wgRequest->getVal( 'rcid' );
                $rdfrom = $wgRequest->getVal( 'rdfrom' );
                $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
+               $purge = $wgRequest->getVal( 'action' ) == 'purge';
 
                $wgOut->setArticleFlag( true );
 
@@ -665,7 +671,7 @@ class Article {
                if ( !is_null( $diff ) ) {
                        $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
 
-                       $de = new DifferenceEngine( $this->mTitle, $oldid, $diff, $rcid );
+                       $de = new DifferenceEngine( $this->mTitle, $oldid, $diff, $rcid, $purge );
                        // DifferenceEngine directly fetched the revision:
                        $this->mRevIdFetched = $de->mNewid;
                        $de->showDiffPage( $diffOnly );
@@ -799,6 +805,7 @@ class Article {
                                // Give hooks a chance to customise the output
                                if( wfRunHooks( 'ShowRawCssJs', array( $this->mContent, $this->mTitle, $wgOut ) ) ) {
                                        // Wrap the whole lot in a <pre> and don't parse
+                                       $m = array();
                                        preg_match( '!\.(css|js)$!u', $this->mTitle->getText(), $m );
                                        $wgOut->addHtml( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
                                        $wgOut->addHtml( htmlspecialchars( $this->mContent ) );
@@ -863,7 +870,7 @@ class Article {
 
                # If we have been passed an &rcid= parameter, we want to give the user a
                # chance to mark this new article as patrolled.
-               if ( $wgUseRCPatrol && !is_null( $rcid ) && $rcid != 0 && $wgUser->isAllowed( 'patrol' ) ) {
+               if( !is_null( $rcid ) && $rcid != 0 && $wgUser->isAllowed( 'patrol' ) && $this->mTitle->exists() ) {
                        $wgOut->addHTML(
                                "<div class='patrollink'>" .
                                        wfMsgHtml( 'markaspatrolledlink',
@@ -921,13 +928,11 @@ class Article {
                        return;
                }
 
-               if ((!$wgUser->isAllowed('delete'))) {
-                       $wgOut->permissionRequired( 'delete' );
-                       return;
-               }
+               $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgUser );
 
-               if (wfReadOnly()) {
-                       $wgOut->readOnlyPage();
+               if (count($permission_errors)>0)
+               {
+                       $wgOut->showPermissionsErrorPage( $permission_errors );
                        return;
                }
 
@@ -956,7 +961,7 @@ class Article {
                        }
                } else {
                        $msg = $wgOut->parse( wfMsg( 'confirm_purge' ) );
-                       $action = $this->mTitle->escapeLocalURL( 'action=purge' );
+                       $action = htmlspecialchars( $_SERVER['REQUEST_URI'] );
                        $button = htmlspecialchars( wfMsg( 'confirm_purge_button' ) );
                        $msg = str_replace( '$1',
                                "<form method=\"post\" action=\"$action\">\n" .
@@ -1177,7 +1182,7 @@ class Article {
 
                        if( $section == 'new' ) {
                                # Inserting a new section
-                               $subject = $summary ? "== {$summary} ==\n\n" : '';
+                               $subject = $summary ? wfMsgForContent('newsectionheaderdefaultlevel',$summary) . "\n\n" : '';
                                $text = strlen( trim( $oldtext ) ) > 0
                                                ? "{$oldtext}\n\n{$subject}{$text}"
                                                : "{$subject}{$text}";
@@ -1203,7 +1208,7 @@ class Article {
 
                # If this is a comment, add the summary as headline
                if ( $comment && $summary != "" ) {
-                       $text = "== {$summary} ==\n\n".$text;
+                       $text = wfMsgForContent('newsectionheaderdefaultlevel',$summary) . "\n\n".$text;
                }
 
                $this->doEdit( $text, $summary, $flags );
@@ -1250,7 +1255,10 @@ class Article {
                                }
                        }
 
-                       $this->doRedirect( $this->isRedirect( $text ), $sectionanchor );
+                       $extraq = ''; // Give extensions a chance to modify URL query on update
+                       wfRunHooks( 'ArticleUpdateBeforeRedirect', array( $this, &$sectionanchor, &$extraq ) );
+
+                       $this->doRedirect( $this->isRedirect( $text ), $sectionanchor, $extraq );
                }
                return $good;
        }
@@ -1290,7 +1298,7 @@ class Article {
         * @return bool success
         */
        function doEdit( $text, $summary, $flags = 0 ) {
-               global $wgUser, $wgDBtransactions;
+               global $wgUser, $wgDBtransactions, $wgRequest;
 
                wfProfileIn( __METHOD__ );
                $good = true;
@@ -1315,7 +1323,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 = ( $wgUser->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot' , true ) : 0 ) || ( $flags & EDIT_FORCE_BOT );
 
                $oldtext = $this->getContent();
                $oldsize = strlen( $oldtext );
@@ -1324,7 +1332,8 @@ class Article {
                if ($flags & EDIT_AUTOSUMMARY && $summary == '')
                        $summary = $this->getAutosummary( $oldtext, $text, $flags );
 
-               $text = $this->preSaveTransform( $text );
+               $editInfo = $this->prepareTextForEdit( $text );
+               $text = $editInfo->pst;
                $newsize = strlen( $text );
 
                $dbw = wfGetDB( DB_MASTER );
@@ -1340,8 +1349,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;
@@ -1390,6 +1401,7 @@ class Article {
                                        $dbw->commit();
                                }
                        } else {
+                               $revision = null;
                                // Keep the same revision ID, but do some updates on it
                                $revisionId = $this->getRevIdFetched();
                                // Update page_touched, this is usually implicit in the page update
@@ -1405,9 +1417,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 {
@@ -1443,7 +1454,7 @@ class Article {
                                $rcid = RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary, $bot,
                                  '', strlen( $text ), $revisionId );
                                # Mark as patrolled if the user can
-                               if( $GLOBALS['wgUseRCPatrol'] && $wgUser->isAllowed( 'autopatrol' ) ) {
+                               if( ($GLOBALS['wgUseRCPatrol'] || $GLOBALS['wgUseNPPatrol']) && $wgUser->isAllowed( 'autopatrol' ) ) {
                                        RecentChange::markPatrolled( $rcid );
                                        PatrolLog::record( $rcid, true );
                                }
@@ -1457,19 +1468,18 @@ class Article {
                        # Clear caches
                        Article::onArticleCreate( $this->mTitle );
 
-                       wfRunHooks( 'ArticleInsertComplete', array( &$this, &$wgUser, $text,
-                               $summary, $flags & EDIT_MINOR,
-                               null, null, &$flags ) );
+                       wfRunHooks( 'ArticleInsertComplete', array( &$this, &$wgUser, $text, $summary,
+                        $flags & EDIT_MINOR, null, null, &$flags, $revision ) );
                }
 
                if ( $good && !( $flags & EDIT_DEFER_UPDATES ) ) {
                        wfDoUpdates();
                }
 
-               wfRunHooks( 'ArticleSaveComplete',
-                       array( &$this, &$wgUser, $text,
-                       $summary, $flags & EDIT_MINOR,
-                       null, null, &$flags ) );
+               if ( $good ) {
+                       wfRunHooks( 'ArticleSaveComplete', array( &$this, &$wgUser, $text, $summary,
+                               $flags & EDIT_MINOR, null, null, &$flags, $revision ) );
+               }
 
                wfProfileOut( __METHOD__ );
                return $good;
@@ -1489,39 +1499,55 @@ class Article {
         * @param boolean $noRedir Add redirect=no
         * @param string $sectionAnchor section to redirect to, including "#"
         */
-       function doRedirect( $noRedir = false, $sectionAnchor = '' ) {
+       function doRedirect( $noRedir = false, $sectionAnchor = '', $extraq = '' ) {
                global $wgOut;
                if ( $noRedir ) {
                        $query = 'redirect=no';
+                       if( $extraq )
+                               $query .= "&$query";
                } else {
-                       $query = '';
+                       $query = $extraq;
                }
                $wgOut->redirect( $this->mTitle->getFullURL( $query ) . $sectionAnchor );
        }
 
        /**
-        * Mark this particular edit as patrolled
+        * Mark this particular edit/page as patrolled
         */
        function markpatrolled() {
-               global $wgOut, $wgRequest, $wgUseRCPatrol, $wgUser;
+               global $wgOut, $wgRequest, $wgUseRCPatrol, $wgUseNPPatrol, $wgUser;
                $wgOut->setRobotPolicy( 'noindex,nofollow' );
 
-               # Check RC patrol config. option
-               if( !$wgUseRCPatrol ) {
+               # Check patrol config options
+
+               if ( !($wgUseNPPatrol || $wgUseRCPatrol)) {
                        $wgOut->errorPage( 'rcpatroldisabled', 'rcpatroldisabledtext' );
+                       return;         
+               }
+
+               # If we haven't been given an rc_id value, we can't do anything
+               $rcid = (int) $wgRequest->getVal('rcid');
+               $rc = $rcid ? RecentChange::newFromId($rcid) : null;
+               if ( is_null ( $rc ) )
+               {
+                       $wgOut->errorPage( 'markedaspatrollederror', 'markedaspatrollederrortext' );
                        return;
                }
 
-               # Check permissions
-               if( !$wgUser->isAllowed( 'patrol' ) ) {
-                       $wgOut->permissionRequired( 'patrol' );
+               if ( !$wgUseRCPatrol && $rc->mAttribs['rc_type'] != RC_NEW) {
+                       // Only new pages can be patrolled if the general patrolling is off....???
+                       // @fixme -- is this necessary? Shouldn't we only bother controlling the
+                       // front end here?
+                       $wgOut->errorPage( 'rcpatroldisabled', 'rcpatroldisabledtext' );
                        return;
                }
+               
+               # Check permissions
+               $permission_errors = $this->mTitle->getUserPermissionsErrors( 'patrol', $wgUser );
 
-               # If we haven't been given an rc_id value, we can't do anything
-               $rcid = $wgRequest->getVal( 'rcid' );
-               if( !$rcid ) {
-                       $wgOut->errorPage( 'markedaspatrollederror', 'markedaspatrollederrortext' );
+               if (count($permission_errors)>0)
+               {
+                       $wgOut->showPermissionsErrorPage( $permission_errors );
                        return;
                }
 
@@ -1530,7 +1556,10 @@ class Article {
                        return;
                }
 
-               $return = SpecialPage::getTitleFor( 'Recentchanges' );
+               #It would be nice to see where the user had actually come from, but for now just guess
+               $returnto = $rc->mAttribs['rc_type'] == RC_NEW ? 'Newpages' : 'Recentchanges';
+               $return = Title::makeTitle( NS_SPECIAL, $returnto );
+
                # If it's left up to us, check that the user is allowed to patrol this edit
                # If the user has the "autopatrol" right, then we'll assume there are no
                # other conditions stopping them doing so
@@ -1681,7 +1710,7 @@ class Article {
                global $wgUser, $wgRestrictionTypes, $wgContLang;
 
                $id = $this->mTitle->getArticleID();
-               if( !$wgUser->isAllowed( 'protect' ) || wfReadOnly() || $id == 0 ) {
+               if( array() != $this->mTitle->getUserPermissionsErrors( 'protect', $wgUser ) || wfReadOnly() || $id == 0 ) {
                        return false;
                }
 
@@ -1812,31 +1841,111 @@ class Article {
                }
                return implode( ':', $bits );
        }
+       
+       /**
+        * Auto-generates a deletion reason
+        * @param bool &$hasHistory Whether the page has a history
+        */
+       public function generateReason(&$hasHistory)
+       {
+               global $wgContLang;
+               $dbw = wfGetDB(DB_MASTER);
+               // Get the last revision
+               $rev = Revision::newFromTitle($this->mTitle);
+               if(is_null($rev))
+                       return false;
+               // Get the article's contents
+               $contents = $rev->getText();
+               $blank = false;
+               // If the page is blank, use the text from the previous revision,
+               // which can only be blank if there's a move/import/protect dummy revision involved
+               if($contents == '')
+               {
+                       $prev = $rev->getPrevious();
+                       if($prev)
+                       {
+                               $contents = $prev->getText();
+                               $blank = true;
+                       }
+               }
+
+               // Find out if there was only one contributor
+               // Only scan the last 20 revisions
+               $limit = 20;
+               $res = $dbw->select('revision', 'rev_user_text', array('rev_page' => $this->getID()), __METHOD__,
+                               array('LIMIT' => $limit));
+               if($res === false)
+                       // This page has no revisions, which is very weird
+                       return false;
+               if($res->numRows() > 1)
+                               $hasHistory = true;
+               else
+                               $hasHistory = false;
+               $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) {
+                               $onlyAuthor = false;
+                               break;
+                       }
+               }
+               $dbw->freeResult($res);
+
+               // Generate the summary with a '$1' placeholder
+               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 {
+                       if($onlyAuthor)
+                               $reason = wfMsgForContent('excontentauthor', '$1', $onlyAuthor);
+                       else
+                               $reason = wfMsgForContent('excontent', '$1');
+               }
+               
+               // Replace newlines with spaces to prevent uglyness
+               $contents = preg_replace("/[\n\r]/", ' ', $contents);
+               // Calculate the maximum amount of chars to get
+               // Max content length = max comment length - length of the comment (excl. $1) - '...'
+               $maxLength = 255 - (strlen($reason) - 2) - 3;
+               $contents = $wgContLang->truncate($contents, $maxLength, '...');
+               // Remove possible unfinished links
+               $contents = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $contents );
+               // Now replace the '$1' placeholder
+               $reason = str_replace( '$1', $contents, $reason );
+               return $reason;
+       }
+
 
        /*
         * UI entry point for page deletion
         */
        function delete() {
                global $wgUser, $wgOut, $wgRequest;
+
                $confirm = $wgRequest->wasPosted() &&
-                       $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
-               $reason = $wgRequest->getText( 'wpReason' );
+                               $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
+               
+               $this->DeleteReasonList = $wgRequest->getText( 'wpDeleteReasonList', 'other' );
+               $this->DeleteReason = $wgRequest->getText( 'wpReason' );
+               
+               $reason = $this->DeleteReasonList;
+               
+               if ( $reason != 'other' && $this->DeleteReason != '') {
+                       // Entry from drop down menu + additional comment
+                       $reason .= ': ' . $this->DeleteReason;
+               } elseif ( $reason == 'other' ) {
+                       $reason = $this->DeleteReason;
+               }
 
                # This code desperately needs to be totally rewritten
 
                # Check permissions
-               if( $wgUser->isAllowed( 'delete' ) ) {
-                       if( $wgUser->isBlocked( !$confirm ) ) {
-                               $wgOut->blockedPage();
-                               return;
-                       }
-               } else {
-                       $wgOut->permissionRequired( 'delete' );
-                       return;
-               }
+               $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgUser );
 
-               if( wfReadOnly() ) {
-                       $wgOut->readOnlyPage();
+               if (count($permission_errors)>0) {
+                       $wgOut->showPermissionsErrorPage( $permission_errors );
                        return;
                }
 
@@ -1861,75 +1970,16 @@ class Article {
                        return;
                }
 
-               # determine whether this page has earlier revisions
-               # and insert a warning if it does
-               $maxRevisions = 20;
-               $authors = $this->getLastNAuthors( $maxRevisions, $latest );
+               // Generate deletion reason
+               $hasHistory = false;
+               if ( !$reason ) $reason = $this->generateReason($hasHistory);
 
-               if( count( $authors ) > 1 && !$confirm ) {
+               // If the page has a history, insert a warning
+               if( $hasHistory && !$confirm ) {
                        $skin=$wgUser->getSkin();
                        $wgOut->addHTML( '<strong>' . wfMsg( 'historywarning' ) . ' ' . $skin->historyLink() . '</strong>' );
                }
-
-               # If a single user is responsible for all revisions, find out who they are
-               if ( count( $authors ) == $maxRevisions ) {
-                       // Query bailed out, too many revisions to find out if they're all the same
-                       $authorOfAll = false;
-               } else {
-                       $authorOfAll = reset( $authors );
-                       foreach ( $authors as $author ) {
-                               if ( $authorOfAll != $author ) {
-                                       $authorOfAll = false;
-                                       break;
-                               }
-                       }
-               }
-               # Fetch article text
-               $rev = Revision::newFromTitle( $this->mTitle );
-
-               if( !is_null( $rev ) ) {
-                       # if this is a mini-text, we can paste part of it into the deletion reason
-                       $text = $rev->getText();
-
-                       #if this is empty, an earlier revision may contain "useful" text
-                       $blanked = false;
-                       if( $text == '' ) {
-                               $prev = $rev->getPrevious();
-                               if( $prev ) {
-                                       $text = $prev->getText();
-                                       $blanked = true;
-                               }
-                       }
-
-                       $length = strlen( $text );
-
-                       # this should not happen, since it is not possible to store an empty, new
-                       # page. Let's insert a standard text in case it does, though
-                       if( $length == 0 && $reason === '' ) {
-                               $reason = wfMsgForContent( 'exblank' );
-                       }
-
-                       if( $reason === '' ) {
-                               # comment field=255, let's grep the first 150 to have some user
-                               # space left
-                               global $wgContLang;
-                               $text = $wgContLang->truncate( $text, 150, '...' );
-
-                               # let's strip out newlines
-                               $text = preg_replace( "/[\n\r]/", '', $text );
-
-                               if( !$blanked ) {
-                                       if( $authorOfAll === false ) {
-                                               $reason = wfMsgForContent( 'excontent', $text );
-                                       } else {
-                                               $reason = wfMsgForContent( 'excontentauthor', $text, $authorOfAll );
-                                       }
-                               } else {
-                                       $reason = wfMsgForContent( 'exbeforeblank', $text );
-                               }
-                       }
-               }
-
+               
                return $this->confirmDelete( '', $reason );
        }
 
@@ -1995,19 +2045,64 @@ class Article {
                $formaction = $this->mTitle->escapeLocalURL( 'action=delete' . $par );
 
                $confirm = htmlspecialchars( wfMsg( 'deletepage' ) );
-               $delcom = htmlspecialchars( wfMsg( 'deletecomment' ) );
+               $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>
+               <tr id=\"wpDeleteReasonListRow\" name=\"wpDeleteReasonListRow\">
                        <td align='right'>
-                               <label for='wpReason'>{$delcom}:</label>
+                               $delcom:
+                       </td>
+                       <td align='left'>
+                               <select tabindex='1' id='wpDeleteReasonList' name=\"wpDeleteReasonList\">
+                                       $deleteReasonList
+                               </select>
+                       </td>
+               </tr>
+               <tr id=\"wpDeleteReasonRow\" name=\"wpDeleteReasonRow\">
+                       <td>
+                               $mDeletereasonother
                        </td>
                        <td align='left'>
-                               <input type='text' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" tabindex=\"1\" />
+                               <input type='text' maxlength='255' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" tabindex=\"2\" />
                        </td>
                </tr>
                <tr>
@@ -2024,7 +2119,7 @@ class Article {
        <input type='hidden' name='wpEditToken' value=\"{$token}\" />
 </form>\n" );
 
-               $wgOut->returnToMain( false );
+               $wgOut->returnToMain( false, $this->mTitle );
 
                $this->showLogExtract( $wgOut );
        }
@@ -2116,7 +2211,8 @@ class Article {
                                'ar_text_id'    => 'rev_text_id',
                                'ar_text'       => '\'\'', // Be explicit to appease
                                'ar_flags'      => '\'\'', // MySQL's "strict mode"...
-                               'ar_len'                => 'rev_len'
+                               'ar_len'                => 'rev_len',
+                               'ar_page_id'    => 'page_id',
                        ), array(
                                'page_id' => $id,
                                'page_id = rev_page'
@@ -2167,7 +2263,11 @@ class Article {
                return true;
        }
 
-       /** Backend rollback implementation. UI logic is in rollback()
+       /**
+        * 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
+        *
         * @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.
@@ -2179,12 +2279,13 @@ class Article {
         * 
         * @return self::SUCCESS on succes, self::* on failure
         */
-       public function doRollback($fromP, $summary, $token, $bot, &$resultDetails) {
-               global $wgUser, $wgUseRCPatrol;
-               
+       public function doRollback( $fromP, $summary, $token, $bot, &$resultDetails ) {
+               global $wgUser, $wgUseRCPatrol, $wgRequest;
                $resultDetails = null;
-               
-               if( $wgUser->isAllowed( 'rollback' ) ) {
+
+               # Just in case it's being called from elsewhere         
+
+               if( $wgUser->isAllowed( 'rollback' ) && $this->mTitle->userCan( 'edit' ) ) {
                        if( $wgUser->isBlocked() ) {
                                return self::BLOCKED;
                        }
@@ -2195,13 +2296,15 @@ class Article {
                if ( wfReadOnly() ) {
                        return self::READONLY;
                }
-               if( !$wgUser->matchEditToken( $token,
-                       array( $this->mTitle->getPrefixedText(), $fromP ))) {
+
+               if( !$wgUser->matchEditToken( $token, array( $this->mTitle->getPrefixedText(), $fromP ) ) )
                        return self::BAD_TOKEN;
+
+               if ( $wgUser->pingLimiter('rollback') || $wgUser->pingLimiter() ) {
+                       return self::RATE_LIMITED;
                }
-               $dbw = wfGetDB( DB_MASTER );
 
-               # Replace all this user's current edits with the next one down
+               $dbw = wfGetDB( DB_MASTER );
 
                # Get the last editor
                $current = Revision::newFromTitle( $this->mTitle );
@@ -2212,7 +2315,7 @@ class Article {
 
                $from = str_replace( '_', ' ', $fromP );
                if( $from != $current->getUserText() ) {
-                       $resultDetails = array('current' => $current);
+                       $resultDetails = array( 'current' => $current );
                        return self::ALREADY_ROLLED;
                }
 
@@ -2235,7 +2338,7 @@ class Article {
                }
        
                $set = array();
-               if ( $bot ) {
+               if ( $bot && $wgUser->isAllowed('markbotedits') ) {
                        # Mark all reverted edits as bot
                        $set['rc_bot'] = 1;
                }
@@ -2256,40 +2359,56 @@ class Article {
 
                # Get the edit summary
                $target = Revision::newFromId( $s->rev_id );
-               if (empty($summary))
+               if( empty( $summary ) )
                        $summary = wfMsgForContent( 'revertpage', $target->getUserText(), $from );
 
                # Save
-               $flags = EDIT_UPDATE | EDIT_MINOR;
-               if( $bot )
+               $flags = EDIT_UPDATE;
+
+               if ($wgUser->isAllowed('minoredit'))
+                       $flags |= EDIT_MINOR;
+
+               if( $bot && $wgRequest->getBool( 'bot' , true ) )
                        $flags |= EDIT_FORCE_BOT;
                $this->doEdit( $target->getText(), $summary, $flags );
 
+               wfRunHooks( 'ArticleRollbackComplete', array( $this, $wgUser, $target ) );
+
                $resultDetails = array(
                        'summary' => $summary,
                        'current' => $current,
-                       'target' => $target);
+                       'target' => $target,
+               );
                return self::SUCCESS;
        }
 
-       /** UI entry point for rollbacks. Relies on doRollback() to do the hard work */
+       /**
+        * User interface for rollback operations
+        */
        function rollback() {
                global $wgUser, $wgOut, $wgRequest, $wgUseRCPatrol;
 
-               // Call doRollback() and interpret its return value
-               $resultDetails = null;
-               $retval = $this->doRollback(
-                       $wgRequest->getVal('from'),
-                       $wgRequest->getText('summary'),
-                       $wgRequest->getVal('token'),
-                       $wgRequest->getBool('bot'),
-                       &$resultDetails);
-
-               switch($retval)
-               {
-                       default:
-                               throw new MWException( "Unknown retval $retval" );
-                               break;
+               $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' ),
+                       $wgRequest->getVal( 'token' ),
+                       $wgRequest->getBool( 'bot' ),
+                       $details
+               );
+
+               switch( $result ) {
                        case self::BLOCKED:
                                $wgOut->blockedPage();
                                break;
@@ -2304,30 +2423,33 @@ class Article {
                                $wgOut->addWikiText( wfMsg( 'sessionfailure' ) );
                                break;
                        case self::BAD_TITLE:
-                               $wgOut->addHTML( wfMsg( 'notanarticle' ) );
+                               $wgOut->addHtml( wfMsg( 'notanarticle' ) );
                                break;
                        case self::ALREADY_ROLLED:
-                               $current = $resultDetails['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',
+                               $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() ) ) );
                                }
                                break;
                        case self::ONLY_AUTHOR:
-                               $wgOut->setPageTitle(wfMsg('rollbackfailed'));
-                               $wgOut->addHTML( wfMsg( 'cantrollback' ) );
+                               $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
+                               $wgOut->addHtml( wfMsg( 'cantrollback' ) );
+                               break;
+                       case self::RATE_LIMITED:
+                               $wgOut->rateLimited();
                                break;
                        case self::SUCCESS:
-                               # User feedback
-                               $current = $resultDetails['current'];
-                               $target = $resultDetails['target'];
-               
+                               $current = $details['current'];
+                               $target = $details['target'];
                                $wgOut->setPageTitle( wfMsg( 'actioncomplete' ) );
                                $wgOut->setRobotPolicy( 'noindex,nofollow' );
                                $old = $wgUser->getSkin()->userLink( $current->getUser(), $current->getUserText() )
@@ -2335,10 +2457,12 @@ class Article {
                                $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 );
+                               $wgOut->returnToMain( false, $this->mTitle );
                                break;
+                       default:
+                               throw new MWException( __METHOD__ . ": Unknown return value `{$result}`" );
                }
+
        }
 
 
@@ -2363,10 +2487,33 @@ class Article {
                $wgUser->clearNotification( $this->mTitle );
        }
 
+       /**
+        * Prepare text which is about to be saved.
+        * Returns a stdclass with source, pst and output members
+        */
+       function prepareTextForEdit( $text, $revid=null ) {
+               if ( $this->mPreparedEdit && $this->mPreparedEdit->newText == $text && $this->mPreparedEdit->revid == $revid) {
+                       // Already prepared
+                       return $this->mPreparedEdit;
+               }
+               global $wgParser;
+               $edit = (object)array();
+               $edit->revid = $revid;
+               $edit->newText = $text;
+               $edit->pst = $this->preSaveTransform( $text );
+               $options = new ParserOptions;
+               $options->setTidy( true );
+               $options->enableLimitReport();
+               $edit->output = $wgParser->parse( $edit->pst, $this->mTitle, $options, true, true, $revid );
+               $edit->oldText = $this->getContent();
+               $this->mPreparedEdit = $edit;
+               return $edit;
+       }
+
        /**
         * Do standard deferred updates after page edit.
         * Update links tables, site stats, search index and message cache.
-        * Every 1000th edit, prune the recent changes table.
+        * Every 100th edit, prune the recent changes table.
         *
         * @private
         * @param $text New text of the article
@@ -2382,24 +2529,28 @@ class Article {
                wfProfileIn( __METHOD__ );
 
                # Parse the text
-               $options = new ParserOptions;
-               $options->setTidy(true);
-               $poutput = $wgParser->parse( $text, $this->mTitle, $options, true, true, $newid );
+               # Be careful not to double-PST: $text is usually already PST-ed once
+               if ( !$this->mPreparedEdit || $this->mPreparedEdit->output->getFlag( 'vary-revision' ) ) {
+                       wfDebug( __METHOD__ . ": No prepared edit or vary-revision is set...\n" );
+                       $editInfo = $this->prepareTextForEdit( $text, $newid );
+               } else {
+                       wfDebug( __METHOD__ . ": No vary-revision, using prepared edit...\n" );
+                       $editInfo = $this->mPreparedEdit;
+               }
 
                # Save it to the parser cache
                $parserCache =& ParserCache::singleton();
-               $parserCache->save( $poutput, $this, $wgUser );
+               $parserCache->save( $editInfo->output, $this, $wgUser );
 
                # Update the links tables
-               $u = new LinksUpdate( $this->mTitle, $poutput );
+               $u = new LinksUpdate( $this->mTitle, $editInfo->output );
                $u->doUpdate();
 
-               if ( wfRunHooks( 'ArticleEditUpdatesDeleteFromRecentchanges', array( &$this ) ) ) {
-                       wfSeedRandom();
+               if( wfRunHooks( 'ArticleEditUpdatesDeleteFromRecentchanges', array( &$this ) ) ) {
                        if ( 0 == mt_rand( 0, 99 ) ) {
-                               # Periodically flush old entries from the recentchanges table.
+                               // Flush old entries from the `recentchanges` table; we do this on
+                               // random requests so as to avoid an increase in writes for no good reason
                                global $wgRCMaxAge;
-
                                $dbw = wfGetDB( DB_MASTER );
                                $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
                                $recentchanges = $dbw->tableName( 'recentchanges' );
@@ -2746,6 +2897,7 @@ class Article {
 
                $title->touchLinks();
                $title->purgeSquid();
+               $title->deleteTitleProtection();
        }
 
        static function onArticleDelete( $title ) {
@@ -2804,16 +2956,22 @@ class Article {
                $page = $this->mTitle->getSubjectPage();
 
                $wgOut->setPagetitle( $page->getPrefixedText() );
-               $wgOut->setSubtitle( wfMsg( 'infosubtitle' ));
-
-               # first, see if the page exists at all.
-               $exists = $page->getArticleId() != 0;
-               if( !$exists ) {
-                       if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
-                               $wgOut->addHTML(wfMsgWeirdKey ( $this->mTitle->getText() ) );
+               $wgOut->setPageTitleActionText( wfMsg( 'info_short' ) );
+               $wgOut->setSubtitle( wfMsg( 'infosubtitle' ) );
+
+               if( !$this->mTitle->exists() ) {
+                       $wgOut->addHtml( '<div class="noarticletext">' );
+                       if( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
+                               // This doesn't quite make sense; the user is asking for
+                               // information about the _page_, not the message... -- RC
+                               $wgOut->addHtml( htmlspecialchars( wfMsgWeirdKey( $this->mTitle->getText() ) ) );
                        } else {
-                               $wgOut->addHTML(wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' ) );
+                               $msg = $wgUser->isLoggedIn()
+                                       ? 'noarticletext'
+                                       : 'noarticletextanon';
+                               $wgOut->addHtml( wfMsgExt( $msg, 'parse' ) );
                        }
+                       $wgOut->addHtml( '</div>' );
                } else {
                        $dbr = wfGetDB( DB_SLAVE );
                        $wl_clause = array(
@@ -2997,9 +3155,11 @@ class Article {
 
                $popts = $wgOut->parserOptions();
                $popts->setTidy(true);
+               $popts->enableLimitReport();
                $parserOutput = $wgParser->parse( $text, $this->mTitle,
                        $popts, true, true, $this->getRevIdFetched() );
                $popts->setTidy(false);
+               $popts->enableLimitReport( false );
                if ( $cache && $this && $parserOutput->getCacheTime() != -1 ) {
                        $parserCache =& ParserCache::singleton();
                        $parserCache->save( $parserOutput, $this, $wgUser );
@@ -3061,4 +3221,4 @@ class Article {
                $wgOut->addParserOutput( $parserOutput );
        }
 
-}
\ No newline at end of file
+}