r87402 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87401‎ | r87402 | r87403 >
Date:15:08, 4 May 2011
Author:awjrichards
Status:resolved (Comments)
Tags:
Comment:
Added caching of latest highs/lows in high/low populating maintenance script; Added data-fetching logic for getDailyHighsAndLows() in special page (including cachability); Change how the title is generated in renderDailyHighsAndLows() to use page id
Modified paths:
  • /trunk/extensions/ArticleFeedback/SpecialArticleFeedback.php (modified) (history)
  • /trunk/extensions/ArticleFeedback/populateAFStatistics.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedback/SpecialArticleFeedback.php
@@ -98,7 +98,7 @@
9999 $rows = array();
100100 foreach ( $this->getDailyHighsAndLows() as $page ) {
101101 $row = array();
102 - $pageTitle = Title::newFromText( $page['page'] );
 102+ $pageTitle = Title::newFromId( $page['page'] );
103103 $row['page'] = Linker::link( $pageTitle, $pageTitle->getPrefixedText() );
104104 foreach ( $page['ratings'] as $id => $value ) {
105105 $row['rating-' . $id] = $value;
@@ -197,31 +197,54 @@
198198 * This data should be updated daily, ideally though a scheduled batch job
199199 */
200200 protected function getDailyHighsAndLows() {
 201+ global $wgMemc;
201202
202 - return array(
203 - array(
204 - 'page' => 'Main Page',
205 - // List of ratings as the currently stand
206 - 'ratings' => array(
207 - 1 => 4,
208 - 2 => 3,
209 - 3 => 2,
210 - 4 => 1,
 203+ // check if we've got results in the cache
 204+ $key = wfMemcKey( 'article_feedback_stats_highs_lows' );
 205+ $cache = $wgMemc->get( $key );
 206+ if ( $cache != false && $cache = -1 ) {
 207+ $result = $cache;
 208+ } else {
 209+ $dbr = wfGetDB( DB_SLAVE );
 210+ // first find the freshest timestamp
 211+ $row = $dbr->selectRow(
 212+ 'article_feedback_stats_highs_lows',
 213+ array( 'afshl_ts' ),
 214+ "",
 215+ __METHOD__,
 216+ array( "ORDER BY" => "afshl_ts DESC", "LIMIT" => 1 )
 217+ );
 218+
 219+ // if we have no results, just return
 220+ if ( !$row->afshl_ts ) {
 221+ return;
 222+ }
 223+
 224+ // select ratings with that ts
 225+ $result = $dbr->select(
 226+ 'article_feedback_stats_highs_lows',
 227+ array(
 228+ 'afshl_page_id',
 229+ 'afshl_avg_overall',
 230+ 'afshl_avg_ratings'
211231 ),
212 - // Current average (considering historic averages of each rating)
213 - 'average' => 2.5
214 - ),
215 - array(
216 - 'page' => 'Test Article',
217 - 'ratings' => array(
218 - 1 => 1,
219 - 2 => 2,
220 - 3 => 3,
221 - 4 => 4,
222 - ),
223 - 'average' => 2.5
224 - )
225 - );
 232+ 'afshl_ts = ' . $row->afshl_ts,
 233+ __METHOD__,
 234+ array( "ORDER BY" => "afshl_avg_overall" )
 235+ );
 236+ $wgMemc->set( $key, $result, 86400 );
 237+ }
 238+
 239+ $highs_lows = array();
 240+ foreach ( $result as $row ) {
 241+ $highs_lows[] = array(
 242+ 'page' => $row->afshl_page_id,
 243+ 'ratings' => FormatJson::decode( $row->afshl_avg_ratings ),
 244+ 'average' => $row->afshl_avg_overall
 245+ );
 246+ }
 247+
 248+ return $highs_lows;
226249 }
227250
228251 /**
Index: trunk/extensions/ArticleFeedback/populateAFStatistics.php
@@ -55,6 +55,7 @@
5656 }
5757
5858 public function execute() {
 59+ global $wgMemc;
5960 $this->dbr = wfGetDB( DB_SLAVE );
6061 $this->dbw = wfGetDB( DB_MASTER );
6162
@@ -153,6 +154,31 @@
154155 $this->output( "Inserted " . $rowsInserted . " rows\n" );
155156 }
156157 $this->output( "Done.\n" );
 158+
 159+ // check to see whether we should bother caching
 160+ if ( is_a( $wgMemc, 'EmptyBagOStuff' )) {
 161+ $this->output( "Object caching not available.\n" );
 162+ $this->output( "Done.\n" );
 163+ return;
 164+ }
 165+
 166+ // loading data into caching
 167+ $this->output( "Caching latest highs/lows.\n" );
 168+ $key = wfMemcKey( 'article_feedback_stats_highs_lows' );
 169+ $wgMemc->delete( $key );
 170+ $result = $this->dbr->select(
 171+ 'article_feedback_stats_highs_lows',
 172+ array(
 173+ 'afshl_page_id',
 174+ 'afshl_avg_overall',
 175+ 'afshl_avg_ratings'
 176+ ),
 177+ 'afshl_ts = ' . $cur_ts,
 178+ __METHOD__,
 179+ array()
 180+ );
 181+ $wgMemc->set( $key, $result, 86400 );
 182+ $this->output( "Done\n" );
157183 }
158184
159185

Follow-up revisions

RevisionCommit summaryAuthorDate
r87522Followup r87402, r87381, r87379...awjrichards00:14, 6 May 2011

Comments

#Comment by Catrope (talk | contribs)   19:48, 4 May 2011
+		if ( $cache != false && $cache = -1 ) {

Assignment to -1 gone in r87429.

+				'afshl_ts = ' . $row->afshl_ts,

This is insecure (no quoting or escaping), use array( 'afshl_ts' => $row->afshl_ts ) . Also occurs in the maintenance script.

+			$wgMemc->set( $key, $result, 86400 );

This stores a ResultWrapper object in the cache, which scares me. Told Arthur about this on IRC. $highs_lows should be cached instead.

+		if ( is_a( $wgMemc, 'EmptyBagOStuff' )) {
+			$this->output( "Object caching not available.\n" );
+			$this->output( "Done.\n" );
+			return;
+		}

Eww; but I guess it's sort of warranted here.

+		$wgMemc->delete( $key );

This is unnecessary. set() will overwrite an existing value just fine. It will probably result in bugs or race conditions because the old value will be briefly unavailable.

#Comment by Awjrichards (talk | contribs)   21:27, 5 May 2011

Is there a saner way to check if caching is available than checking the $wgMemc object type?

Status & tagging log