MediaWiki
master
Toggle main menu visibility
parse.php
Go to the documentation of this file.
1
<?php
2
3
use
MediaWiki\Maintenance\Maintenance
;
4
use MediaWiki\Parser\Parser;
5
use MediaWiki\Parser\ParserOptions;
6
use MediaWiki\Parser\Parsoid\ParsoidParser;
7
use MediaWiki\Title\Title;
8
use Wikimedia\Parsoid\Utils\ContentUtils;
9
45
46
// @codeCoverageIgnoreStart
47
require_once __DIR__ .
'/Maintenance.php'
;
48
// @codeCoverageIgnoreEnd
49
55
class
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;
144
require_once RUN_MAINTENANCE_IF_MAIN;
145
// @codeCoverageIgnoreEnd
wfIsWindows
wfIsWindows()
Check if the operating system is Windows.
Definition
BootstrapHelperFunctions.php:83
$maintClass
$maintClass
Definition
addChangeTag.php:56
CLIParser
Maintenance script to parse some wikitext.
Definition
parse.php:55
CLIParser\execute
execute()
Definition
parse.php:73
CLIParser\getTitle
getTitle()
Title object to use for CLI parsing.
Definition
parse.php:135
CLIParser\__construct
__construct()
Definition
parse.php:59
CLIParser\Wikitext
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition
parse.php:106
CLIParser\$parser
Parser ParsoidParser $parser
Definition
parse.php:57
CLIParser\initParser
initParser()
Definition
parse.php:119
CLIParser\render
render( $wikitext)
Definition
parse.php:82
MediaWiki\Parser\ParserOptions\newFromAnon
static newFromAnon()
Get a ParserOptions object for an anonymous user.
Definition
ParserOptions.php:1205
MediaWiki\Parser\Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition
Parser.php:139
MediaWiki\Parser\Parsoid\ParsoidParser
Parser implementation which uses Parsoid.
Definition
ParsoidParser.php:43
MediaWiki\Title\Title\newFromText
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
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
parse.php
Generated on
for MediaWiki by
1.18.0