MediaWiki REL1_35
parse.php
Go to the documentation of this file.
1<?php
2
4
55require_once __DIR__ . '/Maintenance.php';
56
62class CLIParser extends Maintenance {
63 protected $parser;
64
65 public function __construct() {
66 parent::__construct();
67 $this->addDescription( 'Parse a given wikitext' );
68 $this->addOption(
69 'title',
70 'Title name for the given wikitext (Default: \'CLIParser\')',
71 false,
72 true
73 );
74 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
75 }
76
77 public function execute() {
78 $this->initParser();
79 print $this->render( $this->Wikitext() );
80 }
81
86 public function render( $wikitext ) {
87 return $this->parse( $wikitext )->getText( [ 'wrapperDivClass' => '' ] );
88 }
89
94 protected function Wikitext() {
95 $php_stdin = 'php://stdin';
96 $input_file = $this->getArg( 0, $php_stdin );
97
98 if ( $input_file === $php_stdin && !$this->mQuiet ) {
99 $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
100 $this->error( basename( __FILE__ )
101 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
102 }
103
104 return file_get_contents( $input_file );
105 }
106
107 protected function initParser() {
108 $this->parser = MediaWikiServices::getInstance()->getParserFactory()->create();
109 }
110
118 protected function getTitle() {
119 $title = $this->getOption( 'title' ) ?: 'CLIParser';
120
121 return Title::newFromText( $title );
122 }
123
128 protected function parse( $wikitext ) {
129 $options = ParserOptions::newCanonical( 'canonical' );
130 $options->setOption( 'enableLimitReport', false );
131 return $this->parser->parse(
132 $wikitext,
133 $this->getTitle(),
134 $options
135 );
136 }
137}
138
139$maintClass = CLIParser::class;
140require_once RUN_MAINTENANCE_IF_MAIN;
wfIsWindows()
Check if the operating system is Windows.
const RUN_MAINTENANCE_IF_MAIN
Maintenance script to parse some wikitext.
Definition parse.php:62
execute()
Do the actual work.
Definition parse.php:77
getTitle()
Title object to use for CLI parsing.
Definition parse.php:118
parse( $wikitext)
Definition parse.php:128
__construct()
Default constructor.
Definition parse.php:65
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition parse.php:94
initParser()
Definition parse.php:107
render( $wikitext)
Definition parse.php:86
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
MediaWikiServices is the service locator for the application scope of MediaWiki.
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
$maintClass
Definition parse.php:139