2009-11-22 11:30:13 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-07-17 05:40:40 +00:00
|
|
|
* Outputs page text to stdout.
|
|
|
|
|
* Useful for command-line editing automation.
|
2009-11-22 11:30:13 +00:00
|
|
|
* Example: php getText.php "page title" | sed -e '...' | php edit.php "page title"
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-07-17 05:40:40 +00:00
|
|
|
* @file
|
2009-11-22 11:30:13 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2019-09-12 19:40:57 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2020-04-15 21:50:29 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2019-07-20 08:47:52 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2009-11-22 11:30:13 +00:00
|
|
|
|
2012-07-17 05:40:40 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that outputs page text to stdout.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-11-22 11:30:13 +00:00
|
|
|
class GetTextMaint extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Outputs page text to stdout' );
|
2010-01-17 16:00:36 +00:00
|
|
|
$this->addOption( 'show-private', 'Show the text even if it\'s not available to the public' );
|
2009-11-22 11:30:13 +00:00
|
|
|
$this->addArg( 'title', 'Page title' );
|
2021-02-12 13:43:52 +00:00
|
|
|
$this->addOption( 'revision', 'Revision ID', false, true );
|
2009-11-22 11:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$titleText = $this->getArg( 0 );
|
|
|
|
|
$title = Title::newFromText( $titleText );
|
|
|
|
|
if ( !$title ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "$titleText is not a valid title.\n" );
|
2009-11-22 11:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-12 13:43:52 +00:00
|
|
|
if ( !$title->exists() ) {
|
|
|
|
|
$titleText = $title->getPrefixedText();
|
|
|
|
|
$this->fatalError( "Page $titleText does not exist.\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$revId = (int)$this->getOption( 'revision', $title->getLatestRevID() );
|
|
|
|
|
|
2023-08-31 09:21:12 +00:00
|
|
|
$rev = $this->getServiceContainer()
|
2020-04-15 21:50:29 +00:00
|
|
|
->getRevisionLookup()
|
2021-02-12 13:43:52 +00:00
|
|
|
->getRevisionByTitle( $title, $revId );
|
|
|
|
|
|
2009-11-22 11:30:13 +00:00
|
|
|
if ( !$rev ) {
|
2010-01-17 16:00:36 +00:00
|
|
|
$titleText = $title->getPrefixedText();
|
2021-02-12 13:43:52 +00:00
|
|
|
$this->fatalError( "Could not load revision $revId of $titleText.\n" );
|
2009-11-22 11:30:13 +00:00
|
|
|
}
|
2020-04-15 21:50:29 +00:00
|
|
|
|
|
|
|
|
$audience = $this->hasOption( 'show-private' ) ?
|
|
|
|
|
RevisionRecord::RAW :
|
|
|
|
|
RevisionRecord::FOR_PUBLIC;
|
|
|
|
|
$content = $rev->getContent( SlotRecord::MAIN, $audience );
|
2014-04-23 08:53:03 +00:00
|
|
|
|
2021-08-30 00:00:34 +00:00
|
|
|
if ( $content === null ) {
|
2010-01-17 16:00:36 +00:00
|
|
|
$titleText = $title->getPrefixedText();
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Couldn't extract the text from $titleText.\n" );
|
2010-01-17 16:00:36 +00:00
|
|
|
}
|
2012-09-14 11:13:29 +00:00
|
|
|
$this->output( $content->serialize() );
|
2021-02-12 13:43:52 +00:00
|
|
|
|
|
|
|
|
if ( stream_isatty( STDOUT ) ) {
|
|
|
|
|
// When writing to a TTY, add a linebreak, to keep the terminal output tidy.
|
|
|
|
|
// Wikitext will generally not have a trailing newline.
|
|
|
|
|
$this->output( "\n" );
|
|
|
|
|
}
|
2009-11-22 11:30:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = GetTextMaint::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|