| blib/lib/Search/Circa/Annuaire.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 12 | 151 | 7.9 |
| branch | 0 | 76 | 0.0 |
| condition | 0 | 38 | 0.0 |
| subroutine | 4 | 11 | 36.3 |
| pod | 6 | 7 | 85.7 |
| total | 22 | 283 | 7.7 |
| line | stmt | bran | cond | sub | pod | time | code | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | package Search::Circa::Annuaire; | |||||||||||||
| 2 | ||||||||||||||
| 3 | # module Search::Circa::Annuaire : See Search::Circa | |||||||||||||
| 4 | # Copyright 2000 A.Barbet alian@alianwebserver.com. All rights reserved. | |||||||||||||
| 5 | ||||||||||||||
| 6 | # $Log: Annuaire.pm,v $ | |||||||||||||
| 7 | # Revision 1.7 2002/08/19 10:17:13 alian | |||||||||||||
| 8 | # Correct bug in previous version: @l = ... || return undef | |||||||||||||
| 9 | # | |||||||||||||
| 10 | # Revision 1.6 2002/08/17 18:19:02 alian | |||||||||||||
| 11 | # - Minor changes to all code suite to tests | |||||||||||||
| 12 | # | |||||||||||||
| 13 | # Revision 1.5 2001/10/28 16:28:46 alian | |||||||||||||
| 14 | # - Add some debug info on level 3 | |||||||||||||
| 15 | # | |||||||||||||
| 16 | # Revision 1.4 2001/10/28 12:23:23 alian | |||||||||||||
| 17 | # - Correction d'un warning sur l'affichage du titre de la categorie racine | |||||||||||||
| 18 | # | |||||||||||||
| 19 | # Revision 1.3 2001/08/26 23:12:10 alian | |||||||||||||
| 20 | # - Add POD documentation | |||||||||||||
| 21 | # - Add CreateDirectory method | |||||||||||||
| 22 | ||||||||||||||
| 23 | 2 | 2 | 66369 | use strict; | ||||||||||
| 2 | 6 | |||||||||||||
| 2 | 84 | |||||||||||||
| 24 | 2 | 2 | 5865 | use DBI; | ||||||||||
| 2 | 92807 | |||||||||||||
| 2 | 295 | |||||||||||||
| 25 | 2 | 2 | 2447 | use Search::Circa; | ||||||||||
| 2 | 10 | |||||||||||||
| 2 | 214 | |||||||||||||
| 26 | 2 | 2 | 15 | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); | ||||||||||
| 2 | 5 | |||||||||||||
| 2 | 7260 | |||||||||||||
| 27 | require Exporter; | |||||||||||||
| 28 | ||||||||||||||
| 29 | @ISA = qw(Exporter Search::Circa); | |||||||||||||
| 30 | @EXPORT = qw(); | |||||||||||||
| 31 | $VERSION = ('$Revision: 1.7 $ ' =~ /(\d+\.\d+)/)[0]; | |||||||||||||
| 32 | ||||||||||||||
| 33 | # Default display of item link | |||||||||||||
| 34 | $Circa::Annuaire::Ts = | |||||||||||||
| 35 | '" |
|||||||||||||
| 36 | $description |
|||||||||||||
| 37 | Url: $url | |||||||||||||
| 38 | Last update: $last_update | |||||||||||||
| 39 | \n"'; | |||||||||||||
| 40 | ||||||||||||||
| 41 | # default display of category link | |||||||||||||
| 42 | $Circa::Annuaire::Tc='"\n"'; | |||||||||||||
| 43 | ||||||||||||||
| 44 | ||||||||||||||
| 45 | #------------------------------------------------------------------------------ | |||||||||||||
| 46 | # new | |||||||||||||
| 47 | #------------------------------------------------------------------------------ | |||||||||||||
| 48 | sub new | |||||||||||||
| 49 | { | |||||||||||||
| 50 | 0 | 0 | 0 | my $class = shift; | ||||||||||
| 51 | 0 | my $self = $class->SUPER::new; | ||||||||||||
| 52 | 0 | bless $self, $class; | ||||||||||||
| 53 | 0 | $self->{nbResultPerPage} = 10; | ||||||||||||
| 54 | 0 | 0 | $self->{SCRIPT_NAME} = $ENV{'SCRIPT_NAME'} || "search.cgi"; | |||||||||||
| 55 | 0 | return $self; | ||||||||||||
| 56 | } | |||||||||||||
| 57 | ||||||||||||||
| 58 | #------------------------------------------------------------------------------ | |||||||||||||
| 59 | # create_annuaire | |||||||||||||
| 60 | #------------------------------------------------------------------------------ | |||||||||||||
| 61 | sub create_annuaire | |||||||||||||
| 62 | { | |||||||||||||
| 63 | 0 | 0 | 1 | my ($self, $idr, $masque, $path)=@_; | ||||||||||
| 64 | # On previent que on genere des pages statiques | |||||||||||||
| 65 | 0 | $self->{_create} = 1; | ||||||||||||
| 66 | # racine des fichiers crees | |||||||||||||
| 67 | 0 | my $orig = $path."/".$idr.'/'; | ||||||||||||
| 68 | # On charge la liste des categories | |||||||||||||
| 69 | 0 | my $rt = $self->categorie->loadAll($idr); | ||||||||||||
| 70 | # Pour chaque categorie, on cree la page correspondante | |||||||||||||
| 71 | 0 | foreach my $categorie (keys %$rt) | ||||||||||||
| 72 | { | |||||||||||||
| 73 | 0 | my $f; | ||||||||||||
| 74 | 0 | 0 | if ($categorie != 0) | |||||||||||
| 75 | { | |||||||||||||
| 76 | 0 | my $parent = $categorie; | ||||||||||||
| 77 | 0 | while ($$rt{$parent}[1]) | ||||||||||||
| 78 | { | |||||||||||||
| 79 | 0 | 0 | if ($f) { $f = $$rt{$parent}[1]."/".$f; } | |||||||||||
| 0 | ||||||||||||||
| 0 | ||||||||||||||
| 80 | else {$f = $$rt{$parent}[1]; } | |||||||||||||
| 81 | 0 | $parent = $$rt{$parent}[1]; | ||||||||||||
| 82 | } | |||||||||||||
| 83 | # Creation du repertoire (recursif) | |||||||||||||
| 84 | 0 | 0 | if ($f) { $f = $orig.$f; } | |||||||||||
| 0 | ||||||||||||||
| 85 | 0 | else { $f=$orig; } | ||||||||||||
| 86 | 0 | my @l = split(/\//, $f); | ||||||||||||
| 87 | 0 | push(@l,$categorie); | ||||||||||||
| 88 | 0 | $self->CreateDirectory(@l); | ||||||||||||
| 89 | ||||||||||||||
| 90 | # Creation du fichier | |||||||||||||
| 91 | 0 | $f .= '/'.$categorie.'/index.html'; | ||||||||||||
| 92 | } | |||||||||||||
| 93 | 0 | else { $f= $orig.'/index.html'; } | ||||||||||||
| 94 | 0 | print "Create $f ($$rt{$categorie}[0])\n"; | ||||||||||||
| 95 | 0 | 0 | open(FILE, ">$f") || die "Can't create $f:$!\n"; | |||||||||||
| 96 | 0 | print FILE $self->GetContentOf($masque, $idr, $categorie); | ||||||||||||
| 97 | 0 | close(FILE); | ||||||||||||
| 98 | } | |||||||||||||
| 99 | 0 | $self->{_create} = 0; | ||||||||||||
| 100 | } | |||||||||||||
| 101 | ||||||||||||||
| 102 | #------------------------------------------------------------------------------ | |||||||||||||
| 103 | # CreateDirectory | |||||||||||||
| 104 | #------------------------------------------------------------------------------ | |||||||||||||
| 105 | sub CreateDirectory { | |||||||||||||
| 106 | 0 | 0 | 1 | my ($self, @l) = @_; | ||||||||||
| 107 | 0 | my $o; | ||||||||||||
| 108 | 0 | foreach (@l) { | ||||||||||||
| 109 | 0 | 0 | if ($_) { | |||||||||||
| 110 | 0 | $o.='/'.$_; | ||||||||||||
| 111 | 0 | 0 | if (!-e $o) { | |||||||||||
| 112 | 0 | print "Creation de $o\n"; | ||||||||||||
| 113 | 0 | mkdir($o, 0755); | ||||||||||||
| 114 | } | |||||||||||||
| 115 | } | |||||||||||||
| 116 | } | |||||||||||||
| 117 | } | |||||||||||||
| 118 | ||||||||||||||
| 119 | ||||||||||||||
| 120 | #------------------------------------------------------------------------------ | |||||||||||||
| 121 | # GetContentOf | |||||||||||||
| 122 | #------------------------------------------------------------------------------ | |||||||||||||
| 123 | sub GetContentOf | |||||||||||||
| 124 | { | |||||||||||||
| 125 | 0 | 0 | 1 | my ($self, $masqueOrig, $id, $categorie,$templateS,$templateC,$first)=@_; | ||||||||||
| 126 | 0 | 0 | if (!$categorie) {$categorie=0;} | |||||||||||
| 0 | ||||||||||||||
| 127 | 0 | 0 | if (!$id) {$id=1;} | |||||||||||
| 0 | ||||||||||||||
| 128 | 0 | 0 | if (!$templateC) { $templateC = $Circa::Annuaire::Tc; } | |||||||||||
| 0 | ||||||||||||||
| 129 | 0 | 0 | if (!$templateS) { $templateS = $Circa::Annuaire::Ts; } | |||||||||||
| 0 | ||||||||||||||
| 130 | 0 | $self->trace(3,"Search::Circa::Search->GetContentOf $id $categorie"); | ||||||||||||
| 131 | 0 | 0 | my ($masque) = $self->categorie->get_masque($id,$categorie) || $masqueOrig; | |||||||||||
| 132 | 0 | 0 | 0 | $masque = $masqueOrig if ((!$masque || !-r $masque) && $masqueOrig); | ||||||||||
| 0 | ||||||||||||||
| 133 | 0 | 0 | 0 | $masque = $CircaConf::TemplateDir."/circa.htm" | ||||||||||
| 134 | if (!$masque or !-r $masque); | |||||||||||||
| 135 | 0 | my @catess = $self->GetCategoriesOf($categorie,$id); | ||||||||||||
| 136 | 0 | 0 | return undef if (!@catess); | |||||||||||
| 137 | 0 | my $titre = shift @catess; | ||||||||||||
| 138 | 0 | 0 | if (!$titre) { $titre='';} | |||||||||||
| 0 | ||||||||||||||
| 139 | 0 | $self->trace(3,"Search::Circa::Search->GetContentOf $categorie => $titre"); | ||||||||||||
| 140 | 0 | my ($sites,$liens) | ||||||||||||
| 141 | = $self->GetSitesOf($categorie,$id,$templateS,$first); | |||||||||||||
| 142 | 0 | 0 | 0 | return undef if (!$sites and !$liens); | ||||||||||
| 143 | # Substitution dans le template | |||||||||||||
| 144 | 0 | my ($c1,$c2); | ||||||||||||
| 145 | 0 | 0 | if (@catess) { | |||||||||||
| 146 | 0 | 0 | $c1 = join(' ',@catess[0..$#catess/2]) || ' '; | |||||||||||
| 147 | 0 | 0 | $c2 = join(' ',@catess[($#catess/2)+1..$#catess]) || ' '; | |||||||||||
| 148 | } | |||||||||||||
| 149 | 0 | else { ($c1,$c2)=(' ',' '); } | ||||||||||||
| 150 | ||||||||||||||
| 151 | 0 | 0 | my %vars = | |||||||||||
| 0 | ||||||||||||||
| 0 | ||||||||||||||
| 0 | ||||||||||||||
| 152 | ('resultat' => $sites || ' ', | |||||||||||||
| 153 | 'categories1' => $c1, | |||||||||||||
| 154 | 'categories2' => $c2, | |||||||||||||
| 155 | 'titre' => 'Annuaire' |
|||||||||||||
| 156 | .' '.($titre || ' ').' ', |
|||||||||||||
| 157 | 'listeLiensSuivPrec'=> $liens || ' ', | |||||||||||||
| 158 | 'words' => ' ', | |||||||||||||
| 159 | 'categorie' => $categorie || 0, | |||||||||||||
| 160 | 'id' => $id, | |||||||||||||
| 161 | 'nb' => 0); | |||||||||||||
| 162 | # Affichage du resultat | |||||||||||||
| 163 | 0 | return $self->fill_template($masque,\%vars); | ||||||||||||
| 164 | } | |||||||||||||
| 165 | ||||||||||||||
| 166 | ||||||||||||||
| 167 | #------------------------------------------------------------------------------ | |||||||||||||
| 168 | # GetCategoriesOf | |||||||||||||
| 169 | #------------------------------------------------------------------------------ | |||||||||||||
| 170 | sub GetCategoriesOf { | |||||||||||||
| 171 | 0 | 0 | 1 | my ($self,$id,$idr,$template)=@_; | ||||||||||
| 172 | 0 | $self->trace(3,"Search::Circa::Search->GetCategoriesOf $id $idr"); | ||||||||||||
| 173 | 0 | 0 | $idr=1 if !$idr; | |||||||||||
| 174 | 0 | 0 | $id=0 if !$id; | |||||||||||
| 175 | 0 | 0 | $template = $Circa::Annuaire::Tc if !$template; | |||||||||||
| 176 | 0 | my (@buf,%tab,$titre); | ||||||||||||
| 177 | # On charge toutes les categories | |||||||||||||
| 178 | 0 | my $ref = $self->categorie->loadAll($idr);; | ||||||||||||
| 179 | 0 | 0 | if (ref($ref)) { %tab = %$ref;} | |||||||||||
| 0 | ||||||||||||||
| 180 | else { | |||||||||||||
| 181 | 0 | $self->trace(1,"Search::Circa::Search->GetCategoriesOf after". | ||||||||||||
| 182 | " loadAll $idr"); | |||||||||||||
| 183 | 0 | return undef; | ||||||||||||
| 184 | } | |||||||||||||
| 185 | 0 | foreach my $key (keys %tab) | ||||||||||||
| 186 | { | |||||||||||||
| 187 | 0 | my $nom_complet; | ||||||||||||
| 188 | 0 | my ($nom,$parent)=($tab{$key}[0],$tab{$key}[1]); | ||||||||||||
| 189 | 0 | $nom_complet=$self->categorie->getParent($key,%tab); | ||||||||||||
| 190 | 0 | my $links; | ||||||||||||
| 191 | # Le lien des categorie est != dans le cas de la generation | |||||||||||||
| 192 | # et quand c'est fait a la volee | |||||||||||||
| 193 | 0 | 0 | if ($self->{_create}) { $links = $key; } | |||||||||||
| 0 | ||||||||||||||
| 194 | 0 | else { $links = $self->get_link_categorie($key, $idr, 0);} | ||||||||||||
| 195 | 0 | 0 | 0 | if ( ($parent==$id) and ($key != 0)) | ||||||||||
| 0 | ||||||||||||||
| 196 | {push(@buf,eval $template);} | |||||||||||||
| 197 | } | |||||||||||||
| 198 | 0 | 0 | if ($#buf==-1) {$buf[0]=" Plus de catégorie ";} |
|||||||||||
| 0 | ||||||||||||||
| 199 | 0 | my $nom_complet=$self->categorie->getParent($tab{$id}[1],%tab); | ||||||||||||
| 200 | 0 | 0 | if ($self->{_create}) | |||||||||||
| 201 | 0 | { $titre = "$nom_complet"; } | ||||||||||||
| 202 | else | |||||||||||||
| 203 | { | |||||||||||||
| 204 | 0 | $titre = " | ||||||||||||
| 205 | .$self->get_link_categorie($tab{$id}[1], $idr, 0) | |||||||||||||
| 206 | ."\">$nom_complet"; | |||||||||||||
| 207 | } | |||||||||||||
| 208 | 0 | unshift(@buf,$titre); | ||||||||||||
| 209 | 0 | $self->trace(3,"Search::Circa::Search->GetCategoriesOf End: $id=> $buf[0]"); | ||||||||||||
| 210 | 0 | return @buf; | ||||||||||||
| 211 | } | |||||||||||||
| 212 | ||||||||||||||
| 213 | #------------------------------------------------------------------------------ | |||||||||||||
| 214 | # GetSitesOf | |||||||||||||
| 215 | #------------------------------------------------------------------------------ | |||||||||||||
| 216 | sub GetSitesOf | |||||||||||||
| 217 | { | |||||||||||||
| 218 | 0 | 0 | 1 | my ($self,$id,$idr,$template,$first)=@_; | ||||||||||
| 219 | 0 | $self->trace(3,"Search::Circa::Search->GetSitesOf $id $idr"); | ||||||||||||
| 220 | 0 | 0 | if (!$idr) {$idr=1;} | |||||||||||
| 0 | ||||||||||||||
| 221 | 0 | 0 | if (!$id) {$id=0;} | |||||||||||
| 0 | ||||||||||||||
| 222 | 0 | 0 | if (!$template) {$template=$Circa::Annuaire::Ts;} | |||||||||||
| 0 | ||||||||||||||
| 223 | 0 | my ($buf,$buf_l); | ||||||||||||
| 224 | 0 | my $requete = " | ||||||||||||
| 225 | select url,titre,description,langue,last_update | |||||||||||||
| 226 | from ".$self->{PREFIX_TABLE}.$idr."links | |||||||||||||
| 227 | where categorie=$id and browse_categorie='1' and parse='1'"; | |||||||||||||
| 228 | 0 | my $sth = $self->{DBH}->prepare($requete); | ||||||||||||
| 229 | 0 | 0 | if (!$sth->execute()) { | |||||||||||
| 230 | 0 | $self->trace(1,"Search::Circa::Search-> GetSitesOf ". | ||||||||||||
| 231 | "Erreur $requete:$DBI::errstr\n"); | |||||||||||||
| 232 | 0 | return undef; | ||||||||||||
| 233 | } | |||||||||||||
| 234 | 0 | my ($facteur,$indiceG)=(100,0); | ||||||||||||
| 235 | 0 | while (my ($url,$titre,$description,$langue,$last_update) | ||||||||||||
| 236 | = $sth->fetchrow_array) { | |||||||||||||
| 237 | 0 | $indiceG++; | ||||||||||||
| 238 | 0 | 0 | if ($last_update eq '0000-00-00 00:00:00') {$last_update='?';} | |||||||||||
| 0 | ||||||||||||||
| 239 | 0 | 0 | if (defined($first)) { | |||||||||||
| 240 | 0 | 0 | 0 | if ($indiceG>$first and ($indiceG<($first+$self->{nbResultPerPage}))){ | ||||||||||
| 241 | 0 | $buf.= eval $template; | ||||||||||||
| 242 | } | |||||||||||||
| 243 | 0 | 0 | if (!(($indiceG-1)%$self->{nbResultPerPage})) { | |||||||||||
| 244 | 0 | 0 | if (($indiceG-1)==$first) { | |||||||||||
| 245 | 0 | $buf_l.=((($indiceG-1)/$self->{nbResultPerPage})+1).' -';} | ||||||||||||
| 246 | else { | |||||||||||||
| 247 | 0 | $buf_l .= ' 248 | .$self->get_link_categorie($id,$idr,$indiceG-1).'">' | |||||||||||
| 249 | .((($indiceG-1)/$self->{nbResultPerPage})+1).'-';} | |||||||||||||
| 250 | } | |||||||||||||
| 251 | } | |||||||||||||
| 252 | 0 | else { $buf.= eval $template;} | ||||||||||||
| 253 | } | |||||||||||||
| 254 | 0 | 0 | 0 | if ($indiceG>$self->{nbResultPerPage} and defined($first)) | ||||||||||
| 0 | ||||||||||||||
| 255 | 0 | {chop($buf_l);$buf_l=' <'.$buf_l.'> ';} |
||||||||||||
| 256 | 0 | 0 | if (!$buf) | |||||||||||
| 0 | ||||||||||||||
| 257 | {$buf=" Pas de pages dans cette catégorie ";} |
|||||||||||||
| 258 | 0 | 0 | if (wantarray()) {return ($buf,$buf_l);} | |||||||||||
| 0 | ||||||||||||||
| 0 | ||||||||||||||
| 259 | else {return $buf;} | |||||||||||||
| 260 | } | |||||||||||||
| 261 | ||||||||||||||
| 262 | ||||||||||||||
| 263 | ||||||||||||||
| 264 | #------------------------------------------------------------------------------ | |||||||||||||
| 265 | # get_link_categorie | |||||||||||||
| 266 | #------------------------------------------------------------------------------ | |||||||||||||
| 267 | sub get_link_categorie | |||||||||||||
| 268 | { | |||||||||||||
| 269 | 0 | 0 | 1 | my ($self,$no_categorie,$id,$first) = @_; | ||||||||||
| 270 | 0 | 0 | if (!defined($first)) { $first = 0; } | |||||||||||
| 0 | ||||||||||||||
| 271 | 0 | 0 | if (defined($no_categorie)) | |||||||||||
| 0 | ||||||||||||||
| 272 | 0 | {return $self->{SCRIPT_NAME}."?categorie=$no_categorie&id=$id&first=$first";} | ||||||||||||
| 273 | else {return $self->{SCRIPT_NAME}."?id=$id&first=$first";} | |||||||||||||
| 274 | } | |||||||||||||
| 275 | ||||||||||||||
| 276 | ||||||||||||||
| 277 | #------------------------------------------------------------------------------ | |||||||||||||
| 278 | # POD DOCUMENTATION | |||||||||||||
| 279 | #------------------------------------------------------------------------------ | |||||||||||||
| 280 | ||||||||||||||
| 281 | =head1 NAME | |||||||||||||
| 282 | ||||||||||||||
| 283 | Circa::Annuaire - Create html pages for annuaire | |||||||||||||
| 284 | ||||||||||||||
| 285 | =head1 SYNOPSIS | |||||||||||||
| 286 | ||||||||||||||
| 287 | # Create Search::Circa::Annuaire object | |||||||||||||
| 288 | my $annuaire = new Search::Circa::Annuaire; | |||||||||||||
| 289 | ||||||||||||||
| 290 | # Connect appli | |||||||||||||
| 291 | if (!$annuaire->connect($user,$pass,$db,"localhost")) | |||||||||||||
| 292 | {die "Erreur à la connection MySQL:$DBI::errstr\n";} | |||||||||||||
| 293 | ||||||||||||||
| 294 | # Create all page in /tmp/annuaire directory for | |||||||||||||
| 295 | # account 1 with defaut file $masque | |||||||||||||
| 296 | $annuaire->create_annuaire(1, $masque, "/tmp/annuaire"); | |||||||||||||
| 297 | ||||||||||||||
| 298 | # Disconnect appli | |||||||||||||
| 299 | $annuaire->close; | |||||||||||||
| 300 | ||||||||||||||
| 301 | ||||||||||||||
| 302 | =head1 DESCRIPTION | |||||||||||||
| 303 | ||||||||||||||
| 304 | ||||||||||||||
| 305 | ||||||||||||||
| 306 | =head1 VERSION | |||||||||||||
| 307 | ||||||||||||||
| 308 | $Revision: 1.7 $ | |||||||||||||
| 309 | ||||||||||||||
| 310 | =head1 Public Class Interface | |||||||||||||
| 311 | ||||||||||||||
| 312 | =over | |||||||||||||
| 313 | ||||||||||||||
| 314 | =item create_annuaire | |||||||||||||
| 315 | ||||||||||||||
| 316 | =item GetContentOf | |||||||||||||
| 317 | ||||||||||||||
| 318 | =item GetCategoriesOf | |||||||||||||
| 319 | ||||||||||||||
| 320 | =item GetSitesOf | |||||||||||||
| 321 | ||||||||||||||
| 322 | =back | |||||||||||||
| 323 | ||||||||||||||
| 324 | =head1 Private Class Interface | |||||||||||||
| 325 | ||||||||||||||
| 326 | =over | |||||||||||||
| 327 | ||||||||||||||
| 328 | =item get_link_categorie | |||||||||||||
| 329 | ||||||||||||||
| 330 | =item CreateDirectory | |||||||||||||
| 331 | ||||||||||||||
| 332 | =back | |||||||||||||
| 333 | ||||||||||||||
| 334 | =head1 AUTHOR | |||||||||||||
| 335 | ||||||||||||||
| 336 | Alain BARBET alian@alianwebserver.com | |||||||||||||
| 337 | ||||||||||||||
| 338 | =cut |