forked from wikimedia/mediawiki-tools-grabbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrabText.php
344 lines (306 loc) · 10.4 KB
/
grabText.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* Maintenance script to grab text from a wiki and import it to another wiki.
* Translated from Edward Chernenko's Perl version (text.pl).
*
* @file
* @ingroup Maintenance
* @author Jack Phoenix <[email protected]>
* @author Calimonious the Estrange
* @author Jesús Martínez <[email protected]>
* @version 1.1
* @date 5 August 2019
*/
use MediaWiki\MediaWikiServices;
require_once 'includes/TextGrabber.php';
class GrabText extends TextGrabber {
public function __construct() {
parent::__construct();
$this->mDescription = "Grab text from an external wiki and import it into one of ours.\nDon't use this on a large wiki unless you absolutely must; it will be incredibly slow.";
$this->addOption( 'start', 'Page at which to start, useful if the script stopped at this point', false, true );
$this->addOption( 'namespaces', 'Pipe-separated namespaces (ID) to grab. Defaults to all namespaces', false, true );
}
public function execute() {
parent::execute();
$this->output( "\n" );
# Get all pages as a list, start by getting namespace numbers...
$this->output( "Retrieving namespaces list...\n" );
$params = [
'meta' => 'siteinfo',
'siprop' => 'namespaces|statistics|namespacealiases'
];
$result = $this->bot->query( $params );
$siteinfo = $result['query'];
# No data - bail out early
if ( empty( $siteinfo ) ) {
$this->fatalError( 'No siteinfo data found' );
}
$textNamespaces = [];
if ( $this->hasOption( 'namespaces' ) ) {
$textNamespaces = explode( '|', $this->getOption( 'namespaces', '' ) );
$grabFromAllNamespaces = false;
} else {
$grabFromAllNamespaces = true;
foreach ( array_keys( $siteinfo['namespaces'] ) as $ns ) {
# Ignore special
if ( $ns >= 0 ) {
$textNamespaces[] = $ns;
}
}
}
if ( !$textNamespaces ) {
$this->fatalError( 'Got no namespaces' );
}
if ( $grabFromAllNamespaces ) {
# Get list of live pages from namespaces and continue from there
$pageCount = $siteinfo['statistics']['pages'];
$this->output( "Generating page list from all namespaces - $pageCount expected...\n" );
} else {
$this->output( sprintf( "Generating page list from %s namespaces...\n", count( $textNamespaces ) ) );
}
$start = $this->getOption( 'start' );
if ( $start ) {
$title = Title::newFromText( $start );
if ( is_null( $title ) ) {
$this->fatalError( 'Invalid title provided for the start parameter' );
}
$this->output( sprintf( "Trying to resume import from page %s\n", $title ) );
}
$pageCount = 0;
foreach ( $textNamespaces as $ns ) {
$continueTitle = null;
if ( isset( $title ) && !is_null( $title ) ) {
if ( $title->getNamespace() === (int)$ns ) {
# The apfrom parameter doesn't have namespace!!
$continueTitle = $title->getText();
$title = null;
} else {
continue;
}
}
$pageCount += $this->processPagesFromNamespace( (int)$ns, $continueTitle );
}
$this->output( "\nDone - found $pageCount total pages.\n" );
# Done.
}
/**
* Grabs all pages from a given namespace
*
* @param int $ns Namespace to process.
* @param string $continueTitle Title to start from (optional).
* @return int Number of pages processed.
*/
function processPagesFromNamespace( $ns, $continueTitle = null ) {
$this->output( "Processing pages from namespace $ns...\n" );
$doneCount = 0;
$nsPageCount = 0;
$more = true;
$params = [
'generator' => 'allpages',
'gaplimit' => 'max',
'prop' => 'info',
'inprop' => 'protection',
'gapnamespace' => $ns
];
if ( $continueTitle ) {
$params['gapfrom'] = $continueTitle;
}
do {
$result = $this->bot->query( $params );
# Skip empty namespaces
if ( isset( $result['query'] ) ) {
$pages = $result['query']['pages'];
$resultsCount = 0;
foreach ( $pages as $page ) {
$this->processPage( $page );
$doneCount++;
if ( $doneCount % 500 === 0 ) {
$this->output( "$doneCount\n" );
}
$resultsCount++;
}
$nsPageCount += $resultsCount;
# Add continuation parameters
if ( isset( $result['query-continue'] ) && isset( $result['query-continue']['allpages'] ) ) {
$params = array_merge( $params, $result['query-continue']['allpages'] );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
} else {
$more = false;
}
} else {
$more = false;
}
} while ( $more );
$this->output( "$nsPageCount pages found in namespace $ns.\n" );
return $nsPageCount;
}
/**
* Handle an individual page.
*
* @param array $page: Array retrieved from the API, containing pageid,
* page title, namespace, protection status and more...
*/
function processPage( $page ) {
$pageID = $page['pageid'];
$this->output( "Processing page id $pageID...\n" );
$params = [
'prop' => 'info|revisions',
'rvlimit' => 'max',
'rvprop' => 'ids|flags|timestamp|user|userid|comment|content|tags|contentmodel',
'rvdir' => 'newer',
'rvend' => wfTimestamp( TS_ISO_8601, $this->endDate )
];
$params['pageids'] = $pageID;
$result = $this->bot->query( $params );
if ( !$result || isset( $result['error'] ) ) {
$this->fatalError( "Error getting revision information from API for page id $pageID." );
return;
}
$info_pages = array_values( $result['query']['pages'] );
if ( isset( $info_pages[0]['missing'] ) ) {
$this->output( "Page id $pageID not found.\n" );
return;
}
$page_e = [
'namespace' => null,
'title' => null,
'is_redirect' => 0,
'is_new' => 0,
'random' => wfRandom(),
'touched' => wfTimestampNow(),
'len' => 0,
'content_model' => null
];
# Trim and convert displayed title to database page title
# Get it from the returned value from api
$page_e['namespace'] = $info_pages[0]['ns'];
$page_e['title'] = $this->sanitiseTitle( $info_pages[0]['ns'], $info_pages[0]['title'] );
# We kind of need this to resume...
$this->output( "Title: {$page_e['title']} in namespace {$page_e['namespace']}\n" );
$title = Title::makeTitle( $page_e['namespace'], $page_e['title'] );
# Get other information from api info
$page_e['is_redirect'] = ( isset( $info_pages[0]['redirect'] ) ? 1 : 0 );
$page_e['is_new'] = ( isset( $info_pages[0]['new'] ) ? 1 : 0 );
$page_e['len'] = $info_pages[0]['length'];
$page_e['latest'] = $info_pages[0]['lastrevid'];
$defaultModel = null;
if ( isset( $info_pages[0]['contentmodel'] ) ) {
# This would be the most accurate way of getting the content model for a page.
# However it calls hooks and can be incredibly slow or cause errors
#$defaultModel = ContentHandler::getDefaultModelFor( $title );
$defaultModel = MediaWikiServices::getInstance()->getNamespaceInfo()->
getNamespaceContentModel( $info_pages[0]['ns'] ) || CONTENT_MODEL_WIKITEXT;
# Set only if not the default content model
if ( $defaultModel != $info_pages[0]['contentmodel'] ) {
$page_e['content_model'] = $info_pages[0]['contentmodel'];
}
}
# Check if page is present
$pageRow = $this->dbw->selectRow(
'page',
[ 'page_namespace', 'page_title' ],
[ 'page_id' => $pageID ],
__METHOD__
);
# If page is not present, check if title is present, because we can't insert
# a duplicate title. That would mean the page was moved leaving a redirect but
# we haven't processed the move yet
# Also, check the destination title before moving the page
if ( $pageRow === false ||
$pageRow->page_namespace != $page_e['namespace'] ||
$pageRow->page_title != $page_e['title']
) {
$conflictingPageID = $this->getPageID( $page_e['namespace'], $page_e['title'] );
if ( $conflictingPageID ) {
# Whoops...
$this->resolveConflictingTitle( $page_e['namespace'], $page_e['title'], $pageID, $conflictingPageID );
}
}
# Update page_restrictions (only if requested)
if ( isset( $page['protection'] ) ) {
$this->output( "Setting page_restrictions on page_id $pageID.\n" );
# Delete first any existing protection
$this->dbw->delete(
'page_restrictions',
[ 'pr_page' => $pageID ],
__METHOD__
);
# insert current restrictions
foreach ( $page['protection'] as $prot ) {
# Skip protections inherited from cascade protections
if ( !isset( $prot['source'] ) ) {
$expiry = $prot['expiry'] == 'infinity' ? 'infinity' : wfTimestamp( TS_MW, $prot['expiry'] );
$this->dbw->insert(
'page_restrictions',
[
'pr_page' => $pageID,
'pr_type' => $prot['type'],
'pr_level' => $prot['level'],
'pr_cascade' => (int)isset( $prot['cascade'] ),
'pr_expiry' => $expiry
],
__METHOD__
);
}
}
}
$revisionsProcessed = false;
while ( true ) {
foreach ( $info_pages[0]['revisions'] as $revision ) {
$revisionsProcessed = $this->processRevision( $revision, $pageID, $title ) || $revisionsProcessed;
}
# Add continuation parameters
if ( isset( $result['query-continue'] ) && isset( $result['query-continue']['revisions'] ) ) {
$params = array_merge( $params, $result['query-continue']['revisions'] );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
} else {
break;
}
$result = $this->bot->query( $params );
if ( !$result || isset( $result['error'] ) ) {
$this->fatalError( "Error getting revision information from API for page id $pageID." );
return;
}
$info_pages = array_values( $result['query']['pages'] );
}
if ( !$revisionsProcessed ) {
# We already processed the page before? page doesn't need updating, then
return;
}
$insert_fields = [
'page_namespace' => $page_e['namespace'],
'page_title' => $page_e['title'],
'page_is_redirect' => $page_e['is_redirect'],
'page_is_new' => $page_e['is_new'],
'page_random' => $page_e['random'],
'page_touched' => $page_e['touched'],
'page_latest' => $page_e['latest'],
'page_len' => $page_e['len'],
'page_content_model' => $page_e['content_model']
];
if ( $pageRow === false ) {
# insert if not present
$this->output( "Inserting page entry $pageID\n" );
$insert_fields['page_id'] = $pageID;
$this->dbw->insert(
'page',
$insert_fields,
__METHOD__
);
} else {
# update existing
$this->output( "Updating page entry $pageID\n" );
$this->dbw->update(
'page',
$insert_fields,
[ 'page_id' => $pageID ],
__METHOD__
);
}
$this->dbw->commit();
}
}
$maintClass = 'GrabText';
require_once RUN_MAINTENANCE_IF_MAIN;