mediawiki.util: Add mw.log.deprecate to #jsMessage
[lhc/web/wiklou.git] / maintenance / parse.php
1 <?php
2 /**
3 * Parse some wikitext.
4 *
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 overriden with --title option.
7 *
8 * Example1:
9 * @code
10 * $ php parse.php --title foo
11 * ''[[foo]]''^D
12 * <p><i><strong class="selflink">foo</strong></i>
13 * </p>
14 * @endcode
15 *
16 * Example2:
17 * @code
18 * $ echo "'''bold'''" > /tmp/foo.txt
19 * $ php parse.php /tmp/foo.txt
20 * <p><b>bold</b>
21 * </p>$
22 * @endcode
23 *
24 * Example3:
25 * @code
26 * $ cat /tmp/foo | php parse.php
27 * <p><b>bold</b>
28 * </p>$
29 * @endcode
30 *
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.
35 *
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.
40 *
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
45 *
46 * @file
47 * @ingroup Maintenance
48 * @author Antoine Musso <hashar at free dot fr>
49 * @license GNU General Public License 2.0 or later
50 */
51
52 require_once __DIR__ . '/Maintenance.php';
53
54 /**
55 * Maintenance script to parse some wikitext.
56 *
57 * @ingroup Maintenance
58 */
59 class CLIParser extends Maintenance {
60 protected $parser;
61
62 public function __construct() {
63 parent::__construct();
64 $this->mDescription = "Parse a given wikitext";
65 $this->addOption(
66 'title',
67 'Title name for the given wikitext (Default: \'CLIParser\')',
68 false,
69 true
70 );
71 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
72 }
73
74 public function execute() {
75 $this->initParser();
76 print $this->render( $this->WikiText() );
77 }
78
79 /**
80 * @param string $wikitext Wikitext to get rendered
81 * @return string HTML Rendering
82 */
83 public function render( $wikitext ) {
84 return $this->parse( $wikitext )->getText();
85 }
86
87 /**
88 * Get wikitext from a the file passed as argument or STDIN
89 * @return string Wikitext
90 */
91 protected function Wikitext() {
92
93 $php_stdin = 'php://stdin';
94 $input_file = $this->getArg( 0, $php_stdin );
95
96 if ( $input_file === $php_stdin ) {
97 $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
98 $this->error( basename( __FILE__ )
99 . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
100 }
101
102 return file_get_contents( $input_file );
103 }
104
105 protected function initParser() {
106 global $wgParserConf;
107 $parserClass = $wgParserConf['class'];
108 $this->parser = new $parserClass();
109 }
110
111 /**
112 * Title object to use for CLI parsing.
113 * Default title is 'CLIParser', it can be overriden with the option
114 * --title <Your:Title>
115 *
116 * @return Title object
117 */
118 protected function getTitle() {
119 $title = $this->getOption( 'title' )
120 ? $this->getOption( 'title' )
121 : 'CLIParser';
122
123 return Title::newFromText( $title );
124 }
125
126 /**
127 * @param string $wikitext Wikitext to parse
128 * @return ParserOutput
129 */
130 protected function parse( $wikitext ) {
131 return $this->parser->parse(
132 $wikitext,
133 $this->getTitle(),
134 new ParserOptions()
135 );
136 }
137 }
138
139 $maintClass = "CLIParser";
140 require_once RUN_MAINTENANCE_IF_MAIN;