MediaWiki  REL1_24
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(
00066             'title',
00067             'Title name for the given wikitext (Default: \'CLIParser\')',
00068             false,
00069             true
00070         );
00071         $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
00072     }
00073 
00074     public function execute() {
00075         $this->initParser();
00076         print $this->render( $this->WikiText() );
00077     }
00078 
00083     public function render( $wikitext ) {
00084         return $this->parse( $wikitext )->getText();
00085     }
00086 
00091     protected function Wikitext() {
00092 
00093         $php_stdin = 'php://stdin';
00094         $input_file = $this->getArg( 0, $php_stdin );
00095 
00096         if ( $input_file === $php_stdin ) {
00097             $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
00098             $this->error( basename( __FILE__ )
00099                 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
00100         }
00101 
00102         return file_get_contents( $input_file );
00103     }
00104 
00105     protected function initParser() {
00106         global $wgParserConf;
00107         $parserClass = $wgParserConf['class'];
00108         $this->parser = new $parserClass();
00109     }
00110 
00118     protected function getTitle() {
00119         $title = $this->getOption( 'title' )
00120             ? $this->getOption( 'title' )
00121             : 'CLIParser';
00122 
00123         return Title::newFromText( $title );
00124     }
00125 
00130     protected function parse( $wikitext ) {
00131         return $this->parser->parse(
00132             $wikitext,
00133             $this->getTitle(),
00134             new ParserOptions()
00135         );
00136     }
00137 }
00138 
00139 $maintClass = "CLIParser";
00140 require_once RUN_MAINTENANCE_IF_MAIN;