MediaWiki master
parse.php
Go to the documentation of this file.
1<?php
2
4use MediaWiki\Parser\Parser;
5use MediaWiki\Parser\ParserOptions;
6use MediaWiki\Parser\Parsoid\ParsoidParser;
7use MediaWiki\Title\Title;
8use Wikimedia\Parsoid\Utils\ContentUtils;
9
45
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 $options->setUseParsoid( $this->hasOption( 'parsoid' ) );
86 $po = $this->parser->parse(
87 $wikitext,
88 $this->getTitle(),
89 $options
90 );
91 // TODO T371008 consider if using the Content framework makes sense instead of creating the pipeline
92 $pipeline = $this->getServiceContainer()->getDefaultOutputPipeline();
93 $po = $pipeline->run( $po, $options, [ 'wrapperDivClass' => '' ] );
94 if ( $this->getOption( 'show-rich-attributes' ) ) {
95 $df = $po->getContentHolder()->getAsDom();
96 $df ??= $po->getContentHolder()->createFragment();
97 return ContentUtils::dumpDOM( $df, '', [ 'quiet' => true ] );
98 }
99 return $po->getContentHolderText();
100 }
101
106 protected function Wikitext() {
107 $php_stdin = 'php://stdin';
108 $input_file = $this->getArg( 0, $php_stdin );
109
110 if ( $input_file === $php_stdin && !$this->mQuiet ) {
111 $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
112 $this->error( basename( __FILE__ )
113 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
114 }
115
116 return file_get_contents( $input_file );
117 }
118
119 protected function initParser() {
120 $services = $this->getServiceContainer();
121 if ( $this->hasOption( 'parsoid' ) ) {
122 $this->parser = $services->getParsoidParserFactory()->create();
123 } else {
124 $this->parser = $services->getParserFactory()->create();
125 }
126 }
127
135 protected function getTitle() {
136 $title = $this->getOption( 'title' ) ?: 'CLIParser';
137
138 return Title::newFromText( $title );
139 }
140}
141
142// @codeCoverageIgnoreStart
143$maintClass = CLIParser::class;
144require_once RUN_MAINTENANCE_IF_MAIN;
145// @codeCoverageIgnoreEnd
wfIsWindows()
Check if the operating system is Windows.
$maintClass
Maintenance script to parse some wikitext.
Definition parse.php:55
execute()
Definition parse.php:73
getTitle()
Title object to use for CLI parsing.
Definition parse.php:135
__construct()
Definition parse.php:59
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition parse.php:106
Parser ParsoidParser $parser
Definition parse.php:57
initParser()
Definition parse.php:119
render( $wikitext)
Definition parse.php:82
static newFromAnon()
Get a ParserOptions object for an anonymous user.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:139
Parser implementation which uses Parsoid.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition Title.php:388