git-remote-mediawiki: improve support for non-English Wikis
Mediafiles can live in namespaces with names different from Image and File. While at it, rework the code to make it simpler and easier to read. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matthieu Moy committed
Jul 4, 2012 at 14:53 UTC
6df7e0df0912fe88862287bf9871c50dafe722e6
1 file changed
+61
-79
contrib/mw-to-git/git-remote-mediawiki
+61
-79
@@ -33,7 +33,6 @@
33
use strict;
34
use MediaWiki::API;
35
use DateTime::Format::ISO8601;
36
-use FileHandle;
36
37
# By default, use UTF-8 to communicate with Git and the user
38
binmode STDERR, ":utf8";
@@ -90,9 +89,6 @@ my $shallow_import = run_git("config --get --bool remote.". $remotename .".shall
89
chomp($shallow_import);
90
$shallow_import = ($shallow_import eq "true");
91
93
-# Cache for MediaWiki namespace ids.
94
-my %namespace_id;
95
-
92
# Dumb push: don't update notes and mediawiki ref to reflect the last push.
93
#
94
# Configurable with mediawiki.dumbPush, or per-remote with
@@ -267,7 +263,13 @@ sub mw_connect_maybe {
263
## Functions for listing pages on the remote wiki
264
sub get_mw_tracked_pages {
265
my $pages = shift;
270
- my @some_pages = @tracked_pages;
266
+ get_mw_page_list(\@tracked_pages, $pages);
267
+}
268
+
269
+sub get_mw_page_list {
270
+ my $page_list = shift;
271
+ my $pages = shift;
272
+ my @some_pages = @$page_list;
273
while (@some_pages) {
274
my $last = 50;
275
if ($#some_pages < $last) {
@@ -443,17 +445,17 @@ sub get_linked_mediafiles {
445
my $result = $mediawiki->api($query);
446
447
while (my ($id, $page) = each(%{$result->{query}->{pages}})) {
446
- my @titles;
448
+ my @media_titles;
449
if (defined($page->{links})) {
450
my @link_titles = map $_->{title}, @{$page->{links}};
449
- push(@titles, @link_titles);
451
+ push(@media_titles, @link_titles);
452
}
453
if (defined($page->{images})) {
454
my @image_titles = map $_->{title}, @{$page->{images}};
453
- push(@titles, @image_titles);
455
+ push(@media_titles, @image_titles);
456
}
455
- if (@titles) {
456
- get_mw_first_pages(\@titles, \%{$pages});
457
+ if (@media_titles) {
458
+ get_mw_page_list(\@media_titles, $pages);
459
}
460
}
461
@@ -463,16 +465,16 @@ sub get_linked_mediafiles {
465
466
sub get_mw_mediafile_for_page_revision {
467
# Name of the file on Wiki, with the prefix.
466
- my $mw_filename = shift;
468
+ my $filename = shift;
469
my $timestamp = shift;
470
my %mediafile;
471
470
- # Search if on MediaWiki exists a media file with given
471
- # timestamp. In that case download the file.
472
+ # Search if on a media file with given timestamp exists on
473
+ # MediaWiki. In that case download the file.
474
my $query = {
475
action => 'query',
476
prop => 'imageinfo',
475
- titles => $mw_filename,
477
+ titles => "File:" . $filename,
478
iistart => $timestamp,
479
iiend => $timestamp,
480
iiprop => 'timestamp|archivename|url',
@@ -480,62 +482,33 @@ sub get_mw_mediafile_for_page_revision {
482
};
483
my $result = $mediawiki->api($query);
484
483
- my ($fileid, $file) = each ( %{$result->{query}->{pages}} );
485
+ my ($fileid, $file) = each( %{$result->{query}->{pages}} );
486
# If not defined it means there is no revision of the file for
487
# given timestamp.
488
if (defined($file->{imageinfo})) {
487
- # Get real name of media file.
488
- my $filename;
489
- if (index($mw_filename, 'File:') == 0) {
490
- $filename = substr $mw_filename, 5;
491
- } else {
492
- $filename = substr $mw_filename, 6;
493
- }
489
$mediafile{title} = $filename;
490
491
my $fileinfo = pop(@{$file->{imageinfo}});
492
$mediafile{timestamp} = $fileinfo->{timestamp};
498
- # If this is an old version of the file, the file has to be
499
- # obtained from the archive. Otherwise it can be downloaded
500
- # by MediaWiki API download() function.
501
- if (defined($fileinfo->{archivename})) {
502
- $mediafile{content} = download_mw_mediafile_from_archive($fileinfo->{url});
503
- } else {
504
- $mediafile{content} = download_mw_mediafile($mw_filename);
505
- }
493
+ # Mediawiki::API's download function doesn't support https URLs
494
+ # and can't download old versions of files.
495
+ print STDERR "\tDownloading file $mediafile{title}, version $mediafile{timestamp}\n";
496
+ $mediafile{content} = download_mw_mediafile($fileinfo->{url});
497
}
498
return %mediafile;
499
}
500
510
-sub download_mw_mediafile_from_archive {
501
+sub download_mw_mediafile {
502
my $url = shift;
512
- my $file;
503
514
- my $ua = LWP::UserAgent->new;
515
- my $response = $ua->get($url);
516
- if ($response->code) {
517
- $file = $response->decoded_content;
504
+ my $response = $mediawiki->{ua}->get($url);
505
+ if ($response->code == 200) {
506
+ return $response->decoded_content;
507
} else {
519
- print STDERR "Error downloading a file from archive.\n";
520
- }
521
-
522
- return $file;
523
-}
524
-
525
-sub download_mw_mediafile {
526
- my $filename = shift;
527
-
528
- $mediawiki->{config}->{files_url} = $url;
529
-
530
- my $file_content = $mediawiki->download( { title => $filename } );
531
- if (!defined($file_content)) {
532
- print STDERR "\tFile \'$filename\' could not be downloaded.\n";
533
- exit 1;
534
- } elsif ($file_content eq "") {
535
- print STDERR "\tFile \'$filename\' does not exist on the wiki.\n";
508
+ print STDERR "Error downloading mediafile from :\n";
509
+ print STDERR "URL: $url\n";
510
+ print STDERR "Server response: " . $response->code . " " . $response->message . "\n";
511
exit 1;
537
- } else {
538
- return $file_content;
512
}
513
}
514
@@ -878,24 +851,16 @@ sub mw_import_ref {
851
$commit{date} = DateTime::Format::ISO8601->parse_datetime($last_timestamp);
852
853
# Differentiates classic pages and media files.
881
- my @prefix = split(":", $page_title);
882
-
854
+ my ($namespace, $filename) = $page_title =~ /^([^:]*):(.*)$/;
855
my %mediafile;
884
- if ($prefix[0] eq "File" || $prefix[0] eq "Image") {
885
- # The name of the file is the same as the media page.
886
- my $filename = $page_title;
856
+ if ($namespace && get_mw_namespace_id($namespace) == get_mw_namespace_id("File")) {
857
%mediafile = get_mw_mediafile_for_page_revision($filename, $rev->{timestamp});
858
}
859
# If this is a revision of the media page for new version
860
# of a file do one common commit for both file and media page.
861
# Else do commit only for that page.
862
print STDERR "$n/", scalar(@revisions), ": Revision #$pagerevid->{revid} of $commit{title}\n";
893
- if (%mediafile) {
894
- print STDERR "\tDownloading file $mediafile{title}, version $mediafile{timestamp}\n";
895
- import_file_revision(\%commit, ($fetch_from == 1), $n, \%mediafile);
896
- } else {
897
- import_file_revision(\%commit, ($fetch_from == 1), $n);
898
- }
863
+ import_file_revision(\%commit, ($fetch_from == 1), $n, \%mediafile);
864
}
865
866
if ($fetch_from == 1 && $n == 0) {
@@ -1201,28 +1166,35 @@ sub get_allowed_file_extensions {
1166
return %hashFile;
1167
}
1168
1169
+# In memory cache for MediaWiki namespace ids.
1170
+my %namespace_id;
1171
+
1172
+# Namespaces whose id is cached in the configuration file
1173
+# (to avoid duplicates)
1174
+my %cached_mw_namespace_id;
1175
+
1176
# Return MediaWiki id for a canonical namespace name.
1177
# Ex.: "File", "Project".
1206
-# Looks for the namespace id in the local configuration
1207
-# variables, if it is not found asks MW API.
1178
sub get_mw_namespace_id {
1179
mw_connect_maybe();
1180
my $name = shift;
1181
1182
if (!exists $namespace_id{$name}) {
1183
# Look at configuration file, if the record for that namespace is
1214
- # already stored. Namespaces are stored in form:
1184
+ # already cached. Namespaces are stored in form:
1185
# "Name_of_namespace:Id_namespace", ex.: "File:6".
1186
my @temp = split(/[ \n]/, run_git("config --get-all remote."
1217
- . $remotename .".namespaces"));
1187
+ . $remotename .".namespaceCache"));
1188
chomp(@temp);
1189
foreach my $ns (@temp) {
1220
- my ($n, $s) = split(/:/, $ns);
1221
- $namespace_id{$n} = $s;
1190
+ my ($n, $id) = split(/:/, $ns);
1191
+ $namespace_id{$n} = $id;
1192
+ $cached_mw_namespace_id{$n} = 1;
1193
}
1194
}
1195
1196
if (!exists $namespace_id{$name}) {
1197
+ print STDERR "Namespace $name not found in cache, querying the wiki ...\n";
1198
# NS not found => get namespace id from MW and store it in
1199
# configuration file.
1200
my $query = {
@@ -1233,16 +1205,26 @@ sub get_mw_namespace_id {
1205
my $result = $mediawiki->api($query);
1206
1207
while (my ($id, $ns) = each(%{$result->{query}->{namespaces}})) {
1236
- if (defined($ns->{canonical}) && ($ns->{canonical} eq $name)) {
1237
- run_git("config --add remote.". $remotename
1238
- .".namespaces ". $name .":". $ns->{id});
1239
- $namespace_id{$name} = $ns->{id};
1240
- }
1208
+ if (defined($ns->{id}) && defined($ns->{canonical})) {
1209
+ $namespace_id{$ns->{canonical}} = $ns->{id};
1210
+ if ($ns->{'*'}) {
1211
+ # alias (e.g. french Fichier: as alias for canonical File:)
1212
+ $namespace_id{$ns->{'*'}} = $ns->{id};
1213
+ }
1214
+ }
1215
}
1216
}
1217
1244
- if (exists $namespace_id{$name}) {
1245
- return $namespace_id{$name};
1218
+ my $id = $namespace_id{$name};
1219
+
1220
+ if (defined $id) {
1221
+ # Store explicitely requested namespaces on disk
1222
+ if (!exists $cached_mw_namespace_id{$name}) {
1223
+ run_git("config --add remote.". $remotename
1224
+ .".namespaceCache \"". $name .":". $id ."\"");
1225
+ $cached_mw_namespace_id{$name} = 1;
1226
+ }
1227
+ return $id;
1228
} else {
1229
die "No such namespace $name on MediaWiki.";
1230
}