MediaWiki  master
getText.php
Go to the documentation of this file.
1 <?php
29 
30 require_once __DIR__ . '/Maintenance.php';
31 
37 class GetTextMaint extends Maintenance {
38  public function __construct() {
39  parent::__construct();
40  $this->addDescription( 'Outputs page text to stdout' );
41  $this->addOption( 'show-private', 'Show the text even if it\'s not available to the public' );
42  $this->addArg( 'title', 'Page title' );
43  $this->addOption( 'revision', 'Revision ID', false, true );
44  }
45 
46  public function execute() {
47  $titleText = $this->getArg( 0 );
48  $title = Title::newFromText( $titleText );
49  if ( !$title ) {
50  $this->fatalError( "$titleText is not a valid title.\n" );
51  }
52 
53  if ( !$title->exists() ) {
54  $titleText = $title->getPrefixedText();
55  $this->fatalError( "Page $titleText does not exist.\n" );
56  }
57 
58  $revId = (int)$this->getOption( 'revision', $title->getLatestRevID() );
59 
60  $rev = MediaWikiServices::getInstance()
61  ->getRevisionLookup()
62  ->getRevisionByTitle( $title, $revId );
63 
64  if ( !$rev ) {
65  $titleText = $title->getPrefixedText();
66  $this->fatalError( "Could not load revision $revId of $titleText.\n" );
67  }
68 
69  $audience = $this->hasOption( 'show-private' ) ?
70  RevisionRecord::RAW :
71  RevisionRecord::FOR_PUBLIC;
72  $content = $rev->getContent( SlotRecord::MAIN, $audience );
73 
74  if ( $content === null ) {
75  $titleText = $title->getPrefixedText();
76  $this->fatalError( "Couldn't extract the text from $titleText.\n" );
77  }
78  $this->output( $content->serialize() );
79 
80  if ( stream_isatty( STDOUT ) ) {
81  // When writing to a TTY, add a linebreak, to keep the terminal output tidy.
82  // Wikitext will generally not have a trailing newline.
83  $this->output( "\n" );
84  }
85  }
86 }
87 
88 $maintClass = GetTextMaint::class;
89 require_once RUN_MAINTENANCE_IF_MAIN;
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:383
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
$maintClass
$maintClass
Definition: getText.php:88
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
GetTextMaint\__construct
__construct()
Default constructor.
Definition: getText.php:38
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
$title
$title
Definition: testCompression.php:38
GetTextMaint
Maintenance script that outputs page text to stdout.
Definition: getText.php:37
GetTextMaint\execute
execute()
Do the actual work.
Definition: getText.php:46
$content
$content
Definition: router.php:76
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:300
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:353
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40