http://www.mediawiki.org/wiki/Special:Code/MediaWiki/54786
Revision: 54786 Author: catrope Date: 2009-08-11 12:04:56 +0000 (Tue, 11 Aug 2009) Log Message: ----------- PrefStats: Cache statistics per hour and make them available to everyone Modified Paths: -------------- trunk/extensions/UsabilityInitiative/PrefStats/PrefStats.php trunk/extensions/UsabilityInitiative/PrefStats/SpecialPrefStats.php Modified: trunk/extensions/UsabilityInitiative/PrefStats/PrefStats.php =================================================================== --- trunk/extensions/UsabilityInitiative/PrefStats/PrefStats.php 2009-08-11 12:01:04 UTC (rev 54785) +++ trunk/extensions/UsabilityInitiative/PrefStats/PrefStats.php 2009-08-11 12:04:56 UTC (rev 54786) @@ -54,11 +54,12 @@ // table $wgPrefStatsExpensiveCounts = false; +// For how long statistics should be cached +// Set to false to disable caching +$wgPrefStatsCacheTime = 60 * 60; // one hour + /* Setup */ -// Right required to view Special:PrefStats -$wgAvailableRights[] = 'prefstats'; - // Credits $wgExtensionCredits['other'][] = array( 'path' => __FILE__, Modified: trunk/extensions/UsabilityInitiative/PrefStats/SpecialPrefStats.php =================================================================== --- trunk/extensions/UsabilityInitiative/PrefStats/SpecialPrefStats.php 2009-08-11 12:01:04 UTC (rev 54785) +++ trunk/extensions/UsabilityInitiative/PrefStats/SpecialPrefStats.php 2009-08-11 12:04:56 UTC (rev 54786) @@ -8,7 +8,7 @@ class SpecialPrefStats extends SpecialPage { function __construct() { - parent::__construct( 'PrefStats', 'prefstats' ); + parent::__construct( 'PrefStats' ); wfLoadExtensionMessages( 'PrefStats' ); } @@ -52,7 +52,6 @@ function displayPrefStats( $pref ) { global $wgOut, $wgRequest, $wgPrefStatsExpensiveCounts; - $max = $this->getMaxDuration( $pref ); $stats = $this->getPrefStats( $pref, $wgRequest->getIntOrNull( 'inc' ) ); $counters = $this->getCounters( $pref ); @@ -61,6 +60,7 @@ 'prefstats-counters'; $wgOut->addWikiMsgArray( $message, $counters ); $wgOut->addHTML( $this->incLinks( $pref ) ); + $wgOut->addHTML( Xml::element( 'br' ) ); $wgOut->addHTML( Xml::element( 'img', array( 'src' => $this->getGoogleChartParams( $stats ) ) ) ); } @@ -102,6 +102,20 @@ } function getCounters( $pref ) { + global $wgMemc, $wgPrefStatsCacheTime; + if ( $wgPrefStatsCacheTime === false ) + return $this->reallyGetCounters( $pref ); + + $key = wfMemcKey( 'prefstats', 'counters', $pref ); + $cached = $wgMemc->get( $key ); + if ( $cached ) + return $cached; + $retval = $this->reallyGetCounters( $pref ); + $wgMemc->set( $key, $retval, $wgPrefStatsCacheTime ); + return $retval; + } + + function reallyGetCounters( $pref ) { global $wgPrefStatsExpensiveCounts, $wgPrefStatsTrackPrefs; $val = $wgPrefStatsTrackPrefs[$pref]; @@ -124,8 +138,22 @@ $c4 = 0; return array( $c1, $c2, $c3, $c4 ); } + + function getPrefStats( $pref, $inc = null ) { + global $wgMemc, $wgPrefStatsCacheTime; + if ( $wgPrefStatsCacheTime === false ) + return $this->reallyGetPrefStats( $pref, $inc ); + + $key = wfMemcKey( 'prefstats', 'stats', $pref, $inc ); + $cached = $wgMemc->get( $key ); + if ( $cached ) + return $cached; + $retval = $this->reallyGetPrefStats( $pref, $inc ); + $wgMemc->set( $key, $retval, $wgPrefStatsCacheTime ); + return $retval; + } - function getPrefStats( $pref, $inc = null ) { + function reallyGetPrefStats( $pref, $inc = null ) { global $wgPrefStatsTimeUnit, $wgPrefStatsDefaultScaleBars; $max = ceil( $this->getMaxDuration( $pref ) / $wgPrefStatsTimeUnit ); _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs