14
14
use strict;
15
15
use MediaWiki::API;
16
16
use Git;
17
+ use Git::Mediawiki qw( clean_filename smudge_filename connect_maybe
18
+ EMPTY HTTP_CODE_OK) ;
17
19
use DateTime::Format::ISO8601;
18
20
use warnings;
19
21
23
25
24
26
use URI::Escape;
25
27
26
- # Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced
27
- use constant SLASH_REPLACEMENT => ' %2F' ;
28
-
29
28
# It's not always possible to delete pages (may require some
30
29
# privileges). Deleted pages are replaced with this content.
31
30
use constant DELETED_CONTENT => " [[Category:Deleted]]\n " ;
40
39
# Used on Git's side to reflect empty edit messages on the wiki
41
40
use constant EMPTY_MESSAGE => ' *Empty MediaWiki Message*' ;
42
41
43
- use constant EMPTY => q{ } ;
44
-
45
42
# Number of pages taken into account at once in submodule get_mw_page_list
46
43
use constant SLICE_SIZE => 50;
47
44
50
47
# the number of links to be returned (500 links max).
51
48
use constant BATCH_SIZE => 10;
52
49
53
- use constant HTTP_CODE_OK => 200;
54
-
55
50
if (@ARGV != 2) {
56
51
exit_error_usage();
57
52
}
@@ -199,37 +194,6 @@ sub parse_command {
199
194
# MediaWiki API instance, created lazily.
200
195
my $mediawiki ;
201
196
202
- sub mw_connect_maybe {
203
- if ($mediawiki ) {
204
- return ;
205
- }
206
- $mediawiki = MediaWiki::API-> new;
207
- $mediawiki -> {config }-> {api_url } = " ${url} /api.php" ;
208
- if ($wiki_login ) {
209
- my %credential = (
210
- ' url' => $url ,
211
- ' username' => $wiki_login ,
212
- ' password' => $wiki_passwd
213
- );
214
- Git::credential(\%credential );
215
- my $request = {lgname => $credential {username },
216
- lgpassword => $credential {password },
217
- lgdomain => $wiki_domain };
218
- if ($mediawiki -> login($request )) {
219
- Git::credential(\%credential , ' approve' );
220
- print {*STDERR } qq( Logged in mediawiki user "$credential {username}".\n ) ;
221
- } else {
222
- print {*STDERR } qq( Failed to log in mediawiki user "$credential {username}" on ${url} \n ) ;
223
- print {*STDERR } ' (error ' .
224
- $mediawiki -> {error }-> {code } . ' : ' .
225
- $mediawiki -> {error }-> {details } . " )\n " ;
226
- Git::credential(\%credential , ' reject' );
227
- exit 1;
228
- }
229
- }
230
- return ;
231
- }
232
-
233
197
sub fatal_mw_error {
234
198
my $action = shift ;
235
199
print STDERR " fatal: could not $action .\n " ;
@@ -339,7 +303,7 @@ sub get_mw_first_pages {
339
303
340
304
# Get the list of pages to be fetched according to configuration.
341
305
sub get_mw_pages {
342
- mw_connect_maybe( );
306
+ $mediawiki = connect_maybe( $mediawiki , $remotename , $url );
343
307
344
308
print {*STDERR } " Listing pages on remote wiki...\n " ;
345
309
@@ -529,7 +493,7 @@ sub get_last_local_revision {
529
493
# avoid a loop onto all tracked pages. This is useful for the fetch-by-rev
530
494
# option.
531
495
sub get_last_global_remote_rev {
532
- mw_connect_maybe( );
496
+ $mediawiki = connect_maybe( $mediawiki , $remotename , $url );
533
497
534
498
my $query = {
535
499
action => ' query' ,
@@ -545,7 +509,7 @@ sub get_last_global_remote_rev {
545
509
# Get the last remote revision concerning the tracked pages and the tracked
546
510
# categories.
547
511
sub get_last_remote_revision {
548
- mw_connect_maybe( );
512
+ $mediawiki = connect_maybe( $mediawiki , $remotename , $url );
549
513
550
514
my %pages_hash = get_mw_pages();
551
515
my @pages = values (%pages_hash );
@@ -601,29 +565,6 @@ sub mediawiki_smudge {
601
565
return " ${string} \n " ;
602
566
}
603
567
604
- sub mediawiki_clean_filename {
605
- my $filename = shift ;
606
- $filename =~ s { @{[SLASH_REPLACEMENT]}} { /} g ;
607
- # [, ], |, {, and } are forbidden by MediaWiki, even URL-encoded.
608
- # Do a variant of URL-encoding, i.e. looks like URL-encoding,
609
- # but with _ added to prevent MediaWiki from thinking this is
610
- # an actual special character.
611
- $filename =~ s / [\[\]\{\}\| ]/ sprintf("_%%_ %x ", ord($& ))/ ge ;
612
- # If we use the uri escape before
613
- # we should unescape here, before anything
614
-
615
- return $filename ;
616
- }
617
-
618
- sub mediawiki_smudge_filename {
619
- my $filename = shift ;
620
- $filename =~ s { /} { @{[SLASH_REPLACEMENT]}} g ;
621
- $filename =~ s / / _/ g ;
622
- # Decode forbidden characters encoded in mediawiki_clean_filename
623
- $filename =~ s / _%_([0-9a-fA-F][0-9a-fA-F])/ sprintf('%c ', hex($1 ))/ ge ;
624
- return $filename ;
625
- }
626
-
627
568
sub literal_data {
628
569
my ($content ) = @_ ;
629
570
print {*STDOUT } ' data ' , bytes::length ($content ), " \n " , $content ;
@@ -831,7 +772,7 @@ sub mw_import_ref {
831
772
return ;
832
773
}
833
774
834
- mw_connect_maybe( );
775
+ $mediawiki = connect_maybe( $mediawiki , $remotename , $url );
835
776
836
777
print {*STDERR } " Searching revisions...\n " ;
837
778
my $last_local = get_last_local_revision();
@@ -945,7 +886,7 @@ sub mw_import_revids {
945
886
my %commit ;
946
887
$commit {author } = $rev -> {user } || ' Anonymous' ;
947
888
$commit {comment } = $rev -> {comment } || EMPTY_MESSAGE;
948
- $commit {title } = mediawiki_smudge_filename ($page_title );
889
+ $commit {title } = smudge_filename ($page_title );
949
890
$commit {mw_revision } = $rev -> {revid };
950
891
$commit {content } = mediawiki_smudge($rev -> {' *' });
951
892
@@ -1006,7 +947,7 @@ sub mw_upload_file {
1006
947
}
1007
948
# Deleting and uploading a file requires a priviledged user
1008
949
if ($file_deleted ) {
1009
- mw_connect_maybe( );
950
+ $mediawiki = connect_maybe( $mediawiki , $remotename , $url );
1010
951
my $query = {
1011
952
action => ' delete' ,
1012
953
title => $path ,
@@ -1022,7 +963,7 @@ sub mw_upload_file {
1022
963
# Don't let perl try to interpret file content as UTF-8 => use "raw"
1023
964
my $content = run_git(" cat-file blob ${new_sha1} " , ' raw' );
1024
965
if ($content ne EMPTY) {
1025
- mw_connect_maybe( );
966
+ $mediawiki = connect_maybe( $mediawiki , $remotename , $url );
1026
967
$mediawiki -> {config }-> {upload_url } =
1027
968
" ${url} /index.php/Special:Upload" ;
1028
969
$mediawiki -> edit({
@@ -1070,7 +1011,7 @@ sub mw_push_file {
1070
1011
my $old_sha1 = $diff_info_split [2];
1071
1012
my $page_created = ($old_sha1 eq NULL_SHA1);
1072
1013
my $page_deleted = ($new_sha1 eq NULL_SHA1);
1073
- $complete_file_name = mediawiki_clean_filename ($complete_file_name );
1014
+ $complete_file_name = clean_filename ($complete_file_name );
1074
1015
1075
1016
my ($title , $extension ) = $complete_file_name =~ / ^(.*)\. ([^\. ]*)$ / ;
1076
1017
if (!defined ($extension )) {
@@ -1093,7 +1034,7 @@ sub mw_push_file {
1093
1034
$file_content = run_git(" cat-file blob ${new_sha1} " );
1094
1035
}
1095
1036
1096
- mw_connect_maybe( );
1037
+ $mediawiki = connect_maybe( $mediawiki , $remotename , $url );
1097
1038
1098
1039
my $result = $mediawiki -> edit( {
1099
1040
action => ' edit' ,
@@ -1279,7 +1220,7 @@ sub mw_push_revision {
1279
1220
}
1280
1221
1281
1222
sub get_allowed_file_extensions {
1282
- mw_connect_maybe( );
1223
+ $mediawiki = connect_maybe( $mediawiki , $remotename , $url );
1283
1224
1284
1225
my $query = {
1285
1226
action => ' query' ,
@@ -1303,7 +1244,7 @@ sub get_allowed_file_extensions {
1303
1244
# Return MediaWiki id for a canonical namespace name.
1304
1245
# Ex.: "File", "Project".
1305
1246
sub get_mw_namespace_id {
1306
- mw_connect_maybe( );
1247
+ $mediawiki = connect_maybe( $mediawiki , $remotename , $url );
1307
1248
my $name = shift ;
1308
1249
1309
1250
if (!exists $namespace_id {$name }) {
0 commit comments