git-remote-mediawiki: split get_mw_pages into smaller functions
Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matthieu Moy committed
Jun 27, 2012 at 11:10 UTC
9cb74f3666c86b9d53c661b21d6373eabdbfc796
1 file changed
+62
-44
contrib/mw-to-git/git-remote-mediawiki
+62
-44
@@ -258,6 +258,64 @@ sub mw_connect_maybe {
258
}
259
}
260
261
+## Functions for listing pages on the remote wiki
262
+sub get_mw_tracked_pages {
263
+ my $pages = shift;
264
+ my @some_pages = @tracked_pages;
265
+ while (@some_pages) {
266
+ my $last = 50;
267
+ if ($#some_pages < $last) {
268
+ $last = $#some_pages;
269
+ }
270
+ my @slice = @some_pages[0..$last];
271
+ get_mw_first_pages(\@slice, $pages);
272
+ @some_pages = @some_pages[51..$#some_pages];
273
+ }
274
+}
275
+
276
+sub get_mw_tracked_categories {
277
+ my $pages = shift;
278
+ foreach my $category (@tracked_categories) {
279
+ if (index($category, ':') < 0) {
280
+ # Mediawiki requires the Category
281
+ # prefix, but let's not force the user
282
+ # to specify it.
283
+ $category = "Category:" . $category;
284
+ }
285
+ my $mw_pages = $mediawiki->list( {
286
+ action => 'query',
287
+ list => 'categorymembers',
288
+ cmtitle => $category,
289
+ cmlimit => 'max' } )
290
+ || die $mediawiki->{error}->{code} . ': '
291
+ . $mediawiki->{error}->{details};
292
+ foreach my $page (@{$mw_pages}) {
293
+ $pages->{$page->{title}} = $page;
294
+ }
295
+ }
296
+}
297
+
298
+sub get_mw_all_pages {
299
+ my $pages = shift;
300
+ # No user-provided list, get the list of pages from the API.
301
+ my $mw_pages = $mediawiki->list({
302
+ action => 'query',
303
+ list => 'allpages',
304
+ aplimit => 'max'
305
+ });
306
+ if (!defined($mw_pages)) {
307
+ print STDERR "fatal: could not get the list of wiki pages.\n";
308
+ print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
309
+ print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
310
+ exit 1;
311
+ }
312
+ foreach my $page (@{$mw_pages}) {
313
+ $pages->{$page->{title}} = $page;
314
+ }
315
+}
316
+
317
+# queries the wiki for a set of pages. Meant to be used within a loop
318
+# querying the wiki for slices of page list.
319
sub get_mw_first_pages {
320
my $some_pages = shift;
321
my @some_pages = @{$some_pages};
@@ -286,6 +344,7 @@ sub get_mw_first_pages {
344
}
345
}
346
347
+# Get the list of pages to be fetched according to configuration.
348
sub get_mw_pages {
349
mw_connect_maybe();
350
@@ -295,55 +354,14 @@ sub get_mw_pages {
354
$user_defined = 1;
355
# The user provided a list of pages titles, but we
356
# still need to query the API to get the page IDs.
298
-
299
- my @some_pages = @tracked_pages;
300
- while (@some_pages) {
301
- my $last = 50;
302
- if ($#some_pages < $last) {
303
- $last = $#some_pages;
304
- }
305
- my @slice = @some_pages[0..$last];
306
- get_mw_first_pages(\@slice, \%pages);
307
- @some_pages = @some_pages[51..$#some_pages];
308
- }
357
+ get_mw_tracked_pages(\%pages);
358
}
359
if (@tracked_categories) {
360
$user_defined = 1;
312
- foreach my $category (@tracked_categories) {
313
- if (index($category, ':') < 0) {
314
- # Mediawiki requires the Category
315
- # prefix, but let's not force the user
316
- # to specify it.
317
- $category = "Category:" . $category;
318
- }
319
- my $mw_pages = $mediawiki->list( {
320
- action => 'query',
321
- list => 'categorymembers',
322
- cmtitle => $category,
323
- cmlimit => 'max' } )
324
- || die $mediawiki->{error}->{code} . ': ' . $mediawiki->{error}->{details};
325
- foreach my $page (@{$mw_pages}) {
326
- $pages{$page->{title}} = $page;
327
- }
328
- }
361
+ get_mw_tracked_categories(\%pages);
362
}
363
if (!$user_defined) {
331
- # No user-provided list, get the list of pages from
332
- # the API.
333
- my $mw_pages = $mediawiki->list({
334
- action => 'query',
335
- list => 'allpages',
336
- aplimit => 500,
337
- });
338
- if (!defined($mw_pages)) {
339
- print STDERR "fatal: could not get the list of wiki pages.\n";
340
- print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
341
- print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
342
- exit 1;
343
- }
344
- foreach my $page (@{$mw_pages}) {
345
- $pages{$page->{title}} = $page;
346
- }
364
+ get_mw_all_pages(\%pages);
365
}
366
return values(%pages);
367
}