resolveStubs.php ➔ resolveStub()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 50
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 4
nop 3
dl 0
loc 50
rs 8.8571
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 37 and the first side effect is on line 26.

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
 * Convert history stubs that point to an external row to direct external
4
 * pointers.
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 * http://www.gnu.org/copyleft/gpl.html
20
 *
21
 * @file
22
 * @ingroup Maintenance ExternalStorage
23
 */
24
25
if ( !defined( 'MEDIAWIKI' ) ) {
26
	$optionsWithArgs = [ 'm' ];
27
28
	require_once __DIR__ . '/../commandLine.inc';
29
30
	resolveStubs();
31
}
32
33
/**
34
 * Convert history stubs that point to an external row to direct
35
 * external pointers
36
 */
37
function resolveStubs() {
38
	$fname = 'resolveStubs';
39
40
	$dbr = wfGetDB( DB_REPLICA );
41
	$maxID = $dbr->selectField( 'text', 'MAX(old_id)', false, $fname );
42
	$blockSize = 10000;
43
	$numBlocks = intval( $maxID / $blockSize ) + 1;
44
45
	for ( $b = 0; $b < $numBlocks; $b++ ) {
46
		wfWaitForSlaves();
0 ignored issues
show
Deprecated Code introduced by
The function wfWaitForSlaves() has been deprecated with message: since 1.27 Use LBFactory::waitForReplication

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
47
48
		printf( "%5.2f%%\n", $b / $numBlocks * 100 );
49
		$start = intval( $maxID / $numBlocks ) * $b + 1;
50
		$end = intval( $maxID / $numBlocks ) * ( $b + 1 );
51
52
		$res = $dbr->select( 'text', [ 'old_id', 'old_text', 'old_flags' ],
53
			"old_id>=$start AND old_id<=$end " .
54
			"AND old_flags LIKE '%object%' AND old_flags NOT LIKE '%external%' " .
55
			'AND LOWER(CONVERT(LEFT(old_text,22) USING latin1)) = \'o:15:"historyblobstub"\'',
56
			$fname );
57
		foreach ( $res as $row ) {
58
			resolveStub( $row->old_id, $row->old_text, $row->old_flags );
59
		}
60
	}
61
	print "100%\n";
62
}
63
64
/**
65
 * Resolve a history stub
66
 * @param int $id
67
 * @param string $stubText
68
 * @param string $flags
69
 */
70
function resolveStub( $id, $stubText, $flags ) {
71
	$fname = 'resolveStub';
72
73
	$stub = unserialize( $stubText );
74
	$flags = explode( ',', $flags );
75
76
	$dbr = wfGetDB( DB_REPLICA );
77
	$dbw = wfGetDB( DB_MASTER );
78
79
	if ( strtolower( get_class( $stub ) ) !== 'historyblobstub' ) {
80
		print "Error found object of class " . get_class( $stub ) . ", expecting historyblobstub\n";
81
82
		return;
83
	}
84
85
	# Get the (maybe) external row
86
	$externalRow = $dbr->selectRow(
87
		'text',
88
		[ 'old_text' ],
89
		[
90
			'old_id' => $stub->mOldId,
91
			'old_flags' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() )
92
		],
93
		$fname
94
	);
95
96
	if ( !$externalRow ) {
97
		# Object wasn't external
98
		return;
99
	}
100
101
	# Preserve the legacy encoding flag, but switch from object to external
102
	if ( in_array( 'utf-8', $flags ) ) {
103
		$newFlags = 'external,utf-8';
104
	} else {
105
		$newFlags = 'external';
106
	}
107
108
	# Update the row
109
	# print "oldid=$id\n";
110
	$dbw->update( 'text',
111
		[ /* SET */
112
			'old_flags' => $newFlags,
113
			'old_text' => $externalRow->old_text . '/' . $stub->mHash
114
		],
115
		[ /* WHERE */
116
			'old_id' => $id
117
		], $fname
118
	);
119
}
120