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