b43a02017dcbf66900941c04fb12b8840528ae6a
[lhc/web/wiklou.git] / maintenance / preprocessDump.php
1 <?php
2 /**
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
5 * preprocessor cache.
6 *
7 * Copyright (C) 2011 Platonides - http://www.mediawiki.org/
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 * @ingroup Maintenance
26 */
27
28 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
29
30 class PreprocessDump extends Maintenance {
31
32 private $count = 0;
33 private $outputDirectory, $startTime;
34
35 /* Variables for dressing up as a parser */
36 public $mTitle = 'PreprocessDump';
37 public $mPPNodeCount = 0;
38
39 public function getStripList() {
40 global $wgParser;
41 return $wgParser->getStripList();
42 }
43
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 );
53 }
54
55 public function getDbType() {
56 return Maintenance::DB_NONE;
57 }
58
59 public function execute() {
60 global $wgParser, $wgParserConf, $wgPreprocessorCacheThreshold;
61
62 if (! ( $this->hasOption( 'file' ) ^ $this->hasOption( 'dump' ) ) ) {
63 $this->error("You must provide a file or dump", true);
64 }
65
66 if ( !$this->hasOption( 'cache' ) ) {
67 $wgPreprocessorCacheThreshold = false;
68 $this->saveFailed = $this->getOption('save-failed');
69 }
70
71 if ( $this->hasOption( 'preprocessor' ) ) {
72 $name = $this->getOption( 'preprocessor' );
73 } elseif ( isset( $wgParserConf['preprocessorClass'] ) ) {
74 $name = $wgParserConf['preprocessorClass'];
75 } else {
76 $name = 'Preprocessor_DOM';
77 }
78
79 $wgParser->firstCallInit();
80 $this->mPreprocessor = new $name( $this );
81
82 if ( $this->hasOption( 'file' ) ) {
83 $revision = new WikiRevision;
84
85 $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
86 $revision->setTitle( Title::newFromText( rawurldecode( basename( $this->getOption('file'), '.txt' ) ) ) );
87 $this->handleRevision( $revision );
88 return;
89 }
90
91 $this->startTime = wfTime();
92
93 if ( $this->getOption('dump') == '-' ) {
94 $source = new ImportStreamSource( $this->getStdin() );
95 } else {
96 $this->error("Sorry, I don't support dump filenames yet. Use - and provide it on stdin on the meantime.", true);
97 }
98 $importer = new WikiImporter( $source );
99
100 $importer->setRevisionCallback(
101 array( &$this, 'handleRevision' ) );
102
103 $this->from = $this->getOption( 'from', null );
104 $this->count = 0;
105 $importer->doImport();
106
107 $delta = wfTime() - $this->startTime;
108 $this->error( "{$this->count} revisions preprocessed in " . round($delta, 2) . " seconds " );
109 if ($delta > 0)
110 $this->error( round($this->count / $delta, 2) . " pages/sec" );
111
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" );
115 }
116
117 /**
118 * Callback function for each revision, preprocessToObj()
119 * @param $rev Revision
120 */
121 public function handleRevision( $rev ) {
122 $title = $rev->getTitle();
123 if ( !$title ) {
124 $this->error( "Got bogus revision with null title!" );
125 return;
126 }
127
128 $this->count++;
129 if ( isset( $this->from ) ) {
130 if ( $this->from != $title )
131 return;
132 $this->output( "Skipped " . ($this->count - 1) . " pages\n" );
133
134 $this->count = 1;
135 $this->from = null;
136 }
137 try {
138 $this->mPreprocessor->preprocessToObj( $rev->getText(), 0 );
139 }
140 catch(Exception $e) {
141 $this->error("Caught exception " . $e->getMessage() . " in " . $title-> getPrefixedText() );
142 }
143 }
144 }
145
146 $maintClass = "PreprocessDump";
147 require_once( DO_MAINTENANCE );
148