Index: trunk/tools/mwmultiversion/multiversion/refreshWikiversionsCDB |
— | — | @@ -8,38 +8,32 @@ |
9 | 9 | * @return void |
10 | 10 | */ |
11 | 11 | 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'; |
18 | 13 | $tmpDBPath = MULTIVER_CDB_DIR_HOME . '/wikiversions.cdb.tmp'; |
19 | 14 | $finalDBPath = MULTIVER_CDB_DIR_HOME . '/wikiversions.cdb'; |
20 | 15 | |
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 |
22 | 21 | $db = dba_open( $tmpDBPath, "n", "cdb_make" ); |
23 | 22 | if ( !$db ) { |
24 | 23 | die( "Unable to create wikiversions.cdb.tmp.\n" ); |
25 | 24 | } |
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; |
32 | 27 | dba_insert( "ver:$dbName", $version, $db ); |
33 | 28 | dba_insert( "ext:$dbName", $extVersion, $db ); |
34 | 29 | } |
35 | 30 | dba_close( $db ); |
36 | 31 | |
37 | | - # Sanity... |
38 | | - if ( !file_exists( $tmpDBPath ) ) { |
| 32 | + # Sanity check the temp file... |
| 33 | + if ( !is_file( $tmpDBPath ) ) { |
39 | 34 | die( "Unable to create wikiversions.cdb.tmp.\n" ); |
40 | 35 | } |
41 | 36 | |
42 | | - # Move to final location only when finished... |
43 | | - @unlink( $finalDBPath ); |
| 37 | + # Move temp file to the final location only when finished... |
44 | 38 | if ( !rename( $tmpDBPath, $finalDBPath ) ) { |
45 | 39 | die( "Unable to move wikiversions.cdb.tmp to wikiversions.cdb.\n" ); |
46 | 40 | } |
— | — | @@ -48,4 +42,45 @@ |
49 | 43 | print "wikiversions.cdb successfully built.\n"; |
50 | 44 | } |
51 | 45 | |
| 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 | + |
52 | 87 | refreshWikiversionsCDB(); |