* Use double quoted consistently on attributes in linker output; preparing for new...
[mediawiki.git] / maintenance / fixTimestamps.php
blob784e35cdfc0d4d46dd36349e08a4f7ac73f79652
1 <?php
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 */
11 require_once( 'commandLine.inc' );
13 if ( count( $args ) < 3 ) {
14 echo "Usage: php fixTimestamps.php <offset in hours> <start time> <end time>\n";
15 exit(1);
18 $offset = $args[0] * 3600;
19 $start = $args[1];
20 $end = $args[2];
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";
33 exit(0);
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";
42 if ( $offset > 0 ) {
43 $sql .= " ORDER BY rev_id DESC";
44 $expectedSign = -1;
45 } else {
46 $expectedSign = 1;
49 $res = $dbw->query( $sql, $fname );
51 $lastNormal = 0;
52 $badRevs = array();
53 $numGoodRevs = 0;
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 ) {
60 // Monotonic change
61 $lastNormal = $timestamp;
62 ++ $numGoodRevs;
63 continue;
64 } elseif ( abs( $delta ) <= $grace ) {
65 // Non-monotonic change within grace interval
66 ++ $numGoodRevs;
67 continue;
68 } else {
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 ) {
77 echo
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.
87 exit(1);
88 } elseif ( $numBadRevs == 0 ) {
89 echo "No bad revisions found.\n";
90 exit(0);
93 printf( "Fixing %d revisions (%.2f%% of revisions in search interval)\n",
94 $numBadRevs, $numBadRevs / ($numGoodRevs + $numBadRevs) * 100 );
96 $fixup = -$offset;
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 ) . ')';
100 //echo "$sql\n";
101 $dbw->query( $sql, $fname );
102 echo "Done\n";