* Fixed getLatest() check in Article
authorAaron Schulz <aaron@users.mediawiki.org>
Fri, 1 Jul 2011 23:33:34 +0000 (23:33 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Fri, 1 Jul 2011 23:33:34 +0000 (23:33 +0000)
* Added Title::loadFromRow() function and made WikiPage::loadPageData() use it; avoids raw Title field accessing
* Added Revision::newFromPageId() function and changed WikiPage::loadLastEdit() to use it. This makes it try a slave first instead of always hitting the master. It also makes it more consistent with getLatest() for sanity.
* Made WikiPage::loadPageData() use accessor for Title::mRestrictionsExpiry

includes/Article.php
includes/Revision.php
includes/Title.php
includes/WikiPage.php

index 76d419e..47c540d 100644 (file)
@@ -294,7 +294,7 @@ class Article extends Page {
                                }
                        }
                } else {
-                       if ( $this->mPage->getLatest() === false ) {
+                       if ( !$this->mPage->getLatest() ) {
                                wfDebug( __METHOD__ . " failed to find page data for title " . $this->getTitle()->getPrefixedText() . "\n" );
                                return false;
                        }
index 8496683..fa560d3 100644 (file)
@@ -34,7 +34,7 @@ class Revision {
         * to that title, will return null.
         *
         * @param $title Title
-        * @param $id Integer
+        * @param $id Integer (optional)
         * @return Revision or null
         */
        public static function newFromTitle( $title, $id = 0 ) {
@@ -50,8 +50,7 @@ class Revision {
                        $dbw = wfGetDB( DB_MASTER );
                        $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
                        if ( $latest === false ) {
-                               // Page does not exist
-                               return null;
+                               return null; // page does not exist
                        }
                        $conds['rev_id'] = $latest;
                } else {
@@ -62,6 +61,33 @@ class Revision {
                return Revision::newFromConds( $conds );
        }
 
+       /**
+        * Load either the current, or a specified, revision
+        * that's attached to a given page ID.
+        * Returns null if no such revision can be found.
+        *
+        * @param $revId Integer
+        * @param $pageId Integer (optional)
+        * @return Revision or null
+        */
+       public static function newFromPageId( $pageId, $revId = 0 ) {
+               $conds = array( 'page_id' => $pageId );
+               if ( $revId ) {
+                       $conds['rev_id'] = $pageId;
+               } elseif ( wfGetLB()->getServerCount() > 1 ) {
+                       // Get the latest revision ID from the master
+                       $dbw = wfGetDB( DB_MASTER );
+                       $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
+                       if ( $latest === false ) {
+                               return null; // page does not exist
+                       }
+                       $conds['rev_id'] = $latest;
+               } else {
+                       $conds[] = 'rev_id = page_latest';
+               }
+               return Revision::newFromConds( $conds );
+       }
+
        /**
         * Make a fake revision object from an archive table row. This is queried
         * for permissions or even inserted (as in Special:Undelete)
index 63d690e..f170213 100644 (file)
@@ -252,15 +252,35 @@ class Title {
         */
        public static function newFromRow( $row ) {
                $t = self::makeTitle( $row->page_namespace, $row->page_title );
-
-               $t->mArticleID = isset( $row->page_id ) ? intval( $row->page_id ) : -1;
-               $t->mLength = isset( $row->page_len ) ? intval( $row->page_len ) : -1;
-               $t->mRedirect = isset( $row->page_is_redirect ) ? (bool)$row->page_is_redirect : null;
-               $t->mLatestID = isset( $row->page_latest ) ? intval( $row->page_latest ) : false;
-
+               $t->loadFromRow( $row );
                return $t;
        }
 
+       /**
+        * Load Title object fields from a DB row.
+        * If false is given, the title will be treated as non-existing.
+        *
+        * @param $row Object|false database row
+        * @return void
+        */
+       public function loadFromRow( $row ) {
+               if ( $row ) { // page found
+                       if ( isset( $row->page_id ) )
+                               $this->mArticleID = (int)$row->page_id;
+                       if ( isset( $row->page_len ) )
+                               $this->mLength = (int)$row->page_len;
+                       if ( isset( $row->page_is_redirect ) )
+                               $this->mRedirect = (bool)$row->page_is_redirect;
+                       if ( isset( $row->page_latest ) )
+                               $this->mLatestID = (int)$row->page_latest;
+               } else { // page not found
+                       $this->mArticleID = 0;
+                       $this->mLength = 0;
+                       $this->mRedirect = false;
+                       $this->mLatestID = 0;
+               }
+       }
+
        /**
         * Create a new Title from a namespace index and a DB key.
         * It's assumed that $ns and $title are *valid*, for instance when
index 656bce8..d1e9b70 100644 (file)
@@ -326,7 +326,7 @@ class WikiPage extends Page {
                if ( $data ) {
                        $lc->addGoodLinkObj( $data->page_id, $this->mTitle, $data->page_len, $data->page_is_redirect, $data->page_latest );
 
-                       $this->mTitle->mArticleID = intval( $data->page_id );
+                       $this->mTitle->loadFromRow( $data );
 
                        # Old-fashioned restrictions
                        $this->mTitle->loadRestrictions( $data->page_restrictions );
@@ -337,7 +337,8 @@ class WikiPage extends Page {
                        $this->mLatest      = intval( $data->page_latest );
                } else {
                        $lc->addBadLinkObj( $this->mTitle );
-                       $this->mTitle->mArticleID = 0;
+
+                       $this->mTitle->loadFromRow( false );
                }
 
                $this->mDataLoaded = true;
@@ -461,14 +462,13 @@ class WikiPage extends Page {
                        return; // already loaded
                }
 
-               # New or non-existent articles have no user information
-               $id = $this->getId();
-               if ( 0 == $id ) {
-                       return;
+               $latest = $this->getLatest();
+               if ( !$latest ) {
+                       return; // page doesn't exist or is missing page_latest info
                }
 
-               $revision = Revision::loadFromPageId( wfGetDB( DB_MASTER ), $id );
-               if ( $revision ) {
+               $revision = Revision::newFromPageId( $this->getId(), $latest );
+               if ( $revision ) { // sanity
                        $this->setLastEdit( $revision );
                }
        }
@@ -1279,7 +1279,9 @@ class WikiPage extends Page {
                                # If something changed, we need to log it. Checking $aRChanged
                                # assures that "unprotecting" a page that is not protected does
                                # not log just because the expiry was "changed".
-                               if ( $aRChanged && $this->mTitle->mRestrictionsExpiry[$action] != $expiry[$action] ) {
+                               if ( $aRChanged &&
+                                       $this->mTitle->getRestrictionExpiry( $action ) != $expiry[$action] )
+                               {
                                        $changed = true;
                                }
                        }
@@ -2076,7 +2078,6 @@ class WikiPage extends Page {
                if ( !$this->mDataLoaded ) {
                        $this->loadPageData();
                }
-
                return !$this->mIsRedirect;
        }
 
@@ -2088,7 +2089,6 @@ class WikiPage extends Page {
                if ( !$this->mDataLoaded ) {
                        $this->loadPageData();
                }
-
                return $this->mTouched;
        }
 
@@ -2100,7 +2100,6 @@ class WikiPage extends Page {
                if ( !$this->mDataLoaded ) {
                        $this->loadPageData();
                }
-
                return (int)$this->mLatest;
        }