#!/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 = 'mps.dbxml'; my $output_dir = "$Bin/spikini/mps_commissions"; # 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_commissions --help, -h: print this message USAGE exit 0; } ); $theContainer =~ s/s?(?:\.dbxml)?$/s/; my %commission_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 $commission in distinct-values(collection("mps.dbxml")/politician/functions/function[type/string()='Commission']/label) order by $commission return {$commission} { for $deputy in collection("mps.dbxml")/politician[functions/function/type/string()='Commission' and functions/function/label/string()=$commission] 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/abbreviation/string() } { $deputy/infos/group/name/string() } { concat($deputy/infos/constituency/department/name/string(), " (", $deputy/infos/constituency/department/number/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 $commission = $xml->root->first_child_text('commission'); 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{GROUP} = $mp->first_child_text('group'); $mp_data{DEPARTMENT} = $mp->first_child_text('department'); $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 @{$commission_mps{$commission}}, \%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 commission parlementaire}}}\n\n-----\n\n"; $spikini_page .= join "\n", map {"- [$_ -> MemoirePolitiqueDeputes".wikify_com($_)."]"} sort keys %commission_mps; $spikini_page .= "\n\n-----\n\nRetour à MemoirePolitiqueAppartenance\n"; # Output spikini page my $spikini_location = "$output_dir/DeputesParCommission.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 commission foreach my $commission (sort keys %commission_mps) { my $spikini_page = "{{{GroupeMemoirePolitique : $commission, 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 $deputy (@{$commission_mps{$commission}}) { (my $dep_wiki = $deputy->{DEPARTMENT}) =~ s/ \(\d+\)$//o; $dep_wiki = desaccent($dep_wiki); my $party_wiki; if ($deputy->{GROUP} eq "Députés n'appartenant à aucun groupe") { $party_wiki = 'AucunGroupe' } else { ($party_wiki = $deputy->{PARTY}) =~ s/^Ap\. //o; $party_wiki = ucfirst(lc($party_wiki)); } $spikini_page .= "| $deputy->{NAME} | [$deputy->{PARTY}->MemoirePolitiqueDeputes$party_wiki] | [$deputy->{DEPARTMENT}->MemoirePolitiqueDeputes$dep_wiki] | $deputy->{TEL} | $deputy->{FAX} | $deputy->{EMAIL} |\n"; } $spikini_page .= "\n-----\n\nAutres commissions parlementaires : MemoirePolitiqueDeputesParCommission\n"; # Output spikini page my $commission_wiki = wikify_com($commission); my $spikini_location = "$output_dir/Deputes$commission_wiki.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 wikify_com { my $str = shift; $str =~ s/chargée de|de la|des//go; $str =~ s/l'application de l'|ier et d'apurer les//go; $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; }