moveToExternal.php ➔ moveToExternal()   C
last analyzed

Complexity

Conditions 12
Paths 47

Size

Total Lines 76
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 51
nc 47
nop 3
dl 0
loc 76
rs 5.2846
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 24 and the first side effect is on line 27.

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
 * Move revision's text to external storage
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 * @ingroup Maintenance ExternalStorage
22
 */
23
24
define( 'REPORTING_INTERVAL', 1 );
25
26
if ( !defined( 'MEDIAWIKI' ) ) {
27
	$optionsWithArgs = [ 'e', 's' ];
28
	require_once __DIR__ . '/../commandLine.inc';
29
	require_once 'resolveStubs.php';
30
31
	$fname = 'moveToExternal';
32
33
	if ( !isset( $args[0] ) ) {
34
		print "Usage: php moveToExternal.php [-s <startid>] [-e <endid>] <cluster>\n";
35
		exit;
36
	}
37
38
	$cluster = $args[0];
39
	$dbw = wfGetDB( DB_MASTER );
40
41
	if ( isset( $options['e'] ) ) {
42
		$maxID = $options['e'];
43
	} else {
44
		$maxID = $dbw->selectField( 'text', 'MAX(old_id)', false, $fname );
45
	}
46
	$minID = isset( $options['s'] ) ? $options['s'] : 1;
47
48
	moveToExternal( $cluster, $maxID, $minID );
49
}
50
51
function moveToExternal( $cluster, $maxID, $minID = 1 ) {
52
	$fname = 'moveToExternal';
53
	$dbw = wfGetDB( DB_MASTER );
54
	$dbr = wfGetDB( DB_REPLICA );
55
56
	$count = $maxID - $minID + 1;
57
	$blockSize = 1000;
58
	$numBlocks = ceil( $count / $blockSize );
59
	print "Moving text rows from $minID to $maxID to external storage\n";
60
	$ext = new ExternalStoreDB;
61
	$numMoved = 0;
62
63
	for ( $block = 0; $block < $numBlocks; $block++ ) {
64
		$blockStart = $block * $blockSize + $minID;
65
		$blockEnd = $blockStart + $blockSize - 1;
66
67
		if ( !( $block % REPORTING_INTERVAL ) ) {
68
			print "oldid=$blockStart, moved=$numMoved\n";
69
			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...
70
		}
71
72
		$res = $dbr->select( 'text', [ 'old_id', 'old_flags', 'old_text' ],
73
			[
74
				"old_id BETWEEN $blockStart AND $blockEnd",
75
				'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
76
			], $fname );
77
		foreach ( $res as $row ) {
78
			# Resolve stubs
79
			$text = $row->old_text;
80
			$id = $row->old_id;
81
			if ( $row->old_flags === '' ) {
82
				$flags = 'external';
83
			} else {
84
				$flags = "{$row->old_flags},external";
85
			}
86
87
			if ( strpos( $flags, 'object' ) !== false ) {
88
				$obj = unserialize( $text );
89
				$className = strtolower( get_class( $obj ) );
90
				if ( $className == 'historyblobstub' ) {
91
					# resolveStub( $id, $row->old_text, $row->old_flags );
92
					# $numStubs++;
93
					continue;
94
				} elseif ( $className == 'historyblobcurstub' ) {
95
					$text = gzdeflate( $obj->getText() );
96
					$flags = 'utf-8,gzip,external';
97
				} elseif ( $className == 'concatenatedgziphistoryblob' ) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
98
					// Do nothing
99
				} else {
100
					print "Warning: unrecognised object class \"$className\"\n";
101
					continue;
102
				}
103
			} else {
104
				$className = false;
105
			}
106
107
			if ( strlen( $text ) < 100 && $className === false ) {
108
				// Don't move tiny revisions
109
				continue;
110
			}
111
112
			# print "Storing "  . strlen( $text ) . " bytes to $url\n";
113
			# print "old_id=$id\n";
114
115
			$url = $ext->store( $cluster, $text );
116
			if ( !$url ) {
117
				print "Error writing to external storage\n";
118
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function moveToExternal() 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...
119
			}
120
			$dbw->update( 'text',
121
				[ 'old_flags' => $flags, 'old_text' => $url ],
122
				[ 'old_id' => $id ], $fname );
123
			$numMoved++;
124
		}
125
	}
126
}
127