parse.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Parse some wikitext.
  4. *
  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 overridden 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.txt
  19. * $ php parse.php /tmp/foo.txt
  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. * This program is free software; you can redistribute it and/or modify
  32. * it under the terms of the GNU General Public License as published by
  33. * the Free Software Foundation; either version 2 of the License, or
  34. * (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License along
  42. * with this program; if not, write to the Free Software Foundation, Inc.,
  43. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  44. * http://www.gnu.org/copyleft/gpl.html
  45. *
  46. * @file
  47. * @ingroup Maintenance
  48. * @author Antoine Musso <hashar at free dot fr>
  49. * @license GNU General Public License 2.0 or later
  50. */
  51. require_once __DIR__ . '/Maintenance.php';
  52. /**
  53. * Maintenance script to parse some wikitext.
  54. *
  55. * @ingroup Maintenance
  56. */
  57. class CLIParser extends Maintenance {
  58. protected $parser;
  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. }
  70. public function execute() {
  71. $this->initParser();
  72. print $this->render( $this->Wikitext() );
  73. }
  74. /**
  75. * @param string $wikitext Wikitext to get rendered
  76. * @return string HTML Rendering
  77. */
  78. public function render( $wikitext ) {
  79. return $this->parse( $wikitext )->getText();
  80. }
  81. /**
  82. * Get wikitext from a the file passed as argument or STDIN
  83. * @return string Wikitext
  84. */
  85. protected function Wikitext() {
  86. $php_stdin = 'php://stdin';
  87. $input_file = $this->getArg( 0, $php_stdin );
  88. if ( $input_file === $php_stdin && !$this->mQuiet ) {
  89. $ctrl = wfIsWindows() ? 'CTRL+Z' : 'CTRL+D';
  90. $this->error( basename( __FILE__ )
  91. . ": warning: reading wikitext from STDIN. Press $ctrl to parse.\n" );
  92. }
  93. return file_get_contents( $input_file );
  94. }
  95. protected function initParser() {
  96. global $wgParserConf;
  97. $parserClass = $wgParserConf['class'];
  98. $this->parser = new $parserClass();
  99. }
  100. /**
  101. * Title object to use for CLI parsing.
  102. * Default title is 'CLIParser', it can be overridden with the option
  103. * --title <Your:Title>
  104. *
  105. * @return Title
  106. */
  107. protected function getTitle() {
  108. $title = $this->getOption( 'title' )
  109. ? $this->getOption( 'title' )
  110. : 'CLIParser';
  111. return Title::newFromText( $title );
  112. }
  113. /**
  114. * @param string $wikitext Wikitext to parse
  115. * @return ParserOutput
  116. */
  117. protected function parse( $wikitext ) {
  118. return $this->parser->parse(
  119. $wikitext,
  120. $this->getTitle(),
  121. new ParserOptions()
  122. );
  123. }
  124. }
  125. $maintClass = "CLIParser";
  126. require_once RUN_MAINTENANCE_IF_MAIN;