Use TOCData methods to process new headings
[mediawiki.git] / maintenance / parse.php
blob24fce33ddba1d45ca178fc6349d72c35d043a9bd
1 <?php
3 use MediaWiki\MediaWikiServices;
5 /**
6 * Parse some wikitext.
8 * Wikitext can be given by stdin or using a file. The wikitext will be parsed
9 * using 'CLIParser' as a title. This can be overridden with --title option.
11 * Example1:
12 * @code
13 * $ php parse.php --title foo
14 * ''[[foo]]''^D
15 * <p><i><strong class="selflink">foo</strong></i>
16 * </p>
17 * @endcode
19 * Example2:
20 * @code
21 * $ echo "'''bold'''" > /tmp/foo.txt
22 * $ php parse.php /tmp/foo.txt
23 * <p><b>bold</b>
24 * </p>$
25 * @endcode
27 * Example3:
28 * @code
29 * $ cat /tmp/foo | php parse.php
30 * <p><b>bold</b>
31 * </p>$
32 * @endcode
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
47 * http://www.gnu.org/copyleft/gpl.html
49 * @file
50 * @ingroup Maintenance
51 * @author Antoine Musso <hashar at free dot fr>
52 * @license GPL-2.0-or-later
55 require_once __DIR__ . '/Maintenance.php';
57 /**
58 * Maintenance script to parse some wikitext.
60 * @ingroup Maintenance
62 class CLIParser extends Maintenance {
63 protected $parser;
65 public function __construct() {
66 parent::__construct();
67 $this->addDescription( 'Parse a given wikitext' );
68 $this->addOption(
69 'title',
70 'Title name for the given wikitext (Default: \'CLIParser\')',
71 false,
72 true
74 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
77 public function execute() {
78 $this->initParser();
79 print $this->render( $this->Wikitext() );
82 /**
83 * @param string $wikitext Wikitext to get rendered
84 * @return string HTML Rendering
86 public function render( $wikitext ) {
87 return $this->parse( $wikitext )->getText( [ 'wrapperDivClass' => '' ] );
90 /**
91 * Get wikitext from a the file passed as argument or STDIN
92 * @return string Wikitext
94 protected function Wikitext() {
95 $php_stdin = 'php://stdin';
96 $input_file = $this->getArg( 0, $php_stdin );
98 if ( $input_file === $php_stdin && !$this->mQuiet ) {
99 $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
100 $this->error( basename( __FILE__ )
101 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
104 return file_get_contents( $input_file );
107 protected function initParser() {
108 $this->parser = MediaWikiServices::getInstance()->getParserFactory()->create();
112 * Title object to use for CLI parsing.
113 * Default title is 'CLIParser', it can be overridden with the option
114 * --title <Your:Title>
116 * @return Title
118 protected function getTitle() {
119 $title = $this->getOption( 'title' ) ?: 'CLIParser';
121 return Title::newFromText( $title );
125 * @param string $wikitext Wikitext to parse
126 * @return ParserOutput
128 protected function parse( $wikitext ) {
129 $options = ParserOptions::newFromAnon();
130 $options->setOption( 'enableLimitReport', false );
131 return $this->parser->parse(
132 $wikitext,
133 $this->getTitle(),
134 $options
139 $maintClass = CLIParser::class;
140 require_once RUN_MAINTENANCE_IF_MAIN;