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