MediaWiki  1.29.1
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->addDescription( 'Parse a given wikitext' );
65  $this->addOption(
66  'title',
67  'Title name for the given wikitext (Default: \'CLIParser\')',
68  false,
69  true
70  );
71  $this->addOption( 'tidy', 'Tidy the output' );
72  $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
73  }
74 
75  public function execute() {
76  $this->initParser();
77  print $this->render( $this->Wikitext() );
78  }
79 
84  public function render( $wikitext ) {
85  return $this->parse( $wikitext )->getText();
86  }
87 
92  protected function Wikitext() {
93  $php_stdin = 'php://stdin';
94  $input_file = $this->getArg( 0, $php_stdin );
95 
96  if ( $input_file === $php_stdin && !$this->mQuiet ) {
97  $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
98  $this->error( basename( __FILE__ )
99  . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
100  }
101 
102  return file_get_contents( $input_file );
103  }
104 
105  protected function initParser() {
106  global $wgParserConf;
107  $parserClass = $wgParserConf['class'];
108  $this->parser = new $parserClass();
109  }
110 
118  protected function getTitle() {
119  $title = $this->getOption( 'title' )
120  ? $this->getOption( 'title' )
121  : 'CLIParser';
122 
123  return Title::newFromText( $title );
124  }
125 
130  protected function parse( $wikitext ) {
131  $options = new ParserOptions;
132  if ( $this->getOption( 'tidy' ) ) {
133  $options->setTidy( true );
134  }
135  return $this->parser->parse(
136  $wikitext,
137  $this->getTitle(),
138  $options
139  );
140  }
141 }
142 
143 $maintClass = "CLIParser";
144 require_once RUN_MAINTENANCE_IF_MAIN;
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:33
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:265
CLIParser\getTitle
getTitle()
Title object to use for CLI parsing.
Definition: parse.php:118
CLIParser\execute
execute()
Do the actual work.
Definition: parse.php:75
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
CLIParser
Maintenance script to parse some wikitext.
Definition: parse.php:59
CLIParser\$parser
$parser
Definition: parse.php:60
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
php
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
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
$maintClass
$maintClass
Definition: parse.php:143
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
wfIsWindows
wfIsWindows()
Check if the operating system is Windows.
Definition: GlobalFunctions.php:2033
CLIParser\parse
parse( $wikitext)
Definition: parse.php:130
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
CLIParser\render
render( $wikitext)
Definition: parse.php:84
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:267
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
captcha-old.parser
parser
Definition: captcha-old.py:187
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:306
CLIParser\Wikitext
Wikitext()
Get wikitext from a the file passed as argument or STDIN.
Definition: parse.php:92
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1049
CLIParser\initParser
initParser()
Definition: parse.php:105