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