git-remote-mediawiki: refactor loop over revision ids
authorMatthieu Moy <Matthieu.Moy@imag.fr>
Fri, 6 Jul 2012 10:03:12 +0000 (12:03 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 6 Jul 2012 19:20:46 +0000 (12:20 -0700)
Without changing the behavior, we turn the foreach loop on an array of
revisions into a loop on an array of integer. It will be easier to
implement other strategies as they will only need to produce an array of
integer instead of a more complex data-structure.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/mw-to-git/git-remote-mediawiki

index 4bf990bd58040f2644cf79a0301fb9e7ac41b27d..47dd574dd0dfbb363f74dbd18b2d59526da80558 100755 (executable)
@@ -819,30 +819,35 @@ sub mw_import_ref {
        # Creation of the fast-import stream
        print STDERR "Fetching & writing export data...\n";
 
+       @revisions = sort {$a->{revid} <=> $b->{revid}} @revisions;
+       my @revision_ids = map $_->{revid}, @revisions;
+
        $n = 0;
        my $last_timestamp = 0; # Placeholer in case $rev->timestamp is undefined
 
-       foreach my $pagerevid (sort {$a->{revid} <=> $b->{revid}} @revisions) {
+       foreach my $pagerevid (@revision_ids) {
                # fetch the content of the pages
                my $query = {
                        action => 'query',
                        prop => 'revisions',
                        rvprop => 'content|timestamp|comment|user|ids',
-                       revids => $pagerevid->{revid},
+                       revids => $pagerevid,
                };
 
                my $result = $mediawiki->api($query);
 
-               my $rev = pop(@{$result->{query}->{pages}->{$pagerevid->{pageid}}->{revisions}});
+               my @result_pages = values(%{$result->{query}->{pages}});
+               my $result_page = $result_pages[0];
+               my $rev = $result_pages[0]->{revisions}->[0];
 
                $n++;
 
-               my $page_title = $result->{query}->{pages}->{$pagerevid->{pageid}}->{title};
+               my $page_title = $result_page->{title};
                my %commit;
                $commit{author} = $rev->{user} || 'Anonymous';
                $commit{comment} = $rev->{comment} || '*Empty MediaWiki Message*';
                $commit{title} = mediawiki_smudge_filename($page_title);
-               $commit{mw_revision} = $pagerevid->{revid};
+               $commit{mw_revision} = $rev->{revid};
                $commit{content} = mediawiki_smudge($rev->{'*'});
 
                if (!defined($rev->{timestamp})) {
@@ -861,7 +866,7 @@ sub mw_import_ref {
                # If this is a revision of the media page for new version
                # of a file do one common commit for both file and media page.
                # Else do commit only for that page.
-               print STDERR "$n/", scalar(@revisions), ": Revision #$pagerevid->{revid} of $commit{title}\n";
+               print STDERR "$n/", scalar(@revision_ids), ": Revision #$rev->{revid} of $commit{title}\n";
                import_file_revision(\%commit, ($fetch_from == 1), $n, \%mediafile);
        }