#!/usr/bin/perl # Déclaration des pragmas use strict; use utf8; use open qw/:std :utf8/; # Appel des modules externes de base use Encode qw(decode_utf8 encode_utf8 is_utf8); use Getopt::Long; # Appel des modules spécifiques à l'application ## use URI::Encode qw(uri_encode uri_decode); ## use LWP::Simple; use JSON; ## use Text::Unidecode; my ($programme) = $0 =~ m|^(?:.*/)?(.+)|; my $version = "1.2.1"; my $dateModif = "29 Septembre 2017"; # Variables my $aide = 0; my $corpus = ""; my $logfile = ""; my $prefixe = ""; my $repertoire = ""; eval { $SIG{__WARN__} = sub {usage(1);}; GetOptions( "corpus=s" => \$corpus, "help" => \$aide, "logfile=s" => \$logfile, "prefixe=s" => \$prefixe, "repertoire=s" => \$repertoire, ); }; $SIG{__WARN__} = sub {warn $_[0];}; # “ machin ” if ( $aide ) { print "Programme : \n “$programme”, version $version ($dateModif)\n"; print " Permet de faire des statistiques sur les fichiers extraits d’ISTEX en utilisant le fichier \n"; print " de métadonnées “logRequete.txt” (cf. l’option “-v” du programme “harvestCorpus.pl”). \n"; # print " \n"; print " Si les fichiers XML éditeurs ont été extraits, il permet aussi de vérifier s’ils sont \n"; print " structurés ou non. \n"; print "\nUsage : \n $programme -l logfile ( -p préfixe | -c corpus ) [ -r répertoire ] \n"; print " $programme -h\n"; print "\nOptions : \n"; print " -l indique le nom du fichier “logfile” contenant les métadonnées ISTEX au format JSON \n"; print " créé par l’option “-v” du programme “harvestCorpus.pl”. \n"; print " Par défaut, ce fichier s’appelle “logRequete.txt” et se trouve dans le répetoire des \n"; print " fichiers déchargés depuis le serveur ISTEX. \n"; print " -c indique le nom du fichier “corpus” généré par le programme “harvestCorpus.pl” permettant \n"; print " de faire le lien entre l’identifiant ISTEX d’un document et le nom des fichiers \n"; print " correspondants extraits.\n"; print " -p indique le préfixe utilisé au début du nom des fichiers déchargés par le programme \n"; print " “harvestCorpus.pl”. \n"; print " -r indique le répertoire où se trouve les fichiers XML éditeurs déchargés par le programme \n"; print " “harvestCorpus.pl”.\n"; print "\nExemples : \n"; print " $programme -l Arthropodes/logRequete.txt -c Arthropodes_v2b.corpus -r Arthropodes\n"; print " $programme -l Vieillissement/logRequete.txt -p Vieil_ -r Vieillissement\n\n"; exit 0; } usage(2) if not $logfile; my $format = ""; my $json = ""; my $racine = ""; my $num = 0; my $total = 0; my %racine = (); if ( $corpus ) { open(TAB, "<:utf8", $corpus) or die "$!,"; while(<TAB>) { if ( /^id /o ) { chomp; s/\r//go; # Au cas où ... my ($type, $id, $com, $nom) = split(/\s+/); $racine{$id} = $nom; } } close TAB; } open(INP, "<:raw", $logfile) or die "$!,"; while(<INP>) { if ( /^# (\w+_\d+)/ ) { $racine = $1; next; } elsif ( /^#/ ) { next; } $json .= $_; if ( /^\}/o ) { my $perl = decode_json $json; my %top = %{$perl}; $total = $top{'total'}; if ( $total > 0 ) { $format = sprintf("%%0%dd", length($total) + 1); my @hits = @{$top{'hits'}}; foreach my $hit (@hits) { $num ++; my $fichier = undef; my $xml = undef; my %hit = %{$hit}; my $id = $hit{'id'}; if ( $racine ) { $fichier = $racine; $racine = ""; } elsif ( $racine{$id} ) { $fichier = $racine{$id}; } elsif ( $prefixe ) { $fichier = "$prefixe" . sprintf($format, $num); } if ( $repertoire and defined $fichier ) { $xml = "$repertoire/$fichier.xml"; } else { $fichier = "Nom de fichier inconnu"; } my $titre = $hit{'title'}; my $nom = $hit{'corpusName'}; my $date = ""; if ( $hit{'copyrightDate'} ) { $date = $hit{'copyrightDate'}; } elsif ( $hit{'publicationDate'} ) { $date = $hit{'publicationDate'}; } my $langues = ""; if ( defined $hit{'language'} ) { $langues = join(" ; ", @{$hit{'language'}}); } my $wos = ""; my $scienceMetrix = ""; my $inist = ""; if ( defined $hit{'categories'} ) { my %categories = %{$hit{'categories'}}; if ( defined $categories{'wos'} ) { $wos = join(" ; ", @{$categories{'wos'}}); } if ( defined $categories{'scienceMetrix'} ) { $scienceMetrix = join(" ; ", @{$categories{'scienceMetrix'}}); } if ( defined $categories{'inist'} ) { $inist = join(" ; ", @{$categories{'inist'}}); } } my $genre = ""; if ( defined $hit{'genre'} ) { $genre = join(", ", @{$hit{'genre'}}); } my $pdfVersion = ""; my $score = ""; if ( defined $hit{'qualityIndicators'} ) { my %indicateurs = %{$hit{'qualityIndicators'}}; if ( defined $indicateurs{'pdfVersion'} ) { $pdfVersion = $indicateurs{'pdfVersion'}; } if ( defined $indicateurs{'score'} ) { $score = $indicateurs{'score'}; } } my $revue = ""; my $issn = ""; my $eissn = ""; if ( defined $hit{'host'} ) { my %host = %{$hit{'host'}}; if ( defined $host{'title'} ) { $revue = $host{'title'} } if ( defined $host{'issn'} ) { $issn = join("/", @{$host{'issn'}}); } if ( defined $host{'eissn'} ) { $eissn = join("/", @{$host{'eissn'}}); } if ( defined $host{'genre'} ) { my $tmp = join(", ", @{$host{'genre'}}); if ( $genre ) { $genre = "$tmp ; $genre"; } else { $genre = $tmp; } } } my $structure = "Inconnu"; if ( defined $xml and -f $xml ) { $structure = "Non"; my $texte = ""; open(XML, "<:utf8", $xml) or die "$!,"; while(<XML>) { tr/\n\r/ /s; $texte .= $_; } close XML; # Wiley if ( $texte =~ m|xmlns="http://www.wiley.com/namespaces/wiley"|o ) { $structure = "Oui" if $texte =~ /<body( .+?)?>\s*<section\b/oi; } # Elsevier elsif ( $texte =~ m#xmlns:(ce|sb)="http://www.elsevier.com/xml/common/.*dtd"#oi ) { $structure = "Oui" if $texte =~ m|<body>\s*<ce:sections>\s*<ce:section>|oi; } # Brill elsif ( $texte =~ m|http://dtd.nlm.nih.gov/publishing|o or $texte =~ m|<publisher-name>Brill</publisher-name>|o ) { $structure = "Oui" if $texte =~ m|<body>\s*<sec\b|o; } # OUP elsif ( $texte =~ m|<publisher-name>Oxford University Press</publisher-name>|o ) { $structure = "Oui" if $texte =~ m|<body>\s*<sec\b|o; } # Emerald elsif ( $texte =~ m|<publisher-name>Emerald Group Publishing Limited</publisher-name>|o or $texte =~ m|<publisher-name>MCB UP Ltd</publisher-name>|o ) { $structure = "Oui" if $texte =~ m|<body>\s*<sec\b|o; } # IOP elsif ( $texte =~ m|http://ej.iop.org/dtd|o ) { $structure = "Oui" if $texte =~ m|<body( .+?)?>\s*<sec-level1\b|o; } # BMJ elsif ( $texte =~ /<publisher-name>(BMJ|British Medical Journal) Publishing Group/o ) { $structure = "Oui" if $texte =~ m|<body>\s*<sec\b|o; } # Nature elsif ( $texte =~ m|"-//NPG//DTD XML Article//EN"|o ) { $structure = "Oui" if $texte =~ m|<bdy>\s*<p>|o; } } print "$id\t$fichier\t$nom\t$score\t$pdfVersion\t$structure\t$date\t$titre"; print "\t$revue\t$issn\t$eissn\t$genre\t$wos\t$scienceMetrix\t$inist\n"; } } else { ; } $json = ""; } } exit 0; sub usage { print STDERR "Usage : $programme -l logfile ( -p préfixe | -c corpus ) [ -r répertoire ] \n"; print STDERR " $programme -h \n"; exit shift; }