Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 2 |
DumpIterator | |
0.00% |
0 / 65 |
|
0.00% |
0 / 7 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
30 | |||
finalSetup | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
disableInterwikis | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
handleRevision | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
checkOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
conclusions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
processRevision | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
SearchDump | |
0.00% |
0 / 6 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getDbType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
processRevision | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * Take page text out of an XML dump file and perform some operation on it. |
4 | * Used as a base class for CompareParsers. |
5 | * We implement below the simple task of searching inside a dump. |
6 | * |
7 | * Copyright © 2011 Platonides |
8 | * https://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 | use MediaWiki\Content\ContentHandler; |
30 | use MediaWiki\MainConfigNames; |
31 | use MediaWiki\Maintenance\Maintenance; |
32 | use MediaWiki\Permissions\UltimateAuthority; |
33 | use MediaWiki\Revision\SlotRecord; |
34 | use MediaWiki\Settings\SettingsBuilder; |
35 | use MediaWiki\Title\Title; |
36 | use MediaWiki\User\User; |
37 | |
38 | // @codeCoverageIgnoreStart |
39 | require_once __DIR__ . '/Maintenance.php'; |
40 | // @codeCoverageIgnoreEnd |
41 | |
42 | /** |
43 | * Base class for iterating over a dump. |
44 | * |
45 | * @ingroup Maintenance |
46 | */ |
47 | abstract class DumpIterator extends Maintenance { |
48 | /** @var int */ |
49 | private $count = 0; |
50 | /** @var float */ |
51 | private $startTime; |
52 | /** @var string|null|false */ |
53 | private $from; |
54 | |
55 | public function __construct() { |
56 | parent::__construct(); |
57 | $this->addDescription( 'Does something with a dump' ); |
58 | $this->addOption( 'file', 'File with text to run.', false, true ); |
59 | $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true ); |
60 | $this->addOption( 'from', 'Article from XML dump to start from.', false, true ); |
61 | } |
62 | |
63 | public function execute() { |
64 | if ( !( $this->hasOption( 'file' ) xor $this->hasOption( 'dump' ) ) ) { |
65 | $this->fatalError( "You must provide a file or dump" ); |
66 | } |
67 | |
68 | $this->checkOptions(); |
69 | |
70 | if ( $this->hasOption( 'file' ) ) { |
71 | $file = $this->getOption( 'file' ); |
72 | $revision = new WikiRevision(); |
73 | $text = file_get_contents( $file ); |
74 | $title = Title::newFromText( rawurldecode( basename( $file, '.txt' ) ) ); |
75 | $revision->setTitle( $title ); |
76 | $content = ContentHandler::makeContent( $text, $title ); |
77 | $revision->setContent( SlotRecord::MAIN, $content ); |
78 | |
79 | $this->from = false; |
80 | $this->handleRevision( $revision ); |
81 | |
82 | return; |
83 | } |
84 | |
85 | $this->startTime = microtime( true ); |
86 | |
87 | if ( $this->getOption( 'dump' ) == '-' ) { |
88 | $source = new ImportStreamSource( $this->getStdin() ); |
89 | } else { |
90 | $this->fatalError( "Sorry, I don't support dump filenames yet. " |
91 | . "Use - and provide it on stdin on the meantime." ); |
92 | } |
93 | |
94 | $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
95 | |
96 | $importer = $this->getServiceContainer() |
97 | ->getWikiImporterFactory() |
98 | ->getWikiImporter( $source, new UltimateAuthority( $user ) ); |
99 | |
100 | $importer->setRevisionCallback( |
101 | [ $this, 'handleRevision' ] ); |
102 | $importer->setNoticeCallback( static function ( $msg, $params ) { |
103 | echo wfMessage( $msg, $params )->text() . "\n"; |
104 | } ); |
105 | |
106 | $this->from = $this->getOption( 'from', null ); |
107 | $this->count = 0; |
108 | $importer->doImport(); |
109 | |
110 | $this->conclusions(); |
111 | |
112 | $delta = microtime( true ) - $this->startTime; |
113 | $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " ); |
114 | if ( $delta > 0 ) { |
115 | $this->error( round( $this->count / $delta, 2 ) . " pages/sec" ); |
116 | } |
117 | |
118 | # Perform the memory_get_peak_usage() when all the other data has been |
119 | # output so there's no damage if it dies. It is only available since |
120 | # 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit) |
121 | $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" ); |
122 | } |
123 | |
124 | public function finalSetup( SettingsBuilder $settingsBuilder ) { |
125 | parent::finalSetup( $settingsBuilder ); |
126 | |
127 | if ( $this->getDbType() == Maintenance::DB_NONE ) { |
128 | // TODO: Allow hooks to be registered via SettingsBuilder as well! |
129 | // This matches the idea of unifying SettingsBuilder with ExtensionRegistry. |
130 | // phpcs:disable MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgHooks |
131 | global $wgHooks; |
132 | $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis'; |
133 | |
134 | $settingsBuilder->putConfigValues( [ |
135 | MainConfigNames::UseDatabaseMessages => false, |
136 | MainConfigNames::LocalisationCacheConf => [ 'storeClass' => LCStoreNull::class ], |
137 | ] ); |
138 | } |
139 | } |
140 | |
141 | public static function disableInterwikis( $prefix, &$data ) { |
142 | # Title::newFromText will check on each namespaced article if it's an interwiki. |
143 | # We always answer that it is not. |
144 | |
145 | return false; |
146 | } |
147 | |
148 | /** |
149 | * Callback function for each revision, child classes should override |
150 | * processRevision instead. |
151 | * @param WikiRevision $rev |
152 | */ |
153 | public function handleRevision( $rev ) { |
154 | $title = $rev->getTitle(); |
155 | if ( !$title ) { |
156 | $this->error( "Got bogus revision with null title!" ); |
157 | |
158 | return; |
159 | } |
160 | |
161 | $this->count++; |
162 | if ( $this->from !== false ) { |
163 | if ( $this->from != $title ) { |
164 | return; |
165 | } |
166 | $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" ); |
167 | |
168 | $this->count = 1; |
169 | $this->from = null; |
170 | } |
171 | |
172 | $this->processRevision( $rev ); |
173 | } |
174 | |
175 | /** |
176 | * Stub function for processing additional options |
177 | */ |
178 | public function checkOptions() { |
179 | } |
180 | |
181 | /** |
182 | * Stub function for giving data about what was computed |
183 | */ |
184 | public function conclusions() { |
185 | } |
186 | |
187 | /** |
188 | * Core function which does whatever the maintenance script is designed to do |
189 | * |
190 | * @param WikiRevision $rev |
191 | */ |
192 | abstract public function processRevision( WikiRevision $rev ); |
193 | } |
194 | |
195 | /** |
196 | * Maintenance script that runs a regex in the revisions from a dump. |
197 | * |
198 | * @ingroup Maintenance |
199 | */ |
200 | class SearchDump extends DumpIterator { |
201 | |
202 | public function __construct() { |
203 | parent::__construct(); |
204 | $this->addDescription( 'Runs a regex in the revisions from a dump' ); |
205 | $this->addOption( 'regex', 'Searching regex', true, true ); |
206 | } |
207 | |
208 | public function getDbType() { |
209 | return Maintenance::DB_NONE; |
210 | } |
211 | |
212 | /** |
213 | * @param WikiRevision $rev |
214 | */ |
215 | public function processRevision( WikiRevision $rev ) { |
216 | if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) { |
217 | $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" ); |
218 | } |
219 | } |
220 | } |
221 | |
222 | // @codeCoverageIgnoreStart |
223 | $maintClass = SearchDump::class; |
224 | require_once RUN_MAINTENANCE_IF_MAIN; |
225 | // @codeCoverageIgnoreEnd |