#! /usr/bin/perl # Copyright 2007 Gérald Sédrati-Dinet # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA use strict; use warnings; use LWP::UserAgent; use HTTP::Cookies; use Term::ReadKey; use File::Basename qw(basename dirname); use File::Copy; use Unicode::String qw(utf8 latin1); use Getopt::Long; # Some files are related to the executable location use FindBin qw($Bin); use lib "$Bin"; # Default values my $list_file = "$Bin/candidats_presidentielle.txt"; my $container = 'candidates'; my $output_dir = "$Bin/xml/candidates"; # Parse command line options Getopt::Long::Configure("bundling"); GetOptions( 'file|f=s' => \$list_file, 'container|c=s' => \$container, 'output|o=s' => \$output_dir, 'help|h' => sub { print STDERR <] [-o : path of the file containing urls of spikini pages to create default: $Bin/candidats_presidentielle.txt --container, -c : DB XML container is typically candidates, mps, meps or ministers (for convenience the final "s" can be omitted, also ".dbxml" can be appended) default: candidates.dbxml --output, -o : output directory of xml pages default: $Bin/xml/candidates --help, -h: print this message USAGE exit 0; } ); $container =~ s/s?(?:\.dbxml)?$/s/; # Read list of candidates open LIST, $list_file or die "Cannot open $list_file: $!\n"; my @urls = ; close LIST; # Get user authentification information print "Enter login:\n"; my $user = ; chomp $user; print "Enter password:\n"; ReadMode 'noecho'; my $passwd = ReadLine 0; chomp $passwd; ReadMode 'normal'; # Create a user agent object my $ua = LWP::UserAgent->new(env_proxy => 1); $ua->agent("$0 (gibus perl script to remotely insert a page into wiki.ffii.fr)"); $ua->cookie_jar({}); # Authentification stored in cookie my $auth = HTTP::Request->new(POST => "http://wiki.ffii.fr/spip_cookie.php3"); $auth->content_type('application/x-www-form-urlencoded'); $auth->content("session_login_hidden=$user&session_password=$passwd&essai_login=oui&url=/spikini/"); my $res_auth = $ua->request($auth); unless ($res_auth->is_success or $res_auth->is_redirect) { die "Wrong user login/password !".$res_auth->message."!.\n"; } print "OK identified.\n"; foreach my $full_url (@urls) { chomp $full_url; # Define page to insert (my $title = $full_url) =~ s/^.+\///o; my ($first_name, $last_name) = ($title =~ /MemoirePolitique(\w+)([A-Z][a-z]+)$/o); $first_name =~ s/^Jean(.*)$/Jean-$1/o; $first_name =~ s/Jose/José/o; $first_name =~ s/Frederic/Frédéric/o; $first_name =~ s/Gerard/Gérard/o; $last_name =~ s/Bove/Bové/o; (my $wiki_name = $title) =~ s/MemoirePolitique//o; # First request to display form in order to fetch $previous parameter. my $url = "http://wiki.ffii.fr/spikini/$title:edit"; my $req = HTTP::Request->new(POST => $url); $req->content_type('application/x-www-form-urlencoded'); my $res_first = $ua->request($req); unless ($res_first->is_success or $res_first->is_redirect) { die "Error in first request !".$res_first->message."! code=!".$res_first->code."!\n"; } my $first_content = $res_first->content; $first_content =~ /name="previous" value="(\d*)"/o; my $previous = $1; # Insert spikini page if it doesn't exist if ($previous) { print "Skip $title: $url already exists\n"; } else { my $body = <<"BODY"; {{{GroupeMemoirePolitique : $first_name $last_name, candidat à l'élection présidentielle 2007}}} ----- - {{Informations générales}} - {{Contact}} - {{Mandats}} - {{Curriculum Vitae}} ----- {{{Prises de positions}}} {Merci d'enrichir cette partie en y rapportant les prises de positions de $first_name $last_name concernant les brevets (consultez la page MemoirePolitiqueAide pour savoir comment faire).} - {{Sources d'informations}} -* [Wikipédia->http://fr.wikipedia.org/wiki/${first_name}_${last_name}] -* [Google->http://www.google.fr/search?q=$first_name+$last_name+brevet] BODY # Second request to insert the new page my $res = $ua->post($url,[body=>$body,submit=>"Sauver",previous=>$previous]); unless ($res->is_success or $res->is_redirect) { die "Error in second request !".$res->message."! code=!".$res->code."!\n"; } # Third request to change permissions my $url_acl = "http://wiki.ffii.fr/spikini/$title:acls"; my $req_acl = HTTP::Request->new(POST => $url_acl); $req_acl->content_type('application/x-www-form-urlencoded'); $req_acl->content("read_acl=*&write_acl=%2B&comment_acl=%2B&submit=Enregistrer"); my $res_acl = $ua->request($req_acl); unless ($res_acl->is_success or $res_acl->is_redirect) { die "Error in third request !".$res_acl->message."! code=!".$res_acl->code."!\n"; } print "OK page $title inserted.\n"; } my $out_file = "$output_dir/$wiki_name.xml"; if (-f $out_file) { print "Skip $title: $out_file already exists\n"; } else { my $xml = << "XML"; $container.dbxml $first_name $last_name $wiki_name $wiki_name.jpg XML open OUT, ">$out_file" or die "Cannot write into $out_file: $!\n"; print OUT latin1($xml)->utf8; close OUT; print "OK $out_file created.\n"; } }