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