git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: kim-thuat.nguyen@ensimag.imag.fr
To: git@vger.kernel.org
Cc: nguyenkimthuat <Kim-Thuat.Nguyen@ensimag.imag.fr>,
	VOLEK Pavel <Pavel.Volek@ensimag.imag.fr>,
	ROUCHER IGLESIAS Javier <roucherj@ensimag.imag.fr>,
	Matthieu Moy <Matthieu.Moy@imag.fr>
Subject: [PATCHv3] git-remote-mediawiki: export File: attachments
Date: Wed, 13 Jun 2012 09:56:30 +0200	[thread overview]
Message-ID: <1339574190-2596-1-git-send-email-Kim-Thuat.Nguyen@ensimag.imag.fr> (raw)
In-Reply-To: <y>

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

This patch adds the functionality to export the 
file attachements from the local git's repository 
using the API of mediawiki.

Signed-off-by: VOLEK Pavel <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 | 67 +++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 2 deletions(-)

diff --git a/contrib/mw-to-git/git-remote-mediawiki b/contrib/mw-to-git/git-remote-mediawiki
index c18bfa1..e6f8940 100755
--- a/contrib/mw-to-git/git-remote-mediawiki
+++ b/contrib/mw-to-git/git-remote-mediawiki
@@ -275,6 +275,14 @@ sub run_git {
 	return $res;
 }
 
+sub run_git_raw {
+	no encoding 'utf8';
+	open(my $g, "-|", "git " . $_[0]);
+	my $r = do { local $/; <$g> };
+	close($g);
+
+	return $r;
+}
 
 sub get_last_local_revision {
 	# Get note regarding last mediawiki revision
@@ -644,7 +652,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 $path = "File:".$complete_file_name;
+	my @extensions = split(/\./, $complete_file_name);
+	my $extension = pop(@extensions);
+
+	if ($extension eq "mw") {
 		my $title = substr($complete_file_name,0,-3);
 
 		my $file_content;
@@ -687,7 +699,43 @@ 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"
+		my %hashFiles = get_file_extensions();
+		if (exists($hashFiles{$extension})) {
+			# Deleting and uploading a file require the priviledge of the user
+			if ($page_deleted) {
+				mw_connect_maybe();
+				my $res = $mediawiki->edit( {
+					action => 'delete',
+					title => $path,
+					reason => $summary
+					} )|| die $mediawiki-> {error}->{code} .
+						':' . $mediawiki->{error}->{details};
+			} else {
+				my $content = run_git_raw("cat-file blob $new_sha1");
+				if ($content ne "") {
+					mw_connect_maybe();
+					$mediawiki->{config}->{upload_url} = "$url/index.php/Special:Upload";
+
+					my $res = $mediawiki->edit ( {
+						action => 'upload',
+						filename => $complete_file_name,
+						comment => $summary,
+						file => [undef, $complete_file_name, Content => $content ],
+						ignorewarnings => 1,
+						}, {
+							skip_encoding => 1 # Helps with names with accentuated characters
+						} )  || 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. Can not upload.\n ";
+				}
+			}
+		} else {
+			print STDERR "$complete_file_name is not a permitted file. Check the configuration of file uploads in your mediawiki.\n";
+		}
 	}
 	return ($newrevid, "ok");
 }
@@ -825,3 +873,18 @@ sub mw_push_revision {
 	print STDOUT "ok $remote\n";
 	return 1;
 }
+
+sub get_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.rc2.10.g9fb1faf

             reply	other threads:[~2012-06-13  7:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-13  7:56 kim-thuat.nguyen [this message]
2012-06-13 16:36 ` [PATCHv3] git-remote-mediawiki: export File: attachments 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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=1339574190-2596-1-git-send-email-Kim-Thuat.Nguyen@ensimag.imag.fr \
    --to=kim-thuat.nguyen@ensimag.imag.fr \
    --cc=Matthieu.Moy@imag.fr \
    --cc=Pavel.Volek@ensimag.imag.fr \
    --cc=git@vger.kernel.org \
    --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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).