contrib / mw-to-git / git-remote-mediawikion commit git-remote-mediawiki: set 'basetimestamp' to let the wiki handle conflicts (3c1ed90)
   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
  36use strict;
  37use MediaWiki::API;
  38use DateTime::Format::ISO8601;
  39use encoding 'utf8';
  40
  41# use encoding 'utf8' doesn't change STDERROR
  42# but we're going to output UTF-8 filenames to STDERR
  43binmode STDERR, ":utf8";
  44
  45use URI::Escape;
  46use warnings;
  47
  48# Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced
  49use constant SLASH_REPLACEMENT => "%2F";
  50
  51# It's not always possible to delete pages (may require some
  52# priviledges). Deleted pages are replaced with this content.
  53use constant DELETED_CONTENT => "[[Category:Deleted]]\n";
  54
  55# It's not possible to create empty pages. New empty files in Git are
  56# sent with this content instead.
  57use constant EMPTY_CONTENT => "<!-- empty page -->\n";
  58
  59# used to reflect file creation or deletion in diff.
  60use constant NULL_SHA1 => "0000000000000000000000000000000000000000";
  61
  62my $remotename = $ARGV[0];
  63my $url = $ARGV[1];
  64
  65# Accept both space-separated and multiple keys in config file.
  66# Spaces should be written as _ anyway because we'll use chomp.
  67my @tracked_pages = split(/[ \n]/, run_git("config --get-all remote.". $remotename .".pages"));
  68chomp(@tracked_pages);
  69
  70# Just like @tracked_pages, but for MediaWiki categories.
  71my @tracked_categories = split(/[ \n]/, run_git("config --get-all remote.". $remotename .".categories"));
  72chomp(@tracked_categories);
  73
  74my $wiki_login = run_git("config --get remote.". $remotename .".mwLogin");
  75# TODO: ideally, this should be able to read from keyboard, but we're
  76# inside a remote helper, so our stdin is connect to git, not to a
  77# terminal.
  78my $wiki_passwd = run_git("config --get remote.". $remotename .".mwPassword");
  79chomp($wiki_login);
  80chomp($wiki_passwd);
  81
  82# Import only last revisions (both for clone and fetch)
  83my $shallow_import = run_git("config --get --bool remote.". $remotename .".shallow");
  84chomp($shallow_import);
  85$shallow_import = ($shallow_import eq "true");
  86
  87# Dumb push: don't update notes and mediawiki ref to reflect the last push.
  88#
  89# Configurable with mediawiki.dumbPush, or per-remote with
  90# remote.<remotename>.dumbPush.
  91#
  92# This means the user will have to re-import the just-pushed
  93# revisions. On the other hand, this means that the Git revisions
  94# corresponding to MediaWiki revisions are all imported from the wiki,
  95# regardless of whether they were initially created in Git or from the
  96# web interface, hence all users will get the same history (i.e. if
  97# the push from Git to MediaWiki loses some information, everybody
  98# will get the history with information lost). If the import is
  99# deterministic, this means everybody gets the same sha1 for each
 100# MediaWiki revision.
 101my $dumb_push = run_git("config --get --bool remote.$remotename.dumbPush");
 102unless ($dumb_push) {
 103        $dumb_push = run_git("config --get --bool mediawiki.dumbPush");
 104}
 105chomp($dumb_push);
 106$dumb_push = ($dumb_push eq "true");
 107
 108my $wiki_name = $url;
 109$wiki_name =~ s/[^\/]*:\/\///;
 110
 111# Commands parser
 112my $entry;
 113my @cmd;
 114while (<STDIN>) {
 115        chomp;
 116        @cmd = split(/ /);
 117        if (defined($cmd[0])) {
 118                # Line not blank
 119                if ($cmd[0] eq "capabilities") {
 120                        die("Too many arguments for capabilities") unless (!defined($cmd[1]));
 121                        mw_capabilities();
 122                } elsif ($cmd[0] eq "list") {
 123                        die("Too many arguments for list") unless (!defined($cmd[2]));
 124                        mw_list($cmd[1]);
 125                } elsif ($cmd[0] eq "import") {
 126                        die("Invalid arguments for import") unless ($cmd[1] ne "" && !defined($cmd[2]));
 127                        mw_import($cmd[1]);
 128                } elsif ($cmd[0] eq "option") {
 129                        die("Too many arguments for option") unless ($cmd[1] ne "" && $cmd[2] ne "" && !defined($cmd[3]));
 130                        mw_option($cmd[1],$cmd[2]);
 131                } elsif ($cmd[0] eq "push") {
 132                        mw_push($cmd[1]);
 133                } else {
 134                        print STDERR "Unknown command. Aborting...\n";
 135                        last;
 136                }
 137        } else {
 138                # blank line: we should terminate
 139                last;
 140        }
 141
 142        BEGIN { $| = 1 } # flush STDOUT, to make sure the previous
 143                         # command is fully processed.
 144}
 145
 146########################## Functions ##############################
 147
 148# MediaWiki API instance, created lazily.
 149my $mediawiki;
 150
 151sub mw_connect_maybe {
 152        if ($mediawiki) {
 153            return;
 154        }
 155        $mediawiki = MediaWiki::API->new;
 156        $mediawiki->{config}->{api_url} = "$url/api.php";
 157        if ($wiki_login) {
 158                if (!$mediawiki->login({
 159                        lgname => $wiki_login,
 160                        lgpassword => $wiki_passwd,
 161                })) {
 162                        print STDERR "Failed to log in mediawiki user \"$wiki_login\" on $url\n";
 163                        print STDERR "(error " .
 164                            $mediawiki->{error}->{code} . ': ' .
 165                            $mediawiki->{error}->{details} . ")\n";
 166                        exit 1;
 167                } else {
 168                        print STDERR "Logged in with user \"$wiki_login\".\n";
 169                }
 170        }
 171}
 172
 173sub get_mw_first_pages {
 174        my $some_pages = shift;
 175        my @some_pages = @{$some_pages};
 176
 177        my $pages = shift;
 178
 179        # pattern 'page1|page2|...' required by the API
 180        my $titles = join('|', @some_pages);
 181
 182        my $mw_pages = $mediawiki->api({
 183                action => 'query',
 184                titles => $titles,
 185        });
 186        if (!defined($mw_pages)) {
 187                print STDERR "fatal: could not query the list of wiki pages.\n";
 188                print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
 189                print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
 190                exit 1;
 191        }
 192        while (my ($id, $page) = each(%{$mw_pages->{query}->{pages}})) {
 193                if ($id < 0) {
 194                        print STDERR "Warning: page $page->{title} not found on wiki\n";
 195                } else {
 196                        $pages->{$page->{title}} = $page;
 197                }
 198        }
 199}
 200
 201sub get_mw_pages {
 202        mw_connect_maybe();
 203
 204        my %pages; # hash on page titles to avoid duplicates
 205        my $user_defined;
 206        if (@tracked_pages) {
 207                $user_defined = 1;
 208                # The user provided a list of pages titles, but we
 209                # still need to query the API to get the page IDs.
 210
 211                my @some_pages = @tracked_pages;
 212                while (@some_pages) {
 213                        my $last = 50;
 214                        if ($#some_pages < $last) {
 215                                $last = $#some_pages;
 216                        }
 217                        my @slice = @some_pages[0..$last];
 218                        get_mw_first_pages(\@slice, \%pages);
 219                        @some_pages = @some_pages[51..$#some_pages];
 220                }
 221        }
 222        if (@tracked_categories) {
 223                $user_defined = 1;
 224                foreach my $category (@tracked_categories) {
 225                        if (index($category, ':') < 0) {
 226                                # Mediawiki requires the Category
 227                                # prefix, but let's not force the user
 228                                # to specify it.
 229                                $category = "Category:" . $category;
 230                        }
 231                        my $mw_pages = $mediawiki->list( {
 232                                action => 'query',
 233                                list => 'categorymembers',
 234                                cmtitle => $category,
 235                                cmlimit => 'max' } )
 236                            || die $mediawiki->{error}->{code} . ': ' . $mediawiki->{error}->{details};
 237                        foreach my $page (@{$mw_pages}) {
 238                                $pages{$page->{title}} = $page;
 239                        }
 240                }
 241        }
 242        if (!$user_defined) {
 243                # No user-provided list, get the list of pages from
 244                # the API.
 245                my $mw_pages = $mediawiki->list({
 246                        action => 'query',
 247                        list => 'allpages',
 248                        aplimit => 500,
 249                });
 250                if (!defined($mw_pages)) {
 251                        print STDERR "fatal: could not get the list of wiki pages.\n";
 252                        print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
 253                        print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
 254                        exit 1;
 255                }
 256                foreach my $page (@{$mw_pages}) {
 257                        $pages{$page->{title}} = $page;
 258                }
 259        }
 260        return values(%pages);
 261}
 262
 263sub run_git {
 264        open(my $git, "-|:encoding(UTF-8)", "git " . $_[0]);
 265        my $res = do { local $/; <$git> };
 266        close($git);
 267
 268        return $res;
 269}
 270
 271
 272sub get_last_local_revision {
 273        # Get note regarding last mediawiki revision
 274        my $note = run_git("notes --ref=$remotename/mediawiki show refs/mediawiki/$remotename/master 2>/dev/null");
 275        my @note_info = split(/ /, $note);
 276
 277        my $lastrevision_number;
 278        if (!(defined($note_info[0]) && $note_info[0] eq "mediawiki_revision:")) {
 279                print STDERR "No previous mediawiki revision found";
 280                $lastrevision_number = 0;
 281        } else {
 282                # Notes are formatted : mediawiki_revision: #number
 283                $lastrevision_number = $note_info[1];
 284                chomp($lastrevision_number);
 285                print STDERR "Last local mediawiki revision found is $lastrevision_number";
 286        }
 287        return $lastrevision_number;
 288}
 289
 290# Remember the timestamp corresponding to a revision id.
 291my %basetimestamps;
 292
 293sub get_last_remote_revision {
 294        mw_connect_maybe();
 295
 296        my @pages = get_mw_pages();
 297
 298        my $max_rev_num = 0;
 299
 300        foreach my $page (@pages) {
 301                my $id = $page->{pageid};
 302
 303                my $query = {
 304                        action => 'query',
 305                        prop => 'revisions',
 306                        rvprop => 'ids|timestamp',
 307                        pageids => $id,
 308                };
 309
 310                my $result = $mediawiki->api($query);
 311
 312                my $lastrev = pop(@{$result->{query}->{pages}->{$id}->{revisions}});
 313
 314                $basetimestamps{$lastrev->{revid}} = $lastrev->{timestamp};
 315
 316                $max_rev_num = ($lastrev->{revid} > $max_rev_num ? $lastrev->{revid} : $max_rev_num);
 317        }
 318
 319        print STDERR "Last remote revision found is $max_rev_num.\n";
 320        return $max_rev_num;
 321}
 322
 323# Clean content before sending it to MediaWiki
 324sub mediawiki_clean {
 325        my $string = shift;
 326        my $page_created = shift;
 327        # Mediawiki does not allow blank space at the end of a page and ends with a single \n.
 328        # This function right trims a string and adds a \n at the end to follow this rule
 329        $string =~ s/\s+$//;
 330        if ($string eq "" && $page_created) {
 331                # Creating empty pages is forbidden.
 332                $string = EMPTY_CONTENT;
 333        }
 334        return $string."\n";
 335}
 336
 337# Filter applied on MediaWiki data before adding them to Git
 338sub mediawiki_smudge {
 339        my $string = shift;
 340        if ($string eq EMPTY_CONTENT) {
 341                $string = "";
 342        }
 343        # This \n is important. This is due to mediawiki's way to handle end of files.
 344        return $string."\n";
 345}
 346
 347sub mediawiki_clean_filename {
 348        my $filename = shift;
 349        $filename =~ s/@{[SLASH_REPLACEMENT]}/\//g;
 350        # [, ], |, {, and } are forbidden by MediaWiki, even URL-encoded.
 351        # Do a variant of URL-encoding, i.e. looks like URL-encoding,
 352        # but with _ added to prevent MediaWiki from thinking this is
 353        # an actual special character.
 354        $filename =~ s/[\[\]\{\}\|]/sprintf("_%%_%x", ord($&))/ge;
 355        # If we use the uri escape before
 356        # we should unescape here, before anything
 357
 358        return $filename;
 359}
 360
 361sub mediawiki_smudge_filename {
 362        my $filename = shift;
 363        $filename =~ s/\//@{[SLASH_REPLACEMENT]}/g;
 364        $filename =~ s/ /_/g;
 365        # Decode forbidden characters encoded in mediawiki_clean_filename
 366        $filename =~ s/_%_([0-9a-fA-F][0-9a-fA-F])/sprintf("%c", hex($1))/ge;
 367        return $filename;
 368}
 369
 370sub literal_data {
 371        my ($content) = @_;
 372        print STDOUT "data ", bytes::length($content), "\n", $content;
 373}
 374
 375sub mw_capabilities {
 376        # Revisions are imported to the private namespace
 377        # refs/mediawiki/$remotename/ by the helper and fetched into
 378        # refs/remotes/$remotename later by fetch.
 379        print STDOUT "refspec refs/heads/*:refs/mediawiki/$remotename/*\n";
 380        print STDOUT "import\n";
 381        print STDOUT "list\n";
 382        print STDOUT "push\n";
 383        print STDOUT "\n";
 384}
 385
 386sub mw_list {
 387        # MediaWiki do not have branches, we consider one branch arbitrarily
 388        # called master, and HEAD pointing to it.
 389        print STDOUT "? refs/heads/master\n";
 390        print STDOUT "\@refs/heads/master HEAD\n";
 391        print STDOUT "\n";
 392}
 393
 394sub mw_option {
 395        print STDERR "remote-helper command 'option $_[0]' not yet implemented\n";
 396        print STDOUT "unsupported\n";
 397}
 398
 399sub fetch_mw_revisions_for_page {
 400        my $page = shift;
 401        my $id = shift;
 402        my $fetch_from = shift;
 403        my @page_revs = ();
 404        my $query = {
 405                action => 'query',
 406                prop => 'revisions',
 407                rvprop => 'ids',
 408                rvdir => 'newer',
 409                rvstartid => $fetch_from,
 410                rvlimit => 500,
 411                pageids => $id,
 412        };
 413
 414        my $revnum = 0;
 415        # Get 500 revisions at a time due to the mediawiki api limit
 416        while (1) {
 417                my $result = $mediawiki->api($query);
 418
 419                # Parse each of those 500 revisions
 420                foreach my $revision (@{$result->{query}->{pages}->{$id}->{revisions}}) {
 421                        my $page_rev_ids;
 422                        $page_rev_ids->{pageid} = $page->{pageid};
 423                        $page_rev_ids->{revid} = $revision->{revid};
 424                        push(@page_revs, $page_rev_ids);
 425                        $revnum++;
 426                }
 427                last unless $result->{'query-continue'};
 428                $query->{rvstartid} = $result->{'query-continue'}->{revisions}->{rvstartid};
 429        }
 430        if ($shallow_import && @page_revs) {
 431                print STDERR "  Found 1 revision (shallow import).\n";
 432                @page_revs = sort {$b->{revid} <=> $a->{revid}} (@page_revs);
 433                return $page_revs[0];
 434        }
 435        print STDERR "  Found ", $revnum, " revision(s).\n";
 436        return @page_revs;
 437}
 438
 439sub fetch_mw_revisions {
 440        my $pages = shift; my @pages = @{$pages};
 441        my $fetch_from = shift;
 442
 443        my @revisions = ();
 444        my $n = 1;
 445        foreach my $page (@pages) {
 446                my $id = $page->{pageid};
 447
 448                print STDERR "page $n/", scalar(@pages), ": ". $page->{title} ."\n";
 449                $n++;
 450                my @page_revs = fetch_mw_revisions_for_page($page, $id, $fetch_from);
 451                @revisions = (@page_revs, @revisions);
 452        }
 453
 454        return ($n, @revisions);
 455}
 456
 457sub import_file_revision {
 458        my $commit = shift;
 459        my %commit = %{$commit};
 460        my $full_import = shift;
 461        my $n = shift;
 462
 463        my $title = $commit{title};
 464        my $comment = $commit{comment};
 465        my $content = $commit{content};
 466        my $author = $commit{author};
 467        my $date = $commit{date};
 468
 469        print STDOUT "commit refs/mediawiki/$remotename/master\n";
 470        print STDOUT "mark :$n\n";
 471        print STDOUT "committer $author <$author\@$wiki_name> ", $date->epoch, " +0000\n";
 472        literal_data($comment);
 473
 474        # If it's not a clone, we need to know where to start from
 475        if (!$full_import && $n == 1) {
 476                print STDOUT "from refs/mediawiki/$remotename/master^0\n";
 477        }
 478        if ($content ne DELETED_CONTENT) {
 479                print STDOUT "M 644 inline $title.mw\n";
 480                literal_data($content);
 481                print STDOUT "\n\n";
 482        } else {
 483                print STDOUT "D $title.mw\n";
 484        }
 485
 486        # mediawiki revision number in the git note
 487        if ($full_import && $n == 1) {
 488                print STDOUT "reset refs/notes/$remotename/mediawiki\n";
 489        }
 490        print STDOUT "commit refs/notes/$remotename/mediawiki\n";
 491        print STDOUT "committer $author <$author\@$wiki_name> ", $date->epoch, " +0000\n";
 492        literal_data("Note added by git-mediawiki during import");
 493        if (!$full_import && $n == 1) {
 494                print STDOUT "from refs/notes/$remotename/mediawiki^0\n";
 495        }
 496        print STDOUT "N inline :$n\n";
 497        literal_data("mediawiki_revision: " . $commit{mw_revision});
 498        print STDOUT "\n\n";
 499}
 500
 501# parse a sequence of
 502# <cmd> <arg1>
 503# <cmd> <arg2>
 504# \n
 505# (like batch sequence of import and sequence of push statements)
 506sub get_more_refs {
 507        my $cmd = shift;
 508        my @refs;
 509        while (1) {
 510                my $line = <STDIN>;
 511                if ($line =~ m/^$cmd (.*)$/) {
 512                        push(@refs, $1);
 513                } elsif ($line eq "\n") {
 514                        return @refs;
 515                } else {
 516                        die("Invalid command in a '$cmd' batch: ". $_);
 517                }
 518        }
 519}
 520
 521sub mw_import {
 522        # multiple import commands can follow each other.
 523        my @refs = (shift, get_more_refs("import"));
 524        foreach my $ref (@refs) {
 525                mw_import_ref($ref);
 526        }
 527        print STDOUT "done\n";
 528}
 529
 530sub mw_import_ref {
 531        my $ref = shift;
 532        # The remote helper will call "import HEAD" and
 533        # "import refs/heads/master".
 534        # Since HEAD is a symbolic ref to master (by convention,
 535        # followed by the output of the command "list" that we gave),
 536        # we don't need to do anything in this case.
 537        if ($ref eq "HEAD") {
 538                return;
 539        }
 540
 541        mw_connect_maybe();
 542
 543        my @pages = get_mw_pages();
 544
 545        print STDERR "Searching revisions...\n";
 546        my $last_local = get_last_local_revision();
 547        my $fetch_from = $last_local + 1;
 548        if ($fetch_from == 1) {
 549                print STDERR ", fetching from beginning.\n";
 550        } else {
 551                print STDERR ", fetching from here.\n";
 552        }
 553        my ($n, @revisions) = fetch_mw_revisions(\@pages, $fetch_from);
 554
 555        # Creation of the fast-import stream
 556        print STDERR "Fetching & writing export data...\n";
 557
 558        $n = 0;
 559        my $last_timestamp = 0; # Placeholer in case $rev->timestamp is undefined
 560
 561        foreach my $pagerevid (sort {$a->{revid} <=> $b->{revid}} @revisions) {
 562                # fetch the content of the pages
 563                my $query = {
 564                        action => 'query',
 565                        prop => 'revisions',
 566                        rvprop => 'content|timestamp|comment|user|ids',
 567                        revids => $pagerevid->{revid},
 568                };
 569
 570                my $result = $mediawiki->api($query);
 571
 572                my $rev = pop(@{$result->{query}->{pages}->{$pagerevid->{pageid}}->{revisions}});
 573
 574                $n++;
 575
 576                my %commit;
 577                $commit{author} = $rev->{user} || 'Anonymous';
 578                $commit{comment} = $rev->{comment} || '*Empty MediaWiki Message*';
 579                $commit{title} = mediawiki_smudge_filename(
 580                        $result->{query}->{pages}->{$pagerevid->{pageid}}->{title}
 581                    );
 582                $commit{mw_revision} = $pagerevid->{revid};
 583                $commit{content} = mediawiki_smudge($rev->{'*'});
 584
 585                if (!defined($rev->{timestamp})) {
 586                        $last_timestamp++;
 587                } else {
 588                        $last_timestamp = $rev->{timestamp};
 589                }
 590                $commit{date} = DateTime::Format::ISO8601->parse_datetime($last_timestamp);
 591
 592                print STDERR "$n/", scalar(@revisions), ": Revision #$pagerevid->{revid} of $commit{title}\n";
 593
 594                import_file_revision(\%commit, ($fetch_from == 1), $n);
 595        }
 596
 597        if ($fetch_from == 1 && $n == 0) {
 598                print STDERR "You appear to have cloned an empty MediaWiki.\n";
 599                # Something has to be done remote-helper side. If nothing is done, an error is
 600                # thrown saying that HEAD is refering to unknown object 0000000000000000000
 601                # and the clone fails.
 602        }
 603}
 604
 605sub error_non_fast_forward {
 606        # Native git-push would show this after the summary.
 607        # We can't ask it to display it cleanly, so print it
 608        # ourselves before.
 609        print STDERR "To prevent you from losing history, non-fast-forward updates were rejected\n";
 610        print STDERR "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n";
 611        print STDERR "'Note about fast-forwards' section of 'git push --help' for details.\n";
 612
 613        print STDOUT "error $_[0] \"non-fast-forward\"\n";
 614        return 0;
 615}
 616
 617sub mw_push_file {
 618        my $diff_info = shift;
 619        # $diff_info contains a string in this format:
 620        # 100644 100644 <sha1_of_blob_before_commit> <sha1_of_blob_now> <status>
 621        my @diff_info_split = split(/[ \t]/, $diff_info);
 622
 623        # Filename, including .mw extension
 624        my $complete_file_name = shift;
 625        # Commit message
 626        my $summary = shift;
 627        # MediaWiki revision number. Keep the previous one by default,
 628        # in case there's no edit to perform.
 629        my $newrevid = shift;
 630
 631        my $new_sha1 = $diff_info_split[3];
 632        my $old_sha1 = $diff_info_split[2];
 633        my $page_created = ($old_sha1 eq NULL_SHA1);
 634        my $page_deleted = ($new_sha1 eq NULL_SHA1);
 635        $complete_file_name = mediawiki_clean_filename($complete_file_name);
 636
 637        if (substr($complete_file_name,-3) eq ".mw") {
 638                my $title = substr($complete_file_name,0,-3);
 639
 640                my $file_content;
 641                if ($page_deleted) {
 642                        # Deleting a page usually requires
 643                        # special priviledges. A common
 644                        # convention is to replace the page
 645                        # with this content instead:
 646                        $file_content = DELETED_CONTENT;
 647                } else {
 648                        $file_content = run_git("cat-file blob $new_sha1");
 649                }
 650
 651                mw_connect_maybe();
 652
 653                my $result = $mediawiki->edit( {
 654                        action => 'edit',
 655                        summary => $summary,
 656                        title => $title,
 657                        basetimestamp => $basetimestamps{$newrevid},
 658                        text => mediawiki_clean($file_content, $page_created),
 659                                  }, {
 660                                          skip_encoding => 1 # Helps with names with accentuated characters
 661                                  });
 662                if (!$result) {
 663                        if ($mediawiki->{error}->{code} == 3) {
 664                                # edit conflicts, considered as non-fast-forward
 665                                print STDERR 'Warning: Error ' .
 666                                    $mediawiki->{error}->{code} .
 667                                    ' from mediwiki: ' . $mediawiki->{error}->{details} .
 668                                    ".\n";
 669                                return ($newrevid, "non-fast-forward");
 670                        } else {
 671                                # Other errors. Shouldn't happen => just die()
 672                                die 'Fatal: Error ' .
 673                                    $mediawiki->{error}->{code} .
 674                                    ' from mediwiki: ' . $mediawiki->{error}->{details};
 675                        }
 676                }
 677                $newrevid = $result->{edit}->{newrevid};
 678                print STDERR "Pushed file: $new_sha1 - $title\n";
 679        } else {
 680                print STDERR "$complete_file_name not a mediawiki file (Not pushable on this version of git-remote-mediawiki).\n"
 681        }
 682        return ($newrevid, "ok");
 683}
 684
 685sub mw_push {
 686        # multiple push statements can follow each other
 687        my @refsspecs = (shift, get_more_refs("push"));
 688        my $pushed;
 689        for my $refspec (@refsspecs) {
 690                my ($force, $local, $remote) = $refspec =~ /^(\+)?([^:]*):([^:]*)$/
 691                    or die("Invalid refspec for push. Expected <src>:<dst> or +<src>:<dst>");
 692                if ($force) {
 693                        print STDERR "Warning: forced push not allowed on a MediaWiki.\n";
 694                }
 695                if ($local eq "") {
 696                        print STDERR "Cannot delete remote branch on a MediaWiki\n";
 697                        print STDOUT "error $remote cannot delete\n";
 698                        next;
 699                }
 700                if ($remote ne "refs/heads/master") {
 701                        print STDERR "Only push to the branch 'master' is supported on a MediaWiki\n";
 702                        print STDOUT "error $remote only master allowed\n";
 703                        next;
 704                }
 705                if (mw_push_revision($local, $remote)) {
 706                        $pushed = 1;
 707                }
 708        }
 709
 710        # Notify Git that the push is done
 711        print STDOUT "\n";
 712
 713        if ($pushed && $dumb_push) {
 714                print STDERR "Just pushed some revisions to MediaWiki.\n";
 715                print STDERR "The pushed revisions now have to be re-imported, and your current branch\n";
 716                print STDERR "needs to be updated with these re-imported commits. You can do this with\n";
 717                print STDERR "\n";
 718                print STDERR "  git pull --rebase\n";
 719                print STDERR "\n";
 720        }
 721}
 722
 723sub mw_push_revision {
 724        my $local = shift;
 725        my $remote = shift; # actually, this has to be "refs/heads/master" at this point.
 726        my $last_local_revid = get_last_local_revision();
 727        print STDERR ".\n"; # Finish sentence started by get_last_local_revision()
 728        my $last_remote_revid = get_last_remote_revision();
 729        my $mw_revision = $last_remote_revid;
 730
 731        # Get sha1 of commit pointed by local HEAD
 732        my $HEAD_sha1 = run_git("rev-parse $local 2>/dev/null"); chomp($HEAD_sha1);
 733        # Get sha1 of commit pointed by remotes/$remotename/master
 734        my $remoteorigin_sha1 = run_git("rev-parse refs/remotes/$remotename/master 2>/dev/null");
 735        chomp($remoteorigin_sha1);
 736
 737        if ($last_local_revid > 0 &&
 738            $last_local_revid < $last_remote_revid) {
 739                return error_non_fast_forward($remote);
 740        }
 741
 742        if ($HEAD_sha1 eq $remoteorigin_sha1) {
 743                # nothing to push
 744                return 0;
 745        }
 746
 747        # Get every commit in between HEAD and refs/remotes/origin/master,
 748        # including HEAD and refs/remotes/origin/master
 749        my @commit_pairs = ();
 750        if ($last_local_revid > 0) {
 751                my $parsed_sha1 = $remoteorigin_sha1;
 752                # Find a path from last MediaWiki commit to pushed commit
 753                while ($parsed_sha1 ne $HEAD_sha1) {
 754                        my @commit_info =  grep(/^$parsed_sha1/, split(/\n/, run_git("rev-list --children $local")));
 755                        if (!@commit_info) {
 756                                return error_non_fast_forward($remote);
 757                        }
 758                        my @commit_info_split = split(/ |\n/, $commit_info[0]);
 759                        # $commit_info_split[1] is the sha1 of the commit to export
 760                        # $commit_info_split[0] is the sha1 of its direct child
 761                        push(@commit_pairs, \@commit_info_split);
 762                        $parsed_sha1 = $commit_info_split[1];
 763                }
 764        } else {
 765                # No remote mediawiki revision. Export the whole
 766                # history (linearized with --first-parent)
 767                print STDERR "Warning: no common ancestor, pushing complete history\n";
 768                my $history = run_git("rev-list --first-parent --children $local");
 769                my @history = split('\n', $history);
 770                @history = @history[1..$#history];
 771                foreach my $line (reverse @history) {
 772                        my @commit_info_split = split(/ |\n/, $line);
 773                        push(@commit_pairs, \@commit_info_split);
 774                }
 775        }
 776
 777        foreach my $commit_info_split (@commit_pairs) {
 778                my $sha1_child = @{$commit_info_split}[0];
 779                my $sha1_commit = @{$commit_info_split}[1];
 780                my $diff_infos = run_git("diff-tree -r --raw -z $sha1_child $sha1_commit");
 781                # TODO: we could detect rename, and encode them with a #redirect on the wiki.
 782                # TODO: for now, it's just a delete+add
 783                my @diff_info_list = split(/\0/, $diff_infos);
 784                # Keep the first line of the commit message as mediawiki comment for the revision
 785                my $commit_msg = (split(/\n/, run_git("show --pretty=format:\"%s\" $sha1_commit")))[0];
 786                chomp($commit_msg);
 787                # Push every blob
 788                while (@diff_info_list) {
 789                        my $status;
 790                        # git diff-tree -z gives an output like
 791                        # <metadata>\0<filename1>\0
 792                        # <metadata>\0<filename2>\0
 793                        # and we've split on \0.
 794                        my $info = shift(@diff_info_list);
 795                        my $file = shift(@diff_info_list);
 796                        ($mw_revision, $status) = mw_push_file($info, $file, $commit_msg, $mw_revision);
 797                        if ($status eq "non-fast-forward") {
 798                                # we may already have sent part of the
 799                                # commit to MediaWiki, but it's too
 800                                # late to cancel it. Stop the push in
 801                                # the middle, but still give an
 802                                # accurate error message.
 803                                return error_non_fast_forward($remote);
 804                        }
 805                        if ($status ne "ok") {
 806                                die("Unknown error from mw_push_file()");
 807                        }
 808                }
 809                unless ($dumb_push) {
 810                        run_git("notes --ref=$remotename/mediawiki add -m \"mediawiki_revision: $mw_revision\" $sha1_commit");
 811                        run_git("update-ref -m \"Git-MediaWiki push\" refs/mediawiki/$remotename/master $sha1_commit $sha1_child");
 812                }
 813        }
 814
 815        print STDOUT "ok $remote\n";
 816        return 1;
 817}