1 package Git::Mediawiki;
9 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
11 # Totally unstable API.
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);
25 # Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced
26 use constant SLASH_REPLACEMENT => '%2F';
28 # Used to test for empty strings
29 use constant EMPTY => q{};
32 use constant HTTP_CODE_OK => 200;
33 use constant HTTP_CODE_PAGE_NOT_FOUND => 404;
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
51 $filename =~ s{/}{@{[SLASH_REPLACEMENT]}}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;
64 my $remote_name = shift;
65 my $remote_url = shift;
66 my ($wiki_login, $wiki_password, $wiki_domain);
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");
72 $wiki = MediaWiki::API->new;
73 $wiki->{config}->{api_url} = "${remote_url}/api.php";
77 'username' => $wiki_login,
78 'password' => $wiki_password
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);
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');
100 1; # Famous last words