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