git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matthieu Moy <Matthieu.Moy@imag.fr>
To: git@vger.kernel.org, gitster@pobox.com
Cc: Pavel.Volek@ensimag.imag.fr, Kim-Thuat.Nguyen@ensimag.imag.fr,
	roucherj@ensimag.imag.fr, Matthieu Moy <Matthieu.Moy@imag.fr>
Subject: [PATCH 3/5] git-remote-mediawiki: send "File:" attachments to a remote wiki
Date: Tue, 26 Jun 2012 18:04:31 +0200	[thread overview]
Message-ID: <1340726673-25496-4-git-send-email-Matthieu.Moy@imag.fr> (raw)
In-Reply-To: <1340726673-25496-1-git-send-email-Matthieu.Moy@imag.fr>

From: NGUYEN Kim Thuat <Kim-Thuat.Nguyen@ensimag.imag.fr>

The current version of the git-remote-mediawiki supports only import and
export of plain wiki pages. This patch adds the functionality to export
file attachments (i.e. the content of the File: MediaWiki namespace),
which are also exposed by MediaWiki API.

This requires a recent version of MediaWiki::API (Version 0.37 works.
Version 0.34 doesn't).

Signed-off-by: Pavel Volek <Pavel.Volek@ensimag.imag.fr>
Signed-off-by: NGUYEN Kim Thuat <Kim-Thuat.Nguyen@ensimag.imag.fr>
Signed-off-by: ROUCHER IGLESIAS Javier <roucherj@ensimag.imag.fr>
Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 contrib/mw-to-git/git-remote-mediawiki | 98 +++++++++++++++++++++++++++++++---
 1 file changed, 90 insertions(+), 8 deletions(-)

diff --git a/contrib/mw-to-git/git-remote-mediawiki b/contrib/mw-to-git/git-remote-mediawiki
index ed06ff7..253b449 100755
--- a/contrib/mw-to-git/git-remote-mediawiki
+++ b/contrib/mw-to-git/git-remote-mediawiki
@@ -348,8 +348,12 @@ sub get_mw_pages {
 	return values(%pages);
 }
 
+# usage: $out = run_git("command args");
+#        $out = run_git("command args", "raw"); # don't interpret output as UTF-8.
 sub run_git {
-	open(my $git, "-|:encoding(UTF-8)", "git " . $_[0]);
+	my $args = shift;
+	my $encoding = (shift || "encoding(UTF-8)");
+	open(my $git, "-|:$encoding", "git " . $args);
 	my $res = do { local $/; <$git> };
 	close($git);
 
@@ -705,6 +709,63 @@ sub error_non_fast_forward {
 	return 0;
 }
 
+sub mw_upload_file {
+	my $complete_file_name = shift;
+	my $new_sha1 = shift;
+	my $extension = shift;
+	my $file_deleted = shift;
+	my $summary = shift;
+	my $newrevid;
+	my $path = "File:" . $complete_file_name;
+	my %hashFiles = get_allowed_file_extensions();
+	if (!exists($hashFiles{$extension})) {
+		print STDERR "$complete_file_name is not a permitted file on this wiki.\n";
+		print STDERR "Check the configuration of file uploads in your mediawiki.\n";
+		return $newrevid;
+	}
+	# Deleting and uploading a file requires a priviledged user
+	if ($file_deleted) {
+		mw_connect_maybe();
+		my $query = {
+			action => 'delete',
+			title => $path,
+			reason => $summary
+		};
+		if (!$mediawiki->edit($query)) {
+			print STDERR "Failed to delete file on remote wiki\n";
+			print STDERR "Check your permissions on the remote site. Error code:\n";
+			print STDERR $mediawiki->{error}->{code} . ':' . $mediawiki->{error}->{details};
+			exit 1;
+		}
+	} else {
+		# Don't let perl try to interpret file content as UTF-8 => use "raw"
+		my $content = run_git("cat-file blob $new_sha1", "raw");
+		if ($content ne "") {
+			mw_connect_maybe();
+			$mediawiki->{config}->{upload_url} =
+				"$url/index.php/Special:Upload";
+			$mediawiki->edit({
+				action => 'upload',
+				filename => $complete_file_name,
+				comment => $summary,
+				file => [undef,
+					 $complete_file_name,
+					 Content => $content],
+				ignorewarnings => 1,
+			}, {
+				skip_encoding => 1
+			} ) || die $mediawiki->{error}->{code} . ':'
+				 . $mediawiki->{error}->{details};
+			my $last_file_page = $mediawiki->get_page({title => $path});
+			$newrevid = $last_file_page->{revid};
+			print STDERR "Pushed file: $new_sha1 - $complete_file_name.\n";
+		} else {
+			print STDERR "Empty file $complete_file_name not pushed.\n";
+		}
+	}
+	return $newrevid;
+}
+
 sub mw_push_file {
 	my $diff_info = shift;
 	# $diff_info contains a string in this format:
@@ -717,7 +778,8 @@ sub mw_push_file {
 	my $summary = shift;
 	# MediaWiki revision number. Keep the previous one by default,
 	# in case there's no edit to perform.
-	my $newrevid = shift;
+	my $oldrevid = shift;
+	my $newrevid;
 
 	my $new_sha1 = $diff_info_split[3];
 	my $old_sha1 = $diff_info_split[2];
@@ -725,9 +787,11 @@ sub mw_push_file {
 	my $page_deleted = ($new_sha1 eq NULL_SHA1);
 	$complete_file_name = mediawiki_clean_filename($complete_file_name);
 
-	if (substr($complete_file_name,-3) eq ".mw") {
-		my $title = substr($complete_file_name,0,-3);
-
+	my ($title, $extension) = $complete_file_name =~ /^(.*)\.([^\.]*)$/;
+	if (!defined($extension)) {
+		$extension = "";
+	}
+	if ($extension eq "mw") {
 		my $file_content;
 		if ($page_deleted) {
 			# Deleting a page usually requires
@@ -745,7 +809,7 @@ sub mw_push_file {
 			action => 'edit',
 			summary => $summary,
 			title => $title,
-			basetimestamp => $basetimestamps{$newrevid},
+			basetimestamp => $basetimestamps{$oldrevid},
 			text => mediawiki_clean($file_content, $page_created),
 				  }, {
 					  skip_encoding => 1 # Helps with names with accentuated characters
@@ -757,7 +821,7 @@ sub mw_push_file {
 				    $mediawiki->{error}->{code} .
 				    ' from mediwiki: ' . $mediawiki->{error}->{details} .
 				    ".\n";
-				return ($newrevid, "non-fast-forward");
+				return ($oldrevid, "non-fast-forward");
 			} else {
 				# Other errors. Shouldn't happen => just die()
 				die 'Fatal: Error ' .
@@ -768,8 +832,11 @@ sub mw_push_file {
 		$newrevid = $result->{edit}->{newrevid};
 		print STDERR "Pushed file: $new_sha1 - $title\n";
 	} else {
-		print STDERR "$complete_file_name not a mediawiki file (Not pushable on this version of git-remote-mediawiki).\n"
+		$newrevid = mw_upload_file($complete_file_name, $new_sha1,
+					   $extension, $page_deleted,
+					   $summary);
 	}
+	$newrevid = ($newrevid or $oldrevid);
 	return ($newrevid, "ok");
 }
 
@@ -906,3 +973,18 @@ sub mw_push_revision {
 	print STDOUT "ok $remote\n";
 	return 1;
 }
+
+sub get_allowed_file_extensions {
+	mw_connect_maybe();
+
+	my $query = {
+		action => 'query',
+		meta => 'siteinfo',
+		siprop => 'fileextensions'
+		};
+	my $result = $mediawiki->api($query);
+	my @file_extensions= map $_->{ext},@{$result->{query}->{fileextensions}};
+	my %hashFile = map {$_ => 1}@file_extensions;
+
+	return %hashFile;
+}
-- 
1.7.11.5.g0c7e058.dirty

  parent reply	other threads:[~2012-06-26 16:07 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-26 16:04 [PATCH 0/5] git-remote-mediawiki: support File: import and export Matthieu Moy
2012-06-26 16:04 ` [PATCH 1/5] git-remote-mediawiki: don't compute the diff when getting commit message Matthieu Moy
2012-06-26 17:47   ` Junio C Hamano
2012-06-27  8:32     ` Matthieu Moy
2012-06-26 16:04 ` [PATCH 2/5] git-remote-mediawiki: don't "use encoding 'utf8';" Matthieu Moy
2012-06-26 16:04 ` Matthieu Moy [this message]
2012-06-26 16:04 ` [PATCH 4/5] git-remote-mediawiki: split get_mw_pages into smaller functions Matthieu Moy
2012-06-26 16:04 ` [PATCH 5/5] git-remote-mediawiki: import "File:" attachments Matthieu Moy
2012-06-27  9:10 ` [PATCH 0/5 v2] git-remote-mediawiki: support File: import and export Matthieu Moy
2012-06-27  9:10   ` [PATCH 1/5] git-remote-mediawiki: don't compute the diff when getting commit message Matthieu Moy
2012-06-27  9:10   ` [PATCH 2/5] git-remote-mediawiki: don't "use encoding 'utf8';" Matthieu Moy
2012-06-27  9:10   ` [PATCH 3/5] git-remote-mediawiki: send "File:" attachments to a remote wiki Matthieu Moy
2012-06-27  9:10   ` [PATCH 4/5] git-remote-mediawiki: split get_mw_pages into smaller functions Matthieu Moy
2012-06-27  9:10   ` [PATCH 5/5] git-remote-mediawiki: import "File:" attachments Matthieu Moy
2012-06-27 14:21     ` [PATCH 5/5 v3] " Matthieu Moy
2012-07-04 12:53       ` [PATCH v4] " Matthieu Moy
2012-07-05  6:58         ` Junio C Hamano
2012-07-05  7:42           ` Matthieu Moy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1340726673-25496-4-git-send-email-Matthieu.Moy@imag.fr \
    --to=matthieu.moy@imag.fr \
    --cc=Kim-Thuat.Nguyen@ensimag.imag.fr \
    --cc=Pavel.Volek@ensimag.imag.fr \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=roucherj@ensimag.imag.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).