r68514 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68513‎ | r68514 | r68515 >
Date:13:15, 24 June 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Try to unbreak per-language graphs.

Reworked the ugly key index mechanism by removing one level of indirection.
Modified paths:
  • /trunk/extensions/Translate/SpecialTranslationStats.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/SpecialTranslationStats.php
@@ -52,8 +52,10 @@
5353 } elseif ( $opts['graphit'] ) {
5454
5555 // Cache for two hours
56 - $lastMod = $wgOut->checkLastModified( wfTimestamp( TS_MW, time() - 2 * 3600 ) );
57 - if ( $lastMod ) return;
 56+ if ( !$opts['preview'] ) {
 57+ $lastMod = $wgOut->checkLastModified( wfTimestamp( TS_MW, time() - 2 * 3600 ) );
 58+ if ( $lastMod ) return;
 59+ }
5860
5961 $wgOut->disable();
6062
@@ -279,7 +281,6 @@
280282 $so->preQuery( $tables, $fields, $conds, $type, $options );
281283 $res = $dbr->select( $tables, $fields, $conds, $type, $options );
282284
283 -
284285 // Initialisations
285286 $so->postQuery( $res );
286287
@@ -291,9 +292,9 @@
292293
293294 $data = array();
294295 while ( $cutoff < $now ) {
295 - $date = $wgLang->sprintfDate( $dateFormat, wfTimestamp( TS_MW, $cutoff ) );
296 - $so->preProcess( $data[$date] );
 296+ $date = $wgLang->sprintfDate( $dateFormat, wfTimestamp( TS_MW, $cutoff ) );
297297 $cutoff += $increment;
 298+ $data[$date] = array();
298299 }
299300
300301 // Processing
@@ -301,7 +302,7 @@
302303 $date = $wgLang->sprintfDate( $dateFormat, $row->rc_timestamp );
303304
304305 $indexes = $so->indexOf( $row );
305 - if ( $indexes === -1 ) continue;
 306+ if ( $indexes === false ) continue;
306307
307308 foreach ( (array) $indexes as $index ) {
308309 if ( !isset( $data[$date][$index] ) ) $data[$date][$index] = 0;
@@ -309,9 +310,18 @@
310311 }
311312 }
312313
313 - $labels = null;
314 - $so->labels( $labels );
 314+ $labels = $so->labels();
315315
 316+ /* Make sure there is empty entries for silent days, or otherwise
 317+ * they will be skipped alltogether. */
 318+ foreach ( $data as &$date ) {
 319+ foreach ( $labels as $label ) {
 320+ if ( !isset( $date[$label] ) ) {
 321+ $date[$label] = 0;
 322+ }
 323+ }
 324+ }
 325+
316326 return array( $labels, $data );
317327 }
318328
@@ -378,9 +388,6 @@
379389
380390 class TranslatePerLanguageStats {
381391 protected $opts;
382 - protected $cache;
383 - protected $index;
384 - protected $filters;
385392 protected $usercache;
386393
387394 public function __construct( FormOptions $opts ) {
@@ -390,55 +397,42 @@
391398 public function preQuery( &$tables, &$fields, &$conds, &$type, &$options ) {
392399 $db = wfGetDB( DB_SLAVE );
393400
394 - $groups = array_map( 'trim', explode( ',', $this->opts['group'] ) );
395 - $codes = array_map( 'trim', explode( ',', $this->opts['language'] ) );
 401+ $this->groups = array_filter( array_map( 'trim', explode( ',', $this->opts['group'] ) ) );
 402+ $this->codes = array_filter( array_map( 'trim', explode( ',', $this->opts['language'] ) ) );
396403
397 - $filters['language'] = trim( $this->opts['language'] ) !== '';
398 - $filters['group'] = trim( $this->opts['group'] ) !== '';
399 -
400404 $namespaces = array();
 405+ $languages = array();
401406
402 - foreach ( $groups as $group ) {
403 - if ( $group === '' ) continue;
404 - $ns = MessageGroups::getGroup( $group )->getNamespace();
405 - $namespaces[$ns] = true;
406 -
407 - foreach ( $codes as $code ) {
408 - if ( $code !== '' ) $key = "$group ($code)";
409 - else $key = $group;
410 - $this->cache[$key] = count( $this->cache );
 407+ foreach ( $this->groups as $id ) {
 408+ $group = MessageGroups::getGroup( $id );
 409+ if ( $group ) {
 410+ $namespaces[] = $group->getNamespace();
411411 }
412412 }
413413
 414+ foreach( $this->codes as $code ) {
 415+ $languages[] = 'rc_title like \'%%/' . $db->escapeLike( $code ) . "'";
 416+ }
 417+
414418 if ( count( $namespaces ) ) {
415 - $conds['rc_namespace'] = array_keys( $namespaces );
 419+ $namespaces = array_unique( $namespaces );
 420+ $conds['rc_namespace'] = $namespaces;
416421 }
417422
418 - if ( $filters['language'] ) {
419 - $myconds = array();
420 - foreach ( $codes as $code ) {
421 - if ( $code === '' ) continue;
422 - $myconds[] = 'rc_title like \'%%/' . $db->escapeLike( $code ) . "'";
423 - }
424 -
425 - $conds[] = $db->makeList( $myconds, LIST_OR );
 423+ if ( count( $languages ) ) {
 424+ $languages = array_unique( $languages );
 425+ $conds[] = $db->makeList( $languages, LIST_OR );
426426 }
427427
428 - if ( max( $filters ) ) $fields[] = 'rc_title';
429 - if ( $filters['group'] ) $fields[] = 'rc_namespace';
 428+ $fields[] = 'rc_title';
 429+ if ( $this->groups ) $fields[] = 'rc_namespace';
430430 if ( $this->opts['count'] === 'users' ) $fields[] = 'rc_user_text';
431431
432432 $type .= '-perlang';
433 -
434 - $this->filters = $filters;
435433 }
436434
437435 public function postQuery( $rows ) { }
438436
439 - public function preProcess( &$initial ) {
440 - $initial = array_pad( array(), max( 1, count( $this->cache ) ), 0 );
441 - }
442 -
443437 public function indexOf( $row ) {
444438 global $wgContLang;
445439
@@ -454,41 +448,53 @@
455449 }
456450 }
457451
458 - if ( !max( $this->filters ) ) return 0;
459 - if ( strpos( $row->rc_title, '/' ) === false ) return -1;
460 - if ( !count( $this->cache ) ) return 0;
 452+ // Don't consider language-less pages
 453+ if ( strpos( $row->rc_title, '/' ) === false ) return false;
461454
 455+ // No filters, just one key to track
 456+ if ( !$this->groups && !$this->codes ) return 'all';
 457+
 458+ // The key-building needs to be in sync with ::labels()
 459+ $keys = array();
 460+
462461 list( $key, $code ) = TranslateUtils::figureMessage( $row->rc_title );
463 - $indexKeys = array();
464462
465 - if ( $this->filters['group'] ) {
 463+ if ( $this->groups ) {
 464+ /* Get list of keys that the message belongs to, and filter
 465+ * out those which are not requested */
466466 $groups = TranslateUtils::messageKeyToGroups( $row->rc_namespace, $key );
467 - if ( !count( $groups ) ) {
468 - return -1;
469 - }
470 - foreach ( $groups as $group ) $indexKeys[] = $group;
 467+ $keys = array_intersect( $this->groups, $groups );
 468+ } else {
 469+ $keys[] = '';
471470 }
472 -
473 - if ( $this->filters['language'] ) {
474 - foreach ( $indexKeys as &$value ) {
475 - $value .= " ($code)";
 471+ if ( $this->codes ) {
 472+ foreach ( $keys as &$value ) {
 473+ if ( $value === '' ) {
 474+ $value = $code;
 475+ } else {
 476+ $value .= " ($code)";
 477+ }
476478 }
477479 }
 480+ return $keys;
 481+ }
478482
479 - $indexes = array();
480 -
481 - foreach ( $indexKeys as $value ) {
482 - if ( isset( $this->cache[$value] ) ) {
483 - $indexes[] = $this->cache[$value];
 483+ public function labels() {
 484+ $labels = array();
 485+ if ( $this->groups && $this->codes ) {
 486+ foreach ( $this->codes as $code ) {
 487+ foreach ( $this->groups as $group ) {
 488+ $labels[] = "$group ($code)";
 489+ }
484490 }
 491+ } elseif ( $this->groups ) {
 492+ $labels = $this->groups;
 493+ } elseif ( $this->codes ) {
 494+ $labels = $this->codes;
 495+ } else {
 496+ $labels = array();
485497 }
486 -
487 - return $indexes;
 498+ return $labels;
488499 }
489500
490 - public function labels( &$labels ) {
491 - if ( count( $this->cache ) > 1 ) {
492 - $labels = array_keys( $this->cache );
493 - }
494 - }
495501 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r68601r68514 didn't work properly. This should fix it.nikerabbit07:35, 26 June 2010

Status & tagging log