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