Skip to content

Commit

Permalink
Merge branch 'bp/mediawiki-preview'
Browse files Browse the repository at this point in the history
Add a command to allow previewing the contents locally before
pushing it out, when working with a MediaWiki remote.

I personally do not think this belongs to Git.  If you are working
on a set of AsciiDoc source files, you sure do want to locally
format to preview what you will be pushing out, and if you are
working on a set of C or Java source files, you do want to test it
before pushing it out, too.  That kind of thing belongs to your
build script, not to your SCM.

But I'll let it pass, as this is only a contrib/ thing.

* bp/mediawiki-preview:
  git-remote-mediawiki: add preview subcommand into git mw
  git-remote-mediawiki: add git-mw command
  git-remote-mediawiki: factoring code between git-remote-mediawiki and Git::Mediawiki
  git-remote-mediawiki: update tests to run with the new bin-wrapper
  git-remote-mediawiki: add a git bin-wrapper for developement
  wrap-for-bin: make bin-wrappers chainable
  git-remote-mediawiki: introduction of Git::Mediawiki.pm
  • Loading branch information
Junio C Hamano committed Jul 18, 2013
2 parents 73f4c9a + 0078a7f commit b12aecd
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 83 deletions.
100 changes: 100 additions & 0 deletions contrib/mw-to-git/Git/Mediawiki.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package Git::Mediawiki;

use 5.008;
use strict;
use Git;

BEGIN {

our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);

# Totally unstable API.
$VERSION = '0.01';

require Exporter;

@ISA = qw(Exporter);

@EXPORT = ();

# Methods which can be called as standalone functions as well:
@EXPORT_OK = qw(clean_filename smudge_filename connect_maybe
EMPTY HTTP_CODE_OK HTTP_CODE_PAGE_NOT_FOUND);
}

# Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced
use constant SLASH_REPLACEMENT => '%2F';

# Used to test for empty strings
use constant EMPTY => q{};

# HTTP codes
use constant HTTP_CODE_OK => 200;
use constant HTTP_CODE_PAGE_NOT_FOUND => 404;

sub clean_filename {
my $filename = shift;
$filename =~ s{@{[SLASH_REPLACEMENT]}}{/}g;
# [, ], |, {, and } are forbidden by MediaWiki, even URL-encoded.
# Do a variant of URL-encoding, i.e. looks like URL-encoding,
# but with _ added to prevent MediaWiki from thinking this is
# an actual special character.
$filename =~ s/[\[\]\{\}\|]/sprintf("_%%_%x", ord($&))/ge;
# If we use the uri escape before
# we should unescape here, before anything

return $filename;
}

sub smudge_filename {
my $filename = shift;
$filename =~ s{/}{@{[SLASH_REPLACEMENT]}}g;
$filename =~ s/ /_/g;
# Decode forbidden characters encoded in clean_filename
$filename =~ s/_%_([0-9a-fA-F][0-9a-fA-F])/sprintf('%c', hex($1))/ge;
return $filename;
}

sub connect_maybe {
my $wiki = shift;
if ($wiki) {
return $wiki;
}

my $remote_name = shift;
my $remote_url = shift;
my ($wiki_login, $wiki_password, $wiki_domain);

$wiki_login = Git::config("remote.${remote_name}.mwLogin");
$wiki_password = Git::config("remote.${remote_name}.mwPassword");
$wiki_domain = Git::config("remote.${remote_name}.mwDomain");

$wiki = MediaWiki::API->new;
$wiki->{config}->{api_url} = "${remote_url}/api.php";
if ($wiki_login) {
my %credential = (
'url' => $remote_url,
'username' => $wiki_login,
'password' => $wiki_password
);
Git::credential(\%credential);
my $request = {lgname => $credential{username},
lgpassword => $credential{password},
lgdomain => $wiki_domain};
if ($wiki->login($request)) {
Git::credential(\%credential, 'approve');
print {*STDERR} qq(Logged in mediawiki user "$credential{username}".\n);
} else {
print {*STDERR} qq(Failed to log in mediawiki user "$credential{username}" on ${remote_url}\n);
print {*STDERR} ' (error ' .
$wiki->{error}->{code} . ': ' .
$wiki->{error}->{details} . ")\n";
Git::credential(\%credential, 'reject');
exit 1;
}
}

return $wiki;
}

1; # Famous last words
33 changes: 29 additions & 4 deletions contrib/mw-to-git/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,43 @@
# Copyright (C) 2013
# Matthieu Moy <Matthieu.Moy@imag.fr>
#
## Build git-remote-mediawiki
# To build and test:
#
# make
# bin-wrapper/git mw preview Some_page.mw
# bin-wrapper/git clone mediawiki::http://example.com/wiki/
#
# To install, run Git's toplevel 'make install' then run:
#
# make install

GIT_MEDIAWIKI_PM=Git/Mediawiki.pm
SCRIPT_PERL=git-remote-mediawiki.perl
SCRIPT_PERL+=git-mw.perl
GIT_ROOT_DIR=../..
HERE=contrib/mw-to-git/

SCRIPT_PERL_FULL=$(patsubst %,$(HERE)/%,$(SCRIPT_PERL))
INSTLIBDIR=$(shell $(MAKE) -C $(GIT_ROOT_DIR)/perl \
-s --no-print-directory instlibdir)

all: build

build install clean:
$(MAKE) -C $(GIT_ROOT_DIR) SCRIPT_PERL=$(SCRIPT_PERL_FULL) \
$@-perl-script
install_pm:
install $(GIT_MEDIAWIKI_PM) $(INSTLIBDIR)/$(GIT_MEDIAWIKI_PM)

build:
$(MAKE) -C $(GIT_ROOT_DIR) SCRIPT_PERL="$(SCRIPT_PERL_FULL)" \
build-perl-script

install: install_pm
$(MAKE) -C $(GIT_ROOT_DIR) SCRIPT_PERL="$(SCRIPT_PERL_FULL)" \
install-perl-script

clean:
$(MAKE) -C $(GIT_ROOT_DIR) SCRIPT_PERL="$(SCRIPT_PERL_FULL)" \
clean-perl-script
rm $(INSTLIBDIR)/$(GIT_MEDIAWIKI_PM)

perlcritic:
perlcritic -2 *.perl
14 changes: 14 additions & 0 deletions contrib/mw-to-git/bin-wrapper/git
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

# git executable wrapper script for Git-Mediawiki to run tests without
# installing all the scripts and perl packages.

GIT_ROOT_DIR=../../..
GIT_EXEC_PATH=$(cd "$(dirname "$0")" && cd ${GIT_ROOT_DIR} && pwd)

GITPERLLIB="$GIT_EXEC_PATH"'/contrib/mw-to-git'"${GITPERLLIB:+:$GITPERLLIB}"
PATH="$GIT_EXEC_PATH"'/contrib/mw-to-git:'"$PATH"

export GITPERLLIB PATH

exec "${GIT_EXEC_PATH}/bin-wrappers/git" "$@"
Loading

0 comments on commit b12aecd

Please sign in to comment.