#!/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; use locale; use POSIX 'locale_h'; setlocale(LC_CTYPE, "fr_FR"); # 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_groups"; # 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 : 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_groups --help, -h: print this message USAGE exit 0; } ); $theContainer =~ s/s?(?:\.dbxml)?$/s/; my %groups = ( 'Groupe du Parti populaire européen (Démocrates-chrétiens) et des Démocrates européens' => [0,'PPE/DE'], 'Groupe socialiste au Parlement européen' => [1,'PSE'], 'Groupe Alliance des démocrates et des libéraux pour l\'Europe' => [2, 'ALDE'], 'Groupe Union pour l\'Europe des Nations' => [3, 'UEN'], 'Groupe des Verts/Alliance libre européenne' => [4, 'Verts/ALE'], 'Groupe confédéral de la Gauche unitaire européenne/Gauche verte nordique' => [5, 'GUE/NGL'], 'Groupe Indépendance/Démocratie' => [6, 'IND/DEM'], 'Groupe Identité, Tradition, Souveraineté' => [7, 'ITS'], 'Non-inscrits' => 'NI', ); my %group_meps; eval { # 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"); my $query = <<'QUERY'; for $group in distinct-values(collection("meps.dbxml")/politician/infos/group/name) return {$group} { for $deputy in collection("meps.dbxml")/politician[infos/group/name/string()=$group] order by upper-case($deputy/infos/name/last/string()), upper-case($deputy/infos/name/first/string()) return { concat("[", $deputy/infos/name/first/string(), " ", $deputy/infos/name/last/string(), "->MemoirePolitique", $deputy/infos/name/wiki/string(), "]") } { $deputy/infos/group/party/string() } { string-join(for $tel in $deputy/contact//phone return $tel/string(), "/") } { string-join(for $fax in $deputy/contact//fax return $fax/string(), "/") } { if ($deputy/contact/email) then { $deputy/contact/email/string() } else () } } QUERY # Perform the query eval { my $results = $theMgr->query($query); my $value; while( $results->next($value) ) { my $xml = XML::Twig->new(output_encoding => 'ISO-8859-1')->parse($value); my $group = $xml->root->first_child_text('group'); foreach my $mp ($xml->root->children('deputy')) { my %mp_data; $mp_data{NAME} = $mp->first_child_text('name'); $mp_data{PARTY} = $mp->first_child_text('party'); $mp_data{TEL} = $mp->first_child_text('tel'); $mp_data{FAX} = $mp->first_child_text('fax'); my $mail = $mp->first_child('email')?$mp->first_child_text('email'):'-'; (my $mail_str = $mail) =~ s/@/(à)/o; $mp_data{EMAIL} = ($mail eq '-')?$mail:"[$mail_str->mailto:$mail]"; push @{$group_meps{$group}}, \%mp_data; } } }; if (my $e = catch std::exception) { warn "Query $query failed\n"; warn $e->what() . "\n"; exit( -1 ); } elsif ($@) { warn "Query $query failed\n"; warn $@; exit( -1 ); } }; if (my $e = catch std::exception) { warn "Query failed\n"; warn $e->what() . "\n"; exit( -1 ); } elsif ($@) { warn "Query failed\n"; warn $@; exit( -1 ); } # Build spikini page for navigation my $spikini_page = "{{{GroupeMemoirePolitique : coordonnées des eurodéputés par groupe politique}}}\n\n-----\n\n"; $spikini_page .= join "\n", map {"- [$_ -> MemoirePolitiqueEurodeputes".wikify_grp($_)."]"} sort { $groups{$a}->[0] <=> $groups{$b}->[0] } keys %group_meps; $spikini_page .= "\n\n-----\n\nRetour à MemoirePolitiqueAppartenance\n"; # Output spikini page my $spikini_location = "$output_dir/EurodeputesParGroupe.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"; # Build spikini pages for each group foreach my $group (sort keys %group_meps) { my $group_wiki = wikify_grp($group); my $spikini_page = "{{{GroupeMemoirePolitique : $group, coordonnées des eurodéputés}}}\n\n-----\n\n"; $spikini_page .= "| {{Eurodéputé(e)}} | {{Parti}} | {{Télephone(s)}} | {{Fax}} | {{Courriel}} |\n"; foreach my $deputy (@{$group_meps{$group}}) { $spikini_page .= "| $deputy->{NAME} | $deputy->{PARTY} | $deputy->{TEL} | $deputy->{FAX} | $deputy->{EMAIL} |\n"; } $spikini_page .= "\n-----\n\nAutres groupes politiques : MemoirePolitiqueEurodeputesParGroupe\n"; # Output spikini page my $spikini_location = "$output_dir/Eurodeputes$group_wiki.spikini"; open SPIKINI, ">$spikini_location" or die "Cannot write in file $spikini_location: $!\n"; $spikini_page =~ s/\x{2019}/'/g; print SPIKINI $spikini_page; close SPIKINI or die "Error when closing $spikini_location: $!\n"; warn "$spikini_location done\n"; } sub wikify_grp { my $full_name = shift; my $wiki = $groups{$full_name}->[1]; $wiki =~ s!/.+$!!o; return $wiki; }