MediaWiki master
getText.php
Go to the documentation of this file.
1<?php
30
31// @codeCoverageIgnoreStart
32require_once __DIR__ . '/Maintenance.php';
33// @codeCoverageIgnoreEnd
34
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'Outputs page text to stdout' );
44 $this->addOption( 'show-private', 'Show the text even if it\'s not available to the public' );
45 $this->addArg( 'title', 'Page title' );
46 $this->addOption( 'revision', 'Revision ID', false, true );
47 }
48
49 public function execute() {
50 $titleText = $this->getArg( 0 );
51 $title = Title::newFromText( $titleText );
52 if ( !$title ) {
53 $this->fatalError( "$titleText is not a valid title.\n" );
54 }
55
56 if ( !$title->exists() ) {
57 $titleText = $title->getPrefixedText();
58 $this->fatalError( "Page $titleText does not exist.\n" );
59 }
60
61 $revId = (int)$this->getOption( 'revision', $title->getLatestRevID() );
62
63 $rev = $this->getServiceContainer()
64 ->getRevisionLookup()
65 ->getRevisionByTitle( $title, $revId );
66
67 if ( !$rev ) {
68 $titleText = $title->getPrefixedText();
69 $this->fatalError( "Could not load revision $revId of $titleText.\n" );
70 }
71
72 $audience = $this->hasOption( 'show-private' ) ?
73 RevisionRecord::RAW :
74 RevisionRecord::FOR_PUBLIC;
75 $content = $rev->getContent( SlotRecord::MAIN, $audience );
76
77 if ( $content === null ) {
78 $titleText = $title->getPrefixedText();
79 $this->fatalError( "Couldn't extract the text from $titleText.\n" );
80 }
81 $this->output( $content->serialize() );
82
83 if ( stream_isatty( STDOUT ) ) {
84 // When writing to a TTY, add a linebreak, to keep the terminal output tidy.
85 // Wikitext will generally not have a trailing newline.
86 $this->output( "\n" );
87 }
88 }
89}
90
91// @codeCoverageIgnoreStart
92$maintClass = GetTextMaint::class;
93require_once RUN_MAINTENANCE_IF_MAIN;
94// @codeCoverageIgnoreEnd
Maintenance script that outputs page text to stdout.
Definition getText.php:40
execute()
Do the actual work.
Definition getText.php:49
__construct()
Default constructor.
Definition getText.php:41
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.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
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:92