r91327 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91326‎ | r91327 | r91328 >
Date:23:33, 1 July 2011
Author:aaron
Status:resolved (Comments)
Tags:
Comment:
* Fixed getLatest() check in Article
* 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
Modified paths:
  • /trunk/phase3/includes/Article.php (modified) (history)
  • /trunk/phase3/includes/Revision.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)
  • /trunk/phase3/includes/WikiPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Article.php
@@ -294,7 +294,7 @@
295295 }
296296 }
297297 } else {
298 - if ( $this->mPage->getLatest() === false ) {
 298+ if ( !$this->mPage->getLatest() ) {
299299 wfDebug( __METHOD__ . " failed to find page data for title " . $this->getTitle()->getPrefixedText() . "\n" );
300300 return false;
301301 }
Index: trunk/phase3/includes/Revision.php
@@ -34,7 +34,7 @@
3535 * to that title, will return null.
3636 *
3737 * @param $title Title
38 - * @param $id Integer
 38+ * @param $id Integer (optional)
3939 * @return Revision or null
4040 */
4141 public static function newFromTitle( $title, $id = 0 ) {
@@ -50,8 +50,7 @@
5151 $dbw = wfGetDB( DB_MASTER );
5252 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
5353 if ( $latest === false ) {
54 - // Page does not exist
55 - return null;
 54+ return null; // page does not exist
5655 }
5756 $conds['rev_id'] = $latest;
5857 } else {
@@ -63,6 +62,33 @@
6463 }
6564
6665 /**
 66+ * Load either the current, or a specified, revision
 67+ * that's attached to a given page ID.
 68+ * Returns null if no such revision can be found.
 69+ *
 70+ * @param $revId Integer
 71+ * @param $pageId Integer (optional)
 72+ * @return Revision or null
 73+ */
 74+ public static function newFromPageId( $pageId, $revId = 0 ) {
 75+ $conds = array( 'page_id' => $pageId );
 76+ if ( $revId ) {
 77+ $conds['rev_id'] = $pageId;
 78+ } elseif ( wfGetLB()->getServerCount() > 1 ) {
 79+ // Get the latest revision ID from the master
 80+ $dbw = wfGetDB( DB_MASTER );
 81+ $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
 82+ if ( $latest === false ) {
 83+ return null; // page does not exist
 84+ }
 85+ $conds['rev_id'] = $latest;
 86+ } else {
 87+ $conds[] = 'rev_id = page_latest';
 88+ }
 89+ return Revision::newFromConds( $conds );
 90+ }
 91+
 92+ /**
6793 * Make a fake revision object from an archive table row. This is queried
6894 * for permissions or even inserted (as in Special:Undelete)
6995 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
Index: trunk/phase3/includes/Title.php
@@ -252,16 +252,36 @@
253253 */
254254 public static function newFromRow( $row ) {
255255 $t = self::makeTitle( $row->page_namespace, $row->page_title );
256 -
257 - $t->mArticleID = isset( $row->page_id ) ? intval( $row->page_id ) : -1;
258 - $t->mLength = isset( $row->page_len ) ? intval( $row->page_len ) : -1;
259 - $t->mRedirect = isset( $row->page_is_redirect ) ? (bool)$row->page_is_redirect : null;
260 - $t->mLatestID = isset( $row->page_latest ) ? intval( $row->page_latest ) : false;
261 -
 256+ $t->loadFromRow( $row );
262257 return $t;
263258 }
264259
265260 /**
 261+ * Load Title object fields from a DB row.
 262+ * If false is given, the title will be treated as non-existing.
 263+ *
 264+ * @param $row Object|false database row
 265+ * @return void
 266+ */
 267+ public function loadFromRow( $row ) {
 268+ if ( $row ) { // page found
 269+ if ( isset( $row->page_id ) )
 270+ $this->mArticleID = (int)$row->page_id;
 271+ if ( isset( $row->page_len ) )
 272+ $this->mLength = (int)$row->page_len;
 273+ if ( isset( $row->page_is_redirect ) )
 274+ $this->mRedirect = (bool)$row->page_is_redirect;
 275+ if ( isset( $row->page_latest ) )
 276+ $this->mLatestID = (int)$row->page_latest;
 277+ } else { // page not found
 278+ $this->mArticleID = 0;
 279+ $this->mLength = 0;
 280+ $this->mRedirect = false;
 281+ $this->mLatestID = 0;
 282+ }
 283+ }
 284+
 285+ /**
266286 * Create a new Title from a namespace index and a DB key.
267287 * It's assumed that $ns and $title are *valid*, for instance when
268288 * they came directly from the database or a special page name.
Index: trunk/phase3/includes/WikiPage.php
@@ -326,7 +326,7 @@
327327 if ( $data ) {
328328 $lc->addGoodLinkObj( $data->page_id, $this->mTitle, $data->page_len, $data->page_is_redirect, $data->page_latest );
329329
330 - $this->mTitle->mArticleID = intval( $data->page_id );
 330+ $this->mTitle->loadFromRow( $data );
331331
332332 # Old-fashioned restrictions
333333 $this->mTitle->loadRestrictions( $data->page_restrictions );
@@ -337,7 +337,8 @@
338338 $this->mLatest = intval( $data->page_latest );
339339 } else {
340340 $lc->addBadLinkObj( $this->mTitle );
341 - $this->mTitle->mArticleID = 0;
 341+
 342+ $this->mTitle->loadFromRow( false );
342343 }
343344
344345 $this->mDataLoaded = true;
@@ -461,14 +462,13 @@
462463 return; // already loaded
463464 }
464465
465 - # New or non-existent articles have no user information
466 - $id = $this->getId();
467 - if ( 0 == $id ) {
468 - return;
 466+ $latest = $this->getLatest();
 467+ if ( !$latest ) {
 468+ return; // page doesn't exist or is missing page_latest info
469469 }
470470
471 - $revision = Revision::loadFromPageId( wfGetDB( DB_MASTER ), $id );
472 - if ( $revision ) {
 471+ $revision = Revision::newFromPageId( $this->getId(), $latest );
 472+ if ( $revision ) { // sanity
473473 $this->setLastEdit( $revision );
474474 }
475475 }
@@ -1279,7 +1279,9 @@
12801280 # If something changed, we need to log it. Checking $aRChanged
12811281 # assures that "unprotecting" a page that is not protected does
12821282 # not log just because the expiry was "changed".
1283 - if ( $aRChanged && $this->mTitle->mRestrictionsExpiry[$action] != $expiry[$action] ) {
 1283+ if ( $aRChanged &&
 1284+ $this->mTitle->getRestrictionExpiry( $action ) != $expiry[$action] )
 1285+ {
12841286 $changed = true;
12851287 }
12861288 }
@@ -2076,7 +2078,6 @@
20772079 if ( !$this->mDataLoaded ) {
20782080 $this->loadPageData();
20792081 }
2080 -
20812082 return !$this->mIsRedirect;
20822083 }
20832084
@@ -2088,7 +2089,6 @@
20892090 if ( !$this->mDataLoaded ) {
20902091 $this->loadPageData();
20912092 }
2092 -
20932093 return $this->mTouched;
20942094 }
20952095
@@ -2100,7 +2100,6 @@
21012101 if ( !$this->mDataLoaded ) {
21022102 $this->loadPageData();
21032103 }
2104 -
21052104 return (int)$this->mLatest;
21062105 }
21072106

Follow-up revisions

RevisionCommit summaryAuthorDate
r91328Fixed bogus $conds in r91327aaron23:46, 1 July 2011

Comments

#Comment by SPQRobin (talk | contribs)   23:43, 1 July 2011

This seems to break the software quite a lot.. Each page has different content than it should have.

#Comment by Aaron Schulz (talk | contribs)   23:47, 1 July 2011

Yeah I just noticed right after commit, fixed in r91328. Using special:Random isn't always great for testing...

#Comment by Catrope (talk | contribs)   13:02, 7 September 2011
+		} else { // page not found
+			$this->mArticleID = 0;
+			$this->mLength = 0;
+			$this->mRedirect = false;
+			$this->mLatestID = 0;

These defaults do not correspond to the ones you removed from newFromRow().

#Comment by Aaron Schulz (talk | contribs)   18:11, 7 September 2011

They shouldn't. This is for when the entire row object is false, not where just a field is missing. newFromRow() should work the same since loadFromRow() only sets fields given in $row, and the others fall back to the constructor defaults.

#Comment by Aaron Schulz (talk | contribs)   18:11, 7 September 2011

They shouldn't. This is for when the entire row object is false, not where just a field is missing. newFromRow() should work the same since loadFromRow() only sets fields given in $row, and the others fall back to the constructor defaults.

Status & tagging log