Add ability to override mb_strtoupper in Language::ucfirst
[lhc/web/wiklou.git] / maintenance / parse.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * Parse some wikitext.
7 *
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.
10 *
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
18 *
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
26 *
27 * Example3:
28 * @code
29 * $ cat /tmp/foo | php parse.php
30 * <p><b>bold</b>
31 * </p>$
32 * @endcode
33 *
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.
38 *
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.
43 *
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
48 *
49 * @file
50 * @ingroup Maintenance
51 * @author Antoine Musso <hashar at free dot fr>
52 * @license GPL-2.0-or-later
53 */
54
55 require_once __DIR__ . '/Maintenance.php';
56
57 /**
58 * Maintenance script to parse some wikitext.
59 *
60 * @ingroup Maintenance
61 */
62 class CLIParser extends Maintenance {
63 protected $parser;
64
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
73 );
74 $this->addOption( 'no-tidy', 'Don\'t tidy the output (deprecated)' );
75 $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
76 }
77
78 public function execute() {
79 $this->initParser();
80 print $this->render( $this->Wikitext() );
81 }
82
83 /**
84 * @param string $wikitext Wikitext to get rendered
85 * @return string HTML Rendering
86 */
87 public function render( $wikitext ) {
88 return $this->parse( $wikitext )->getText( [ 'wrapperDivClass' => '' ] );
89 }
90
91 /**
92 * Get wikitext from a the file passed as argument or STDIN
93 * @return string Wikitext
94 */
95 protected function Wikitext() {
96 $php_stdin = 'php://stdin';
97 $input_file = $this->getArg( 0, $php_stdin );
98
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" );
103 }
104
105 return file_get_contents( $input_file );
106 }
107
108 protected function initParser() {
109 $this->parser = MediaWikiServices::getInstance()->getParserFactory()->create();
110 }
111
112 /**
113 * Title object to use for CLI parsing.
114 * Default title is 'CLIParser', it can be overridden with the option
115 * --title <Your:Title>
116 *
117 * @return Title
118 */
119 protected function getTitle() {
120 $title = $this->getOption( 'title' ) ?: 'CLIParser';
121
122 return Title::newFromText( $title );
123 }
124
125 /**
126 * @param string $wikitext Wikitext to parse
127 * @return ParserOutput
128 */
129 protected function parse( $wikitext ) {
130 $options = ParserOptions::newCanonical();
131 $options->setOption( 'enableLimitReport', false );
132 if ( $this->getOption( 'no-tidy' ) ) {
133 $options->setTidy( false );
134 }
135 return $this->parser->parse(
136 $wikitext,
137 $this->getTitle(),
138 $options
139 );
140 }
141 }
142
143 $maintClass = CLIParser::class;
144 require_once RUN_MAINTENANCE_IF_MAIN;