NewsStats.pm: Fix $VERSION and $PackageVersion.
[usenet/newsstats.git] / install / 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 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
28 ################################# Main program #################################
29
30 ### read commandline options
31 my %Options = &ReadOptions('u:');
32
33 ### change working directory to .. (as we're in .../install)
34 chdir dirname($0).'/..';
35 my $Path = cwd();
36
37 ### read configuration
38 print("Reading configuration.\n");
39 my %Conf = %{ReadConfig('newsstats.conf')};
40
41 ##### --------------------------------------------------------------------------
42 ##### Database table definitions
43 ##### --------------------------------------------------------------------------
44
45 my %DBCreate = ('DBTableRaw'  => <<RAW, 'DBTableGrps' => <<GRPS);
46 -- 
47 -- Table structure for table DBTableRaw
48 -- 
49
50 CREATE TABLE IF NOT EXISTS `$Conf{'DBTableRaw'}` (
51   `id` bigint(20) unsigned NOT NULL auto_increment,
52   `day` date NOT NULL,
53   `mid` varchar(250) character set ascii NOT NULL,
54   `date` datetime NOT NULL,
55   `timestamp` bigint(20) NOT NULL,
56   `token` varchar(80) character set ascii NOT NULL,
57   `size` bigint(20) NOT NULL,
58   `peer` varchar(250) NOT NULL,
59   `path` varchar(1000) NOT NULL,
60   `newsgroups` varchar(1000) NOT NULL,
61   `headers` longtext NOT NULL,
62   `disregard` tinyint(1) default '0',
63   PRIMARY KEY  (`id`),
64   KEY `day` (`day`),
65   KEY `mid` (`mid`),
66   KEY `peer` (`peer`)
67 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='Raw data';
68 RAW
69 -- 
70 -- Table structure for table DBTableGrps
71 -- 
72
73 CREATE TABLE IF NOT EXISTS `$Conf{'DBTableGrps'}` (
74   `id` bigint(20) unsigned NOT NULL auto_increment,
75   `month` varchar(7) character set ascii NOT NULL,
76   `newsgroup` varchar(100) NOT NULL,
77   `postings` int(11) NOT NULL,
78   `revision` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
79   PRIMARY KEY  (`id`),
80   UNIQUE KEY `month_newsgroup` (`month`,`newsgroup`),
81   KEY `newsgroup` (`newsgroup`),
82   KEY `postings` (`postings`)
83 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='Postings per newsgroup';
84 GRPS
85
86 ##### --------------------------------------------------------------------------
87 ##### Installation / upgrade instructions
88 ##### --------------------------------------------------------------------------
89
90 my $Install = <<INSTALL;
91 ----------
92 Things left to do:
93
94 1) Setup an INN feed to feedlog.pl
95
96    a) Edit your 'newsfeeds' file and insert something like
97
98           ## gather statistics for NewsStats
99           newsstats!\\
100                   :!*,de.*\\
101                   :Tc,WmtfbsPNH,Ac:$Path/feedlog.pl
102
103       Please
104
105       * check that you got the path to feedlog.pl right
106       * check that feedlog.pl can be executed by the news user
107       * adapt the pattern (here: 'de.*') to your needs
108
109    b) Check your 'newsfeeds' syntax:
110
111          # ctlinnd checkfile
112
113       and reload 'newsfeeds':
114
115          # ctlinnd reload newsfeeds 'Adding newsstats! feed'
116
117    c) Watch your 'news.notice' and 'errlog' files:
118
119          # tail -f /var/log/news/news.notice
120          ...
121          # tail -f /var/log/news/errlog
122
123 2) Watch your $Conf{'DBTableRaw'} table fill.
124
125 3) Read the documentation. ;)
126
127 Enjoy!
128
129 -thh <thh\@inter.net>
130 INSTALL
131
132 my $Upgrade = <<UPGRADE;
133 ----------
134 Your installation was upgraded from $Options{'u'} to $PackageVersion.
135
136 Don't forget to restart your INN feed so that it can pick up the new version:
137
138    # ctlinnd begin 'newsstats!'
139
140 (or whatever you called your feed).
141 UPGRADE
142
143 ##### --------------------------------------------------------------------------
144 ##### Upgrading
145 ##### --------------------------------------------------------------------------
146 my (%DBUpgrade,%Instructions);
147
148 # 0.01 -> 0.02
149 $DBUpgrade{'0.02'}     = <<DB0point02;
150 SELECT 1;
151 DB0point02
152 $Instructions{'0.02'}  = <<IN0point02;
153 Dummy Instructions.
154 IN0point02
155
156 ##### --------------------------- End of definitions ---------------------------
157
158 ### DB init, read list of tables
159 print "Reading database information.\n";
160 my $DBHandle = InitDB(\%Conf,1);
161 my %TablesInDB = %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
162
163 if (!$Options{'u'}) {
164   ##### installation mode
165   print "----------\nStarting database table generation.\n";
166   # check for tables and create them, if they don't exist yet
167   foreach my $Table (keys %DBCreate) {
168     &CreateTable($Table);
169   };
170   print "Database table generation done.\n";
171  
172   # Display install instructions
173   print $Install;
174 } else {
175   ##### upgrade mode
176   print "----------\nStarting upgrade process.\n";
177   if ($Options{'u'} < $PackageVersion) {
178     # Database upgrades for each version
179     foreach my $UpVersion (sort keys %DBUpgrade) {
180       if ($UpVersion > $Options{'u'} and $UpVersion <= $PackageVersion) {
181         print "v$UpVersion: Executing database upgrade ...\n"; 
182         &DoMySQL($DBUpgrade{$UpVersion});
183       };
184     };
185     # Display upgrade instructions for each version
186     foreach my $UpVersion (sort keys %Instructions) {
187       if ($UpVersion > $Options{'u'} and $UpVersion <= $PackageVersion) {
188         print "v$UpVersion: Upgrade Instructions >>>>>\n";
189         my $Padding = ' ' x (length($UpVersion) + 3);
190         $Instructions{$UpVersion} =~ s/^/$Padding/;
191         print $Instructions{$UpVersion};
192         print "<" x (length($UpVersion) + 29) . "\n";
193       };
194     };
195   };
196   # Display upgrade instructions
197   print $Upgrade;
198 };
199
200 # close handle
201 $DBHandle->disconnect;
202
203 exit(0);
204
205 ################################# Subroutines ##################################
206
207 sub CreateTable() {
208   my $Table = shift;
209   if (defined($TablesInDB{$Conf{$Table}})) {
210     printf("Database table %s.%s already exists, skipping ....\n",$Conf{'DBDatabase'},$Conf{$Table});
211     return;
212   };
213   my $DBQuery = $DBHandle->prepare($DBCreate{$Table});
214   $DBQuery->execute() or die sprintf("$MySelf: E: Can't create table %s in database %s: %s%\n",$Table,$Conf{'DBDatabase'},$DBI::errstr);
215   printf("Database table %s.%s created succesfully.\n",$Conf{'DBDatabase'},$Conf{$Table});
216   return;
217 };
218
219 sub DoMySQL() {
220   my $SQL = shift;
221   my $DBQuery = $DBHandle->prepare($SQL);
222   $DBQuery->execute() or warn sprintf("$MySelf: E: Database error: %s\n",$DBI::errstr);
223   return;
224 };
225
226
227 __END__
228
229 ################################ Documentation #################################
230
231 =head1 NAME
232
233 install - installation script
234
235 =head1 SYNOPSIS
236
237 B<install> [B<-Vh>]
238
239 =head1 REQUIREMENTS
240
241 See doc/README: Perl 5.8.x itself and the following modules from CPAN:
242
243 =over 2
244
245 =item -
246
247 Config::Auto
248
249 =item -
250
251 DBI
252
253 =back
254
255 =head1 DESCRIPTION
256
257 This script will create database tables as necessary and configured.
258
259 =head2 Configuration
260
261 F<install.pl> will read its configuration from F<newsstats.conf> via
262 Config::Auto.
263
264 See doc/INSTALL for an overview of possible configuration options.
265
266 =head1 OPTIONS
267
268 =over 3
269
270 =item B<-V> (version)
271
272 Print out version and copyright information on B<yapfaq> and exit.
273
274 =item B<-h> (help)
275
276 Print this man page and exit.
277
278 =back
279
280 =head1 FILES
281
282 =over 4
283
284 =item F<install.pl>
285
286 The script itself.
287
288 =item F<NewsStats.pm>
289
290 Library functions for the NewsStats package.
291
292 =item F<newsstats.conf>
293
294 Runtime configuration file for B<yapfaq>.
295
296 =back
297
298 =head1 BUGS
299
300 Please report any bugs or feature requests to the author or use the
301 bug tracker at L<http://bugs.th-h.de/>!
302
303 =head1 SEE ALSO
304
305 =over 2
306
307 =item -
308
309 doc/README
310
311 =item -
312
313 doc/INSTALL
314
315 =back
316
317 This script is part of the B<NewsStats> package.
318
319 =head1 AUTHOR
320
321 Thomas Hochstein <thh@inter.net>
322
323 =head1 COPYRIGHT AND LICENSE
324
325 Copyright (c) 2010 Thomas Hochstein <thh@inter.net>
326
327 This program is free software; you may redistribute it and/or modify it
328 under the same terms as Perl itself.
329
330 =cut
This page took 0.018865 seconds and 3 git commands to generate.