3 # Library functions for the NewsStats package.
5 # Copyright (c) 2010-2013 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.02';
62 #####-------------------------------- Vars --------------------------------#####
64 # save $0 in $FullPath
66 # strip filename and /bin or /install directory to create the $HomePath
67 our $HomePath = dirname(realpath($0));
68 $HomePath =~ s/\/(bin|install)//;
72 our $MyVersion = "$0 $::VERSION (NewsStats.pm $VERSION)";
74 #####------------------------------- Basics -------------------------------#####
76 ################################################################################
78 ################################################################################
80 ################################################################################
81 ### display version and exit
82 print "NewsStats v$PackageVersion\n$MyVersion\n";
83 print "Copyright (c) 2010-2013 Thomas Hochstein <thh\@inter.net>\n";
84 print "This program is free software; you may redistribute it ".
85 "and/or modify it under the same terms as Perl itself.\n";
88 ################################################################################
90 ################################################################################
92 ################################################################################
93 ### feed myself to perldoc and exit
94 exec('perldoc', $FullPath);
97 ################################################################################
99 ################################################################################
101 ################################################################################
102 ### read config via Config::Auto
103 ### IN : $ConfFile: config filename
104 ### OUT: reference to a hash containing the configuration
107 $ConfFile = $HomePath . '/etc/newsstats.conf' if !$ConfFile;
108 # mandatory configuration options
109 my @Mandatory = ('DBDriver','DBHost','DBUser','DBPw','DBDatabase',
110 'DBTableRaw','DBTableParse','DBTableGrps');
111 # read config via Config::Auto
112 my $ConfR = Config::Auto::parse($ConfFile, format => 'equal');
113 my %Conf = %{$ConfR};
114 # check for mandatory options
115 foreach (@Mandatory) {
116 &Bleat(2,sprintf("Mandatory configuration option %s is not set!",$_))
117 if (!defined($Conf{$_}));
119 # $Conf{'TLH'} is checked in gatherstats.pl
122 ################################################################################
124 ################################################################################
126 ################################################################################
127 ### override configuration values
128 ### IN : $ConfigR : reference to configuration hash
129 ### $OverrideR: reference to a hash containing overrides
130 my ($ConfigR,$OverrideR) = @_;
131 my %Override = %$OverrideR;
133 &Bleat(1,"Empty configuration hash passed to OverrideConfig()")
134 if ( keys %$ConfigR < 1);
135 # return if no overrides
136 return if (keys %Override < 1 or keys %$ConfigR < 1);
137 foreach my $Key (keys %Override) {
138 $$ConfigR{$Key} = $Override{$Key};
141 ################################################################################
143 ################################################################################
145 ################################################################################
146 ### initialise database connection
147 ### IN : $ConfigR: reference to configuration hash
148 ### $Die : if TRUE, die if connection fails
150 my ($ConfigR,$Die) = @_;
151 my %Conf = %$ConfigR;
152 my $DBHandle = DBI->connect(sprintf('DBI:%s:database=%s;host=%s',
153 $Conf{'DBDriver'},$Conf{'DBDatabase'},
154 $Conf{'DBHost'}), $Conf{'DBUser'},
155 $Conf{'DBPw'}, { PrintError => 0 });
157 &Bleat(2,$DBI::errstr) if (defined($Die) and $Die);
158 &Bleat(1,$DBI::errstr);
162 ################################################################################
164 ################################################################################
166 ################################################################################
167 ### print warning or error messages and terminate in case of error
168 ### IN : $Level : 1 = warning, 2 = error
169 ### $Message: warning or error message
170 my ($Level,$Message) = @_;
172 warn "$0 W: $Message\n"
173 } elsif ($Level == 2) {
174 die "$0 E: $Message\n"
176 print "$0: $Message\n"
179 ################################################################################
181 #####------------------------------ GetStats ------------------------------#####
183 ################################################################################
185 ################################################################################
186 ### explode a (scalar) list of newsgroup names to a list of newsgroup and
187 ### hierarchy names where every newsgroup and hierarchy appears only once:
188 ### de.alt.test,de.alt.admin -> de.ALL, de.alt.ALL, de.alt.test, de.alt.admin
189 ### IN : $Newsgroups : a list of newsgroups (content of Newsgroups: header)
190 ### $TLH : top level hierarchy (all other newsgroups are ignored)
191 ### $ValidGroupsR: reference to a hash containing all valid newsgroups
193 ### OUT: %Newsgroups : hash containing all newsgroup and hierarchy names as keys
194 my ($Newsgroups,$TLH,$ValidGroupsR) = @_;
195 my %ValidGroups = %{$ValidGroupsR} if $ValidGroupsR;
198 # remove whitespace from contents of Newsgroups:
199 $Newsgroups =~ s/\s//;
200 # call &HierarchyCount for each newsgroup in $Newsgroups:
201 for (split /,/, $Newsgroups) {
202 # don't count newsgroup/hierarchy in wrong TLH
203 next if($TLH and !/^$TLH/);
204 # don't count invalid newsgroups
205 if(%ValidGroups and !defined($ValidGroups{$_})) {
206 warn (sprintf("DROPPED: %s\n",$_));
209 # add original newsgroup to %Newsgroups
211 # add all hierarchy elements to %Newsgroups, amended by '.ALL',
212 # i.e. de.alt.ALL and de.ALL
213 foreach (ParseHierarchies($_)) {
214 $Newsgroups{$_.'.ALL'} = 1;
220 ################################################################################
221 sub ParseHierarchies {
222 ################################################################################
223 ### return a list of all hierarchy levels a newsgroup belongs to
224 ### (for de.alt.test.moderated that would be de/de.alt/de.alt.test)
225 ### IN : $Newsgroup : a newsgroup name
226 ### OUT: @Hierarchies: array containing all hierarchies the newsgroup belongs to
227 my ($Newsgroup) = @_;
229 # strip trailing dots
230 $Newsgroup =~ s/(.+)\.+$/$1/;
231 # butcher newsgroup name by "." and add each hierarchy to @Hierarchies
232 # i.e. de.alt.test: "de.alt" and "de"
233 while ($Newsgroup =~ /\./) {
234 $Newsgroup =~ s/^((?:\.?[^.]+)*)\.[^.]+$/$1/;
235 push @Hierarchies, $Newsgroup;
240 ################################################################################
242 ################################################################################
243 ### read a list of valid newsgroups from file (each group on one line,
244 ### ignoring everything after the first whitespace and so accepting files
245 ### in checkgroups format as well as (parts of) an INN active file)
246 ### IN : $Filename : file to read
247 ### OUT: \%ValidGroups: reference to a hash containing all valid newsgroups
250 open (my $LIST,"<$Filename") or &Bleat(2,"Cannot read $Filename: $!");
255 $ValidGroups{$_} = '1';
258 return \%ValidGroups;
261 ################################################################################
263 #####----------------------------- TimePeriods ----------------------------#####
265 ################################################################################
267 ################################################################################
268 ### get a time period to act on from --month / --day option;
269 ### if empty, default to last month / day
270 ### IN : $Period: may be empty, 'YYYY-MM(-DD)', 'YYYY-MM(-DD):YYYY-MM(-DD)'
272 ### $Type : may be 'month' or 'day'
273 ### OUT: $Verbal,$SQL: verbal description and WHERE-clause
274 ### of the chosen time period
275 my ($Period,$Type) = @_;
276 # define result variables
279 $Type = 'month' if (!$Type or ($Type ne 'month' and $Type ne 'day'));
280 # define a regular expressions for a month or day
281 my $REPeriod = '\d{4}-\d{2}';
282 $REPeriod .= '-\d{2}' if ($Type eq 'day');
284 # default to last month / day if option is not set
286 $Period = &LastMonthDay($Type);
289 # check for valid input
290 if ($Period =~ /^$REPeriod$/) {
291 # single month/day [YYYY-MM(-DD)]
292 ($Period) = &CheckPeriod($Type,$Period);
294 $SQL = sprintf("%s = '%s'",$Type,$Period);
295 } elsif ($Period =~ /^$REPeriod:$REPeriod$/) {
296 # time period [YYYY-MM(-DD):YYYY-MM(-DD)]
297 $Verbal = sprintf('%s to %s',&SplitPeriod($Period,$Type));
298 $SQL = sprintf("%s BETWEEN '%s' AND '%s'",$Type,
299 &SplitPeriod($Period,$Type));
300 } elsif ($Period =~ /^all$/i) {
302 $Verbal = 'all time';
306 return (undef,undef);
309 return ($Verbal,$SQL);
312 ################################################################################
314 ################################################################################
315 ### get last month/day from todays date in YYYY-MM format
316 ### IN : $Type : may be 'month' or 'day'
317 ### OUT: last month/day as YYYY-MM(-DD)
319 my ($Day,$Month,$Year);
320 if ($Type eq 'day') {
321 # get yesterdays's date
322 (undef,undef,undef,$Day,$Month,$Year,undef,undef,undef) = localtime(time-86400);
323 # $Month is defined from 0 to 11, so add 1
326 # get today's date (month and year)
327 (undef,undef,undef,undef,$Month,$Year,undef,undef,undef) = localtime(time);
328 # $Month is already defined from 0 to 11, so no need to decrease it by 1
335 # return last month / day
336 if ($Type eq 'day') {
337 return sprintf('%4d-%02d-%02d',$Year,$Month,$Day);
339 return sprintf('%4d-%02d',$Year,$Month);
343 ################################################################################
345 ################################################################################
346 ### check if input (in YYYY-MM(-DD) form) is a valid month / day;
347 ### otherwise, fix it
348 ### IN : $Type : may be 'month' or 'day'
349 ### @Period: array of month/day
350 ### OUT: @Period: a valid month/day
351 my ($Type,@Period) = @_;
352 foreach my $Period (@Period) {
353 my ($OldPeriod) = $Period;
354 my ($CalMonth,$CalDay);
355 $Period .= '-01' if ($Type eq 'month');
356 $CalDay = substr ($Period, -2);
357 $CalMonth = substr ($Period, 5, 2);
358 if ($CalMonth < 1 or $CalMonth > 12 or $CalDay < 1 or $CalDay > 31) {
359 $CalMonth = '12' if $CalMonth > 12;
360 $CalMonth = '01' if $CalMonth < 1;
361 substr($Period, 5, 2) = $CalMonth;
362 $CalDay = '01' if $CalDay < 1;
363 $CalDay = '31' if $CalDay > 31;
364 # FIXME! - month with less than 31 days ...
365 substr($Period, -2) = $CalDay;
366 &Bleat(1,sprintf("'%s' is an invalid date, set to '%s'.",
367 $OldPeriod,$Period));
369 $Period = substr($Period,0,7) if ($Type eq 'month');
374 ################################################################################
376 ################################################################################
377 ### split a time period denoted by YYYY-MM(-DD):YYYY-MM(-DD) into start and end
378 ### IN : $Period: time period
379 ### $Type : may be 'month' or 'day'
380 ### OUT: $StartTime, $EndTime
381 my ($Period,$Type) = @_;
382 my ($StartTime, $EndTime) = split /:/, $Period;
383 ($StartTime,$EndTime) = CheckPeriod($Type,$StartTime,$EndTime);
384 # switch parameters as necessary
385 if ($EndTime gt $StartTime) {
386 return ($StartTime, $EndTime);
388 return ($EndTime, $StartTime);
392 ################################################################################
394 ################################################################################
395 ### return a list of months (YYYY-MM) between start and end month
396 ### IN : $MonthExpression ('YYYY-MM' or 'YYYY-MM to YYYY-MM')
397 ### OUT: @Months: array containing all months from $MonthExpression enumerated
398 my ($MonthExpression )= @_;
399 # return if single month
400 return ($MonthExpression) if ($MonthExpression =~ /^\d{4}-\d{2}$/);
401 # parse $MonthExpression
402 my ($StartMonth, $EndMonth) = split(' to ',$MonthExpression);
403 # set $Year, $Month from $StartMonth
404 my ($Year, $Month) = split /-/, $StartMonth;
407 until ("$Year-$Month" gt $EndMonth) {
408 push @Months, "$Year-$Month";
409 $Month = "$Month"; # force string context
419 #####---------------------------- OutputFormats ---------------------------#####
421 ################################################################################
423 ################################################################################
424 ### read database query results from DBHandle and print results with formatting
425 ### IN : $Format : format specifier
426 ### $Comments : print or suppress all comments for machine-readable output
427 ### $GroupBy : primary sorting order (month or key)
428 ### $Precision: number of digits right of decimal point (0 or 2)
429 ### $ValidKeys: reference to a hash containing all valid keys
430 ### $FileTempl: file name template (--filetemplate): filetempl-YYYY-MM
431 ### $DBQuery : database query handle with executed query,
432 ### containing $Month, $Key, $Value
433 ### $PadField : padding length for key field (optional) for 'pretty'
434 ### $PadValue : padding length for value field (optional) for 'pretty'
435 my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $FileTempl,
436 $DBQuery, $PadField, $PadValue) = @_;
437 my %ValidKeys = %{$ValidKeys} if $ValidKeys;
438 my ($FileName, $Handle, $OUT);
441 # define output types
443 @LegalOutput{('dump','list','pretty')} = ();
444 # bail out if format is unknown
445 &Bleat(2,"Unknown output type '$Format'!") if !exists($LegalOutput{$Format});
447 while (my ($Month, $Key, $Value) = $DBQuery->fetchrow_array) {
448 # don't display invalid keys
449 if(%ValidKeys and !defined($ValidKeys{$Key})) {
451 # &Bleat(1,sprintf("DROPPED: %s",$Key));
454 # care for correct sorting order and abstract from month and keys:
455 # $Caption will be $Month or $Key, according to sorting order,
456 # and $Key will be $Key or $Month, respectively
458 if ($GroupBy eq 'key') {
464 # set output file handle
466 $Handle = *STDOUT{IO}; # set $Handle to a reference to STDOUT
467 } elsif (!defined($LastIteration) or $LastIteration ne $Caption) {
468 close $OUT if ($LastIteration);
469 # safeguards for filename creation:
470 # replace potential problem characters with '_'
471 $FileName = sprintf('%s-%s',$FileTempl,$Caption);
472 $FileName =~ s/[^a-zA-Z0-9_-]+/_/g;
473 open ($OUT,">$FileName")
474 or &Bleat(2,sprintf("Cannot open output file '%s': $!",
478 print $Handle &FormatOutput($Format, $Comments, $Caption, $Key, $Value,
479 $Precision, $PadField, $PadValue);
480 $LastIteration = $Caption;
482 close $OUT if ($FileTempl);
485 ################################################################################
487 ################################################################################
488 ### format information for output according to format specifier
489 ### IN : $Format : format specifier
490 ### $Comments : print or suppress all comments for machine-readable output
491 ### $Caption : month (as YYYY-MM) or $Key, according to sorting order
492 ### $Key : newsgroup, client, ... or $Month, as above
493 ### $Value : number of postings with that attribute
494 ### $Precision: number of digits right of decimal point (0 or 2)
495 ### $PadField : padding length for key field (optional) for 'pretty'
496 ### $PadValue : padding length for value field (optional) for 'pretty'
497 ### OUT: $Output: formatted output
498 my ($Format, $Comments, $Caption, $Key, $Value, $Precision, $PadField,
501 # keep last caption in mind
502 our ($LastIteration);
503 # create one line of output
504 if ($Format eq 'dump') {
505 # output as dump (key value)
506 $Output = sprintf ("# %s:\n",$Caption)
507 if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration));
508 $Output .= sprintf ("%s %u\n",$Key,$Value);
509 } elsif ($Format eq 'list') {
510 # output as list (caption key value)
511 $Output = sprintf ("%s %s %u\n",$Caption,$Key,$Value);
512 } elsif ($Format eq 'pretty') {
514 $Output = sprintf ("# ----- %s:\n",$Caption)
515 if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration));
516 # increase $PadValue for numbers with decimal point
517 $PadValue += $Precision+1 if $Precision;
518 # add padding if $PadField is set; $PadValue HAS to be set then
519 $Output .= sprintf ($PadField ?
520 sprintf("%%-%us%%s %%%u.*f\n",$PadField,$PadValue) :
521 "%s%s %.*f\n",$Key,$Comments ? ':' : '',
527 #####------------------------- QueryModifications -------------------------#####
529 ################################################################################
531 ################################################################################
532 ### add exclusion of hierarchy levels (de.alt.ALL) from SQL query by
533 ### amending the WHERE clause if $ShowHierarchies is false (or don't, if it is
534 ### true, accordingly)
535 ### IN : $ShowHierarchies: boolean value
537 my ($ShowHierarchies) = @_;
538 return $ShowHierarchies ? '' : "newsgroup NOT LIKE '%.ALL'";
541 ################################################################################
543 ################################################################################
544 ### get length of longest fields in future query result
545 ### IN : $DBHandle : database handle
546 ### $Table : table to query
547 ### $Field : field (key!, i.e. month, newsgroup, ...) to check
548 ### $Value : field (value!, i.e. postings) to check
549 ### $WhereClause : WHERE clause
550 ### $HavingClause: HAVING clause
551 ### @BindVars : bind variables for WHERE clause
552 ### OUT: $FieldLength : length of longest instance of $Field
553 ### $ValueLength : length of longest instance of $Value
554 my ($DBHandle,$Table,$Field,$Value,$WhereClause,$HavingClause,@BindVars) = @_;
555 my $DBQuery = $DBHandle->prepare(sprintf("SELECT MAX(LENGTH(%s)),".
557 "FROM %s %s %s",$Field,,$Value,
558 $Table,$WhereClause,$HavingClause ?
559 'GROUP BY newsgroup' . $HavingClause .
560 ' ORDER BY LENGTH(newsgroup) '.
561 'DESC LIMIT 1': ''));
562 $DBQuery->execute(@BindVars) or &Bleat(1,sprintf("Can't get field length ".
563 "for '%s' from table '%s': ".
564 "$DBI::errstr",$Field,$Table));
565 my ($FieldLength,$ValueMax) = $DBQuery->fetchrow_array;
566 my $ValueLength = length($ValueMax) if ($ValueMax);
567 return ($FieldLength,$ValueLength);
570 ################################################################################
572 ################################################################################
573 ### build a SQL 'ORDER BY' clause from $OptGroupBy (primary sorting) and
574 ### $OptOrderBy (secondary sorting), both ascending or descending;
575 ### descending sorting order is done by adding '-desc'
576 ### IN : $GroupBy: primary sort by 'month' (default) or 'newsgroups'
577 ### $OrderBy: secondary sort by month/newsgroups (default)
578 ### or number of 'postings'
579 ### OUT: a SQL ORDER BY clause
580 my ($GroupBy,$OrderBy) = @_;
581 my ($GroupSort,$OrderSort) = ('','');
582 # $GroupBy (primary sorting)
586 ($GroupBy, $GroupSort) = SQLParseOrder($GroupBy);
587 if ($GroupBy =~ /group/i) {
588 $GroupBy = 'newsgroup';
593 my $Secondary = ($GroupBy eq 'month') ? 'newsgroup' : 'month';
594 # $OrderBy (secondary sorting)
596 $OrderBy = $Secondary;
598 ($OrderBy, $OrderSort) = SQLParseOrder($OrderBy);
599 if ($OrderBy =~ /posting/i) {
600 $OrderBy = "postings $OrderSort, $Secondary";
602 $OrderBy = "$Secondary $OrderSort";
605 return ($GroupBy,&SQLBuildClause('order',"$GroupBy $GroupSort",$OrderBy));
608 ################################################################################
610 ################################################################################
611 ### parse $OptGroupBy or $OptOrderBy option of the form param[-desc], e.g.
612 ### 'month', 'month-desc', 'newsgroups-desc', but also just 'desc'
613 ### IN : $OrderOption: order option (see above)
614 ### OUT: parameter to sort by,
615 ### sort order ('DESC' or nothing, meaning 'ASC')
616 my ($OrderOption) = @_;
618 if ($OrderOption =~ s/-?desc$//i) {
621 $OrderOption =~ s/-?asc$//i
623 return ($OrderOption,$SortOrder);
626 ################################################################################
628 ################################################################################
629 ### explode list of newsgroups separated by : (with wildcards)
630 ### to a SQL 'WHERE' expression
631 ### IN : $Newsgroups: list of newsgroups (group.one.*:group.two:group.three.*)
632 ### OUT: SQL code to become part of a 'WHERE' clause,
633 ### list of newsgroups for SQL bindings
634 my ($Newsgroups) = @_;
635 # substitute '*' wildcard with SQL wildcard character '%'
636 $Newsgroups =~ s/\*/%/g;
637 return (undef,undef) if !CheckValidNewsgroups($Newsgroups);
638 # just one newsgroup?
639 return (SQLGroupWildcard($Newsgroups),$Newsgroups) if $Newsgroups !~ /:/;
640 my ($SQL,@WildcardGroups,@NoWildcardGroups);
641 # list of newsgroups separated by ':'
642 my @GroupList = split /:/, $Newsgroups;
643 foreach (@GroupList) {
645 # add to list of newsgroup names WITHOUT wildcard
646 push (@NoWildcardGroups,$_);
648 # add to list of newsgroup names WITH wildcard
649 push (@WildcardGroups,$_);
650 # add wildcard to SQL clause
651 # 'OR' if SQL clause is not empty
652 $SQL .= ' OR ' if $SQL;
653 $SQL .= 'newsgroup LIKE ?'
656 if (scalar(@NoWildcardGroups)) {
657 # add 'OR' if SQL clause is not empty
658 $SQL .= ' OR ' if $SQL;
659 if (scalar(@NoWildcardGroups) < 2) {
660 # special case: just one newsgroup without wildcard
661 $SQL .= 'newsgroup = ?';
663 # create list of newsgroups to include: 'newsgroup IN (...)'
664 $SQL .= 'newsgroup IN (';
666 foreach (@NoWildcardGroups) {
667 $SQLin .= ',' if $SQLin;
670 # add list to SQL clause
671 $SQL .= $SQLin .= ')';
674 # add brackets '()' to SQL clause as needed (more than one wildcard group)
675 if (scalar(@WildcardGroups)) {
676 $SQL = '(' . $SQL .')';
678 # rebuild @GroupList in (now) correct order
679 @GroupList = (@WildcardGroups,@NoWildcardGroups);
680 return ($SQL,@GroupList);
683 ################################################################################
684 sub SQLGroupWildcard {
685 ################################################################################
686 ### build a valid SQL 'WHERE' expression with or without wildcards
687 ### IN : $Newsgroup: newsgroup expression, probably with wildcard
688 ### (group.name or group.name.%)
689 ### OUT: SQL code to become part of a 'WHERE' clause
690 my ($Newsgroup) = @_;
691 if ($Newsgroup !~ /%/) {
692 return 'newsgroup = ?';
694 return 'newsgroup LIKE ?';
698 ################################################################################
700 ################################################################################
701 ### set upper and/or lower boundary (number of postings)
702 ### IN : $Type: 'level', 'average', 'sum' or 'default'
703 ### $LowBound,$UppBound: lower/upper boundary, respectively
704 ### OUT: SQL code to become part of a WHERE or HAVING clause
705 my ($Type,$LowBound,$UppBound) = @_;
706 ($LowBound,$UppBound) = SQLCheckNumber($LowBound,$UppBound);
707 if($LowBound and $UppBound and $LowBound > $UppBound) {
708 &Bleat(1,"Lower boundary $LowBound is larger than Upper boundary ".
709 "$UppBound, exchanging boundaries.");
710 ($LowBound,$UppBound) = ($UppBound,$LowBound);
712 # default to 'default'
713 my $WhereHavingFunction = 'postings';
714 # set $LowBound to SQL statement:
715 # 'WHERE postings >=', 'HAVING MIN(postings) >=' or 'HAVING AVG(postings) >='
716 if ($Type eq 'level') {
717 $WhereHavingFunction = 'MIN(postings)'
718 } elsif ($Type eq 'average') {
719 $WhereHavingFunction = 'AVG(postings)'
720 } elsif ($Type eq 'sum') {
721 $WhereHavingFunction = 'SUM(postings)'
723 $LowBound = sprintf('%s >= '.$LowBound,$WhereHavingFunction) if ($LowBound);
724 # set $LowBound to SQL statement:
725 # 'WHERE postings <=', 'HAVING MAX(postings) <=' or 'HAVING AVG(postings) <='
726 if ($Type eq 'level') {
727 $WhereHavingFunction = 'MAX(postings)'
728 } elsif ($Type eq 'average') {
729 $WhereHavingFunction = 'AVG(postings)'
730 } elsif ($Type eq 'sum') {
731 $WhereHavingFunction = 'SUM(postings)'
733 $UppBound = sprintf('%s <= '.$UppBound,$WhereHavingFunction) if ($UppBound);
734 return ($LowBound,$UppBound);
737 ################################################################################
739 ################################################################################
740 ### check if input is a valid positive integer; otherwise, make it one
741 ### IN : @Numbers: array of parameters
742 ### OUT: @Numbers: a valid positive integer
744 foreach my $Number (@Numbers) {
745 if ($Number and $Number < 0) {
746 &Bleat(1,"Boundary $Number is < 0, set to ".-$Number);
749 $Number = '' if ($Number and $Number !~ /^\d+$/);
754 ################################################################################
756 ################################################################################
757 ### build a valid SQL WHERE, GROUP BY, ORDER BY or HAVING clause
758 ### from multiple expressions which *may* be empty
759 ### IN : $Type: 'where', 'having', 'group' or 'order'
760 ### @Expressions: array of expressions
761 ### OUT: $SQLClause: a SQL clause
762 my ($Type,@Expressions) = @_;
763 my ($SQLClause,$Separator,$Statement);
764 # set separator ('AND' or ',')
765 if ($Type eq 'where' or $Type eq 'having') {
771 if ($Type eq 'where') {
772 $Statement = 'WHERE';
773 } elsif ($Type eq 'order') {
774 $Statement = 'ORDER BY';
775 } elsif ($Type eq 'having') {
776 $Statement = 'HAVING';
778 $Statement = 'GROUP BY';
780 # build query from expressions with separators
781 foreach my $Expression (@Expressions) {
783 $SQLClause .= " $Separator " if ($SQLClause);
784 $SQLClause .= $Expression;
787 # add statement in front if not already present
788 $SQLClause = " $Statement " . $SQLClause
789 if ($SQLClause and $SQLClause !~ /$Statement/);
793 #####--------------------------- Verifications ----------------------------#####
795 ################################################################################
796 sub CheckValidNewsgroups {
797 ################################################################################
798 ### syntax check of newgroup list
799 ### IN : $Newsgroups: list of newsgroups (group.one.*:group.two:group.three.*)
801 my ($Newsgroups) = @_;
802 my $InvalidCharRegExp = ',; ';
803 return ($Newsgroups =~ /[$InvalidCharRegExp]/) ? 0 : 1;
807 #####------------------------------- done ---------------------------------#####