contrib / mw-to-git / git-remote-mediawiki.perlon commit Merge branch 'jk/push-scrub-url' into maint (71076e1)
   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# Documentation & bugtracker: https://github.com/moy/Git-Mediawiki/
  13
  14use strict;
  15use MediaWiki::API;
  16use Git;
  17use Git::Mediawiki qw(clean_filename smudge_filename connect_maybe
  18                                        EMPTY HTTP_CODE_OK);
  19use DateTime::Format::ISO8601;
  20use warnings;
  21
  22# By default, use UTF-8 to communicate with Git and the user
  23binmode STDERR, ':encoding(UTF-8)';
  24binmode STDOUT, ':encoding(UTF-8)';
  25
  26use URI::Escape;
  27
  28# It's not always possible to delete pages (may require some
  29# privileges). Deleted pages are replaced with this content.
  30use constant DELETED_CONTENT => "[[Category:Deleted]]\n";
  31
  32# It's not possible to create empty pages. New empty files in Git are
  33# sent with this content instead.
  34use constant EMPTY_CONTENT => "<!-- empty page -->\n";
  35
  36# used to reflect file creation or deletion in diff.
  37use constant NULL_SHA1 => '0000000000000000000000000000000000000000';
  38
  39# Used on Git's side to reflect empty edit messages on the wiki
  40use constant EMPTY_MESSAGE => '*Empty MediaWiki Message*';
  41
  42# Number of pages taken into account at once in submodule get_mw_page_list
  43use constant SLICE_SIZE => 50;
  44
  45# Number of linked mediafile to get at once in get_linked_mediafiles
  46# The query is split in small batches because of the MW API limit of
  47# the number of links to be returned (500 links max).
  48use constant BATCH_SIZE => 10;
  49
  50if (@ARGV != 2) {
  51        exit_error_usage();
  52}
  53
  54my $remotename = $ARGV[0];
  55my $url = $ARGV[1];
  56
  57# Accept both space-separated and multiple keys in config file.
  58# Spaces should be written as _ anyway because we'll use chomp.
  59my @tracked_pages = split(/[ \n]/, run_git("config --get-all remote.${remotename}.pages"));
  60chomp(@tracked_pages);
  61
  62# Just like @tracked_pages, but for MediaWiki categories.
  63my @tracked_categories = split(/[ \n]/, run_git("config --get-all remote.${remotename}.categories"));
  64chomp(@tracked_categories);
  65
  66# Import media files on pull
  67my $import_media = run_git("config --get --bool remote.${remotename}.mediaimport");
  68chomp($import_media);
  69$import_media = ($import_media eq 'true');
  70
  71# Export media files on push
  72my $export_media = run_git("config --get --bool remote.${remotename}.mediaexport");
  73chomp($export_media);
  74$export_media = !($export_media eq 'false');
  75
  76my $wiki_login = run_git("config --get remote.${remotename}.mwLogin");
  77# Note: mwPassword is discourraged. Use the credential system instead.
  78my $wiki_passwd = run_git("config --get remote.${remotename}.mwPassword");
  79my $wiki_domain = run_git("config --get remote.${remotename}.mwDomain");
  80chomp($wiki_login);
  81chomp($wiki_passwd);
  82chomp($wiki_domain);
  83
  84# Import only last revisions (both for clone and fetch)
  85my $shallow_import = run_git("config --get --bool remote.${remotename}.shallow");
  86chomp($shallow_import);
  87$shallow_import = ($shallow_import eq 'true');
  88
  89# Fetch (clone and pull) by revisions instead of by pages. This behavior
  90# is more efficient when we have a wiki with lots of pages and we fetch
  91# the revisions quite often so that they concern only few pages.
  92# Possible values:
  93# - by_rev: perform one query per new revision on the remote wiki
  94# - by_page: query each tracked page for new revision
  95my $fetch_strategy = run_git("config --get remote.${remotename}.fetchStrategy");
  96if (!$fetch_strategy) {
  97        $fetch_strategy = run_git('config --get mediawiki.fetchStrategy');
  98}
  99chomp($fetch_strategy);
 100if (!$fetch_strategy) {
 101        $fetch_strategy = 'by_page';
 102}
 103
 104# Remember the timestamp corresponding to a revision id.
 105my %basetimestamps;
 106
 107# Dumb push: don't update notes and mediawiki ref to reflect the last push.
 108#
 109# Configurable with mediawiki.dumbPush, or per-remote with
 110# remote.<remotename>.dumbPush.
 111#
 112# This means the user will have to re-import the just-pushed
 113# revisions. On the other hand, this means that the Git revisions
 114# corresponding to MediaWiki revisions are all imported from the wiki,
 115# regardless of whether they were initially created in Git or from the
 116# web interface, hence all users will get the same history (i.e. if
 117# the push from Git to MediaWiki loses some information, everybody
 118# will get the history with information lost). If the import is
 119# deterministic, this means everybody gets the same sha1 for each
 120# MediaWiki revision.
 121my $dumb_push = run_git("config --get --bool remote.${remotename}.dumbPush");
 122if (!$dumb_push) {
 123        $dumb_push = run_git('config --get --bool mediawiki.dumbPush');
 124}
 125chomp($dumb_push);
 126$dumb_push = ($dumb_push eq 'true');
 127
 128my $wiki_name = $url;
 129$wiki_name =~ s{[^/]*://}{};
 130# If URL is like http://user:password@example.com/, we clearly don't
 131# want the password in $wiki_name. While we're there, also remove user
 132# and '@' sign, to avoid author like MWUser@HTTPUser@host.com
 133$wiki_name =~ s/^.*@//;
 134
 135# Commands parser
 136while (<STDIN>) {
 137        chomp;
 138
 139        if (!parse_command($_)) {
 140                last;
 141        }
 142
 143        BEGIN { $| = 1 } # flush STDOUT, to make sure the previous
 144                         # command is fully processed.
 145}
 146
 147########################## Functions ##############################
 148
 149## error handling
 150sub exit_error_usage {
 151        die "ERROR: git-remote-mediawiki module was not called with a correct number of\n" .
 152            "parameters\n" .
 153            "You may obtain this error because you attempted to run the git-remote-mediawiki\n" .
 154            "module directly.\n" .
 155            "This module can be used the following way:\n" .
 156            "\tgit clone mediawiki://<address of a mediawiki>\n" .
 157            "Then, use git commit, push and pull as with every normal git repository.\n";
 158}
 159
 160sub parse_command {
 161        my ($line) = @_;
 162        my @cmd = split(/ /, $line);
 163        if (!defined $cmd[0]) {
 164                return 0;
 165        }
 166        if ($cmd[0] eq 'capabilities') {
 167                die("Too many arguments for capabilities\n")
 168                    if (defined($cmd[1]));
 169                mw_capabilities();
 170        } elsif ($cmd[0] eq 'list') {
 171                die("Too many arguments for list\n") if (defined($cmd[2]));
 172                mw_list($cmd[1]);
 173        } elsif ($cmd[0] eq 'import') {
 174                die("Invalid argument for import\n")
 175                    if ($cmd[1] eq EMPTY);
 176                die("Too many arguments for import\n")
 177                    if (defined($cmd[2]));
 178                mw_import($cmd[1]);
 179        } elsif ($cmd[0] eq 'option') {
 180                die("Invalid arguments for option\n")
 181                    if ($cmd[1] eq EMPTY || $cmd[2] eq EMPTY);
 182                die("Too many arguments for option\n")
 183                    if (defined($cmd[3]));
 184                mw_option($cmd[1],$cmd[2]);
 185        } elsif ($cmd[0] eq 'push') {
 186                mw_push($cmd[1]);
 187        } else {
 188                print {*STDERR} "Unknown command. Aborting...\n";
 189                return 0;
 190        }
 191        return 1;
 192}
 193
 194# MediaWiki API instance, created lazily.
 195my $mediawiki;
 196
 197sub fatal_mw_error {
 198        my $action = shift;
 199        print STDERR "fatal: could not $action.\n";
 200        print STDERR "fatal: '$url' does not appear to be a mediawiki\n";
 201        if ($url =~ /^https/) {
 202                print STDERR "fatal: make sure '$url/api.php' is a valid page\n";
 203                print STDERR "fatal: and the SSL certificate is correct.\n";
 204        } else {
 205                print STDERR "fatal: make sure '$url/api.php' is a valid page.\n";
 206        }
 207        print STDERR "fatal: (error " .
 208            $mediawiki->{error}->{code} . ': ' .
 209            $mediawiki->{error}->{details} . ")\n";
 210        exit 1;
 211}
 212
 213## Functions for listing pages on the remote wiki
 214sub get_mw_tracked_pages {
 215        my $pages = shift;
 216        get_mw_page_list(\@tracked_pages, $pages);
 217        return;
 218}
 219
 220sub get_mw_page_list {
 221        my $page_list = shift;
 222        my $pages = shift;
 223        my @some_pages = @{$page_list};
 224        while (@some_pages) {
 225                my $last_page = SLICE_SIZE;
 226                if ($#some_pages < $last_page) {
 227                        $last_page = $#some_pages;
 228                }
 229                my @slice = @some_pages[0..$last_page];
 230                get_mw_first_pages(\@slice, $pages);
 231                @some_pages = @some_pages[(SLICE_SIZE + 1)..$#some_pages];
 232        }
 233        return;
 234}
 235
 236sub get_mw_tracked_categories {
 237        my $pages = shift;
 238        foreach my $category (@tracked_categories) {
 239                if (index($category, ':') < 0) {
 240                        # Mediawiki requires the Category
 241                        # prefix, but let's not force the user
 242                        # to specify it.
 243                        $category = "Category:${category}";
 244                }
 245                my $mw_pages = $mediawiki->list( {
 246                        action => 'query',
 247                        list => 'categorymembers',
 248                        cmtitle => $category,
 249                        cmlimit => 'max' } )
 250                        || die $mediawiki->{error}->{code} . ': '
 251                                . $mediawiki->{error}->{details} . "\n";
 252                foreach my $page (@{$mw_pages}) {
 253                        $pages->{$page->{title}} = $page;
 254                }
 255        }
 256        return;
 257}
 258
 259sub get_mw_all_pages {
 260        my $pages = shift;
 261        # No user-provided list, get the list of pages from the API.
 262        my $mw_pages = $mediawiki->list({
 263                action => 'query',
 264                list => 'allpages',
 265                aplimit => 'max'
 266        });
 267        if (!defined($mw_pages)) {
 268                fatal_mw_error("get the list of wiki pages");
 269        }
 270        foreach my $page (@{$mw_pages}) {
 271                $pages->{$page->{title}} = $page;
 272        }
 273        return;
 274}
 275
 276# queries the wiki for a set of pages. Meant to be used within a loop
 277# querying the wiki for slices of page list.
 278sub get_mw_first_pages {
 279        my $some_pages = shift;
 280        my @some_pages = @{$some_pages};
 281
 282        my $pages = shift;
 283
 284        # pattern 'page1|page2|...' required by the API
 285        my $titles = join('|', @some_pages);
 286
 287        my $mw_pages = $mediawiki->api({
 288                action => 'query',
 289                titles => $titles,
 290        });
 291        if (!defined($mw_pages)) {
 292                fatal_mw_error("query the list of wiki pages");
 293        }
 294        while (my ($id, $page) = each(%{$mw_pages->{query}->{pages}})) {
 295                if ($id < 0) {
 296                        print {*STDERR} "Warning: page $page->{title} not found on wiki\n";
 297                } else {
 298                        $pages->{$page->{title}} = $page;
 299                }
 300        }
 301        return;
 302}
 303
 304# Get the list of pages to be fetched according to configuration.
 305sub get_mw_pages {
 306        $mediawiki = connect_maybe($mediawiki, $remotename, $url);
 307
 308        print {*STDERR} "Listing pages on remote wiki...\n";
 309
 310        my %pages; # hash on page titles to avoid duplicates
 311        my $user_defined;
 312        if (@tracked_pages) {
 313                $user_defined = 1;
 314                # The user provided a list of pages titles, but we
 315                # still need to query the API to get the page IDs.
 316                get_mw_tracked_pages(\%pages);
 317        }
 318        if (@tracked_categories) {
 319                $user_defined = 1;
 320                get_mw_tracked_categories(\%pages);
 321        }
 322        if (!$user_defined) {
 323                get_mw_all_pages(\%pages);
 324        }
 325        if ($import_media) {
 326                print {*STDERR} "Getting media files for selected pages...\n";
 327                if ($user_defined) {
 328                        get_linked_mediafiles(\%pages);
 329                } else {
 330                        get_all_mediafiles(\%pages);
 331                }
 332        }
 333        print {*STDERR} (scalar keys %pages) . " pages found.\n";
 334        return %pages;
 335}
 336
 337# usage: $out = run_git("command args");
 338#        $out = run_git("command args", "raw"); # don't interpret output as UTF-8.
 339sub run_git {
 340        my $args = shift;
 341        my $encoding = (shift || 'encoding(UTF-8)');
 342        open(my $git, "-|:${encoding}", "git ${args}")
 343            or die "Unable to fork: $!\n";
 344        my $res = do {
 345                local $/ = undef;
 346                <$git>
 347        };
 348        close($git);
 349
 350        return $res;
 351}
 352
 353
 354sub get_all_mediafiles {
 355        my $pages = shift;
 356        # Attach list of all pages for media files from the API,
 357        # they are in a different namespace, only one namespace
 358        # can be queried at the same moment
 359        my $mw_pages = $mediawiki->list({
 360                action => 'query',
 361                list => 'allpages',
 362                apnamespace => get_mw_namespace_id('File'),
 363                aplimit => 'max'
 364        });
 365        if (!defined($mw_pages)) {
 366                print {*STDERR} "fatal: could not get the list of pages for media files.\n";
 367                print {*STDERR} "fatal: '$url' does not appear to be a mediawiki\n";
 368                print {*STDERR} "fatal: make sure '$url/api.php' is a valid page.\n";
 369                exit 1;
 370        }
 371        foreach my $page (@{$mw_pages}) {
 372                $pages->{$page->{title}} = $page;
 373        }
 374        return;
 375}
 376
 377sub get_linked_mediafiles {
 378        my $pages = shift;
 379        my @titles = map { $_->{title} } values(%{$pages});
 380
 381        my $batch = BATCH_SIZE;
 382        while (@titles) {
 383                if ($#titles < $batch) {
 384                        $batch = $#titles;
 385                }
 386                my @slice = @titles[0..$batch];
 387
 388                # pattern 'page1|page2|...' required by the API
 389                my $mw_titles = join('|', @slice);
 390
 391                # Media files could be included or linked from
 392                # a page, get all related
 393                my $query = {
 394                        action => 'query',
 395                        prop => 'links|images',
 396                        titles => $mw_titles,
 397                        plnamespace => get_mw_namespace_id('File'),
 398                        pllimit => 'max'
 399                };
 400                my $result = $mediawiki->api($query);
 401
 402                while (my ($id, $page) = each(%{$result->{query}->{pages}})) {
 403                        my @media_titles;
 404                        if (defined($page->{links})) {
 405                                my @link_titles
 406                                    = map { $_->{title} } @{$page->{links}};
 407                                push(@media_titles, @link_titles);
 408                        }
 409                        if (defined($page->{images})) {
 410                                my @image_titles
 411                                    = map { $_->{title} } @{$page->{images}};
 412                                push(@media_titles, @image_titles);
 413                        }
 414                        if (@media_titles) {
 415                                get_mw_page_list(\@media_titles, $pages);
 416                        }
 417                }
 418
 419                @titles = @titles[($batch+1)..$#titles];
 420        }
 421        return;
 422}
 423
 424sub get_mw_mediafile_for_page_revision {
 425        # Name of the file on Wiki, with the prefix.
 426        my $filename = shift;
 427        my $timestamp = shift;
 428        my %mediafile;
 429
 430        # Search if on a media file with given timestamp exists on
 431        # MediaWiki. In that case download the file.
 432        my $query = {
 433                action => 'query',
 434                prop => 'imageinfo',
 435                titles => "File:${filename}",
 436                iistart => $timestamp,
 437                iiend => $timestamp,
 438                iiprop => 'timestamp|archivename|url',
 439                iilimit => 1
 440        };
 441        my $result = $mediawiki->api($query);
 442
 443        my ($fileid, $file) = each( %{$result->{query}->{pages}} );
 444        # If not defined it means there is no revision of the file for
 445        # given timestamp.
 446        if (defined($file->{imageinfo})) {
 447                $mediafile{title} = $filename;
 448
 449                my $fileinfo = pop(@{$file->{imageinfo}});
 450                $mediafile{timestamp} = $fileinfo->{timestamp};
 451                # Mediawiki::API's download function doesn't support https URLs
 452                # and can't download old versions of files.
 453                print {*STDERR} "\tDownloading file $mediafile{title}, version $mediafile{timestamp}\n";
 454                $mediafile{content} = download_mw_mediafile($fileinfo->{url});
 455        }
 456        return %mediafile;
 457}
 458
 459sub download_mw_mediafile {
 460        my $download_url = shift;
 461
 462        my $response = $mediawiki->{ua}->get($download_url);
 463        if ($response->code == HTTP_CODE_OK) {
 464                # It is tempting to return
 465                # $response->decoded_content({charset => "none"}), but
 466                # when doing so, utf8::downgrade($content) fails with
 467                # "Wide character in subroutine entry".
 468                $response->decode();
 469                return $response->content();
 470        } else {
 471                print {*STDERR} "Error downloading mediafile from :\n";
 472                print {*STDERR} "URL: ${download_url}\n";
 473                print {*STDERR} 'Server response: ' . $response->code . q{ } . $response->message . "\n";
 474                exit 1;
 475        }
 476}
 477
 478sub get_last_local_revision {
 479        # Get note regarding last mediawiki revision
 480        my $note = run_git("notes --ref=${remotename}/mediawiki show refs/mediawiki/${remotename}/master 2>/dev/null");
 481        my @note_info = split(/ /, $note);
 482
 483        my $lastrevision_number;
 484        if (!(defined($note_info[0]) && $note_info[0] eq 'mediawiki_revision:')) {
 485                print {*STDERR} 'No previous mediawiki revision found';
 486                $lastrevision_number = 0;
 487        } else {
 488                # Notes are formatted : mediawiki_revision: #number
 489                $lastrevision_number = $note_info[1];
 490                chomp($lastrevision_number);
 491                print {*STDERR} "Last local mediawiki revision found is ${lastrevision_number}";
 492        }
 493        return $lastrevision_number;
 494}
 495
 496# Get the last remote revision without taking in account which pages are
 497# tracked or not. This function makes a single request to the wiki thus
 498# avoid a loop onto all tracked pages. This is useful for the fetch-by-rev
 499# option.
 500sub get_last_global_remote_rev {
 501        $mediawiki = connect_maybe($mediawiki, $remotename, $url);
 502
 503        my $query = {
 504                action => 'query',
 505                list => 'recentchanges',
 506                prop => 'revisions',
 507                rclimit => '1',
 508                rcdir => 'older',
 509        };
 510        my $result = $mediawiki->api($query);
 511        return $result->{query}->{recentchanges}[0]->{revid};
 512}
 513
 514# Get the last remote revision concerning the tracked pages and the tracked
 515# categories.
 516sub get_last_remote_revision {
 517        $mediawiki = connect_maybe($mediawiki, $remotename, $url);
 518
 519        my %pages_hash = get_mw_pages();
 520        my @pages = values(%pages_hash);
 521
 522        my $max_rev_num = 0;
 523
 524        print {*STDERR} "Getting last revision id on tracked pages...\n";
 525
 526        foreach my $page (@pages) {
 527                my $id = $page->{pageid};
 528
 529                my $query = {
 530                        action => 'query',
 531                        prop => 'revisions',
 532                        rvprop => 'ids|timestamp',
 533                        pageids => $id,
 534                };
 535
 536                my $result = $mediawiki->api($query);
 537
 538                my $lastrev = pop(@{$result->{query}->{pages}->{$id}->{revisions}});
 539
 540                $basetimestamps{$lastrev->{revid}} = $lastrev->{timestamp};
 541
 542                $max_rev_num = ($lastrev->{revid} > $max_rev_num ? $lastrev->{revid} : $max_rev_num);
 543        }
 544
 545        print {*STDERR} "Last remote revision found is $max_rev_num.\n";
 546        return $max_rev_num;
 547}
 548
 549# Clean content before sending it to MediaWiki
 550sub mediawiki_clean {
 551        my $string = shift;
 552        my $page_created = shift;
 553        # Mediawiki does not allow blank space at the end of a page and ends with a single \n.
 554        # This function right trims a string and adds a \n at the end to follow this rule
 555        $string =~ s/\s+$//;
 556        if ($string eq EMPTY && $page_created) {
 557                # Creating empty pages is forbidden.
 558                $string = EMPTY_CONTENT;
 559        }
 560        return $string."\n";
 561}
 562
 563# Filter applied on MediaWiki data before adding them to Git
 564sub mediawiki_smudge {
 565        my $string = shift;
 566        if ($string eq EMPTY_CONTENT) {
 567                $string = EMPTY;
 568        }
 569        # This \n is important. This is due to mediawiki's way to handle end of files.
 570        return "${string}\n";
 571}
 572
 573sub literal_data {
 574        my ($content) = @_;
 575        print {*STDOUT} 'data ', bytes::length($content), "\n", $content;
 576        return;
 577}
 578
 579sub literal_data_raw {
 580        # Output possibly binary content.
 581        my ($content) = @_;
 582        # Avoid confusion between size in bytes and in characters
 583        utf8::downgrade($content);
 584        binmode STDOUT, ':raw';
 585        print {*STDOUT} 'data ', bytes::length($content), "\n", $content;
 586        binmode STDOUT, ':encoding(UTF-8)';
 587        return;
 588}
 589
 590sub mw_capabilities {
 591        # Revisions are imported to the private namespace
 592        # refs/mediawiki/$remotename/ by the helper and fetched into
 593        # refs/remotes/$remotename later by fetch.
 594        print {*STDOUT} "refspec refs/heads/*:refs/mediawiki/${remotename}/*\n";
 595        print {*STDOUT} "import\n";
 596        print {*STDOUT} "list\n";
 597        print {*STDOUT} "push\n";
 598        if ($dumb_push) {
 599                print {*STDOUT} "no-private-update\n";
 600        }
 601        print {*STDOUT} "\n";
 602        return;
 603}
 604
 605sub mw_list {
 606        # MediaWiki do not have branches, we consider one branch arbitrarily
 607        # called master, and HEAD pointing to it.
 608        print {*STDOUT} "? refs/heads/master\n";
 609        print {*STDOUT} "\@refs/heads/master HEAD\n";
 610        print {*STDOUT} "\n";
 611        return;
 612}
 613
 614sub mw_option {
 615        print {*STDERR} "remote-helper command 'option $_[0]' not yet implemented\n";
 616        print {*STDOUT} "unsupported\n";
 617        return;
 618}
 619
 620sub fetch_mw_revisions_for_page {
 621        my $page = shift;
 622        my $id = shift;
 623        my $fetch_from = shift;
 624        my @page_revs = ();
 625        my $query = {
 626                action => 'query',
 627                prop => 'revisions',
 628                rvprop => 'ids',
 629                rvdir => 'newer',
 630                rvstartid => $fetch_from,
 631                rvlimit => 500,
 632                pageids => $id,
 633
 634                # Let MediaWiki know that we support the latest API.
 635                continue => '',
 636        };
 637
 638        my $revnum = 0;
 639        # Get 500 revisions at a time due to the mediawiki api limit
 640        while (1) {
 641                my $result = $mediawiki->api($query);
 642
 643                # Parse each of those 500 revisions
 644                foreach my $revision (@{$result->{query}->{pages}->{$id}->{revisions}}) {
 645                        my $page_rev_ids;
 646                        $page_rev_ids->{pageid} = $page->{pageid};
 647                        $page_rev_ids->{revid} = $revision->{revid};
 648                        push(@page_revs, $page_rev_ids);
 649                        $revnum++;
 650                }
 651
 652                if ($result->{'query-continue'}) { # For legacy APIs
 653                        $query->{rvstartid} = $result->{'query-continue'}->{revisions}->{rvstartid};
 654                } elsif ($result->{continue}) { # For newer APIs
 655                        $query->{rvstartid} = $result->{continue}->{rvcontinue};
 656                        $query->{continue} = $result->{continue}->{continue};
 657                } else {
 658                        last;
 659                }
 660        }
 661        if ($shallow_import && @page_revs) {
 662                print {*STDERR} "  Found 1 revision (shallow import).\n";
 663                @page_revs = sort {$b->{revid} <=> $a->{revid}} (@page_revs);
 664                return $page_revs[0];
 665        }
 666        print {*STDERR} "  Found ${revnum} revision(s).\n";
 667        return @page_revs;
 668}
 669
 670sub fetch_mw_revisions {
 671        my $pages = shift; my @pages = @{$pages};
 672        my $fetch_from = shift;
 673
 674        my @revisions = ();
 675        my $n = 1;
 676        foreach my $page (@pages) {
 677                my $id = $page->{pageid};
 678                print {*STDERR} "page ${n}/", scalar(@pages), ': ', $page->{title}, "\n";
 679                $n++;
 680                my @page_revs = fetch_mw_revisions_for_page($page, $id, $fetch_from);
 681                @revisions = (@page_revs, @revisions);
 682        }
 683
 684        return ($n, @revisions);
 685}
 686
 687sub fe_escape_path {
 688    my $path = shift;
 689    $path =~ s/\\/\\\\/g;
 690    $path =~ s/"/\\"/g;
 691    $path =~ s/\n/\\n/g;
 692    return qq("${path}");
 693}
 694
 695sub import_file_revision {
 696        my $commit = shift;
 697        my %commit = %{$commit};
 698        my $full_import = shift;
 699        my $n = shift;
 700        my $mediafile = shift;
 701        my %mediafile;
 702        if ($mediafile) {
 703                %mediafile = %{$mediafile};
 704        }
 705
 706        my $title = $commit{title};
 707        my $comment = $commit{comment};
 708        my $content = $commit{content};
 709        my $author = $commit{author};
 710        my $date = $commit{date};
 711
 712        print {*STDOUT} "commit refs/mediawiki/${remotename}/master\n";
 713        print {*STDOUT} "mark :${n}\n";
 714        print {*STDOUT} "committer ${author} <${author}\@${wiki_name}> " . $date->epoch . " +0000\n";
 715        literal_data($comment);
 716
 717        # If it's not a clone, we need to know where to start from
 718        if (!$full_import && $n == 1) {
 719                print {*STDOUT} "from refs/mediawiki/${remotename}/master^0\n";
 720        }
 721        if ($content ne DELETED_CONTENT) {
 722                print {*STDOUT} 'M 644 inline ' .
 723                    fe_escape_path("${title}.mw") . "\n";
 724                literal_data($content);
 725                if (%mediafile) {
 726                        print {*STDOUT} 'M 644 inline '
 727                            . fe_escape_path($mediafile{title}) . "\n";
 728                        literal_data_raw($mediafile{content});
 729                }
 730                print {*STDOUT} "\n\n";
 731        } else {
 732                print {*STDOUT} 'D ' . fe_escape_path("${title}.mw") . "\n";
 733        }
 734
 735        # mediawiki revision number in the git note
 736        if ($full_import && $n == 1) {
 737                print {*STDOUT} "reset refs/notes/${remotename}/mediawiki\n";
 738        }
 739        print {*STDOUT} "commit refs/notes/${remotename}/mediawiki\n";
 740        print {*STDOUT} "committer ${author} <${author}\@${wiki_name}> " . $date->epoch . " +0000\n";
 741        literal_data('Note added by git-mediawiki during import');
 742        if (!$full_import && $n == 1) {
 743                print {*STDOUT} "from refs/notes/${remotename}/mediawiki^0\n";
 744        }
 745        print {*STDOUT} "N inline :${n}\n";
 746        literal_data("mediawiki_revision: $commit{mw_revision}");
 747        print {*STDOUT} "\n\n";
 748        return;
 749}
 750
 751# parse a sequence of
 752# <cmd> <arg1>
 753# <cmd> <arg2>
 754# \n
 755# (like batch sequence of import and sequence of push statements)
 756sub get_more_refs {
 757        my $cmd = shift;
 758        my @refs;
 759        while (1) {
 760                my $line = <STDIN>;
 761                if ($line =~ /^$cmd (.*)$/) {
 762                        push(@refs, $1);
 763                } elsif ($line eq "\n") {
 764                        return @refs;
 765                } else {
 766                        die("Invalid command in a '$cmd' batch: $_\n");
 767                }
 768        }
 769        return;
 770}
 771
 772sub mw_import {
 773        # multiple import commands can follow each other.
 774        my @refs = (shift, get_more_refs('import'));
 775        foreach my $ref (@refs) {
 776                mw_import_ref($ref);
 777        }
 778        print {*STDOUT} "done\n";
 779        return;
 780}
 781
 782sub mw_import_ref {
 783        my $ref = shift;
 784        # The remote helper will call "import HEAD" and
 785        # "import refs/heads/master".
 786        # Since HEAD is a symbolic ref to master (by convention,
 787        # followed by the output of the command "list" that we gave),
 788        # we don't need to do anything in this case.
 789        if ($ref eq 'HEAD') {
 790                return;
 791        }
 792
 793        $mediawiki = connect_maybe($mediawiki, $remotename, $url);
 794
 795        print {*STDERR} "Searching revisions...\n";
 796        my $last_local = get_last_local_revision();
 797        my $fetch_from = $last_local + 1;
 798        if ($fetch_from == 1) {
 799                print {*STDERR} ", fetching from beginning.\n";
 800        } else {
 801                print {*STDERR} ", fetching from here.\n";
 802        }
 803
 804        my $n = 0;
 805        if ($fetch_strategy eq 'by_rev') {
 806                print {*STDERR} "Fetching & writing export data by revs...\n";
 807                $n = mw_import_ref_by_revs($fetch_from);
 808        } elsif ($fetch_strategy eq 'by_page') {
 809                print {*STDERR} "Fetching & writing export data by pages...\n";
 810                $n = mw_import_ref_by_pages($fetch_from);
 811        } else {
 812                print {*STDERR} qq(fatal: invalid fetch strategy "${fetch_strategy}".\n);
 813                print {*STDERR} "Check your configuration variables remote.${remotename}.fetchStrategy and mediawiki.fetchStrategy\n";
 814                exit 1;
 815        }
 816
 817        if ($fetch_from == 1 && $n == 0) {
 818                print {*STDERR} "You appear to have cloned an empty MediaWiki.\n";
 819                # Something has to be done remote-helper side. If nothing is done, an error is
 820                # thrown saying that HEAD is referring to unknown object 0000000000000000000
 821                # and the clone fails.
 822        }
 823        return;
 824}
 825
 826sub mw_import_ref_by_pages {
 827
 828        my $fetch_from = shift;
 829        my %pages_hash = get_mw_pages();
 830        my @pages = values(%pages_hash);
 831
 832        my ($n, @revisions) = fetch_mw_revisions(\@pages, $fetch_from);
 833
 834        @revisions = sort {$a->{revid} <=> $b->{revid}} @revisions;
 835        my @revision_ids = map { $_->{revid} } @revisions;
 836
 837        return mw_import_revids($fetch_from, \@revision_ids, \%pages_hash);
 838}
 839
 840sub mw_import_ref_by_revs {
 841
 842        my $fetch_from = shift;
 843        my %pages_hash = get_mw_pages();
 844
 845        my $last_remote = get_last_global_remote_rev();
 846        my @revision_ids = $fetch_from..$last_remote;
 847        return mw_import_revids($fetch_from, \@revision_ids, \%pages_hash);
 848}
 849
 850# Import revisions given in second argument (array of integers).
 851# Only pages appearing in the third argument (hash indexed by page titles)
 852# will be imported.
 853sub mw_import_revids {
 854        my $fetch_from = shift;
 855        my $revision_ids = shift;
 856        my $pages = shift;
 857
 858        my $n = 0;
 859        my $n_actual = 0;
 860        my $last_timestamp = 0; # Placeholer in case $rev->timestamp is undefined
 861
 862        foreach my $pagerevid (@{$revision_ids}) {
 863                # Count page even if we skip it, since we display
 864                # $n/$total and $total includes skipped pages.
 865                $n++;
 866
 867                # fetch the content of the pages
 868                my $query = {
 869                        action => 'query',
 870                        prop => 'revisions',
 871                        rvprop => 'content|timestamp|comment|user|ids',
 872                        revids => $pagerevid,
 873                };
 874
 875                my $result = $mediawiki->api($query);
 876
 877                if (!$result) {
 878                        die "Failed to retrieve modified page for revision $pagerevid\n";
 879                }
 880
 881                if (defined($result->{query}->{badrevids}->{$pagerevid})) {
 882                        # The revision id does not exist on the remote wiki.
 883                        next;
 884                }
 885
 886                if (!defined($result->{query}->{pages})) {
 887                        die "Invalid revision ${pagerevid}.\n";
 888                }
 889
 890                my @result_pages = values(%{$result->{query}->{pages}});
 891                my $result_page = $result_pages[0];
 892                my $rev = $result_pages[0]->{revisions}->[0];
 893
 894                my $page_title = $result_page->{title};
 895
 896                if (!exists($pages->{$page_title})) {
 897                        print {*STDERR} "${n}/", scalar(@{$revision_ids}),
 898                                ": Skipping revision #$rev->{revid} of ${page_title}\n";
 899                        next;
 900                }
 901
 902                $n_actual++;
 903
 904                my %commit;
 905                $commit{author} = $rev->{user} || 'Anonymous';
 906                $commit{comment} = $rev->{comment} || EMPTY_MESSAGE;
 907                $commit{title} = smudge_filename($page_title);
 908                $commit{mw_revision} = $rev->{revid};
 909                $commit{content} = mediawiki_smudge($rev->{'*'});
 910
 911                if (!defined($rev->{timestamp})) {
 912                        $last_timestamp++;
 913                } else {
 914                        $last_timestamp = $rev->{timestamp};
 915                }
 916                $commit{date} = DateTime::Format::ISO8601->parse_datetime($last_timestamp);
 917
 918                # Differentiates classic pages and media files.
 919                my ($namespace, $filename) = $page_title =~ /^([^:]*):(.*)$/;
 920                my %mediafile;
 921                if ($namespace) {
 922                        my $id = get_mw_namespace_id($namespace);
 923                        if ($id && $id == get_mw_namespace_id('File')) {
 924                                %mediafile = get_mw_mediafile_for_page_revision($filename, $rev->{timestamp});
 925                        }
 926                }
 927                # If this is a revision of the media page for new version
 928                # of a file do one common commit for both file and media page.
 929                # Else do commit only for that page.
 930                print {*STDERR} "${n}/", scalar(@{$revision_ids}), ": Revision #$rev->{revid} of $commit{title}\n";
 931                import_file_revision(\%commit, ($fetch_from == 1), $n_actual, \%mediafile);
 932        }
 933
 934        return $n_actual;
 935}
 936
 937sub error_non_fast_forward {
 938        my $advice = run_git('config --bool advice.pushNonFastForward');
 939        chomp($advice);
 940        if ($advice ne 'false') {
 941                # Native git-push would show this after the summary.
 942                # We can't ask it to display it cleanly, so print it
 943                # ourselves before.
 944                print {*STDERR} "To prevent you from losing history, non-fast-forward updates were rejected\n";
 945                print {*STDERR} "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n";
 946                print {*STDERR} "'Note about fast-forwards' section of 'git push --help' for details.\n";
 947        }
 948        print {*STDOUT} qq(error $_[0] "non-fast-forward"\n);
 949        return 0;
 950}
 951
 952sub mw_upload_file {
 953        my $complete_file_name = shift;
 954        my $new_sha1 = shift;
 955        my $extension = shift;
 956        my $file_deleted = shift;
 957        my $summary = shift;
 958        my $newrevid;
 959        my $path = "File:${complete_file_name}";
 960        my %hashFiles = get_allowed_file_extensions();
 961        if (!exists($hashFiles{$extension})) {
 962                print {*STDERR} "${complete_file_name} is not a permitted file on this wiki.\n";
 963                print {*STDERR} "Check the configuration of file uploads in your mediawiki.\n";
 964                return $newrevid;
 965        }
 966        # Deleting and uploading a file requires a priviledged user
 967        if ($file_deleted) {
 968                $mediawiki = connect_maybe($mediawiki, $remotename, $url);
 969                my $query = {
 970                        action => 'delete',
 971                        title => $path,
 972                        reason => $summary
 973                };
 974                if (!$mediawiki->edit($query)) {
 975                        print {*STDERR} "Failed to delete file on remote wiki\n";
 976                        print {*STDERR} "Check your permissions on the remote site. Error code:\n";
 977                        print {*STDERR} $mediawiki->{error}->{code} . ':' . $mediawiki->{error}->{details};
 978                        exit 1;
 979                }
 980        } else {
 981                # Don't let perl try to interpret file content as UTF-8 => use "raw"
 982                my $content = run_git("cat-file blob ${new_sha1}", 'raw');
 983                if ($content ne EMPTY) {
 984                        $mediawiki = connect_maybe($mediawiki, $remotename, $url);
 985                        $mediawiki->{config}->{upload_url} =
 986                                "${url}/index.php/Special:Upload";
 987                        $mediawiki->edit({
 988                                action => 'upload',
 989                                filename => $complete_file_name,
 990                                comment => $summary,
 991                                file => [undef,
 992                                         $complete_file_name,
 993                                         Content => $content],
 994                                ignorewarnings => 1,
 995                        }, {
 996                                skip_encoding => 1
 997                        } ) || die $mediawiki->{error}->{code} . ':'
 998                                 . $mediawiki->{error}->{details} . "\n";
 999                        my $last_file_page = $mediawiki->get_page({title => $path});
1000                        $newrevid = $last_file_page->{revid};
1001                        print {*STDERR} "Pushed file: ${new_sha1} - ${complete_file_name}.\n";
1002                } else {
1003                        print {*STDERR} "Empty file ${complete_file_name} not pushed.\n";
1004                }
1005        }
1006        return $newrevid;
1007}
1008
1009sub mw_push_file {
1010        my $diff_info = shift;
1011        # $diff_info contains a string in this format:
1012        # 100644 100644 <sha1_of_blob_before_commit> <sha1_of_blob_now> <status>
1013        my @diff_info_split = split(/[ \t]/, $diff_info);
1014
1015        # Filename, including .mw extension
1016        my $complete_file_name = shift;
1017        # Commit message
1018        my $summary = shift;
1019        # MediaWiki revision number. Keep the previous one by default,
1020        # in case there's no edit to perform.
1021        my $oldrevid = shift;
1022        my $newrevid;
1023
1024        if ($summary eq EMPTY_MESSAGE) {
1025                $summary = EMPTY;
1026        }
1027
1028        my $new_sha1 = $diff_info_split[3];
1029        my $old_sha1 = $diff_info_split[2];
1030        my $page_created = ($old_sha1 eq NULL_SHA1);
1031        my $page_deleted = ($new_sha1 eq NULL_SHA1);
1032        $complete_file_name = clean_filename($complete_file_name);
1033
1034        my ($title, $extension) = $complete_file_name =~ /^(.*)\.([^\.]*)$/;
1035        if (!defined($extension)) {
1036                $extension = EMPTY;
1037        }
1038        if ($extension eq 'mw') {
1039                my $ns = get_mw_namespace_id_for_page($complete_file_name);
1040                if ($ns && $ns == get_mw_namespace_id('File') && (!$export_media)) {
1041                        print {*STDERR} "Ignoring media file related page: ${complete_file_name}\n";
1042                        return ($oldrevid, 'ok');
1043                }
1044                my $file_content;
1045                if ($page_deleted) {
1046                        # Deleting a page usually requires
1047                        # special privileges. A common
1048                        # convention is to replace the page
1049                        # with this content instead:
1050                        $file_content = DELETED_CONTENT;
1051                } else {
1052                        $file_content = run_git("cat-file blob ${new_sha1}");
1053                }
1054
1055                $mediawiki = connect_maybe($mediawiki, $remotename, $url);
1056
1057                my $result = $mediawiki->edit( {
1058                        action => 'edit',
1059                        summary => $summary,
1060                        title => $title,
1061                        basetimestamp => $basetimestamps{$oldrevid},
1062                        text => mediawiki_clean($file_content, $page_created),
1063                                  }, {
1064                                          skip_encoding => 1 # Helps with names with accentuated characters
1065                                  });
1066                if (!$result) {
1067                        if ($mediawiki->{error}->{code} == 3) {
1068                                # edit conflicts, considered as non-fast-forward
1069                                print {*STDERR} 'Warning: Error ' .
1070                                    $mediawiki->{error}->{code} .
1071                                    ' from mediawiki: ' . $mediawiki->{error}->{details} .
1072                                    ".\n";
1073                                return ($oldrevid, 'non-fast-forward');
1074                        } else {
1075                                # Other errors. Shouldn't happen => just die()
1076                                die 'Fatal: Error ' .
1077                                    $mediawiki->{error}->{code} .
1078                                    ' from mediawiki: ' . $mediawiki->{error}->{details} . "\n";
1079                        }
1080                }
1081                $newrevid = $result->{edit}->{newrevid};
1082                print {*STDERR} "Pushed file: ${new_sha1} - ${title}\n";
1083        } elsif ($export_media) {
1084                $newrevid = mw_upload_file($complete_file_name, $new_sha1,
1085                                           $extension, $page_deleted,
1086                                           $summary);
1087        } else {
1088                print {*STDERR} "Ignoring media file ${title}\n";
1089        }
1090        $newrevid = ($newrevid or $oldrevid);
1091        return ($newrevid, 'ok');
1092}
1093
1094sub mw_push {
1095        # multiple push statements can follow each other
1096        my @refsspecs = (shift, get_more_refs('push'));
1097        my $pushed;
1098        for my $refspec (@refsspecs) {
1099                my ($force, $local, $remote) = $refspec =~ /^(\+)?([^:]*):([^:]*)$/
1100                    or die("Invalid refspec for push. Expected <src>:<dst> or +<src>:<dst>\n");
1101                if ($force) {
1102                        print {*STDERR} "Warning: forced push not allowed on a MediaWiki.\n";
1103                }
1104                if ($local eq EMPTY) {
1105                        print {*STDERR} "Cannot delete remote branch on a MediaWiki\n";
1106                        print {*STDOUT} "error ${remote} cannot delete\n";
1107                        next;
1108                }
1109                if ($remote ne 'refs/heads/master') {
1110                        print {*STDERR} "Only push to the branch 'master' is supported on a MediaWiki\n";
1111                        print {*STDOUT} "error ${remote} only master allowed\n";
1112                        next;
1113                }
1114                if (mw_push_revision($local, $remote)) {
1115                        $pushed = 1;
1116                }
1117        }
1118
1119        # Notify Git that the push is done
1120        print {*STDOUT} "\n";
1121
1122        if ($pushed && $dumb_push) {
1123                print {*STDERR} "Just pushed some revisions to MediaWiki.\n";
1124                print {*STDERR} "The pushed revisions now have to be re-imported, and your current branch\n";
1125                print {*STDERR} "needs to be updated with these re-imported commits. You can do this with\n";
1126                print {*STDERR} "\n";
1127                print {*STDERR} "  git pull --rebase\n";
1128                print {*STDERR} "\n";
1129        }
1130        return;
1131}
1132
1133sub mw_push_revision {
1134        my $local = shift;
1135        my $remote = shift; # actually, this has to be "refs/heads/master" at this point.
1136        my $last_local_revid = get_last_local_revision();
1137        print {*STDERR} ".\n"; # Finish sentence started by get_last_local_revision()
1138        my $last_remote_revid = get_last_remote_revision();
1139        my $mw_revision = $last_remote_revid;
1140
1141        # Get sha1 of commit pointed by local HEAD
1142        my $HEAD_sha1 = run_git("rev-parse ${local} 2>/dev/null");
1143        chomp($HEAD_sha1);
1144        # Get sha1 of commit pointed by remotes/$remotename/master
1145        my $remoteorigin_sha1 = run_git("rev-parse refs/remotes/${remotename}/master 2>/dev/null");
1146        chomp($remoteorigin_sha1);
1147
1148        if ($last_local_revid > 0 &&
1149            $last_local_revid < $last_remote_revid) {
1150                return error_non_fast_forward($remote);
1151        }
1152
1153        if ($HEAD_sha1 eq $remoteorigin_sha1) {
1154                # nothing to push
1155                return 0;
1156        }
1157
1158        # Get every commit in between HEAD and refs/remotes/origin/master,
1159        # including HEAD and refs/remotes/origin/master
1160        my @commit_pairs = ();
1161        if ($last_local_revid > 0) {
1162                my $parsed_sha1 = $remoteorigin_sha1;
1163                # Find a path from last MediaWiki commit to pushed commit
1164                print {*STDERR} "Computing path from local to remote ...\n";
1165                my @local_ancestry = split(/\n/, run_git("rev-list --boundary --parents ${local} ^${parsed_sha1}"));
1166                my %local_ancestry;
1167                foreach my $line (@local_ancestry) {
1168                        if (my ($child, $parents) = $line =~ /^-?([a-f0-9]+) ([a-f0-9 ]+)/) {
1169                                foreach my $parent (split(/ /, $parents)) {
1170                                        $local_ancestry{$parent} = $child;
1171                                }
1172                        } elsif (!$line =~ /^([a-f0-9]+)/) {
1173                                die "Unexpected output from git rev-list: ${line}\n";
1174                        }
1175                }
1176                while ($parsed_sha1 ne $HEAD_sha1) {
1177                        my $child = $local_ancestry{$parsed_sha1};
1178                        if (!$child) {
1179                                print {*STDERR} "Cannot find a path in history from remote commit to last commit\n";
1180                                return error_non_fast_forward($remote);
1181                        }
1182                        push(@commit_pairs, [$parsed_sha1, $child]);
1183                        $parsed_sha1 = $child;
1184                }
1185        } else {
1186                # No remote mediawiki revision. Export the whole
1187                # history (linearized with --first-parent)
1188                print {*STDERR} "Warning: no common ancestor, pushing complete history\n";
1189                my $history = run_git("rev-list --first-parent --children ${local}");
1190                my @history = split(/\n/, $history);
1191                @history = @history[1..$#history];
1192                foreach my $line (reverse @history) {
1193                        my @commit_info_split = split(/[ \n]/, $line);
1194                        push(@commit_pairs, \@commit_info_split);
1195                }
1196        }
1197
1198        foreach my $commit_info_split (@commit_pairs) {
1199                my $sha1_child = @{$commit_info_split}[0];
1200                my $sha1_commit = @{$commit_info_split}[1];
1201                my $diff_infos = run_git("diff-tree -r --raw -z ${sha1_child} ${sha1_commit}");
1202                # TODO: we could detect rename, and encode them with a #redirect on the wiki.
1203                # TODO: for now, it's just a delete+add
1204                my @diff_info_list = split(/\0/, $diff_infos);
1205                # Keep the subject line of the commit message as mediawiki comment for the revision
1206                my $commit_msg = run_git(qq(log --no-walk --format="%s" ${sha1_commit}));
1207                chomp($commit_msg);
1208                # Push every blob
1209                while (@diff_info_list) {
1210                        my $status;
1211                        # git diff-tree -z gives an output like
1212                        # <metadata>\0<filename1>\0
1213                        # <metadata>\0<filename2>\0
1214                        # and we've split on \0.
1215                        my $info = shift(@diff_info_list);
1216                        my $file = shift(@diff_info_list);
1217                        ($mw_revision, $status) = mw_push_file($info, $file, $commit_msg, $mw_revision);
1218                        if ($status eq 'non-fast-forward') {
1219                                # we may already have sent part of the
1220                                # commit to MediaWiki, but it's too
1221                                # late to cancel it. Stop the push in
1222                                # the middle, but still give an
1223                                # accurate error message.
1224                                return error_non_fast_forward($remote);
1225                        }
1226                        if ($status ne 'ok') {
1227                                die("Unknown error from mw_push_file()\n");
1228                        }
1229                }
1230                if (!$dumb_push) {
1231                        run_git(qq(notes --ref=${remotename}/mediawiki add -f -m "mediawiki_revision: ${mw_revision}" ${sha1_commit}));
1232                }
1233        }
1234
1235        print {*STDOUT} "ok ${remote}\n";
1236        return 1;
1237}
1238
1239sub get_allowed_file_extensions {
1240        $mediawiki = connect_maybe($mediawiki, $remotename, $url);
1241
1242        my $query = {
1243                action => 'query',
1244                meta => 'siteinfo',
1245                siprop => 'fileextensions'
1246                };
1247        my $result = $mediawiki->api($query);
1248        my @file_extensions = map { $_->{ext}} @{$result->{query}->{fileextensions}};
1249        my %hashFile = map { $_ => 1 } @file_extensions;
1250
1251        return %hashFile;
1252}
1253
1254# In memory cache for MediaWiki namespace ids.
1255my %namespace_id;
1256
1257# Namespaces whose id is cached in the configuration file
1258# (to avoid duplicates)
1259my %cached_mw_namespace_id;
1260
1261# Return MediaWiki id for a canonical namespace name.
1262# Ex.: "File", "Project".
1263sub get_mw_namespace_id {
1264        $mediawiki = connect_maybe($mediawiki, $remotename, $url);
1265        my $name = shift;
1266
1267        if (!exists $namespace_id{$name}) {
1268                # Look at configuration file, if the record for that namespace is
1269                # already cached. Namespaces are stored in form:
1270                # "Name_of_namespace:Id_namespace", ex.: "File:6".
1271                my @temp = split(/\n/,
1272                                 run_git("config --get-all remote.${remotename}.namespaceCache"));
1273                chomp(@temp);
1274                foreach my $ns (@temp) {
1275                        my ($n, $id) = split(/:/, $ns);
1276                        if ($id eq 'notANameSpace') {
1277                                $namespace_id{$n} = {is_namespace => 0};
1278                        } else {
1279                                $namespace_id{$n} = {is_namespace => 1, id => $id};
1280                        }
1281                        $cached_mw_namespace_id{$n} = 1;
1282                }
1283        }
1284
1285        if (!exists $namespace_id{$name}) {
1286                print {*STDERR} "Namespace ${name} not found in cache, querying the wiki ...\n";
1287                # NS not found => get namespace id from MW and store it in
1288                # configuration file.
1289                my $query = {
1290                        action => 'query',
1291                        meta => 'siteinfo',
1292                        siprop => 'namespaces'
1293                };
1294                my $result = $mediawiki->api($query);
1295
1296                while (my ($id, $ns) = each(%{$result->{query}->{namespaces}})) {
1297                        if (defined($ns->{id}) && defined($ns->{canonical})) {
1298                                $namespace_id{$ns->{canonical}} = {is_namespace => 1, id => $ns->{id}};
1299                                if ($ns->{'*'}) {
1300                                        # alias (e.g. french Fichier: as alias for canonical File:)
1301                                        $namespace_id{$ns->{'*'}} = {is_namespace => 1, id => $ns->{id}};
1302                                }
1303                        }
1304                }
1305        }
1306
1307        my $ns = $namespace_id{$name};
1308        my $id;
1309
1310        if (!defined $ns) {
1311                print {*STDERR} "No such namespace ${name} on MediaWiki.\n";
1312                $ns = {is_namespace => 0};
1313                $namespace_id{$name} = $ns;
1314        }
1315
1316        if ($ns->{is_namespace}) {
1317                $id = $ns->{id};
1318        }
1319
1320        # Store "notANameSpace" as special value for inexisting namespaces
1321        my $store_id = ($id || 'notANameSpace');
1322
1323        # Store explicitly requested namespaces on disk
1324        if (!exists $cached_mw_namespace_id{$name}) {
1325                run_git(qq(config --add remote.${remotename}.namespaceCache "${name}:${store_id}"));
1326                $cached_mw_namespace_id{$name} = 1;
1327        }
1328        return $id;
1329}
1330
1331sub get_mw_namespace_id_for_page {
1332        my $namespace = shift;
1333        if ($namespace =~ /^([^:]*):/) {
1334                return get_mw_namespace_id($namespace);
1335        } else {
1336                return;
1337        }
1338}