]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/mw-to-git/t/test-gitmw.pl
remote-mediawiki tests: replace deprecated Perl construct
[thirdparty/git.git] / contrib / mw-to-git / t / test-gitmw.pl
CommitLineData
8435b289
GS
1#!/usr/bin/perl -w -s
2# Copyright (C) 2012
3# Charles Roussel <charles.roussel@ensimag.imag.fr>
4# Simon Cathebras <simon.cathebras@ensimag.imag.fr>
5# Julien Khayat <julien.khayat@ensimag.imag.fr>
6# Guillaume Sasdy <guillaume.sasdy@ensimag.imag.fr>
7# Simon Perrat <simon.perrat@ensimag.imag.fr>
8# License: GPL v2 or later
9
10# Usage:
11# ./test-gitmw.pl <command> [argument]*
12# Execute in terminal using the name of the function to call as first
13# parameter, and the function's arguments as following parameters
14#
15# Example:
16# ./test-gitmw.pl "get_page" foo .
17# will call <wiki_getpage> with arguments <foo> and <.>
18#
19# Available functions are:
20# "get_page"
21# "delete_page"
22# "edit_page"
23# "getallpagename"
24
25use MediaWiki::API;
26use Getopt::Long;
8435b289
GS
27use DateTime::Format::ISO8601;
28use open ':encoding(utf8)';
29use constant SLASH_REPLACEMENT => "%2F";
30
31#Parsing of the config file
32
33my $configfile = "$ENV{'CURR_DIR'}/test.config";
34my %config;
35open my $CONFIG, "<", $configfile or die "can't open $configfile: $!";
36while (<$CONFIG>)
37{
38 chomp;
39 s/#.*//;
40 s/^\s+//;
41 s/\s+$//;
42 next unless length;
43 my ($key, $value) = split (/\s*=\s*/,$_, 2);
44 $config{$key} = $value;
45 last if ($key eq 'LIGHTTPD' and $value eq 'false');
46 last if ($key eq 'PORT');
47}
48close $CONFIG or die "can't close $configfile: $!";
49
50my $wiki_address = "http://$config{'SERVER_ADDR'}".":"."$config{'PORT'}";
51my $wiki_url = "$wiki_address/$config{'WIKI_DIR_NAME'}/api.php";
52my $wiki_admin = "$config{'WIKI_ADMIN'}";
53my $wiki_admin_pass = "$config{'WIKI_PASSW'}";
54my $mw = MediaWiki::API->new;
55$mw->{config}->{api_url} = $wiki_url;
56
57
58# wiki_login <name> <password>
59#
60# Logs the user with <name> and <password> in the global variable
61# of the mediawiki $mw
62sub wiki_login {
63 $mw->login( { lgname => "$_[0]",lgpassword => "$_[1]" } )
64 || die "getpage: login failed";
65}
66
67# wiki_getpage <wiki_page> <dest_path>
68#
69# fetch a page <wiki_page> from the wiki referenced in the global variable
70# $mw and copies its content in directory dest_path
71sub wiki_getpage {
72 my $pagename = $_[0];
73 my $destdir = $_[1];
74
75 my $page = $mw->get_page( { title => $pagename } );
76 if (!defined($page)) {
77 die "getpage: wiki does not exist";
78 }
79
80 my $content = $page->{'*'};
81 if (!defined($content)) {
82 die "getpage: page does not exist";
83 }
84
85 $pagename=$page->{'title'};
86 # Replace spaces by underscore in the page name
87 $pagename =~ s/ /_/g;
88 $pagename =~ s/\//%2F/g;
89 open(my $file, ">$destdir/$pagename.mw");
90 print $file "$content";
91 close ($file);
92
93}
94
95# wiki_delete_page <page_name>
96#
97# delete the page with name <page_name> from the wiki referenced
98# in the global variable $mw
99sub wiki_delete_page {
100 my $pagename = $_[0];
101
102 my $exist=$mw->get_page({title => $pagename});
103
104 if (defined($exist->{'*'})){
105 $mw->edit({ action => 'delete',
106 title => $pagename})
107 || die $mw->{error}->{code} . ": " . $mw->{error}->{details};
108 } else {
109 die "no page with such name found: $pagename\n";
110 }
111}
112
113# wiki_editpage <wiki_page> <wiki_content> <wiki_append> [-c=<category>] [-s=<summary>]
114#
115# Edit a page named <wiki_page> with content <wiki_content> on the wiki
116# referenced with the global variable $mw
117# If <wiki_append> == true : append <wiki_content> at the end of the actual
118# content of the page <wiki_page>
119# If <wik_page> doesn't exist, that page is created with the <wiki_content>
120sub wiki_editpage {
121 my $wiki_page = $_[0];
122 my $wiki_content = $_[1];
123 my $wiki_append = $_[2];
124 my $summary = "";
125 my ($summ, $cat) = ();
126 GetOptions('s=s' => \$summ, 'c=s' => \$cat);
127
128 my $append = 0;
129 if (defined($wiki_append) && $wiki_append eq 'true') {
130 $append=1;
131 }
132
133 my $previous_text ="";
134
135 if ($append) {
136 my $ref = $mw->get_page( { title => $wiki_page } );
137 $previous_text = $ref->{'*'};
138 }
139
140 my $text = $wiki_content;
141 if (defined($previous_text)) {
142 $text="$previous_text$text";
143 }
144
145 # Eventually, add this page to a category.
146 if (defined($cat)) {
147 my $category_name="[[Category:$cat]]";
148 $text="$text\n $category_name";
149 }
150 if(defined($summ)){
151 $summary=$summ;
152 }
153
154 $mw->edit( { action => 'edit', title => $wiki_page, summary => $summary, text => "$text"} );
155}
156
157# wiki_getallpagename [<category>]
158#
159# Fetch all pages of the wiki referenced by the global variable $mw
160# and print the names of each one in the file all.txt with a new line
161# ("\n") between these.
162# If the argument <category> is defined, then this function get only the pages
163# belonging to <category>.
164sub wiki_getallpagename {
165 # fetch the pages of the wiki
166 if (defined($_[0])) {
167 my $mw_pages = $mw->list ( { action => 'query',
168 list => 'categorymembers',
169 cmtitle => "Category:$_[0]",
170 cmnamespace => 0,
171 cmlimit => 500 },
172 )
173 || die $mw->{error}->{code}.": ".$mw->{error}->{details};
174 open(my $file, ">all.txt");
175 foreach my $page (@{$mw_pages}) {
176 print $file "$page->{title}\n";
177 }
178 close ($file);
179
180 } else {
181 my $mw_pages = $mw->list({
182 action => 'query',
183 list => 'allpages',
184 aplimit => 500,
185 })
186 || die $mw->{error}->{code}.": ".$mw->{error}->{details};
187 open(my $file, ">all.txt");
188 foreach my $page (@{$mw_pages}) {
189 print $file "$page->{title}\n";
190 }
191 close ($file);
192 }
193}
194
eb63bfaa
MM
195sub wiki_upload_file {
196 my $file_name = $_[0];
197 my $resultat = $mw->edit ( {
198 action => 'upload',
199 filename => $file_name,
200 comment => 'upload a file',
201 file => [ $file_name ],
202 ignorewarnings=>1,
203 }, {
204 skip_encoding => 1
205 } ) || die $mw->{error}->{code} . ' : ' . $mw->{error}->{details};
206}
207
208
209
8435b289
GS
210# Main part of this script: parse the command line arguments
211# and select which function to execute
212my $fct_to_call = shift;
213
214wiki_login($wiki_admin, $wiki_admin_pass);
215
dde66eb6
ÆAB
216my %functions_to_call = (
217 upload_file => \&wiki_upload_file,
218 get_page => \&wiki_getpage,
219 delete_page => \&wiki_delete_page,
220 edit_page => \&wiki_editpage,
221 getallpagename => \&wiki_getallpagename,
8435b289
GS
222);
223die "$0 ERROR: wrong argument" unless exists $functions_to_call{$fct_to_call};
4f80bc9b 224$functions_to_call{$fct_to_call}->(map { utf8::decode($_); $_ } @ARGV);