Merge "Simplify code to avoid interpreting "$" characters in string replacement"
[mediawiki.git] / maintenance / getText.php
blobc418c8aa1c0dbcaed9c1969d93f92b16179a7d50
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"
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.
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.
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
22 * @file
23 * @ingroup Maintenance
26 use MediaWiki\Revision\RevisionRecord;
27 use MediaWiki\Revision\SlotRecord;
28 use MediaWiki\Title\Title;
30 // @codeCoverageIgnoreStart
31 require_once __DIR__ . '/Maintenance.php';
32 // @codeCoverageIgnoreEnd
34 /**
35 * Maintenance script that outputs page text to stdout.
37 * @ingroup Maintenance
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 );
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" );
55 if ( !$title->exists() ) {
56 $titleText = $title->getPrefixedText();
57 $this->fatalError( "Page $titleText does not exist.\n" );
60 $revId = (int)$this->getOption( 'revision', $title->getLatestRevID() );
62 $rev = $this->getServiceContainer()
63 ->getRevisionLookup()
64 ->getRevisionByTitle( $title, $revId );
66 if ( !$rev ) {
67 $titleText = $title->getPrefixedText();
68 $this->fatalError( "Could not load revision $revId of $titleText.\n" );
71 $audience = $this->hasOption( 'show-private' ) ?
72 RevisionRecord::RAW :
73 RevisionRecord::FOR_PUBLIC;
74 $content = $rev->getContent( SlotRecord::MAIN, $audience );
76 if ( $content === null ) {
77 $titleText = $title->getPrefixedText();
78 $this->fatalError( "Couldn't extract the text from $titleText.\n" );
80 $this->output( $content->serialize() );
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" );
90 // @codeCoverageIgnoreStart
91 $maintClass = GetTextMaint::class;
92 require_once RUN_MAINTENANCE_IF_MAIN;
93 // @codeCoverageIgnoreEnd