MediaWiki  1.23.8
parse.php
Go to the documentation of this file.
1 <?php
52 require_once __DIR__ . '/Maintenance.php';
53 
59 class CLIParser extends Maintenance {
60  protected $parser;
61 
62  public function __construct() {
63  parent::__construct();
64  $this->mDescription = "Parse a given wikitext";
65  $this->addOption( 'title', 'Title name for the given wikitext (Default: \'CLIParser\')', false, true );
66  $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
67  }
68 
69  public function execute() {
70  $this->initParser();
71  print $this->render( $this->WikiText() );
72  }
73 
78  public function render( $wikitext ) {
79  return $this->parse( $wikitext )->getText();
80  }
81 
86  protected function Wikitext() {
87 
88  $php_stdin = 'php://stdin';
89  $input_file = $this->getArg( 0, $php_stdin );
90 
91  if ( $input_file === $php_stdin ) {
92  $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
93  $this->error( basename( __FILE__ ) . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
94  }
95 
96  return file_get_contents( $input_file );
97  }
98 
99  protected function initParser() {
100  global $wgParserConf;
101  $parserClass = $wgParserConf['class'];
102  $this->parser = new $parserClass();
103  }
104 
112  protected function getTitle() {
113  $title =
114  $this->getOption( 'title' )
115  ? $this->getOption( 'title' )
116  : 'CLIParser';
117  return Title::newFromText( $title );
118  }
119 
124  protected function parse( $wikitext ) {
125  return $this->parser->parse(
126  $wikitext,
127  $this->getTitle(),
128  new ParserOptions()
129  );
130  }
131 }
132 
133 $maintClass = "CLIParser";
134 require_once RUN_MAINTENANCE_IF_MAIN;
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:31
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:189
CLIParser\getTitle
getTitle()
Title object to use for CLI parsing.
Definition: parse.php:112
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
CLIParser\execute
execute()
Do the actual work.
Definition: parse.php:69
CLIParser
Maintenance script to parse some wikitext.
Definition: parse.php:59
CLIParser\$parser
$parser
Definition: parse.php:60
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$maintClass
$maintClass
Definition: parse.php:133
CLIParser\__construct
__construct()
Default constructor.
Definition: parse.php:62
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
wfIsWindows
wfIsWindows()
Check if the operating system is Windows.
Definition: GlobalFunctions.php:2524
CLIParser\parse
parse( $wikitext)
Definition: parse.php:124
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
CLIParser\render
render( $wikitext)
Definition: parse.php:78
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:207
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246
CLIParser\Wikitext
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition: parse.php:86
CLIParser\initParser
initParser()
Definition: parse.php:99