MediaWiki  REL1_31
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  $grace = 60; // maximum normal clock offset
50 
51  # Find bounding revision IDs
52  $dbw = $this->getDB( DB_MASTER );
53  $revisionTable = $dbw->tableName( 'revision' );
54  $res = $dbw->query( "SELECT MIN(rev_id) as minrev, MAX(rev_id) as maxrev FROM $revisionTable " .
55  "WHERE rev_timestamp BETWEEN '{$start}' AND '{$end}'", __METHOD__ );
56  $row = $dbw->fetchObject( $res );
57 
58  if ( is_null( $row->minrev ) ) {
59  $this->fatalError( "No revisions in search period." );
60  }
61 
62  $minRev = $row->minrev;
63  $maxRev = $row->maxrev;
64 
65  # Select all timestamps and IDs
66  $sql = "SELECT rev_id, rev_timestamp FROM $revisionTable " .
67  "WHERE rev_id BETWEEN $minRev AND $maxRev";
68  if ( $offset > 0 ) {
69  $sql .= " ORDER BY rev_id DESC";
70  $expectedSign = -1;
71  } else {
72  $expectedSign = 1;
73  }
74 
75  $res = $dbw->query( $sql, __METHOD__ );
76 
77  $lastNormal = 0;
78  $badRevs = [];
79  $numGoodRevs = 0;
80 
81  foreach ( $res as $row ) {
82  $timestamp = wfTimestamp( TS_UNIX, $row->rev_timestamp );
83  $delta = $timestamp - $lastNormal;
84  $sign = $delta == 0 ? 0 : $delta / abs( $delta );
85  if ( $sign == 0 || $sign == $expectedSign ) {
86  // Monotonic change
87  $lastNormal = $timestamp;
88  ++$numGoodRevs;
89  continue;
90  } elseif ( abs( $delta ) <= $grace ) {
91  // Non-monotonic change within grace interval
92  ++$numGoodRevs;
93  continue;
94  } else {
95  // Non-monotonic change larger than grace interval
96  $badRevs[] = $row->rev_id;
97  }
98  }
99 
100  $numBadRevs = count( $badRevs );
101  if ( $numBadRevs > $numGoodRevs ) {
102  $this->fatalError(
103  "The majority of revisions in the search interval are marked as bad.
104 
105  Are you sure the offset ($offset) has the right sign? Positive means the clock
106  was incorrectly set forward, negative means the clock was incorrectly set back.
107 
108  If the offset is right, then increase the search interval until there are enough
109  good revisions to provide a majority reference." );
110  } elseif ( $numBadRevs == 0 ) {
111  $this->output( "No bad revisions found.\n" );
112  exit( 0 );
113  }
114 
115  $this->output( sprintf( "Fixing %d revisions (%.2f%% of revisions in search interval)\n",
116  $numBadRevs, $numBadRevs / ( $numGoodRevs + $numBadRevs ) * 100 ) );
117 
118  $fixup = -$offset;
119  $sql = "UPDATE $revisionTable " .
120  "SET rev_timestamp="
121  . "DATE_FORMAT(DATE_ADD(rev_timestamp, INTERVAL $fixup SECOND), '%Y%m%d%H%i%s') " .
122  "WHERE rev_id IN (" . $dbw->makeList( $badRevs ) . ')';
123  $dbw->query( $sql, __METHOD__ );
124  $this->output( "Done\n" );
125  }
126 }
127 
129 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:439
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:291
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1980
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
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:29
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:271
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1309
$maintClass
$maintClass
Definition: fixTimestamps.php:128
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:388
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:310
FixTimestamps
Maintenance script that fixes timestamp corruption caused by one or more webservers temporarily being...
Definition: fixTimestamps.php:36