git-remote-mediawiki: allow page names with a ':'

Traditionnally, pages named Foo:Bar are page 'Bar' in namespace 'Foo'. However, it is also possible to call a page Foo:Bar if 'Foo' is not a namespace. In this case, the actual name of the page is 'Foo:Bar', in the main namespace. Since we can't tell with only the filename, query the wiki for a namespace 'Foo' in these cases, but deal with the case where no such namespace is found. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Jul 17, 2012 at 16:06 UTC 2272400c93332629e8c34dcf3b1207159352e539
2 files changed +53 -16
contrib/mw-to-git/git-remote-mediawiki
+33 -16
@@ -968,8 +968,11 @@ sub mw_import_revids {
968 # Differentiates classic pages and media files.
969 my ($namespace, $filename) = $page_title =~ /^([^:]*):(.*)$/;
970 my %mediafile;
971 - if ($namespace && get_mw_namespace_id($namespace) == get_mw_namespace_id("File")) {
972 - %mediafile = get_mw_mediafile_for_page_revision($filename, $rev->{timestamp});
971 + if ($namespace) {
972 + my $id = get_mw_namespace_id($namespace);
973 + if ($id && $id == get_mw_namespace_id("File")) {
974 + %mediafile = get_mw_mediafile_for_page_revision($filename, $rev->{timestamp});
975 + }
976 }
977 # If this is a revision of the media page for new version
978 # of a file do one common commit for both file and media page.
@@ -1319,7 +1322,11 @@ sub get_mw_namespace_id {
1322 chomp(@temp);
1323 foreach my $ns (@temp) {
1324 my ($n, $id) = split(/:/, $ns);
1322 - $namespace_id{$n} = $id;
1325 + if ($id eq 'notANameSpace') {
1326 + $namespace_id{$n} = {is_namespace => 0};
1327 + } else {
1328 + $namespace_id{$n} = {is_namespace => 1, id => $id};
1329 + }
1330 $cached_mw_namespace_id{$n} = 1;
1331 }
1332 }
@@ -1337,28 +1344,38 @@ sub get_mw_namespace_id {
1344
1345 while (my ($id, $ns) = each(%{$result->{query}->{namespaces}})) {
1346 if (defined($ns->{id}) && defined($ns->{canonical})) {
1340 - $namespace_id{$ns->{canonical}} = $ns->{id};
1347 + $namespace_id{$ns->{canonical}} = {is_namespace => 1, id => $ns->{id}};
1348 if ($ns->{'*'}) {
1349 # alias (e.g. french Fichier: as alias for canonical File:)
1343 - $namespace_id{$ns->{'*'}} = $ns->{id};
1350 + $namespace_id{$ns->{'*'}} = {is_namespace => 1, id => $ns->{id}};
1351 }
1352 }
1353 }
1354 }
1355
1349 - my $id = $namespace_id{$name};
1356 + my $ns = $namespace_id{$name};
1357 + my $id;
1358
1351 - if (defined $id) {
1352 - # Store explicitely requested namespaces on disk
1353 - if (!exists $cached_mw_namespace_id{$name}) {
1354 - run_git("config --add remote.". $remotename
1355 - .".namespaceCache \"". $name .":". $id ."\"");
1356 - $cached_mw_namespace_id{$name} = 1;
1357 - }
1358 - return $id;
1359 - } else {
1360 - die "No such namespace $name on MediaWiki.";
1359 + unless (defined $ns) {
1360 + print STDERR "No such namespace $name on MediaWiki.\n";
1361 + $ns = {is_namespace => 0};
1362 + $namespace_id{$name} = $ns;
1363 + }
1364 +
1365 + if ($ns->{is_namespace}) {
1366 + $id = $ns->{id};
1367 + }
1368 +
1369 + # Store "notANameSpace" as special value for inexisting namespaces
1370 + my $store_id = ($id || 'notANameSpace');
1371 +
1372 + # Store explicitely requested namespaces on disk
1373 + if (!exists $cached_mw_namespace_id{$name}) {
1374 + run_git("config --add remote.". $remotename
1375 + .".namespaceCache \"". $name .":". $store_id ."\"");
1376 + $cached_mw_namespace_id{$name} = 1;
1377 }
1378 + return $id;
1379 }
1380
1381 sub get_mw_namespace_id_for_page {
contrib/mw-to-git/t/t9362-mw-to-git-utf8.sh
+20
@@ -169,6 +169,26 @@ test_expect_failure 'special character at the begining of file name from mw to g
169 test_path_is_file mw_dir_11/[char_2
170 '
171
172 +test_expect_success 'Pull page with title containing ":" other than namespace separator' '
173 + wiki_editpage Foo:Bar content false &&
174 + (
175 + cd mw_dir_11 &&
176 + git pull
177 + ) &&
178 + test_path_is_file mw_dir_11/Foo:Bar.mw
179 +'
180 +
181 +test_expect_success 'Push page with title containing ":" other than namespace separator' '
182 + (
183 + cd mw_dir_11 &&
184 + echo content >NotANameSpace:Page.mw &&
185 + git add NotANameSpace:Page.mw &&
186 + git commit -m "add page with colon" &&
187 + git push
188 + ) &&
189 + wiki_page_exist NotANameSpace:Page
190 +'
191 +
192 test_expect_success 'test of correct formating for file name from mw to git' '
193 wiki_reset &&
194 git clone mediawiki::'"$WIKI_URL"' mw_dir_12 &&