* Fix WebRequest.php
[lhc/web/wiklou.git] / includes / Article.php
index 68e561a..d6809bf 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 /**
  * File for articles
+ * @file
  */
 
 /**
@@ -15,25 +16,30 @@ class Article {
        /**@{{
         * @private
         */
-       var $mComment;                  //!<
-       var $mContent;                  //!<
-       var $mContentLoaded;    //!<
-       var $mCounter;                  //!<
-       var $mForUpdate;                //!<
-       var $mGoodAdjustment;   //!<
-       var $mLatest;                   //!<
-       var $mMinorEdit;                //!<
-       var $mOldId;                    //!<
-       var $mRedirectedFrom;   //!<
-       var $mRedirectUrl;              //!<
-       var $mRevIdFetched;             //!<
-       var $mRevision;                 //!<
-       var $mTimestamp;                //!<
-       var $mTitle;                    //!<
-       var $mTotalAdjustment;  //!<
-       var $mTouched;                  //!<
-       var $mUser;                             //!<
-       var $mUserText;                 //!<
+       var $mComment = '';               //!<
+       var $mContent;                    //!<
+       var $mContentLoaded = false;      //!<
+       var $mCounter = -1;               //!< Not loaded
+       var $mCurID = -1;                 //!< Not loaded
+       var $mDataLoaded = false;         //!<
+       var $mForUpdate = false;          //!<
+       var $mGoodAdjustment = 0;         //!<
+       var $mIsRedirect = false;         //!<
+       var $mLatest = false;             //!<
+       var $mMinorEdit;                  //!<
+       var $mOldId;                      //!<
+       var $mPreparedEdit = false;       //!< Title object if set
+       var $mRedirectedFrom = null;      //!< Title object if set
+       var $mRedirectTarget = null;      //!< Title object if set
+       var $mRedirectUrl = false;        //!<
+       var $mRevIdFetched = 0;           //!<
+       var $mRevision;                   //!<
+       var $mTimestamp = '';             //!<
+       var $mTitle;                      //!<
+       var $mTotalAdjustment = 0;        //!<
+       var $mTouched = '19700101000000'; //!<
+       var $mUser = -1;                  //!< Not loaded
+       var $mUserText = '';              //!<
        /**@}}*/
 
        /**
@@ -44,7 +50,6 @@ class Article {
        function __construct( Title $title, $oldId = null ) {
                $this->mTitle =& $title;
                $this->mOldId = $oldId;
-               $this->clear();
        }
 
        /**
@@ -57,12 +62,69 @@ class Article {
        }
 
        /**
+        * If this page is a redirect, get its target
+        *
+        * The target will be fetched from the redirect table if possible.
+        * If this page doesn't have an entry there, call insertRedirect()
+        * @return mixed Title object, or null if this page is not a redirect
+        */
+       public function getRedirectTarget() {
+               if(!$this->mTitle || !$this->mTitle->isRedirect())
+                       return null;
+               if(!is_null($this->mRedirectTarget))
+                       return $this->mRedirectTarget;
+
+               # Query the redirect table
+               $dbr = wfGetDB(DB_SLAVE);
+               $res = $dbr->select('redirect',
+                               array('rd_namespace', 'rd_title'),
+                               array('rd_from' => $this->getID()),
+                               __METHOD__
+               );
+               $row = $dbr->fetchObject($res);
+               if($row)
+                       return $this->mRedirectTarget = Title::makeTitle($row->rd_namespace, $row->rd_title);
+
+               # This page doesn't have an entry in the redirect table
+               return $this->mRedirectTarget = $this->insertRedirect();
+       }
+
+       /**
+        * Insert an entry for this page into the redirect table.
+        *
+        * Don't call this function directly unless you know what you're doing.
+        * @return Title object
+        */
+       public function insertRedirect() {
+               $retval = Title::newFromRedirect($this->getContent());
+               if(!$retval)
+                       return null;
+               $dbw = wfGetDB(DB_MASTER);
+               $dbw->replace('redirect', array('rd_from'), array(
+                               'rd_from' => $this->getID(),
+                               'rd_namespace' => $retval->getNamespace(),
+                               'rd_title' => $retval->getDBKey()
+               ), __METHOD__);
+               return $retval;
+       }
+
+       /**
+        * Get the Title object this page redirects to
+        *
         * @return mixed false, Title of in-wiki target, or string with URL
         */
-       function followRedirect() {
+       public function followRedirect() {
                $text = $this->getContent();
+               return self::followRedirectText( $text );
+       }
+       
+       /**
+        * Get the Title object this text redirects to
+        *
+        * @return mixed false, Title of in-wiki target, or string with URL
+        */
+       public function followRedirectText( $text ) {
                $rt = Title::newFromRedirect( $text );
-
                # process if title object is valid and not special:userlogout
                if( $rt ) {
                        if( $rt->getInterwiki() != '' ) {
@@ -92,7 +154,6 @@ class Article {
                                return $rt;
                        }
                }
-
                // No or invalid redirect
                return false;
        }
@@ -114,6 +175,7 @@ class Article {
 
                $this->mCurID = $this->mUser = $this->mCounter = -1; # Not loaded
                $this->mRedirectedFrom = null; # Title object if set
+               $this->mRedirectTarget = null; # Title object if set
                $this->mUserText =
                $this->mTimestamp = $this->mComment = '';
                $this->mGoodAdjustment = $this->mTotalAdjustment = 0;
@@ -138,21 +200,22 @@ class Article {
         * @return Return the text of this revision
        */
        function getContent() {
-               global $wgUser, $wgOut;
+               global $wgUser, $wgOut, $wgMessageCache;
 
                wfProfileIn( __METHOD__ );
 
                if ( 0 == $this->getID() ) {
                        wfProfileOut( __METHOD__ );
-                       $wgOut->setRobotpolicy( 'noindex,nofollow' );
+                       $wgOut->setRobotPolicy( 'noindex,nofollow' );
 
                        if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
+                               $wgMessageCache->loadAllMessages();
                                $ret = wfMsgWeirdKey ( $this->mTitle->getText() ) ;
                        } else {
                                $ret = wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' );
                        }
 
-                       return "<div class='noarticletext'>$ret</div>";
+                       return "<div class='noarticletext'>\n$ret\n</div>";
                } else {
                        $this->loadContent();
                        wfProfileOut( __METHOD__ );
@@ -298,13 +361,13 @@ class Article {
         */
        function loadPageData( $data = 'fromdb' ) {
                if ( $data === 'fromdb' ) {
-                       $dbr = $this->getDB();
+                       $dbr = wfGetDB( DB_MASTER );
                        $data = $this->pageDataFromId( $dbr, $this->getId() );
                }
 
-               $lc =& LinkCache::singleton();
+               $lc = LinkCache::singleton();
                if ( $data ) {
-                       $lc->addGoodLinkObj( $data->page_id, $this->mTitle );
+                       $lc->addGoodLinkObj( $data->page_id, $this->mTitle, $data->page_len, $data->page_is_redirect );
 
                        $this->mTitle->mArticleID = $data->page_id;
 
@@ -336,15 +399,13 @@ class Article {
                        return $this->mContent;
                }
 
-               $dbr = $this->getDB();
+               $dbr = wfGetDB( DB_MASTER );
 
                # Pre-fill content with error message so that if something
                # fails we'll have something telling us what we intended.
                $t = $this->mTitle->getPrefixedText();
-               if( $oldid ) {
-                       $t .= ',oldid='.$oldid;
-               }
-               $this->mContent = wfMsg( 'missingarticle', $t ) ;
+               $d = $oldid ? wfMsgExt( 'missingarticle-rev', array( 'escape' ), $oldid ) : '';
+               $this->mContent = wfMsg( 'missing-article', $t, $d ) ;
 
                if( $oldid ) {
                        $revision = Revision::newFromId( $oldid );
@@ -377,15 +438,14 @@ class Article {
 
                // FIXME: Horrible, horrible! This content-loading interface just plain sucks.
                // We should instead work with the Revision object when we need it...
-               $this->mContent = $revision->userCan( Revision::DELETED_TEXT ) ? $revision->getRawText() : "";
-               //$this->mContent   = $revision->getText();
+               $this->mContent   = $revision->revText(); // Loads if user is allowed
 
                $this->mUser      = $revision->getUser();
                $this->mUserText  = $revision->getUserText();
                $this->mComment   = $revision->getComment();
                $this->mTimestamp = wfTimestamp( TS_MW, $revision->getTimestamp() );
 
-               $this->mRevIdFetched = $revision->getID();
+               $this->mRevIdFetched = $revision->getId();
                $this->mContentLoaded = true;
                $this->mRevision =& $revision;
 
@@ -407,8 +467,10 @@ class Article {
         * Get the database which should be used for reads
         *
         * @return Database
+        * @deprecated - just call wfGetDB( DB_MASTER ) instead
         */
        function getDB() {
+               wfDeprecated( __METHOD__ );
                return wfGetDB( DB_MASTER );
        }
 
@@ -490,6 +552,10 @@ class Article {
         */
        function isRedirect( $text = false ) {
                if ( $text === false ) {
+                       if ( $this->mDataLoaded ) 
+                               return $this->mIsRedirect;
+                       
+                       // Apparently loadPageData was never called
                        $this->loadContent();
                        $titleObj = Title::newFromRedirect( $this->fetchContent() );
                } else {
@@ -526,14 +592,14 @@ class Article {
                $id = $this->getID();
                if ( 0 == $id ) return;
 
-               $this->mLastRevision = Revision::loadFromPageId( $this->getDB(), $id );
+               $this->mLastRevision = Revision::loadFromPageId( wfGetDB( DB_MASTER ), $id );
                if( !is_null( $this->mLastRevision ) ) {
                        $this->mUser      = $this->mLastRevision->getUser();
                        $this->mUserText  = $this->mLastRevision->getUserText();
                        $this->mTimestamp = $this->mLastRevision->getTimestamp();
                        $this->mComment   = $this->mLastRevision->getComment();
                        $this->mMinorEdit = $this->mLastRevision->isMinor();
-                       $this->mRevIdFetched = $this->mLastRevision->getID();
+                       $this->mRevIdFetched = $this->mLastRevision->getId();
                }
        }
 
@@ -571,7 +637,6 @@ class Article {
        }
 
        /**
-        * @todo Document, fixme $offset never used.
         * @param $limit Integer: default 0.
         * @param $offset Integer: default 0.
         */
@@ -593,6 +658,8 @@ class Article {
                        ORDER BY timestamp DESC";
 
                if ($limit > 0) { $sql .= ' LIMIT '.$limit; }
+               if ($offset > 0) { $sql .= ' OFFSET '.$offset; }
+               
                $sql .= ' '. $this->getSelectOptions();
 
                $res = $dbr->query($sql, __METHOD__);
@@ -609,7 +676,7 @@ class Article {
         * This is the default action of the script: just view the page of
         * the given title.
        */
-       function view() {
+       function view() {
                global $wgUser, $wgOut, $wgRequest, $wgContLang;
                global $wgEnableParserCache, $wgStylePath, $wgParser;
                global $wgUseTrackbacks, $wgNamespaceRobotPolicies, $wgArticleRobotPolicies;
@@ -618,7 +685,7 @@ class Article {
 
                wfProfileIn( __METHOD__ );
 
-               $parserCache =& ParserCache::singleton();
+               $parserCache = ParserCache::singleton();
                $ns = $this->mTitle->getNamespace(); # shortcut
 
                # Get variables from query string
@@ -689,12 +756,7 @@ class Article {
                }
 
                # Should the parser cache be used?
-               $pcache = $wgEnableParserCache
-                       && intval( $wgUser->getOption( 'stubthreshold' ) ) == 0
-                       && $this->exists()
-                       && empty( $oldid )
-                       && !$this->mTitle->isCssOrJsPage()
-                       && !$this->mTitle->isCssJsSubpage();
+               $pcache = $this->useParserCache( $oldid );
                wfDebug( 'Article::view using parser cache: ' . ($pcache ? 'yes' : 'no' ) . "\n" );
                if ( $wgUser->getOption( 'stubthreshold' ) ) {
                        wfIncrStats( 'pcache_miss_stub' );
@@ -705,7 +767,6 @@ class Article {
                        // This is an internally redirected page view.
                        // We'll need a backlink to the source page for navigation.
                        if ( wfRunHooks( 'ArticleViewRedirect', array( &$this ) ) ) {
-                               $sk = $wgUser->getSkin();
                                $redir = $sk->makeKnownLinkObj( $this->mRedirectedFrom, '', 'redirect=no' );
                                $s = wfMsg( 'redirectedfrom', $redir );
                                $wgOut->setSubtitle( $s );
@@ -722,7 +783,6 @@ class Article {
                        // If it was reported from a trusted site, supply a backlink.
                        global $wgRedirectSources;
                        if( $wgRedirectSources && preg_match( $wgRedirectSources, $rdfrom ) ) {
-                               $sk = $wgUser->getSkin();
                                $redir = $sk->makeExternalLink( $rdfrom, $rdfrom );
                                $s = wfMsg( 'redirectedfrom', $redir );
                                $wgOut->setSubtitle( $s );
@@ -740,16 +800,17 @@ class Article {
                                $outputDone = true;
                        }
                }
+               # Fetch content and check for errors
                if ( !$outputDone ) {
                        $text = $this->getContent();
                        if ( $text === false ) {
                                # Failed to load, replace text with error message
                                $t = $this->mTitle->getPrefixedText();
                                if( $oldid ) {
-                                       $t .= ',oldid='.$oldid;
-                                       $text = wfMsg( 'missingarticle', $t );
+                                       $d = wfMsgExt( 'missingarticle-rev', array( 'escape' ), $oldid );
+                                       $text = wfMsg( 'missing-article', $t, $d );
                                } else {
-                                       $text = wfMsg( 'noarticletext', $t );
+                                       $text = wfMsg( 'noarticletext' );
                                }
                        }
 
@@ -757,13 +818,13 @@ class Article {
                        if ( !$this->mTitle->userCanRead() ) {
                                $wgOut->loginToUse();
                                $wgOut->output();
+                               wfProfileOut( __METHOD__ );
                                exit;
                        }
 
                        # We're looking at an old revision
-
                        if ( !empty( $oldid ) ) {
-                               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+                               $wgOut->setRobotPolicy( 'noindex,nofollow' );
                                if( is_null( $this->mRevision ) ) {
                                        // FIXME: This would be a nice place to load the 'no such page' text.
                                } else {
@@ -772,6 +833,7 @@ class Article {
                                                if( !$this->mRevision->userCan( Revision::DELETED_TEXT ) ) {
                                                        $wgOut->addWikiMsg( 'rev-deleted-text-permission' );
                                                        $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
+                                                       wfProfileOut( __METHOD__ );
                                                        return;
                                                } else {
                                                        $wgOut->addWikiMsg( 'rev-deleted-text-view' );
@@ -779,16 +841,13 @@ class Article {
                                                }
                                        }
                                }
-
                        }
-               }
-               if( !$outputDone ) {
-                       $wgOut->setRevisionId( $this->getRevIdFetched() );
                        
+                       $wgOut->setRevisionId( $this->getRevIdFetched() );
+
                         // Pages containing custom CSS or JavaScript get special treatment
                        if( $this->mTitle->isCssOrJsPage() || $this->mTitle->isCssJsSubpage() ) {
                                $wgOut->addHtml( wfMsgExt( 'clearyourcache', 'parse' ) );
-
                                // 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
@@ -798,22 +857,9 @@ class Article {
                                        $wgOut->addHtml( htmlspecialchars( $this->mContent ) );
                                        $wgOut->addHtml( "\n</pre>\n" );
                                }
-                       
-                       }
-                       
-                       elseif ( $rt = Title::newFromRedirect( $text ) ) {
-                               # Display redirect
-                               $imageDir = $wgContLang->isRTL() ? 'rtl' : 'ltr';
-                               $imageUrl = $wgStylePath.'/common/images/redirect' . $imageDir . '.png';
-                               # Don't overwrite the subtitle if this was an old revision
-                               if( !$wasRedirected && $this->isCurrent() ) {
-                                       $wgOut->setSubtitle( wfMsgHtml( 'redirectpagesub' ) );
-                               }
-                               $link = $sk->makeLinkObj( $rt, $rt->getFullText() );
-
-                               $wgOut->addHTML( '<img src="'.$imageUrl.'" alt="#REDIRECT " />' .
-                                 '<span class="redirectText">'.$link.'</span>' );
-
+                       } else if ( $rt = Title::newFromRedirect( $text ) ) {
+                               # Don't append the subtitle if this was an old revision
+                               $wgOut->addHTML( $this->viewRedirect( $rt, !$wasRedirected && $this->isCurrent() ) );
                                $parseout = $wgParser->parse($text, $this->mTitle, ParserOptions::newFromUser($wgUser));
                                $wgOut->addParserOutputNoText( $parseout );
                        } else if ( $pcache ) {
@@ -840,7 +886,6 @@ class Article {
                                if( !$this->isCurrent() ) {
                                        $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
                                }
-
                        }
                }
                /* title may have been set from the cache */
@@ -850,8 +895,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() ) ) {
+               if( $ns == NS_USER_TALK && IP::isValid( $this->mTitle->getText() ) ) {
                        $wgOut->addWikiMsg('anontalkpagetext');
                }
 
@@ -875,6 +919,47 @@ class Article {
                $this->viewUpdates();
                wfProfileOut( __METHOD__ );
        }
+       
+       /* 
+       * Should the parser cache be used?
+       */
+       protected function useParserCache( $oldid ) {
+               global $wgUser, $wgEnableParserCache;
+               
+               return $wgEnableParserCache
+                       && intval( $wgUser->getOption( 'stubthreshold' ) ) == 0
+                       && $this->exists()
+                       && empty( $oldid )
+                       && !$this->mTitle->isCssOrJsPage()
+                       && !$this->mTitle->isCssJsSubpage();
+       }
+       
+       /**
+        * View redirect
+        * @param Title $target Title of destination to redirect
+        * @param Bool  $appendSubtitle Object[optional]
+        * @param Bool  $forceKnown Should the image be shown as a bluelink regardless of existence?
+        */
+       public function viewRedirect( $target, $appendSubtitle = true, $forceKnown = false ) {
+               global $wgParser, $wgOut, $wgContLang, $wgStylePath, $wgUser;
+               
+               # Display redirect
+               $imageDir = $wgContLang->isRTL() ? 'rtl' : 'ltr';
+               $imageUrl = $wgStylePath.'/common/images/redirect' . $imageDir . '.png';
+               
+               if( $appendSubtitle ) {
+                       $wgOut->appendSubtitle( wfMsgHtml( 'redirectpagesub' ) );
+               }
+               $sk = $wgUser->getSkin();
+               if ( $forceKnown )
+                       $link = $sk->makeKnownLinkObj( $target, htmlspecialchars( $target->getFullText() ) );
+               else
+                       $link = $sk->makeLinkObj( $target, htmlspecialchars( $target->getFullText() ) );
+
+               return '<img src="'.$imageUrl.'" alt="#REDIRECT " />' .
+                       '<span class="redirectText">'.$link.'</span>';
+               
+       }
 
        function addTrackbacks() {
                global $wgOut, $wgUser;
@@ -897,6 +982,7 @@ class Article {
                                                . $o->tb_id . "&token=" . urlencode( $wgUser->editToken() ) );
                                $rmvtxt = wfMsg( 'trackbackremove', htmlspecialchars( $delurl ) );
                        }
+                       $tbtext .= "\n";
                        $tbtext .= wfMsg(strlen($o->tb_ex) ? 'trackbackexcerpt' : 'trackback',
                                        $o->tb_title,
                                        $o->tb_url,
@@ -956,7 +1042,7 @@ class Article {
                                "</form>\n", $msg );
 
                        $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
-                       $wgOut->setRobotpolicy( 'noindex,nofollow' );
+                       $wgOut->setRobotPolicy( 'noindex,nofollow' );
                        $wgOut->addHTML( $msg );
                }
        }
@@ -1067,7 +1153,6 @@ class Article {
                $result = $dbw->affectedRows() != 0;
 
                if ($result) {
-                       // FIXME: Should the result from updateRedirectOn() be returned instead?
                        $this->updateRedirectOn( $dbw, $rt, $lastRevIsRedirect );
                }
 
@@ -1112,6 +1197,8 @@ class Article {
                                $dbw->delete( 'redirect', $where, __METHOD__);
                        }
 
+                       if( $this->getTitle()->getNamespace() == NS_IMAGE )
+                               RepoGroup::singleton()->getLocalRepo()->invalidateImageRedirect( $this->getTitle() );
                        wfProfileOut( __METHOD__ );
                        return ( $dbw->affectedRows() != 0 );
                }
@@ -1252,10 +1339,10 @@ class Article {
                                }
                        }
 
-                       $extraq = ''; // Give extensions a chance to modify URL query on update
-                       wfRunHooks( 'ArticleUpdateBeforeRedirect', array( $this, &$sectionanchor, &$extraq ) );
+                       $extraQuery = ''; // Give extensions a chance to modify URL query on update
+                       wfRunHooks( 'ArticleUpdateBeforeRedirect', array( $this, &$sectionanchor, &$extraQuery ) );
 
-                       $this->doRedirect( $this->isRedirect( $text ), $sectionanchor, $extraq );
+                       $this->doRedirect( $this->isRedirect( $text ), $sectionanchor, $extraQuery );
                }
                return $good;
        }
@@ -1291,11 +1378,12 @@ class Article {
         * EDIT_NEW is specified and the article does exist, a duplicate key error will cause an exception
         * to be thrown from the Database. These two conditions are also possible with auto-detection due
         * to MediaWiki's performance-optimised locking strategy.
+        * @param $baseRevId, the revision ID this edit was based off, if any
         *
         * @return bool success
         */
-       function doEdit( $text, $summary, $flags = 0 ) {
-               global $wgUser, $wgDBtransactions;
+       function doEdit( $text, $summary, $flags = 0, $baseRevId = false ) {
+               global $wgUser, $wgDBtransactions, $wgUseAutomaticEditSummaries;
 
                wfProfileIn( __METHOD__ );
                $good = true;
@@ -1325,9 +1413,10 @@ class Article {
                $oldtext = $this->getContent();
                $oldsize = strlen( $oldtext );
 
-               # Provide autosummaries if one is not provided.
-               if ($flags & EDIT_AUTOSUMMARY && $summary == '')
+               # Provide autosummaries if one is not provided and autosummaries are enabled.
+               if( $wgUseAutomaticEditSummaries && $flags & EDIT_AUTOSUMMARY && $summary == '' ) {
                        $summary = $this->getAutosummary( $oldtext, $text, $flags );
+               }
 
                $editInfo = $this->prepareTextForEdit( $text );
                $text = $editInfo->pst;
@@ -1346,7 +1435,7 @@ class Article {
 
                        $lastRevision = 0;
                        $revisionId = 0;
-                       
+
                        $changed = ( strcmp( $text, $oldtext ) != 0 );
 
                        if ( $changed ) {
@@ -1368,7 +1457,8 @@ class Article {
                                        'page'       => $this->getId(),
                                        'comment'    => $summary,
                                        'minor_edit' => $isminor,
-                                       'text'       => $text
+                                       'text'       => $text,
+                                       'parent_id'  => $lastRevision
                                        ) );
 
                                $dbw->begin();
@@ -1382,6 +1472,8 @@ class Article {
                                        $good = false;
                                        $dbw->rollback();
                                } else {
+                                       wfRunHooks( 'NewRevisionFromEditComplete', array( $this, $revision, $baseRevId ) );
+
                                        # Update recentchanges
                                        if( !( $flags & EDIT_SUPPRESS_RC ) ) {
                                                $rcid = RecentChange::notifyEdit( $now, $this->mTitle, $isminor, $wgUser, $summary,
@@ -1414,7 +1506,7 @@ 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.
                                $this->editUpdates( $text, $summary, $isminor, $now, $revisionId, $changed );
                        }
@@ -1446,6 +1538,8 @@ class Article {
 
                        # Update the page record with revision data
                        $this->updateRevisionOn( $dbw, $revision, 0 );
+                       
+                       wfRunHooks( 'NewRevisionFromEditComplete', array($this, $revision, false) );
 
                        if( !( $flags & EDIT_SUPPRESS_RC ) ) {
                                $rcid = RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary, $bot,
@@ -1495,15 +1589,16 @@ class Article {
         *
         * @param boolean $noRedir Add redirect=no
         * @param string $sectionAnchor section to redirect to, including "#"
+        * @param string $extraQuery, extra query params
         */
-       function doRedirect( $noRedir = false, $sectionAnchor = '', $extraq = '' ) {
+       function doRedirect( $noRedir = false, $sectionAnchor = '', $extraQuery = '' ) {
                global $wgOut;
                if ( $noRedir ) {
                        $query = 'redirect=no';
-                       if( $extraq )
+                       if( $extraQuery )
                                $query .= "&$query";
                } else {
-                       $query = $extraq;
+                       $query = $extraQuery;
                }
                $wgOut->redirect( $this->mTitle->getFullURL( $query ) . $sectionAnchor );
        }
@@ -1518,8 +1613,8 @@ class Article {
                # Check patrol config options
 
                if ( !($wgUseNPPatrol || $wgUseRCPatrol)) {
-                       $wgOut->errorPage( 'rcpatroldisabled', 'rcpatroldisabledtext' );
-                       return;         
+                       $wgOut->showErrorPage( 'rcpatroldisabled', 'rcpatroldisabledtext' );
+                       return;
                }
 
                # If we haven't been given an rc_id value, we can't do anything
@@ -1527,18 +1622,18 @@ class Article {
                $rc = $rcid ? RecentChange::newFromId($rcid) : null;
                if ( is_null ( $rc ) )
                {
-                       $wgOut->errorPage( 'markedaspatrollederror', 'markedaspatrollederrortext' );
+                       $wgOut->showErrorPage( 'markedaspatrollederror', 'markedaspatrollederrortext' );
                        return;
                }
 
-               if ( !$wgUseRCPatrol && $rc->mAttribs['rc_type'] != RC_NEW) {
+               if ( !$wgUseRCPatrol && $rc->getAttribute( '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' );
+                       $wgOut->showErrorPage( 'rcpatroldisabled', 'rcpatroldisabledtext' );
                        return;
                }
-               
+
                # Check permissions
                $permission_errors = $this->mTitle->getUserPermissionsErrors( 'patrol', $wgUser );
 
@@ -1554,7 +1649,7 @@ class Article {
                }
 
                #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';
+               $returnto = $rc->getAttribute( '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
@@ -1575,10 +1670,14 @@ class Article {
                        }
                }
 
-               # Mark the edit as patrolled
-               RecentChange::markPatrolled( $rcid );
-               PatrolLog::record( $rcid );
-               wfRunHooks( 'MarkPatrolledComplete', array( &$rcid, &$wgUser, false ) );
+               # Check that the revision isn't patrolled already
+               # Prevents duplicate log entries
+               if( !$rc->getAttribute( 'rc_patrolled' ) ) {
+                       # Mark the edit as patrolled
+                       RecentChange::markPatrolled( $rcid );
+                       PatrolLog::record( $rcid );
+                       wfRunHooks( 'MarkPatrolledComplete', array( &$rcid, &$wgUser, false ) );
+               }
 
                # Inform the user
                $wgOut->setPageTitle( wfMsg( 'markedaspatrolled' ) );
@@ -1605,7 +1704,7 @@ class Article {
 
                if( $this->doWatch() ) {
                        $wgOut->setPagetitle( wfMsg( 'addedwatch' ) );
-                       $wgOut->setRobotpolicy( 'noindex,nofollow' );
+                       $wgOut->setRobotPolicy( 'noindex,nofollow' );
 
                        $wgOut->addWikiMsg( 'addedwatchtext', $this->mTitle->getPrefixedText() );
                }
@@ -1650,7 +1749,7 @@ class Article {
 
                if( $this->doUnwatch() ) {
                        $wgOut->setPagetitle( wfMsg( 'removedwatch' ) );
-                       $wgOut->setRobotpolicy( 'noindex,nofollow' );
+                       $wgOut->setRobotPolicy( 'noindex,nofollow' );
 
                        $wgOut->addWikiMsg( 'removedwatchtext', $this->mTitle->getPrefixedText() );
                }
@@ -1724,13 +1823,12 @@ class Article {
                $updated = Article::flattenRestrictions( $limit );
 
                $changed = ( $current != $updated );
-               $changed = $changed || ($this->mTitle->areRestrictionsCascading() != $cascade);
-               $changed = $changed || ($this->mTitle->mRestrictionsExpiry != $expiry);
+               $changed = $changed || ($updated && $this->mTitle->areRestrictionsCascading() != $cascade);
+               $changed = $changed || ($updated && $this->mTitle->mRestrictionsExpiry != $expiry);
                $protect = ( $updated != '' );
 
                # If nothing's changed, do nothing
                if( $changed ) {
-                       global $wgGroupPermissions;
                        if( wfRunHooks( 'ArticleProtect', array( &$this, &$wgUser, $limit, $reason ) ) ) {
 
                                $dbw = wfGetDB( DB_MASTER );
@@ -1751,12 +1849,16 @@ class Article {
                                }
                                $comment = $wgContLang->ucfirst( wfMsgForContent( $comment_type, $this->mTitle->getPrefixedText() ) );
 
-                               foreach( $limit as $action => $restrictions ) {
-                                       # Check if the group level required to edit also can protect pages
-                                       # Otherwise, people who cannot normally protect can "protect" pages via transclusion
-                                       $cascade = ( $cascade && isset($wgGroupPermissions[$restrictions]['protect']) && $wgGroupPermissions[$restrictions]['protect'] );       
+                               # Only restrictions with the 'protect' right can cascade...
+                               # Otherwise, people who cannot normally protect can "protect" pages via transclusion
+                               foreach( $limit as $action => $restriction ) {
+                                       # FIXME: can $restriction be an array or what? (same as fixme above)
+                                       if( $restriction != 'protect' && $restriction != 'sysop' ) {
+                                               $cascade = false;
+                                               break;
+                                       }
                                }
-                               
+
                                $cascade_description = '';
                                if ($cascade) {
                                        $cascade_description = ' ['.wfMsg('protect-summary-cascade').']';
@@ -1770,7 +1872,7 @@ class Article {
                                        $comment .= "$expiry_description";
                                if ( $cascade )
                                        $comment .= "$cascade_description";
-                               
+
                                # Update restrictions table
                                foreach( $limit as $action => $restrictions ) {
                                        if ($restrictions != '' ) {
@@ -1788,6 +1890,7 @@ class Article {
                                $nullRevision = Revision::newNullRevision( $dbw, $id, $comment, true );
                                $nullRevId = $nullRevision->insertOn( $dbw );
 
+                               $latest = $this->getLatest();
                                # Update page record
                                $dbw->update( 'page',
                                        array( /* SET */
@@ -1798,15 +1901,15 @@ class Article {
                                                'page_id' => $id
                                        ), 'Article::protect'
                                );
+                               
+                               wfRunHooks( 'NewRevisionFromEditComplete', array($this, $nullRevision, $latest) );
                                wfRunHooks( 'ArticleProtectComplete', array( &$this, &$wgUser, $limit, $reason ) );
 
                                # Update the protection log
                                $log = new LogPage( 'protect' );
-                               
-                               
-
                                if( $protect ) {
-                                       $log->addEntry( $modified ? 'modify' : 'protect', $this->mTitle, trim( $reason . " [$updated]$cascade_description$expiry_description" ) );
+                                       $log->addEntry( $modified ? 'modify' : 'protect', $this->mTitle, 
+                                               trim( $reason . " [$updated]$cascade_description$expiry_description" ) );
                                } else {
                                        $log->addEntry( 'unprotect', $this->mTitle, $reason );
                                }
@@ -1837,7 +1940,7 @@ class Article {
                }
                return implode( ':', $bits );
        }
-       
+
        /**
         * Auto-generates a deletion reason
         * @param bool &$hasHistory Whether the page has a history
@@ -1880,7 +1983,7 @@ class Article {
                $row = $dbw->fetchObject($res);
                $onlyAuthor = $row->rev_user_text;
                // Try to find a second contributor
-               while( $row = $dbw->fetchObject($res) ) {
+               foreach( $res as $row ) {
                        if($row->rev_user_text != $onlyAuthor) {
                                $onlyAuthor = false;
                                break;
@@ -1899,7 +2002,7 @@ class Article {
                        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
@@ -1922,18 +2025,20 @@ class Article {
 
                $confirm = $wgRequest->wasPosted() &&
                                $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;
                }
+               # Flag to hide all contents of the archived revisions
+               $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed('suppressrevision');
 
                # This code desperately needs to be totally rewritten
 
@@ -1942,7 +2047,7 @@ class Article {
                        $wgOut->readOnlyPage();
                        return;
                }
-               
+
                # Check permissions
                $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgUser );
 
@@ -1972,7 +2077,7 @@ class Article {
                }
 
                if( $confirm ) {
-                       $this->doDelete( $reason );
+                       $this->doDelete( $reason, $suppress );
                        if( $wgRequest->getCheck( 'wpWatch' ) ) {
                                $this->doWatch();
                        } elseif( $this->mTitle->userIsWatching() ) {
@@ -1995,10 +2100,10 @@ class Article {
                                        array( 'delete-warning-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
                        }
                }
-               
-               return $this->confirmDelete( '', $reason );
+
+               return $this->confirmDelete( $reason );
        }
-       
+
        /**
         * @return bool whether or not the page surpasses $wgDeleteRevisionsLimit revisions
         */
@@ -2010,7 +2115,7 @@ class Article {
                }
                return false;
        }
-       
+
        /**
         * @return int approximate revision count
         */
@@ -2071,22 +2176,29 @@ 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 ) {
+       function confirmDelete( $reason ) {
                global $wgOut, $wgUser, $wgContLang;
                $align = $wgContLang->isRtl() ? 'left' : 'right';
 
                wfDebug( "Article::confirmDelete\n" );
 
                $wgOut->setSubtitle( wfMsg( 'delete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->mTitle ) ) );
-               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+               $wgOut->setRobotPolicy( 'noindex,nofollow' );
                $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' ) ) .
+               if( $wgUser->isAllowed( 'suppressrevision' ) ) {
+                       $suppress = "<tr id=\"wpDeleteSuppressRow\" name=\"wpDeleteSuppressRow\"><td></td><td>";
+                       $suppress .= Xml::checkLabel( wfMsg( 'revdelete-suppress' ), 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '2' ) );
+                       $suppress .= "</td></tr>";
+               } else {
+                       $suppress = '';
+               }
+
+               $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->mTitle->getLocalURL( 'action=delete' ), 'id' => 'deleteconfirm' ) ) .
+                       Xml::openElement( 'fieldset', array( 'id' => 'mw-delete-table' ) ) .
+                       Xml::tags( 'legend', null, wfMsgExt( 'delete-legend', array( 'parsemag', 'escapenoentities' ) ) ) .
                        Xml::openElement( 'table' ) .
                        "<tr id=\"wpDeleteReasonListRow\">
                                <td align='$align'>" .
@@ -2094,7 +2206,7 @@ class Article {
                                "</td>
                                <td>" .
                                        Xml::listDropDown( 'wpDeleteReasonList',
-                                               wfMsgForContent( 'deletereason-dropdown' ), 
+                                               wfMsgForContent( 'deletereason-dropdown' ),
                                                wfMsgForContent( 'deletereasonotherlist' ), '', 'wpReasonDropDown', 1 ) .
                                "</td>
                        </tr>
@@ -2112,6 +2224,7 @@ class Article {
                                        Xml::checkLabel( wfMsg( 'watchthis' ), 'wpWatch', 'wpWatch', $wgUser->getBoolOption( 'watchdeletion' ) || $this->mTitle->userIsWatching(), array( 'tabindex' => '3' ) ) .
                                "</td>
                        </tr>
+                       $suppress
                        <tr>
                                <td></td>
                                <td>" .
@@ -2123,6 +2236,12 @@ class Article {
                        Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
                        Xml::closeElement( 'form' );
 
+                       if ( $wgUser->isAllowed( 'editinterface' ) ) {
+                               $skin = $wgUser->getSkin();
+                               $link = $skin->makeLink ( 'MediaWiki:Deletereason-dropdown', wfMsgHtml( 'delete-edit-reasonlist' ) );
+                               $form .= '<p class="mw-delete-editreasons">' . $link . '</p>';
+                       }
+
                $wgOut->addHTML( $form );
                $this->showLogExtract( $wgOut );
        }
@@ -2132,37 +2251,39 @@ class Article {
         * Show relevant lines from the deletion log
         */
        function showLogExtract( $out ) {
-               $out->addHtml( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . '</h2>' );
-               $logViewer = new LogViewer(
-                       new LogReader(
-                               new FauxRequest(
-                                       array( 'page' => $this->mTitle->getPrefixedText(),
-                                              'type' => 'delete' ) ) ) );
-               $logViewer->showList( $out );
+               $out->addHtml( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
+               LogEventsList::showLogExtract( $out, 'delete', $this->mTitle->getPrefixedText() );
        }
 
 
        /**
         * Perform a deletion and output success or failure messages
         */
-       function doDelete( $reason ) {
+       function doDelete( $reason, $suppress = false ) {
                global $wgOut, $wgUser;
                wfDebug( __METHOD__."\n" );
+               
+               $id = $this->getId();
+               
+               $error = '';
 
-               if (wfRunHooks('ArticleDelete', array(&$this, &$wgUser, &$reason))) {
-                       if ( $this->doDeleteArticle( $reason ) ) {
+               if (wfRunHooks('ArticleDelete', array(&$this, &$wgUser, &$reason, &$error))) {
+                       if ( $this->doDeleteArticle( $reason, $suppress ) ) {
                                $deleted = $this->mTitle->getPrefixedText();
 
                                $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
-                               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+                               $wgOut->setRobotPolicy( 'noindex,nofollow' );
 
                                $loglink = '[[Special:Log/delete|' . wfMsgNoTrans( 'deletionlog' ) . ']]';
 
                                $wgOut->addWikiMsg( 'deletedtext', $deleted, $loglink );
                                $wgOut->returnToMain( false );
-                               wfRunHooks('ArticleDeleteComplete', array(&$this, &$wgUser, $reason));
+                               wfRunHooks('ArticleDeleteComplete', array(&$this, &$wgUser, $reason, $id));
                        } else {
-                               $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
+                               if ($error = '')
+                                       $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
+                               else
+                                       $wgOut->showFatalError( $error );
                        }
                }
        }
@@ -2172,7 +2293,7 @@ class Article {
         * Deletes the article with database consistency, writes logs, purges caches
         * Returns success
         */
-       function doDeleteArticle( $reason ) {
+       function doDeleteArticle( $reason, $suppress = false ) {
                global $wgUseSquid, $wgDeferredUpdateList;
                global $wgUseTrackbacks;
 
@@ -2190,6 +2311,19 @@ class Article {
                $u = new SiteStatsUpdate( 0, 1, -(int)$this->isCountable( $this->getContent() ), -1 );
                array_push( $wgDeferredUpdateList, $u );
 
+               // Bitfields to further suppress the content
+               if ( $suppress ) {
+                       $bitfield = 0;
+                       // This should be 15...
+                       $bitfield |= Revision::DELETED_TEXT;
+                       $bitfield |= Revision::DELETED_COMMENT;
+                       $bitfield |= Revision::DELETED_USER;
+                       $bitfield |= Revision::DELETED_RESTRICTED;
+               } else {
+                       $bitfield = 'rev_deleted';
+               }
+
+               $dbw->begin();
                // For now, shunt the revision data into the archive table.
                // Text is *not* removed from the text table; bulk storage
                // is left intact to avoid breaking block-compression or
@@ -2213,8 +2347,9 @@ 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',
+                               'ar_deleted'    => $bitfield
                        ), array(
                                'page_id' => $id,
                                'page_id = rev_page'
@@ -2224,12 +2359,25 @@ class Article {
                # Delete restrictions for it
                $dbw->delete( 'page_restrictions', array ( 'pr_page' => $id ), __METHOD__ );
 
+               # Fix category table counts
+               $cats = array();
+               $res = $dbw->select( 'categorylinks', 'cl_to',
+                       array( 'cl_from' => $id ), __METHOD__ );
+               foreach( $res as $row ) {
+                       $cats []= $row->cl_to;
+               }
+               $this->updateCategoryCounts( array(), $cats );
+
                # Now that it's safely backed up, delete it
                $dbw->delete( 'page', array( 'page_id' => $id ), __METHOD__);
+               $ok = ( $dbw->affectedRows() > 0 ); // getArticleId() uses slave, could be laggy
+               if( !$ok ) {
+                       $dbw->rollback();
+                       return false;
+               }
 
                # If using cascading deletes, we can skip some explicit deletes
                if ( !$dbw->cascadingDeletes() ) {
-
                        $dbw->delete( 'revision', array( 'rev_page' => $id ), __METHOD__ );
 
                        if ($wgUseTrackbacks)
@@ -2249,19 +2397,26 @@ class Article {
                if ( !$dbw->cleanupTriggers() ) {
 
                        # Clean up recentchanges entries...
-                       $dbw->delete( 'recentchanges', array( 'rc_namespace' => $ns, 'rc_title' => $t ), __METHOD__ );
+                       $dbw->delete( 'recentchanges',
+                               array( 'rc_namespace' => $ns, 'rc_title' => $t, 'rc_type != '.RC_LOG ),
+                               __METHOD__ );
                }
+               $dbw->commit();
 
                # Clear caches
                Article::onArticleDelete( $this->mTitle );
 
-               # Log the deletion
-               $log = new LogPage( 'delete' );
-               $log->addEntry( 'delete', $this->mTitle, $reason );
-
                # Clear the cached article id so the interface doesn't act like we exist
                $this->mTitle->resetArticleID( 0 );
                $this->mTitle->mArticleID = 0;
+
+               # Log the deletion, if the page was suppressed, log it at Oversight instead
+               $logtype = $suppress ? 'suppress' : 'delete';
+               $log = new LogPage( $logtype );
+
+               # Make sure logging got through
+               $log->addEntry( 'delete', $this->mTitle, $reason, array() );
+
                return true;
        }
 
@@ -2272,15 +2427,15 @@ class Article {
         * 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 $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 array $resultDetails contains result-specific array of additional values
         *    'alreadyrolled' : 'current' (rev)
         *    success        : 'summary' (str), 'current' (rev), 'target' (rev)
-        * 
+        *
         * @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
@@ -2302,10 +2457,10 @@ class Article {
                # 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
@@ -2314,9 +2469,9 @@ class Article {
         * 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;
+               global $wgUseRCPatrol, $wgUser, $wgLang;
                $dbw = wfGetDB( DB_MASTER );
 
                if( wfReadOnly() ) {
@@ -2344,7 +2499,7 @@ class Article {
                $user = intval( $current->getUser() );
                $user_text = $dbw->addQuotes( $current->getUserText() );
                $s = $dbw->selectRow( 'revision',
-                       array( 'rev_id', 'rev_timestamp' ),
+                       array( 'rev_id', 'rev_timestamp', 'rev_deleted' ),
                        array(  'rev_page' => $current->getPage(),
                                "rev_user <> {$user} OR rev_user_text <> {$user_text}"
                        ), __METHOD__,
@@ -2354,8 +2509,11 @@ class Article {
                if( $s === false ) {
                        # No one else ever edited this page
                        return array(array('cantrollback'));
+               } else if( $s->rev_deleted & REVISION::DELETED_TEXT || $s->rev_deleted & REVISION::DELETED_USER ) {
+                       # Only admins can see this text
+                       return array(array('notvisiblerev'));
                }
-       
+
                $set = array();
                if ( $bot && $wgUser->isAllowed('markbotedits') ) {
                        # Mark all reverted edits as bot
@@ -2378,15 +2536,17 @@ class Article {
 
                # Generate the edit summary if necessary
                $target = Revision::newFromId( $s->rev_id );
-               if( empty( $summary ) )
-               {
-                       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())
-                       );
+               if( empty( $summary ) ){
+                       $summary = wfMsgForContent( 'revertpage' );
                }
+               
+               # Allow the custom summary to use the same args as the default message
+               $args = array(
+                       $target->getUserText(), $from, $s->rev_id,
+                       $wgLang->timeanddate(wfTimestamp(TS_MW, $s->rev_timestamp), true),
+                       $current->getId(), $wgLang->timeanddate($current->getTimestamp())
+               );
+               $summary = wfMsgReplaceArgs( $summary, $args ); 
 
                # Save
                $flags = EDIT_UPDATE;
@@ -2396,7 +2556,7 @@ class Article {
 
                if( $bot && ($wgUser->isAllowed('markbotedits') || $wgUser->isAllowed('bot')) )
                        $flags |= EDIT_FORCE_BOT;
-               $this->doEdit( $target->getText(), $summary, $flags );
+               $this->doEdit( $target->getText(), $summary, $flags, $target->getId() );
 
                wfRunHooks( 'ArticleRollbackComplete', array( $this, $wgUser, $target ) );
 
@@ -2431,6 +2591,19 @@ class Article {
                        $wgOut->rateLimited();
                        return;
                }
+               if( isset( $result[0][0] ) && ( $result[0][0] == 'alreadyrolled' || $result[0][0] == 'cantrollback' ) ){
+                       $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
+                       $errArray = $result[0];
+                       $errMsg = array_shift( $errArray );
+                       $wgOut->addWikiMsgArray( $errMsg, $errArray );
+                       if( isset( $details['current'] ) ){
+                               $current = $details['current'];
+                               if( $current->getComment() != '' ) {
+                                       $wgOut->addWikiMsgArray( 'editcomment', array( $wgUser->getSkin()->formatComment( $current->getComment() ) ), array( 'replaceafter' ) );
+                               }
+                       }
+                       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.
@@ -2461,6 +2634,11 @@ class Article {
                        . $wgUser->getSkin()->userToolLinks( $target->getUser(), $target->getUserText() );
                $wgOut->addHtml( wfMsgExt( 'rollback-success', array( 'parse', 'replaceafter' ), $old, $new ) );
                $wgOut->returnToMain( false, $this->mTitle );
+               
+               if( !$wgRequest->getBool( 'hidediff', false ) ) {
+                       $de = new DifferenceEngine( $this->mTitle, $current->getId(), 'next', false, true );
+                       $de->showDiff( '', '' );
+               }
        }
 
 
@@ -2469,11 +2647,12 @@ class Article {
         * @private
         */
        function viewUpdates() {
-               global $wgDeferredUpdateList;
+               global $wgDeferredUpdateList, $wgUser;
 
                if ( 0 != $this->getID() ) {
+                       # Don't update page view counters on views from bot users (bug 14044)
                        global $wgDisableCounters;
-                       if( !$wgDisableCounters ) {
+                       if( !$wgDisableCounters && !$wgUser->isAllowed( 'bot' ) ) {
                                Article::incViewCount( $this->getID() );
                                $u = new SiteStatsUpdate( 1, 0, 0 );
                                array_push( $wgDeferredUpdateList, $u );
@@ -2481,7 +2660,6 @@ class Article {
                }
 
                # Update newtalk / watchlist notification status
-               global $wgUser;
                $wgUser->clearNotification( $this->mTitle );
        }
 
@@ -2538,7 +2716,7 @@ class Article {
 
                # Save it to the parser cache
                if ( $wgEnableParserCache ) {
-                       $parserCache =& ParserCache::singleton();
+                       $parserCache = ParserCache::singleton();
                        $parserCache->save( $editInfo->output, $this, $wgUser );
                }
 
@@ -2638,7 +2816,7 @@ class Article {
                $sk = $wgUser->getSkin();
                $lnk = $current
                        ? wfMsg( 'currentrevisionlink' )
-                       : $lnk = $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'currentrevisionlink' ) );
+                       : $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'currentrevisionlink' ) );
                $curdiff = $current
                        ? wfMsg( 'diff' )
                        : $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'diff' ), 'diff=cur&oldid='.$oldid );
@@ -2656,16 +2834,38 @@ class Article {
                        ? wfMsg( 'diff' )
                        : $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'diff' ), 'diff=next&oldid='.$oldid );
 
-               $userlinks = $sk->userLink( $revision->getUser(), $revision->getUserText() )
-                                               . $sk->userToolLinks( $revision->getUser(), $revision->getUserText() );
+               $cdel='';
+               if( $wgUser->isAllowed( 'deleterevision' ) ) {
+                       $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
+                       if( $revision->isCurrent() ) {
+                       // We don't handle top deleted edits too well
+                               $cdel = wfMsgHtml('rev-delundel');
+                       } else if( !$revision->userCan( Revision::DELETED_RESTRICTED ) ) {
+                       // If revision was hidden from sysops
+                               $cdel = wfMsgHtml('rev-delundel');
+                       } else {
+                               $cdel = $sk->makeKnownLinkObj( $revdel,
+                                       wfMsgHtml('rev-delundel'),
+                                       'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
+                                       '&oldid=' . urlencode( $oldid ) );
+                               // Bolden oversighted content
+                               if( $revision->isDeleted( Revision::DELETED_RESTRICTED ) )
+                                       $cdel = "<strong>$cdel</strong>";
+                       }
+                       $cdel = "(<small>$cdel</small>) ";
+               }
+               # Show user links if allowed to see them. Normally they
+               # are hidden regardless, but since we can already see the text here...
+               $userlinks = $sk->revUserTools( $revision, false );
 
                $m = wfMsg( 'revision-info-current' );
                $infomsg = $current && !wfEmptyMsg( 'revision-info-current', $m ) && $m != '-'
                        ? 'revision-info-current'
                        : 'revision-info';
-                       
+
                $r = "\n\t\t\t\t<div id=\"mw-{$infomsg}\">" . wfMsg( $infomsg, $td, $userlinks ) . "</div>\n" .
-                    "\n\t\t\t\t<div id=\"mw-revision-nav\">" . wfMsg( 'revision-nav', $prevdiff, $prevlink, $lnk, $curdiff, $nextlink, $nextdiff ) . "</div>\n\t\t\t";
+
+                    "\n\t\t\t\t<div id=\"mw-revision-nav\">" . $cdel . wfMsg( 'revision-nav', $prevdiff, $prevlink, $lnk, $curdiff, $nextlink, $nextdiff ) . "</div>\n\t\t\t";
                $wgOut->setSubtitle( $r );
        }
 
@@ -2723,9 +2923,9 @@ class Article {
                $printable = $wgRequest->getVal( 'printable' );
                $page      = $wgRequest->getVal( 'page' );
 
-               //check for non-standard user language; this covers uselang, 
+               //check for non-standard user language; this covers uselang,
                //and extensions for auto-detecting user language.
-               $ulang     = $wgLang->getCode(); 
+               $ulang     = $wgLang->getCode();
                $clang     = $wgContLang->getCode();
 
                $cacheable = $wgUseFileCache
@@ -2806,6 +3006,8 @@ class Article {
                $revision->insertOn( $dbw );
                $this->updateRevisionOn( $dbw, $revision );
                $dbw->commit();
+               
+               wfRunHooks( 'NewRevisionFromEditComplete', array($this, $revision, false) );
 
                wfProfileOut( __METHOD__ );
        }
@@ -2903,6 +3105,15 @@ class Article {
        static function onArticleDelete( $title ) {
                global $wgUseFileCache, $wgMessageCache;
 
+               // Update existence markers on article/talk tabs...
+               if( $title->isTalkPage() ) {
+                       $other = $title->getSubjectPage();
+               } else {
+                       $other = $title->getTalkPage();
+               }
+               $other->invalidateCache();
+               $other->purgeSquid();
+
                $title->touchLinks();
                $title->purgeSquid();
 
@@ -2912,13 +3123,20 @@ class Article {
                        @unlink( $cm->fileCacheName() );
                }
 
-               if( $title->getNamespace() == NS_MEDIAWIKI) {
+               # Messages
+               if( $title->getNamespace() == NS_MEDIAWIKI ) {
                        $wgMessageCache->replace( $title->getDBkey(), false );
                }
+               # Images
                if( $title->getNamespace() == NS_IMAGE ) {
                        $update = new HTMLCacheUpdate( $title, 'imagelinks' );
                        $update->doUpdate();
                }
+               # User talk pages
+               if( $title->getNamespace() == NS_USER_TALK ) {
+                       $user = User::newFromName( $title->getText(), false );
+                       $user->setNewtalk( false );
+               }
        }
 
        /**
@@ -2932,7 +3150,7 @@ class Article {
 
                // Invalidate the caches of all pages which redirect here
                $wgDeferredUpdateList[] = new HTMLCacheUpdate( $title, 'redirect' );
-               
+
                # Purge squid for this page only
                $title->purgeSquid();
 
@@ -2945,6 +3163,15 @@ class Article {
 
        /**#@-*/
 
+       /**
+        * Overriden by ImagePage class, only present here to avoid a fatal error
+        * Called for ?action=revert
+        */
+       public function revert(){
+               global $wgOut;
+               $wgOut->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
+       }
+
        /**
         * Info about this page
         * Called for ?action=info when $wgAllowPageInfo is on.
@@ -3059,12 +3286,10 @@ class Article {
                $res = $dbr->select( array( 'templatelinks' ),
                        array( 'tl_namespace', 'tl_title' ),
                        array( 'tl_from' => $id ),
-                       'Article:getUsedTemplates' );
-               if ( false !== $res ) {
-                       if ( $dbr->numRows( $res ) ) {
-                               while ( $row = $dbr->fetchObject( $res ) ) {
-                                       $result[] = Title::makeTitle( $row->tl_namespace, $row->tl_title );
-                               }
+                       __METHOD__ );
+               if( false !== $res ) {
+                       foreach( $res as $row ) {
+                               $result[] = Title::makeTitle( $row->tl_namespace, $row->tl_title );
                        }
                }
                $dbr->freeResult( $res );
@@ -3087,55 +3312,18 @@ class Article {
                $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', 
+                       array( 'cl_from' => $id, 'pp_page=page_id', 'pp_propname' => 'hiddencat',
                                'page_namespace' => NS_CATEGORY, 'page_title=cl_to'),
-                       'Article:getHiddenCategories' );
+                       __METHOD__ );
                if ( false !== $res ) {
-                       if ( $dbr->numRows( $res ) ) {
-                               while ( $row = $dbr->fetchObject( $res ) ) {
-                                       $result[] = Title::makeTitle( NS_CATEGORY, $row->cl_to );
-                               }
+                       foreach( $res as $row ) {
+                               $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.
-        *
-        * @param  string $text The wikitext to check
-        * @return string '' or an appropriate summary
-        */
-       public static function getRedirectAutosummary( $text ) {
-               $rt = Title::newFromRedirect( $text );
-               if( is_object( $rt ) )
-                       return wfMsgForContent( 'autoredircomment', $rt->getFullText() );
-               else
-                       return '';
-       }
-
-       /**
-        * Return an auto-generated summary if the new text is much shorter than
-        * the old text.
-        *
-        * @param  string $oldtext The previous text of the page
-        * @param  string $text    The submitted text of the page
-        * @return string An appropriate autosummary, or an empty string.
-        */
-       public static function getBlankingAutosummary( $oldtext, $text ) {
-               if ($oldtext!='' && $text=='') {
-                       return wfMsgForContent('autosumm-blank');
-               } elseif (strlen($oldtext) > 10 * strlen($text) && strlen($text) < 500) {
-                       #Removing more than 90% of the article
-                       global $wgContLang;
-                       $truncatedtext = $wgContLang->truncate($text, max(0, 200 - strlen(wfMsgForContent('autosumm-replace'))), '...');
-                       return wfMsgForContent('autosumm-replace', $truncatedtext);
-               } else {
-                       return '';
-               }
-       }
-
        /**
        * Return an applicable autosummary if one exists for the given edit.
        * @param string $oldtext The previous text of the page.
@@ -3144,38 +3332,42 @@ class Article {
        * @return string An appropriate autosummary, or an empty string.
        */
        public static function getAutosummary( $oldtext, $newtext, $flags ) {
+               # Decide what kind of autosummary is needed.
 
-               # This code is UGLY UGLY UGLY.
-               # Somebody PLEASE come up with a more elegant way to do it.
-
-               #Redirect autosummaries
-               $summary = self::getRedirectAutosummary( $newtext );
-
-               if ($summary)
-                       return $summary;
-
-               #Blanking autosummaries
-               if (!($flags & EDIT_NEW))
-                       $summary = self::getBlankingAutosummary( $oldtext, $newtext );
-
-               if ($summary)
-                       return $summary;
+               # Redirect autosummaries
+               $rt = Title::newFromRedirect( $newtext );
+               if( is_object( $rt ) ) {
+                       return wfMsgForContent( 'autoredircomment', $rt->getFullText() );
+               }
 
-               #New page autosummaries
-               if ($flags & EDIT_NEW && strlen($newtext)) {
-                       #If they're making a new article, give its text, truncated, in the summary.
+               # New page autosummaries
+               if( $flags & EDIT_NEW && strlen( $newtext ) ) {
+                       # If they're making a new article, give its text, truncated, in the summary.
                        global $wgContLang;
                        $truncatedtext = $wgContLang->truncate(
                                str_replace("\n", ' ', $newtext),
                                max( 0, 200 - strlen( wfMsgForContent( 'autosumm-new') ) ),
                                '...' );
-                       $summary = wfMsgForContent( 'autosumm-new', $truncatedtext );
+                       return wfMsgForContent( 'autosumm-new', $truncatedtext );
                }
 
-               if ($summary)
-                       return $summary;
+               # Blanking autosummaries
+               if( $oldtext != '' && $newtext == '' ) {
+                       return wfMsgForContent('autosumm-blank');
+               } elseif( strlen( $oldtext ) > 10 * strlen( $newtext ) && strlen( $newtext ) < 500) {
+                       # Removing more than 90% of the article
+                       global $wgContLang;
+                       $truncatedtext = $wgContLang->truncate(
+                               $newtext,
+                               max( 0, 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) ),
+                               '...'
+                       );
+                       return wfMsgForContent( 'autosumm-replace', $truncatedtext );
+               }
 
-               return $summary;
+               # If we reach this point, there's no applicable autosummary for our case, so our
+               # autosummary is empty.
+               return '';
        }
 
        /**
@@ -3197,7 +3389,7 @@ class Article {
                $popts->setTidy(false);
                $popts->enableLimitReport( false );
                if ( $wgEnableParserCache && $cache && $this && $parserOutput->getCacheTime() != -1 ) {
-                       $parserCache =& ParserCache::singleton();
+                       $parserCache = ParserCache::singleton();
                        $parserCache->save( $parserOutput, $this, $wgUser );
                }
 
@@ -3218,15 +3410,13 @@ class Article {
                        $res = $dbr->select( array( 'templatelinks' ),
                                array( 'tl_namespace', 'tl_title' ),
                                array( 'tl_from' => $id ),
-                               'Article:getUsedTemplates' );
+                               __METHOD__ );
 
                        global $wgContLang;
 
                        if ( false !== $res ) {
-                               if ( $dbr->numRows( $res ) ) {
-                                       while ( $row = $dbr->fetchObject( $res ) ) {
-                                               $tlTemplates[] = $wgContLang->getNsText( $row->tl_namespace ) . ':' . $row->tl_title ;
-                                       }
+                               foreach( $res as $row ) {
+                                       $tlTemplates[] = $wgContLang->getNsText( $row->tl_namespace ) . ':' . $row->tl_title ;
                                }
                        }
 
@@ -3257,4 +3447,60 @@ class Article {
                $wgOut->addParserOutput( $parserOutput );
        }
 
+       /**
+        * Update all the appropriate counts in the category table, given that
+        * we've added the categories $added and deleted the categories $deleted.
+        *
+        * @param $added array   The names of categories that were added
+        * @param $deleted array The names of categories that were deleted
+        * @return null
+        */
+       public function updateCategoryCounts( $added, $deleted ) {
+               $ns = $this->mTitle->getNamespace();
+               $dbw = wfGetDB( DB_MASTER );
+
+               # First make sure the rows exist.  If one of the "deleted" ones didn't
+               # exist, we might legitimately not create it, but it's simpler to just
+               # create it and then give it a negative value, since the value is bogus
+               # anyway.
+               #
+               # Sometimes I wish we had INSERT ... ON DUPLICATE KEY UPDATE.
+               $insertCats = array_merge( $added, $deleted );
+               if( !$insertCats ) {
+                       # Okay, nothing to do
+                       return;
+               }
+               $insertRows = array();
+               foreach( $insertCats as $cat ) {
+                       $insertRows[] = array( 'cat_title' => $cat );
+               }
+               $dbw->insert( 'category', $insertRows, __METHOD__, 'IGNORE' );
+
+               $addFields    = array( 'cat_pages = cat_pages + 1' );
+               $removeFields = array( 'cat_pages = cat_pages - 1' );
+               if( $ns == NS_CATEGORY ) {
+                       $addFields[]    = 'cat_subcats = cat_subcats + 1';
+                       $removeFields[] = 'cat_subcats = cat_subcats - 1';
+               } elseif( $ns == NS_IMAGE ) {
+                       $addFields[]    = 'cat_files = cat_files + 1';
+                       $removeFields[] = 'cat_files = cat_files - 1';
+               }
+
+               if ( $added ) {
+                       $dbw->update(
+                               'category',
+                               $addFields,
+                               array( 'cat_title' => $added ),
+                               __METHOD__
+                       );
+               }
+               if ( $deleted ) {
+                       $dbw->update(
+                               'category',
+                               $removeFields,
+                               array( 'cat_title' => $deleted ),
+                               __METHOD__
+                       );
+               }
+       }
 }