3 # Library functions for the NewsStats package.
5 # Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
7 # This module can be redistributed and/or modified under the same terms under
8 # which Perl itself is published.
14 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
47 %EXPORT_TAGS = ( TimePeriods => [qw(GetTimePeriod LastMonth SplitPeriod
49 Output => [qw(OutputData FormatOutput)],
50 SQLHelper => [qw(SQLHierarchies SQLSortOrder SQLGroupList
51 SQLSetBounds SQLBuildClause GetMaxLength)]);
53 our $PackageVersion = '0.01';
61 #####-------------------------------- Vars --------------------------------#####
65 our $HomePath = dirname($0);
68 our $MyVersion = "$0 $::VERSION (NewsStats.pm $VERSION)";
70 #####------------------------------- Basics -------------------------------#####
72 ################################################################################
74 ################################################################################
76 ################################################################################
77 ### display version and exit
78 print "NewsStats v$PackageVersion\n$MyVersion\n";
79 print "Copyright (c) 2010-2012 Thomas Hochstein <thh\@inter.net>\n";
80 print "This program is free software; you may redistribute it ".
81 "and/or modify it under the same terms as Perl itself.\n";
84 ################################################################################
86 ################################################################################
88 ################################################################################
89 ### feed myself to perldoc and exit
90 exec('perldoc', $FullPath);
93 ################################################################################
95 ################################################################################
97 ################################################################################
98 ### read config via Config::Auto
99 ### IN : $ConfFile: config filename
100 ### OUT: reference to a hash containing the configuration
102 return Config::Auto::parse($ConfFile, format => 'equal');
104 ################################################################################
106 ################################################################################
108 ################################################################################
109 ### override configuration values
110 ### IN : $ConfigR : reference to configuration hash
111 ### $OverrideR: reference to a hash containing overrides
112 my ($ConfigR,$OverrideR) = @_;
113 my %Override = %$OverrideR;
115 &Bleat(1,"Empty configuration hash passed to OverrideConfig()")
116 if ( keys %$ConfigR < 1);
117 # return if no overrides
118 return if (keys %Override < 1 or keys %$ConfigR < 1);
119 foreach my $Key (keys %Override) {
120 $$ConfigR{$Key} = $Override{$Key};
123 ################################################################################
125 ################################################################################
127 ################################################################################
128 ### initialise database connection
129 ### IN : $ConfigR: reference to configuration hash
130 ### $Die : if TRUE, die if connection fails
132 my ($ConfigR,$Die) = @_;
133 my %Conf = %$ConfigR;
134 my $DBHandle = DBI->connect(sprintf('DBI:%s:database=%s;host=%s',
135 $Conf{'DBDriver'},$Conf{'DBDatabase'},
136 $Conf{'DBHost'}), $Conf{'DBUser'},
137 $Conf{'DBPw'}, { PrintError => 0 });
139 &Bleat(2,$DBI::errstr) if (defined($Die) and $Die);
140 &Bleat(1,$DBI::errstr);
144 ################################################################################
146 ################################################################################
148 ################################################################################
149 ### print warning or error messages and terminate in case of error
150 ### IN : $Level : 1 = warning, 2 = error
151 ### $Message: warning or error message
152 my ($Level,$Message) = @_;
154 warn "$0 W: $Message\n"
155 } elsif ($Level == 2) {
156 die "$0 E: $Message\n"
158 print "$0: $Message\n"
161 ################################################################################
163 #####------------------------------ GetStats ------------------------------#####
165 ################################################################################
167 ################################################################################
168 ### explode a (scalar) list of newsgroup names to a list of newsgroup and
169 ### hierarchy names where every newsgroup and hierarchy appears only once:
170 ### de.alt.test,de.alt.admin -> de.ALL, de.alt.ALL, de.alt.test, de.alt.admin
171 ### IN : $Newsgroups : a list of newsgroups (content of Newsgroups: header)
172 ### $TLH : top level hierarchy (all other newsgroups are ignored)
173 ### $ValidGroupsR: reference to a hash containing all valid newsgroups
175 ### OUT: %Newsgroups : hash containing all newsgroup and hierarchy names as keys
176 my ($Newsgroups,$TLH,$ValidGroupsR) = @_;
177 my %ValidGroups = %{$ValidGroupsR} if $ValidGroupsR;
180 # remove whitespace from contents of Newsgroups:
181 $Newsgroups =~ s/\s//;
182 # call &HierarchyCount for each newsgroup in $Newsgroups:
183 for (split /,/, $Newsgroups) {
184 # don't count newsgroup/hierarchy in wrong TLH
185 next if($TLH and !/^$TLH/);
186 # don't count invalid newsgroups
187 if(%ValidGroups and !defined($ValidGroups{$_})) {
188 warn (sprintf("DROPPED: %s\n",$_));
191 # add original newsgroup to %Newsgroups
193 # add all hierarchy elements to %Newsgroups, amended by '.ALL',
194 # i.e. de.alt.ALL and de.ALL
195 foreach (ParseHierarchies($_)) {
196 $Newsgroups{$_.'.ALL'} = 1;
202 ################################################################################
203 sub ParseHierarchies {
204 ################################################################################
205 ### return a list of all hierarchy levels a newsgroup belongs to
206 ### (for de.alt.test.moderated that would be de/de.alt/de.alt.test)
207 ### IN : $Newsgroup : a newsgroup name
208 ### OUT: @Hierarchies: array containing all hierarchies the newsgroup belongs to
209 my ($Newsgroup) = @_;
211 # strip trailing dots
212 $Newsgroup =~ s/(.+)\.+$/$1/;
213 # butcher newsgroup name by "." and add each hierarchy to @Hierarchies
214 # i.e. de.alt.test: "de.alt" and "de"
215 while ($Newsgroup =~ /\./) {
216 $Newsgroup =~ s/^((?:\.?[^.]+)*)\.[^.]+$/$1/;
217 push @Hierarchies, $Newsgroup;
222 ################################################################################
224 ################################################################################
225 ### read a list of valid newsgroups from file (each group on one line,
226 ### ignoring everything after the first whitespace and so accepting files
227 ### in checkgroups format as well as (parts of) an INN active file)
228 ### IN : $Filename : file to read
229 ### OUT: \%ValidGroups: hash containing all valid newsgroups
232 open (my $LIST,"<$Filename") or &Bleat(2,"Cannot read $Filename: $!");
237 $ValidGroups{$_} = '1';
240 return \%ValidGroups;
243 ################################################################################
245 #####----------------------------- TimePeriods ----------------------------#####
247 ################################################################################
249 ################################################################################
250 ### get a time period to act on from --month option;
251 ### if empty, default to last month
252 ### IN : $Month: may be empty, 'YYYY-MM', 'YYYY-MM:YYYY-MM' or 'all'
253 ### OUT: $Verbal,$SQL: verbal description and WHERE-clause
254 ### of the chosen time period
256 # define result variables
258 # define a regular expression for a month
259 my $REMonth = '\d{4}-\d{2}';
261 # default to last month if option is not set
266 # check for valid input
267 if ($Month =~ /^$REMonth$/) {
268 # single month (YYYY-MM)
269 ($Month) = &CheckMonth($Month);
271 $SQL = sprintf("month = '%s'",$Month);
272 } elsif ($Month =~ /^$REMonth:$REMonth$/) {
273 # time period (YYYY-MM:YYYY-MM)
274 $Verbal = sprintf('%s to %s',&SplitPeriod($Month));
275 $SQL = sprintf("month BETWEEN '%s' AND '%s'",&SplitPeriod($Month));
276 } elsif ($Month =~ /^all$/i) {
278 $Verbal = 'all time';
282 return (undef,undef);
285 return ($Verbal,$SQL);
288 ################################################################################
290 ################################################################################
291 ### get last month from todays date in YYYY-MM format
292 ### OUT: last month as YYYY-MM
294 my (undef,undef,undef,undef,$Month,$Year,undef,undef,undef) = localtime(time);
295 # $Month is already defined from 0 to 11, so no need to decrease it by 1
302 return sprintf('%4d-%02d',$Year,$Month);
305 ################################################################################
307 ################################################################################
308 ### check if input (in YYYY-MM form) is valid with MM between 01 and 12;
309 ### otherwise, fix it
310 ### IN : @Month: array of month
311 ### OUT: @Month: a valid month
313 foreach my $Month (@Month) {
314 my ($OldMonth) = $Month;
315 my ($CalMonth) = substr ($Month, -2);
316 if ($CalMonth < 1 or $CalMonth > 12) {
317 $CalMonth = '12' if $CalMonth > 12;
318 $CalMonth = '01' if $CalMonth < 1;
319 substr($Month, -2) = $CalMonth;
320 &Bleat(1,sprintf("'%s' is an invalid date (MM must be between '01' ".
321 "and '12'), set to '%s'.",$OldMonth,$Month));
327 ################################################################################
329 ################################################################################
330 ### split a time period denoted by YYYY-MM:YYYY-MM into start and end month
331 ### IN : $Period: time period
332 ### OUT: $StartMonth, $EndMonth
334 my ($StartMonth, $EndMonth) = split /:/, $Period;
335 ($StartMonth,$EndMonth) = CheckMonth($StartMonth,$EndMonth);
336 # switch parameters as necessary
337 if ($EndMonth gt $StartMonth) {
338 return ($StartMonth, $EndMonth);
340 return ($EndMonth, $StartMonth);
344 ################################################################################
346 ################################################################################
347 ### return a list of months (YYYY-MM) between start and end month
348 ### IN : $MonthExpression ('YYYY-MM' or 'YYYY-MM to YYYY-MM')
349 ### OUT: @Months: array containing all months from $MonthExpression enumerated
350 my ($MonthExpression )= @_;
351 # return if single month
352 return ($MonthExpression) if ($MonthExpression =~ /^\d{4}-\d{2}$/);
353 # parse $MonthExpression
354 my ($StartMonth, $EndMonth) = split(' to ',$MonthExpression);
355 # set $Year, $Month from $StartMonth
356 my ($Year, $Month) = split /-/, $StartMonth;
359 until ("$Year-$Month" gt $EndMonth) {
360 push @Months, "$Year-$Month";
361 $Month = "$Month"; # force string context
371 #####---------------------------- OutputFormats ---------------------------#####
373 ################################################################################
375 ################################################################################
376 ### read database query results from DBHandle and print results with formatting
377 ### IN : $Format : format specifier
378 ### $Comments : print or suppress all comments for machine-readable output
379 ### $GroupBy : primary sorting order (month or key)
380 ### $Precision: number of digits right of decimal point (0 or 2)
381 ### $ValidKeys: reference to a hash containing all valid keys
382 ### $FileTempl: file name template (--filetemplate): filetempl-YYYY-MM
383 ### $DBQuery : database query handle with executed query,
384 ### containing $Month, $Key, $Value
385 ### $PadGroup : padding length for key field (optional) for 'pretty'
386 my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $FileTempl,
387 $DBQuery, $PadGroup) = @_;
388 my %ValidKeys = %{$ValidKeys} if $ValidKeys;
389 my ($FileName, $Handle, $OUT);
392 # define output types
394 @LegalOutput{('dump',,'list','pretty')} = ();
395 # bail out if format is unknown
396 &Bleat(2,"Unknown output type '$Format'!") if !exists($LegalOutput{$Format});
398 while (my ($Month, $Key, $Value) = $DBQuery->fetchrow_array) {
399 # don't display invalid keys
400 if(%ValidKeys and !defined($ValidKeys{$Key})) {
402 # &Bleat(1,sprintf("DROPPED: %s",$Key));
405 # care for correct sorting order and abstract from month and keys:
406 # $Caption will be $Month or $Key, according to sorting order,
407 # and $Key will be $Key or $Month, respectively
409 if ($GroupBy eq 'key') {
415 # set output file handle
417 $Handle = *STDOUT{IO}; # set $Handle to a reference to STDOUT
418 } elsif (!defined($LastIteration) or $LastIteration ne $Caption) {
419 close $OUT if ($LastIteration);
420 # safeguards for filename creation:
421 # replace potential problem characters with '_'
422 $FileName = sprintf('%s-%s',$FileTempl,$Caption);
423 $FileName =~ s/[^a-zA-Z0-9_-]+/_/g;
424 open ($OUT,">$FileName")
425 or &Bleat(2,sprintf("Cannot open output file '%s': $!",
429 print $Handle &FormatOutput($Format, $Comments, $Caption, $Key, $Value,
430 $Precision, $PadGroup);
431 $LastIteration = $Caption;
433 close $OUT if ($FileTempl);
436 ################################################################################
438 ################################################################################
439 ### format information for output according to format specifier
440 ### IN : $Format : format specifier
441 ### $Comments : print or suppress all comments for machine-readable output
442 ### $Caption : month (as YYYY-MM) or $Key, according to sorting order
443 ### $Key : newsgroup, client, ... or $Month, as above
444 ### $Value : number of postings with that attribute
445 ### $Precision: number of digits right of decimal point (0 or 2)
446 ### $PadGroup : padding length for key field (optional) for 'pretty'
447 ### OUT: $Output: formatted output
448 my ($Format, $Comments, $Caption, $Key, $Value, $Precision, $PadGroup) = @_;
450 # keep last caption in mind
451 our ($LastIteration);
452 # create one line of output
453 if ($Format eq 'dump') {
454 # output as dump (key value)
455 $Output = sprintf ("# %s:\n",$Caption)
456 if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration));
457 $Output .= sprintf ("%s %u\n",$Key,$Value);
458 } elsif ($Format eq 'list') {
459 # output as list (caption key value)
460 $Output = sprintf ("%s %s %u\n",$Caption,$Key,$Value);
461 } elsif ($Format eq 'pretty') {
463 $Output = sprintf ("# ----- %s:\n",$Caption)
464 if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration));
465 $Output .= sprintf ($PadGroup ? sprintf("%%-%us %%10.*f\n",$PadGroup) :
466 "%s %.*f\n",$Key,$Precision,$Value);
471 #####------------------------- QueryModifications -------------------------#####
473 ################################################################################
475 ################################################################################
476 ### add exclusion of hierarchy levels (de.alt.ALL) from SQL query by
477 ### amending the WHERE clause if $ShowHierarchies is false (or don't, if it is
478 ### true, accordingly)
479 ### IN : $ShowHierarchies: boolean value
481 my ($ShowHierarchies) = @_;
482 return $ShowHierarchies ? '' : "newsgroup NOT LIKE '%.ALL'";
485 ################################################################################
487 ################################################################################
488 ### get length of longest field in future query result
489 ### IN : $DBHandle : database handel
490 ### $Table : table to query
491 ### $Field : field to check
492 ### $WhereClause : WHERE clause
493 ### $HavingClause: HAVING clause
494 ### @BindVars : bind variables for WHERE clause
495 ### OUT: $Length: length of longest instnace of $Field
496 my ($DBHandle,$Table,$Field,$WhereClause,$HavingClause,@BindVars) = @_;
497 my $DBQuery = $DBHandle->prepare(sprintf("SELECT MAX(LENGTH(%s)) ".
498 "FROM %s %s %s",$Field,$Table,
499 $WhereClause,$HavingClause ?
500 'GROUP BY newsgroup' . $HavingClause .
501 ' ORDER BY LENGTH(newsgroup) '.
502 'DESC LIMIT 1': ''));
503 $DBQuery->execute(@BindVars) or &Bleat(1,sprintf("Can't get field length ".
504 "for '%s' from table '%s': ".
505 "$DBI::errstr",$Field,$Table));
506 my ($Length) = $DBQuery->fetchrow_array;
510 ################################################################################
512 ################################################################################
513 ### build a SQL 'ORDER BY' clause from $OptGroupBy (primary sorting) and
514 ### $OptOrderBy (secondary sorting), both ascending or descending;
515 ### descending sorting order is done by adding '-desc'
516 ### IN : $GroupBy: primary sort by 'month' (default) or 'newsgroups'
517 ### $OrderBy: secondary sort by month/newsgroups (default)
518 ### or number of 'postings'
519 ### OUT: a SQL ORDER BY clause
520 my ($GroupBy,$OrderBy) = @_;
521 my ($GroupSort,$OrderSort) = ('','');
522 # $GroupBy (primary sorting)
526 ($GroupBy, $GroupSort) = SQLParseOrder($GroupBy);
527 if ($GroupBy =~ /group/i) {
528 $GroupBy = 'newsgroup';
533 my $Secondary = ($GroupBy eq 'month') ? 'newsgroup' : 'month';
534 # $OrderBy (secondary sorting)
536 $OrderBy = $Secondary;
538 ($OrderBy, $OrderSort) = SQLParseOrder($OrderBy);
539 if ($OrderBy =~ /posting/i) {
540 $OrderBy = "postings $OrderSort, $Secondary";
542 $OrderBy = "$Secondary $OrderSort";
545 return ($GroupBy,&SQLBuildClause('order',"$GroupBy $GroupSort",$OrderBy));
548 ################################################################################
550 ################################################################################
551 ### parse $OptGroupBy or $OptOrderBy option of the form param[-desc], e.g.
552 ### 'month', 'month-desc', 'newsgroups-desc', but also just 'desc'
553 ### IN : $OrderOption: order option (see above)
554 ### OUT: parameter to sort by,
555 ### sort order ('DESC' or nothing, meaning 'ASC')
556 my ($OrderOption) = @_;
558 if ($OrderOption =~ s/-?desc$//i) {
561 $OrderOption =~ s/-?asc$//i
563 return ($OrderOption,$SortOrder);
566 ################################################################################
568 ################################################################################
569 ### explode list of newsgroups separated by : (with wildcards)
570 ### to a SQL 'WHERE' expression
571 ### IN : $Newsgroups: list of newsgroups (group.one.*:group.two:group.three.*)
572 ### OUT: SQL code to become part of a 'WHERE' clause,
573 ### list of newsgroups for SQL bindings
574 my ($Newsgroups) = @_;
575 # substitute '*' wildcard with SQL wildcard character '%'
576 $Newsgroups =~ s/\*/%/g;
577 # just one newsgroup?
578 return (SQLGroupWildcard($Newsgroups),$Newsgroups) if $Newsgroups !~ /:/;
579 # list of newsgroups separated by ':'
581 my @GroupList = split /:/, $Newsgroups;
582 foreach (@GroupList) {
583 $SQL .= ' OR ' if $SQL gt '(';
584 $SQL .= SQLGroupWildcard($_);
587 return ($SQL,@GroupList);
590 ################################################################################
591 sub SQLGroupWildcard {
592 ################################################################################
593 ### build a valid SQL 'WHERE' expression with or without wildcards
594 ### IN : $Newsgroup: newsgroup expression, probably with wildcard
595 ### (group.name or group.name.%)
596 ### OUT: SQL code to become part of a 'WHERE' clause
597 my ($Newsgroup) = @_;
598 # FIXME: check for validity
599 if ($Newsgroup !~ /%/) {
600 return 'newsgroup = ?';
602 return 'newsgroup LIKE ?';
606 ################################################################################
608 ################################################################################
609 ### set upper and/or lower boundary (number of postings)
610 ### IN : $Type: 'level', 'average', 'sum' or 'default'
611 ### $LowBound,$UppBound: lower/upper boundary, respectively
612 ### OUT: SQL code to become part of a WHERE or HAVING clause
613 my ($Type,$LowBound,$UppBound) = @_;
614 ($LowBound,$UppBound) = SQLCheckNumber($LowBound,$UppBound);
615 if($LowBound and $UppBound and $LowBound > $UppBound) {
616 &Bleat(1,"Lower boundary $LowBound is larger than Upper boundary ".
617 "$UppBound, exchanging boundaries.");
618 ($LowBound,$UppBound) = ($UppBound,$LowBound);
620 # default to 'default'
621 my $WhereHavingFunction = 'postings';
622 # set $LowBound to SQL statement:
623 # 'WHERE postings >=', 'HAVING MIN(postings) >=' or 'HAVING AVG(postings) >='
624 if ($Type eq 'level') {
625 $WhereHavingFunction = 'MIN(postings)'
626 } elsif ($Type eq 'average') {
627 $WhereHavingFunction = 'AVG(postings)'
628 } elsif ($Type eq 'sum') {
629 $WhereHavingFunction = 'SUM(postings)'
631 $LowBound = sprintf('%s >= '.$LowBound,$WhereHavingFunction) if ($LowBound);
632 # set $LowBound to SQL statement:
633 # 'WHERE postings <=', 'HAVING MAX(postings) <=' or 'HAVING AVG(postings) <='
634 if ($Type eq 'level') {
635 $WhereHavingFunction = 'MAX(postings)'
636 } elsif ($Type eq 'average') {
637 $WhereHavingFunction = 'AVG(postings)'
638 } elsif ($Type eq 'sum') {
639 $WhereHavingFunction = 'SUM(postings)'
641 $UppBound = sprintf('%s <= '.$UppBound,$WhereHavingFunction) if ($UppBound);
642 return ($LowBound,$UppBound);
645 ################################################################################
647 ################################################################################
648 ### check if input is a valid positive integer; otherwise, make it one
649 ### IN : @Numbers: array of parameters
650 ### OUT: @Numbers: a valid positive integer
652 foreach my $Number (@Numbers) {
653 if ($Number and $Number < 0) {
654 &Bleat(1,"Boundary $Number is < 0, set to ".-$Number);
657 $Number = '' if ($Number and $Number !~ /^\d+$/);
662 ################################################################################
664 ################################################################################
665 ### build a valid SQL WHERE, GROUP BY, ORDER BY or HAVING clause
666 ### from multiple expressions which *may* be empty
667 ### IN : $Type: 'where', 'having', 'group' or 'order'
668 ### @Expressions: array of expressions
669 ### OUT: $SQLClause: a SQL clause
670 my ($Type,@Expressions) = @_;
671 my ($SQLClause,$Separator,$Statement);
672 # set separator ('AND' or ',')
673 if ($Type eq 'where' or $Type eq 'having') {
679 if ($Type eq 'where') {
680 $Statement = 'WHERE';
681 } elsif ($Type eq 'order') {
682 $Statement = 'ORDER BY';
683 } elsif ($Type eq 'having') {
684 $Statement = 'HAVING';
686 $Statement = 'GROUP BY';
688 # build query from expressions with separators
689 foreach my $Expression (@Expressions) {
691 $SQLClause .= " $Separator " if ($SQLClause);
692 $SQLClause .= $Expression;
695 # add statement in front if not already present
696 $SQLClause = " $Statement " . $SQLClause
697 if ($SQLClause and $SQLClause !~ /$Statement/);
702 #####------------------------------- done ---------------------------------#####