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($FullPath).'/..';
42 ### read configuration
43 print("Reading configuration.\n");
44 my %Conf = %{ReadConfig($Path.'/newsstats.conf')};
46 ##### --------------------------------------------------------------------------
47 ##### Database table definitions
48 ##### --------------------------------------------------------------------------
50 my $DBCreate = <<SQLDB;
51 CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8;
54 my %DBCreate = ('DBTableRaw' => <<RAW, 'DBTableGrps' => <<GRPS);
56 -- Table structure for table DBTableRaw
59 CREATE TABLE IF NOT EXISTS `$Conf{'DBTableRaw'}` (
60 `id` bigint(20) unsigned NOT NULL auto_increment,
62 `mid` varchar(250) character set ascii NOT NULL,
63 `date` datetime NOT NULL,
64 `timestamp` bigint(20) NOT NULL,
65 `token` varchar(80) character set ascii NOT NULL,
66 `size` bigint(20) NOT NULL,
67 `peer` varchar(250) NOT NULL,
68 `path` varchar(1000) NOT NULL,
69 `newsgroups` varchar(1000) NOT NULL,
70 `headers` longtext NOT NULL,
71 `disregard` tinyint(1) default '0',
76 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Raw data';
79 -- Table structure for table DBTableGrps
82 CREATE TABLE IF NOT EXISTS `$Conf{'DBTableGrps'}` (
83 `id` bigint(20) unsigned NOT NULL auto_increment,
84 `month` varchar(7) character set ascii NOT NULL,
85 `newsgroup` varchar(100) NOT NULL,
86 `postings` int(11) NOT NULL,
87 `revision` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
89 UNIQUE KEY `month_newsgroup` (`month`,`newsgroup`),
90 KEY `newsgroup` (`newsgroup`),
91 KEY `postings` (`postings`)
92 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Postings per newsgroup';
95 ##### --------------------------------------------------------------------------
96 ##### Installation / upgrade instructions
97 ##### --------------------------------------------------------------------------
99 my $Install = <<INSTALL;
103 1) Setup an INN feed to feedlog.pl
105 a) Edit your 'newsfeeds' file and insert something like
107 ## gather statistics for NewsStats
110 :Tc,WmtfbsPNH,Ac:$Path/feedlog.pl
114 * check that you got the path to feedlog.pl right
115 * check that feedlog.pl can be executed by the news user
116 * adapt the pattern (here: 'de.*') to your needs
118 b) Check your 'newsfeeds' syntax:
122 and reload 'newsfeeds':
124 # ctlinnd reload newsfeeds 'Adding newsstats! feed'
126 c) Watch your 'news.notice' and 'errlog' files:
128 # tail -f /var/log/news/news.notice
130 # tail -f /var/log/news/errlog
132 2) Watch your $Conf{'DBTableRaw'} table fill.
134 3) Read the documentation. ;)
138 -thh <thh\@inter.net>
143 $Upgrade = <<UPGRADE;
145 Your installation was upgraded from $OptUpdate to $PackageVersion.
147 Don't forget to restart your INN feed so that it can pick up the new version:
149 # ctlinnd begin 'newsstats!'
151 (or whatever you called your feed).
155 ##### --------------------------- End of definitions ---------------------------
157 ### create DB, if necessary
159 print "----------\nStarting database creation.\n";
161 # we can't use InitDB() as that will use a table name of
162 # the table that doesn't exist yet ...
163 my $DBHandle = DBI->connect(sprintf('DBI:%s:host=%s',$Conf{'DBDriver'},
164 $Conf{'DBHost'}), $Conf{'DBUser'},
165 $Conf{'DBPw'}, { PrintError => 0 });
166 my $DBQuery = $DBHandle->prepare($DBCreate);
167 $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n",
168 $Conf{'DBDatabase'}, $DBI::errstr));
170 printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'});
171 $DBHandle->disconnect;
174 ### DB init, read list of tables
175 print "Reading database information.\n";
176 my $DBHandle = InitDB(\%Conf,1);
178 %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
181 ##### installation mode
182 # check for tables and create them, if they don't exist yet
183 foreach my $Table (keys %DBCreate) {
184 &CreateTable($Table);
186 print "Database table generation done.\n";
188 # Display install instructions
192 print "----------\nStarting upgrade process.\n";
193 $PackageVersion = '0.03';
194 if ($OptUpdate < $PackageVersion) {
195 if ($OptUpdate < 0.02) {
198 # print "v0.02: Database upgrades ...\n";
199 # &PrintInstructions('0.02',<<" INSTRUCTIONS");
203 # Display general upgrade instructions
208 $DBHandle->disconnect;
212 ################################# Subroutines ##################################
216 if (defined($TablesInDB{$Conf{$Table}})) {
217 printf("Database table %s.%s already exists, skipping ....\n",
218 $Conf{'DBDatabase'},$Conf{$Table});
221 my $DBQuery = $DBHandle->prepare($DBCreate{$Table});
222 $DBQuery->execute() or
223 &Bleat(2, sprintf("Can't create table %s in database %s: %s%\n",$Table,
224 $Conf{'DBDatabase'},$DBI::errstr));
225 printf("Database table %s.%s created succesfully.\n",
226 $Conf{'DBDatabase'},$Conf{$Table});
232 my $DBQuery = $DBHandle->prepare($SQL);
233 $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr));
237 sub PrintInstructions {
238 my ($UpVersion,$Instructions) = @_;
239 print "v$UpVersion: Upgrade Instructions >>>>>\n";
240 my $Padding = ' ' x (length($UpVersion) + 3);
241 $Instructions =~ s/^ /$Padding/mg;
243 print "<" x (length($UpVersion) + 29) . "\n";
249 ################################ Documentation #################################
253 install - installation script
257 B<install> [B<-Vh> [--update I<version>]
265 This script will create database tables as necessary and configured.
269 B<install> will read its configuration from F<newsstats.conf> via
272 See L<doc/INSTALL> for an overview of possible configuration options.
278 =item B<-V>, B<--version>
280 Print out version and copyright information and exit.
282 =item B<-h>, B<--help>
284 Print this man page and exit.
286 =item B<-u>, B<--update> I<version>
288 Don't do a fresh install, but update from I<version>.
300 =item F<NewsStats.pm>
302 Library functions for the NewsStats package.
304 =item F<newsstats.conf>
306 Runtime configuration file.
312 Please report any bugs or feature requests to the author or use the
313 bug tracker at L<http://bugs.th-h.de/>!
329 This script is part of the B<NewsStats> package.
333 Thomas Hochstein <thh@inter.net>
335 =head1 COPYRIGHT AND LICENSE
337 Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
339 This program is free software; you may redistribute it and/or modify it
340 under the same terms as Perl itself.