1package Git::Mediawiki; 2 3use5.008; 4use strict; 5use Git; 6 7BEGIN{ 8 9our($VERSION,@ISA,@EXPORT,@EXPORT_OK); 10 11# Totally unstable API. 12$VERSION='0.01'; 13 14require 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 26useconstant SLASH_REPLACEMENT =>'%2F'; 27 28# Used to test for empty strings 29useconstant EMPTY =>q{}; 30 31# HTTP codes 32useconstant HTTP_CODE_OK =>200; 33useconstant HTTP_CODE_PAGE_NOT_FOUND =>404; 34 35sub clean_filename { 36my$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 46return$filename; 47} 48 49sub smudge_filename { 50my$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; 55return$filename; 56} 57 58sub connect_maybe { 59my$wiki=shift; 60if($wiki) { 61return$wiki; 62} 63 64my$remote_name=shift; 65my$remote_url=shift; 66my($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"; 74if($wiki_login) { 75my%credential= ( 76'url'=>$remote_url, 77'username'=>$wiki_login, 78'password'=>$wiki_password 79); 80 Git::credential(\%credential); 81my$request= {lgname =>$credential{username}, 82 lgpassword =>$credential{password}, 83 lgdomain =>$wiki_domain}; 84if($wiki->login($request)) { 85 Git::credential(\%credential,'approve'); 86print{*STDERR}qq(Logged in mediawiki user "$credential{username}".\n); 87}else{ 88print{*STDERR}qq(Failed to log in mediawiki user "$credential{username}" on ${remote_url}\n); 89print{*STDERR}' (error '. 90$wiki->{error}->{code} .': '. 91$wiki->{error}->{details} .")\n"; 92 Git::credential(\%credential,'reject'); 93exit1; 94} 95} 96 97return$wiki; 98} 99 1001;# Famous last words