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
11 require_once( 'commandLine.inc' );
13 if ( count( $args ) < 3 ) {
14 echo "Usage: php fixTimestamps.php <offset in hours> <start time> <end time>\n";
18 $offset = $args[0] * 3600;
21 $fname = 'fixTimestamps.php';
22 $grace = 60; // maximum normal clock offset
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
;
33 # Select all timestamps and IDs
34 $sql = "SELECT rev_id, rev_timestamp FROM $revisionTable " .
35 "WHERE rev_id BETWEEN $minRev AND $maxRev";
37 $sql .= " ORDER BY rev_id DESC";
43 $res = $dbw->query( $sql, $fname );
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 ) {
55 $lastNormal = $timestamp;
58 } elseif ( abs( $delta ) <= $grace ) {
59 // Non-monotonic change within grace interval
63 // Non-monotonic change larger than grace interval
64 $badRevs[] = $row->rev_id
;
67 $dbw->freeResult( $res );
69 if ( count( $badRevs ) > $numGoodRevs ) {
71 "The majority of revisions in the search interval are marked as bad.
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.
76 If the offset is right, then increase the search interval until there are enough
77 good revisions to provide a majority reference.
83 printf( "Fixing %d revisions (%.2f%% of revisions in search interval)\n",
84 count( $badRevs ), count($badRevs) / $numGoodRevs * 100 );
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 ) . ')';
90 //$dbw->query( $sql, $fname );