Skip to content

Commit b12aecd

Browse files
committed
Merge branch 'bp/mediawiki-preview'
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
2 parents 73f4c9a + 0078a7f commit b12aecd

File tree

8 files changed

+528
-83
lines changed

8 files changed

+528
-83
lines changed

contrib/mw-to-git/Git/Mediawiki.pm

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package Git::Mediawiki;
2+
3+
use 5.008;
4+
use strict;
5+
use Git;
6+
7+
BEGIN {
8+
9+
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
10+
11+
# Totally unstable API.
12+
$VERSION = '0.01';
13+
14+
require Exporter;
15+
16+
@ISA = qw(Exporter);
17+
18+
@EXPORT = ();
19+
20+
# Methods which can be called as standalone functions as well:
21+
@EXPORT_OK = qw(clean_filename smudge_filename connect_maybe
22+
EMPTY HTTP_CODE_OK HTTP_CODE_PAGE_NOT_FOUND);
23+
}
24+
25+
# Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced
26+
use constant SLASH_REPLACEMENT => '%2F';
27+
28+
# Used to test for empty strings
29+
use constant EMPTY => q{};
30+
31+
# HTTP codes
32+
use constant HTTP_CODE_OK => 200;
33+
use constant HTTP_CODE_PAGE_NOT_FOUND => 404;
34+
35+
sub clean_filename {
36+
my $filename = shift;
37+
$filename =~ s{@{[SLASH_REPLACEMENT]}}{/}g;
38+
# [, ], |, {, and } are forbidden by MediaWiki, even URL-encoded.
39+
# Do a variant of URL-encoding, i.e. looks like URL-encoding,
40+
# but with _ added to prevent MediaWiki from thinking this is
41+
# an actual special character.
42+
$filename =~ s/[\[\]\{\}\|]/sprintf("_%%_%x", ord($&))/ge;
43+
# If we use the uri escape before
44+
# we should unescape here, before anything
45+
46+
return $filename;
47+
}
48+
49+
sub smudge_filename {
50+
my $filename = shift;
51+
$filename =~ s{/}{@{[SLASH_REPLACEMENT]}}g;
52+
$filename =~ s/ /_/g;
53+
# Decode forbidden characters encoded in clean_filename
54+
$filename =~ s/_%_([0-9a-fA-F][0-9a-fA-F])/sprintf('%c', hex($1))/ge;
55+
return $filename;
56+
}
57+
58+
sub connect_maybe {
59+
my $wiki = shift;
60+
if ($wiki) {
61+
return $wiki;
62+
}
63+
64+
my $remote_name = shift;
65+
my $remote_url = shift;
66+
my ($wiki_login, $wiki_password, $wiki_domain);
67+
68+
$wiki_login = Git::config("remote.${remote_name}.mwLogin");
69+
$wiki_password = Git::config("remote.${remote_name}.mwPassword");
70+
$wiki_domain = Git::config("remote.${remote_name}.mwDomain");
71+
72+
$wiki = MediaWiki::API->new;
73+
$wiki->{config}->{api_url} = "${remote_url}/api.php";
74+
if ($wiki_login) {
75+
my %credential = (
76+
'url' => $remote_url,
77+
'username' => $wiki_login,
78+
'password' => $wiki_password
79+
);
80+
Git::credential(\%credential);
81+
my $request = {lgname => $credential{username},
82+
lgpassword => $credential{password},
83+
lgdomain => $wiki_domain};
84+
if ($wiki->login($request)) {
85+
Git::credential(\%credential, 'approve');
86+
print {*STDERR} qq(Logged in mediawiki user "$credential{username}".\n);
87+
} else {
88+
print {*STDERR} qq(Failed to log in mediawiki user "$credential{username}" on ${remote_url}\n);
89+
print {*STDERR} ' (error ' .
90+
$wiki->{error}->{code} . ': ' .
91+
$wiki->{error}->{details} . ")\n";
92+
Git::credential(\%credential, 'reject');
93+
exit 1;
94+
}
95+
}
96+
97+
return $wiki;
98+
}
99+
100+
1; # Famous last words

contrib/mw-to-git/Makefile

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,43 @@
22
# Copyright (C) 2013
33
# Matthieu Moy <[email protected]>
44
#
5-
## Build git-remote-mediawiki
5+
# To build and test:
6+
#
7+
# make
8+
# bin-wrapper/git mw preview Some_page.mw
9+
# bin-wrapper/git clone mediawiki::http://example.com/wiki/
10+
#
11+
# To install, run Git's toplevel 'make install' then run:
12+
#
13+
# make install
614

15+
GIT_MEDIAWIKI_PM=Git/Mediawiki.pm
716
SCRIPT_PERL=git-remote-mediawiki.perl
17+
SCRIPT_PERL+=git-mw.perl
818
GIT_ROOT_DIR=../..
919
HERE=contrib/mw-to-git/
1020

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

1325
all: build
1426

15-
build install clean:
16-
$(MAKE) -C $(GIT_ROOT_DIR) SCRIPT_PERL=$(SCRIPT_PERL_FULL) \
17-
$@-perl-script
27+
install_pm:
28+
install $(GIT_MEDIAWIKI_PM) $(INSTLIBDIR)/$(GIT_MEDIAWIKI_PM)
29+
30+
build:
31+
$(MAKE) -C $(GIT_ROOT_DIR) SCRIPT_PERL="$(SCRIPT_PERL_FULL)" \
32+
build-perl-script
33+
34+
install: install_pm
35+
$(MAKE) -C $(GIT_ROOT_DIR) SCRIPT_PERL="$(SCRIPT_PERL_FULL)" \
36+
install-perl-script
37+
38+
clean:
39+
$(MAKE) -C $(GIT_ROOT_DIR) SCRIPT_PERL="$(SCRIPT_PERL_FULL)" \
40+
clean-perl-script
41+
rm $(INSTLIBDIR)/$(GIT_MEDIAWIKI_PM)
42+
1843
perlcritic:
1944
perlcritic -2 *.perl

contrib/mw-to-git/bin-wrapper/git

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
# git executable wrapper script for Git-Mediawiki to run tests without
4+
# installing all the scripts and perl packages.
5+
6+
GIT_ROOT_DIR=../../..
7+
GIT_EXEC_PATH=$(cd "$(dirname "$0")" && cd ${GIT_ROOT_DIR} && pwd)
8+
9+
GITPERLLIB="$GIT_EXEC_PATH"'/contrib/mw-to-git'"${GITPERLLIB:+:$GITPERLLIB}"
10+
PATH="$GIT_EXEC_PATH"'/contrib/mw-to-git:'"$PATH"
11+
12+
export GITPERLLIB PATH
13+
14+
exec "${GIT_EXEC_PATH}/bin-wrappers/git" "$@"

0 commit comments

Comments
 (0)