MediaWiki master
parse.php
Go to the documentation of this file.
1<?php
2
8use Wikimedia\Parsoid\Utils\ContentUtils;
9
46// @codeCoverageIgnoreStart
47require_once __DIR__ . '/Maintenance.php';
48// @codeCoverageIgnoreEnd
49
55class CLIParser extends Maintenance {
57 protected $parser;
58
59 public function __construct() {
60 parent::__construct();
61 $this->addDescription( 'Parse a given wikitext' );
62 $this->addOption(
63 'title',
64 'Title name for the given wikitext (Default: \'CLIParser\')',
65 false,
66 true
67 );
68 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
69 $this->addOption( 'parsoid', 'Whether to use Parsoid', false, false, 'p' );
70 $this->addOption( 'show-rich-attributes', 'Show rich attributes', false );
71 }
72
73 public function execute() {
74 $this->initParser();
75 print $this->render( $this->Wikitext() );
76 }
77
82 public function render( $wikitext ) {
83 $options = ParserOptions::newFromAnon();
84 $options->setOption( 'enableLimitReport', false );
85 $po = $this->parser->parse(
86 $wikitext,
87 $this->getTitle(),
88 $options
89 );
90 // TODO T371008 consider if using the Content framework makes sense instead of creating the pipeline
91 $pipeline = $this->getServiceContainer()->getDefaultOutputPipeline();
92 $po = $pipeline->run( $po, $options, [ 'wrapperDivClass' => '' ] );
93 if ( $this->getOption( 'show-rich-attributes' ) ) {
94 $df = $po->getContentHolder()->getAsDom();
95 $df ??= $po->getContentHolder()->createFragment();
96 return ContentUtils::dumpDOM( $df, '', [ 'quiet' => true ] );
97 }
98 return $po->getContentHolderText();
99 }
100
105 protected function Wikitext() {
106 $php_stdin = 'php://stdin';
107 $input_file = $this->getArg( 0, $php_stdin );
108
109 if ( $input_file === $php_stdin && !$this->mQuiet ) {
110 $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
111 $this->error( basename( __FILE__ )
112 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
113 }
114
115 return file_get_contents( $input_file );
116 }
117
118 protected function initParser() {
119 $services = $this->getServiceContainer();
120 if ( $this->hasOption( 'parsoid' ) ) {
121 $this->parser = $services->getParsoidParserFactory()->create();
122 } else {
123 $this->parser = $services->getParserFactory()->create();
124 }
125 }
126
134 protected function getTitle() {
135 $title = $this->getOption( 'title' ) ?: 'CLIParser';
136
137 return Title::newFromText( $title );
138 }
139}
140
141// @codeCoverageIgnoreStart
142$maintClass = CLIParser::class;
143require_once RUN_MAINTENANCE_IF_MAIN;
144// @codeCoverageIgnoreEnd
wfIsWindows()
Check if the operating system is Windows.
Maintenance script to parse some wikitext.
Definition parse.php:55
execute()
Do the actual work.
Definition parse.php:73
getTitle()
Title object to use for CLI parsing.
Definition parse.php:134
__construct()
Default constructor.
Definition parse.php:59
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition parse.php:105
Parser ParsoidParser $parser
Definition parse.php:57
initParser()
Definition parse.php:118
render( $wikitext)
Definition parse.php:82
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:135
Parser implementation which uses Parsoid.
Represents a title within MediaWiki.
Definition Title.php:69
$maintClass
Definition parse.php:142