MediaWiki 1.43.1
parse.php
Go to the documentation of this file.
1<?php
2
7
58// @codeCoverageIgnoreStart
59require_once __DIR__ . '/Maintenance.php';
60// @codeCoverageIgnoreEnd
61
67class CLIParser extends Maintenance {
69 protected $parser;
70
71 public function __construct() {
72 parent::__construct();
73 $this->addDescription( 'Parse a given wikitext' );
74 $this->addOption(
75 'title',
76 'Title name for the given wikitext (Default: \'CLIParser\')',
77 false,
78 true
79 );
80 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
81 $this->addOption( 'parsoid', 'Whether to use Parsoid', false, false, 'p' );
82 }
83
84 public function execute() {
85 $this->initParser();
86 print $this->render( $this->Wikitext() );
87 }
88
93 public function render( $wikitext ) {
94 $options = ParserOptions::newFromAnon();
95 $options->setOption( 'enableLimitReport', false );
96 $po = $this->parser->parse(
97 $wikitext,
98 $this->getTitle(),
99 $options
100 );
101 // TODO T371008 consider if using the Content framework makes sense instead of creating the pipeline
102 $pipeline = $this->getServiceContainer()->getDefaultOutputPipeline();
103 return $pipeline->run( $po, $options, [ 'wrapperDivClass' => '' ] )->getContentHolderText();
104 }
105
110 protected function Wikitext() {
111 $php_stdin = 'php://stdin';
112 $input_file = $this->getArg( 0, $php_stdin );
113
114 if ( $input_file === $php_stdin && !$this->mQuiet ) {
115 $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
116 $this->error( basename( __FILE__ )
117 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
118 }
119
120 return file_get_contents( $input_file );
121 }
122
123 protected function initParser() {
124 $services = $this->getServiceContainer();
125 if ( $this->hasOption( 'parsoid' ) ) {
126 $this->parser = $services->getParsoidParserFactory()->create();
127 } else {
128 $this->parser = $services->getParserFactory()->create();
129 }
130 }
131
139 protected function getTitle() {
140 $title = $this->getOption( 'title' ) ?: 'CLIParser';
141
142 return Title::newFromText( $title );
143 }
144}
145
146// @codeCoverageIgnoreStart
147$maintClass = CLIParser::class;
148require_once RUN_MAINTENANCE_IF_MAIN;
149// @codeCoverageIgnoreEnd
wfIsWindows()
Check if the operating system is Windows.
Maintenance script to parse some wikitext.
Definition parse.php:67
execute()
Definition parse.php:84
getTitle()
Title object to use for CLI parsing.
Definition parse.php:139
__construct()
Definition parse.php:71
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition parse.php:110
Parser ParsoidParser $parser
Definition parse.php:69
initParser()
Definition parse.php:123
render( $wikitext)
Definition parse.php:93
Set options of the Parser.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:143
Parser implementation which uses Parsoid.
Represents a title within MediaWiki.
Definition Title.php:78
$maintClass
Definition parse.php:147