r114978 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114977‎ | r114978 | r114979 >
Date:21:14, 19 April 2012
Author:aaron
Status:ok
Tags:
Comment:
Bug 36079 - "refreshWikiversionsCDB should fail if invalid version is specified in wikiversions.dat". Also added support for comments using #.
Modified paths:
  • /trunk/tools/mwmultiversion/multiversion/refreshWikiversionsCDB (modified) (history)

Diff [purge]

Index: trunk/tools/mwmultiversion/multiversion/refreshWikiversionsCDB
@@ -8,38 +8,32 @@
99 * @return void
1010 */
1111 function refreshWikiversionsCDB() {
12 - $path = MULTIVER_CDB_DIR_HOME . '/wikiversions.dat';
13 - $verList = array_filter( explode( "\n", file_get_contents( $path ) ) );
14 - if ( !count( $verList ) ) {
15 - die( "Unable to read wikiversions.dat.\n" );
16 - }
17 -
 12+ $srcPath = MULTIVER_CDB_DIR_HOME . '/wikiversions.dat';
1813 $tmpDBPath = MULTIVER_CDB_DIR_HOME . '/wikiversions.cdb.tmp';
1914 $finalDBPath = MULTIVER_CDB_DIR_HOME . '/wikiversions.cdb';
2015
21 - # Build new database at temp location...
 16+ // Get the array of sanitized wikiversion rows...
 17+ $rows = getWikiVerionRows( $srcPath );
 18+
 19+ # Build the new database at the temp location...
 20+ @unlink( $tmpDBPath ); // clear any old temp file for sanity
2221 $db = dba_open( $tmpDBPath, "n", "cdb_make" );
2322 if ( !$db ) {
2423 die( "Unable to create wikiversions.cdb.tmp.\n" );
2524 }
26 - foreach ( $verList as $row ) {
27 - $items = explode( ' ', $row );
28 - $dbName = $items[0];
29 - $version = $items[1];
30 - $extVersion = isset( $items[2] ) ? $items[2] : '';
31 -
 25+ foreach ( $rows as $row ) {
 26+ list( $dbName, $version, $extVersion ) = $row;
3227 dba_insert( "ver:$dbName", $version, $db );
3328 dba_insert( "ext:$dbName", $extVersion, $db );
3429 }
3530 dba_close( $db );
3631
37 - # Sanity...
38 - if ( !file_exists( $tmpDBPath ) ) {
 32+ # Sanity check the temp file...
 33+ if ( !is_file( $tmpDBPath ) ) {
3934 die( "Unable to create wikiversions.cdb.tmp.\n" );
4035 }
4136
42 - # Move to final location only when finished...
43 - @unlink( $finalDBPath );
 37+ # Move temp file to the final location only when finished...
4438 if ( !rename( $tmpDBPath, $finalDBPath ) ) {
4539 die( "Unable to move wikiversions.cdb.tmp to wikiversions.cdb.\n" );
4640 }
@@ -48,4 +42,45 @@
4943 print "wikiversions.cdb successfully built.\n";
5044 }
5145
 46+function getWikiVerionRows( $srcPath ) {
 47+ $data = file_get_contents( $srcPath );
 48+ if ( $data === false ) {
 49+ die( "Unable to read wikiversions.dat.\n" );
 50+ }
 51+ // Read the lines of the dat file into an array...
 52+ $verList = array_filter( explode( "\n", $data ) );
 53+ if ( !count( $verList ) ) {
 54+ die( "Empty table in wikiversions.dat.\n" );
 55+ }
 56+
 57+ $result = array();
 58+ foreach ( $verList as $lineNo => $line ) {
 59+ // Strip comments and ignore comment lines...
 60+ $len = strcspn( $line, '#' );
 61+ if ( $len === 0 ) {
 62+ continue; // comment line
 63+ }
 64+ $row = substr( $line, 0, $len );
 65+
 66+ // Get the column values for this row...
 67+ $items = explode( ' ', trim( $row ) ); // cleanup w/s
 68+ if ( count( $items ) === 3 ) {
 69+ list( $dbName, $version, $extVersion ) = $items;
 70+ } elseif ( count( $items ) === 2 ) {
 71+ list( $dbName, $version ) = $items;
 72+ $extVersion = ''; // none
 73+ } else {
 74+ die( "Invalid row on line $lineNo ('$line').\n" );
 75+ }
 76+
 77+ // Sanity check version directory
 78+ if ( !is_dir( MULTIVER_COMMON_HOME . '/' . $version ) ) {
 79+ die( "Invalid version dir on line $lineNo ('$line').\n" );
 80+ }
 81+
 82+ $result[] = array( $dbName, $version, $extVersion );
 83+ }
 84+ return $result;
 85+}
 86+
5287 refreshWikiversionsCDB();

Status & tagging log