5 # This script will create database tables as necessary.
7 # It is part of the NewsStats package.
9 # Copyright (c) 2010-2013 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.02";
17 # we're in .../install, so our module is in ../lib
18 push(@INC, dirname($0).'/../lib');
23 use NewsStats qw(:DEFAULT);
26 use Getopt::Long qw(GetOptions);
27 Getopt::Long::config ('bundling');
29 ################################# Main program #################################
31 ### read commandline options
32 my ($OptUpdate,$OptConfFile);
33 GetOptions ('u|update=s' => \$OptUpdate,
34 'conffile=s' => \$OptConfFile,
35 'h|help' => \&ShowPOD,
36 'V|version' => \&ShowVersion) or exit 1;
38 ### read configuration
39 print("Reading configuration.\n");
40 my %Conf = %{ReadConfig($OptConfFile)};
42 ##### --------------------------------------------------------------------------
43 ##### Database table definitions
44 ##### --------------------------------------------------------------------------
46 my $DBCreate = <<SQLDB;
47 CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8;
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:$HomePath/bin/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 ### create DB, if necessary
155 print "----------\nStarting database creation.\n";
157 # we can't use InitDB() as that will use a table name of
158 # the table that doesn't exist yet ...
159 my $DBHandle = DBI->connect(sprintf('DBI:%s:host=%s',$Conf{'DBDriver'},
160 $Conf{'DBHost'}), $Conf{'DBUser'},
161 $Conf{'DBPw'}, { PrintError => 0 });
162 my $DBQuery = $DBHandle->prepare($DBCreate);
163 $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n",
164 $Conf{'DBDatabase'}, $DBI::errstr));
166 printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'});
167 $DBHandle->disconnect;
170 ### DB init, read list of tables
171 print "Reading database information.\n";
172 my $DBHandle = InitDB(\%Conf,1);
174 %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
177 ##### installation mode
178 # check for tables and create them, if they don't exist yet
179 foreach my $Table (keys %DBCreate) {
180 &CreateTable($Table);
182 print "Database table generation done.\n";
184 # Display install instructions
188 print "----------\nStarting upgrade process.\n";
189 $PackageVersion = '0.03';
190 if ($OptUpdate < $PackageVersion) {
191 if ($OptUpdate < 0.02) {
194 # print "v0.02: Database upgrades ...\n";
195 # &PrintInstructions('0.02',<<" INSTRUCTIONS");
199 # Display general upgrade instructions
204 $DBHandle->disconnect;
208 ################################# Subroutines ##################################
212 if (defined($TablesInDB{$Conf{$Table}})) {
213 printf("Database table %s.%s already exists, skipping ....\n",
214 $Conf{'DBDatabase'},$Conf{$Table});
217 my $DBQuery = $DBHandle->prepare($DBCreate{$Table});
218 $DBQuery->execute() or
219 &Bleat(2, sprintf("Can't create table %s in database %s: %s%\n",$Table,
220 $Conf{'DBDatabase'},$DBI::errstr));
221 printf("Database table %s.%s created succesfully.\n",
222 $Conf{'DBDatabase'},$Conf{$Table});
228 my $DBQuery = $DBHandle->prepare($SQL);
229 $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr));
233 sub PrintInstructions {
234 my ($UpVersion,$Instructions) = @_;
235 print "v$UpVersion: Upgrade Instructions >>>>>\n";
236 my $Padding = ' ' x (length($UpVersion) + 3);
237 $Instructions =~ s/^ /$Padding/mg;
239 print "<" x (length($UpVersion) + 29) . "\n";
245 ################################ Documentation #################################
249 install - installation script
253 B<install> [B<-Vh> [--update I<version>] [B<--conffile> I<filename>]
261 This script will create database tables as necessary and configured.
265 B<install> will read its configuration from F<newsstats.conf> which should
266 be present in etc/ via Config::Auto or from a configuration file submitted
267 by the B<--conffile> option.
269 See L<doc/INSTALL> for an overview of possible configuration options.
275 =item B<-V>, B<--version>
277 Print out version and copyright information and exit.
279 =item B<-h>, B<--help>
281 Print this man page and exit.
283 =item B<-u>, B<--update> I<version>
285 Don't do a fresh install, but update from I<version>.
287 =item B<--conffile> I<filename>
289 Load configuration from I<filename> instead of F<newsstats.conf>.
297 =item F<install/install.pl>
301 =item F<lib/NewsStats.pm>
303 Library functions for the NewsStats package.
305 =item F<etc/newsstats.conf>
307 Runtime configuration file.
313 Please report any bugs or feature requests to the author or use the
314 bug tracker at L<http://bugs.th-h.de/>!
330 This script is part of the B<NewsStats> package.
334 Thomas Hochstein <thh@inter.net>
336 =head1 COPYRIGHT AND LICENSE
338 Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net>
340 This program is free software; you may redistribute it and/or modify it
341 under the same terms as Perl itself.