Skip to content

Commit 8afb990

Browse files
committed
Merge branch 'ab/mediawiki-namespace' into pu
* ab/mediawiki-namespace: remote-mediawiki: show progress while fetching namespaces remote-mediawiki: process namespaces in order remote-mediawiki: support fetching from (Main) namespace remote-mediawiki: skip virtual namespaces remote-mediawiki: show known namespace choices on failure remote-mediawiki: allow fetching namespaces with spaces remote-mediawiki: add namespace support
2 parents a52213e + 2e7708b commit 8afb990

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

contrib/mw-to-git/git-remote-mediawiki.perl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
my @tracked_categories = split(/[ \n]/, run_git("config --get-all remote.${remotename}.categories"));
6464
chomp(@tracked_categories);
6565

66+
# Just like @tracked_categories, but for MediaUncyclo namespaces.
67+
my @tracked_namespaces = split(/[ \n]/, run_git("config --get-all remote.${remotename}.namespaces"));
68+
for (@tracked_namespaces) { s/_/ /g; }
69+
chomp(@tracked_namespaces);
70+
6671
# Import media files on pull
6772
my $import_media = run_git("config --get --bool remote.${remotename}.mediaimport");
6873
chomp($import_media);
@@ -256,6 +261,31 @@ sub get_mw_tracked_categories {
256261
return;
257262
}
258263

264+
sub get_mw_tracked_namespaces {
265+
my $pages = shift;
266+
foreach my $local_namespace (sort @tracked_namespaces) {
267+
my ($namespace_id, $mw_pages);
268+
if ($local_namespace eq "(Main)") {
269+
$namespace_id = 0;
270+
} else {
271+
$namespace_id = get_mw_namespace_id($local_namespace);
272+
}
273+
next if $namespace_id < 0; # virtual namespaces don't support allpages
274+
$mw_pages = $mediawiki->list( {
275+
action => 'query',
276+
list => 'allpages',
277+
apnamespace => $namespace_id,
278+
aplimit => 'max' } )
279+
|| die $mediawiki->{error}->{code} . ': '
280+
. $mediawiki->{error}->{details} . "\n";
281+
print {*STDERR} "$#{$mw_pages} found in namespace $local_namespace ($namespace_id)\n";
282+
foreach my $page (@{$mw_pages}) {
283+
$pages->{$page->{title}} = $page;
284+
}
285+
}
286+
return;
287+
}
288+
259289
sub get_mw_all_pages {
260290
my $pages = shift;
261291
# No user-provided list, get the list of pages from the API.
@@ -319,6 +349,10 @@ sub get_mw_pages {
319349
$user_defined = 1;
320350
get_mw_tracked_categories(\%pages);
321351
}
352+
if (@tracked_namespaces) {
353+
$user_defined = 1;
354+
get_mw_tracked_namespaces(\%pages);
355+
}
322356
if (!$user_defined) {
323357
get_mw_all_pages(\%pages);
324358
}
@@ -1308,7 +1342,9 @@ sub get_mw_namespace_id {
13081342
my $id;
13091343

13101344
if (!defined $ns) {
1311-
print {*STDERR} "No such namespace ${name} on MediaUncyclo.\n";
1345+
my @namespaces = sort keys %namespace_id;
1346+
for (@namespaces) { s/ /_/g; }
1347+
print {*STDERR} "No such namespace ${name} on MediaUncyclo, known namespaces: @namespaces\n";
13121348
$ns = {is_namespace => 0};
13131349
$namespace_id{$name} = $ns;
13141350
}

0 commit comments

Comments
 (0)