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.01";
17 # we're in .../install, so our module is in ../lib
18 push(@INC, dirname($0).'/../lib');
23 use NewsStats qw(:DEFAULT);
28 use Getopt::Long qw(GetOptions);
29 Getopt::Long::config ('bundling');
31 ################################# Main program #################################
33 ### read commandline options
35 GetOptions ('u|update=s' => \$OptUpdate,
36 'h|help' => \&ShowPOD,
37 'V|version' => \&ShowVersion) or exit 1;
39 ### change working directory to .. (as we're in .../install)
40 chdir dirname($FullPath).'/..';
43 ### read configuration
44 print("Reading configuration.\n");
45 my %Conf = %{ReadConfig('')};
47 ##### --------------------------------------------------------------------------
48 ##### Database table definitions
49 ##### --------------------------------------------------------------------------
51 my $DBCreate = <<SQLDB;
52 CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8;
55 my %DBCreate = ('DBTableRaw' => <<RAW, 'DBTableGrps' => <<GRPS);
57 -- Table structure for table DBTableRaw
60 CREATE TABLE IF NOT EXISTS `$Conf{'DBTableRaw'}` (
61 `id` bigint(20) unsigned NOT NULL auto_increment,
63 `mid` varchar(250) character set ascii NOT NULL,
64 `date` datetime NOT NULL,
65 `timestamp` bigint(20) NOT NULL,
66 `token` varchar(80) character set ascii NOT NULL,
67 `size` bigint(20) NOT NULL,
68 `peer` varchar(250) NOT NULL,
69 `path` varchar(1000) NOT NULL,
70 `newsgroups` varchar(1000) NOT NULL,
71 `headers` longtext NOT NULL,
72 `disregard` tinyint(1) default '0',
77 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Raw data';
80 -- Table structure for table DBTableGrps
83 CREATE TABLE IF NOT EXISTS `$Conf{'DBTableGrps'}` (
84 `id` bigint(20) unsigned NOT NULL auto_increment,
85 `month` varchar(7) character set ascii NOT NULL,
86 `newsgroup` varchar(100) NOT NULL,
87 `postings` int(11) NOT NULL,
88 `revision` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
90 UNIQUE KEY `month_newsgroup` (`month`,`newsgroup`),
91 KEY `newsgroup` (`newsgroup`),
92 KEY `postings` (`postings`)
93 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Postings per newsgroup';
96 ##### --------------------------------------------------------------------------
97 ##### Installation / upgrade instructions
98 ##### --------------------------------------------------------------------------
100 my $Install = <<INSTALL;
104 1) Setup an INN feed to feedlog.pl
106 a) Edit your 'newsfeeds' file and insert something like
108 ## gather statistics for NewsStats
111 :Tc,WmtfbsPNH,Ac:$Path/feedlog.pl
115 * check that you got the path to feedlog.pl right
116 * check that feedlog.pl can be executed by the news user
117 * adapt the pattern (here: 'de.*') to your needs
119 b) Check your 'newsfeeds' syntax:
123 and reload 'newsfeeds':
125 # ctlinnd reload newsfeeds 'Adding newsstats! feed'
127 c) Watch your 'news.notice' and 'errlog' files:
129 # tail -f /var/log/news/news.notice
131 # tail -f /var/log/news/errlog
133 2) Watch your $Conf{'DBTableRaw'} table fill.
135 3) Read the documentation. ;)
139 -thh <thh\@inter.net>
144 $Upgrade = <<UPGRADE;
146 Your installation was upgraded from $OptUpdate to $PackageVersion.
148 Don't forget to restart your INN feed so that it can pick up the new version:
150 # ctlinnd begin 'newsstats!'
152 (or whatever you called your feed).
156 ##### --------------------------- End of definitions ---------------------------
158 ### create DB, if necessary
160 print "----------\nStarting database creation.\n";
162 # we can't use InitDB() as that will use a table name of
163 # the table that doesn't exist yet ...
164 my $DBHandle = DBI->connect(sprintf('DBI:%s:host=%s',$Conf{'DBDriver'},
165 $Conf{'DBHost'}), $Conf{'DBUser'},
166 $Conf{'DBPw'}, { PrintError => 0 });
167 my $DBQuery = $DBHandle->prepare($DBCreate);
168 $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n",
169 $Conf{'DBDatabase'}, $DBI::errstr));
171 printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'});
172 $DBHandle->disconnect;
175 ### DB init, read list of tables
176 print "Reading database information.\n";
177 my $DBHandle = InitDB(\%Conf,1);
179 %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
182 ##### installation mode
183 # check for tables and create them, if they don't exist yet
184 foreach my $Table (keys %DBCreate) {
185 &CreateTable($Table);
187 print "Database table generation done.\n";
189 # Display install instructions
193 print "----------\nStarting upgrade process.\n";
194 $PackageVersion = '0.03';
195 if ($OptUpdate < $PackageVersion) {
196 if ($OptUpdate < 0.02) {
199 # print "v0.02: Database upgrades ...\n";
200 # &PrintInstructions('0.02',<<" INSTRUCTIONS");
204 # Display general upgrade instructions
209 $DBHandle->disconnect;
213 ################################# Subroutines ##################################
217 if (defined($TablesInDB{$Conf{$Table}})) {
218 printf("Database table %s.%s already exists, skipping ....\n",
219 $Conf{'DBDatabase'},$Conf{$Table});
222 my $DBQuery = $DBHandle->prepare($DBCreate{$Table});
223 $DBQuery->execute() or
224 &Bleat(2, sprintf("Can't create table %s in database %s: %s%\n",$Table,
225 $Conf{'DBDatabase'},$DBI::errstr));
226 printf("Database table %s.%s created succesfully.\n",
227 $Conf{'DBDatabase'},$Conf{$Table});
233 my $DBQuery = $DBHandle->prepare($SQL);
234 $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr));
238 sub PrintInstructions {
239 my ($UpVersion,$Instructions) = @_;
240 print "v$UpVersion: Upgrade Instructions >>>>>\n";
241 my $Padding = ' ' x (length($UpVersion) + 3);
242 $Instructions =~ s/^ /$Padding/mg;
244 print "<" x (length($UpVersion) + 29) . "\n";
250 ################################ Documentation #################################
254 install - installation script
258 B<install> [B<-Vh> [--update I<version>]
266 This script will create database tables as necessary and configured.
270 B<install> will read its configuration from F<newsstats.conf> via
273 See L<doc/INSTALL> for an overview of possible configuration options.
279 =item B<-V>, B<--version>
281 Print out version and copyright information and exit.
283 =item B<-h>, B<--help>
285 Print this man page and exit.
287 =item B<-u>, B<--update> I<version>
289 Don't do a fresh install, but update from I<version>.
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-2012 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.