#!/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 Sleepycat::DbXml 'simple'; use XML::Twig; use Getopt::Long; # Some files will be save under tree hierarchy based on executable location use FindBin qw($Bin); use lib "$Bin"; # Default values my $path2DbEnv = "$Bin/dbxml"; my $theContainer = 'meps.dbxml'; my $output_dir = "$Bin/spikini/meps"; # Parse command line options Getopt::Long::Configure("bundling"); GetOptions( 'path2dbenv|p=s' => \$path2DbEnv, 'container|c=s' => \$theContainer, 'output|o=s' => \$output_dir, 'help|h' => sub { print STDERR <] [-c ] [-o Options can be: --path2dbenv, -p : path to directory of DB XML database default: $Bin/dbxml --container, -c : DB XML container is typically meps (for convenience the final "s" can be omitted, also ".dbxml" can be appended) default: meps.dbxml --output, -o : output directory of spikini pages default: $Bin/spikini/meps --help, -h: print this message USAGE exit 0; } ); $theContainer =~ s/s?(?:\.dbxml)?$/s/; my $mp_wiki = shift or die "You should provide the wiki name of the Member of the Parliament, see $0 -h\n"; # Open a container in the db environment my $env = new DbEnv(0); $env->set_cachesize(0, 64 * 1024, 1); $env->open($path2DbEnv, Db::DB_INIT_MPOOL|Db::DB_CREATE|Db::DB_INIT_LOCK|Db::DB_INIT_LOG); my $theMgr = new XmlManager($env); my $container = $theMgr->openContainer("$theContainer.dbxml"); # Get the document eval { my $theDocument = $container->getDocument($mp_wiki); my $docString = $theDocument->getContent(); # Build xml tree my $xml = XML::Twig->new(output_encoding => 'ISO-8859-1')->parse($value); # Extract infos my $infos = $xml->root->first_child('infos'); my $name = $infos->first_child('name'); my $first_name = $name->first_child_text('first'); my $last_name = $name->first_child_text('last'); my $wiki_name = $name->first_child_text('wiki'); my $pic = $infos->first_child_text('picture'); my $birth = $infos->first_child('birth'); my $role = 'eurodéputé'.(($birth->first_child_text('gender') eq 'Né')?'':'e'); my $birth_date = $birth->first_child('date'); my $birth_place = $birth->first_child('place'); my $birth_str = sprintf "{%s le} %02d %s %04d {à} %s", $birth->first_child_text('gender'), $birth_date->first_child_text('day'), $birth_date->first_child_text('month'), $birth_date->first_child_text('year'), $birth_place->first_child_text('city'); my $constituency = $infos->first_child('constituency'); my $region = $constituency->first_child('region')->first_child_text('name'); my $region_wiki = wikify_reg($region); my $grp = $infos->first_child('group'); my $grp_str = $grp->first_child_text('name'); my $party = $grp->first_child_text('party'); my $grp_abbrev = $grp->first_child_text('abbreviation'); my $grp_wiki = wikify_grp($grp_abbrev); my $contact = $xml->root->first_child('contact'); my $address = "-* " . join("\n-* ", map({($_->first_child('label')?$_->first_child_text('label')."\n_ ":'').($_->first_child('building')?'Bâtiment '.$_->first_child_text('building').' ':'').($_->first_child('office')?$_->first_child_text('office')."\n_ ":'').($_->first_child('street')?$_->first_child_text('street').',':'').($_->first_child('postcode')?' '.$_->first_child_text('postcode'):'').($_->first_child('city')?' '.$_->first_child_text('city'):'').($_->first_child('phone')?(" \n_ {Tél.} : ".join('/', $_->children_text('phone'))):'').($_->first_child('fax')?("\n_ {Fax} : ".join('/', $_->children_text('fax'))):'')} $contact->children('address')))."\n"; # Hack for telephone/fax without address $address =~ s/^-\* - /-* /om; my $mail = $contact->first_child_text('email'); my $mail_str; (($mail_str = $mail) =~ s/@/(à)/o) if $mail; my @webs = map { (my $web_str = $_->text) =~ s#^http://##o; my $type=$_->att('type')?ucfirst($_->att('type')):'Site web'; [$_->text(), $web_str, $type]; } $contact->children('web'); my @functions; foreach my $func ($xml->root->first_child('functions')->children('function')) { my $label = $func->first_child_text('label'); my $role = $func->first_child_text('role'); if ($label =~ /^Commission/o) { unless ($label =~ /^Commission (?:temporaire|d'enquête)/o) { $label = "[$label->".wikify_com($label)."]"; } } my $func_str = "$label ($role)"; push @functions, $func_str; } my @positions = map {$_->text} $xml->root->first_child('cv')->children('position'); my $activities = $xml->root->first_child('activities'); my $questions_url = $activities->first_child_text('questions'); my $motions_url = $activities->first_child_text('motions'); my $reports_url = $activities->first_child_text('reports'); my $declarations_url = $activities->first_child_text('declarations'); my @opinions=(); if ($xml->root->first_child('opinions')) { @opinions = map {"- ".$_->first_child_text('date')." [".$_->first_child_text('title')."->".$_->first_child_text('url')."]\n\n".$_->first_child_text('content')."\n\n"} $xml->root->first_child('opinions')->children('opinion'); } # Build spikini page my $spikini_page = << "SPIKINI_PAGE"; {{{GroupeMemoirePolitique : $first_name $last_name, $role}}} ----- - {{Informations générales}} -* $birth_str -* {Région d'élection} : [$region -> MemoirePolitiqueEurodeputes$region_wiki] -* {Groupe politique} : [$grp_str ($grp_abbrev) -> MemoirePolitiqueEurodeputes$grp_wiki] _ {Parti} : $party SPIKINI_PAGE $spikini_page .= "\n- {{Contact}}\n\n$address\n"; $spikini_page .= "-* {Courriel} : [$mail_str->mailto:$mail]\n" if $mail; foreach my $web (@webs) { $spikini_page .= "-* {$web->[2]} : [$web->[1] -> $web->[0]]\n"; } if (scalar @functions) { $spikini_page .= "\n- {{Fonctions au Parlement européen}}\n\n"; foreach my $function (@functions) { $spikini_page .= "-* $function\n"; } } if (scalar @positions) { $spikini_page .= "\n- {{Curriculum Vitae}}\n\n"; foreach my $pos (@positions) { $spikini_page .= "-* $pos\n"; } } $spikini_page .= <<"SPIKINI"; ----- {{{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}} -* [Questions posées au Parlement européen->$questions_url] -* [Motions de résolutions->$motions_url] -* [Rapports parlementaires->$reports_url] -* [Déclarations écrites au Parlement européen->$declarations_url] -* [Wikipédia->http://fr.wikipedia.org/wiki/${first_name}_${last_name}] -* [Google->http://www.google.fr/search?q=$first_name+$last_name+brevet] SPIKINI foreach my $opinion (@opinions) { $spikini_page .= "$opinion\n"; } # ISO-8859-1 doesn't include some characters $spikini_page =~ s/\x{153}/œ/g; $spikini_page =~ s/\x{2019}/'/g; # Output spikini page my $spikini_location = "$output_dir/$wiki_name.spikini"; open SPIKINI, ">$spikini_location" or die "Cannot write in file $spikini_location: $!\n"; print SPIKINI $spikini_page; close SPIKINI or die "Error when closing $spikini_location: $!\n"; warn "$spikini_location done\n"; }; if (my $e = catch std::exception) { warn "getDocument failed\n"; warn $e->what() . "\n"; exit( -1 ); } elsif ($@) { warn "getDocument failed\n"; warn $@; exit( -1 ); } sub wikify_grp { my $str = shift; $str =~ s!/.+$!!o; return $str; } sub wikify_com { my $str = shift; $str =~ s/\b(?:de la|de l'|du|des|, )\b//go; $str =~ s/(\w+)/\u\L$1/go; $str = desaccent($str); return $str; } sub wikify_reg { my $str = shift; $str =~ s/(\w+)/\u\L$1/go; $str = desaccent($str); return $str; } sub desaccent { my $str = shift; my $mp_name = $str; $str =~ s/À/A/go; $str =~ s/Â/A/go; $str =~ s/Ä/A/go; $str =~ s/É/E/go; $str =~ s/È/E/go; $str =~ s/Ê/E/go; $str =~ s/Ë/E/go; $str =~ s/Í/I/go; $str =~ s/Î/I/go; $str =~ s/Ï/I/go; $str =~ s/Ó/O/go; $str =~ s/Ô/O/go; $str =~ s/Ö/O/go; $str =~ s/Ù/U/go; $str =~ s/Û/U/go; $str =~ s/Ü/U/go; $str =~ s/Ç/C/go; $str =~ s/à/a/go; $str =~ s/â/a/go; $str =~ s/ä/a/go; $str =~ s/é/e/go; $str =~ s/è/e/go; $str =~ s/ê/e/go; $str =~ s/ë/e/go; $str =~ s/í/i/go; $str =~ s/î/i/go; $str =~ s/ï/i/go; $str =~ s/ó/o/go; $str =~ s/ô/o/go; $str =~ s/ö/o/go; $str =~ s/ù/u/go; $str =~ s/û/u/go; $str =~ s/ü/u/go; $str =~ s/ç/c/go; $str =~ s/[ '-]//go; return $str; }