MediaWiki master
getText.php
Go to the documentation of this file.
1<?php
29
30require_once __DIR__ . '/Maintenance.php';
31
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 = $this->getServiceContainer()
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;
89require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script that outputs page text to stdout.
Definition getText.php:37
execute()
Do the actual work.
Definition getText.php:46
__construct()
Default constructor.
Definition getText.php:38
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Page revision base class.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:78
$maintClass
Definition getText.php:88