MediaWiki  REL1_20
parse.php
Go to the documentation of this file.
00001 <?php
00052 require_once( __DIR__ . '/Maintenance.php' );
00053 
00059 class CLIParser extends Maintenance {
00060         protected $parser;
00061 
00062         public function __construct() {
00063                 parent::__construct();
00064                 $this->mDescription = "Parse a given wikitext";
00065                 $this->addOption( 'title', 'Title name for the given wikitext (Default: \'CLIParser\')', false, true );
00066                 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
00067         }
00068 
00069         public function execute() {
00070                 $this->initParser();
00071                 print $this->render( $this->WikiText() );
00072         }
00073 
00078         public function render( $wikitext ) {
00079                 return $this->parse( $wikitext )->getText();
00080         }
00081 
00086         protected function Wikitext() {
00087 
00088                 $php_stdin  = 'php://stdin';
00089                 $input_file = $this->getArg( 0, $php_stdin );
00090 
00091                 if( $input_file === $php_stdin ) {
00092                         $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
00093                         $this->error( basename(__FILE__) .": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
00094                 }
00095 
00096                 return file_get_contents( $input_file );
00097         }
00098 
00099         protected function initParser() {
00100                 global $wgParserConf;
00101                 $parserClass = $wgParserConf['class'];
00102                 $this->parser = new $parserClass();
00103         }
00104 
00112         protected function getTitle( ) {
00113                 $title =
00114                         $this->getOption( 'title' )
00115                         ? $this->getOption( 'title' )
00116                         : 'CLIParser' ;
00117                 return Title::newFromText( $title );
00118         }
00119 
00124         protected function parse( $wikitext ) {
00125                 return $this->parser->parse(
00126                         $wikitext
00127                         , $this->getTitle()
00128                         , new ParserOptions()
00129                 );
00130         }
00131 }
00132 
00133 $maintClass = "CLIParser";
00134 require_once( RUN_MAINTENANCE_IF_MAIN );