GetTextMaint   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B execute() 0 22 5
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 33 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
 * Outputs page text to stdout.
4
 * Useful for command-line editing automation.
5
 * Example: php getText.php "page title" | sed -e '...' | php edit.php "page title"
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
 * http://www.gnu.org/copyleft/gpl.html
21
 *
22
 * @file
23
 * @ingroup Maintenance
24
 */
25
26
require_once __DIR__ . '/Maintenance.php';
27
28
/**
29
 * Maintenance script that outputs page text to stdout.
30
 *
31
 * @ingroup Maintenance
32
 */
33
class GetTextMaint extends Maintenance {
34
	public function __construct() {
35
		parent::__construct();
36
		$this->addDescription( 'Outputs page text to stdout' );
37
		$this->addOption( 'show-private', 'Show the text even if it\'s not available to the public' );
38
		$this->addArg( 'title', 'Page title' );
39
	}
40
41
	public function execute() {
42
		$titleText = $this->getArg( 0 );
43
		$title = Title::newFromText( $titleText );
44
		if ( !$title ) {
45
			$this->error( "$titleText is not a valid title.\n", true );
46
		}
47
48
		$rev = Revision::newFromTitle( $title );
0 ignored issues
show
Bug introduced by
It seems like $title defined by \Title::newFromText($titleText) on line 43 can be null; however, Revision::newFromTitle() 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...
49
		if ( !$rev ) {
50
			$titleText = $title->getPrefixedText();
51
			$this->error( "Page $titleText does not exist.\n", true );
52
		}
53
		$content = $rev->getContent( $this->hasOption( 'show-private' )
54
			? Revision::RAW
55
			: Revision::FOR_PUBLIC );
56
57
		if ( $content === false ) {
58
			$titleText = $title->getPrefixedText();
59
			$this->error( "Couldn't extract the text from $titleText.\n", true );
60
		}
61
		$this->output( $content->serialize() );
62
	}
63
}
64
65
$maintClass = "GetTextMaint";
66
require_once RUN_MAINTENANCE_IF_MAIN;
67