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->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
77 public function execute() {
79 print $this->render( $this->Wikitext() );
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' => '' ] );
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>
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(
139 $maintClass = CLIParser
::class;
140 require_once RUN_MAINTENANCE_IF_MAIN
;