git-remote-mediawiki: make mediafiles export optional
It is possible to use git-remote-mediawiki on a tree with both .mw files and other files. Before git-remote-mediawiki learnt how to export mediafiles, such mixed trees allowed the user to maintain both the wiki and other files for the same project in the same repository. With the newly added support for exporting mediafiles, pushing such mixed trees would upload unrelated files as mediafiles, which may not be desired. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matthieu Moy committed
Jul 16, 2012 at 21:46 UTC
2045e293eb69204ac06910c7570718efb09b7059
1 file changed
+22
-2
contrib/mw-to-git/git-remote-mediawiki
+22
-2
@@ -66,11 +66,16 @@ chomp(@tracked_pages);
66
my @tracked_categories = split(/[ \n]/, run_git("config --get-all remote.". $remotename .".categories"));
67
chomp(@tracked_categories);
68
69
-# Import media files too.
69
+# Import media files on pull
70
my $import_media = run_git("config --get --bool remote.". $remotename .".mediaimport");
71
chomp($import_media);
72
$import_media = ($import_media eq "true");
73
74
+# Export media files on push
75
+my $export_media = run_git("config --get --bool remote.". $remotename .".mediaexport");
76
+chomp($export_media);
77
+$export_media = !($export_media eq "false");
78
+
79
my $wiki_login = run_git("config --get remote.". $remotename .".mwLogin");
80
# Note: mwPassword is discourraged. Use the credential system instead.
81
my $wiki_passwd = run_git("config --get remote.". $remotename .".mwPassword");
@@ -1068,6 +1073,11 @@ sub mw_push_file {
1073
$extension = "";
1074
}
1075
if ($extension eq "mw") {
1076
+ my $ns = get_mw_namespace_id_for_page($complete_file_name);
1077
+ if ($ns && $ns == get_mw_namespace_id("File") && (!$export_media)) {
1078
+ print STDERR "Ignoring media file related page: $complete_file_name\n";
1079
+ return ($oldrevid, "ok");
1080
+ }
1081
my $file_content;
1082
if ($page_deleted) {
1083
# Deleting a page usually requires
@@ -1107,10 +1117,12 @@ sub mw_push_file {
1117
}
1118
$newrevid = $result->{edit}->{newrevid};
1119
print STDERR "Pushed file: $new_sha1 - $title\n";
1110
- } else {
1120
+ } elsif ($export_media) {
1121
$newrevid = mw_upload_file($complete_file_name, $new_sha1,
1122
$extension, $page_deleted,
1123
$summary);
1124
+ } else {
1125
+ print STDERR "Ignoring media file $title\n";
1126
}
1127
$newrevid = ($newrevid or $oldrevid);
1128
return ($newrevid, "ok");
@@ -1328,3 +1340,11 @@ sub get_mw_namespace_id {
1340
die "No such namespace $name on MediaWiki.";
1341
}
1342
}
1343
+
1344
+sub get_mw_namespace_id_for_page {
1345
+ if (my ($namespace) = $_[0] =~ /^([^:]*):/) {
1346
+ return get_mw_namespace_id($namespace);
1347
+ } else {
1348
+ return;
1349
+ }
1350
+}