4069bf5aa4cddec305f8487a45c9ed02b58d4fc2
[usenet/newsstats.git] / install.pl
1 #! /usr/bin/perl -W
2 #
3 # install.pl
4 #
5 # This script will create database tables as necessary.
6
7 # It is part of the NewsStats package.
8 #
9 # Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
10 #
11 # It can be redistributed and/or modified under the same terms under 
12 # which Perl itself is published.
13
14 BEGIN {
15   our $VERSION = "0.01";
16   use File::Basename;
17   # we're in .../install, so our module is in ..
18   push(@INC, dirname($0).'/..');
19 }
20 use strict;
21
22 use NewsStats qw(:DEFAULT);
23
24 use Cwd;
25
26 use DBI;
27 use Getopt::Long qw(GetOptions);
28 Getopt::Long::config ('bundling');
29
30 ################################# Main program #################################
31
32 ### read commandline options
33 my ($OptUpdate);
34 GetOptions ('u|update=s' => \$OptUpdate,
35             'h|help'     => \&ShowPOD,
36             'V|version'  => \&ShowVersion) or exit 1;
37
38 ### change working directory to .. (as we're in .../install)
39 chdir dirname($FullPath).'/..';
40 my $Path = cwd();
41
42 ### read configuration
43 print("Reading configuration.\n");
44 my %Conf = %{ReadConfig($Path.'/newsstats.conf')};
45
46 ##### --------------------------------------------------------------------------
47 ##### Database table definitions
48 ##### --------------------------------------------------------------------------
49
50 my $DBCreate = <<SQLDB;
51 CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8;
52 SQLDB
53
54 my %DBCreate = ('DBTableRaw'  => <<RAW, 'DBTableGrps' => <<GRPS);
55 -- 
56 -- Table structure for table DBTableRaw
57 -- 
58
59 CREATE TABLE IF NOT EXISTS `$Conf{'DBTableRaw'}` (
60   `id` bigint(20) unsigned NOT NULL auto_increment,
61   `day` date NOT NULL,
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',
72   PRIMARY KEY  (`id`),
73   KEY `day` (`day`),
74   KEY `mid` (`mid`),
75   KEY `peer` (`peer`)
76 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='Raw data';
77 RAW
78 -- 
79 -- Table structure for table DBTableGrps
80 -- 
81
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,
88   PRIMARY KEY  (`id`),
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';
93 GRPS
94
95 ##### --------------------------------------------------------------------------
96 ##### Installation / upgrade instructions
97 ##### --------------------------------------------------------------------------
98
99 my $Install = <<INSTALL;
100 ----------
101 Things left to do:
102
103 1) Setup an INN feed to feedlog.pl
104
105    a) Edit your 'newsfeeds' file and insert something like
106
107           ## gather statistics for NewsStats
108           newsstats!\\
109                   :!*,de.*\\
110                   :Tc,WmtfbsPNH,Ac:$Path/feedlog.pl
111
112       Please
113
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
117
118    b) Check your 'newsfeeds' syntax:
119
120          # ctlinnd checkfile
121
122       and reload 'newsfeeds':
123
124          # ctlinnd reload newsfeeds 'Adding newsstats! feed'
125
126    c) Watch your 'news.notice' and 'errlog' files:
127
128          # tail -f /var/log/news/news.notice
129          ...
130          # tail -f /var/log/news/errlog
131
132 2) Watch your $Conf{'DBTableRaw'} table fill.
133
134 3) Read the documentation. ;)
135
136 Enjoy!
137
138 -thh <thh\@inter.net>
139 INSTALL
140
141 my $Upgrade ='';
142 if ($OptUpdate) {
143  $Upgrade = <<UPGRADE;
144 ----------
145 Your installation was upgraded from $OptUpdate to $PackageVersion.
146
147 Don't forget to restart your INN feed so that it can pick up the new version:
148
149    # ctlinnd begin 'newsstats!'
150
151 (or whatever you called your feed).
152 UPGRADE
153 }
154
155 ##### --------------------------- End of definitions ---------------------------
156
157 ### create DB, if necessary
158 if (!$OptUpdate) {
159   print "----------\nStarting database creation.\n";
160   # create database
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));
169   
170   printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'});
171   $DBHandle->disconnect;
172 };
173
174 ### DB init, read list of tables
175 print "Reading database information.\n";
176 my $DBHandle = InitDB(\%Conf,1);
177 my %TablesInDB =
178    %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
179
180 if (!$OptUpdate) {
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);
185   };
186   print "Database table generation done.\n";
187  
188   # Display install instructions
189   print $Install;
190 } else {
191   ##### upgrade mode
192   print "----------\nStarting upgrade process.\n";
193   $PackageVersion = '0.03';
194   if ($OptUpdate < $PackageVersion) {
195     if ($OptUpdate < 0.02) {
196       # 0.01 -> 0.02
197       # &DoMySQL('...;');
198       # print "v0.02: Database upgrades ...\n";
199       # &PrintInstructions('0.02',<<"      INSTRUCTIONS");
200       # INSTRUCTIONS
201     };
202   };
203   # Display general upgrade instructions
204   print $Upgrade;
205 };
206
207 # close handle
208 $DBHandle->disconnect;
209
210 exit(0);
211
212 ################################# Subroutines ##################################
213
214 sub CreateTable {
215   my $Table = shift;
216   if (defined($TablesInDB{$Conf{$Table}})) {
217     printf("Database table %s.%s already exists, skipping ....\n",
218            $Conf{'DBDatabase'},$Conf{$Table});
219     return;
220   };
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});
227   return;
228 };
229
230 sub DoMySQL {
231   my $SQL = shift;
232   my $DBQuery = $DBHandle->prepare($SQL);
233   $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr));
234   return;
235 };
236
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;
242     print $Instructions;
243     print "<" x (length($UpVersion) + 29) . "\n";
244 };
245
246
247 __END__
248
249 ################################ Documentation #################################
250
251 =head1 NAME
252
253 install - installation script
254
255 =head1 SYNOPSIS
256
257 B<install> [B<-Vh> [--update I<version>]
258
259 =head1 REQUIREMENTS
260
261 See L<doc/README>.
262
263 =head1 DESCRIPTION
264
265 This script will create database tables as necessary and configured.
266
267 =head2 Configuration
268
269 B<install> will read its configuration from F<newsstats.conf> via
270 Config::Auto.
271
272 See L<doc/INSTALL> for an overview of possible configuration options.
273
274 =head1 OPTIONS
275
276 =over 3
277
278 =item B<-V>, B<--version>
279
280 Print out version and copyright information and exit.
281
282 =item B<-h>, B<--help>
283
284 Print this man page and exit.
285
286 =item B<-u>, B<--update> I<version>
287
288 Don't do a fresh install, but update from I<version>.
289
290 =back
291
292 =head1 FILES
293
294 =over 4
295
296 =item F<install.pl>
297
298 The script itself.
299
300 =item F<NewsStats.pm>
301
302 Library functions for the NewsStats package.
303
304 =item F<newsstats.conf>
305
306 Runtime configuration file.
307
308 =back
309
310 =head1 BUGS
311
312 Please report any bugs or feature requests to the author or use the
313 bug tracker at L<http://bugs.th-h.de/>!
314
315 =head1 SEE ALSO
316
317 =over 2
318
319 =item -
320
321 L<doc/README>
322
323 =item -
324
325 L<doc/INSTALL>
326
327 =back
328
329 This script is part of the B<NewsStats> package.
330
331 =head1 AUTHOR
332
333 Thomas Hochstein <thh@inter.net>
334
335 =head1 COPYRIGHT AND LICENSE
336
337 Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
338
339 This program is free software; you may redistribute it and/or modify it
340 under the same terms as Perl itself.
341
342 =cut
This page took 0.01672 seconds and 2 git commands to generate.