Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
CLIParser | |
0.00% |
0 / 33 |
|
0.00% |
0 / 7 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
render | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
Wikitext | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
initParser | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getTitle | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
parse | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | use MediaWiki\Parser\ParserOutput; |
4 | use MediaWiki\Title\Title; |
5 | |
6 | /** |
7 | * Parse some wikitext. |
8 | * |
9 | * Wikitext can be given by stdin or using a file. The wikitext will be parsed |
10 | * using 'CLIParser' as a title. This can be overridden with --title option. |
11 | * |
12 | * Example1: |
13 | * @code |
14 | * $ php parse.php --title foo |
15 | * ''[[foo]]''^D |
16 | * <p><i><strong class="selflink">foo</strong></i> |
17 | * </p> |
18 | * @endcode |
19 | * |
20 | * Example2: |
21 | * @code |
22 | * $ echo "'''bold'''" > /tmp/foo.txt |
23 | * $ php parse.php /tmp/foo.txt |
24 | * <p><b>bold</b> |
25 | * </p>$ |
26 | * @endcode |
27 | * |
28 | * Example3: |
29 | * @code |
30 | * $ cat /tmp/foo | php parse.php |
31 | * <p><b>bold</b> |
32 | * </p>$ |
33 | * @endcode |
34 | * |
35 | * This program is free software; you can redistribute it and/or modify |
36 | * it under the terms of the GNU General Public License as published by |
37 | * the Free Software Foundation; either version 2 of the License, or |
38 | * (at your option) any later version. |
39 | * |
40 | * This program is distributed in the hope that it will be useful, |
41 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
42 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
43 | * GNU General Public License for more details. |
44 | * |
45 | * You should have received a copy of the GNU General Public License along |
46 | * with this program; if not, write to the Free Software Foundation, Inc., |
47 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
48 | * http://www.gnu.org/copyleft/gpl.html |
49 | * |
50 | * @file |
51 | * @ingroup Maintenance |
52 | * @author Antoine Musso <hashar at free dot fr> |
53 | * @license GPL-2.0-or-later |
54 | */ |
55 | |
56 | require_once __DIR__ . '/Maintenance.php'; |
57 | |
58 | /** |
59 | * Maintenance script to parse some wikitext. |
60 | * |
61 | * @ingroup Maintenance |
62 | */ |
63 | class CLIParser extends Maintenance { |
64 | protected $parser; |
65 | |
66 | public function __construct() { |
67 | parent::__construct(); |
68 | $this->addDescription( 'Parse a given wikitext' ); |
69 | $this->addOption( |
70 | 'title', |
71 | 'Title name for the given wikitext (Default: \'CLIParser\')', |
72 | false, |
73 | true |
74 | ); |
75 | $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false ); |
76 | $this->addOption( 'parsoid', 'Whether to use Parsoid', false, false, 'p' ); |
77 | } |
78 | |
79 | public function execute() { |
80 | $this->initParser(); |
81 | print $this->render( $this->Wikitext() ); |
82 | } |
83 | |
84 | /** |
85 | * @param string $wikitext Wikitext to get rendered |
86 | * @return string HTML Rendering |
87 | */ |
88 | public function render( $wikitext ) { |
89 | return $this->parse( $wikitext )->getText( [ 'wrapperDivClass' => '' ] ); |
90 | } |
91 | |
92 | /** |
93 | * Get wikitext from a the file passed as argument or STDIN |
94 | * @return string Wikitext |
95 | */ |
96 | protected function Wikitext() { |
97 | $php_stdin = 'php://stdin'; |
98 | $input_file = $this->getArg( 0, $php_stdin ); |
99 | |
100 | if ( $input_file === $php_stdin && !$this->mQuiet ) { |
101 | $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D'; |
102 | $this->error( basename( __FILE__ ) |
103 | . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" ); |
104 | } |
105 | |
106 | return file_get_contents( $input_file ); |
107 | } |
108 | |
109 | protected function initParser() { |
110 | $services = $this->getServiceContainer(); |
111 | if ( $this->hasOption( 'parsoid' ) ) { |
112 | $this->parser = $services->getParsoidParserFactory()->create(); |
113 | } else { |
114 | $this->parser = $services->getParserFactory()->create(); |
115 | } |
116 | } |
117 | |
118 | /** |
119 | * Title object to use for CLI parsing. |
120 | * Default title is 'CLIParser', it can be overridden with the option |
121 | * --title <Your:Title> |
122 | * |
123 | * @return Title |
124 | */ |
125 | protected function getTitle() { |
126 | $title = $this->getOption( 'title' ) ?: 'CLIParser'; |
127 | |
128 | return Title::newFromText( $title ); |
129 | } |
130 | |
131 | /** |
132 | * @param string $wikitext Wikitext to parse |
133 | * @return ParserOutput |
134 | */ |
135 | protected function parse( $wikitext ) { |
136 | $options = ParserOptions::newFromAnon(); |
137 | $options->setOption( 'enableLimitReport', false ); |
138 | return $this->parser->parse( |
139 | $wikitext, |
140 | $this->getTitle(), |
141 | $options |
142 | ); |
143 | } |
144 | } |
145 | |
146 | $maintClass = CLIParser::class; |
147 | require_once RUN_MAINTENANCE_IF_MAIN; |