git-remote-mediawiki: split get_mw_pages into smaller functions
[git.git] / contrib / mw-to-git / git-remote-mediawiki
1 #! /usr/bin/perl
2
3 # Copyright (C) 2011
4 #     Jérémie Nikaes <jeremie.nikaes@ensimag.imag.fr>
5 #     Arnaud Lacurie <arnaud.lacurie@ensimag.imag.fr>
6 #     Claire Fousse <claire.fousse@ensimag.imag.fr>
7 #     David Amouyal <david.amouyal@ensimag.imag.fr>
8 #     Matthieu Moy <matthieu.moy@grenoble-inp.fr>
9 # License: GPL v2 or later
10
11 # Gateway between Git and MediaWiki.
12 #   https://github.com/Bibzball/Git-Mediawiki/wiki
13 #
14 # Known limitations:
15 #
16 # - Only wiki pages are managed, no support for [[File:...]]
17 #   attachments.
18 #
19 # - Poor performance in the best case: it takes forever to check
20 #   whether we're up-to-date (on fetch or push) or to fetch a few
21 #   revisions from a large wiki, because we use exclusively a
22 #   page-based synchronization. We could switch to a wiki-wide
23 #   synchronization when the synchronization involves few revisions
24 #   but the wiki is large.
25 #
26 # - Git renames could be turned into MediaWiki renames (see TODO
27 #   below)
28 #
29 # - login/password support requires the user to write the password
30 #   cleartext in a file (see TODO below).
31 #
32 # - No way to import "one page, and all pages included in it"
33 #
34 # - Multiple remote MediaWikis have not been very well tested.
35
36 use strict;
37 use MediaWiki::API;
38 use DateTime::Format::ISO8601;
39
40 # By default, use UTF-8 to communicate with Git and the user
41 binmode STDERR, ":utf8";
42 binmode STDOUT, ":utf8";
43
44 use URI::Escape;
45 use IPC::Open2;
46
47 use warnings;
48
49 # Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced
50 use constant SLASH_REPLACEMENT => "%2F";
51
52 # It's not always possible to delete pages (may require some
53 # priviledges). Deleted pages are replaced with this content.
54 use constant DELETED_CONTENT => "[[Category:Deleted]]\n";
55
56 # It's not possible to create empty pages. New empty files in Git are
57 # sent with this content instead.
58 use constant EMPTY_CONTENT => "<!-- empty page -->\n";
59
60 # used to reflect file creation or deletion in diff.
61 use constant NULL_SHA1 => "0000000000000000000000000000000000000000";
62
63 my $remotename = $ARGV[0];
64 my $url = $ARGV[1];
65
66 # Accept both space-separated and multiple keys in config file.
67 # Spaces should be written as _ anyway because we'll use chomp.
68 my @tracked_pages = split(/[ \n]/, run_git("config --get-all remote.". $remotename .".pages"));
69 chomp(@tracked_pages);
70
71 # Just like @tracked_pages, but for MediaWiki categories.
72 my @tracked_categories = split(/[ \n]/, run_git("config --get-all remote.". $remotename .".categories"));
73 chomp(@tracked_categories);
74
75 my $wiki_login = run_git("config --get remote.". $remotename .".mwLogin");
76 # TODO: ideally, this should be able to read from keyboard, but we're
77 # inside a remote helper, so our stdin is connect to git, not to a
78 # terminal.
79 my $wiki_passwd = run_git("config --get remote.". $remotename .".mwPassword");
80 my $wiki_domain = run_git("config --get remote.". $remotename .".mwDomain");
81 chomp($wiki_login);
82 chomp($wiki_passwd);
83 chomp($wiki_domain);
84
85 # Import only last revisions (both for clone and fetch)
86 my $shallow_import = run_git("config --get --bool remote.". $remotename .".shallow");
87 chomp($shallow_import);
88 $shallow_import = ($shallow_import eq "true");
89
90 # Dumb push: don't update notes and mediawiki ref to reflect the last push.
91 #
92 # Configurable with mediawiki.dumbPush, or per-remote with
93 # remote.<remotename>.dumbPush.
94 #
95 # This means the user will have to re-import the just-pushed
96 # revisions. On the other hand, this means that the Git revisions
97 # corresponding to MediaWiki revisions are all imported from the wiki,
98 # regardless of whether they were initially created in Git or from the
99 # web interface, hence all users will get the same history (i.e. if
100 # the push from Git to MediaWiki loses some information, everybody
101 # will get the history with information lost). If the import is
102 # deterministic, this means everybody gets the same sha1 for each
103 # MediaWiki revision.
104 my $dumb_push = run_git("config --get --bool remote.$remotename.dumbPush");
105 unless ($dumb_push) {
106         $dumb_push = run_git("config --get --bool mediawiki.dumbPush");
107 }
108 chomp($dumb_push);
109 $dumb_push = ($dumb_push eq "true");
110
111 my $wiki_name = $url;
112 $wiki_name =~ s/[^\/]*:\/\///;
113 # If URL is like http://user:password@example.com/, we clearly don't
114 # want the password in $wiki_name. While we're there, also remove user
115 # and '@' sign, to avoid author like MWUser@HTTPUser@host.com
116 $wiki_name =~ s/^.*@//;
117
118 # Commands parser
119 my $entry;
120 my @cmd;
121 while (<STDIN>) {
122         chomp;
123         @cmd = split(/ /);
124         if (defined($cmd[0])) {
125                 # Line not blank
126                 if ($cmd[0] eq "capabilities") {
127                         die("Too many arguments for capabilities") unless (!defined($cmd[1]));
128                         mw_capabilities();
129                 } elsif ($cmd[0] eq "list") {
130                         die("Too many arguments for list") unless (!defined($cmd[2]));
131                         mw_list($cmd[1]);
132                 } elsif ($cmd[0] eq "import") {
133                         die("Invalid arguments for import") unless ($cmd[1] ne "" && !defined($cmd[2]));
134                         mw_import($cmd[1]);
135                 } elsif ($cmd[0] eq "option") {
136                         die("Too many arguments for option") unless ($cmd[1] ne "" && $cmd[2] ne "" && !defined($cmd[3]));
137                         mw_option($cmd[1],$cmd[2]);
138                 } elsif ($cmd[0] eq "push") {
139                         mw_push($cmd[1]);
140                 } else {
141                         print STDERR "Unknown command. Aborting...\n";
142                         last;
143                 }
144         } else {
145                 # blank line: we should terminate
146                 last;
147         }
148
149         BEGIN { $| = 1 } # flush STDOUT, to make sure the previous
150                          # command is fully processed.
151 }
152
153 ########################## Functions ##############################
154
155 ## credential API management (generic functions)
156
157 sub credential_from_url {
158         my $url = shift;
159         my $parsed = URI->new($url);
160         my %credential;
161
162         if ($parsed->scheme) {
163                 $credential{protocol} = $parsed->scheme;
164         }
165         if ($parsed->host) {
166                 $credential{host} = $parsed->host;
167         }
168         if ($parsed->path) {
169                 $credential{path} = $parsed->path;
170         }
171         if ($parsed->userinfo) {
172                 if ($parsed->userinfo =~ /([^:]*):(.*)/) {
173                         $credential{username} = $1;
174                         $credential{password} = $2;
175                 } else {
176                         $credential{username} = $parsed->userinfo;
177                 }
178         }
179
180         return %credential;
181 }
182
183 sub credential_read {
184         my %credential;
185         my $reader = shift;
186         my $op = shift;
187         while (<$reader>) {
188                 my ($key, $value) = /([^=]*)=(.*)/;
189                 if (not defined $key) {
190                         die "ERROR receiving response from git credential $op:\n$_\n";
191                 }
192                 $credential{$key} = $value;
193         }
194         return %credential;
195 }
196
197 sub credential_write {
198         my $credential = shift;
199         my $writer = shift;
200         while (my ($key, $value) = each(%$credential) ) {
201                 if ($value) {
202                         print $writer "$key=$value\n";
203                 }
204         }
205 }
206
207 sub credential_run {
208         my $op = shift;
209         my $credential = shift;
210         my $pid = open2(my $reader, my $writer, "git credential $op");
211         credential_write($credential, $writer);
212         print $writer "\n";
213         close($writer);
214
215         if ($op eq "fill") {
216                 %$credential = credential_read($reader, $op);
217         } else {
218                 if (<$reader>) {
219                         die "ERROR while running git credential $op:\n$_";
220                 }
221         }
222         close($reader);
223         waitpid($pid, 0);
224         my $child_exit_status = $? >> 8;
225         if ($child_exit_status != 0) {
226                 die "'git credential $op' failed with code $child_exit_status.";
227         }
228 }
229
230 # MediaWiki API instance, created lazily.
231 my $mediawiki;
232
233 sub mw_connect_maybe {
234         if ($mediawiki) {
235                 return;
236         }
237         $mediawiki = MediaWiki::API->new;
238         $mediawiki->{config}->{api_url} = "$url/api.php";
239         if ($wiki_login) {
240                 my %credential = credential_from_url($url);
241                 $credential{username} = $wiki_login;
242                 $credential{password} = $wiki_passwd;
243                 credential_run("fill", \%credential);
244                 my $request = {lgname => $credential{username},
245                                lgpassword => $credential{password},
246                                lgdomain => $wiki_domain};
247                 if ($mediawiki->login($request)) {
248                         credential_run("approve", \%credential);
249                         print STDERR "Logged in mediawiki user \"$credential{username}\".\n";
250                 } else {
251                         print STDERR "Failed to log in mediawiki user \"$credential{username}\" on $url\n";
252                         print STDERR "  (error " .
253                                 $mediawiki->{error}->{code} . ': ' .
254                                 $mediawiki->{error}->{details} . ")\n";
255                         credential_run("reject", \%credential);
256                         exit 1;
257                 }
258         }
259 }
260
261 ## Functions for listing pages on the remote wiki
262 sub get_mw_tracked_pages {
263         my $pages = shift;
264         my @some_pages = @tracked_pages;
265         while (@some_pages) {
266                 my $last = 50;
267                 if ($#some_pages < $last) {
268                         $last = $#some_pages;
269                 }
270                 my @slice = @some_pages[0..$last];
271                 get_mw_first_pages(\@slice, $pages);
272                 @some_pages = @some_pages[51..$#some_pages];
273         }
274 }
275
276 sub get_mw_tracked_categories {
277         my $pages = shift;
278         foreach my $category (@tracked_categories) {
279                 if (index($category, ':') < 0) {
280                         # Mediawiki requires the Category
281                         # prefix, but let's not force the user
282                         # to specify it.
283                         $category = "Category:" . $category;
284                 }
285                 my $mw_pages = $mediawiki->list( {
286                         action => 'query',
287                         list => 'categorymembers',
288                         cmtitle => $category,
289                         cmlimit => 'max' } )
290                         || die $mediawiki->{error}->{code} . ': '
291                                 . $mediawiki->{error}->{details};
292                 foreach my $page (@{$mw_pages}) {
293                         $pages->{$page->{title}} = $page;
294                 }
295         }
296 }
297
298 sub get_mw_all_pages {
299         my $pages = shift;
300         # No user-provided list, get the list of pages from the API.
301         my $mw_pages = $mediawiki->list({
302                 action => 'query',
303                 list => 'allpages',
304                 aplimit => 'max'
305         });
306         if (!defined($mw_pages)) {
307                 print STDERR "fatal: could not get the list of wiki pages.\n";
308                 print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
309                 print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
310                 exit 1;
311         }
312         foreach my $page (@{$mw_pages}) {
313                 $pages->{$page->{title}} = $page;
314         }
315 }
316
317 # queries the wiki for a set of pages. Meant to be used within a loop
318 # querying the wiki for slices of page list.
319 sub get_mw_first_pages {
320         my $some_pages = shift;
321         my @some_pages = @{$some_pages};
322
323         my $pages = shift;
324
325         # pattern 'page1|page2|...' required by the API
326         my $titles = join('|', @some_pages);
327
328         my $mw_pages = $mediawiki->api({
329                 action => 'query',
330                 titles => $titles,
331         });
332         if (!defined($mw_pages)) {
333                 print STDERR "fatal: could not query the list of wiki pages.\n";
334                 print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
335                 print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
336                 exit 1;
337         }
338         while (my ($id, $page) = each(%{$mw_pages->{query}->{pages}})) {
339                 if ($id < 0) {
340                         print STDERR "Warning: page $page->{title} not found on wiki\n";
341                 } else {
342                         $pages->{$page->{title}} = $page;
343                 }
344         }
345 }
346
347 # Get the list of pages to be fetched according to configuration.
348 sub get_mw_pages {
349         mw_connect_maybe();
350
351         my %pages; # hash on page titles to avoid duplicates
352         my $user_defined;
353         if (@tracked_pages) {
354                 $user_defined = 1;
355                 # The user provided a list of pages titles, but we
356                 # still need to query the API to get the page IDs.
357                 get_mw_tracked_pages(\%pages);
358         }
359         if (@tracked_categories) {
360                 $user_defined = 1;
361                 get_mw_tracked_categories(\%pages);
362         }
363         if (!$user_defined) {
364                 get_mw_all_pages(\%pages);
365         }
366         return values(%pages);
367 }
368
369 # usage: $out = run_git("command args");
370 #        $out = run_git("command args", "raw"); # don't interpret output as UTF-8.
371 sub run_git {
372         my $args = shift;
373         my $encoding = (shift || "encoding(UTF-8)");
374         open(my $git, "-|:$encoding", "git " . $args);
375         my $res = do { local $/; <$git> };
376         close($git);
377
378         return $res;
379 }
380
381
382 sub get_last_local_revision {
383         # Get note regarding last mediawiki revision
384         my $note = run_git("notes --ref=$remotename/mediawiki show refs/mediawiki/$remotename/master 2>/dev/null");
385         my @note_info = split(/ /, $note);
386
387         my $lastrevision_number;
388         if (!(defined($note_info[0]) && $note_info[0] eq "mediawiki_revision:")) {
389                 print STDERR "No previous mediawiki revision found";
390                 $lastrevision_number = 0;
391         } else {
392                 # Notes are formatted : mediawiki_revision: #number
393                 $lastrevision_number = $note_info[1];
394                 chomp($lastrevision_number);
395                 print STDERR "Last local mediawiki revision found is $lastrevision_number";
396         }
397         return $lastrevision_number;
398 }
399
400 # Remember the timestamp corresponding to a revision id.
401 my %basetimestamps;
402
403 sub get_last_remote_revision {
404         mw_connect_maybe();
405
406         my @pages = get_mw_pages();
407
408         my $max_rev_num = 0;
409
410         foreach my $page (@pages) {
411                 my $id = $page->{pageid};
412
413                 my $query = {
414                         action => 'query',
415                         prop => 'revisions',
416                         rvprop => 'ids|timestamp',
417                         pageids => $id,
418                 };
419
420                 my $result = $mediawiki->api($query);
421
422                 my $lastrev = pop(@{$result->{query}->{pages}->{$id}->{revisions}});
423
424                 $basetimestamps{$lastrev->{revid}} = $lastrev->{timestamp};
425
426                 $max_rev_num = ($lastrev->{revid} > $max_rev_num ? $lastrev->{revid} : $max_rev_num);
427         }
428
429         print STDERR "Last remote revision found is $max_rev_num.\n";
430         return $max_rev_num;
431 }
432
433 # Clean content before sending it to MediaWiki
434 sub mediawiki_clean {
435         my $string = shift;
436         my $page_created = shift;
437         # Mediawiki does not allow blank space at the end of a page and ends with a single \n.
438         # This function right trims a string and adds a \n at the end to follow this rule
439         $string =~ s/\s+$//;
440         if ($string eq "" && $page_created) {
441                 # Creating empty pages is forbidden.
442                 $string = EMPTY_CONTENT;
443         }
444         return $string."\n";
445 }
446
447 # Filter applied on MediaWiki data before adding them to Git
448 sub mediawiki_smudge {
449         my $string = shift;
450         if ($string eq EMPTY_CONTENT) {
451                 $string = "";
452         }
453         # This \n is important. This is due to mediawiki's way to handle end of files.
454         return $string."\n";
455 }
456
457 sub mediawiki_clean_filename {
458         my $filename = shift;
459         $filename =~ s/@{[SLASH_REPLACEMENT]}/\//g;
460         # [, ], |, {, and } are forbidden by MediaWiki, even URL-encoded.
461         # Do a variant of URL-encoding, i.e. looks like URL-encoding,
462         # but with _ added to prevent MediaWiki from thinking this is
463         # an actual special character.
464         $filename =~ s/[\[\]\{\}\|]/sprintf("_%%_%x", ord($&))/ge;
465         # If we use the uri escape before
466         # we should unescape here, before anything
467
468         return $filename;
469 }
470
471 sub mediawiki_smudge_filename {
472         my $filename = shift;
473         $filename =~ s/\//@{[SLASH_REPLACEMENT]}/g;
474         $filename =~ s/ /_/g;
475         # Decode forbidden characters encoded in mediawiki_clean_filename
476         $filename =~ s/_%_([0-9a-fA-F][0-9a-fA-F])/sprintf("%c", hex($1))/ge;
477         return $filename;
478 }
479
480 sub literal_data {
481         my ($content) = @_;
482         print STDOUT "data ", bytes::length($content), "\n", $content;
483 }
484
485 sub mw_capabilities {
486         # Revisions are imported to the private namespace
487         # refs/mediawiki/$remotename/ by the helper and fetched into
488         # refs/remotes/$remotename later by fetch.
489         print STDOUT "refspec refs/heads/*:refs/mediawiki/$remotename/*\n";
490         print STDOUT "import\n";
491         print STDOUT "list\n";
492         print STDOUT "push\n";
493         print STDOUT "\n";
494 }
495
496 sub mw_list {
497         # MediaWiki do not have branches, we consider one branch arbitrarily
498         # called master, and HEAD pointing to it.
499         print STDOUT "? refs/heads/master\n";
500         print STDOUT "\@refs/heads/master HEAD\n";
501         print STDOUT "\n";
502 }
503
504 sub mw_option {
505         print STDERR "remote-helper command 'option $_[0]' not yet implemented\n";
506         print STDOUT "unsupported\n";
507 }
508
509 sub fetch_mw_revisions_for_page {
510         my $page = shift;
511         my $id = shift;
512         my $fetch_from = shift;
513         my @page_revs = ();
514         my $query = {
515                 action => 'query',
516                 prop => 'revisions',
517                 rvprop => 'ids',
518                 rvdir => 'newer',
519                 rvstartid => $fetch_from,
520                 rvlimit => 500,
521                 pageids => $id,
522         };
523
524         my $revnum = 0;
525         # Get 500 revisions at a time due to the mediawiki api limit
526         while (1) {
527                 my $result = $mediawiki->api($query);
528
529                 # Parse each of those 500 revisions
530                 foreach my $revision (@{$result->{query}->{pages}->{$id}->{revisions}}) {
531                         my $page_rev_ids;
532                         $page_rev_ids->{pageid} = $page->{pageid};
533                         $page_rev_ids->{revid} = $revision->{revid};
534                         push(@page_revs, $page_rev_ids);
535                         $revnum++;
536                 }
537                 last unless $result->{'query-continue'};
538                 $query->{rvstartid} = $result->{'query-continue'}->{revisions}->{rvstartid};
539         }
540         if ($shallow_import && @page_revs) {
541                 print STDERR "  Found 1 revision (shallow import).\n";
542                 @page_revs = sort {$b->{revid} <=> $a->{revid}} (@page_revs);
543                 return $page_revs[0];
544         }
545         print STDERR "  Found ", $revnum, " revision(s).\n";
546         return @page_revs;
547 }
548
549 sub fetch_mw_revisions {
550         my $pages = shift; my @pages = @{$pages};
551         my $fetch_from = shift;
552
553         my @revisions = ();
554         my $n = 1;
555         foreach my $page (@pages) {
556                 my $id = $page->{pageid};
557
558                 print STDERR "page $n/", scalar(@pages), ": ". $page->{title} ."\n";
559                 $n++;
560                 my @page_revs = fetch_mw_revisions_for_page($page, $id, $fetch_from);
561                 @revisions = (@page_revs, @revisions);
562         }
563
564         return ($n, @revisions);
565 }
566
567 sub import_file_revision {
568         my $commit = shift;
569         my %commit = %{$commit};
570         my $full_import = shift;
571         my $n = shift;
572
573         my $title = $commit{title};
574         my $comment = $commit{comment};
575         my $content = $commit{content};
576         my $author = $commit{author};
577         my $date = $commit{date};
578
579         print STDOUT "commit refs/mediawiki/$remotename/master\n";
580         print STDOUT "mark :$n\n";
581         print STDOUT "committer $author <$author\@$wiki_name> ", $date->epoch, " +0000\n";
582         literal_data($comment);
583
584         # If it's not a clone, we need to know where to start from
585         if (!$full_import && $n == 1) {
586                 print STDOUT "from refs/mediawiki/$remotename/master^0\n";
587         }
588         if ($content ne DELETED_CONTENT) {
589                 print STDOUT "M 644 inline $title.mw\n";
590                 literal_data($content);
591                 print STDOUT "\n\n";
592         } else {
593                 print STDOUT "D $title.mw\n";
594         }
595
596         # mediawiki revision number in the git note
597         if ($full_import && $n == 1) {
598                 print STDOUT "reset refs/notes/$remotename/mediawiki\n";
599         }
600         print STDOUT "commit refs/notes/$remotename/mediawiki\n";
601         print STDOUT "committer $author <$author\@$wiki_name> ", $date->epoch, " +0000\n";
602         literal_data("Note added by git-mediawiki during import");
603         if (!$full_import && $n == 1) {
604                 print STDOUT "from refs/notes/$remotename/mediawiki^0\n";
605         }
606         print STDOUT "N inline :$n\n";
607         literal_data("mediawiki_revision: " . $commit{mw_revision});
608         print STDOUT "\n\n";
609 }
610
611 # parse a sequence of
612 # <cmd> <arg1>
613 # <cmd> <arg2>
614 # \n
615 # (like batch sequence of import and sequence of push statements)
616 sub get_more_refs {
617         my $cmd = shift;
618         my @refs;
619         while (1) {
620                 my $line = <STDIN>;
621                 if ($line =~ m/^$cmd (.*)$/) {
622                         push(@refs, $1);
623                 } elsif ($line eq "\n") {
624                         return @refs;
625                 } else {
626                         die("Invalid command in a '$cmd' batch: ". $_);
627                 }
628         }
629 }
630
631 sub mw_import {
632         # multiple import commands can follow each other.
633         my @refs = (shift, get_more_refs("import"));
634         foreach my $ref (@refs) {
635                 mw_import_ref($ref);
636         }
637         print STDOUT "done\n";
638 }
639
640 sub mw_import_ref {
641         my $ref = shift;
642         # The remote helper will call "import HEAD" and
643         # "import refs/heads/master".
644         # Since HEAD is a symbolic ref to master (by convention,
645         # followed by the output of the command "list" that we gave),
646         # we don't need to do anything in this case.
647         if ($ref eq "HEAD") {
648                 return;
649         }
650
651         mw_connect_maybe();
652
653         my @pages = get_mw_pages();
654
655         print STDERR "Searching revisions...\n";
656         my $last_local = get_last_local_revision();
657         my $fetch_from = $last_local + 1;
658         if ($fetch_from == 1) {
659                 print STDERR ", fetching from beginning.\n";
660         } else {
661                 print STDERR ", fetching from here.\n";
662         }
663         my ($n, @revisions) = fetch_mw_revisions(\@pages, $fetch_from);
664
665         # Creation of the fast-import stream
666         print STDERR "Fetching & writing export data...\n";
667
668         $n = 0;
669         my $last_timestamp = 0; # Placeholer in case $rev->timestamp is undefined
670
671         foreach my $pagerevid (sort {$a->{revid} <=> $b->{revid}} @revisions) {
672                 # fetch the content of the pages
673                 my $query = {
674                         action => 'query',
675                         prop => 'revisions',
676                         rvprop => 'content|timestamp|comment|user|ids',
677                         revids => $pagerevid->{revid},
678                 };
679
680                 my $result = $mediawiki->api($query);
681
682                 my $rev = pop(@{$result->{query}->{pages}->{$pagerevid->{pageid}}->{revisions}});
683
684                 $n++;
685
686                 my %commit;
687                 $commit{author} = $rev->{user} || 'Anonymous';
688                 $commit{comment} = $rev->{comment} || '*Empty MediaWiki Message*';
689                 $commit{title} = mediawiki_smudge_filename(
690                         $result->{query}->{pages}->{$pagerevid->{pageid}}->{title}
691                     );
692                 $commit{mw_revision} = $pagerevid->{revid};
693                 $commit{content} = mediawiki_smudge($rev->{'*'});
694
695                 if (!defined($rev->{timestamp})) {
696                         $last_timestamp++;
697                 } else {
698                         $last_timestamp = $rev->{timestamp};
699                 }
700                 $commit{date} = DateTime::Format::ISO8601->parse_datetime($last_timestamp);
701
702                 print STDERR "$n/", scalar(@revisions), ": Revision #$pagerevid->{revid} of $commit{title}\n";
703
704                 import_file_revision(\%commit, ($fetch_from == 1), $n);
705         }
706
707         if ($fetch_from == 1 && $n == 0) {
708                 print STDERR "You appear to have cloned an empty MediaWiki.\n";
709                 # Something has to be done remote-helper side. If nothing is done, an error is
710                 # thrown saying that HEAD is refering to unknown object 0000000000000000000
711                 # and the clone fails.
712         }
713 }
714
715 sub error_non_fast_forward {
716         my $advice = run_git("config --bool advice.pushNonFastForward");
717         chomp($advice);
718         if ($advice ne "false") {
719                 # Native git-push would show this after the summary.
720                 # We can't ask it to display it cleanly, so print it
721                 # ourselves before.
722                 print STDERR "To prevent you from losing history, non-fast-forward updates were rejected\n";
723                 print STDERR "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n";
724                 print STDERR "'Note about fast-forwards' section of 'git push --help' for details.\n";
725         }
726         print STDOUT "error $_[0] \"non-fast-forward\"\n";
727         return 0;
728 }
729
730 sub mw_upload_file {
731         my $complete_file_name = shift;
732         my $new_sha1 = shift;
733         my $extension = shift;
734         my $file_deleted = shift;
735         my $summary = shift;
736         my $newrevid;
737         my $path = "File:" . $complete_file_name;
738         my %hashFiles = get_allowed_file_extensions();
739         if (!exists($hashFiles{$extension})) {
740                 print STDERR "$complete_file_name is not a permitted file on this wiki.\n";
741                 print STDERR "Check the configuration of file uploads in your mediawiki.\n";
742                 return $newrevid;
743         }
744         # Deleting and uploading a file requires a priviledged user
745         if ($file_deleted) {
746                 mw_connect_maybe();
747                 my $query = {
748                         action => 'delete',
749                         title => $path,
750                         reason => $summary
751                 };
752                 if (!$mediawiki->edit($query)) {
753                         print STDERR "Failed to delete file on remote wiki\n";
754                         print STDERR "Check your permissions on the remote site. Error code:\n";
755                         print STDERR $mediawiki->{error}->{code} . ':' . $mediawiki->{error}->{details};
756                         exit 1;
757                 }
758         } else {
759                 # Don't let perl try to interpret file content as UTF-8 => use "raw"
760                 my $content = run_git("cat-file blob $new_sha1", "raw");
761                 if ($content ne "") {
762                         mw_connect_maybe();
763                         $mediawiki->{config}->{upload_url} =
764                                 "$url/index.php/Special:Upload";
765                         $mediawiki->edit({
766                                 action => 'upload',
767                                 filename => $complete_file_name,
768                                 comment => $summary,
769                                 file => [undef,
770                                          $complete_file_name,
771                                          Content => $content],
772                                 ignorewarnings => 1,
773                         }, {
774                                 skip_encoding => 1
775                         } ) || die $mediawiki->{error}->{code} . ':'
776                                  . $mediawiki->{error}->{details};
777                         my $last_file_page = $mediawiki->get_page({title => $path});
778                         $newrevid = $last_file_page->{revid};
779                         print STDERR "Pushed file: $new_sha1 - $complete_file_name.\n";
780                 } else {
781                         print STDERR "Empty file $complete_file_name not pushed.\n";
782                 }
783         }
784         return $newrevid;
785 }
786
787 sub mw_push_file {
788         my $diff_info = shift;
789         # $diff_info contains a string in this format:
790         # 100644 100644 <sha1_of_blob_before_commit> <sha1_of_blob_now> <status>
791         my @diff_info_split = split(/[ \t]/, $diff_info);
792
793         # Filename, including .mw extension
794         my $complete_file_name = shift;
795         # Commit message
796         my $summary = shift;
797         # MediaWiki revision number. Keep the previous one by default,
798         # in case there's no edit to perform.
799         my $oldrevid = shift;
800         my $newrevid;
801
802         my $new_sha1 = $diff_info_split[3];
803         my $old_sha1 = $diff_info_split[2];
804         my $page_created = ($old_sha1 eq NULL_SHA1);
805         my $page_deleted = ($new_sha1 eq NULL_SHA1);
806         $complete_file_name = mediawiki_clean_filename($complete_file_name);
807
808         my ($title, $extension) = $complete_file_name =~ /^(.*)\.([^\.]*)$/;
809         if (!defined($extension)) {
810                 $extension = "";
811         }
812         if ($extension eq "mw") {
813                 my $file_content;
814                 if ($page_deleted) {
815                         # Deleting a page usually requires
816                         # special priviledges. A common
817                         # convention is to replace the page
818                         # with this content instead:
819                         $file_content = DELETED_CONTENT;
820                 } else {
821                         $file_content = run_git("cat-file blob $new_sha1");
822                 }
823
824                 mw_connect_maybe();
825
826                 my $result = $mediawiki->edit( {
827                         action => 'edit',
828                         summary => $summary,
829                         title => $title,
830                         basetimestamp => $basetimestamps{$oldrevid},
831                         text => mediawiki_clean($file_content, $page_created),
832                                   }, {
833                                           skip_encoding => 1 # Helps with names with accentuated characters
834                                   });
835                 if (!$result) {
836                         if ($mediawiki->{error}->{code} == 3) {
837                                 # edit conflicts, considered as non-fast-forward
838                                 print STDERR 'Warning: Error ' .
839                                     $mediawiki->{error}->{code} .
840                                     ' from mediwiki: ' . $mediawiki->{error}->{details} .
841                                     ".\n";
842                                 return ($oldrevid, "non-fast-forward");
843                         } else {
844                                 # Other errors. Shouldn't happen => just die()
845                                 die 'Fatal: Error ' .
846                                     $mediawiki->{error}->{code} .
847                                     ' from mediwiki: ' . $mediawiki->{error}->{details};
848                         }
849                 }
850                 $newrevid = $result->{edit}->{newrevid};
851                 print STDERR "Pushed file: $new_sha1 - $title\n";
852         } else {
853                 $newrevid = mw_upload_file($complete_file_name, $new_sha1,
854                                            $extension, $page_deleted,
855                                            $summary);
856         }
857         $newrevid = ($newrevid or $oldrevid);
858         return ($newrevid, "ok");
859 }
860
861 sub mw_push {
862         # multiple push statements can follow each other
863         my @refsspecs = (shift, get_more_refs("push"));
864         my $pushed;
865         for my $refspec (@refsspecs) {
866                 my ($force, $local, $remote) = $refspec =~ /^(\+)?([^:]*):([^:]*)$/
867                     or die("Invalid refspec for push. Expected <src>:<dst> or +<src>:<dst>");
868                 if ($force) {
869                         print STDERR "Warning: forced push not allowed on a MediaWiki.\n";
870                 }
871                 if ($local eq "") {
872                         print STDERR "Cannot delete remote branch on a MediaWiki\n";
873                         print STDOUT "error $remote cannot delete\n";
874                         next;
875                 }
876                 if ($remote ne "refs/heads/master") {
877                         print STDERR "Only push to the branch 'master' is supported on a MediaWiki\n";
878                         print STDOUT "error $remote only master allowed\n";
879                         next;
880                 }
881                 if (mw_push_revision($local, $remote)) {
882                         $pushed = 1;
883                 }
884         }
885
886         # Notify Git that the push is done
887         print STDOUT "\n";
888
889         if ($pushed && $dumb_push) {
890                 print STDERR "Just pushed some revisions to MediaWiki.\n";
891                 print STDERR "The pushed revisions now have to be re-imported, and your current branch\n";
892                 print STDERR "needs to be updated with these re-imported commits. You can do this with\n";
893                 print STDERR "\n";
894                 print STDERR "  git pull --rebase\n";
895                 print STDERR "\n";
896         }
897 }
898
899 sub mw_push_revision {
900         my $local = shift;
901         my $remote = shift; # actually, this has to be "refs/heads/master" at this point.
902         my $last_local_revid = get_last_local_revision();
903         print STDERR ".\n"; # Finish sentence started by get_last_local_revision()
904         my $last_remote_revid = get_last_remote_revision();
905         my $mw_revision = $last_remote_revid;
906
907         # Get sha1 of commit pointed by local HEAD
908         my $HEAD_sha1 = run_git("rev-parse $local 2>/dev/null"); chomp($HEAD_sha1);
909         # Get sha1 of commit pointed by remotes/$remotename/master
910         my $remoteorigin_sha1 = run_git("rev-parse refs/remotes/$remotename/master 2>/dev/null");
911         chomp($remoteorigin_sha1);
912
913         if ($last_local_revid > 0 &&
914             $last_local_revid < $last_remote_revid) {
915                 return error_non_fast_forward($remote);
916         }
917
918         if ($HEAD_sha1 eq $remoteorigin_sha1) {
919                 # nothing to push
920                 return 0;
921         }
922
923         # Get every commit in between HEAD and refs/remotes/origin/master,
924         # including HEAD and refs/remotes/origin/master
925         my @commit_pairs = ();
926         if ($last_local_revid > 0) {
927                 my $parsed_sha1 = $remoteorigin_sha1;
928                 # Find a path from last MediaWiki commit to pushed commit
929                 while ($parsed_sha1 ne $HEAD_sha1) {
930                         my @commit_info =  grep(/^$parsed_sha1/, split(/\n/, run_git("rev-list --children $local")));
931                         if (!@commit_info) {
932                                 return error_non_fast_forward($remote);
933                         }
934                         my @commit_info_split = split(/ |\n/, $commit_info[0]);
935                         # $commit_info_split[1] is the sha1 of the commit to export
936                         # $commit_info_split[0] is the sha1 of its direct child
937                         push(@commit_pairs, \@commit_info_split);
938                         $parsed_sha1 = $commit_info_split[1];
939                 }
940         } else {
941                 # No remote mediawiki revision. Export the whole
942                 # history (linearized with --first-parent)
943                 print STDERR "Warning: no common ancestor, pushing complete history\n";
944                 my $history = run_git("rev-list --first-parent --children $local");
945                 my @history = split('\n', $history);
946                 @history = @history[1..$#history];
947                 foreach my $line (reverse @history) {
948                         my @commit_info_split = split(/ |\n/, $line);
949                         push(@commit_pairs, \@commit_info_split);
950                 }
951         }
952
953         foreach my $commit_info_split (@commit_pairs) {
954                 my $sha1_child = @{$commit_info_split}[0];
955                 my $sha1_commit = @{$commit_info_split}[1];
956                 my $diff_infos = run_git("diff-tree -r --raw -z $sha1_child $sha1_commit");
957                 # TODO: we could detect rename, and encode them with a #redirect on the wiki.
958                 # TODO: for now, it's just a delete+add
959                 my @diff_info_list = split(/\0/, $diff_infos);
960                 # Keep the subject line of the commit message as mediawiki comment for the revision
961                 my $commit_msg = run_git("log --no-walk --format=\"%s\" $sha1_commit");
962                 chomp($commit_msg);
963                 # Push every blob
964                 while (@diff_info_list) {
965                         my $status;
966                         # git diff-tree -z gives an output like
967                         # <metadata>\0<filename1>\0
968                         # <metadata>\0<filename2>\0
969                         # and we've split on \0.
970                         my $info = shift(@diff_info_list);
971                         my $file = shift(@diff_info_list);
972                         ($mw_revision, $status) = mw_push_file($info, $file, $commit_msg, $mw_revision);
973                         if ($status eq "non-fast-forward") {
974                                 # we may already have sent part of the
975                                 # commit to MediaWiki, but it's too
976                                 # late to cancel it. Stop the push in
977                                 # the middle, but still give an
978                                 # accurate error message.
979                                 return error_non_fast_forward($remote);
980                         }
981                         if ($status ne "ok") {
982                                 die("Unknown error from mw_push_file()");
983                         }
984                 }
985                 unless ($dumb_push) {
986                         run_git("notes --ref=$remotename/mediawiki add -m \"mediawiki_revision: $mw_revision\" $sha1_commit");
987                         run_git("update-ref -m \"Git-MediaWiki push\" refs/mediawiki/$remotename/master $sha1_commit $sha1_child");
988                 }
989         }
990
991         print STDOUT "ok $remote\n";
992         return 1;
993 }
994
995 sub get_allowed_file_extensions {
996         mw_connect_maybe();
997
998         my $query = {
999                 action => 'query',
1000                 meta => 'siteinfo',
1001                 siprop => 'fileextensions'
1002                 };
1003         my $result = $mediawiki->api($query);
1004         my @file_extensions= map $_->{ext},@{$result->{query}->{fileextensions}};
1005         my %hashFile = map {$_ => 1}@file_extensions;
1006
1007         return %hashFile;
1008 }