1 package Git::Mediawiki;
10 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
12 # Totally unstable API.
21 # Methods which can be called as standalone functions as well:
22 @EXPORT_OK = qw(clean_filename smudge_filename connect_maybe
23 EMPTY HTTP_CODE_OK HTTP_CODE_PAGE_NOT_FOUND);
26 # Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced
27 use constant SLASH_REPLACEMENT => '%2F';
29 # Used to test for empty strings
30 use constant EMPTY => q{};
33 use constant HTTP_CODE_OK => 200;
34 use constant HTTP_CODE_PAGE_NOT_FOUND => 404;
38 $filename =~ s{@{[SLASH_REPLACEMENT]}}{/}g;
39 # [, ], |, {, and } are forbidden by MediaWiki, even URL-encoded.
40 # Do a variant of URL-encoding, i.e. looks like URL-encoding,
41 # but with _ added to prevent MediaWiki from thinking this is
42 # an actual special character.
43 $filename =~ s/[\[\]\{\}\|]/sprintf("_%%_%x", ord($&))/ge;
44 # If we use the uri escape before
45 # we should unescape here, before anything
52 $filename =~ s{/}{@{[SLASH_REPLACEMENT]}}g;
54 # Decode forbidden characters encoded in clean_filename
55 $filename =~ s/_%_([0-9a-fA-F][0-9a-fA-F])/sprintf('%c', hex($1))/ge;
56 return substr($filename, 0, NAME_MAX-length('.mw'));
65 my $remote_name = shift;
66 my $remote_url = shift;
67 my ($wiki_login, $wiki_password, $wiki_domain);
69 $wiki_login = Git::config("remote.${remote_name}.mwLogin");
70 $wiki_password = Git::config("remote.${remote_name}.mwPassword");
71 $wiki_domain = Git::config("remote.${remote_name}.mwDomain");
73 $wiki = MediaWiki::API->new;
74 $wiki->{config}->{api_url} = "${remote_url}/api.php";
78 'username' => $wiki_login,
79 'password' => $wiki_password
81 Git::credential(\%credential);
82 my $request = {lgname => $credential{username},
83 lgpassword => $credential{password},
84 lgdomain => $wiki_domain};
85 if ($wiki->login($request)) {
86 Git::credential(\%credential, 'approve');
87 print {*STDERR} qq(Logged in mediawiki user "$credential{username}".\n);
89 print {*STDERR} qq(Failed to log in mediawiki user "$credential{username}" on ${remote_url}\n);
90 print {*STDERR} ' (error ' .
91 $wiki->{error}->{code} . ': ' .
92 $wiki->{error}->{details} . ")\n";
93 Git::credential(\%credential, 'reject');
101 1; # Famous last words