Bug 21549: Lock expired patron accounts
[koha.git] / misc / cronjobs / cloud-kw.pl
1 #!/usr/bin/perl
2
3 #
4 # Copyright 2008 Tamil s.a.r.l.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23 use diagnostics;
24 use Carp;
25 use YAML::XS;
26 use Pod::Usage;
27 use Getopt::Long;
28
29 use Koha::Script -cron;
30 use C4::Context;
31 use C4::Log;
32
33 my $verbose     = 0;
34 my $help        = 0;
35 my $conf        = '';
36 GetOptions( 
37     'verbose'   => \$verbose,
38     'help'      => \$help,
39     'conf=s'    => \$conf,
40 );
41
42 sub usage {
43     pod2usage( -verbose => 2 );
44     exit;
45
46
47 usage() if $help || !$conf;
48
49 cronlogaction();
50
51 my @clouds;
52 print "Reading configuration file: $conf\n" if $verbose;
53 eval {
54     @clouds = YAML::XS::LoadFile( $conf );
55 };
56 croak "Unable to read configuration file: $conf\n" if $@;
57
58 for my $cloud ( @clouds ) {
59     print "Create a cloud\n",
60           "  Koha conf file:  ", $cloud->{KohaConf} ? $cloud->{KohaConf} : "default", "\n",
61           "  Zebra Index:     ", $cloud->{ZebraIndex}, "\n",
62           "  Koha Keyword:    ", $cloud->{KohaIndex}, "\n",
63           "  Count:           ", $cloud->{Count}, "\n",
64           "  Withcss:         ", $cloud->{Withcss}, "\n",
65           "  Output:          ", $cloud->{Output}, "\n",
66       if $verbose;  
67
68     # Set Koha context if KohaConf is present
69     my $set_new_context = 0;
70     if ( $cloud->{KohaConf} ) {
71         if ( -e $cloud->{KohaConf} ) {
72             my $context = C4::Context->new( $cloud->{KohaConf} );
73             $context->set_context();
74             $set_new_context = 1;
75         }
76         else {
77             carp "Koha conf file doesn't exist: ", $cloud->{KohaConf}, " ; use KOHA_CONF\n";
78         }
79     }
80
81     my $index = ZebraIndex->new( $cloud->{ZebraIndex} );
82     $index->scan( $cloud->{Count} );
83
84     open my $fh, ">", $cloud->{Output}
85         or croak "Unable to create file ", $cloud->{Output};
86
87     my $withcss = $cloud->{Withcss} =~ /^yes/i;
88     print $fh $index->html_cloud( $cloud->{KohaIndex}, $withcss );
89     close $fh;
90     $set_new_context && restore_context C4::Context;
91 }
92
93
94
95 package ZebraIndex;
96
97 use strict;
98 use warnings;
99 use diagnostics;
100 use Carp;
101
102 sub new {
103     my $self = {};
104     my $class = shift;
105     $self->{ zebra_index  } = shift;
106     $self->{ top_terms    } = undef;
107     $self->{ levels_cloud } = 24;
108     bless $self, $class;
109
110     # Test Zebra index
111     my $zbiblio = C4::Context->Zconn( "biblioserver" );
112     eval {
113         my $ss = $zbiblio->scan_pqf(
114             '@attr 1=' . $self->{ zebra_index } . ' @attr 4=1 @attr 6=3 "a"'
115         );
116     };
117     croak "Invalid Zebra index: ", $self->{ zebra_index } if $@;
118
119     return $self;
120 }
121
122
123 #
124 # scan
125 #   Scan zebra index and populate an array of top terms
126 #
127 # PARAMETERS:
128 #   $max_terms    Max number of top terms
129 #
130 # RETURN:
131 #   A 4-dimensionnal array in $self->{top_terms}
132 #   [0] term
133 #   [1] term number of occurrences
134 #   [2] term proportional relative weight in terms set E[0-1]
135 #   [3] term logarithmic relative weight E [0-levels_cloud]
136 #   
137 #   This array is sorted alphabetically by terms ([0])
138 #   It can be easily sorted by occurrences:
139 #     @t = sort { $a[1] <=> $a[1] } @{$self->{top_terms}};
140 #
141 sub scan {
142     my $self       = shift;
143     my $index_name = $self->{ zebra_index };
144     my $max_terms  = shift;
145     
146     my $MAX_OCCURENCE = 1000000000;
147     
148     my $zbiblio = C4::Context->Zconn( "biblioserver" );
149     my $number_of_terms = 0; 
150     my @terms;      # 2 dimensions array
151     my $min_occurence_index = -1;
152     my $min_occurence;
153     my $from = '0';
154
155     while (1) {
156         my $ss;
157         eval {
158             print "$from\n" if $verbose;
159             $from =~ s/\"/\\\"/g;
160             my $query = '@attr 1=' . $index_name . ' @attr 4=1 @attr 6=3 "'
161                         . $from . 'a"';
162             $ss = $zbiblio->scan_pqf( $query );
163         };
164         if ($@) {
165             chop $from;
166             next;
167         }
168         $ss->option( rpnCharset => 'UTF-8' );
169         last if $ss->size() == 0;
170         my $term = '';
171         my $occ = 0;
172         for my $index ( 0..$ss->size()-1 ) {
173             ($term, $occ) = $ss->display_term($index);
174             #print "$term:$occ\n";
175             if ( $number_of_terms < $max_terms ) {
176                 push( @terms, [ $term, $occ ] ); 
177                 ++$number_of_terms;
178                 if ( $number_of_terms == $max_terms ) {
179                     $min_occurence = $MAX_OCCURENCE;
180                     for (0..$number_of_terms-1) {
181                         my @term = @{ $terms[$_] };
182                         if ( $term[1] <= $min_occurence ) {
183                             $min_occurence       = $term[1];
184                             $min_occurence_index = $_;
185                         }
186                     }
187                 }
188             }
189             else {
190                 if ( $occ > $min_occurence) {
191                     @{ $terms[$min_occurence_index] }[0] = $term;
192                     @{ $terms[$min_occurence_index] }[1] = $occ;
193                     $min_occurence = $MAX_OCCURENCE;
194                     for (0..$max_terms-1) {
195                         my @term = @{ $terms[$_] };
196                         if ( $term[1] <= $min_occurence ) {
197                             $min_occurence       = $term[1];
198                             $min_occurence_index = $_;
199                         }
200                     }
201                 }
202             }
203         }
204         $from = $term;
205     }
206
207     # Sort array of array by terms weight
208     @terms = sort { @{$a}[1] <=> @{$b}[1] } @terms;
209
210     # A relatif weight to other set terms is added to each term
211     my $min     = $terms[0][1];
212     my $log_min = log( $min );
213     my $max     = $terms[$#terms][1];
214     my $log_max = log( $max );
215     my $delta   = $max - $min;
216     $delta = 1 if $delta == 0; # Very unlikely
217     my $factor;
218     if ($log_max - $log_min == 0) {
219         $log_min = $log_min - $self->{levels_cloud};
220         $factor = 1;
221     } 
222     else {
223         $factor = $self->{levels_cloud} / ($log_max - $log_min);
224     }
225
226     foreach (0..$#terms) {
227         my $count = @{ $terms[$_] }[1];
228         my $weight = ( $count - $min ) / $delta;
229         my $log_weight = int( (log($count) - $log_min) * $factor);
230         push( @{ $terms[$_] }, $weight );
231         push( @{ $terms[$_] }, $log_weight );
232     }
233     $self->{ top_terms } = \@terms;
234
235     # Sort array of array by terms alphabetical order
236     @terms = sort { @{$a}[0] cmp @{$b}[0] } @terms;
237 }
238
239
240 #
241 # Returns a HTML version of index top terms formatted
242 # as a 'tag cloud'.
243 #
244 sub html_cloud {
245     my $self = shift;
246     my $koha_index = shift;
247     my $withcss = shift;
248     my @terms = @{ $self->{top_terms} };
249     my $html = '';
250     if ( $withcss ) {
251         $html = <<EOS;
252 <style>
253 .subjectcloud {
254     text-align:  center; 
255     line-height: 16px; 
256     margin: 20px;
257     background: #f0f0f0;
258     padding: 3%;
259 }
260 .subjectcloud a {
261     font-weight: lighter;
262     text-decoration: none;
263 }
264 span.tagcloud0  { font-size: 12px;}
265 span.tagcloud1  { font-size: 13px;}
266 span.tagcloud2  { font-size: 14px;}
267 span.tagcloud3  { font-size: 15px;}
268 span.tagcloud4  { font-size: 16px;}
269 span.tagcloud5  { font-size: 17px;}
270 span.tagcloud6  { font-size: 18px;}
271 span.tagcloud7  { font-size: 19px;}
272 span.tagcloud8  { font-size: 20px;}
273 span.tagcloud9  { font-size: 21px;}
274 span.tagcloud10 { font-size: 22px;}
275 span.tagcloud11 { font-size: 23px;}
276 span.tagcloud12 { font-size: 24px;}
277 span.tagcloud13 { font-size: 25px;}
278 span.tagcloud14 { font-size: 26px;}
279 span.tagcloud15 { font-size: 27px;}
280 span.tagcloud16 { font-size: 28px;}
281 span.tagcloud17 { font-size: 29px;}
282 span.tagcloud18 { font-size: 30px;}
283 span.tagcloud19 { font-size: 31px;}
284 span.tagcloud20 { font-size: 32px;}
285 span.tagcloud21 { font-size: 33px;}
286 span.tagcloud22 { font-size: 34px;}
287 span.tagcloud23 { font-size: 35px;}
288 span.tagcloud24 { font-size: 36px;}
289 </style>
290 <div class="subjectcloud">
291 EOS
292     }
293     for (0..$#terms) {
294         my @term = @{ $terms[$_] };
295         my $uri = $term[0];
296         $uri =~ s/\(//g;
297         #print "  0=", $term[0]," - 1=", $term[1], " - 2=", $term[2], " - 3=", $term[3],"\n";
298         $html = $html
299             . '<span class="tagcloud'
300             . $term[3]
301             . '">'
302             . '<a href="/cgi-bin/koha/opac-search.pl?q='
303             . $koha_index
304             . '%3A'
305             . $uri
306             . '">'
307             . $term[0]
308             . "</a></span>\n";
309     }
310     $html .= "</div>\n";
311     return $html;
312 }
313
314
315 =head1 NAME
316
317 cloud-kw.pl - Creates HTML keywords clouds from Koha Zebra Indexes
318
319 =head1 USAGE
320
321 =over
322
323 =item cloud-kw.pl [--verbose|--help] --conf=F<cloud.conf> 
324
325 Creates multiple HTML files containing kewords cloud with top terms sorted
326 by their logarithmic weight.
327 F<cloud.conf> is a YAML configuration file driving cloud generation
328 process.
329
330 =back
331
332 =head1 PARAMETERS
333
334 =over
335
336 =item B<--conf=configuration file>
337
338 Specify configuration file name
339
340 =item B<--verbose|-v>
341
342 Enable script verbose mode. 
343
344 =item B<--help|-h>
345
346 Print this help page.
347
348 =back
349
350 =head1 CONFIGURATION
351
352 Configuration file looks like that:
353
354  --- 
355   # Koha configuration file for a specific installation
356   # If not present, defaults to KOHA_CONF
357   KohaConf: /home/koha/mylibray/etc/koha-conf.xml
358   # Zebra index to scan
359   ZebraIndex: Author
360   # Koha index used to link found kewords with an opac search URL
361   KohaIndex: au
362   # Number of top keyword to use for the cloud
363   Count: 50
364   # Include CSS style directives with the cloud
365   # This could be used as a model and then CSS directives are
366   # put in the appropriate CSS file directly.
367   Withcss: Yes
368   # HTML file where to output the cloud
369   Output: /home/koha/mylibrary/koharoot/koha-tmpl/cloud-author.html
370  --- 
371   KohaConf: /home/koha/yourlibray/etc/koha-conf.xml
372   ZebraIndex: Subject
373   KohaIndex: su
374   Count: 200
375   Withcss: no
376   Output: /home/koha/yourlibrary/koharoot/koha-tmpl/cloud-subject.html
377
378 =head1 IMPROVEMENTS
379
380 Generated top terms have more informations than those outputted from
381 the time being. Some parameters could be easily added to improve
382 this script:
383
384 =over
385
386 =item B<WithCount>
387
388 In order to output terms with the number of occurrences they
389 have been found in Koha Catalogue by Zebra.
390
391 =item B<CloudLevels>
392
393 Number of levels in the cloud. Now 24 levels are hardcoded.
394
395 =item B<Weithing>
396
397 Weighting method used to distribute terms in the cloud. We could have two
398 values: Logarithmic and Linear. Now it's Logarithmic by default.
399
400 =item B<Order>
401
402 Now terms are outputted in the lexical order. They could be sorted
403 by their weight.
404
405 =back
406
407 =cut
408
409