5 # This script will create database tables as necessary.
7 # It is part of the NewsStats package.
9 # Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
11 # It can be redistributed and/or modified under the same terms under
12 # which Perl itself is published.
15 our $VERSION = "0.01";
17 # we're in .../install, so our module is in ..
18 push(@INC, dirname($0).'/..');
22 use NewsStats qw(:DEFAULT);
27 use Getopt::Long qw(GetOptions);
28 Getopt::Long::config ('bundling');
30 ################################# Main program #################################
32 ### read commandline options
34 GetOptions ('u|update=s' => \$OptUpdate,
35 'h|help' => \&ShowPOD,
36 'V|version' => \&ShowVersion) or exit 1;
38 ### change working directory to .. (as we're in .../install)
39 chdir dirname($0).'/..';
42 ### read configuration
43 print("Reading configuration.\n");
44 my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')};
46 ##### --------------------------------------------------------------------------
47 ##### Database table definitions
48 ##### --------------------------------------------------------------------------
50 my %DBCreate = ('DBTableRaw' => <<RAW, 'DBTableGrps' => <<GRPS);
52 -- Table structure for table DBTableRaw
55 CREATE TABLE IF NOT EXISTS `$Conf{'DBTableRaw'}` (
56 `id` bigint(20) unsigned NOT NULL auto_increment,
58 `mid` varchar(250) character set ascii NOT NULL,
59 `date` datetime NOT NULL,
60 `timestamp` bigint(20) NOT NULL,
61 `token` varchar(80) character set ascii NOT NULL,
62 `size` bigint(20) NOT NULL,
63 `peer` varchar(250) NOT NULL,
64 `path` varchar(1000) NOT NULL,
65 `newsgroups` varchar(1000) NOT NULL,
66 `headers` longtext NOT NULL,
67 `disregard` tinyint(1) default '0',
72 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Raw data';
75 -- Table structure for table DBTableGrps
78 CREATE TABLE IF NOT EXISTS `$Conf{'DBTableGrps'}` (
79 `id` bigint(20) unsigned NOT NULL auto_increment,
80 `month` varchar(7) character set ascii NOT NULL,
81 `newsgroup` varchar(100) NOT NULL,
82 `postings` int(11) NOT NULL,
83 `revision` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
85 UNIQUE KEY `month_newsgroup` (`month`,`newsgroup`),
86 KEY `newsgroup` (`newsgroup`),
87 KEY `postings` (`postings`)
88 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Postings per newsgroup';
91 ##### --------------------------------------------------------------------------
92 ##### Installation / upgrade instructions
93 ##### --------------------------------------------------------------------------
95 my $Install = <<INSTALL;
99 1) Setup an INN feed to feedlog.pl
101 a) Edit your 'newsfeeds' file and insert something like
103 ## gather statistics for NewsStats
106 :Tc,WmtfbsPNH,Ac:$Path/feedlog.pl
110 * check that you got the path to feedlog.pl right
111 * check that feedlog.pl can be executed by the news user
112 * adapt the pattern (here: 'de.*') to your needs
114 b) Check your 'newsfeeds' syntax:
118 and reload 'newsfeeds':
120 # ctlinnd reload newsfeeds 'Adding newsstats! feed'
122 c) Watch your 'news.notice' and 'errlog' files:
124 # tail -f /var/log/news/news.notice
126 # tail -f /var/log/news/errlog
128 2) Watch your $Conf{'DBTableRaw'} table fill.
130 3) Read the documentation. ;)
134 -thh <thh\@inter.net>
139 $Upgrade = <<UPGRADE;
141 Your installation was upgraded from $OptUpdate to $PackageVersion.
143 Don't forget to restart your INN feed so that it can pick up the new version:
145 # ctlinnd begin 'newsstats!'
147 (or whatever you called your feed).
151 ##### --------------------------- End of definitions ---------------------------
153 ### DB init, read list of tables
154 print "Reading database information.\n";
155 my $DBHandle = InitDB(\%Conf,1);
157 %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
160 ##### installation mode
161 print "----------\nStarting database table generation.\n";
162 # check for tables and create them, if they don't exist yet
163 foreach my $Table (keys %DBCreate) {
164 &CreateTable($Table);
166 print "Database table generation done.\n";
168 # Display install instructions
172 print "----------\nStarting upgrade process.\n";
173 $PackageVersion = '0.03';
174 if ($OptUpdate < $PackageVersion) {
175 if ($OptUpdate < 0.02) {
178 # print "v0.02: Database upgrades ...\n";
179 # &PrintInstructions('0.02',<<" INSTRUCTIONS");
183 # Display general upgrade instructions
188 $DBHandle->disconnect;
192 ################################# Subroutines ##################################
196 if (defined($TablesInDB{$Conf{$Table}})) {
197 printf("Database table %s.%s already exists, skipping ....\n",
198 $Conf{'DBDatabase'},$Conf{$Table});
201 my $DBQuery = $DBHandle->prepare($DBCreate{$Table});
202 $DBQuery->execute() or
203 &Bleat(2, sprintf("Can't create table %s in database %s: %s%\n",$Table,
204 $Conf{'DBDatabase'},$DBI::errstr));
205 printf("Database table %s.%s created succesfully.\n",
206 $Conf{'DBDatabase'},$Conf{$Table});
212 my $DBQuery = $DBHandle->prepare($SQL);
213 $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr));
217 sub PrintInstructions {
218 my ($UpVersion,$Instructions) = @_;
219 print "v$UpVersion: Upgrade Instructions >>>>>\n";
220 my $Padding = ' ' x (length($UpVersion) + 3);
221 $Instructions =~ s/^ /$Padding/mg;
223 print "<" x (length($UpVersion) + 29) . "\n";
229 ################################ Documentation #################################
233 install - installation script
237 B<install> [B<-Vh> [--update I<version>]
245 This script will create database tables as necessary and configured.
249 B<install> will read its configuration from F<newsstats.conf> via
252 See L<doc/INSTALL> for an overview of possible configuration options.
258 =item B<-V>, B<--version>
260 Print out version and copyright information and exit.
262 =item B<-h>, B<--help>
264 Print this man page and exit.
266 =item B<-u>, B<--update> I<version>
268 Don't do a fresh install, but update from I<version>.
280 =item F<NewsStats.pm>
282 Library functions for the NewsStats package.
284 =item F<newsstats.conf>
286 Runtime configuration file.
292 Please report any bugs or feature requests to the author or use the
293 bug tracker at L<http://bugs.th-h.de/>!
309 This script is part of the B<NewsStats> package.
313 Thomas Hochstein <thh@inter.net>
315 =head1 COPYRIGHT AND LICENSE
317 Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
319 This program is free software; you may redistribute it and/or modify it
320 under the same terms as Perl itself.