git-remote-mediawiki: split get_mw_pages into smaller functions
[gitweb.git] / contrib / mw-to-git / git-remote-mediawiki
index 4b6149fb574960a05a12f47509d1913a1e70897e..361dbb1b28569a98e919eeb19d6353130e64fdcb 100755 (executable)
@@ -258,6 +258,64 @@ sub mw_connect_maybe {
        }
 }
 
+## Functions for listing pages on the remote wiki
+sub get_mw_tracked_pages {
+       my $pages = shift;
+       my @some_pages = @tracked_pages;
+       while (@some_pages) {
+               my $last = 50;
+               if ($#some_pages < $last) {
+                       $last = $#some_pages;
+               }
+               my @slice = @some_pages[0..$last];
+               get_mw_first_pages(\@slice, $pages);
+               @some_pages = @some_pages[51..$#some_pages];
+       }
+}
+
+sub get_mw_tracked_categories {
+       my $pages = shift;
+       foreach my $category (@tracked_categories) {
+               if (index($category, ':') < 0) {
+                       # Mediawiki requires the Category
+                       # prefix, but let's not force the user
+                       # to specify it.
+                       $category = "Category:" . $category;
+               }
+               my $mw_pages = $mediawiki->list( {
+                       action => 'query',
+                       list => 'categorymembers',
+                       cmtitle => $category,
+                       cmlimit => 'max' } )
+                       || die $mediawiki->{error}->{code} . ': '
+                               . $mediawiki->{error}->{details};
+               foreach my $page (@{$mw_pages}) {
+                       $pages->{$page->{title}} = $page;
+               }
+       }
+}
+
+sub get_mw_all_pages {
+       my $pages = shift;
+       # No user-provided list, get the list of pages from the API.
+       my $mw_pages = $mediawiki->list({
+               action => 'query',
+               list => 'allpages',
+               aplimit => 'max'
+       });
+       if (!defined($mw_pages)) {
+               print STDERR "fatal: could not get the list of wiki pages.\n";
+               print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
+               print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
+               exit 1;
+       }
+       foreach my $page (@{$mw_pages}) {
+               $pages->{$page->{title}} = $page;
+       }
+}
+
+# queries the wiki for a set of pages. Meant to be used within a loop
+# querying the wiki for slices of page list.
 sub get_mw_first_pages {
        my $some_pages = shift;
        my @some_pages = @{$some_pages};
@@ -286,6 +344,7 @@ sub get_mw_first_pages {
        }
 }
 
+# Get the list of pages to be fetched according to configuration.
 sub get_mw_pages {
        mw_connect_maybe();
 
@@ -295,61 +354,24 @@ sub get_mw_pages {
                $user_defined = 1;
                # The user provided a list of pages titles, but we
                # still need to query the API to get the page IDs.
-
-               my @some_pages = @tracked_pages;
-               while (@some_pages) {
-                       my $last = 50;
-                       if ($#some_pages < $last) {
-                               $last = $#some_pages;
-                       }
-                       my @slice = @some_pages[0..$last];
-                       get_mw_first_pages(\@slice, \%pages);
-                       @some_pages = @some_pages[51..$#some_pages];
-               }
+               get_mw_tracked_pages(\%pages);
        }
        if (@tracked_categories) {
                $user_defined = 1;
-               foreach my $category (@tracked_categories) {
-                       if (index($category, ':') < 0) {
-                               # Mediawiki requires the Category
-                               # prefix, but let's not force the user
-                               # to specify it.
-                               $category = "Category:" . $category;
-                       }
-                       my $mw_pages = $mediawiki->list( {
-                               action => 'query',
-                               list => 'categorymembers',
-                               cmtitle => $category,
-                               cmlimit => 'max' } )
-                           || die $mediawiki->{error}->{code} . ': ' . $mediawiki->{error}->{details};
-                       foreach my $page (@{$mw_pages}) {
-                               $pages{$page->{title}} = $page;
-                       }
-               }
+               get_mw_tracked_categories(\%pages);
        }
        if (!$user_defined) {
-               # No user-provided list, get the list of pages from
-               # the API.
-               my $mw_pages = $mediawiki->list({
-                       action => 'query',
-                       list => 'allpages',
-                       aplimit => 500,
-               });
-               if (!defined($mw_pages)) {
-                       print STDERR "fatal: could not get the list of wiki pages.\n";
-                       print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
-                       print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
-                       exit 1;
-               }
-               foreach my $page (@{$mw_pages}) {
-                       $pages{$page->{title}} = $page;
-               }
+               get_mw_all_pages(\%pages);
        }
        return values(%pages);
 }
 
+# usage: $out = run_git("command args");
+#        $out = run_git("command args", "raw"); # don't interpret output as UTF-8.
 sub run_git {
-       open(my $git, "-|:encoding(UTF-8)", "git " . $_[0]);
+       my $args = shift;
+       my $encoding = (shift || "encoding(UTF-8)");
+       open(my $git, "-|:$encoding", "git " . $args);
        my $res = do { local $/; <$git> };
        close($git);
 
@@ -705,6 +727,63 @@ sub error_non_fast_forward {
        return 0;
 }
 
+sub mw_upload_file {
+       my $complete_file_name = shift;
+       my $new_sha1 = shift;
+       my $extension = shift;
+       my $file_deleted = shift;
+       my $summary = shift;
+       my $newrevid;
+       my $path = "File:" . $complete_file_name;
+       my %hashFiles = get_allowed_file_extensions();
+       if (!exists($hashFiles{$extension})) {
+               print STDERR "$complete_file_name is not a permitted file on this wiki.\n";
+               print STDERR "Check the configuration of file uploads in your mediawiki.\n";
+               return $newrevid;
+       }
+       # Deleting and uploading a file requires a priviledged user
+       if ($file_deleted) {
+               mw_connect_maybe();
+               my $query = {
+                       action => 'delete',
+                       title => $path,
+                       reason => $summary
+               };
+               if (!$mediawiki->edit($query)) {
+                       print STDERR "Failed to delete file on remote wiki\n";
+                       print STDERR "Check your permissions on the remote site. Error code:\n";
+                       print STDERR $mediawiki->{error}->{code} . ':' . $mediawiki->{error}->{details};
+                       exit 1;
+               }
+       } else {
+               # Don't let perl try to interpret file content as UTF-8 => use "raw"
+               my $content = run_git("cat-file blob $new_sha1", "raw");
+               if ($content ne "") {
+                       mw_connect_maybe();
+                       $mediawiki->{config}->{upload_url} =
+                               "$url/index.php/Special:Upload";
+                       $mediawiki->edit({
+                               action => 'upload',
+                               filename => $complete_file_name,
+                               comment => $summary,
+                               file => [undef,
+                                        $complete_file_name,
+                                        Content => $content],
+                               ignorewarnings => 1,
+                       }, {
+                               skip_encoding => 1
+                       } ) || die $mediawiki->{error}->{code} . ':'
+                                . $mediawiki->{error}->{details};
+                       my $last_file_page = $mediawiki->get_page({title => $path});
+                       $newrevid = $last_file_page->{revid};
+                       print STDERR "Pushed file: $new_sha1 - $complete_file_name.\n";
+               } else {
+                       print STDERR "Empty file $complete_file_name not pushed.\n";
+               }
+       }
+       return $newrevid;
+}
+
 sub mw_push_file {
        my $diff_info = shift;
        # $diff_info contains a string in this format:
@@ -717,7 +796,8 @@ sub mw_push_file {
        my $summary = shift;
        # MediaWiki revision number. Keep the previous one by default,
        # in case there's no edit to perform.
-       my $newrevid = shift;
+       my $oldrevid = shift;
+       my $newrevid;
 
        my $new_sha1 = $diff_info_split[3];
        my $old_sha1 = $diff_info_split[2];
@@ -725,9 +805,11 @@ sub mw_push_file {
        my $page_deleted = ($new_sha1 eq NULL_SHA1);
        $complete_file_name = mediawiki_clean_filename($complete_file_name);
 
-       if (substr($complete_file_name,-3) eq ".mw") {
-               my $title = substr($complete_file_name,0,-3);
-
+       my ($title, $extension) = $complete_file_name =~ /^(.*)\.([^\.]*)$/;
+       if (!defined($extension)) {
+               $extension = "";
+       }
+       if ($extension eq "mw") {
                my $file_content;
                if ($page_deleted) {
                        # Deleting a page usually requires
@@ -745,7 +827,7 @@ sub mw_push_file {
                        action => 'edit',
                        summary => $summary,
                        title => $title,
-                       basetimestamp => $basetimestamps{$newrevid},
+                       basetimestamp => $basetimestamps{$oldrevid},
                        text => mediawiki_clean($file_content, $page_created),
                                  }, {
                                          skip_encoding => 1 # Helps with names with accentuated characters
@@ -757,7 +839,7 @@ sub mw_push_file {
                                    $mediawiki->{error}->{code} .
                                    ' from mediwiki: ' . $mediawiki->{error}->{details} .
                                    ".\n";
-                               return ($newrevid, "non-fast-forward");
+                               return ($oldrevid, "non-fast-forward");
                        } else {
                                # Other errors. Shouldn't happen => just die()
                                die 'Fatal: Error ' .
@@ -768,8 +850,11 @@ sub mw_push_file {
                $newrevid = $result->{edit}->{newrevid};
                print STDERR "Pushed file: $new_sha1 - $title\n";
        } else {
-               print STDERR "$complete_file_name not a mediawiki file (Not pushable on this version of git-remote-mediawiki).\n"
+               $newrevid = mw_upload_file($complete_file_name, $new_sha1,
+                                          $extension, $page_deleted,
+                                          $summary);
        }
+       $newrevid = ($newrevid or $oldrevid);
        return ($newrevid, "ok");
 }
 
@@ -906,3 +991,18 @@ sub mw_push_revision {
        print STDOUT "ok $remote\n";
        return 1;
 }
+
+sub get_allowed_file_extensions {
+       mw_connect_maybe();
+
+       my $query = {
+               action => 'query',
+               meta => 'siteinfo',
+               siprop => 'fileextensions'
+               };
+       my $result = $mediawiki->api($query);
+       my @file_extensions= map $_->{ext},@{$result->{query}->{fileextensions}};
+       my %hashFile = map {$_ => 1}@file_extensions;
+
+       return %hashFile;
+}