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  }
44 
45  public function execute() {
46  $titleText = $this->getArg( 0 );
47  $title = Title::newFromText( $titleText );
48  if ( !$title ) {
49  $this->fatalError( "$titleText is not a valid title.\n" );
50  }
51 
52  $rev = MediaWikiServices::getInstance()
53  ->getRevisionLookup()
54  ->getRevisionByTitle( $title );
55  if ( !$rev ) {
56  $titleText = $title->getPrefixedText();
57  $this->fatalError( "Page $titleText does not exist.\n" );
58  }
59 
60  $audience = $this->hasOption( 'show-private' ) ?
61  RevisionRecord::RAW :
62  RevisionRecord::FOR_PUBLIC;
63  $content = $rev->getContent( SlotRecord::MAIN, $audience );
64 
65  if ( $content === false ) {
66  $titleText = $title->getPrefixedText();
67  $this->fatalError( "Couldn't extract the text from $titleText.\n" );
68  }
69  $this->output( $content->serialize() );
70  }
71 }
72 
73 $maintClass = GetTextMaint::class;
74 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
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:329
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
$maintClass
$maintClass
Definition: getText.php:73
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:480
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:53
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:241
$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:45
$content
$content
Definition: router.php:76
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:294
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:262
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:347
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39