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 );
31 if ( is_null( $row->minrev
) ) {
32 echo "No revisions in search period.\n";
36 $minRev = $row->minrev
;
37 $maxRev = $row->maxrev
;
39 # Select all timestamps and IDs
40 $sql = "SELECT rev_id, rev_timestamp FROM $revisionTable " .
41 "WHERE rev_id BETWEEN $minRev AND $maxRev";
43 $sql .= " ORDER BY rev_id DESC";
49 $res = $dbw->query( $sql, $fname );
55 while ( $row = $dbw->fetchObject( $res ) ) {
56 $timestamp = wfTimestamp( TS_UNIX
, $row->rev_timestamp
);
57 $delta = $timestamp - $lastNormal;
58 $sign = $delta == 0 ?
0 : $delta / abs( $delta );
59 if ( $sign == 0 ||
$sign == $expectedSign ) {
61 $lastNormal = $timestamp;
64 } elseif ( abs( $delta ) <= $grace ) {
65 // Non-monotonic change within grace interval
69 // Non-monotonic change larger than grace interval
70 $badRevs[] = $row->rev_id
;
73 $dbw->freeResult( $res );
75 $numBadRevs = count( $badRevs );
76 if ( $numBadRevs > $numGoodRevs ) {
78 "The majority of revisions in the search interval are marked as bad.
80 Are you sure the offset ($offset) has the right sign? Positive means the clock
81 was incorrectly set forward, negative means the clock was incorrectly set back.
83 If the offset is right, then increase the search interval until there are enough
84 good revisions to provide a majority reference.
88 } elseif ( $numBadRevs == 0 ) {
89 echo "No bad revisions found.\n";
93 printf( "Fixing %d revisions (%.2f%% of revisions in search interval)\n",
94 $numBadRevs, $numBadRevs / ($numGoodRevs +
$numBadRevs) * 100 );
97 $sql = "UPDATE $revisionTable " .
98 "SET rev_timestamp=DATE_FORMAT(DATE_ADD(rev_timestamp, INTERVAL $fixup SECOND), '%Y%m%d%H%i%s') " .
99 "WHERE rev_id IN (" . $dbw->makeList( $badRevs ) . ')';
101 $dbw->query( $sql, $fname );