r85307 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85306‎ | r85307 | r85308 >
Date:02:18, 4 April 2011
Author:aaron
Status:ok
Tags:
Comment:
* Cleanup and fixes to template/file version arrays
* Updated related tests
Modified paths:
  • /trunk/extensions/FlaggedRevs/FRInclusionManager.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevision.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/forms/RevisionReviewForm.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/maintenance/tests/FRInclusionManagerTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/maintenance/tests/FRInclusionManagerTest.php
@@ -3,7 +3,7 @@
44 class FRInclusionManagerTest extends PHPUnit_Framework_TestCase {
55 /* starting input */
66 protected static $inputTemplates = array(
7 - 10 => array('XX' => '1242', 'YY' => '0'),
 7+ 10 => array('XX' => '1242', 'YY' => '0', 'KK' => false),
88 4 => array('Cite' => '30', 'Moo' => 0),
99 0 => array('ZZ' => 464, '0' => 13)
1010 );
@@ -12,45 +12,50 @@
1313 'FileYY' => array('time' => '20000403101300', 'sha1' => 1134),
1414 'FileZZ' => array('time' => '0', 'sha1' => ''),
1515 'Filele' => array('time' => 0, 'sha1' => ''),
 16+ 'FileKK' => array('time' => false, 'sha1' => false),
1617 '0' => array('time' => '20000203101350', 'sha1' => 'ae33'),
1718 );
1819 /* output to test against (<test,NS,dbkey,expected rev ID>) */
1920 protected static $reviewedOutputTemplates = array(
20 - array( "Output version when given '1224'", 10, 'XX', 1242 ),
21 - array( "Output version when given '0'", 10, 'YY', 0 ),
22 - array( "Output version when given '30'", 4, 'Cite', 30 ),
23 - array( "Output version when given 0", 4, 'Moo', 0 ),
24 - array( "Output version when given 464", 0, 'ZZ', 464 ),
25 - array( "Output version when given 13", 0, '0', 13 ),
26 - array( "Output version when not given", 0, 'Notexists', null ),
 21+ array( "Output template version when given '1224'", 10, 'XX', 1242 ),
 22+ array( "Output template version when given '0'", 10, 'YY', 0 ),
 23+ array( "Output template version when given false", 10, 'KK', 0 ),
 24+ array( "Output template version when given '30'", 4, 'Cite', 30 ),
 25+ array( "Output template version when given 0", 4, 'Moo', 0 ),
 26+ array( "Output template version when given 464", 0, 'ZZ', 464 ),
 27+ array( "Output template version when given 13", 0, '0', 13 ),
 28+ array( "Output template version when not given", 0, 'Notexists', null ),
2729 );
2830 protected static $stableOutputTemplates = array(
29 - array( "Output version when given '1224'", 10, 'XX', 1242 ),
30 - array( "Output version when given '0'", 10, 'YY', 0 ),
31 - array( "Output version when given '30'", 4, 'Cite', 30 ),
32 - array( "Output version when given 0", 4, 'Moo', 0 ),
33 - array( "Output version when given 464", 0, 'ZZ', 464 ),
34 - array( "Output version when given 13", 0, '0', 13 ),
35 - array( "Output version when not given", 0, 'NotexistsPage1111', 0 ),
 31+ array( "Output template version when given '1224'", 10, 'XX', 1242 ),
 32+ array( "Output template version when given '0'", 10, 'YY', 0 ),
 33+ array( "Output template version when given false", 10, 'KK', 0 ),
 34+ array( "Output template version when given '30'", 4, 'Cite', 30 ),
 35+ array( "Output template version when given 0", 4, 'Moo', 0 ),
 36+ array( "Output template version when given 464", 0, 'ZZ', 464 ),
 37+ array( "Output template version when given 13", 0, '0', 13 ),
 38+ array( "Output template version when not given", 0, 'NotexistsPage1111', 0 ),
3639 );
3740 /* output to test against (<test,dbkey,expected TS,expected sha1>) */
3841 protected static $reviewedOutputFiles = array(
39 - array( "Output version when given '20100405192110'/'abc1'",
 42+ array( "Output file version when given '20100405192110'/'abc1'",
4043 'FileXX', '20100405192110', 'abc1'),
41 - array( "Output version when given '20000403101300'/'ffc2'",
 44+ array( "Output file version when given '20000403101300'/'ffc2'",
4245 'FileYY', '20000403101300', '1134'),
43 - array( "Output version when given '0'/''", 'FileZZ', '0', ''),
44 - array( "Output version when given 0/''", 'Filele', '0', ''),
45 - array( "Output version when not given", 'Notgiven', null, null),
 46+ array( "Output file version when given '0'/''", 'FileZZ', '0', false),
 47+ array( "Output file version when given false/''", 'FileKK', '0', false),
 48+ array( "Output file version when given 0/''", 'Filele', '0', false),
 49+ array( "Output file version when not given", 'Notgiven', null, null),
4650 );
4751 protected static $stableOutputFiles = array(
48 - array( "Output version when given '20100405192110'/'abc1'",
 52+ array( "Output file version when given '20100405192110'/'abc1'",
4953 'FileXX', '20100405192110', 'abc1'),
50 - array( "Output version when given '20000403101300'/'ffc2'",
 54+ array( "Output file version when given '20000403101300'/'ffc2'",
5155 'FileYY', '20000403101300', '1134'),
52 - array( "Output version when given '0'/''", 'FileZZ', '0', ''),
53 - array( "Output version when given 0/''", 'Filele', '0', ''),
54 - array( "Output version when not given", 'NotexistsPage1111', '0', ''),
 56+ array( "Output file version when given '0'/''", 'FileZZ', '0', false),
 57+ array( "Output file version when given false/''", 'FileKK', '0', false),
 58+ array( "Output file version when given 0/''", 'Filele', '0', false),
 59+ array( "Output file version when not given", 'NotexistsPage1111', '0', false),
5560 );
5661
5762 /**
Index: trunk/extensions/FlaggedRevs/forms/RevisionReviewForm.php
@@ -606,8 +606,8 @@
607607 continue; // Page must be valid!
608608 }
609609 $fileSHA1Keys[$img_title->getDBkey()] = array();
610 - $fileSHA1Keys[$img_title->getDBkey()]['time'] = $time ? $time : '0';
611 - $fileSHA1Keys[$img_title->getDBkey()]['sha1'] = $key ? $key : '';
 610+ $fileSHA1Keys[$img_title->getDBkey()]['time'] = $time ? $time : false;
 611+ $fileSHA1Keys[$img_title->getDBkey()]['sha1'] = strlen( $key ) ? $key : false;
612612 }
613613 return array( $templateIds, $fileSHA1Keys );
614614 }
Index: trunk/extensions/FlaggedRevs/FlaggedRevision.php
@@ -281,7 +281,7 @@
282282 'fi_rev_id' => $this->getRevId(),
283283 'fi_name' => $dbkey,
284284 'fi_img_sha1' => strval( $timeSHA1['sha1'] ),
285 - 'fi_img_timestamp' => $timeSHA1['time'] ? // '0' => NULL
 285+ 'fi_img_timestamp' => $timeSHA1['time'] ? // false => NULL
286286 $dbw->timestamp( $timeSHA1['time'] ) : null
287287 );
288288 }
@@ -479,7 +479,7 @@
480480 * Get original template versions at time of review
481481 * @param int $flags FR_MASTER
482482 * @return Array file versions (dbKey => array('time' => MW timestamp,'sha1' => sha1) )
483 - * Note: '0' used for file timestamp if it didn't exist ('' for sha1)
 483+ * Note: false used for file timestamp/sha1 if it didn't exist
484484 */
485485 public function getFileVersions( $flags = 0 ) {
486486 if ( $this->mFiles == null ) {
@@ -492,11 +492,15 @@
493493 __METHOD__
494494 );
495495 foreach ( $res as $row ) {
496 - $reviewedTS = trim( $row->fi_img_timestamp ); // may be ''/NULL
497 - $reviewedTS = $reviewedTS ? wfTimestamp( TS_MW, $reviewedTS ) : '0';
 496+ $reviewedTS = $reviewedSha1 = false;
 497+ $fi_img_timestamp = trim( $row->fi_img_timestamp ); // may have \0's
 498+ if ( $fi_img_timestamp ) {
 499+ $reviewedTS = wfTimestamp( TS_MW, $fi_img_timestamp );
 500+ $reviewedSha1 = strval( $row->fi_img_sha1 );
 501+ }
498502 $this->mFiles[$row->fi_name] = array();
499503 $this->mFiles[$row->fi_name]['time'] = $reviewedTS;
500 - $this->mFiles[$row->fi_name]['sha1'] = $row->fi_img_sha1;
 504+ $this->mFiles[$row->fi_name]['sha1'] = $reviewedSha1;
501505 }
502506 }
503507 return $this->mFiles;
@@ -540,7 +544,7 @@
541545 * Get the current stable version of the files used at time of review
542546 * @param int $flags FR_MASTER
543547 * @return Array file versions (dbKey => array('time' => MW timestamp,'sha1' => sha1) )
544 - * Note: '0' used for file timestamp if it doesn't exist ('' for sha1)
 548+ * Note: false used for file timestamp/sha1 if it didn't exist
545549 */
546550 public function getStableFileVersions( $flags = 0 ) {
547551 if ( $this->mStableFiles == null ) {
@@ -558,12 +562,11 @@
559563 'page_namespace = ' . NS_FILE . ' AND page_title = fi_name' ),
560564 'flaggedpages' => array( 'LEFT JOIN', 'fp_page_id = page_id' ),
561565 'flaggedrevs' => array( 'LEFT JOIN',
562 - 'fr_page_id = fp_page_id AND fr_rev_id = fp_stable' )
 566+ 'fr_page_id = fp_page_id AND fr_rev_id = fp_stable' )
563567 )
564568 );
565569 foreach ( $res as $row ) {
566 - $reviewedTS = '0';
567 - $reviewedSha1 = '';
 570+ $reviewedTS = $reviewedSha1 = false;
568571 if ( $row->fr_img_timestamp ) {
569572 $reviewedTS = wfTimestamp( TS_MW, $row->fr_img_timestamp );
570573 $reviewedSha1 = strval( $row->fr_img_sha1 );
@@ -691,7 +694,7 @@
692695 $fileChanges = array();
693696 foreach ( $ret as $row ) {
694697 $title = Title::makeTitleSafe( NS_FILE, $row->il_to );
695 - $reviewedTS = trim( $row->fi_img_timestamp ); // may be ''/NULL
 698+ $reviewedTS = trim( $row->fi_img_timestamp ); // may have \0's
696699 $reviewedTS = $reviewedTS ? wfTimestamp( TS_MW, $reviewedTS ) : null;
697700 if ( FlaggedRevs::inclusionSetting() == FR_INCLUDES_STABLE ) {
698701 $stableTS = wfTimestampOrNull( TS_MW, $row->fr_img_timestamp );
Index: trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php
@@ -296,7 +296,7 @@
297297 if ( !$incManager->parserOutputIsStabilized() ) {
298298 return true; // trigger for stable version parsing only
299299 }
300 - $id = false; // current
 300+ $id = false; // current version
301301 # Check for the version of this template used when reviewed...
302302 $maybeId = $incManager->getReviewedTemplateVersion( $title );
303303 if ( $maybeId !== null ) {
@@ -320,7 +320,7 @@
321321 /**
322322 * Select the desired images based on the selected stable version time/SHA-1
323323 */
324 - public static function parserFetchStableFile( $parser, Title $nt, &$time, &$sha1, &$query ) {
 324+ public static function parserFetchStableFile( $parser, Title $title, &$time, &$sha1, &$query ) {
325325 if ( !( $parser instanceof Parser ) ) {
326326 return true; // nothing to do
327327 }
@@ -329,18 +329,17 @@
330330 return true; // trigger for stable version parsing only
331331 }
332332 # Normalize NS_MEDIA to NS_FILE
333 - if ( $nt->getNamespace() == NS_MEDIA ) {
334 - $title = Title::makeTitle( NS_FILE, $nt->getDBkey() );
335 - $title->resetArticleId( $nt->getArticleId() ); // avoid extra queries
 333+ if ( $title->getNamespace() == NS_MEDIA ) {
 334+ $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
 335+ $title->resetArticleId( $title->getArticleId() ); // avoid extra queries
336336 } else {
337 - $title =& $nt;
 337+ $title =& $title;
338338 }
339 - $time = false; // current version
340 - $sha1 = false; // corresponds to $time
 339+ $time = $sha1 = false; // current version
341340 # Check for the version of this file used when reviewed...
342341 list( $maybeTS, $maybeSha1 ) = $incManager->getReviewedFileVersion( $title );
343342 if ( $maybeTS !== null ) {
344 - $time = $maybeTS; // use if specified (even "0")
 343+ $time = $maybeTS; // use if specified (even '0')
345344 $sha1 = $maybeSha1;
346345 }
347346 # Check for stable version of file if this feature is enabled...
Index: trunk/extensions/FlaggedRevs/FRInclusionManager.php
@@ -34,7 +34,7 @@
3535 $this->stableVersions['templates'] = array();
3636 $this->stableVersions['files'] = array();
3737 }
38 -
 38+
3939 /**
4040 * (a) Stabilize inclusions in Parser output
4141 * (b) Set the template/image versions used in the flagged version of a revision
@@ -43,8 +43,8 @@
4444 */
4545 public function setReviewedVersions( array $tmpParams, array $imgParams ) {
4646 $this->reviewedVersions = array();
47 - $this->reviewedVersions['templates'] = $tmpParams;
48 - $this->reviewedVersions['files'] = $imgParams;
 47+ $this->reviewedVersions['templates'] = self::formatTemplateArray( $tmpParams );
 48+ $this->reviewedVersions['files'] = self::formatFileArray( $imgParams );
4949 }
5050
5151 /**
@@ -53,11 +53,46 @@
5454 * @param array $imgParams (dbKey => array('time' => MW timestamp,'sha1' => sha1) )
5555 */
5656 public function setStableVersionCache( array $tmpParams, array $imgParams ) {
57 - $this->stableVersions['templates'] = $tmpParams;
58 - $this->stableVersions['files'] = $imgParams;
 57+ $this->stableVersions['templates'] = self::formatTemplateArray( $tmpParams );
 58+ $this->stableVersions['files'] = self::formatFileArray( $imgParams );
5959 }
6060
6161 /**
 62+ * Clean up a template version array
 63+ * @param array $tmpParams (ns => dbKey => revId )
 64+ * @returns Array
 65+ */
 66+ protected function formatTemplateArray( array $params ) {
 67+ $res = array();
 68+ foreach ( $params as $ns => $templates ) {
 69+ $res[$ns] = array();
 70+ foreach ( $templates as $dbKey => $revId ) {
 71+ $res[$ns][$dbKey] = (int)$revId;
 72+ }
 73+ }
 74+ return $res;
 75+ }
 76+
 77+ /**
 78+ * Clean up a file version array
 79+ * @param array $imgParams (dbKey => array('time' => MW timestamp,'sha1' => sha1) )
 80+ * @returns Array
 81+ */
 82+ protected function formatFileArray( array $params ) {
 83+ $res = array();
 84+ foreach ( $params as $dbKey => $timeKey ) {
 85+ $time = '0'; // missing
 86+ $sha1 = false;
 87+ if ( $timeKey['time'] ) {
 88+ $time = $timeKey['time'];
 89+ $sha1 = strval( $timeKey['sha1'] );
 90+ }
 91+ $res[$dbKey] = array( 'time' => $time, 'sha1' => $sha1 );
 92+ }
 93+ return $res;
 94+ }
 95+
 96+ /**
6297 * (a) Stabilize inclusions in Parser output
6398 * (b) Load all of the "at review time" versions of template/files
6499 * (c) Load their stable version counterparts (avoids DB hits)
@@ -150,8 +185,8 @@
151186 */
152187 public function getStableFileVersion( Title $title ) {
153188 $dbKey = $title->getDBkey();
154 - $time = '0';
155 - $sha1 = '';
 189+ $time = '0'; // missing
 190+ $sha1 = false;
156191 # All NS_FILE, no need to check namespace
157192 if ( isset( $this->stableVersions['files'][$dbKey] ) ) {
158193 $time = $this->stableVersions['files'][$dbKey]['time'];
@@ -160,7 +195,7 @@
161196 }
162197 $srev = FlaggedRevision::newFromStable( $title );
163198 if ( $srev && $srev->getFileTimestamp() ) {
164 - $time = $srev->getFileTimestamp(); // TS or null
 199+ $time = $srev->getFileTimestamp();
165200 $sha1 = $srev->getFileSha1();
166201 }
167202 $this->stableVersions['files'][$dbKey] = array();

Status & tagging log