3 * CLI script to easily parse some wikitext.
4 * Wikitext can be given by stdin or using a file. The wikitext will be parsed
5 * using 'CLIParser' as a title. This can be overriden with --title option.
9 * $ php parse.php --title foo
11 * <p><i><strong class="selflink">foo</strong></i>
17 * $ echo "'''bold'''" > /tmp/foo.txt
18 * $ php parse.php /tmp/foo.txt
25 * $ cat /tmp/foo | php parse.php
30 * @ingroup Maintenance
31 * @author Antoine Musso <hashar at free dot fr>
32 * @license GNU General Public License 2.0 or later
34 require_once( dirname(__FILE__
) . '/Maintenance.php' );
36 class CLIParser
extends Maintenance
{
39 public function __construct() {
40 parent
::__construct();
41 $this->mDescription
= "Parse a given wikitext";
42 $this->addOption( 'title', 'Title name for the given wikitext (Default: \'CLIParser\')', false, true );
43 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
46 public function execute() {
48 print $this->render( $this->WikiText() );
52 * @param string $wikitext Wikitext to get rendered
53 * @return string HTML Rendering
55 public function render( $wikitext ) {
56 return $this->parse( $wikitext )->getText();
60 * Get wikitext from a the file passed as argument or STDIN
61 * @return string Wikitext
63 protected function Wikitext() {
65 $php_stdin = 'php://stdin';
66 $input_file = $this->getArg( 0, $php_stdin );
68 if( $input_file === $php_stdin ) {
69 $ctrl = wfIsWindows() ?
'CTRL+Z' : 'CTRL+D';
70 $this->error( basename(__FILE__
) .": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
73 return file_get_contents( $input_file );
76 protected function initParser() {
78 $parserClass = $wgParserConf['class'];
79 $this->parser
= new $parserClass();
83 * Title object to use for CLI parsing.
84 * Default title is 'CLIParser', it can be overriden with the option
85 * --title <Your:Title>
87 * @return Title object
89 protected function getTitle( ) {
91 $this->getOption( 'title' )
92 ?
$this->getOption( 'title' )
94 return Title
::newFromText( $title );
98 * @param string $wikitext Wikitext to parse
99 * @return ParserOutput
101 protected function parse( $wikitext ) {
102 return $this->parser
->parse(
105 , new ParserOptions()
110 $maintClass = "CLIParser";
111 require_once( RUN_MAINTENANCE_IF_MAIN
);