MediaWiki  1.34.0
parse.php
Go to the documentation of this file.
1 <?php
2 
4 
55 require_once __DIR__ . '/Maintenance.php';
56 
62 class 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->addOption( 'no-tidy', 'Don\'t tidy the output (deprecated)' );
75  $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
76  }
77 
78  public function execute() {
79  $this->initParser();
80  print $this->render( $this->Wikitext() );
81  }
82 
87  public function render( $wikitext ) {
88  return $this->parse( $wikitext )->getText( [ 'wrapperDivClass' => '' ] );
89  }
90 
95  protected function Wikitext() {
96  $php_stdin = 'php://stdin';
97  $input_file = $this->getArg( 0, $php_stdin );
98 
99  if ( $input_file === $php_stdin && !$this->mQuiet ) {
100  $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
101  $this->error( basename( __FILE__ )
102  . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
103  }
104 
105  return file_get_contents( $input_file );
106  }
107 
108  protected function initParser() {
109  $this->parser = MediaWikiServices::getInstance()->getParserFactory()->create();
110  }
111 
119  protected function getTitle() {
120  $title = $this->getOption( 'title' ) ?: 'CLIParser';
121 
122  return Title::newFromText( $title );
123  }
124 
129  protected function parse( $wikitext ) {
130  $options = ParserOptions::newCanonical();
131  $options->setOption( 'enableLimitReport', false );
132  if ( $this->getOption( 'no-tidy' ) ) {
133  $options->setTidy( false );
134  }
135  return $this->parser->parse(
136  $wikitext,
137  $this->getTitle(),
138  $options
139  );
140  }
141 }
142 
143 $maintClass = CLIParser::class;
144 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
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:316
CLIParser\getTitle
getTitle()
Title object to use for CLI parsing.
Definition: parse.php:119
CLIParser\execute
execute()
Do the actual work.
Definition: parse.php:78
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
CLIParser
Maintenance script to parse some wikitext.
Definition: parse.php:62
CLIParser\$parser
$parser
Definition: parse.php:63
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$maintClass
$maintClass
Definition: parse.php:143
$title
$title
Definition: testCompression.php:34
CLIParser\__construct
__construct()
Default constructor.
Definition: parse.php:65
wfIsWindows
wfIsWindows()
Check if the operating system is Windows.
Definition: GlobalFunctions.php:1907
ParserOptions\newCanonical
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1073
CLIParser\parse
parse( $wikitext)
Definition: parse.php:129
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
CLIParser\render
render( $wikitext)
Definition: parse.php:87
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:319
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:481
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:371
CLIParser\Wikitext
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition: parse.php:95
CLIParser\initParser
initParser()
Definition: parse.php:108