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);
46 %EXPORT_TAGS = ( TimePeriods => [qw(GetTimePeriod LastMonth SplitPeriod
48 Output => [qw(OutputData FormatOutput)],
49 SQLHelper => [qw(SQLHierarchies SQLSortOrder SQLGroupList
50 SQLSetBounds SQLBuildClause GetMaxLength)]);
52 our $PackageVersion = '0.01';
60 #####-------------------------------- Vars --------------------------------#####
64 our $HomePath = dirname($0);
67 our $MyVersion = "$0 $::VERSION (NewsStats.pm $VERSION)";
69 #####------------------------------- Basics -------------------------------#####
71 ################################################################################
73 ################################################################################
75 ################################################################################
76 ### display version and exit
77 print "NewsStats v$PackageVersion\n$MyVersion\n";
78 print "Copyright (c) 2010-2012 Thomas Hochstein <thh\@inter.net>\n";
79 print "This program is free software; you may redistribute it ".
80 "and/or modify it under the same terms as Perl itself.\n";
83 ################################################################################
85 ################################################################################
87 ################################################################################
88 ### feed myself to perldoc and exit
89 exec('perldoc', $FullPath);
92 ################################################################################
94 ################################################################################
96 ################################################################################
97 ### read config via Config::Auto
98 ### IN : $ConfFile: config filename
99 ### OUT: reference to a hash containing the configuration
101 return Config::Auto::parse($ConfFile, format => 'equal');
103 ################################################################################
105 ################################################################################
107 ################################################################################
108 ### override configuration values
109 ### IN : $ConfigR : reference to configuration hash
110 ### $OverrideR: reference to a hash containing overrides
111 my ($ConfigR,$OverrideR) = @_;
112 my %Override = %$OverrideR;
114 &Bleat(1,"Empty configuration hash passed to OverrideConfig()")
115 if ( keys %$ConfigR < 1);
116 # return if no overrides
117 return if (keys %Override < 1 or keys %$ConfigR < 1);
118 foreach my $Key (keys %Override) {
119 $$ConfigR{$Key} = $Override{$Key};
122 ################################################################################
124 ################################################################################
126 ################################################################################
127 ### initialise database connection
128 ### IN : $ConfigR: reference to configuration hash
129 ### $Die : if TRUE, die if connection fails
131 my ($ConfigR,$Die) = @_;
132 my %Conf = %$ConfigR;
133 my $DBHandle = DBI->connect(sprintf('DBI:%s:database=%s;host=%s',
134 $Conf{'DBDriver'},$Conf{'DBDatabase'},
135 $Conf{'DBHost'}), $Conf{'DBUser'},
136 $Conf{'DBPw'}, { PrintError => 0 });
138 &Bleat(2,$DBI::errstr) if (defined($Die) and $Die);
139 &Bleat(1,$DBI::errstr);
143 ################################################################################
145 ################################################################################
147 ################################################################################
148 ### print warning or error messages and terminate in case of error
149 ### IN : $Level : 1 = warning, 2 = error
150 ### $Message: warning or error message
151 my ($Level,$Message) = @_;
153 warn "$0 W: $Message\n"
154 } elsif ($Level == 2) {
155 die "$0 E: $Message\n"
157 print "$0: $Message\n"
160 ################################################################################
162 #####------------------------------ GetStats ------------------------------#####
164 ################################################################################
166 ################################################################################
167 ### explode a (scalar) list of newsgroup names to a list of newsgroup and
168 ### hierarchy names where every newsgroup and hierarchy appears only once:
169 ### de.alt.test,de.alt.admin -> de.ALL, de.alt.ALL, de.alt.test, de.alt.admin
170 ### IN : $Newsgroups : a list of newsgroups (content of Newsgroups: header)
171 ### $TLH : top level hierarchy (all other newsgroups are ignored)
172 ### $ValidGroupsR: reference to a hash containing all valid newsgroups
174 ### OUT: %Newsgroups : hash containing all newsgroup and hierarchy names as keys
175 my ($Newsgroups,$TLH,$ValidGroupsR) = @_;
176 my %ValidGroups = %{$ValidGroupsR} if $ValidGroupsR;
179 # remove whitespace from contents of Newsgroups:
180 $Newsgroups =~ s/\s//;
181 # call &HierarchyCount for each newsgroup in $Newsgroups:
182 for (split /,/, $Newsgroups) {
183 # don't count newsgroup/hierarchy in wrong TLH
184 next if($TLH and !/^$TLH/);
185 # don't count invalid newsgroups
186 if(%ValidGroups and !defined($ValidGroups{$_})) {
187 warn (sprintf("DROPPED: %s\n",$_));
190 # add original newsgroup to %Newsgroups
192 # add all hierarchy elements to %Newsgroups, amended by '.ALL',
193 # i.e. de.alt.ALL and de.ALL
194 foreach (ParseHierarchies($_)) {
195 $Newsgroups{$_.'.ALL'} = 1;
201 ################################################################################
202 sub ParseHierarchies {
203 ################################################################################
204 ### return a list of all hierarchy levels a newsgroup belongs to
205 ### (for de.alt.test.moderated that would be de/de.alt/de.alt.test)
206 ### IN : $Newsgroup : a newsgroup name
207 ### OUT: @Hierarchies: array containing all hierarchies the newsgroup belongs to
208 my ($Newsgroup) = @_;
210 # strip trailing dots
211 $Newsgroup =~ s/(.+)\.+$/$1/;
212 # butcher newsgroup name by "." and add each hierarchy to @Hierarchies
213 # i.e. de.alt.test: "de.alt" and "de"
214 while ($Newsgroup =~ /\./) {
215 $Newsgroup =~ s/^((?:\.?[^.]+)*)\.[^.]+$/$1/;
216 push @Hierarchies, $Newsgroup;
221 ################################################################################
223 ################################################################################
224 ### read a list of valid newsgroups from file (each group on one line,
225 ### ignoring everything after the first whitespace and so accepting files
226 ### in checkgroups format as well as (parts of) an INN active file)
227 ### IN : $Filename : file to read
228 ### OUT: \%ValidGroups: hash containing all valid newsgroups
231 open (my $LIST,"<$Filename") or &Bleat(2,"Cannot read $Filename: $!");
236 $ValidGroups{$_} = '1';
239 return \%ValidGroups;
242 ################################################################################
244 #####----------------------------- TimePeriods ----------------------------#####
246 ################################################################################
248 ################################################################################
249 ### get a time period to act on from --month option;
250 ### if empty, default to last month
251 ### IN : $Month: may be empty, 'YYYY-MM', 'YYYY-MM:YYYY-MM' or 'all'
252 ### OUT: $Verbal,$SQL: verbal description and WHERE-clause
253 ### of the chosen time period
255 # define result variables
257 # define a regular expression for a month
258 my $REMonth = '\d{4}-\d{2}';
260 # default to last month if option is not set
265 # check for valid input
266 if ($Month =~ /^$REMonth$/) {
267 # single month (YYYY-MM)
268 ($Month) = &CheckMonth($Month);
270 $SQL = sprintf("month = '%s'",$Month);
271 } elsif ($Month =~ /^$REMonth:$REMonth$/) {
272 # time period (YYYY-MM:YYYY-MM)
273 $Verbal = sprintf('%s to %s',&SplitPeriod($Month));
274 $SQL = sprintf("month BETWEEN '%s' AND '%s'",&SplitPeriod($Month));
275 } elsif ($Month =~ /^all$/i) {
277 $Verbal = 'all time';
281 return (undef,undef);
284 return ($Verbal,$SQL);
287 ################################################################################
289 ################################################################################
290 ### get last month from todays date in YYYY-MM format
291 ### OUT: last month as YYYY-MM
293 my (undef,undef,undef,undef,$Month,$Year,undef,undef,undef) = localtime(time);
294 # $Month is already defined from 0 to 11, so no need to decrease it by 1
301 return sprintf('%4d-%02d',$Year,$Month);
304 ################################################################################
306 ################################################################################
307 ### check if input (in YYYY-MM form) is valid with MM between 01 and 12;
308 ### otherwise, fix it
309 ### IN : @Month: array of month
310 ### OUT: @Month: a valid month
312 foreach my $Month (@Month) {
313 my ($OldMonth) = $Month;
314 my ($CalMonth) = substr ($Month, -2);
315 if ($CalMonth < 1 or $CalMonth > 12) {
316 $CalMonth = '12' if $CalMonth > 12;
317 $CalMonth = '01' if $CalMonth < 1;
318 substr($Month, -2) = $CalMonth;
319 &Bleat(1,sprintf("'%s' is an invalid date (MM must be between '01' ".
320 "and '12'), set to '%s'.",$OldMonth,$Month));
326 ################################################################################
328 ################################################################################
329 ### split a time period denoted by YYYY-MM:YYYY-MM into start and end month
330 ### IN : $Period: time period
331 ### OUT: $StartMonth, $EndMonth
333 my ($StartMonth, $EndMonth) = split /:/, $Period;
334 ($StartMonth,$EndMonth) = CheckMonth($StartMonth,$EndMonth);
335 # switch parameters as necessary
336 if ($EndMonth gt $StartMonth) {
337 return ($StartMonth, $EndMonth);
339 return ($EndMonth, $StartMonth);
343 ################################################################################
345 ################################################################################
346 ### return a list of months (YYYY-MM) between start and end month
347 ### IN : $MonthExpression ('YYYY-MM' or 'YYYY-MM to YYYY-MM')
348 ### OUT: @Months: array containing all months from $MonthExpression enumerated
349 my ($MonthExpression )= @_;
350 # return if single month
351 return ($MonthExpression) if ($MonthExpression =~ /^\d{4}-\d{2}$/);
352 # parse $MonthExpression
353 my ($StartMonth, $EndMonth) = split(' to ',$MonthExpression);
354 # set $Year, $Month from $StartMonth
355 my ($Year, $Month) = split /-/, $StartMonth;
358 until ("$Year-$Month" gt $EndMonth) {
359 push @Months, "$Year-$Month";
360 $Month = "$Month"; # force string context
370 #####---------------------------- OutputFormats ---------------------------#####
372 ################################################################################
374 ################################################################################
375 ### read database query results from DBHandle and print results with formatting
376 ### IN : $Format : format specifier
377 ### $Comments : print or suppress all comments for machine-readable output
378 ### $GroupBy : primary sorting order (month or key)
379 ### $Precision: number of digits right of decimal point (0 or 2)
380 ### $ValidKeys: reference to a hash containing all valid keys
381 ### $FileTempl: file name template (--filetemplate): filetempl-YYYY-MM
382 ### $DBQuery : database query handle with executed query,
383 ### containing $Month, $Key, $Value
384 ### $PadGroup : padding length for key field (optional) for 'pretty'
385 my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $FileTempl,
386 $DBQuery, $PadGroup) = @_;
387 my %ValidKeys = %{$ValidKeys} if $ValidKeys;
388 my ($FileName, $Handle, $OUT);
391 # define output types
393 @LegalOutput{('dump',,'list','pretty')} = ();
394 # bail out if format is unknown
395 &Bleat(2,"Unknown output type '$Format'!") if !exists($LegalOutput{$Format});
397 while (my ($Month, $Key, $Value) = $DBQuery->fetchrow_array) {
398 # don't display invalid keys
399 if(%ValidKeys and !defined($ValidKeys{$Key})) {
401 # &Bleat(1,sprintf("DROPPED: %s",$Key));
404 # care for correct sorting order and abstract from month and keys:
405 # $Caption will be $Month or $Key, according to sorting order,
406 # and $Key will be $Key or $Month, respectively
408 if ($GroupBy eq 'key') {
414 # set output file handle
416 $Handle = *STDOUT{IO}; # set $Handle to a reference to STDOUT
417 } elsif (!defined($LastIteration) or $LastIteration ne $Caption) {
418 close $OUT if ($LastIteration);
419 # safeguards for filename creation:
420 # replace potential problem characters with '_'
421 $FileName = sprintf('%s-%s',$FileTempl,$Caption);
422 $FileName =~ s/[^a-zA-Z0-9_-]+/_/g;
423 open ($OUT,">$FileName")
424 or &Bleat(2,sprintf("Cannot open output file '%s': $!",
428 print $Handle &FormatOutput($Format, $Comments, $Caption, $Key, $Value,
429 $Precision, $PadGroup);
430 $LastIteration = $Caption;
432 close $OUT if ($FileTempl);
435 ################################################################################
437 ################################################################################
438 ### format information for output according to format specifier
439 ### IN : $Format : format specifier
440 ### $Comments : print or suppress all comments for machine-readable output
441 ### $Caption : month (as YYYY-MM) or $Key, according to sorting order
442 ### $Key : newsgroup, client, ... or $Month, as above
443 ### $Value : number of postings with that attribute
444 ### $Precision: number of digits right of decimal point (0 or 2)
445 ### $PadGroup : padding length for key field (optional) for 'pretty'
446 ### OUT: $Output: formatted output
447 my ($Format, $Comments, $Caption, $Key, $Value, $Precision, $PadGroup) = @_;
449 # keep last caption in mind
450 our ($LastIteration);
451 # create one line of output
452 if ($Format eq 'dump') {
453 # output as dump (key value)
454 $Output = sprintf ("# %s:\n",$Caption)
455 if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration));
456 $Output .= sprintf ("%s %u\n",$Key,$Value);
457 } elsif ($Format eq 'list') {
458 # output as list (caption key value)
459 $Output = sprintf ("%s %s %u\n",$Caption,$Key,$Value);
460 } elsif ($Format eq 'pretty') {
462 $Output = sprintf ("# ----- %s:\n",$Caption)
463 if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration));
464 $Output .= sprintf ($PadGroup ? sprintf("%%-%us %%10.*f\n",$PadGroup) :
465 "%s %.*f\n",$Key,$Precision,$Value);
470 #####------------------------- QueryModifications -------------------------#####
472 ################################################################################
474 ################################################################################
475 ### add exclusion of hierarchy levels (de.alt.ALL) from SQL query by
476 ### amending the WHERE clause if $ShowHierarchies is false (or don't, if it is
477 ### true, accordingly)
478 ### IN : $ShowHierarchies: boolean value
480 my ($ShowHierarchies) = @_;
481 return $ShowHierarchies ? '' : "newsgroup NOT LIKE '%.ALL'";
484 ################################################################################
486 ################################################################################
487 ### get length of longest field in future query result
488 ### IN : $DBHandle : database handel
489 ### $Table : table to query
490 ### $Field : field to check
491 ### $WhereClause : WHERE clause
492 ### $HavingClause: HAVING clause
493 ### @BindVars : bind variables for WHERE clause
494 ### OUT: $Length: length of longest instnace of $Field
495 my ($DBHandle,$Table,$Field,$WhereClause,$HavingClause,@BindVars) = @_;
496 my $DBQuery = $DBHandle->prepare(sprintf("SELECT MAX(LENGTH(%s)) ".
497 "FROM %s %s %s",$Field,$Table,
498 $WhereClause,$HavingClause ?
499 'GROUP BY newsgroup' . $HavingClause .
500 ' ORDER BY LENGTH(newsgroup) '.
501 'DESC LIMIT 1': ''));
502 $DBQuery->execute(@BindVars) or &Bleat(1,sprintf("Can't get field length ".
503 "for '%s' from table '%s': ".
504 "$DBI::errstr",$Field,$Table));
505 my ($Length) = $DBQuery->fetchrow_array;
509 ################################################################################
511 ################################################################################
512 ### build a SQL 'ORDER BY' clause from $OptGroupBy (primary sorting) and
513 ### $OptOrderBy (secondary sorting), both ascending or descending;
514 ### descending sorting order is done by adding '-desc'
515 ### IN : $GroupBy: primary sort by 'month' (default) or 'newsgroups'
516 ### $OrderBy: secondary sort by month/newsgroups (default)
517 ### or number of 'postings'
518 ### OUT: a SQL ORDER BY clause
519 my ($GroupBy,$OrderBy) = @_;
520 my ($GroupSort,$OrderSort) = ('','');
521 # $GroupBy (primary sorting)
525 ($GroupBy, $GroupSort) = SQLParseOrder($GroupBy);
526 if ($GroupBy =~ /group/i) {
527 $GroupBy = 'newsgroup';
532 my $Secondary = ($GroupBy eq 'month') ? 'newsgroup' : 'month';
533 # $OrderBy (secondary sorting)
535 $OrderBy = $Secondary;
537 ($OrderBy, $OrderSort) = SQLParseOrder($OrderBy);
538 if ($OrderBy =~ /posting/i) {
539 $OrderBy = "postings $OrderSort, $Secondary";
541 $OrderBy = "$Secondary $OrderSort";
544 return ($GroupBy,&SQLBuildClause('order',"$GroupBy $GroupSort",$OrderBy));
547 ################################################################################
549 ################################################################################
550 ### parse $OptGroupBy or $OptOrderBy option of the form param[-desc], e.g.
551 ### 'month', 'month-desc', 'newsgroups-desc', but also just 'desc'
552 ### IN : $OrderOption: order option (see above)
553 ### OUT: parameter to sort by,
554 ### sort order ('DESC' or nothing, meaning 'ASC')
555 my ($OrderOption) = @_;
557 if ($OrderOption =~ s/-?desc$//i) {
560 $OrderOption =~ s/-?asc$//i
562 return ($OrderOption,$SortOrder);
565 ################################################################################
567 ################################################################################
568 ### explode list of newsgroups separated by : (with wildcards)
569 ### to a SQL 'WHERE' expression
570 ### IN : $Newsgroups: list of newsgroups (group.one.*:group.two:group.three.*)
571 ### OUT: SQL code to become part of a 'WHERE' clause,
572 ### list of newsgroups for SQL bindings
573 my ($Newsgroups) = @_;
574 # substitute '*' wildcard with SQL wildcard character '%'
575 $Newsgroups =~ s/\*/%/g;
576 # just one newsgroup?
577 return (SQLGroupWildcard($Newsgroups),$Newsgroups) if $Newsgroups !~ /:/;
578 # list of newsgroups separated by ':'
580 my @GroupList = split /:/, $Newsgroups;
581 foreach (@GroupList) {
582 $SQL .= ' OR ' if $SQL gt '(';
583 $SQL .= SQLGroupWildcard($_);
586 return ($SQL,@GroupList);
589 ################################################################################
590 sub SQLGroupWildcard {
591 ################################################################################
592 ### build a valid SQL 'WHERE' expression with or without wildcards
593 ### IN : $Newsgroup: newsgroup expression, probably with wildcard
594 ### (group.name or group.name.%)
595 ### OUT: SQL code to become part of a 'WHERE' clause
596 my ($Newsgroup) = @_;
597 # FIXME: check for validity
598 if ($Newsgroup !~ /%/) {
599 return 'newsgroup = ?';
601 return 'newsgroup LIKE ?';
605 ################################################################################
607 ################################################################################
608 ### set upper and/or lower boundary (number of postings)
609 ### IN : $Type: 'level', 'average', 'sum' or 'default'
610 ### $LowBound,$UppBound: lower/upper boundary, respectively
611 ### OUT: SQL code to become part of a WHERE or HAVING clause
612 my ($Type,$LowBound,$UppBound) = @_;
613 ($LowBound,$UppBound) = SQLCheckNumber($LowBound,$UppBound);
614 if($LowBound and $UppBound and $LowBound > $UppBound) {
615 &Bleat(1,"Lower boundary $LowBound is larger than Upper boundary ".
616 "$UppBound, exchanging boundaries.");
617 ($LowBound,$UppBound) = ($UppBound,$LowBound);
619 # default to 'default'
620 my $WhereHavingFunction = 'postings';
621 # set $LowBound to SQL statement:
622 # 'WHERE postings >=', 'HAVING MIN(postings) >=' or 'HAVING AVG(postings) >='
623 if ($Type eq 'level') {
624 $WhereHavingFunction = 'MIN(postings)'
625 } elsif ($Type eq 'average') {
626 $WhereHavingFunction = 'AVG(postings)'
627 } elsif ($Type eq 'sum') {
628 $WhereHavingFunction = 'SUM(postings)'
630 $LowBound = sprintf('%s >= '.$LowBound,$WhereHavingFunction) if ($LowBound);
631 # set $LowBound to SQL statement:
632 # 'WHERE postings <=', 'HAVING MAX(postings) <=' or 'HAVING AVG(postings) <='
633 if ($Type eq 'level') {
634 $WhereHavingFunction = 'MAX(postings)'
635 } elsif ($Type eq 'average') {
636 $WhereHavingFunction = 'AVG(postings)'
637 } elsif ($Type eq 'sum') {
638 $WhereHavingFunction = 'SUM(postings)'
640 $UppBound = sprintf('%s <= '.$UppBound,$WhereHavingFunction) if ($UppBound);
641 return ($LowBound,$UppBound);
644 ################################################################################
646 ################################################################################
647 ### check if input is a valid positive integer; otherwise, make it one
648 ### IN : @Numbers: array of parameters
649 ### OUT: @Numbers: a valid positive integer
651 foreach my $Number (@Numbers) {
652 if ($Number and $Number < 0) {
653 &Bleat(1,"Boundary $Number is < 0, set to ".-$Number);
656 $Number = '' if ($Number and $Number !~ /^\d+$/);
661 ################################################################################
663 ################################################################################
664 ### build a valid SQL WHERE, GROUP BY, ORDER BY or HAVING clause
665 ### from multiple expressions which *may* be empty
666 ### IN : $Type: 'where', 'having', 'group' or 'order'
667 ### @Expressions: array of expressions
668 ### OUT: $SQLClause: a SQL clause
669 my ($Type,@Expressions) = @_;
670 my ($SQLClause,$Separator,$Statement);
671 # set separator ('AND' or ',')
672 if ($Type eq 'where' or $Type eq 'having') {
678 if ($Type eq 'where') {
679 $Statement = 'WHERE';
680 } elsif ($Type eq 'order') {
681 $Statement = 'ORDER BY';
682 } elsif ($Type eq 'having') {
683 $Statement = 'HAVING';
685 $Statement = 'GROUP BY';
687 # build query from expressions with separators
688 foreach my $Expression (@Expressions) {
690 $SQLClause .= " $Separator " if ($SQLClause);
691 $SQLClause .= $Expression;
694 # add statement in front if not already present
695 $SQLClause = " $Statement " . $SQLClause
696 if ($SQLClause and $SQLClause !~ /$Statement/);
701 #####------------------------------- done ---------------------------------#####