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