* Display the anon talk page info message on anon talk pages again (moved outside...
[lhc/web/wiklou.git] / includes / Article.php
index 9f1d3b8..4ede20f 100644 (file)
@@ -37,6 +37,7 @@ class Article {
        var $mRevIdFetched;
        var $mRevision;
        var $mRedirectUrl;
+       var $mLatest;
        /**#@-*/
 
        /**
@@ -91,11 +92,8 @@ class Article {
                                        } else {
                                                return $rt->getFullURL();
                                        }
-                               } elseif( $rt->exists() ) {
-                                       // Internal redirects can be handled relatively gracefully.
-                                       // We may have to change to another Article subclass, though.
-                                       return $rt;
                                }
+                               return $rt;
                        }
                }
                
@@ -128,6 +126,7 @@ class Article {
                $this->mIsRedirect = false;
                $this->mRevIdFetched = 0;
                $this->mRedirectUrl = false;
+               $this->mLatest = false;
        }
 
        /**
@@ -176,43 +175,29 @@ class Article {
                        return "<div class='noarticletext'>$ret</div>";
                } else {
                        $this->loadContent();
-                       # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
-                       if ( $this->mTitle->getNamespace() == NS_USER_TALK &&
-                         $wgUser->isIP($this->mTitle->getText()) &&
-                         $action=='view'
-                       ) {
-                               wfProfileOut( $fname );
-                               return $this->mContent . "\n" .wfMsg('anontalkpagetext');
-                       } else {
-                               if($action=='edit') {
-                                       if($section!='') {
-                                               if($section=='new') {
-                                                       wfProfileOut( $fname );
-                                                       $text=$this->getPreloadedText($preload);
-                                                       return $text;
-                                               }
-
-                                               # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
-                                               # comments to be stripped as well)
-                                               $rv=$this->getSection($this->mContent,$section);
+                       if($action=='edit') {
+                               if($section!='') {
+                                       if($section=='new') {
                                                wfProfileOut( $fname );
-                                               return $rv;
+                                               $text=$this->getPreloadedText($preload);
+                                               return $text;
                                        }
+
+                                       # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
+                                       # comments to be stripped as well)
+                                       $rv=$this->getSection($this->mContent,$section);
+                                       wfProfileOut( $fname );
+                                       return $rv;
                                }
-                               wfProfileOut( $fname );
-                               return $this->mContent;
                        }
+                       wfProfileOut( $fname );
+                       return $this->mContent;
                }
        }
 
        /**
         * Get the contents of a page from its title and remove includeonly tags
         *
-        * TODO FIXME: This is only here because of the inputbox extension and
-        * should be moved there
-        *
-        * @deprecated
-        *
         * @param string The title of the page
         * @return string The contents of the page
         */
@@ -354,8 +339,6 @@ class Article {
         * Load the revision (including text) into this object
         */
        function loadContent() {
-               global $wgOut, $wgRequest;
-
                if ( $this->mContentLoaded ) return;
 
                # Query variables :P
@@ -426,14 +409,30 @@ class Article {
         * @param object $data
         * @access private
         */
-       function loadPageData( $data ) {
-               $this->mTitle->loadRestrictions( $data->page_restrictions );
-               $this->mTitle->mRestrictionsLoaded = true;
+       function loadPageData( $data = 'fromdb' ) {
+               if ( $data === 'fromdb' ) {
+                       $dbr =& $this->getDB();
+                       $data = $this->pageDataFromId( $dbr, $this->getId() );
+               }
+                       
+               $lc =& LinkCache::singleton();
+               if ( $data ) {
+                       $lc->addGoodLinkObj( $data->page_id, $this->mTitle );
+
+                       $this->mTitle->mArticleID = $data->page_id;
+                       $this->mTitle->loadRestrictions( $data->page_restrictions );
+                       $this->mTitle->mRestrictionsLoaded = true;
 
-               $this->mCounter     = $data->page_counter;
-               $this->mTouched     = wfTimestamp( TS_MW, $data->page_touched );
-               $this->mIsRedirect  = $data->page_is_redirect;
-               $this->mLatest      = $data->page_latest;
+                       $this->mCounter     = $data->page_counter;
+                       $this->mTouched     = wfTimestamp( TS_MW, $data->page_touched );
+                       $this->mIsRedirect  = $data->page_is_redirect;
+                       $this->mLatest      = $data->page_latest;
+               } else {
+                       if ( is_object( $this->mTitle ) ) {
+                               $lc->addBadLinkObj( $this->mTitle );
+                       }
+                       $this->mTitle->mArticleID = 0;
+               }
 
                $this->mDataLoaded  = true;
        }
@@ -484,12 +483,15 @@ class Article {
                        }
                        $revision = Revision::newFromId( $this->mLatest );
                        if( is_null( $revision ) ) {
-                               wfDebug( "$fname failed to retrieve current page, rev_id $data->page_latest\n" );
+                               wfDebug( "$fname failed to retrieve current page, rev_id {$data->page_latest}\n" );
                                return false;
                        }
                }
 
-               $this->mContent   = $revision->getText();
+               // 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( MW_REV_DELETED_TEXT ) ? $revision->getRawText() : "";
+               //$this->mContent   = $revision->getText();
 
                $this->mUser      = $revision->getUser();
                $this->mUserText  = $revision->getUserText();
@@ -566,9 +568,13 @@ class Article {
        function getCount() {
                if ( -1 == $this->mCounter ) {
                        $id = $this->getID();
-                       $dbr =& wfGetDB( DB_SLAVE );
-                       $this->mCounter = $dbr->selectField( 'page', 'page_counter', array( 'page_id' => $id ),
-                               'Article::getCount', $this->getSelectOptions() );
+                       if ( $id == 0 ) {
+                               $this->mCounter = 0;
+                       } else {
+                               $dbr =& wfGetDB( DB_SLAVE );
+                               $this->mCounter = $dbr->selectField( 'page', 'page_counter', array( 'page_id' => $id ),
+                                       'Article::getCount', $this->getSelectOptions() );
+                       }
                }
                return $this->mCounter;
        }
@@ -623,8 +629,6 @@ class Article {
         * @access private
         */
        function loadLastEdit() {
-               global $wgOut;
-
                if ( -1 != $this->mUser )
                        return;
 
@@ -644,7 +648,10 @@ class Article {
        }
 
        function getTimestamp() {
-               $this->loadLastEdit();
+               // Check if the field has been filled by ParserCache::get()
+               if ( !$this->mTimestamp ) {
+                       $this->loadLastEdit();
+               }
                return wfTimestamp(TS_MW, $this->mTimestamp);
        }
 
@@ -713,7 +720,7 @@ class Article {
         * the given title.
        */
        function view() {
-               global $wgUser, $wgOut, $wgRequest, $wgOnlySysopsCanPatrol, $wgContLang;
+               global $wgUser, $wgOut, $wgRequest, $wgContLang;
                global $wgEnableParserCache, $wgStylePath, $wgUseRCPatrol, $wgParser;
                global $wgUseTrackbacks;
                $sk = $wgUser->getSkin();
@@ -756,7 +763,7 @@ class Article {
                        wfProfileOut( $fname );
                        return;
                }
-
+               
                if ( empty( $oldid ) && $this->checkTouched() ) {
                        $wgOut->setETag($parserCache->getETag($this, $wgUser));
 
@@ -818,8 +825,10 @@ class Article {
                                $t = $this->mTitle->getPrefixedText();
                                if( $oldid ) {
                                        $t .= ',oldid='.$oldid;
+                                       $text = wfMsg( 'missingarticle', $t );
+                               } else {
+                                       $text = wfMsg( 'noarticletext', $t );
                                }
-                               $text = wfMsg( 'missingarticle', $t );
                        }
 
                        # Another whitelist check in case oldid is altering the title
@@ -832,8 +841,23 @@ class Article {
                        # We're looking at an old revision
 
                        if ( !empty( $oldid ) ) {
-                               $this->setOldSubtitle( isset($this->mOldId) ? $this->mOldId : $oldid );
-                               $wgOut->setRobotpolicy( 'noindex,follow' );
+                               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+                               if( is_null( $this->mRevision ) ) {
+                                       // FIXME: This would be a nice place to load the 'no such page' text.
+                               } else {
+                                       $this->setOldSubtitle( isset($this->mOldId) ? $this->mOldId : $oldid );
+                                       if( $this->mRevision->isDeleted( MW_REV_DELETED_TEXT ) ) {
+                                               if( !$this->mRevision->userCan( MW_REV_DELETED_TEXT ) ) {
+                                                       $wgOut->addWikiText( wfMsg( 'rev-deleted-text-permission' ) );
+                                                       $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
+                                                       return;
+                                               } else {
+                                                       $wgOut->addWikiText( wfMsg( 'rev-deleted-text-view' ) );
+                                                       // and we are allowed to see...
+                                               }
+                                       }
+                               }
+
                        }
                }
                if( !$outputDone ) {
@@ -877,7 +901,8 @@ class Article {
                                if( !$this->isCurrent() ) {
                                        $oldEditSectionSetting = $wgOut->mParserOptions->setEditSection( false );
                                }
-                               $wgOut->addWikiText( $text );
+                               # Display content and don't save to parser cache
+                               $wgOut->addPrimaryWikiText( $text, $this, false );
 
                                if( !$this->isCurrent() ) {
                                        $wgOut->mParserOptions->setEditSection( $oldEditSectionSetting );
@@ -889,15 +914,16 @@ class Article {
                if( empty( $t ) ) {
                        $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
                }
+               
+               # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
+               if( $this->mTitle->getNamespace() == NS_USER_TALK &&
+                       User::isIP( $this->mTitle->getText() ) ) {
+                       $wgOut->addWikiText( wfMsg('anontalkpagetext') );
+               }
 
                # 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->isLoggedIn()
-                       && ( $wgUser->isAllowed('patrol') || !$wgOnlySysopsCanPatrol ) )
-               {
+               if ( $wgUseRCPatrol && !is_null( $rcid ) && $rcid != 0 && $wgUser->isAllowed( 'patrol' ) ) {
                        $wgOut->addHTML(
                                "<div class='patrollink'>" .
                                        wfMsg ( 'markaspatrolledlink',
@@ -977,23 +1003,16 @@ class Article {
                $this->view();
        }
 
+       /**
+        * Handle action=purge
+        */
        function purge() {
-               global $wgUser, $wgRequest, $wgOut, $wgUseSquid;
-
-               if ( $wgUser->isLoggedIn() || $wgRequest->wasPosted() || ! wfRunHooks( 'ArticlePurge', array( &$this ) ) ) {
-                       // Invalidate the cache
-                       $this->mTitle->invalidateCache();
-
-                       if ( $wgUseSquid ) {
-                               // Commit the transaction before the purge is sent
-                               $dbw = wfGetDB( DB_MASTER );
-                               $dbw->immediateCommit();
+               global $wgUser, $wgRequest, $wgOut;
 
-                               // Send purge
-                               $update = SquidUpdate::newSimplePurge( $this->mTitle );
-                               $update->doUpdate();
+               if ( $wgUser->isLoggedIn() || $wgRequest->wasPosted() ) {
+                       if( wfRunHooks( 'ArticlePurge', array( &$this ) ) ) {
+                               $this->doPurge();
                        }
-                       $this->view();
                } else {
                        $msg = $wgOut->parse( wfMsg( 'confirm_purge' ) );
                        $action = $this->mTitle->escapeLocalURL( 'action=purge' );
@@ -1008,6 +1027,26 @@ class Article {
                        $wgOut->addHTML( $msg );
                }
        }
+       
+       /**
+        * Perform the actions of a page purging
+        */
+       function doPurge() {
+               global $wgUseSquid;
+               // Invalidate the cache
+               $this->mTitle->invalidateCache();
+
+               if ( $wgUseSquid ) {
+                       // Commit the transaction before the purge is sent
+                       $dbw = wfGetDB( DB_MASTER );
+                       $dbw->immediateCommit();
+
+                       // Send purge
+                       $update = SquidUpdate::newSimplePurge( $this->mTitle );
+                       $update->doUpdate();
+               }
+               $this->view();
+       }
 
        /**
         * Insert a new empty page record for this article.
@@ -1121,14 +1160,11 @@ class Article {
        }
 
        /**
-        * Theoretically we could defer these whole insert and update
-        * functions for after display, but that's taking a big leap
-        * of faith, and we want to be able to report database
-        * errors at some point.
+        * Insert a new article into the database
         * @access private
         */
        function insertNewArticle( $text, $summary, $isminor, $watchthis, $suppressRC=false, $comment=false ) {
-               global $wgOut, $wgUser, $wgUseSquid;
+               global $wgUser;
 
                $fname = 'Article::insertNewArticle';
                wfProfileIn( $fname );
@@ -1140,9 +1176,6 @@ class Article {
                        return false;
                }
 
-               $this->mGoodAdjustment = (int)$this->isCountable( $text );
-               $this->mTotalAdjustment = 1;
-
                $ns = $this->mTitle->getNamespace();
                $ttl = $this->mTitle->getDBkey();
 
@@ -1152,6 +1185,13 @@ class Article {
                }
                $text = $this->preSaveTransform( $text );
 
+
+               # Set statistics members
+               # We work out if it's countable after PST to avoid counter drift 
+               # when articles are created with {{subst:}}
+               $this->mGoodAdjustment = (int)$this->isCountable( $text );
+               $this->mTotalAdjustment = 1;
+
                /* Silently ignore minoredit if not allowed */
                $isminor = $isminor && $wgUser->isAllowed('minoredit');
                $now = wfTimestampNow();
@@ -1178,15 +1218,19 @@ class Article {
                Article::onArticleCreate( $this->mTitle );
                if(!$suppressRC) {
                        require_once( 'RecentChange.php' );
-                       RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary, 'default',
+                       $rcid = RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary, 'default',
                          '', strlen( $text ), $revisionId );
+                       # Mark as patrolled if the user can and has the option set
+                       if( $wgUser->isAllowed( 'patrol' ) && $wgUser->getOption( 'autopatrol' ) ) {
+                               RecentChange::markPatrolled( $rcid );
+                       }
                }
 
                if ($watchthis) {
-                       if(!$this->mTitle->userIsWatching()) $this->watch();
+                       if(!$this->mTitle->userIsWatching()) $this->doWatch();
                } else {
                        if ( $this->mTitle->userIsWatching() ) {
-                               $this->unwatch();
+                               $this->doUnwatch();
                        }
                }
 
@@ -1319,7 +1363,7 @@ class Article {
         * first set $wgUser, and clean up $wgDeferredUpdates after each edit.
         */
        function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false, $sectionanchor = '' ) {
-               global $wgOut, $wgUser, $wgDBtransactions, $wgMwRedir, $wgUseSquid;
+               global $wgUser, $wgDBtransactions, $wgUseSquid;
                global $wgPostCommitUpdateList, $wgUseFileCache;
 
                $fname = 'Article::updateArticle';
@@ -1387,9 +1431,15 @@ class Article {
                                # Update recentchanges and purge cache and whatnot
                                require_once( 'RecentChange.php' );
                                $bot = (int)($wgUser->isBot() || $forceBot);
-                               RecentChange::notifyEdit( $now, $this->mTitle, $isminor, $wgUser, $summary,
+                               $rcid = RecentChange::notifyEdit( $now, $this->mTitle, $isminor, $wgUser, $summary,
                                        $lastRevision, $this->getTimestamp(), $bot, '', $oldsize, $newsize,
                                        $revisionId );
+                                       
+                               # Mark as patrolled if the user can do so and has it set in their options
+                               if( $wgUser->isAllowed( 'patrol' ) && $wgUser->getOption( 'autopatrol' ) ) {
+                                       RecentChange::markPatrolled( $rcid );
+                               }
+                                       
                                $dbw->commit();
 
                                // Update caches outside the main transaction
@@ -1409,14 +1459,14 @@ class Article {
                                if (!$this->mTitle->userIsWatching()) {
                                        $dbw->immediateCommit();
                                        $dbw->begin();
-                                       $this->watch();
+                                       $this->doWatch();
                                        $dbw->commit();
                                }
                        } else {
                                if ( $this->mTitle->userIsWatching() ) {
                                        $dbw->immediateCommit();
                                        $dbw->begin();
-                                       $this->unwatch();
+                                       $this->doUnwatch();
                                        $dbw->commit();
                                }
                        }
@@ -1465,8 +1515,7 @@ class Article {
         * the link tables and redirect to the new page.
         */
        function showArticle( $text, $subtitle , $sectionanchor = '', $me2, $now, $summary, $oldid ) {
-               global $wgOut, $wgUser;
-               global $wgUseEnotif;
+               global $wgOut;
 
                $fname = 'Article::showArticle';
                wfProfileIn( $fname );
@@ -1485,7 +1534,7 @@ class Article {
         * Mark this particular edit as patrolled
         */
        function markpatrolled() {
-               global $wgOut, $wgRequest, $wgOnlySysopsCanPatrol, $wgUseRCPatrol, $wgUser;
+               global $wgOut, $wgRequest, $wgUseRCPatrol, $wgUser;
                $wgOut->setRobotpolicy( 'noindex,follow' );
 
                # Check RC patrol config. option
@@ -1495,37 +1544,30 @@ class Article {
                }
                
                # Check permissions
-               if( $wgUser->isLoggedIn() ) {
-                       if( !$wgUser->isAllowed( 'patrol' ) ) {
-                               $wgOut->permissionRequired( 'patrol' );
-                               return;
-                       }
-               } else {
-                       $wgOut->loginToUse();
+               if( !$wgUser->isAllowed( 'patrol' ) ) {
+                       $wgOut->permissionRequired( 'patrol' );
                        return;
                }
                
                $rcid = $wgRequest->getVal( 'rcid' );
-               if ( !is_null ( $rcid ) )
-               {
-                       if( wfRunHooks( 'MarkPatrolled', array( &$rcid, &$wgUser, $wgOnlySysopsCanPatrol ) ) ) {
+               if ( !is_null ( $rcid ) ) {
+                       if( wfRunHooks( 'MarkPatrolled', array( &$rcid, &$wgUser, false ) ) ) {
                                require_once( 'RecentChange.php' );
                                RecentChange::markPatrolled( $rcid );
-                               wfRunHooks( 'MarkPatrolledComplete', array( &$rcid, &$wgUser, $wgOnlySysopsCanPatrol ) );
+                               wfRunHooks( 'MarkPatrolledComplete', array( &$rcid, &$wgUser, false ) );
                                $wgOut->setPagetitle( wfMsg( 'markedaspatrolled' ) );
                                $wgOut->addWikiText( wfMsg( 'markedaspatrolledtext' ) );
                        }
                        $rcTitle = Title::makeTitle( NS_SPECIAL, 'Recentchanges' );
                        $wgOut->returnToMain( false, $rcTitle->getPrefixedText() );
                }
-               else
-               {
+               else {
                        $wgOut->errorpage( 'markedaspatrollederror', 'markedaspatrollederrortext' );
                }
        }
 
        /**
-        * Add this page to $wgUser's watchlist
+        * User-interface handler for the "watch" action
         */
 
        function watch() {
@@ -1540,14 +1582,8 @@ class Article {
                        $wgOut->readOnlyPage();
                        return;
                }
-
-               if (wfRunHooks('WatchArticle', array(&$wgUser, &$this))) {
-
-                       $wgUser->addWatch( $this->mTitle );
-                       $wgUser->saveSettings();
-
-                       wfRunHooks('WatchArticleComplete', array(&$wgUser, &$this));
-
+               
+               if( $this->doWatch() ) {
                        $wgOut->setPagetitle( wfMsg( 'addedwatch' ) );
                        $wgOut->setRobotpolicy( 'noindex,follow' );
 
@@ -1558,11 +1594,30 @@ class Article {
 
                $wgOut->returnToMain( true, $this->mTitle->getPrefixedText() );
        }
-
+       
        /**
-        * Stop watching a page
+        * Add this page to $wgUser's watchlist
+        * @return bool true on successful watch operation
         */
+       function doWatch() {
+               global $wgUser;
+               if( $wgUser->isAnon() ) {
+                       return false;
+               }
+               
+               if (wfRunHooks('WatchArticle', array(&$wgUser, &$this))) {
+                       $wgUser->addWatch( $this->mTitle );
+                       $wgUser->saveSettings();
 
+                       return wfRunHooks('WatchArticleComplete', array(&$wgUser, &$this));
+               }
+               
+               return false;
+       }
+
+       /**
+        * User interface handler for the "unwatch" action.
+        */
        function unwatch() {
 
                global $wgUser, $wgOut;
@@ -1575,14 +1630,8 @@ class Article {
                        $wgOut->readOnlyPage();
                        return;
                }
-
-               if (wfRunHooks('UnwatchArticle', array(&$wgUser, &$this))) {
-
-                       $wgUser->removeWatch( $this->mTitle );
-                       $wgUser->saveSettings();
-
-                       wfRunHooks('UnwatchArticleComplete', array(&$wgUser, &$this));
-
+               
+               if( $this->doUnwatch() ) {
                        $wgOut->setPagetitle( wfMsg( 'removedwatch' ) );
                        $wgOut->setRobotpolicy( 'noindex,follow' );
 
@@ -1593,6 +1642,26 @@ class Article {
 
                $wgOut->returnToMain( true, $this->mTitle->getPrefixedText() );
        }
+       
+       /**
+        * Stop watching a page
+        * @return bool true on successful unwatch
+        */
+       function doUnwatch() {
+               global $wgUser;
+               if( $wgUser->isAnon() ) {
+                       return false;
+               }
+
+               if (wfRunHooks('UnwatchArticle', array(&$wgUser, &$this))) {
+                       $wgUser->removeWatch( $this->mTitle );
+                       $wgUser->saveSettings();
+
+                       return wfRunHooks('UnwatchArticleComplete', array(&$wgUser, &$this));
+               }
+               
+               return false;
+       }
 
        /**
         * action=protect handler
@@ -1618,47 +1687,63 @@ class Article {
         * @return bool true on success
         */
        function updateRestrictions( $limit = array(), $reason = '' ) {
-               global $wgUser, $wgOut, $wgRequest;
-
-               if ( !$wgUser->isAllowed( 'protect' ) ) {
-                       return false;
-               }
-
-               if( wfReadOnly() ) {
-                       return false;
-               }
-
+               global $wgUser, $wgRestrictionTypes, $wgContLang;
+               
                $id = $this->mTitle->getArticleID();
-               if ( 0 == $id ) {
+               if( !$wgUser->isAllowed( 'protect' ) || wfReadOnly() || $id == 0 ) {
                        return false;
                }
 
-               $flat = Article::flattenRestrictions( $limit );
-               $protecting = ($flat != '');
+               # FIXME: Same limitations as described in ProtectionForm.php (line 37);
+               # we expect a single selection, but the schema allows otherwise.
+               $current = array();
+               foreach( $wgRestrictionTypes as $action )
+                       $current[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
 
-               if( wfRunHooks( 'ArticleProtect', array( &$this, &$wgUser,
-                       $limit, $reason ) ) ) {
-
-                       $dbw =& wfGetDB( DB_MASTER );
-                       $dbw->update( 'page',
-                               array( /* SET */
-                                       'page_touched' => $dbw->timestamp(),
-                                       'page_restrictions' => $flat
-                               ), array( /* WHERE */
-                                       'page_id' => $id
-                               ), 'Article::protect'
-                       );
-
-                       wfRunHooks( 'ArticleProtectComplete', array( &$this, &$wgUser,
-                               $limit, $reason ) );
+               $current = Article::flattenRestrictions( $current );
+               $updated = Article::flattenRestrictions( $limit );
+               
+               $changed = ( $current != $updated );
+               $protect = ( $updated != '' );
+               
+               # If nothing's changed, do nothing
+               if( $changed ) {
+                       if( wfRunHooks( 'ArticleProtect', array( &$this, &$wgUser, $limit, $reason ) ) ) {
 
-                       $log = new LogPage( 'protect' );
-                       if( $protecting ) {
-                               $log->addEntry( 'protect', $this->mTitle, trim( $reason . " [$flat]" ) );
-                       } else {
-                               $log->addEntry( 'unprotect', $this->mTitle, $reason );
-                       }
-               }
+                               $dbw =& wfGetDB( DB_MASTER );
+                               
+                               # Prepare a null revision to be added to the history
+                               $comment = $wgContLang->ucfirst( wfMsgForContent( $protect ? 'protectedarticle' : 'unprotectedarticle', $this->mTitle->getPrefixedText() ) );
+                               if( $reason )
+                                       $comment .= ": $reason";
+                               if( $protect )
+                                       $comment .= " [$updated]";
+                               $nullRevision = Revision::newNullRevision( $dbw, $id, $comment, true );
+                               $nullRevId = $nullRevision->insertOn( $dbw );
+                       
+                               # Update page record
+                               $dbw->update( 'page',
+                                       array( /* SET */
+                                               'page_touched' => $dbw->timestamp(),
+                                               'page_restrictions' => $updated,
+                                               'page_latest' => $nullRevId
+                                       ), array( /* WHERE */
+                                               'page_id' => $id
+                                       ), 'Article::protect'
+                               );
+                               wfRunHooks( 'ArticleProtectComplete', array( &$this, &$wgUser, $limit, $reason ) );
+       
+                               # Update the protection log
+                               $log = new LogPage( 'protect' );
+                               if( $protect ) {
+                                       $log->addEntry( 'protect', $this->mTitle, trim( $reason . " [$updated]" ) );
+                               } else {
+                                       $log->addEntry( 'unprotect', $this->mTitle, $reason );
+                               }
+                               
+                       } # End hook
+               } # End "changed" check
+               
                return true;
        }
 
@@ -1674,6 +1759,7 @@ class Article {
                        wfDebugDieBacktrace( 'Article::flattenRestrictions given non-array restriction set' );
                }
                $bits = array();
+               ksort( $limit );
                foreach( $limit as $action => $restrictions ) {
                        if( $restrictions != '' ) {
                                $bits[] = "$action=$restrictions";
@@ -1733,8 +1819,7 @@ class Article {
                
                if( count( $authors ) > 1 && !$confirm ) {
                        $skin=$wgUser->getSkin();
-                       $wgOut->addHTML('<b>'.wfMsg('historywarning'));
-                       $wgOut->addHTML( $skin->historyLink() .'</b>');
+                       $wgOut->addHTML( '<strong>' . wfMsg( 'historywarning' ) . ' ' . $skin->historyLink() . '</strong>' );
                }
 
                # If a single user is responsible for all revisions, find out who they are
@@ -1800,7 +1885,7 @@ class Article {
        }
 
        /**
-        * Get the last N authors 
+        * Get the last N authors
         * @param int $num Number of revisions to get
         * @param string $revLatest The latest rev_id, selected from the master (optional)
         * @return array Array of authors, duplicates not removed
@@ -1820,7 +1905,7 @@ class Article {
                                        'page_namespace' => $this->mTitle->getNamespace(),
                                        'page_title' => $this->mTitle->getDBkey(),
                                        'rev_page = page_id'
-                               ), $fname, $this->getSelectOptions( array( 
+                               ), $fname, $this->getSelectOptions( array(
                                        'ORDER BY' => 'rev_timestamp DESC',
                                        'LIMIT' => $num
                                ) )
@@ -1894,7 +1979,7 @@ class Article {
         * Perform a deletion and output success or failure messages
         */
        function doDelete( $reason ) {
-               global $wgOut, $wgUser, $wgContLang;
+               global $wgOut, $wgUser;
                $fname = 'Article::doDelete';
                wfDebug( $fname."\n" );
 
@@ -1923,7 +2008,7 @@ class Article {
         * Returns success
         */
        function doDeleteArticle( $reason ) {
-               global $wgUser, $wgUseSquid, $wgDeferredUpdateList;
+               global $wgUseSquid, $wgDeferredUpdateList;
                global $wgPostCommitUpdateList, $wgUseTrackbacks;
 
                $fname = 'Article::doDeleteArticle';
@@ -2005,6 +2090,8 @@ class Article {
                $dbw->delete( 'pagelinks', array( 'pl_from' => $id ) );
                $dbw->delete( 'imagelinks', array( 'il_from' => $id ) );
                $dbw->delete( 'categorylinks', array( 'cl_from' => $id ) );
+               $dbw->delete( 'templatelinks', array( 'tl_from' => $id ) );
+               $dbw->delete( 'externallinks', array( 'el_from' => $id ) );
 
                # Log the deletion
                $log = new LogPage( 'delete' );
@@ -2172,6 +2259,7 @@ class Article {
 
                # Parse the text
                $options = new ParserOptions;
+               $options->setTidy(true);
                $poutput = $wgParser->parse( $text, $this->mTitle, $options, true, true, $newid );
 
                # Save it to the parser cache
@@ -2213,14 +2301,16 @@ class Article {
                # If this is another user's talk page, update newtalk
 
                if ($this->mTitle->getNamespace() == NS_USER_TALK && $shortTitle != $wgUser->getName()) {
-                       $other = User::newFromName( $shortTitle );
-                       if( is_null( $other ) && User::isIP( $shortTitle ) ) {
-                               // An anonymous user
-                               $other = new User();
-                               $other->setName( $shortTitle );
-                       }
-                       if( $other ) {
-                               $other->setNewtalk( true );
+                       if (wfRunHooks('ArticleEditUpdateNewTalk', array(&$this)) ) {
+                               $other = User::newFromName( $shortTitle );
+                               if( is_null( $other ) && User::isIP( $shortTitle ) ) {
+                                       // An anonymous user
+                                       $other = new User();
+                                       $other->setName( $shortTitle );
+                               }
+                               if( $other ) {
+                                       $other->setNewtalk( true );
+                               }
                        }
                }
 
@@ -2232,7 +2322,11 @@ class Article {
        }
 
        /**
-        * @todo document this function
+        * Generate the navigation links when browsing through an article revisions
+        * It shows the information as:
+        *   Revision as of <date>; view current revision
+        *   <- Previous version | Next Version ->
+        *
         * @access private
         * @param string $oldid         Revision ID of this article revision
         */
@@ -2245,7 +2339,10 @@ class Article {
                $lnk = $current
                        ? wfMsg( 'currentrevisionlink' )
                        : $lnk = $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'currentrevisionlink' ) );
-               $prevlink = $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'previousrevision' ), 'direction=prev&oldid='.$oldid );
+               $prev = $this->mTitle->getPreviousRevisionID( $oldid ) ;
+               $prevlink = $prev
+                       ? $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'previousrevision' ), 'direction=prev&oldid='.$oldid )
+                       : wfMsg( 'previousrevision' );
                $nextlink = $current
                        ? wfMsg( 'nextrevision' )
                        : $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'nextrevision' ), 'direction=next&oldid='.$oldid );
@@ -2282,7 +2379,6 @@ class Article {
                        $touched = $this->mTouched;
                        $cache = new CacheManager( $this->mTitle );
                        if($cache->isFileCacheGood( $touched )) {
-                               global $wgOut;
                                wfDebug( " tryFileCache() - about to load\n" );
                                $cache->loadFromFileCache();
                                return true;
@@ -2324,11 +2420,7 @@ class Article {
        function checkTouched() {
                $fname = 'Article::checkTouched';
                if( !$this->mDataLoaded ) {
-                       $dbr =& $this->getDB();
-                       $data = $this->pageDataFromId( $dbr, $this->getId() );
-                       if( $data ) {
-                               $this->loadPageData( $data );
-                       }
+                       $this->loadPageData();
                }
                return !$this->mIsRedirect;
        }
@@ -2339,15 +2431,21 @@ class Article {
        function getTouched() {
                # Ensure that page data has been loaded
                if( !$this->mDataLoaded ) {
-                       $dbr =& $this->getDB();
-                       $data = $this->pageDataFromId( $dbr, $this->getId() );
-                       if( $data ) {
-                               $this->loadPageData( $data );
-                       }
+                       $this->loadPageData();
                }
                return $this->mTouched;
        }
 
+       /**
+        * Get the page_latest field
+        */
+       function getLatest() {
+               if ( !$this->mDataLoaded ) {
+                       $this->loadPageData();
+               }
+               return $this->mLatest;
+       }
+
        /**
         * Edit an article without doing all that other stuff
         * The article must already exist; link tables etc
@@ -2608,6 +2706,9 @@ class Article {
        function getUsedTemplates() {
                $result = array();
                $id = $this->mTitle->getArticleID();
+               if( $id == 0 ) {
+                       return array();
+               }
 
                $dbr =& wfGetDB( DB_SLAVE );
                $res = $dbr->select( array( 'templatelinks' ),