Don't require an existence check before calling loadPageData(). Added an accessor...
[lhc/web/wiklou.git] / includes / Article.php
index 6a1c0e3..18e5f8d 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;
        }
 
        /**
@@ -208,11 +207,6 @@ class Article {
        /**
         * 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 +348,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 +418,28 @@ 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 {
+                       $lc->addBadLinkObj( $this->mTitle );
+                       $this->mTitle->mArticleID = 0;
+               }
 
                $this->mDataLoaded  = true;
        }
@@ -566,9 +572,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 +633,6 @@ class Article {
         * @access private
         */
        function loadLastEdit() {
-               global $wgOut;
-
                if ( -1 != $this->mUser )
                        return;
 
@@ -644,7 +652,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);
        }
 
@@ -818,8 +829,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
@@ -978,23 +991,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();
+               global $wgUser, $wgRequest, $wgOut;
 
-                       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();
+               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' );
@@ -1009,6 +1015,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.
@@ -1122,14 +1148,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 );
@@ -1184,10 +1207,10 @@ class Article {
                }
 
                if ($watchthis) {
-                       if(!$this->mTitle->userIsWatching()) $this->watch();
+                       if(!$this->mTitle->userIsWatching()) $this->doWatch();
                } else {
                        if ( $this->mTitle->userIsWatching() ) {
-                               $this->unwatch();
+                               $this->doUnwatch();
                        }
                }
 
@@ -1320,7 +1343,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';
@@ -1410,14 +1433,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();
                                }
                        }
@@ -1466,8 +1489,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 );
@@ -1526,7 +1548,7 @@ class Article {
        }
 
        /**
-        * Add this page to $wgUser's watchlist
+        * User-interface handler for the "watch" action
         */
 
        function watch() {
@@ -1541,14 +1563,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' );
 
@@ -1559,11 +1575,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;
@@ -1576,14 +1611,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' );
 
@@ -1594,6 +1623,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
@@ -1619,7 +1668,7 @@ class Article {
         * @return bool true on success
         */
        function updateRestrictions( $limit = array(), $reason = '' ) {
-               global $wgUser, $wgOut, $wgRequest;
+               global $wgUser;
 
                if ( !$wgUser->isAllowed( 'protect' ) ) {
                        return false;
@@ -1801,7 +1850,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
@@ -1821,7 +1870,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
                                ) )
@@ -1895,7 +1944,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" );
 
@@ -1924,7 +1973,7 @@ class Article {
         * Returns success
         */
        function doDeleteArticle( $reason ) {
-               global $wgUser, $wgUseSquid, $wgDeferredUpdateList;
+               global $wgUseSquid, $wgDeferredUpdateList;
                global $wgPostCommitUpdateList, $wgUseTrackbacks;
 
                $fname = 'Article::doDeleteArticle';
@@ -2006,6 +2055,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' );
@@ -2173,6 +2224,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
@@ -2214,14 +2266,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 );
+                               }
                        }
                }
 
@@ -2233,7 +2287,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
         */
@@ -2246,7 +2304,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 );
@@ -2283,7 +2344,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;
@@ -2325,11 +2385,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;
        }
@@ -2340,15 +2396,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
@@ -2417,7 +2479,7 @@ class Article {
                        $old_user_abort = ignore_user_abort( true );
 
                        $dbw->query("LOCK TABLES $hitcounterTable WRITE");
-                       $dbw->query("CREATE TEMPORARY TABLE $acchitsTable TYPE=HEAP ".
+                       $dbw->query("CREATE TEMPORARY TABLE $acchitsTable ENGINE=HEAP ".
                                "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable ".
                                'GROUP BY hc_id');
                        $dbw->query("DELETE FROM $hitcounterTable");
@@ -2609,6 +2671,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' ),