renderDump.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Take page text out of an XML dump file and render basic HTML out to files.
  4. * This is *NOT* suitable for publishing or offline use; it's intended for
  5. * running comparative tests of parsing behavior using real-world data.
  6. *
  7. * Templates etc are pulled from the local wiki database, not from the dump.
  8. *
  9. * Copyright (C) 2006 Brion Vibber <brion@pobox.com>
  10. * https://www.mediawiki.org/
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. * http://www.gnu.org/copyleft/gpl.html
  26. *
  27. * @file
  28. * @ingroup Maintenance
  29. */
  30. require_once __DIR__ . '/Maintenance.php';
  31. /**
  32. * Maintenance script that takes page text out of an XML dump file
  33. * and render basic HTML out to files.
  34. *
  35. * @ingroup Maintenance
  36. */
  37. class DumpRenderer extends Maintenance {
  38. private $count = 0;
  39. private $outputDirectory, $startTime;
  40. public function __construct() {
  41. parent::__construct();
  42. $this->addDescription(
  43. 'Take page text out of an XML dump file and render basic HTML out to files' );
  44. $this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true );
  45. $this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true );
  46. $this->addOption( 'parser', 'Use an alternative parser class', false, true );
  47. }
  48. public function execute() {
  49. $this->outputDirectory = $this->getOption( 'output-dir' );
  50. $this->prefix = $this->getOption( 'prefix', 'wiki' );
  51. $this->startTime = microtime( true );
  52. if ( $this->hasOption( 'parser' ) ) {
  53. global $wgParserConf;
  54. $wgParserConf['class'] = $this->getOption( 'parser' );
  55. $this->prefix .= "-{$wgParserConf['class']}";
  56. }
  57. $source = new ImportStreamSource( $this->getStdin() );
  58. $importer = new WikiImporter( $source, $this->getConfig() );
  59. $importer->setRevisionCallback(
  60. [ $this, 'handleRevision' ] );
  61. $importer->doImport();
  62. $delta = microtime( true ) - $this->startTime;
  63. $this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " );
  64. if ( $delta > 0 ) {
  65. $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
  66. }
  67. $this->error( "\n" );
  68. }
  69. /**
  70. * Callback function for each revision, turn into HTML and save
  71. * @param Revision $rev
  72. */
  73. public function handleRevision( $rev ) {
  74. $title = $rev->getTitle();
  75. if ( !$title ) {
  76. $this->error( "Got bogus revision with null title!" );
  77. return;
  78. }
  79. $display = $title->getPrefixedText();
  80. $this->count++;
  81. $sanitized = rawurlencode( $display );
  82. $filename = sprintf( "%s/%s-%07d-%s.html",
  83. $this->outputDirectory,
  84. $this->prefix,
  85. $this->count,
  86. $sanitized );
  87. $this->output( sprintf( "%s\n", $filename, $display ) );
  88. $user = new User();
  89. $options = ParserOptions::newFromUser( $user );
  90. $content = $rev->getContent();
  91. $output = $content->getParserOutput( $title, null, $options );
  92. file_put_contents( $filename,
  93. "<!DOCTYPE html>\n" .
  94. "<html lang=\"en\" dir=\"ltr\">\n" .
  95. "<head>\n" .
  96. "<meta charset=\"UTF-8\" />\n" .
  97. "<title>" . htmlspecialchars( $display ) . "</title>\n" .
  98. "</head>\n" .
  99. "<body>\n" .
  100. $output->getText() .
  101. "</body>\n" .
  102. "</html>" );
  103. }
  104. }
  105. $maintClass = "DumpRenderer";
  106. require_once RUN_MAINTENANCE_IF_MAIN;