MediaWiki master
fixTimestamps.php
Go to the documentation of this file.
1<?php
15use Wikimedia\Timestamp\TimestampFormat as TS;
16
17// @codeCoverageIgnoreStart
18require_once __DIR__ . '/Maintenance.php';
19// @codeCoverageIgnoreEnd
20
28 public function __construct() {
29 parent::__construct();
30 $this->addDescription( '' );
31 $this->addArg( 'offset', '' );
32 $this->addArg( 'start', 'Starting timestamp' );
33 $this->addArg( 'end', 'Ending timestamp' );
34 }
35
36 public function execute() {
37 $offset = $this->getArg( 0 ) * 3600;
38 $start = $this->getArg( 1 );
39 $end = $this->getArg( 2 );
40 // maximum normal clock offset
41 $grace = 60;
42
43 # Find bounding revision IDs
44 $dbw = $this->getPrimaryDB();
45 $revisionTable = $dbw->tableName( 'revision' );
46 $res = $dbw->query( "SELECT MIN(rev_id) as minrev, MAX(rev_id) as maxrev FROM $revisionTable " .
47 "WHERE rev_timestamp BETWEEN '{$start}' AND '{$end}'", __METHOD__ );
48 $row = $res->fetchObject();
49
50 if ( $row->minrev === null ) {
51 $this->fatalError( "No revisions in search period." );
52 }
53
54 $minRev = $row->minrev;
55 $maxRev = $row->maxrev;
56
57 # Select all timestamps and IDs
58 $sql = "SELECT rev_id, rev_timestamp FROM $revisionTable " .
59 "WHERE rev_id BETWEEN $minRev AND $maxRev";
60 if ( $offset > 0 ) {
61 $sql .= " ORDER BY rev_id DESC";
62 $expectedSign = -1;
63 } else {
64 $expectedSign = 1;
65 }
66
67 $res = $dbw->query( $sql, __METHOD__ );
68
69 $lastNormal = 0;
70 $badRevs = [];
71 $numGoodRevs = 0;
72
73 foreach ( $res as $row ) {
74 $timestamp = (int)wfTimestamp( TS::UNIX, $row->rev_timestamp );
75 $delta = $timestamp - $lastNormal;
76 $sign = $delta == 0 ? 0 : $delta / abs( $delta );
77 if ( $sign == 0 || $sign == $expectedSign ) {
78 // Monotonic change
79 $lastNormal = $timestamp;
80 ++$numGoodRevs;
81 } elseif ( abs( $delta ) <= $grace ) {
82 // Non-monotonic change within grace interval
83 ++$numGoodRevs;
84 } else {
85 // Non-monotonic change larger than grace interval
86 $badRevs[] = $row->rev_id;
87 }
88 }
89
90 $numBadRevs = count( $badRevs );
91 if ( $numBadRevs > $numGoodRevs ) {
92 $this->fatalError(
93 "The majority of revisions in the search interval are marked as bad.
94
95 Are you sure the offset ($offset) has the right sign? Positive means the clock
96 was incorrectly set forward, negative means the clock was incorrectly set back.
97
98 If the offset is right, then increase the search interval until there are enough
99 good revisions to provide a majority reference." );
100 } elseif ( $numBadRevs == 0 ) {
101 $this->output( "No bad revisions found.\n" );
102 return;
103 }
104
105 $this->output( sprintf( "Fixing %d revisions (%.2f%% of revisions in search interval)\n",
106 $numBadRevs, $numBadRevs / ( $numGoodRevs + $numBadRevs ) * 100 ) );
107
108 $fixup = -$offset;
109 $sql = "UPDATE $revisionTable " .
110 "SET rev_timestamp="
111 . "DATE_FORMAT(DATE_ADD(rev_timestamp, INTERVAL $fixup SECOND), '%Y%m%d%H%i%s') " .
112 "WHERE rev_id IN (" . $dbw->makeList( $badRevs ) . ')';
113 $dbw->query( $sql, __METHOD__ );
114 $this->output( "Done\n" );
115 }
116}
117
118// @codeCoverageIgnoreStart
119$maintClass = FixTimestamps::class;
120require_once RUN_MAINTENANCE_IF_MAIN;
121// @codeCoverageIgnoreEnd
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
Maintenance script that fixes timestamp corruption caused by one or more webservers temporarily being...
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
$maintClass