Debian Codegraph @

maintenance/parse.php

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