MediaWiki master
fixTimestamps.php
Go to the documentation of this file.
1<?php
29
30// @codeCoverageIgnoreStart
31require_once __DIR__ . '/Maintenance.php';
32// @codeCoverageIgnoreEnd
33
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( '' );
44 $this->addArg( 'offset', '' );
45 $this->addArg( 'start', 'Starting timestamp' );
46 $this->addArg( 'end', 'Ending timestamp' );
47 }
48
49 public function execute() {
50 $offset = $this->getArg( 0 ) * 3600;
51 $start = $this->getArg( 1 );
52 $end = $this->getArg( 2 );
53 // maximum normal clock offset
54 $grace = 60;
55
56 # Find bounding revision IDs
57 $dbw = $this->getPrimaryDB();
58 $revisionTable = $dbw->tableName( 'revision' );
59 $res = $dbw->query( "SELECT MIN(rev_id) as minrev, MAX(rev_id) as maxrev FROM $revisionTable " .
60 "WHERE rev_timestamp BETWEEN '{$start}' AND '{$end}'", __METHOD__ );
61 $row = $res->fetchObject();
62
63 if ( $row->minrev === null ) {
64 $this->fatalError( "No revisions in search period." );
65 }
66
67 $minRev = $row->minrev;
68 $maxRev = $row->maxrev;
69
70 # Select all timestamps and IDs
71 $sql = "SELECT rev_id, rev_timestamp FROM $revisionTable " .
72 "WHERE rev_id BETWEEN $minRev AND $maxRev";
73 if ( $offset > 0 ) {
74 $sql .= " ORDER BY rev_id DESC";
75 $expectedSign = -1;
76 } else {
77 $expectedSign = 1;
78 }
79
80 $res = $dbw->query( $sql, __METHOD__ );
81
82 $lastNormal = 0;
83 $badRevs = [];
84 $numGoodRevs = 0;
85
86 foreach ( $res as $row ) {
87 $timestamp = (int)wfTimestamp( TS_UNIX, $row->rev_timestamp );
88 $delta = $timestamp - $lastNormal;
89 $sign = $delta == 0 ? 0 : $delta / abs( $delta );
90 if ( $sign == 0 || $sign == $expectedSign ) {
91 // Monotonic change
92 $lastNormal = $timestamp;
93 ++$numGoodRevs;
94 } elseif ( abs( $delta ) <= $grace ) {
95 // Non-monotonic change within grace interval
96 ++$numGoodRevs;
97 } else {
98 // Non-monotonic change larger than grace interval
99 $badRevs[] = $row->rev_id;
100 }
101 }
102
103 $numBadRevs = count( $badRevs );
104 if ( $numBadRevs > $numGoodRevs ) {
105 $this->fatalError(
106 "The majority of revisions in the search interval are marked as bad.
107
108 Are you sure the offset ($offset) has the right sign? Positive means the clock
109 was incorrectly set forward, negative means the clock was incorrectly set back.
110
111 If the offset is right, then increase the search interval until there are enough
112 good revisions to provide a majority reference." );
113 } elseif ( $numBadRevs == 0 ) {
114 $this->output( "No bad revisions found.\n" );
115 return;
116 }
117
118 $this->output( sprintf( "Fixing %d revisions (%.2f%% of revisions in search interval)\n",
119 $numBadRevs, $numBadRevs / ( $numGoodRevs + $numBadRevs ) * 100 ) );
120
121 $fixup = -$offset;
122 $sql = "UPDATE $revisionTable " .
123 "SET rev_timestamp="
124 . "DATE_FORMAT(DATE_ADD(rev_timestamp, INTERVAL $fixup SECOND), '%Y%m%d%H%i%s') " .
125 "WHERE rev_id IN (" . $dbw->makeList( $badRevs ) . ')';
126 $dbw->query( $sql, __METHOD__ );
127 $this->output( "Done\n" );
128 }
129}
130
131// @codeCoverageIgnoreStart
132$maintClass = FixTimestamps::class;
133require_once RUN_MAINTENANCE_IF_MAIN;
134// @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.
addDescription( $text)
Set the description text.
$maintClass