Merge branch 'km/maint-doc-git-reset' into maint
[gitweb.git] / contrib / mw-to-git / git-remote-mediawiki
index dc137409b1aff48644b2b6ac30ceca3847189fb8..094129de09ca92643d98f34f20a35479089bcbcb 100755 (executable)
@@ -9,20 +9,7 @@
 # License: GPL v2 or later
 
 # Gateway between Git and MediaWiki.
-#   https://github.com/Bibzball/Git-Mediawiki/wiki
-#
-# Known limitations:
-#
-# - Several strategies are provided to fetch modifications from the
-#   wiki, but no automatic heuristics is provided, the user has
-#   to understand and chose which strategy is appropriate for him.
-#
-# - Git renames could be turned into MediaWiki renames (see TODO
-#   below)
-#
-# - No way to import "one page, and all pages included in it"
-#
-# - Multiple remote MediaWikis have not been very well tested.
+# Documentation & bugtracker: https://github.com/moy/Git-Mediawiki/
 
 use strict;
 use MediaWiki::API;
@@ -171,32 +158,6 @@ while (<STDIN>) {
 
 ## credential API management (generic functions)
 
-sub credential_from_url {
-       my $url = shift;
-       my $parsed = URI->new($url);
-       my %credential;
-
-       if ($parsed->scheme) {
-               $credential{protocol} = $parsed->scheme;
-       }
-       if ($parsed->host) {
-               $credential{host} = $parsed->host;
-       }
-       if ($parsed->path) {
-               $credential{path} = $parsed->path;
-       }
-       if ($parsed->userinfo) {
-               if ($parsed->userinfo =~ /([^:]*):(.*)/) {
-                       $credential{username} = $1;
-                       $credential{password} = $2;
-               } else {
-                       $credential{username} = $parsed->userinfo;
-               }
-       }
-
-       return %credential;
-}
-
 sub credential_read {
        my %credential;
        my $reader = shift;
@@ -214,8 +175,10 @@ sub credential_read {
 sub credential_write {
        my $credential = shift;
        my $writer = shift;
+       # url overwrites other fields, so it must come first
+       print $writer "url=$credential->{url}\n" if exists $credential->{url};
        while (my ($key, $value) = each(%$credential) ) {
-               if ($value) {
+               if (length $value && $key ne 'url') {
                        print $writer "$key=$value\n";
                }
        }
@@ -254,7 +217,7 @@ sub mw_connect_maybe {
        $mediawiki = MediaWiki::API->new;
        $mediawiki->{config}->{api_url} = "$url/api.php";
        if ($wiki_login) {
-               my %credential = credential_from_url($url);
+               my %credential = (url => $url);
                $credential{username} = $wiki_login;
                $credential{password} = $wiki_passwd;
                credential_run("fill", \%credential);
@@ -748,6 +711,14 @@ sub fetch_mw_revisions {
        return ($n, @revisions);
 }
 
+sub fe_escape_path {
+    my $path = shift;
+    $path =~ s/\\/\\\\/g;
+    $path =~ s/"/\\"/g;
+    $path =~ s/\n/\\n/g;
+    return '"' . $path . '"';
+}
+
 sub import_file_revision {
        my $commit = shift;
        my %commit = %{$commit};
@@ -775,15 +746,17 @@ sub import_file_revision {
                print STDOUT "from refs/mediawiki/$remotename/master^0\n";
        }
        if ($content ne DELETED_CONTENT) {
-               print STDOUT "M 644 inline $title.mw\n";
+               print STDOUT "M 644 inline " .
+                   fe_escape_path($title . ".mw") . "\n";
                literal_data($content);
                if (%mediafile) {
-                       print STDOUT "M 644 inline $mediafile{title}\n";
+                       print STDOUT "M 644 inline "
+                           . fe_escape_path($mediafile{title}) . "\n";
                        literal_data_raw($mediafile{content});
                }
                print STDOUT "\n\n";
        } else {
-               print STDOUT "D $title.mw\n";
+               print STDOUT "D " . fe_escape_path($title . ".mw") . "\n";
        }
 
        # mediawiki revision number in the git note