Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.50% covered (warning)
82.50%
33 / 40
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CLIParser
82.50% covered (warning)
82.50%
33 / 40
50.00% covered (danger)
50.00%
3 / 6
12.77
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
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
78.57% covered (warning)
78.57%
11 / 14
0.00% covered (danger)
0.00%
0 / 1
2.04
 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;
8use Wikimedia\Parsoid\Utils\ContentUtils;
9
10/**
11 * Parse some wikitext.
12 *
13 * Wikitext can be given by stdin or using a file. The wikitext will be parsed
14 * using 'CLIParser' as a title. This can be overridden with --title option.
15 *
16 * Example1:
17 * @code
18 * $ php parse.php --title foo
19 * ''[[foo]]''^D
20 * <p><i><strong class="selflink">foo</strong></i>
21 * </p>
22 * @endcode
23 *
24 * Example2:
25 * @code
26 * $ echo "'''bold'''" > /tmp/foo.txt
27 * $ php parse.php /tmp/foo.txt
28 * <p><b>bold</b>
29 * </p>$
30 * @endcode
31 *
32 * Example3:
33 * @code
34 * $ cat /tmp/foo | php parse.php
35 * <p><b>bold</b>
36 * </p>$
37 * @endcode
38 *
39 * @license GPL-2.0-or-later
40 * @file
41 * @ingroup Maintenance
42 * @author Antoine Musso <hashar at free dot fr>
43 * @license GPL-2.0-or-later
44 */
45
46// @codeCoverageIgnoreStart
47require_once __DIR__ . '/Maintenance.php';
48// @codeCoverageIgnoreEnd
49
50/**
51 * Maintenance script to parse some wikitext.
52 *
53 * @ingroup Maintenance
54 */
55class CLIParser extends Maintenance {
56    /** @var Parser|ParsoidParser */
57    protected $parser;
58
59    public function __construct() {
60        parent::__construct();
61        $this->addDescription( 'Parse a given wikitext' );
62        $this->addOption(
63            'title',
64            'Title name for the given wikitext (Default: \'CLIParser\')',
65            false,
66            true
67        );
68        $this->addArg( 'file', 'File containing wikitext (Default: stdin)', false );
69        $this->addOption( 'parsoid', 'Whether to use Parsoid', false, false, 'p' );
70        $this->addOption( 'show-rich-attributes', 'Show rich attributes', false );
71    }
72
73    public function execute() {
74        $this->initParser();
75        print $this->render( $this->Wikitext() );
76    }
77
78    /**
79     * @param string $wikitext Wikitext to get rendered
80     * @return string HTML Rendering
81     */
82    public function render( $wikitext ) {
83        $options = ParserOptions::newFromAnon();
84        $options->setOption( 'enableLimitReport', false );
85        $po = $this->parser->parse(
86            $wikitext,
87            $this->getTitle(),
88            $options
89        );
90        // TODO T371008 consider if using the Content framework makes sense instead of creating the pipeline
91        $pipeline = $this->getServiceContainer()->getDefaultOutputPipeline();
92        $po = $pipeline->run( $po, $options, [ 'wrapperDivClass' => '' ] );
93        if ( $this->getOption( 'show-rich-attributes' ) ) {
94            $df = $po->getContentHolder()->getAsDom();
95            $df ??= $po->getContentHolder()->createFragment();
96            return ContentUtils::dumpDOM( $df, '', [ 'quiet' => true ] );
97        }
98        return $po->getContentHolderText();
99    }
100
101    /**
102     * Get wikitext from a the file passed as argument or STDIN
103     * @return string Wikitext
104     */
105    protected function Wikitext() {
106        $php_stdin = 'php://stdin';
107        $input_file = $this->getArg( 0, $php_stdin );
108
109        if ( $input_file === $php_stdin && !$this->mQuiet ) {
110            $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
111            $this->error( basename( __FILE__ )
112                . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
113        }
114
115        return file_get_contents( $input_file );
116    }
117
118    protected function initParser() {
119        $services = $this->getServiceContainer();
120        if ( $this->hasOption( 'parsoid' ) ) {
121            $this->parser = $services->getParsoidParserFactory()->create();
122        } else {
123            $this->parser = $services->getParserFactory()->create();
124        }
125    }
126
127    /**
128     * Title object to use for CLI parsing.
129     * Default title is 'CLIParser', it can be overridden with the option
130     * --title <Your:Title>
131     *
132     * @return Title
133     */
134    protected function getTitle() {
135        $title = $this->getOption( 'title' ) ?: 'CLIParser';
136
137        return Title::newFromText( $title );
138    }
139}
140
141// @codeCoverageIgnoreStart
142$maintClass = CLIParser::class;
143require_once RUN_MAINTENANCE_IF_MAIN;
144// @codeCoverageIgnoreEnd