#!/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 = 'mps.dbxml'; my $output_dir = "$Bin/spikini/mps_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 mps (for convenience the final "s" can be omitted, also ".dbxml" can be appended) default: mps.dbxml --output, -o : output directory of spikini pages default: $Bin/spikini/mps_groups --help, -h: print this message USAGE exit 0; } ); $theContainer =~ s/s?(?:\.dbxml)?$/s/; my %party_mps; 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 $deputy in collection("mps.dbxml")/politician let $party := $deputy/infos/group, $name := $deputy/infos/name order by $party/name/string(), upper-case($name/last/string()), upper-case($name/first/string()) return { $party/name/string() } { if ($party/@status) then { $party/@status/string() } else () } { $party/abbreviation/string() } { concat("[", $name/first/string(), " ", $name/last/string(), "->MemoirePolitique", $name/wiki/string(), "]") } { $deputy/infos/constituency/department/name/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 %mp_data; my $xml = XML::Twig->new(output_encoding => 'ISO-8859-1')->parse($value); $mp_data{NAME} = $xml->root->first_child_text('name'); if ($xml->root->first_child('status')) { $mp_data{NAME} = "{$mp_data{NAME}}"; } $mp_data{PARTY_ABBREV} = $xml->root->first_child_text('abbrev'); $mp_data{DEPARTMENT} = $xml->root->first_child_text('department'); $mp_data{TEL} = $xml->root->first_child_text('tel'); $mp_data{FAX} = $xml->root->first_child_text('fax'); my $mail = $xml->root->first_child('email')?$xml->root->first_child_text('email'):'-'; (my $mail_str = $mail) =~ s/@/(à)/o; $mp_data{EMAIL} = ($mail eq '-')?$mail:"[$mail_str->mailto:$mail]\n"; my $party_name = $xml->root->first_child_text('party'); my $party_wiki; if ($party_name eq "Députés n'appartenant à aucun groupe") { $party_wiki = 'AucunGroupe' } else { ($party_wiki = $mp_data{PARTY_ABBREV}) =~ s/^Ap\. //o; $party_wiki = ucfirst(lc($party_wiki)); } if (exists $party_mps{$party_wiki}) { push @{$party_mps{$party_wiki}->[1]}, \%mp_data; } else { $party_mps{$party_wiki} = [$party_name, [\%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 députés par groupe politique}}}\n\n-----\n\n"; $spikini_page .= join "\n", map {"- [$party_mps{$_}->[0] -> MemoirePolitiqueDeputes$_]"} sort keys %party_mps; $spikini_page .= "\n\n-----\n\nRetour à MemoirePolitiqueAppartenance\n"; # Output spikini page my $spikini_location = "$output_dir/DeputesParGroupe.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 party foreach my $party (sort keys %party_mps) { my $spikini_page = "{{{GroupeMemoirePolitique : $party_mps{$party}->[0], coordonnées des députés}}}\n\n-----\n\n"; $spikini_page .= "| {{Député(e)}} | {{Parti}} | {{Département}} | {{Télephone(s)}} | {{Fax}} | {{Courriel}} |\n"; foreach my $depute (@{$party_mps{$party}->[1]}) { my $dep_wiki = desaccent($depute->{DEPARTMENT}); $spikini_page .= "| $depute->{NAME} | $depute->{PARTY_ABBREV} | [$depute->{DEPARTMENT}->MemoirePolitiqueDeputes$dep_wiki] | $depute->{TEL} | $depute->{FAX} | $depute->{EMAIL} |\n"; } $spikini_page .= "\n{Les député(e)s dont le nom est en italique sont apparenté(e)s au groupe $party_mps{$party}->[0].}\n"; $spikini_page .= "\n\n-----\n\nAutres groupes politiques : MemoirePolitiqueDeputesParGroupe\n"; # Output spikini page my $spikini_location = "$output_dir/Deputes$party.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"; } 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; }