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 | * @license GPL-2.0-or-later |
| 8 | * @file |
| 9 | * @ingroup Maintenance |
| 10 | */ |
| 11 | |
| 12 | use MediaWiki\Maintenance\Maintenance; |
| 13 | use MediaWiki\Revision\RevisionRecord; |
| 14 | use MediaWiki\Revision\SlotRecord; |
| 15 | use MediaWiki\Title\Title; |
| 16 | |
| 17 | // @codeCoverageIgnoreStart |
| 18 | require_once __DIR__ . '/Maintenance.php'; |
| 19 | // @codeCoverageIgnoreEnd |
| 20 | |
| 21 | /** |
| 22 | * Maintenance script that outputs page text to stdout. |
| 23 | * |
| 24 | * @ingroup Maintenance |
| 25 | */ |
| 26 | class GetTextMaint extends Maintenance { |
| 27 | public function __construct() { |
| 28 | parent::__construct(); |
| 29 | $this->addDescription( 'Outputs page text to stdout' ); |
| 30 | $this->addOption( 'show-private', 'Show the text even if it\'s not available to the public' ); |
| 31 | $this->addArg( 'title', 'Page title' ); |
| 32 | $this->addOption( 'revision', 'Revision ID', false, true ); |
| 33 | } |
| 34 | |
| 35 | public function execute() { |
| 36 | $titleText = $this->getArg( 0 ); |
| 37 | $title = Title::newFromText( $titleText ); |
| 38 | if ( !$title ) { |
| 39 | $this->fatalError( "$titleText is not a valid title.\n" ); |
| 40 | } |
| 41 | |
| 42 | if ( !$title->exists() ) { |
| 43 | $titleText = $title->getPrefixedText(); |
| 44 | $this->fatalError( "Page $titleText does not exist.\n" ); |
| 45 | } |
| 46 | |
| 47 | $revId = (int)$this->getOption( 'revision', $title->getLatestRevID() ); |
| 48 | |
| 49 | $rev = $this->getServiceContainer() |
| 50 | ->getRevisionLookup() |
| 51 | ->getRevisionByTitle( $title, $revId ); |
| 52 | |
| 53 | if ( !$rev ) { |
| 54 | $titleText = $title->getPrefixedText(); |
| 55 | $this->fatalError( "Could not load revision $revId of $titleText.\n" ); |
| 56 | } |
| 57 | |
| 58 | $audience = $this->hasOption( 'show-private' ) ? |
| 59 | RevisionRecord::RAW : |
| 60 | RevisionRecord::FOR_PUBLIC; |
| 61 | $content = $rev->getContent( SlotRecord::MAIN, $audience ); |
| 62 | |
| 63 | if ( $content === null ) { |
| 64 | $titleText = $title->getPrefixedText(); |
| 65 | $this->fatalError( "Couldn't extract the text from $titleText.\n" ); |
| 66 | } |
| 67 | $this->output( $content->serialize() ); |
| 68 | |
| 69 | if ( stream_isatty( STDOUT ) ) { |
| 70 | // When writing to a TTY, add a linebreak, to keep the terminal output tidy. |
| 71 | // Wikitext will generally not have a trailing newline. |
| 72 | $this->output( "\n" ); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // @codeCoverageIgnoreStart |
| 78 | $maintClass = GetTextMaint::class; |
| 79 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 80 | // @codeCoverageIgnoreEnd |