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