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
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
16 # ./test-gitmw.pl "get_page" foo .
17 # will call <wiki_getpage> with arguments <foo> and <.>
19 # Available functions are:
27 use DateTime::Format::ISO8601;
28 use open ':encoding(utf8)';
29 use constant SLASH_REPLACEMENT => "%2F";
31 #Parsing of the config file
33 my $configfile = "$ENV{'CURR_DIR'}/test.config";
35 open my $CONFIG, "<", $configfile or die "can't open $configfile: $!";
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');
48 close $CONFIG or die "can't close $configfile: $!";
50 my $wiki_address = "http://$config{'SERVER_ADDR'}".":"."$config{'PORT'}";
51 my $wiki_url = "$wiki_address/$config{'WIKI_DIR_NAME'}/api.php";
52 my $wiki_admin = "$config{'WIKI_ADMIN'}";
53 my $wiki_admin_pass = "$config{'WIKI_PASSW'}";
54 my $mw = MediaWiki::API->new;
55 $mw->{config}->{api_url} = $wiki_url;
58 # wiki_login <name> <password>
60 # Logs the user with <name> and <password> in the global variable
61 # of the mediawiki $mw
63 $mw->login( { lgname => "$_[0]",lgpassword => "$_[1]" } )
64 || die "getpage: login failed";
67 # wiki_getpage <wiki_page> <dest_path>
69 # fetch a page <wiki_page> from the wiki referenced in the global variable
70 # $mw and copies its content in directory dest_path
75 my $page = $mw->get_page( { title => $pagename } );
76 if (!defined($page)) {
77 die "getpage: wiki does not exist";
80 my $content = $page->{'*'};
81 if (!defined($content)) {
82 die "getpage: page does not exist";
85 $pagename=$page->{'title'};
86 # Replace spaces by underscore in the page name
88 $pagename =~ s/\//%2F/g;
89 open(my $file, ">$destdir/$pagename.mw");
90 print $file "$content";
95 # wiki_delete_page <page_name>
97 # delete the page with name <page_name> from the wiki referenced
98 # in the global variable $mw
99 sub wiki_delete_page {
100 my $pagename = $_[0];
102 my $exist=$mw->get_page({title => $pagename});
104 if (defined($exist->{'*'})){
105 $mw->edit({ action => 'delete',
107 || die $mw->{error}->{code} . ": " . $mw->{error}->{details};
109 die "no page with such name found: $pagename\n";
113 # wiki_editpage <wiki_page> <wiki_content> <wiki_append> [-c=<category>] [-s=<summary>]
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>
121 my $wiki_page = $_[0];
122 my $wiki_content = $_[1];
123 my $wiki_append = $_[2];
125 my ($summ, $cat) = ();
126 GetOptions('s=s' => \$summ, 'c=s' => \$cat);
129 if (defined($wiki_append) && $wiki_append eq 'true') {
133 my $previous_text ="";
136 my $ref = $mw->get_page( { title => $wiki_page } );
137 $previous_text = $ref->{'*'};
140 my $text = $wiki_content;
141 if (defined($previous_text)) {
142 $text="$previous_text$text";
145 # Eventually, add this page to a category.
147 my $category_name="[[Category:$cat]]";
148 $text="$text\n $category_name";
154 $mw->edit( { action => 'edit', title => $wiki_page, summary => $summary, text => "$text"} );
157 # wiki_getallpagename [<category>]
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>.
164 sub 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]",
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";
181 my $mw_pages = $mw->list({
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";
195 sub wiki_upload_file {
196 my $file_name = $_[0];
197 my $resultat = $mw->edit ( {
199 filename => $file_name,
200 comment => 'upload a file',
201 file => [ $file_name ],
205 } ) || die $mw->{error}->{code} . ' : ' . $mw->{error}->{details};
210 # Main part of this script: parse the command line arguments
211 # and select which function to execute
212 my $fct_to_call = shift;
214 wiki_login($wiki_admin, $wiki_admin_pass);
216 my %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,
223 die "$0 ERROR: wrong argument" unless exists $functions_to_call{$fct_to_call};
224 $functions_to_call{$fct_to_call}->(map { utf8::decode($_); $_ } @ARGV);