MediaWiki master
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 $this->addOption( 'parsoid', 'Whether to use Parsoid', false, false, 'p' );
77 }
78
79 public function execute() {
80 $this->initParser();
81 print $this->render( $this->Wikitext() );
82 }
83
88 public function render( $wikitext ) {
89 return $this->parse( $wikitext )->getText( [ 'wrapperDivClass' => '' ] );
90 }
91
96 protected function Wikitext() {
97 $php_stdin = 'php://stdin';
98 $input_file = $this->getArg( 0, $php_stdin );
99
100 if ( $input_file === $php_stdin && !$this->mQuiet ) {
101 $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
102 $this->error( basename( __FILE__ )
103 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
104 }
105
106 return file_get_contents( $input_file );
107 }
108
109 protected function initParser() {
110 $services = $this->getServiceContainer();
111 if ( $this->hasOption( 'parsoid' ) ) {
112 $this->parser = $services->getParsoidParserFactory()->create();
113 } else {
114 $this->parser = $services->getParserFactory()->create();
115 }
116 }
117
125 protected function getTitle() {
126 $title = $this->getOption( 'title' ) ?: 'CLIParser';
127
128 return Title::newFromText( $title );
129 }
130
135 protected function parse( $wikitext ) {
136 $options = ParserOptions::newFromAnon();
137 $options->setOption( 'enableLimitReport', false );
138 return $this->parser->parse(
139 $wikitext,
140 $this->getTitle(),
141 $options
142 );
143 }
144}
145
146$maintClass = CLIParser::class;
147require_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:79
getTitle()
Title object to use for CLI parsing.
Definition parse.php:125
parse( $wikitext)
Definition parse.php:135
__construct()
Default constructor.
Definition parse.php:66
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition parse.php:96
initParser()
Definition parse.php:109
render( $wikitext)
Definition parse.php:88
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.
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.
ParserOutput is a rendering of a Content object or a message.
Represents a title within MediaWiki.
Definition Title.php:78
$maintClass
Definition parse.php:146