jenkins-bot has submitted this change and it was merged.

Change subject: Fix a variety of bugs
......................................................................


Fix a variety of bugs

* Cache #'s for only 1min if they're less than 100.
* Format hit views/edit properly.
* Don't use cached # for first hit for any page.
* Link title and give ns.
* Clean up line lengths.

Bug: T101595
Bug: T105860
Bug: T105861
Bug: T109180
Change-Id: I143384f5b16373434cc7c07e9aeb887c0d25a73b
---
M HitCounters.body.php
M HitCounters.hooks.php
2 files changed, 74 insertions(+), 39 deletions(-)

Approvals:
  MarkAHershberger: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/HitCounters.body.php b/HitCounters.body.php
index 7b95a1f..cba4fc7 100644
--- a/HitCounters.body.php
+++ b/HitCounters.body.php
@@ -11,6 +11,16 @@
 class HitCounters {
        protected static $mViews;
 
+       protected static function cacheStore( $cache, $key, $views ) {
+               if ( $views < 100 ) {
+                       // Only cache for a minute
+                       $cache->set( $key, $views, 60 );
+               } else {
+                       /* update only once a day */
+                       $cache->set( $key, $views, 24 * 3600 );
+               }
+       }
+
        /**
         * @return int The view count for the page
         */
@@ -30,30 +40,23 @@
                $cache = wfGetCache( CACHE_ANYTHING );
                $key = wfMemcKey( 'viewcount', $title->getDBkey() );
                $views = $cache->get( $key );
-               wfDebugLog( "HitCounters", "Got viewcount=" . var_export( 
$views, true ) .
-                       " from cache" );
+               wfDebugLog( "HitCounters", "Got viewcount=" .
+                       var_export( $views, true ) . " from cache" );
 
-               if ( !$views ) {
+               if ( !$views || $views == 1 ) {
                        $dbr = wfGetDB( DB_SLAVE );
                        $row = $dbr->select(
                                array( 'hit_counter' ),
                                array( 'hits' => 'page_counter' ),
                                array( 'page_id' => $title->getArticleID() ),
                                __METHOD__ );
-                       wfDebugLog( "HitCounters", "Got result=" . var_export( 
$row, true ) .
-                               " from DB and setting cache." );
 
                        if ( $row !== false && $current = $row->current() ) {
                                $views = $current->hits;
-                               wfDebugLog( "HitCounters", "Got result=" . 
var_export( $current, true ) .
+                               wfDebugLog( "HitCounters", "Got result=" .
+                                       var_export( $current, true ) .
                                        " from DB and setting cache." );
-                               if ( $views < 100 ) {
-                                       // Only cache for a minute
-                                       $cache->set( $key, $views, 60 );
-                               } else {
-                                       /* update only once a day */
-                                       $cache->set( $key, $views, 24 * 3600 );
-                               }
+                               self::cacheStore( $cache, $key, $views );
                        }
                }
 
@@ -67,15 +70,18 @@
                // Re-calculate the count if the last tally is old...
                if ( !self::$mViews ) {
                        self::$mViews = $cache->get( $key );
-                       wfDebugLog( "HitCounters", __METHOD__ . ": got " . 
var_export( self::$mViews, true ) .
+                       wfDebugLog( "HitCounters", __METHOD__
+                               . ": got " . var_export( self::$mViews, true ) .
                                " from cache." );
-                       if ( !self::$mViews ) {
+                       if ( !self::$mViews || self::$mViews == 1 ) {
                                $dbr = wfGetDB( DB_SLAVE );
-                               self::$mViews = $dbr->selectField( 
'hit_counter', 'SUM(page_counter)', '',
-                                       __METHOD__ );
-                               wfDebugLog( "HitCounters", __METHOD__ . ": got 
" . var_export( self::$mViews, true ) .
+                               self::$mViews = $dbr->selectField(
+                                       'hit_counter', 'SUM(page_counter)', '', 
__METHOD__
+                               );
+                               wfDebugLog( "HitCounters", __METHOD__ . ": got 
" .
+                                       var_export( self::$mViews, true ) .
                                        " from select." );
-                               $cache->set( $key, self::$mViews, 24 * 3600 ); 
// don't update for 1 day
+                               self::cacheStore( $cache, $key, self::$mViews );
                        }
                }
                return self::$mViews;
@@ -87,7 +93,9 @@
         *
         * @SuppressWarnings(PHPMD.UnusedFormalParameter)
         */
-       public static function numberOfViews( Parser &$parser, PPFrame $frame, 
$args ) {
+       public static function numberOfViews(
+               Parser &$parser, PPFrame $frame, $args
+       ) {
                return self::getCount( $frame->title );
        }
 
@@ -105,8 +113,10 @@
                                'page_namespace' => 
MWNamespace::getContentNamespaces(),
                        ),
                        'join_conds' => array(
-                               'page' => array( 'INNER JOIN', $wgDBprefix . 
'page.page_id = ' . $wgDBprefix .
-                                       'hit_counter.page_id' )
+                               'page' => array(
+                                       'INNER JOIN',
+                                       $wgDBprefix . 'page.page_id = ' .
+                                       $wgDBprefix . 'hit_counter.page_id' )
                        )
                );
        }
diff --git a/HitCounters.hooks.php b/HitCounters.hooks.php
index 9cf35ea..bf0d594 100644
--- a/HitCounters.hooks.php
+++ b/HitCounters.hooks.php
@@ -29,16 +29,27 @@
                $specialPages['PopularPages'] = 
'HitCounters\SpecialPopularPages';
        }
 
-       public static function onLoadExtensionSchemaUpdates( DatabaseUpdater 
$updater ) {
+       public static function onLoadExtensionSchemaUpdates(
+               DatabaseUpdater $updater
+       ) {
                HCUpdater::getDBUpdates( $updater );
        }
 
-       public static function onSpecialStatsAddExtra( array &$extraStats, 
RequestContext $statsPage ) {
+       public static function onSpecialStatsAddExtra(
+               array &$extraStats, RequestContext $statsPage
+       ) {
+               global $wgContLang;
+
                $totalViews = HitCounters::views();
-               
$extraStats['hitcounters-statistics-header-views']['hitcounters-statistics-views-total']
 = $totalViews;
-               
$extraStats['hitcounters-statistics-header-views']['hitcounters-statistics-views-peredit']
 =
-                       $totalViews / SiteStats::edits();
-               $extraStats['hitcounters-statistics-mostpopular'] = 
self::getMostViewedPages( $statsPage );
+               $extraStats['hitcounters-statistics-header-views']
+                       ['hitcounters-statistics-views-total'] = $totalViews;
+               $extraStats['hitcounters-statistics-header-views']
+                       ['hitcounters-statistics-views-peredit'] =
+                       $wgContLang->formatNum( $totalViews
+                               ? sprintf( '%.2f', $totalViews / 
SiteStats::edits() )
+                               : 0 );
+               $extraStats['hitcounters-statistics-mostpopular'] =
+                       self::getMostViewedPages( $statsPage );
                return true;
        }
 
@@ -47,8 +58,10 @@
                $param = HitCounters::getQueryInfo();
                $options['ORDER BY'] = array( 'page_counter DESC' );
                $options['LIMIT'] = 10;
-               $res = $dbr->select( $param['tables'], $param['fields'], 
array(), __METHOD__,
-                       $options, $param['join_conds'] );
+               $res = $dbr->select(
+                       $param['tables'], $param['fields'], array(), __METHOD__,
+                       $options, $param['join_conds']
+               );
 
                $ret = array();
                if ( $res->numRows() > 0 ) {
@@ -58,8 +71,7 @@
                                if ( $title instanceof Title ) {
                                        $ret[ $title->getPrefixedText() 
]['number'] = $row->value;
                                        $ret[ $title->getPrefixedText() 
]['name'] =
-                                               $statsPage->msg( 
'hitcounters-page-label',
-                                                       $title->getText() 
)->title( $statsPage->getTitle() );
+                                               \Linker::link( $title );
                                }
                        }
                        $res->free();
@@ -94,14 +106,22 @@
                global $wgDisableCounters;
 
                // Don't update page view counters on views from bot users (bug 
14044)
-               if ( !$wgDisableCounters && !$user->isAllowed( 'bot' ) && 
$wikipage->exists() ) {
-                       DeferredUpdates::addUpdate( new ViewCountUpdate( 
$wikipage->getId() ) );
+               if (
+                       !$wgDisableCounters &&
+                       !$user->isAllowed( 'bot' ) &&
+                       $wikipage->exists()
+               ) {
+                       DeferredUpdates::addUpdate(
+                               new ViewCountUpdate( $wikipage->getId() )
+                       );
                        DeferredUpdates::addUpdate( new SiteStatsUpdate( 1, 0, 
0 ) );
                }
        }
 
-       public static function onSkinTemplateOutputPageBeforeExec( SkinTemplate 
&$skin,
-                                                                               
                                           QuickTemplate &$tpl) {
+       public static function onSkinTemplateOutputPageBeforeExec(
+               SkinTemplate &$skin,
+               QuickTemplate &$tpl
+       ) {
                global $wgDisableCounters;
 
                /* Without this check two lines are added to the page. */
@@ -114,15 +134,20 @@
                if ( !$wgDisableCounters ) {
                        $footer = $tpl->get( 'footerlinks' );
                        if ( isset( $footer['info'] ) && is_array( 
$footer['info'] ) ) {
-                               // 'viewcount' goes after 'lastmod', we'll just 
assume 'viewcount' is the 0th item
+                               // 'viewcount' goes after 'lastmod', we'll just 
assume
+                               // 'viewcount' is the 0th item
                                array_splice( $footer['info'], 1, 0, 
'viewcount' );
                                $tpl->set( 'footerlinks', $footer );
                        }
 
                        $viewcount = HitCounters::getCount( $skin->getTitle());
                        if ( $viewcount ) {
-                               wfDebugLog( "HitCounters", "Got 
viewcount=$viewcount and putting in page" );
-                               $tpl->set( 'viewcount', $skin->msg( 'viewcount' 
)->numParams( $viewcount )->parse() );
+                               wfDebugLog(
+                                       "HitCounters",
+                                       "Got viewcount=$viewcount and putting 
in page"
+                               );
+                               $tpl->set( 'viewcount', $skin->msg( 'viewcount' 
)->
+                                       numParams( $viewcount )->parse() );
                        }
                }
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/251646
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I143384f5b16373434cc7c07e9aeb887c0d25a73b
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/HitCounters
Gerrit-Branch: master
Gerrit-Owner: MarkAHershberger <[email protected]>
Gerrit-Reviewer: MarkAHershberger <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to