3 use MediaWiki\MediaWikiServices
;
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.
13 * $ php parse.php --title foo
15 * <p><i><strong class="selflink">foo</strong></i>
21 * $ echo "'''bold'''" > /tmp/foo.txt
22 * $ php parse.php /tmp/foo.txt
29 * $ cat /tmp/foo | php parse.php
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
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';
58 * Maintenance script to parse some wikitext.
60 * @ingroup Maintenance
62 class CLIParser
extends Maintenance
{
65 public function __construct() {
66 parent
::__construct();
67 $this->addDescription( 'Parse a given wikitext' );
70 'Title name for the given wikitext (Default: \'CLIParser\')',
74 $this->addOption( 'no-tidy', 'Don\'t tidy the output (deprecated)' );
75 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
78 public function execute() {
80 print $this->render( $this->Wikitext() );
84 * @param string $wikitext Wikitext to get rendered
85 * @return string HTML Rendering
87 public function render( $wikitext ) {
88 return $this->parse( $wikitext )->getText( [ 'wrapperDivClass' => '' ] );
92 * Get wikitext from a the file passed as argument or STDIN
93 * @return string Wikitext
95 protected function Wikitext() {
96 $php_stdin = 'php://stdin';
97 $input_file = $this->getArg( 0, $php_stdin );
99 if ( $input_file === $php_stdin && !$this->mQuiet
) {
100 $ctrl = wfIsWindows() ?
'CTRL+Z' : 'CTRL+D';
101 $this->error( basename( __FILE__
)
102 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
105 return file_get_contents( $input_file );
108 protected function initParser() {
109 $this->parser
= MediaWikiServices
::getInstance()->getParserFactory()->create();
113 * Title object to use for CLI parsing.
114 * Default title is 'CLIParser', it can be overridden with the option
115 * --title <Your:Title>
119 protected function getTitle() {
120 $title = $this->getOption( 'title' )
121 ?
$this->getOption( 'title' )
124 return Title
::newFromText( $title );
128 * @param string $wikitext Wikitext to parse
129 * @return ParserOutput
131 protected function parse( $wikitext ) {
132 $options = ParserOptions
::newCanonical();
133 $options->setOption( 'enableLimitReport', false );
134 if ( $this->getOption( 'no-tidy' ) ) {
135 $options->setTidy( false );
137 return $this->parser
->parse(
145 $maintClass = CLIParser
::class;
146 require_once RUN_MAINTENANCE_IF_MAIN
;