| 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 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
require_once __DIR__ . '/Maintenance.php'; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
class CLIParser extends Maintenance { |
| 68 |
|
| 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 |
|
| 91 |
|
| 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 |
|
| 102 |
$pipeline = $this->getServiceContainer()->getDefaultOutputPipeline(); |
| 103 |
return $pipeline->run( $po, $options, [ 'wrapperDivClass' => '' ] )->getContentHolderText(); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
protected function getTitle() { |
| 140 |
$title = $this->getOption( 'title' ) ?: 'CLIParser'; |
| 141 |
|
| 142 |
return Title::newFromText( $title ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
$maintClass = CLIParser::class; |
| 148 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 149 |
|