ajaxwatch.js: coding style tweaks
[lhc/web/wiklou.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 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @ingroup Maintenance
25 */
26
27 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
28
29 class FixTimestamps extends Maintenance {
30 public function __construct() {
31 parent::__construct();
32 $this->mDescription = "";
33 $this->addArg( 'offset', '' );
34 $this->addArg( 'start', 'Starting timestamp' );
35 $this->addArg( 'end', 'Ending timestamp' );
36 }
37
38 public function execute() {
39 $offset = $this->getArg( 0 ) * 3600;
40 $start = $this->getArg( 1 );
41 $end = $this->getArg( 2 );
42 $grace = 60; // maximum normal clock offset
43
44 # Find bounding revision IDs
45 $dbw = wfGetDB( DB_MASTER );
46 $revisionTable = $dbw->tableName( 'revision' );
47 $res = $dbw->query( "SELECT MIN(rev_id) as minrev, MAX(rev_id) as maxrev FROM $revisionTable " .
48 "WHERE rev_timestamp BETWEEN '{$start}' AND '{$end}'", __METHOD__ );
49 $row = $dbw->fetchObject( $res );
50
51 if ( is_null( $row->minrev ) ) {
52 $this->error( "No revisions in search period.", true );
53 }
54
55 $minRev = $row->minrev;
56 $maxRev = $row->maxrev;
57
58 # Select all timestamps and IDs
59 $sql = "SELECT rev_id, rev_timestamp FROM $revisionTable " .
60 "WHERE rev_id BETWEEN $minRev AND $maxRev";
61 if ( $offset > 0 ) {
62 $sql .= " ORDER BY rev_id DESC";
63 $expectedSign = -1;
64 } else {
65 $expectedSign = 1;
66 }
67
68 $res = $dbw->query( $sql, __METHOD__ );
69
70 $lastNormal = 0;
71 $badRevs = array();
72 $numGoodRevs = 0;
73
74 foreach ( $res as $row ) {
75 $timestamp = wfTimestamp( TS_UNIX, $row->rev_timestamp );
76 $delta = $timestamp - $lastNormal;
77 $sign = $delta == 0 ? 0 : $delta / abs( $delta );
78 if ( $sign == 0 || $sign == $expectedSign ) {
79 // Monotonic change
80 $lastNormal = $timestamp;
81 ++ $numGoodRevs;
82 continue;
83 } elseif ( abs( $delta ) <= $grace ) {
84 // Non-monotonic change within grace interval
85 ++ $numGoodRevs;
86 continue;
87 } else {
88 // Non-monotonic change larger than grace interval
89 $badRevs[] = $row->rev_id;
90 }
91 }
92 $dbw->freeResult( $res );
93
94 $numBadRevs = count( $badRevs );
95 if ( $numBadRevs > $numGoodRevs ) {
96 $this->error(
97 "The majority of revisions in the search interval are marked as bad.
98
99 Are you sure the offset ($offset) has the right sign? Positive means the clock
100 was incorrectly set forward, negative means the clock was incorrectly set back.
101
102 If the offset is right, then increase the search interval until there are enough
103 good revisions to provide a majority reference.", true );
104 } elseif ( $numBadRevs == 0 ) {
105 $this->output( "No bad revisions found.\n" );
106 exit( 0 );
107 }
108
109 $this->output( sprintf( "Fixing %d revisions (%.2f%% of revisions in search interval)\n",
110 $numBadRevs, $numBadRevs / ( $numGoodRevs + $numBadRevs ) * 100 ) );
111
112 $fixup = -$offset;
113 $sql = "UPDATE $revisionTable " .
114 "SET rev_timestamp=DATE_FORMAT(DATE_ADD(rev_timestamp, INTERVAL $fixup SECOND), '%Y%m%d%H%i%s') " .
115 "WHERE rev_id IN (" . $dbw->makeList( $badRevs ) . ')';
116 $dbw->query( $sql, __METHOD__ );
117 $this->output( "Done\n" );
118 }
119 }
120
121 $maintClass = "FixTimestamps";
122 require_once( DO_MAINTENANCE );