Adapt feedlog.pl to new coding style.
[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 ##### --------------------------- End of definitions ---------------------------
144
145 ### DB init, read list of tables
146 print "Reading database information.\n";
147 my $DBHandle = InitDB(\%Conf,1);
148 my %TablesInDB = %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
149
150 if (!$Options{'u'}) {
151   ##### installation mode
152   print "----------\nStarting database table generation.\n";
153   # check for tables and create them, if they don't exist yet
154   foreach my $Table (keys %DBCreate) {
155     &CreateTable($Table);
156   };
157   print "Database table generation done.\n";
158  
159   # Display install instructions
160   print $Install;
161 } else {
162   ##### upgrade mode
163   print "----------\nStarting upgrade process.\n";
164   $PackageVersion = '0.03';
165   if ($Options{'u'} < $PackageVersion) {
166     if ($Options{'u'} < 0.02) {
167       # 0.01 -> 0.02
168       # &DoMySQL('...;');
169       # print "v0.02: Database upgrades ...\n";
170       # &PrintInstructions('0.02',<<"      INSTRUCTIONS");
171       # INSTRUCTIONS
172     };
173   };
174   # Display general upgrade instructions
175   print $Upgrade;
176 };
177
178 # close handle
179 $DBHandle->disconnect;
180
181 exit(0);
182
183 ################################# Subroutines ##################################
184
185 sub CreateTable {
186   my $Table = shift;
187   if (defined($TablesInDB{$Conf{$Table}})) {
188     printf("Database table %s.%s already exists, skipping ....\n",$Conf{'DBDatabase'},$Conf{$Table});
189     return;
190   };
191   my $DBQuery = $DBHandle->prepare($DBCreate{$Table});
192   $DBQuery->execute() or die sprintf("$MySelf: E: Can't create table %s in database %s: %s%\n",$Table,$Conf{'DBDatabase'},$DBI::errstr);
193   printf("Database table %s.%s created succesfully.\n",$Conf{'DBDatabase'},$Conf{$Table});
194   return;
195 };
196
197 sub DoMySQL {
198   my $SQL = shift;
199   my $DBQuery = $DBHandle->prepare($SQL);
200   $DBQuery->execute() or warn sprintf("$MySelf: E: Database error: %s\n",$DBI::errstr);
201   return;
202 };
203
204 sub PrintInstructions {
205   my ($UpVersion,$Instructions) = @_;
206   print "v$UpVersion: Upgrade Instructions >>>>>\n";
207   my $Padding = ' ' x (length($UpVersion) + 3);
208     $Instructions =~ s/^      /$Padding/mg;
209     print $Instructions;
210     print "<" x (length($UpVersion) + 29) . "\n";
211 };
212
213
214 __END__
215
216 ################################ Documentation #################################
217
218 =head1 NAME
219
220 install - installation script
221
222 =head1 SYNOPSIS
223
224 B<install> [B<-Vh>]
225
226 =head1 REQUIREMENTS
227
228 See doc/README: Perl 5.8.x itself and the following modules from CPAN:
229
230 =over 2
231
232 =item -
233
234 Config::Auto
235
236 =item -
237
238 DBI
239
240 =back
241
242 =head1 DESCRIPTION
243
244 This script will create database tables as necessary and configured.
245
246 =head2 Configuration
247
248 F<install.pl> will read its configuration from F<newsstats.conf> via
249 Config::Auto.
250
251 See doc/INSTALL for an overview of possible configuration options.
252
253 =head1 OPTIONS
254
255 =over 3
256
257 =item B<-V> (version)
258
259 Print out version and copyright information on B<yapfaq> and exit.
260
261 =item B<-h> (help)
262
263 Print this man page and exit.
264
265 =back
266
267 =head1 FILES
268
269 =over 4
270
271 =item F<install.pl>
272
273 The script itself.
274
275 =item F<NewsStats.pm>
276
277 Library functions for the NewsStats package.
278
279 =item F<newsstats.conf>
280
281 Runtime configuration file for B<yapfaq>.
282
283 =back
284
285 =head1 BUGS
286
287 Please report any bugs or feature requests to the author or use the
288 bug tracker at L<http://bugs.th-h.de/>!
289
290 =head1 SEE ALSO
291
292 =over 2
293
294 =item -
295
296 doc/README
297
298 =item -
299
300 doc/INSTALL
301
302 =back
303
304 This script is part of the B<NewsStats> package.
305
306 =head1 AUTHOR
307
308 Thomas Hochstein <thh@inter.net>
309
310 =head1 COPYRIGHT AND LICENSE
311
312 Copyright (c) 2010 Thomas Hochstein <thh@inter.net>
313
314 This program is free software; you may redistribute it and/or modify it
315 under the same terms as Perl itself.
316
317 =cut
This page took 0.018391 seconds and 3 git commands to generate.