FetchText::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
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 32 and the first side effect is on line 25.

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
 * Communications protocol.
4
 * This is used by dumpTextPass.php when the --spawn option is present.
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
23
 */
24
25
require_once __DIR__ . '/Maintenance.php';
26
27
/**
28
 * Maintenance script used to fetch page text in a subprocess.
29
 *
30
 * @ingroup Maintenance
31
 */
32
class FetchText extends Maintenance {
33
	public function __construct() {
34
		parent::__construct();
35
		$this->addDescription( "Fetch the raw revision blob from an old_id.\n" .
36
			"NOTE: Export transformations are NOT applied. " .
37
			"This is left to backupTextPass.php"
38
		);
39
	}
40
41
	/**
42
	 * returns a string containing the following in order:
43
	 *   textid
44
	 *   \n
45
	 *   length of text (-1 on error = failure to retrieve/unserialize/gunzip/etc)
46
	 *   \n
47
	 *   text  (may be empty)
48
	 *
49
	 * note that the text string itself is *not* followed by newline
50
	 */
51
	public function execute() {
52
		$db = $this->getDB( DB_REPLICA );
53
		$stdin = $this->getStdin();
54
		while ( !feof( $stdin ) ) {
55
			$line = fgets( $stdin );
56
			if ( $line === false ) {
57
				// We appear to have lost contact...
58
				break;
59
			}
60
			$textId = intval( $line );
61
			$text = $this->doGetText( $db, $textId );
0 ignored issues
show
Bug introduced by
It seems like $db defined by $this->getDB(DB_REPLICA) on line 52 can be null; however, FetchText::doGetText() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
62
			if ( $text === false ) {
63
				# actual error, not zero-length text
64
				$textLen = "-1";
65
			} else {
66
				$textLen = strlen( $text );
67
			}
68
			$this->output( $textId . "\n" . $textLen . "\n" . $text );
69
		}
70
	}
71
72
	/**
73
	 * May throw a database error if, say, the server dies during query.
74
	 * @param Database $db
75
	 * @param int $id The old_id
76
	 * @return string
77
	 */
78
	private function doGetText( $db, $id ) {
79
		$id = intval( $id );
80
		$row = $db->selectRow( 'text',
81
			[ 'old_text', 'old_flags' ],
82
			[ 'old_id' => $id ],
83
			__METHOD__ );
84
		$text = Revision::getRevisionText( $row );
0 ignored issues
show
Bug introduced by
It seems like $row defined by $db->selectRow('text', a...d' => $id), __METHOD__) on line 80 can also be of type boolean; however, Revision::getRevisionText() does only seem to accept object<stdClass>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
		if ( $text === false ) {
86
			return false;
87
		}
88
89
		return $text;
90
	}
91
}
92
93
$maintClass = "FetchText";
94
require_once RUN_MAINTENANCE_IF_MAIN;
95