MediaWiki 1.40.4
parse.php
Go to the documentation of this file.
1<?php
2
5
56require_once __DIR__ . '/Maintenance.php';
57
63class CLIParser extends Maintenance {
64 protected $parser;
65
66 public function __construct() {
67 parent::__construct();
68 $this->addDescription( 'Parse a given wikitext' );
69 $this->addOption(
70 'title',
71 'Title name for the given wikitext (Default: \'CLIParser\')',
72 false,
73 true
74 );
75 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
76 }
77
78 public function execute() {
79 $this->initParser();
80 print $this->render( $this->Wikitext() );
81 }
82
87 public function render( $wikitext ) {
88 return $this->parse( $wikitext )->getText( [ 'wrapperDivClass' => '' ] );
89 }
90
95 protected function Wikitext() {
96 $php_stdin = 'php://stdin';
97 $input_file = $this->getArg( 0, $php_stdin );
98
99 if ( $input_file === $php_stdin && !$this->mQuiet ) {
100 $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
101 $this->error( basename( __FILE__ )
102 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
103 }
104
105 return file_get_contents( $input_file );
106 }
107
108 protected function initParser() {
109 $this->parser = MediaWikiServices::getInstance()->getParserFactory()->create();
110 }
111
119 protected function getTitle() {
120 $title = $this->getOption( 'title' ) ?: 'CLIParser';
121
122 return Title::newFromText( $title );
123 }
124
129 protected function parse( $wikitext ) {
130 $options = ParserOptions::newFromAnon();
131 $options->setOption( 'enableLimitReport', false );
132 return $this->parser->parse(
133 $wikitext,
134 $this->getTitle(),
135 $options
136 );
137 }
138}
139
140$maintClass = CLIParser::class;
141require_once RUN_MAINTENANCE_IF_MAIN;
wfIsWindows()
Check if the operating system is Windows.
Maintenance script to parse some wikitext.
Definition parse.php:63
execute()
Do the actual work.
Definition parse.php:78
getTitle()
Title object to use for CLI parsing.
Definition parse.php:119
parse( $wikitext)
Definition parse.php:129
__construct()
Default constructor.
Definition parse.php:66
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition parse.php:95
initParser()
Definition parse.php:108
render( $wikitext)
Definition parse.php:87
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
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.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
$maintClass
Definition parse.php:140