Index: branches/wmf/1.18wmf1/extensions/FlaggedRevs/maintenance/fixBug28348.inc |
— | — | @@ -3,16 +3,17 @@ |
4 | 4 | function update_images_bug_28348( $start = null ) { |
5 | 5 | echo "Correcting fi_img_timestamp column in flaggedimages\n"; |
6 | 6 | |
7 | | - $BATCH_SIZE = 5000; |
| 7 | + $BATCH_SIZE = 3000; |
8 | 8 | |
9 | 9 | $logFilePath = "bug28348-fixed-" . wfWikiID(); |
10 | | - if ( 0 && file_exists( $logFilePath ) ) { |
| 10 | + if ( file_exists( $logFilePath ) ) { |
11 | 11 | echo "Already done\n"; |
12 | 12 | return; |
13 | 13 | } |
14 | 14 | |
15 | 15 | $db = wfGetDB( DB_MASTER ); |
16 | | - |
| 16 | + $dbCommons = wfGetDB( DB_SLAVE, array(), 'commonswiki' ); |
| 17 | + |
17 | 18 | if( $start === null ) { |
18 | 19 | $start = $db->selectField( 'flaggedimages', 'MIN(fi_rev_id)', false, __FUNCTION__ ); |
19 | 20 | } |
— | — | @@ -31,46 +32,61 @@ |
32 | 33 | $count = $changed = 0; |
33 | 34 | while( $blockEnd <= $end ) { |
34 | 35 | echo "...doing fi_rev_id from $blockStart to $blockEnd\n"; |
35 | | - $cond = "fi_rev_id BETWEEN $blockStart AND $blockEnd" . |
36 | | - " AND fi_img_timestamp IS NOT NULL AND img_name IS NULL"; // optimize |
37 | | - $res = $db->select( array( 'flaggedimages', 'image' ), '*', $cond, |
| 36 | + $cond = "fi_rev_id BETWEEN $blockStart AND $blockEnd AND fi_img_timestamp IS NOT NULL" . |
| 37 | + " AND img_name IS NULL AND oi_name IS NULL"; // optimize |
| 38 | + $res = $db->select( array( 'flaggedimages', 'image', 'oldimage' ), |
| 39 | + '*', |
| 40 | + $cond, |
38 | 41 | __FUNCTION__, |
39 | 42 | array(), |
40 | | - array( 'image' => array( 'LEFT JOIN', |
41 | | - 'img_sha1 = fi_img_sha1 AND img_timestamp = fi_img_timestamp' ) ) |
| 43 | + array( // skip OK references to local files |
| 44 | + 'image' => array( 'LEFT JOIN', |
| 45 | + 'img_sha1 = fi_img_sha1 AND img_timestamp = fi_img_timestamp' ), |
| 46 | + 'oldimage' => array( 'LEFT JOIN', |
| 47 | + 'oi_sha1 = fi_img_sha1 AND oi_timestamp = fi_img_timestamp' ) |
| 48 | + ) |
42 | 49 | ); |
43 | 50 | |
| 51 | + $didAnything = false; |
44 | 52 | $db->begin(); |
45 | | - # Go through and clean up missing items, as well as correct fr_quality... |
46 | | - foreach( $res as $row ) { |
| 53 | + # Go through and clean up broken file rows |
| 54 | + foreach ( $res as $row ) { |
47 | 55 | $count++; |
| 56 | + $sha1 = $row->fi_img_sha1; |
48 | 57 | $fi_img_timestamp = trim( $row->fi_img_timestamp ); // clear pad garbage |
49 | 58 | if ( !$fi_img_timestamp ) { |
50 | 59 | continue; // nothing to check |
51 | 60 | } |
52 | 61 | $time = wfTimestamp( TS_MW, $fi_img_timestamp ); |
53 | | - $sha1 = $row->fi_img_sha1; |
54 | | - # Check if the specified file exists... |
55 | | - $file = RepoGroup::singleton()->findFileFromKey( $sha1, array( 'time' => $time ) ); |
56 | | - if ( !$file ) { // doesn't exist? |
57 | | - $time = wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $time ) + 1 ); |
58 | | - # Check if the fi_img_timestamp value is off by 1 second... |
| 62 | + $offsetTime = wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $time ) + 1 ); |
| 63 | + # WMF optimization |
| 64 | + $ok = $dbCommons->select( 'image', '1', |
| 65 | + array( 'img_sha1' => $sha1, 'img_timestamp' => $dbCommons->timestamp( $time ) ) ); |
| 66 | + if ( $ok ) { |
| 67 | + continue; // current version is it; skip since it must be OK |
| 68 | + } |
| 69 | + # Check if the file exists with offset time... |
| 70 | + $file = RepoGroup::singleton()->findFileFromKey( $sha1, array( 'time' => $offsetTime ) ); |
| 71 | + if ( $file ) { // doesn't exist? |
| 72 | + # Confirm that fi_img_timestamp value is off by 1 second... |
59 | 73 | $file = RepoGroup::singleton()->findFileFromKey( $sha1, array( 'time' => $time ) ); |
60 | | - if ( $file ) { |
| 74 | + if ( !$file ) { |
61 | 75 | echo "fixed file {$row->fi_name} reference in rev ID {$row->fi_rev_id}\n"; |
62 | 76 | # Fix the fi_img_timestamp value... |
63 | 77 | $db->update( 'flaggedimages', |
64 | | - array( 'fi_img_timestamp' => $db->timestamp( $time ) ), |
| 78 | + array( 'fi_img_timestamp' => $db->timestamp( $offsetTime ) ), |
65 | 79 | array( 'fi_rev_id' => $row->fi_rev_id, 'fi_name' => $row->fi_name ), |
66 | 80 | __METHOD__ |
67 | 81 | ); |
68 | 82 | $changed++; |
| 83 | + $didAnything = true; |
69 | 84 | $logDump .= "{$row->fi_rev_id} {$row->fi_name} {$fi_img_timestamp}\n"; |
70 | 85 | } |
71 | 86 | } |
72 | 87 | } |
73 | | - $didAnything = ( $res->numRows() > 0 ); |
74 | | - file_put_contents( $logFilePath, $logDump, FILE_APPEND ); |
| 88 | + if ( $logDump != '' ) { |
| 89 | + file_put_contents( $logFilePath, $logDump, FILE_APPEND ); |
| 90 | + } |
75 | 91 | $logDump = ''; |
76 | 92 | $db->commit(); |
77 | 93 | |