https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114978

Revision: 114978
Author:   aaron
Date:     2012-04-19 21:14:57 +0000 (Thu, 19 Apr 2012)
Log Message:
-----------
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: trunk/tools/mwmultiversion/multiversion/refreshWikiversionsCDB
===================================================================
--- trunk/tools/mwmultiversion/multiversion/refreshWikiversionsCDB      
2012-04-19 21:11:12 UTC (rev 114977)
+++ trunk/tools/mwmultiversion/multiversion/refreshWikiversionsCDB      
2012-04-19 21:14:57 UTC (rev 114978)
@@ -8,38 +8,32 @@
  * @return void
  */
 function refreshWikiversionsCDB() {
-       $path = MULTIVER_CDB_DIR_HOME . '/wikiversions.dat';
-       $verList = array_filter( explode( "\n", file_get_contents( $path ) ) );
-       if ( !count( $verList ) ) {
-               die( "Unable to read wikiversions.dat.\n" );
-       }
-
+       $srcPath = MULTIVER_CDB_DIR_HOME . '/wikiversions.dat';
        $tmpDBPath = MULTIVER_CDB_DIR_HOME . '/wikiversions.cdb.tmp';
        $finalDBPath = MULTIVER_CDB_DIR_HOME . '/wikiversions.cdb';
 
-       # Build new database at temp location...
+       // Get the array of sanitized wikiversion rows...
+       $rows = getWikiVerionRows( $srcPath );
+
+       # Build the new database at the temp location...
+       @unlink( $tmpDBPath ); // clear any old temp file for sanity
        $db = dba_open( $tmpDBPath, "n", "cdb_make" );
        if ( !$db ) {
                die( "Unable to create wikiversions.cdb.tmp.\n" );
        }
-       foreach ( $verList as $row ) {
-               $items = explode( ' ', $row );
-               $dbName = $items[0];
-               $version = $items[1];
-               $extVersion = isset( $items[2] ) ? $items[2] : '';
-
+       foreach ( $rows as $row ) {
+               list( $dbName, $version, $extVersion ) = $row;
                dba_insert( "ver:$dbName", $version, $db );
                dba_insert( "ext:$dbName", $extVersion, $db );
        }
        dba_close( $db );
 
-       # Sanity...
-       if ( !file_exists( $tmpDBPath ) ) {
+       # Sanity check the temp file...
+       if ( !is_file( $tmpDBPath ) ) {
                die( "Unable to create wikiversions.cdb.tmp.\n" );
        }
 
-       # Move to final location only when finished...
-       @unlink( $finalDBPath );
+       # Move temp file to the final location only when finished...
        if ( !rename( $tmpDBPath, $finalDBPath ) ) {
                die( "Unable to move wikiversions.cdb.tmp to 
wikiversions.cdb.\n" );
        }
@@ -48,4 +42,45 @@
        print "wikiversions.cdb successfully built.\n";
 }
 
+function getWikiVerionRows( $srcPath ) {
+       $data = file_get_contents( $srcPath );
+       if ( $data === false ) {
+               die( "Unable to read wikiversions.dat.\n" );
+       }
+       // Read the lines of the dat file into an array...
+       $verList = array_filter( explode( "\n", $data ) );
+       if ( !count( $verList ) ) {
+               die( "Empty table in wikiversions.dat.\n" );
+       }
+
+       $result = array();
+       foreach ( $verList as $lineNo => $line ) {
+               // Strip comments and ignore comment lines...
+               $len = strcspn( $line, '#' );
+               if ( $len === 0 ) {
+                       continue; // comment line
+               }
+               $row = substr( $line, 0, $len );
+
+               // Get the column values for this row...
+               $items = explode( ' ', trim( $row ) ); // cleanup w/s
+               if ( count( $items ) === 3 ) {
+                       list( $dbName, $version, $extVersion ) = $items;
+               } elseif ( count( $items ) === 2 ) {
+                       list( $dbName, $version ) = $items;
+                       $extVersion = ''; // none
+               } else {
+                       die( "Invalid row on line $lineNo ('$line').\n" );
+               }
+
+               // Sanity check version directory
+               if ( !is_dir( MULTIVER_COMMON_HOME . '/' . $version ) ) {
+                       die( "Invalid version dir on line $lineNo ('$line').\n" 
);
+               }
+
+               $result[] = array( $dbName, $version, $extVersion );
+       }
+       return $result;
+}
+
 refreshWikiversionsCDB();


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to