MediaWiki  master
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 ) {
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 
144 require_once RUN_MAINTENANCE_IF_MAIN;
getArg( $argId=0, $default=null)
Get an argument.
execute()
Definition: parse.php:78
error( $err, $die=0)
Throw an error to the user.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Maintenance script to parse some wikitext.
Definition: parse.php:62
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
parse( $wikitext)
Definition: parse.php:129
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
getTitle()
Title object to use for CLI parsing.
Definition: parse.php:119
wfIsWindows()
Check if the operating system is Windows.
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
addDescription( $text)
Set the description text.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1982
addArg( $arg, $description, $required=true)
Add some args that are needed.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
initParser()
Definition: parse.php:108
$maintClass
Definition: parse.php:143
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition: parse.php:95
__construct()
Definition: parse.php:65
render( $wikitext)
Definition: parse.php:87
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319