| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
require_once __DIR__ . '/Maintenance.php'; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class FixTimestamps extends Maintenance { |
| 39 |
public function __construct() { |
| 40 |
parent::__construct(); |
| 41 |
$this->addDescription( '' ); |
| 42 |
$this->addArg( 'offset', '' ); |
| 43 |
$this->addArg( 'start', 'Starting timestamp' ); |
| 44 |
$this->addArg( 'end', 'Ending timestamp' ); |
| 45 |
} |
| 46 |
|
| 47 |
public function execute() { |
| 48 |
$offset = $this->getArg( 0 ) * 3600; |
| 49 |
$start = $this->getArg( 1 ); |
| 50 |
$end = $this->getArg( 2 ); |
| 51 |
|
| 52 |
$grace = 60; |
| 53 |
|
| 54 |
|
| 55 |
$dbw = $this->getPrimaryDB(); |
| 56 |
$revisionTable = $dbw->tableName( 'revision' ); |
| 57 |
$res = $dbw->query( "SELECT MIN(rev_id) as minrev, MAX(rev_id) as maxrev FROM $revisionTable " . |
| 58 |
"WHERE rev_timestamp BETWEEN '{$start}' AND '{$end}'", __METHOD__ ); |
| 59 |
$row = $res->fetchObject(); |
| 60 |
|
| 61 |
if ( $row->minrev === null ) { |
| 62 |
$this->fatalError( "No revisions in search period." ); |
| 63 |
} |
| 64 |
|
| 65 |
$minRev = $row->minrev; |
| 66 |
$maxRev = $row->maxrev; |
| 67 |
|
| 68 |
|
| 69 |
$sql = "SELECT rev_id, rev_timestamp FROM $revisionTable " . |
| 70 |
"WHERE rev_id BETWEEN $minRev AND $maxRev"; |
| 71 |
if ( $offset > 0 ) { |
| 72 |
$sql .= " ORDER BY rev_id DESC"; |
| 73 |
$expectedSign = -1; |
| 74 |
} else { |
| 75 |
$expectedSign = 1; |
| 76 |
} |
| 77 |
|
| 78 |
$res = $dbw->query( $sql, __METHOD__ ); |
| 79 |
|
| 80 |
$lastNormal = 0; |
| 81 |
$badRevs = []; |
| 82 |
$numGoodRevs = 0; |
| 83 |
|
| 84 |
foreach ( $res as $row ) { |
| 85 |
$timestamp = (int)wfTimestamp( TS_UNIX, $row->rev_timestamp ); |
| 86 |
$delta = $timestamp - $lastNormal; |
| 87 |
$sign = $delta == 0 ? 0 : $delta / abs( $delta ); |
| 88 |
if ( $sign == 0 || $sign == $expectedSign ) { |
| 89 |
|
| 90 |
$lastNormal = $timestamp; |
| 91 |
++$numGoodRevs; |
| 92 |
} elseif ( abs( $delta ) <= $grace ) { |
| 93 |
|
| 94 |
++$numGoodRevs; |
| 95 |
} else { |
| 96 |
|
| 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 |
return; |
| 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 |
|
| 130 |
$maintClass = FixTimestamps::class; |
| 131 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 132 |
|