Revert r106521: creates lots of long, unwrappable lines in help output
[mediawiki.git] / maintenance / parse.php
blob82e6510b26d70c9740778374f4d9a6dc1e0803df
1 <?php
2 /**
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.
7 * Example1:
8 * @code
9 * $ php parse.php --title foo
10 * ''[[foo]]''^D
11 * <p><i><strong class="selflink">foo</strong></i>
12 * </p>
13 * @endcode
15 * Example2:
16 * @code
17 * $ echo "'''bold'''" > /tmp/foo
18 * $ php parse.php --file /tmp/foo
19 * <p><b>bold</b>
20 * </p>$
21 * @endcode
23 * Example3:
24 * @code
25 * $ cat /tmp/foo | php parse.php
26 * <p><b>bold</b>
27 * </p>$
28 * @endcode
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 {
37 protected $parser;
39 public function __construct() {
40 $this->mDescription = "Parse a given wikitext";
41 $this->addOption( 'title', 'Title name for the given wikitext (Default: \'CLIParser\')', false, true );
42 $this->addOption( 'file', 'File containing wikitext (Default: stdin)', false, true );
43 parent::__construct();
46 public function execute() {
47 $this->initParser();
48 print $this->render( $this->WikiText() );
51 /**
52 * @param string $wikitext Wikitext to get rendered
53 * @return string HTML Rendering
55 public function render( $wikitext ) {
56 return $this->parse( $wikitext )->getText();
59 /**
60 * Get wikitext from --file or from STDIN
61 * @return string Wikitext
63 protected function Wikitext() {
64 return file_get_contents(
65 $this->getOption( 'file', 'php://stdin' )
69 protected function initParser() {
70 global $wgParserConf;
71 $parserClass = $wgParserConf['class'];
72 $this->parser = new $parserClass();
75 /**
76 * Title object to use for CLI parsing.
77 * Default title is 'CLIParser', it can be overriden with the option
78 * --title <Your:Title>
80 * @return Title object
82 protected function getTitle( ) {
83 $title =
84 $this->getOption( 'title' )
85 ? $this->getOption( 'title' )
86 : 'CLIParser' ;
87 return Title::newFromText( $title );
90 /**
91 * @param string $text Wikitext to parse
92 * @return ParserOutput
94 protected function parse( $wikitext ) {
95 return $this->parser->parse(
96 $wikitext
97 , $this->getTitle()
98 , new ParserOptions()
103 $maintClass = "CLIParser";
104 require_once( RUN_MAINTENANCE_IF_MAIN );