Index: trunk/phase3/maintenance/preprocessDump.php |
— | — | @@ -0,0 +1,148 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Take page text out of an XML dump file and preprocess it to obj. |
| 5 | + * It may be useful for getting preprocessor statistics or filling the |
| 6 | + * preprocessor cache. |
| 7 | + * |
| 8 | + * Copyright (C) 2011 Platonides - http://www.mediawiki.org/ |
| 9 | + * |
| 10 | + * This program is free software; you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation; either version 2 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License along |
| 21 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | + * http://www.gnu.org/copyleft/gpl.html |
| 24 | + * |
| 25 | + * @file |
| 26 | + * @ingroup Maintenance |
| 27 | + */ |
| 28 | + |
| 29 | +require_once( dirname( __FILE__ ) . '/Maintenance.php' ); |
| 30 | + |
| 31 | +class PreprocessDump extends Maintenance { |
| 32 | + |
| 33 | + private $count = 0; |
| 34 | + private $outputDirectory, $startTime; |
| 35 | + |
| 36 | + /* Variables for dressing up as a parser */ |
| 37 | + public $mTitle = 'PreprocessDump'; |
| 38 | + public $mPPNodeCount = 0; |
| 39 | + |
| 40 | + public function getStripList() { |
| 41 | + global $wgParser; |
| 42 | + return $wgParser->getStripList(); |
| 43 | + } |
| 44 | + |
| 45 | + public function __construct() { |
| 46 | + parent::__construct(); |
| 47 | + $this->saveFailed = false; |
| 48 | + $this->mDescription = "Run a file or dump with a preprocessor"; |
| 49 | + $this->addOption( 'file', 'File with text to run.', false, true ); |
| 50 | + $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true ); |
| 51 | + $this->addOption( 'from', 'Article from XML dump to start from.', false, true ); |
| 52 | + $this->addOption( 'cache', 'Use and populate the preprocessor cache.', false, false ); |
| 53 | + $this->addOption( 'preprocessor', 'Preprocessor to use.', false, false ); |
| 54 | + } |
| 55 | + |
| 56 | + public function getDbType() { |
| 57 | + return Maintenance::DB_NONE; |
| 58 | + } |
| 59 | + |
| 60 | + public function execute() { |
| 61 | + global $wgParser, $wgParserConf, $wgPreprocessorCacheThreshold; |
| 62 | + |
| 63 | + if (! ( $this->hasOption( 'file' ) ^ $this->hasOption( 'dump' ) ) ) { |
| 64 | + $this->error("You must provide a file or dump", true); |
| 65 | + } |
| 66 | + |
| 67 | + if ( !$this->hasOption( 'cache' ) ) { |
| 68 | + $wgPreprocessorCacheThreshold = false; |
| 69 | + $this->saveFailed = $this->getOption('save-failed'); |
| 70 | + } |
| 71 | + |
| 72 | + if ( $this->hasOption( 'preprocessor' ) ) { |
| 73 | + $name = $this->getOption( 'preprocessor' ); |
| 74 | + } elseif ( isset( $wgParserConf['preprocessorClass'] ) ) { |
| 75 | + $name = $wgParserConf['preprocessorClass']; |
| 76 | + } else { |
| 77 | + $name = 'Preprocessor_DOM'; |
| 78 | + } |
| 79 | + |
| 80 | + $wgParser->firstCallInit(); |
| 81 | + $this->mPreprocessor = new $name( $this ); |
| 82 | + |
| 83 | + if ( $this->hasOption( 'file' ) ) { |
| 84 | + $revision = new WikiRevision; |
| 85 | + |
| 86 | + $revision->setText( file_get_contents( $this->getOption( 'file' ) ) ); |
| 87 | + $revision->setTitle( Title::newFromText( rawurldecode( basename( $this->getOption('file'), '.txt' ) ) ) ); |
| 88 | + $this->handleRevision( $revision ); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $this->startTime = wfTime(); |
| 93 | + |
| 94 | + if ( $this->getOption('dump') == '-' ) { |
| 95 | + $source = new ImportStreamSource( $this->getStdin() ); |
| 96 | + } else { |
| 97 | + $this->error("Sorry, I don't support dump filenames yet. Use - and provide it on stdin on the meantime.", true); |
| 98 | + } |
| 99 | + $importer = new WikiImporter( $source ); |
| 100 | + |
| 101 | + $importer->setRevisionCallback( |
| 102 | + array( &$this, 'handleRevision' ) ); |
| 103 | + |
| 104 | + $this->from = $this->getOption( 'from', null ); |
| 105 | + $this->count = 0; |
| 106 | + $importer->doImport(); |
| 107 | + |
| 108 | + $delta = wfTime() - $this->startTime; |
| 109 | + $this->error( "{$this->count} revisions preprocessed in " . round($delta, 2) . " seconds " ); |
| 110 | + if ($delta > 0) |
| 111 | + $this->error( round($this->count / $delta, 2) . " pages/sec" ); |
| 112 | + |
| 113 | + # Perform the memory_get_peak_usage() when all the other data has been output so there's no damage if it dies. |
| 114 | + # It is only available since 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit) |
| 115 | + $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Callback function for each revision, preprocessToObj() |
| 120 | + * @param $rev Revision |
| 121 | + */ |
| 122 | + public function handleRevision( $rev ) { |
| 123 | + $title = $rev->getTitle(); |
| 124 | + if ( !$title ) { |
| 125 | + $this->error( "Got bogus revision with null title!" ); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + $this->count++; |
| 130 | + if ( isset( $this->from ) ) { |
| 131 | + if ( $this->from != $title ) |
| 132 | + return; |
| 133 | + $this->output( "Skipped " . ($this->count - 1) . " pages\n" ); |
| 134 | + |
| 135 | + $this->count = 1; |
| 136 | + $this->from = null; |
| 137 | + } |
| 138 | + try { |
| 139 | + $this->mPreprocessor->preprocessToObj( $rev->getText(), 0 ); |
| 140 | + } |
| 141 | + catch(Exception $e) { |
| 142 | + $this->error("Caught exception " . $e->getMessage() . " in " . $title-> getPrefixedText() ); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +$maintClass = "PreprocessDump"; |
| 148 | +require_once( DO_MAINTENANCE ); |
| 149 | + |
Property changes on: trunk/phase3/maintenance/preprocessDump.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 150 | + native |