Start the 2.46 cycle
[git.git] / contrib / mw-to-git / t / test-gitmw.pl
blobc5d687f078a8f001d283bff5ffebd7ff1ee0f022
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
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
15 # Example:
16 # ./test-gitmw.pl "get_page" foo .
17 # will call <wiki_getpage> with arguments <foo> and <.>
19 # Available functions are:
20 # "get_page"
21 # "delete_page"
22 # "edit_page"
23 # "getallpagename"
25 use MediaWiki::API;
26 use Getopt::Long;
27 use DateTime::Format::ISO8601;
28 use constant SLASH_REPLACEMENT => "%2F";
30 #Parsing of the config file
32 my $configfile = "$ENV{'CURR_DIR'}/test.config";
33 my %config;
34 open my $CONFIG, "<", $configfile or die "can't open $configfile: $!";
35 while (<$CONFIG>)
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');
47 close $CONFIG or die "can't close $configfile: $!";
49 my $wiki_address = "http://$config{'SERVER_ADDR'}".":"."$config{'PORT'}";
50 my $wiki_url = "$wiki_address/$config{'WIKI_DIR_NAME'}/api.php";
51 my $wiki_admin = "$config{'WIKI_ADMIN'}";
52 my $wiki_admin_pass = "$config{'WIKI_PASSW'}";
53 my $mw = MediaWiki::API->new;
54 $mw->{config}->{api_url} = $wiki_url;
57 # wiki_login <name> <password>
59 # Logs the user with <name> and <password> in the global variable
60 # of the mediawiki $mw
61 sub wiki_login {
62 $mw->login( { lgname => "$_[0]",lgpassword => "$_[1]" } )
63 || die "getpage: login failed";
66 # wiki_getpage <wiki_page> <dest_path>
68 # fetch a page <wiki_page> from the wiki referenced in the global variable
69 # $mw and copies its content in directory dest_path
70 sub wiki_getpage {
71 my $pagename = $_[0];
72 my $destdir = $_[1];
74 my $page = $mw->get_page( { title => $pagename } );
75 if (!defined($page)) {
76 die "getpage: wiki does not exist";
79 my $content = $page->{'*'};
80 if (!defined($content)) {
81 die "getpage: page does not exist";
84 $pagename=$page->{'title'};
85 # Replace spaces by underscore in the page name
86 $pagename =~ s/ /_/g;
87 $pagename =~ s/\//%2F/g;
88 open(my $file, ">:encoding(UTF-8)", "$destdir/$pagename.mw");
89 print $file "$content";
90 close ($file);
94 # wiki_delete_page <page_name>
96 # delete the page with name <page_name> from the wiki referenced
97 # in the global variable $mw
98 sub wiki_delete_page {
99 my $pagename = $_[0];
101 my $exist=$mw->get_page({title => $pagename});
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";
112 # wiki_editpage <wiki_page> <wiki_content> <wiki_append> [-c=<category>] [-s=<summary>]
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>
119 sub 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);
127 my $append = 0;
128 if (defined($wiki_append) && $wiki_append eq 'true') {
129 $append=1;
132 my $previous_text ="";
134 if ($append) {
135 my $ref = $mw->get_page( { title => $wiki_page } );
136 $previous_text = $ref->{'*'};
139 my $text = $wiki_content;
140 if (defined($previous_text)) {
141 $text="$previous_text$text";
144 # Eventually, add this page to a category.
145 if (defined($cat)) {
146 my $category_name="[[Category:$cat]]";
147 $text="$text\n $category_name";
149 if(defined($summ)){
150 $summary=$summ;
153 $mw->edit( { action => 'edit', title => $wiki_page, summary => $summary, text => "$text"} );
156 # wiki_getallpagename [<category>]
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>.
163 sub 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 },
172 || die $mw->{error}->{code}.": ".$mw->{error}->{details};
173 open(my $file, ">:encoding(UTF-8)", "all.txt");
174 foreach my $page (@{$mw_pages}) {
175 print $file "$page->{title}\n";
177 close ($file);
179 } else {
180 my $mw_pages = $mw->list({
181 action => 'query',
182 list => 'allpages',
183 aplimit => 500,
185 || die $mw->{error}->{code}.": ".$mw->{error}->{details};
186 open(my $file, ">:encoding(UTF-8)", "all.txt");
187 foreach my $page (@{$mw_pages}) {
188 print $file "$page->{title}\n";
190 close ($file);
194 sub 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};
209 # Main part of this script: parse the command line arguments
210 # and select which function to execute
211 my $fct_to_call = shift;
213 wiki_login($wiki_admin, $wiki_admin_pass);
215 my %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,
222 die "$0 ERROR: wrong argument" unless exists $functions_to_call{$fct_to_call};
223 $functions_to_call{$fct_to_call}->(map { utf8::decode($_); $_ } @ARGV);