FixTimestamps::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 36 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Fixes timestamp corruption caused by one or more webservers temporarily
4
 * being set to the wrong time.
5
 * The time offset must be known and consistent. Start and end times
6
 * (in 14-character format) restrict the search, and must bracket the damage.
7
 * There must be a majority of good timestamps in the search period.
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License along
20
 * with this program; if not, write to the Free Software Foundation, Inc.,
21
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
 * http://www.gnu.org/copyleft/gpl.html
23
 *
24
 * @file
25
 * @ingroup Maintenance
26
 */
27
28
require_once __DIR__ . '/Maintenance.php';
29
30
/**
31
 * Maintenance script that fixes timestamp corruption caused by one or
32
 * more webservers temporarily being set to the wrong time.
33
 *
34
 * @ingroup Maintenance
35
 */
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->error( "No revisions in search period.", true );
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->error(
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.", true );
110
		} elseif ( $numBadRevs == 0 ) {
111
			$this->output( "No bad revisions found.\n" );
112
			exit( 0 );
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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
128
$maintClass = "FixTimestamps";
129
require_once RUN_MAINTENANCE_IF_MAIN;
130