Untested fixup script, committing for test on wikimedia cluster.
[lhc/web/wiklou.git] / maintenance / fixTimestamps.php
1 <?php
2
3 /**
4 * This script fixes timestamp corruption caused by one or more webservers
5 * temporarily being set to the wrong time. The time offset must be known and
6 * consistent. Start and end times (in 14-character format) restrict the search,
7 * and must bracket the damage. There must be a majority of good timestamps in the
8 * search period.
9 */
10
11 require_once( 'commandLine.inc' );
12
13 if ( count( $args ) < 3 ) {
14 echo "Usage: php fixTimestamps.php <offset in hours> <start time> <end time>\n";
15 exit(1);
16 }
17
18 $offset = $args[0] * 3600;
19 $start = $args[1];
20 $end = $args[2];
21 $fname = 'fixTimestamps.php';
22 $grace = 60; // maximum normal clock offset
23
24 # Find bounding revision IDs
25 $dbw =& wfGetDB( DB_MASTER );
26 $revisionTable = $dbw->tableName( 'revision' );
27 $res = $dbw->query( "SELECT MIN(rev_id) as minrev, MAX(rev_id) as maxrev FROM $revisionTable " .
28 "WHERE rev_timestamp BETWEEN $start AND $end", $fname );
29 $row = $dbw->fetchObject( $res );
30 $minRev = $row->minrev;
31 $maxRev = $row->maxrev;
32
33 # Select all timestamps and IDs
34 $sql = "SELECT rev_id, rev_timestamp FROM $revisionTable " .
35 "WHERE rev_id BETWEEN $minRev AND $maxRev";
36 if ( $offset > 0 ) {
37 $sql .= " ORDER BY rev_id DESC";
38 $expectedSign = -1;
39 } else {
40 $expectedSign = 1;
41 }
42
43 $res = $dbw->query( $sql, $fname );
44
45 $lastNormal = 0;
46 $badRevs = array();
47 $numGoodRevs = 0;
48
49 while ( $row = $dbw->fetchObject( $res ) ) {
50 $timestamp = wfTimestamp( TS_UNIX, $row->rev_timestamp );
51 $delta = $timestamp - $lastNormal;
52 $sign = $delta / abs( $delta );
53 if ( $sign == $expectedSign ) {
54 // Monotonic change
55 $lastNormal = $timestamp;
56 ++ $numGoodRevs;
57 continue;
58 } elseif ( abs( $delta ) <= $grace ) {
59 // Non-monotonic change within grace interval
60 ++ $numGoodRevs;
61 continue;
62 } else {
63 // Non-monotonic change larger than grace interval
64 $badRevs[] = $row->rev_id;
65 }
66 }
67 $dbw->freeResult( $res );
68
69 if ( count( $badRevs ) > $numGoodRevs ) {
70 echo
71 "The majority of revisions in the search interval are marked as bad.
72
73 Are you sure the offset ($offset) has the right sign? Positive means the clock
74 was incorrectly set forward, negative means the clock was incorrectly set back.
75
76 If the offset is right, then increase the search interval until there are enough
77 good revisions to provide a majority reference.
78 ";
79
80 exit(1);
81 }
82
83 printf( "Fixing %d revisions (%.2f%% of revisions in search interval)\n",
84 count( $badRevs ), count($badRevs) / $numGoodRevs * 100 );
85
86 $sql = "UPDATE $revisionTable " .
87 "SET rev_timestamp=DATE_FORMAT(DATE_ADD(rev_timestamp, INTERVAL $fixup SECOND), '%Y%m%d%H%i%s') " .
88 "WHERE rev_id IN (" . $dbw->makeList( $badRevs ) . ')';
89 echo $sql;
90 //$dbw->query( $sql, $fname );
91 //echo "Done\n";
92
93 ?>