Index: trunk/phase3/maintenance/parse.php |
— | — | @@ -0,0 +1,104 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * CLI script to easily parse some wikitext. |
| 5 | + * Wikitext can be given by stdin or using a file. The wikitext will be parsed |
| 6 | + * using 'CLIParser' as a title. This can be overriden with --title option. |
| 7 | + * |
| 8 | + * Example1: |
| 9 | + * @code |
| 10 | + * $ php parse.php --title foo |
| 11 | + * ''[[foo]]''^D |
| 12 | + * <p><i><strong class="selflink">foo</strong></i> |
| 13 | + * </p> |
| 14 | + * @endcode |
| 15 | + * |
| 16 | + * Example2: |
| 17 | + * @code |
| 18 | + * $ echo "'''bold'''" > /tmp/foo |
| 19 | + * $ php parse.php --file /tmp/foo |
| 20 | + * <p><b>bold</b> |
| 21 | + * </p>$ |
| 22 | + * @endcode |
| 23 | + * |
| 24 | + * Example3: |
| 25 | + * @code |
| 26 | + * $ cat /tmp/foo | php parse.php |
| 27 | + * <p><b>bold</b> |
| 28 | + * </p>$ |
| 29 | + * @endcode |
| 30 | + * |
| 31 | + * @inGroup Maintenance |
| 32 | + * @author Antoine Musso <hashar at free dot fr> |
| 33 | + * @license GNU General Public License 2.0 or later |
| 34 | + */ |
| 35 | +require_once( dirname(__FILE__) . '/Maintenance.php' ); |
| 36 | + |
| 37 | +class CLIParser extends Maintenance { |
| 38 | + protected $parser; |
| 39 | + |
| 40 | + public function __construct() { |
| 41 | + $this->mDescription = "Parse a given wikitext"; |
| 42 | + $this->addOption( 'title', 'Title name for the given wikitext (Default: \'CLIParser\')', false, true ); |
| 43 | + $this->addOption( 'file', 'File containing wikitext (Default: stdin)', false, true ); |
| 44 | + parent::__construct(); |
| 45 | + } |
| 46 | + |
| 47 | + public function execute() { |
| 48 | + $this->initParser(); |
| 49 | + print $this->render( $this->WikiText() ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param string $wikitext Wikitext to get rendered |
| 54 | + * @return string HTML Rendering |
| 55 | + */ |
| 56 | + public function render( $wikitext ) { |
| 57 | + return $this->parse( $wikitext )->getText(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get wikitext from --file or from STDIN |
| 62 | + * @return string Wikitext |
| 63 | + */ |
| 64 | + protected function Wikitext() { |
| 65 | + return file_get_contents( |
| 66 | + $this->getOption( 'file', 'php://stdin' ) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + protected function initParser() { |
| 71 | + global $wgParserConf; |
| 72 | + $parserClass = $wgParserConf['class']; |
| 73 | + $this->parser = new $parserClass(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Title object to use for CLI parsing. |
| 78 | + * Default title is 'CLIParser', it can be overriden with the option |
| 79 | + * --title <Your:Title> |
| 80 | + * |
| 81 | + * @return Title object |
| 82 | + */ |
| 83 | + protected function getTitle( ) { |
| 84 | + $title = |
| 85 | + $this->getOption( 'title' ) |
| 86 | + ? $this->getOption( 'title' ) |
| 87 | + : 'CLIParser' ; |
| 88 | + return Title::newFromText( $title ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param string $text Wikitext to parse |
| 93 | + * @return ParserOutput |
| 94 | + */ |
| 95 | + protected function parse( $wikitext ) { |
| 96 | + return $this->parser->parse( |
| 97 | + $wikitext |
| 98 | + , $this->getTitle() |
| 99 | + , new ParserOptions() |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +$maintClass = "CLIParser"; |
| 105 | +require_once( RUN_MAINTENANCE_IF_MAIN ); |
Property changes on: trunk/phase3/maintenance/parse.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 106 | + native |