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