5 * Wikitext can be given by stdin or using a file. The wikitext will be parsed
6 * using 'CLIParser' as a title. This can be overridden with --title option.
10 * $ php parse.php --title foo
12 * <p><i><strong class="selflink">foo</strong></i>
18 * $ echo "'''bold'''" > /tmp/foo.txt
19 * $ php parse.php /tmp/foo.txt
26 * $ cat /tmp/foo | php parse.php
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, write to the Free Software Foundation, Inc.,
43 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
44 * http://www.gnu.org/copyleft/gpl.html
47 * @ingroup Maintenance
48 * @author Antoine Musso <hashar at free dot fr>
49 * @license GNU General Public License 2.0 or later
52 require_once __DIR__
. '/Maintenance.php';
55 * Maintenance script to parse some wikitext.
57 * @ingroup Maintenance
59 class CLIParser
extends Maintenance
{
62 public function __construct() {
63 parent
::__construct();
64 $this->addDescription( 'Parse a given wikitext' );
67 'Title name for the given wikitext (Default: \'CLIParser\')',
71 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
74 public function execute() {
76 print $this->render( $this->Wikitext() );
80 * @param string $wikitext Wikitext to get rendered
81 * @return string HTML Rendering
83 public function render( $wikitext ) {
84 return $this->parse( $wikitext )->getText();
88 * Get wikitext from a the file passed as argument or STDIN
89 * @return string Wikitext
91 protected function Wikitext() {
92 $php_stdin = 'php://stdin';
93 $input_file = $this->getArg( 0, $php_stdin );
95 if ( $input_file === $php_stdin && !$this->mQuiet
) {
96 $ctrl = wfIsWindows() ?
'CTRL+Z' : 'CTRL+D';
97 $this->error( basename( __FILE__
)
98 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
101 return file_get_contents( $input_file );
104 protected function initParser() {
105 global $wgParserConf;
106 $parserClass = $wgParserConf['class'];
107 $this->parser
= new $parserClass();
111 * Title object to use for CLI parsing.
112 * Default title is 'CLIParser', it can be overridden with the option
113 * --title <Your:Title>
117 protected function getTitle() {
118 $title = $this->getOption( 'title' )
119 ?
$this->getOption( 'title' )
122 return Title
::newFromText( $title );
126 * @param string $wikitext Wikitext to parse
127 * @return ParserOutput
129 protected function parse( $wikitext ) {
130 return $this->parser
->parse(
138 $maintClass = "CLIParser";
139 require_once RUN_MAINTENANCE_IF_MAIN
;