Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.55% |
28 / 29 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
GetTextMaint | |
96.55% |
28 / 29 |
|
50.00% |
1 / 2 |
8 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
7 |
1 | <?php |
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 | use MediaWiki\Revision\RevisionRecord; |
27 | use MediaWiki\Revision\SlotRecord; |
28 | use MediaWiki\Title\Title; |
29 | |
30 | // @codeCoverageIgnoreStart |
31 | require_once __DIR__ . '/Maintenance.php'; |
32 | // @codeCoverageIgnoreEnd |
33 | |
34 | /** |
35 | * Maintenance script that outputs page text to stdout. |
36 | * |
37 | * @ingroup Maintenance |
38 | */ |
39 | class GetTextMaint extends Maintenance { |
40 | public function __construct() { |
41 | parent::__construct(); |
42 | $this->addDescription( 'Outputs page text to stdout' ); |
43 | $this->addOption( 'show-private', 'Show the text even if it\'s not available to the public' ); |
44 | $this->addArg( 'title', 'Page title' ); |
45 | $this->addOption( 'revision', 'Revision ID', false, true ); |
46 | } |
47 | |
48 | public function execute() { |
49 | $titleText = $this->getArg( 0 ); |
50 | $title = Title::newFromText( $titleText ); |
51 | if ( !$title ) { |
52 | $this->fatalError( "$titleText is not a valid title.\n" ); |
53 | } |
54 | |
55 | if ( !$title->exists() ) { |
56 | $titleText = $title->getPrefixedText(); |
57 | $this->fatalError( "Page $titleText does not exist.\n" ); |
58 | } |
59 | |
60 | $revId = (int)$this->getOption( 'revision', $title->getLatestRevID() ); |
61 | |
62 | $rev = $this->getServiceContainer() |
63 | ->getRevisionLookup() |
64 | ->getRevisionByTitle( $title, $revId ); |
65 | |
66 | if ( !$rev ) { |
67 | $titleText = $title->getPrefixedText(); |
68 | $this->fatalError( "Could not load revision $revId of $titleText.\n" ); |
69 | } |
70 | |
71 | $audience = $this->hasOption( 'show-private' ) ? |
72 | RevisionRecord::RAW : |
73 | RevisionRecord::FOR_PUBLIC; |
74 | $content = $rev->getContent( SlotRecord::MAIN, $audience ); |
75 | |
76 | if ( $content === null ) { |
77 | $titleText = $title->getPrefixedText(); |
78 | $this->fatalError( "Couldn't extract the text from $titleText.\n" ); |
79 | } |
80 | $this->output( $content->serialize() ); |
81 | |
82 | if ( stream_isatty( STDOUT ) ) { |
83 | // When writing to a TTY, add a linebreak, to keep the terminal output tidy. |
84 | // Wikitext will generally not have a trailing newline. |
85 | $this->output( "\n" ); |
86 | } |
87 | } |
88 | } |
89 | |
90 | // @codeCoverageIgnoreStart |
91 | $maintClass = GetTextMaint::class; |
92 | require_once RUN_MAINTENANCE_IF_MAIN; |
93 | // @codeCoverageIgnoreEnd |