Merge "fix some spacing"
[lhc/web/wiklou.git] / includes / WikiPage.php
index 7686ed8..2da314f 100644 (file)
@@ -23,7 +23,7 @@
 /**
  * Abstract class for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
  */
-abstract class Page {}
+interface Page {}
 
 /**
  * Class representing a MediaWiki article and history.
@@ -33,7 +33,7 @@ abstract class Page {}
  *
  * @internal documentation reviewed 15 Mar 2010
  */
-class WikiPage extends Page implements IDBAccessObject {
+class WikiPage implements Page, IDBAccessObject {
        // Constants for $mDataLoadedFrom and related
 
        /**
@@ -50,6 +50,11 @@ class WikiPage extends Page implements IDBAccessObject {
        public $mPreparedEdit = false;       // !< Array
        /**@}}*/
 
+       /**
+        * @var int
+        */
+       protected $mId = null;
+
        /**
         * @var int; one of the READ_* constants
         */
@@ -228,6 +233,7 @@ class WikiPage extends Page implements IDBAccessObject {
         * @return void
         */
        protected function clearCacheFields() {
+               $this->mId = null;
                $this->mCounter = null;
                $this->mRedirectTarget = null; // Title object if set
                $this->mLastRevision = null; // Latest revision
@@ -380,10 +386,11 @@ class WikiPage extends Page implements IDBAccessObject {
                        // Old-fashioned restrictions
                        $this->mTitle->loadRestrictions( $data->page_restrictions );
 
-                       $this->mCounter     = intval( $data->page_counter );
-                       $this->mTouched     = wfTimestamp( TS_MW, $data->page_touched );
-                       $this->mIsRedirect  = intval( $data->page_is_redirect );
-                       $this->mLatest      = intval( $data->page_latest );
+                       $this->mId = intval( $data->page_id );
+                       $this->mCounter = intval( $data->page_counter );
+                       $this->mTouched = wfTimestamp( TS_MW, $data->page_touched );
+                       $this->mIsRedirect = intval( $data->page_is_redirect );
+                       $this->mLatest = intval( $data->page_latest );
                        // Bug 37225: $latest may no longer match the cached latest Revision object.
                        // Double-check the ID of any cached latest Revision object for consistency.
                        if ( $this->mLastRevision && $this->mLastRevision->getId() != $this->mLatest ) {
@@ -396,6 +403,8 @@ class WikiPage extends Page implements IDBAccessObject {
                        $this->mTitle->loadFromRow( false );
 
                        $this->clearCacheFields();
+
+                       $this->mId = 0;
                }
 
                $this->mDataLoaded = true;
@@ -406,14 +415,20 @@ class WikiPage extends Page implements IDBAccessObject {
         * @return int Page ID
         */
        public function getId() {
-               return $this->mTitle->getArticleID();
+               if ( !$this->mDataLoaded ) {
+                       $this->loadPageData();
+               }
+               return $this->mId;
        }
 
        /**
         * @return bool Whether or not the page exists in the database
         */
        public function exists() {
-               return $this->mTitle->exists();
+               if ( !$this->mDataLoaded ) {
+                       $this->loadPageData();
+               }
+               return $this->mId > 0;
        }
 
        /**
@@ -425,7 +440,7 @@ class WikiPage extends Page implements IDBAccessObject {
         * @return bool
         */
        public function hasViewableContent() {
-               return $this->mTitle->exists() || $this->mTitle->isAlwaysKnown();
+               return $this->exists() || $this->mTitle->isAlwaysKnown();
        }
 
        /**
@@ -1067,7 +1082,7 @@ class WikiPage extends Page implements IDBAccessObject {
 
                return $wgEnableParserCache
                        && $parserOptions->getStubThreshold() == 0
-                       && $this->mTitle->exists()
+                       && $this->exists()
                        && ( $oldid === null || $oldid === 0 || $oldid === $this->getLatest() )
                        && $this->getContentHandler()->isParserCacheSupported();
        }
@@ -1123,7 +1138,7 @@ class WikiPage extends Page implements IDBAccessObject {
                }
 
                // Don't update page view counters on views from bot users (bug 14044)
-               if ( !$wgDisableCounters && !$user->isAllowed( 'bot' ) && $this->mTitle->exists() ) {
+               if ( !$wgDisableCounters && !$user->isAllowed( 'bot' ) && $this->exists() ) {
                        DeferredUpdates::addUpdate( new ViewCountUpdate( $this->getId() ) );
                        DeferredUpdates::addUpdate( new SiteStatsUpdate( 1, 0, 0 ) );
                }
@@ -1160,7 +1175,7 @@ class WikiPage extends Page implements IDBAccessObject {
                if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
                        // @todo: move this logic to MessageCache
 
-                       if ( $this->mTitle->exists() ) {
+                       if ( $this->exists() ) {
                                // NOTE: use transclusion text for messages.
                                //       This is consistent with  MessageCache::getMsgFromNamespace()
 
@@ -1209,6 +1224,7 @@ class WikiPage extends Page implements IDBAccessObject {
 
                if ( $affected ) {
                        $newid = $dbw->insertId();
+                       $this->mId = $newid;
                        $this->mTitle->resetArticleID( $newid );
                }
                wfProfileOut( __METHOD__ );
@@ -1506,7 +1522,7 @@ class WikiPage extends Page implements IDBAccessObject {
         */
        function checkFlags( $flags ) {
                if ( !( $flags & EDIT_NEW ) && !( $flags & EDIT_UPDATE ) ) {
-                       if ( $this->mTitle->getArticleID() ) {
+                       if ( $this->exists() ) {
                                $flags |= EDIT_UPDATE;
                        } else {
                                $flags |= EDIT_NEW;
@@ -1688,7 +1704,7 @@ class WikiPage extends Page implements IDBAccessObject {
                $editInfo = $this->prepareContentForEdit( $content, null, $user, $serialisation_format );
                $serialized = $editInfo->pst;
                $content = $editInfo->pstContent;
-               $newsize =  $content->getSize();
+               $newsize = $content->getSize();
 
                $dbw = wfGetDB( DB_MASTER );
                $now = wfTimestampNow();
@@ -2068,7 +2084,7 @@ class WikiPage extends Page implements IDBAccessObject {
                        }
                }
 
-               if ( !$this->mTitle->exists() ) {
+               if ( !$this->exists() ) {
                        wfProfileOut( __METHOD__ );
                        return;
                }
@@ -2207,7 +2223,7 @@ class WikiPage extends Page implements IDBAccessObject {
 
                $restrictionTypes = $this->mTitle->getRestrictionTypes();
 
-               $id = $this->mTitle->getArticleID();
+               $id = $this->getId();
 
                if ( !$cascade ) {
                        $cascade = false;
@@ -2357,7 +2373,7 @@ class WikiPage extends Page implements IDBAccessObject {
                                )->inContentLanguage()->text()
                        );
                        if ( $reason ) {
-                               $editComment .= ": $reason";
+                               $editComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
                        }
                        if ( $protectDescription ) {
                                $editComment .= wfMessage( 'word-separator' )->inContentLanguage()->text();
@@ -2588,7 +2604,7 @@ class WikiPage extends Page implements IDBAccessObject {
 
                // 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
+               $ok = ( $dbw->affectedRows() > 0 ); // $id could be laggy
 
                if ( !$ok ) {
                        $dbw->rollback( __METHOD__ );
@@ -2635,11 +2651,8 @@ class WikiPage extends Page implements IDBAccessObject {
                // Clear caches
                WikiPage::onArticleDelete( $this->mTitle );
 
-               // Reset this object
-               $this->clear();
-
-               // Clear the cached article id so the interface doesn't act like we exist
-               $this->mTitle->resetArticleID( 0 );
+               // Reset this object and the Title object
+               $this->loadFromRow( false, self::READ_LATEST );
        }
 
        /**
@@ -2943,7 +2956,7 @@ class WikiPage extends Page implements IDBAccessObject {
         */
        public function getHiddenCategories() {
                $result = array();
-               $id = $this->mTitle->getArticleID();
+               $id = $this->getId();
 
                if ( $id == 0 ) {
                        return array();
@@ -2966,14 +2979,14 @@ class WikiPage extends Page implements IDBAccessObject {
        }
 
        /**
-       * Return an applicable autosummary if one exists for the given edit.
-       * @param $oldtext String|null: the previous text of the page.
-       * @param $newtext String|null: The submitted text of the page.
-       * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
-       * @return string An appropriate autosummary, or an empty string.
-       *
-       * @deprecated since 1.21, use ContentHandler::getAutosummary() instead
-       */
+        * Return an applicable autosummary if one exists for the given edit.
+        * @param $oldtext String|null: the previous text of the page.
+        * @param $newtext String|null: The submitted text of the page.
+        * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
+        * @return string An appropriate autosummary, or an empty string.
+        *
+        * @deprecated since 1.21, use ContentHandler::getAutosummary() instead
+        */
        public static function getAutosummary( $oldtext, $newtext, $flags ) {
                // NOTE: stub for backwards-compatibility. assumes the given text is wikitext. will break horribly if it isn't.
 
@@ -3030,14 +3043,14 @@ class WikiPage extends Page implements IDBAccessObject {
                }
                $dbw->insert( 'category', $insertRows, __METHOD__, 'IGNORE' );
 
-               $addFields    = array( 'cat_pages = cat_pages + 1' );
+               $addFields = array( 'cat_pages = cat_pages + 1' );
                $removeFields = array( 'cat_pages = cat_pages - 1' );
 
                if ( $ns == NS_CATEGORY ) {
-                       $addFields[]    = 'cat_subcats = cat_subcats + 1';
+                       $addFields[] = 'cat_subcats = cat_subcats + 1';
                        $removeFields[] = 'cat_subcats = cat_subcats - 1';
                } elseif ( $ns == NS_FILE ) {
-                       $addFields[]    = 'cat_files = cat_files + 1';
+                       $addFields[] = 'cat_files = cat_files + 1';
                        $removeFields[] = 'cat_files = cat_files - 1';
                }
 
@@ -3058,6 +3071,15 @@ class WikiPage extends Page implements IDBAccessObject {
                                __METHOD__
                        );
                }
+
+               foreach( $added as $catName ) {
+                       $cat = Category::newFromName( $catName );
+                       wfRunHooks( 'CategoryAfterPageAdded', array( $cat, $this ) );
+               }
+               foreach( $deleted as $catName ) {
+                       $cat = Category::newFromName( $catName );
+                       wfRunHooks( 'CategoryAfterPageRemoved', array( $cat, $this ) );
+               }
        }
 
        /**
@@ -3078,7 +3100,7 @@ class WikiPage extends Page implements IDBAccessObject {
                // are visible.
 
                // Get templates from templatelinks
-               $id = $this->mTitle->getArticleID();
+               $id = $this->getId();
 
                $tlTemplates = array();
 
@@ -3315,7 +3337,7 @@ class PoolWorkArticleView extends PoolCounterWork {
         * @param $content Content|String: content to parse or null to load it; may also be given as a wikitext string, for BC
         */
        function __construct( Page $page, ParserOptions $parserOptions, $revid, $useParserCache, $content = null ) {
-               if ( is_string($content) ) { // BC: old style call
+               if ( is_string( $content ) ) { // BC: old style call
                        $modelId = $page->getRevision()->getContentModel();
                        $format = $page->getRevision()->getContentFormat();
                        $content = ContentHandler::makeContent( $content, $page->getTitle(), $modelId, $format );