r105790 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105789‎ | r105790 | r105791 >
Date:11:30, 11 December 2011
Author:ialex
Status:reverted (Comments)
Tags:
Comment:
Moved view count from WikiPage to Title; avoids an extra DB query when showing the view count in SkinTemplate::outputPage() (since it's not the same WikiPage object)
Modified paths:
  • /trunk/phase3/includes/SkinTemplate.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)
  • /trunk/phase3/includes/WikiPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Title.php
@@ -63,6 +63,7 @@
6464 var $mFragment; // /< Title fragment (i.e. the bit after the #)
6565 var $mArticleID = -1; // /< Article ID, fetched from the link cache on demand
6666 var $mLatestID = false; // /< ID of most recent revision
 67+ var $mCounter = -1; // /< Number of times this page has been viewed (-1 means "not loaded")
6768 var $mRestrictions = array(); // /< Array of groups allowed to edit this article
6869 var $mOldRestrictions = false;
6970 var $mCascadeRestriction; ///< Cascade restrictions on this page to included templates and images?
@@ -272,11 +273,14 @@
273274 $this->mRedirect = (bool)$row->page_is_redirect;
274275 if ( isset( $row->page_latest ) )
275276 $this->mLatestID = (int)$row->page_latest;
 277+ if ( isset( $row->page_counter ) )
 278+ $this->mCounter = (int)$row->page_counter;
276279 } else { // page not found
277280 $this->mArticleID = 0;
278281 $this->mLength = 0;
279282 $this->mRedirect = false;
280283 $this->mLatestID = 0;
 284+ $this->mCounter = 0;
281285 }
282286 }
283287
@@ -2517,6 +2521,28 @@
25182522 }
25192523
25202524 /**
 2525+ * Get the number of views of this page
 2526+ *
 2527+ * @return int The view count for the page
 2528+ */
 2529+ public function getCount() {
 2530+ if ( $this->mCounter == -1 ) {
 2531+ if ( $this->exists() ) {
 2532+ $dbr = wfGetDB( DB_SLAVE );
 2533+ $this->mCounter = $dbr->selectField( 'page',
 2534+ 'page_counter',
 2535+ array( 'page_id' => $this->getArticleID() ),
 2536+ __METHOD__
 2537+ );
 2538+ } else {
 2539+ $this->mCounter = 0;
 2540+ }
 2541+ }
 2542+
 2543+ return $this->mCounter;
 2544+ }
 2545+
 2546+ /**
25212547 * Get the article ID for this Title from the link cache,
25222548 * adding it if necessary
25232549 *
@@ -2628,6 +2654,7 @@
26292655 $this->mRedirect = null;
26302656 $this->mLength = -1;
26312657 $this->mLatestID = false;
 2658+ $this->mCounter = -1;
26322659 }
26332660
26342661 /**
Index: trunk/phase3/includes/SkinTemplate.php
@@ -330,9 +330,8 @@
331331 $tpl->set( 'numberofwatchingusers', false );
332332 if ( $out->isArticle() && $title->exists() ) {
333333 if ( $this->isRevisionCurrent() ) {
334 - $page = WikiPage::factory( $title );
335334 if ( !$wgDisableCounters ) {
336 - $viewcount = $page->getCount();
 335+ $viewcount = $title->getCount();
337336 if ( $viewcount ) {
338337 $tpl->set( 'viewcount', $this->msg( 'viewcount' )->numParams( $viewcount )->parse() );
339338 }
@@ -352,7 +351,8 @@
353352 }
354353
355354 if ( $wgMaxCredits != 0 ) {
356 - $tpl->set( 'credits', Action::factory( 'credits', $page, $this->getContext() )->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
 355+ $tpl->set( 'credits', Action::factory( 'credits', WikiPage::factory( $title ),
 356+ $this->getContext() )->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
357357 } else {
358358 $tpl->set( 'lastmod', $this->lastModified() );
359359 }
Index: trunk/phase3/includes/WikiPage.php
@@ -21,7 +21,6 @@
2222 /**@{{
2323 * @protected
2424 */
25 - public $mCounter = -1; // !< Integer (-1 means "not loaded")
2625 public $mDataLoaded = false; // !< Boolean
2726 public $mIsRedirect = false; // !< Boolean
2827 public $mLatest = false; // !< Integer (false means "not loaded")
@@ -240,7 +239,6 @@
241240 public function clear() {
242241 $this->mDataLoaded = false;
243242
244 - $this->mCounter = -1; # Not loaded
245243 $this->mRedirectTarget = null; # Title object if set
246244 $this->mLastRevision = null; # Latest revision
247245 $this->mTimestamp = '';
@@ -380,7 +378,6 @@
381379 # Old-fashioned restrictions
382380 $this->mTitle->loadRestrictions( $data->page_restrictions );
383381
384 - $this->mCounter = intval( $data->page_counter );
385382 $this->mTouched = wfTimestamp( TS_MW, $data->page_touched );
386383 $this->mIsRedirect = intval( $data->page_is_redirect );
387384 $this->mLatest = intval( $data->page_latest );
@@ -420,25 +417,12 @@
421418 }
422419
423420 /**
 421+ * Get the number of views of this page
 422+ *
424423 * @return int The view count for the page
425424 */
426425 public function getCount() {
427 - if ( -1 == $this->mCounter ) {
428 - $id = $this->getId();
429 -
430 - if ( $id == 0 ) {
431 - $this->mCounter = 0;
432 - } else {
433 - $dbr = wfGetDB( DB_SLAVE );
434 - $this->mCounter = $dbr->selectField( 'page',
435 - 'page_counter',
436 - array( 'page_id' => $id ),
437 - __METHOD__
438 - );
439 - }
440 - }
441 -
442 - return $this->mCounter;
 426+ return $this->mTitle->getCount();
443427 }
444428
445429 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r105795Follow-up r105790: make tests work againialex13:29, 11 December 2011
r107927Give Title a decent loading mechanism:...ialex19:28, 3 January 2012
r108274* Added WikiPage to RequestContext and related so that it can be shared to av...ialex20:00, 6 January 2012
r108281Revert r105790 and move back view counter back to WikiPage with two modificat...ialex20:26, 6 January 2012

Comments

#Comment by Hashar (talk | contribs)   14:12, 12 December 2011

When page count is disabled (with $wgDisableCounters) you might returns -1 even early and save another DB query.

#Comment by IAlex (talk | contribs)   19:37, 16 December 2011

This should not be called when $wgDisableCounters is true.

Status & tagging log