MediaWiki master
getText.php
Go to the documentation of this file.
1<?php
16
17// @codeCoverageIgnoreStart
18require_once __DIR__ . '/Maintenance.php';
19// @codeCoverageIgnoreEnd
20
27 public function __construct() {
28 parent::__construct();
29 $this->addDescription( 'Outputs page text to stdout' );
30 $this->addOption( 'show-private', 'Show the text even if it\'s not available to the public' );
31 $this->addArg( 'title', 'Page title' );
32 $this->addOption( 'revision', 'Revision ID', false, true );
33 }
34
35 public function execute() {
36 $titleText = $this->getArg( 0 );
37 $title = Title::newFromText( $titleText );
38 if ( !$title ) {
39 $this->fatalError( "$titleText is not a valid title.\n" );
40 }
41
42 if ( !$title->exists() ) {
43 $titleText = $title->getPrefixedText();
44 $this->fatalError( "Page $titleText does not exist.\n" );
45 }
46
47 $revId = (int)$this->getOption( 'revision', $title->getLatestRevID() );
48
49 $rev = $this->getServiceContainer()
50 ->getRevisionLookup()
51 ->getRevisionByTitle( $title, $revId );
52
53 if ( !$rev ) {
54 $titleText = $title->getPrefixedText();
55 $this->fatalError( "Could not load revision $revId of $titleText.\n" );
56 }
57
58 $audience = $this->hasOption( 'show-private' ) ?
59 RevisionRecord::RAW :
60 RevisionRecord::FOR_PUBLIC;
61 $content = $rev->getContent( SlotRecord::MAIN, $audience );
62
63 if ( $content === null ) {
64 $titleText = $title->getPrefixedText();
65 $this->fatalError( "Couldn't extract the text from $titleText.\n" );
66 }
67 $this->output( $content->serialize() );
68
69 if ( stream_isatty( STDOUT ) ) {
70 // When writing to a TTY, add a linebreak, to keep the terminal output tidy.
71 // Wikitext will generally not have a trailing newline.
72 $this->output( "\n" );
73 }
74 }
75}
76
77// @codeCoverageIgnoreStart
78$maintClass = GetTextMaint::class;
79require_once RUN_MAINTENANCE_IF_MAIN;
80// @codeCoverageIgnoreEnd
Maintenance script that outputs page text to stdout.
Definition getText.php:26
execute()
Do the actual work.
Definition getText.php:35
__construct()
Default constructor.
Definition getText.php:27
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:69
$maintClass
Definition getText.php:78