Merge branch 'thh-parsedb' into pu
[usenet/newsstats.git] / install / install.pl
CommitLineData
3f817eb4 1#! /usr/bin/perl
2832c235
TH
2#
3# install.pl
4#
5# This script will create database tables as necessary.
dfc2b81c 6#
2832c235
TH
7# It is part of the NewsStats package.
8#
07c0b258 9# Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net>
2832c235 10#
dfc2b81c 11# It can be redistributed and/or modified under the same terms under
2832c235
TH
12# which Perl itself is published.
13
14BEGIN {
24d2011f 15 our $VERSION = "0.02";
2832c235 16 use File::Basename;
2ad99c20
TH
17 # we're in .../install, so our module is in ../lib
18 push(@INC, dirname($0).'/../lib');
2832c235
TH
19}
20use strict;
3f817eb4 21use warnings;
2832c235
TH
22
23use NewsStats qw(:DEFAULT);
24
2832c235 25use DBI;
1fa94799
TH
26use Getopt::Long qw(GetOptions);
27Getopt::Long::config ('bundling');
2832c235
TH
28
29################################# Main program #################################
30
31### read commandline options
23ab67a0 32my ($OptUpdate,$OptConfFile);
1fa94799 33GetOptions ('u|update=s' => \$OptUpdate,
23ab67a0 34 'conffile=s' => \$OptConfFile,
1fa94799
TH
35 'h|help' => \&ShowPOD,
36 'V|version' => \&ShowVersion) or exit 1;
2832c235 37
2832c235
TH
38### read configuration
39print("Reading configuration.\n");
23ab67a0 40my %Conf = %{ReadConfig($OptConfFile)};
2832c235
TH
41
42##### --------------------------------------------------------------------------
43##### Database table definitions
44##### --------------------------------------------------------------------------
45
36cffe7a
TH
46my $DBCreate = <<SQLDB;
47CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8;
48SQLDB
49
6d72dad2 50my %DBCreate = ('DBTableRaw' => <<RAW, 'DBTableParse' => <<PARSE, 'DBTableGrps' => <<GRPS);
23ab67a0 51--
2832c235 52-- Table structure for table DBTableRaw
23ab67a0 53--
2832c235
TH
54
55CREATE 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';
73RAW
23ab67a0 74--
6d72dad2
TH
75-- Table structure for table DBTableParse
76--
77
78CREATE TABLE IF NOT EXISTS `$Conf{'DBTableParse'}` (
79 `id` bigint(20) unsigned NOT NULL auto_increment,
80 `day` date NOT NULL,
81 `mid` varchar(250) character set ascii NOT NULL,
82 `refs` varchar(1000) character set ascii,
83 `date` varchar(100) NOT NULL,
84 `path` varchar(1000) NOT NULL,
85 `newsgroups` varchar(1000) NOT NULL,
86 `fupto` varchar(200),
87 `from_` varchar(500),
88 `from_parsed` varchar(200),
89 `from_name` varchar(200),
90 `from_address` varchar(200),
91 `sender` varchar(500),
92 `sender_parsed` varchar(200),
93 `sender_name` varchar(200),
94 `sender_address` varchar(200),
95 `replyto` varchar(500),
96 `replyto_parsed` varchar(200),
97 `replyto_name` varchar(200),
98 `replyto_address` varchar(200),
99 `subject` varchar(1000) NOT NULL,
100 `subject_parsed` varchar(1000),
101 `organization` varchar(1000),
102 `linecount` int(4) unsigned,
103 `approved` varchar(250),
104 `supersedes` varchar(250),
105 `expires` varchar(100),
106 `useragent` varchar(500),
107 `xnewsreader` varchar(500),
108 `xmailer` varchar(500),
109 `xnoarchive` varchar(100),
110 `contenttype` varchar(500),
111 `contentencoding` varchar(500),
112 `cancellock` varchar(500),
113 `injectioninfo` varchar(500),
114 `xtrace` varchar(500),
115 `postinghost` varchar(1000),
116 `headers` longtext,
117 `disregard` tinyint(1) default '0',
118 PRIMARY KEY (`id`),
119 KEY `day` (`day`),
120 KEY `mid` (`mid`),
121 KEY `newsgroups` (`newsgroups`)
122) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Parsed data';
123PARSE
124--
2832c235 125-- Table structure for table DBTableGrps
23ab67a0 126--
2832c235
TH
127
128CREATE TABLE IF NOT EXISTS `$Conf{'DBTableGrps'}` (
129 `id` bigint(20) unsigned NOT NULL auto_increment,
130 `month` varchar(7) character set ascii NOT NULL,
131 `newsgroup` varchar(100) NOT NULL,
132 `postings` int(11) NOT NULL,
133 `revision` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
134 PRIMARY KEY (`id`),
135 UNIQUE KEY `month_newsgroup` (`month`,`newsgroup`),
136 KEY `newsgroup` (`newsgroup`),
137 KEY `postings` (`postings`)
138) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Postings per newsgroup';
139GRPS
140
f6d15ca7
TH
141##### --------------------------------------------------------------------------
142##### Installation / upgrade instructions
143##### --------------------------------------------------------------------------
2832c235 144
f6d15ca7
TH
145my $Install = <<INSTALL;
146----------
2832c235
TH
147Things left to do:
148
1491) Setup an INN feed to feedlog.pl
150
151 a) Edit your 'newsfeeds' file and insert something like
152
153 ## gather statistics for NewsStats
9c141b0c
TH
154 newsstats!\\
155 :!*,de.*\\
fd0717a1 156 :Tc,WmtfbsPNH,Ac:$HomePath/bin/feedlog.pl
2832c235
TH
157
158 Please
159
160 * check that you got the path to feedlog.pl right
161 * check that feedlog.pl can be executed by the news user
162 * adapt the pattern (here: 'de.*') to your needs
163
164 b) Check your 'newsfeeds' syntax:
165
166 # ctlinnd checkfile
167
168 and reload 'newsfeeds':
169
170 # ctlinnd reload newsfeeds 'Adding newsstats! feed'
171
172 c) Watch your 'news.notice' and 'errlog' files:
173
174 # tail -f /var/log/news/news.notice
175 ...
176 # tail -f /var/log/news/errlog
177
1782) Watch your $Conf{'DBTableRaw'} table fill.
179
1803) Read the documentation. ;)
181
182Enjoy!
183
184-thh <thh\@inter.net>
f6d15ca7
TH
185INSTALL
186
da6fc073
TH
187my $Upgrade ='';
188if ($OptUpdate) {
189 $Upgrade = <<UPGRADE;
f6d15ca7 190----------
1fa94799 191Your installation was upgraded from $OptUpdate to $PackageVersion.
f6d15ca7
TH
192
193Don't forget to restart your INN feed so that it can pick up the new version:
194
195 # ctlinnd begin 'newsstats!'
196
197(or whatever you called your feed).
198UPGRADE
da6fc073 199}
f6d15ca7 200
f6d15ca7
TH
201##### --------------------------- End of definitions ---------------------------
202
36cffe7a
TH
203### create DB, if necessary
204if (!$OptUpdate) {
205 print "----------\nStarting database creation.\n";
206 # create database
207 # we can't use InitDB() as that will use a table name of
208 # the table that doesn't exist yet ...
209 my $DBHandle = DBI->connect(sprintf('DBI:%s:host=%s',$Conf{'DBDriver'},
210 $Conf{'DBHost'}), $Conf{'DBUser'},
211 $Conf{'DBPw'}, { PrintError => 0 });
212 my $DBQuery = $DBHandle->prepare($DBCreate);
213 $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n",
214 $Conf{'DBDatabase'}, $DBI::errstr));
23ab67a0 215
36cffe7a
TH
216 printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'});
217 $DBHandle->disconnect;
218};
219
f6d15ca7
TH
220### DB init, read list of tables
221print "Reading database information.\n";
222my $DBHandle = InitDB(\%Conf,1);
1fa94799
TH
223my %TablesInDB =
224 %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
f6d15ca7 225
1fa94799 226if (!$OptUpdate) {
f6d15ca7 227 ##### installation mode
f6d15ca7
TH
228 # check for tables and create them, if they don't exist yet
229 foreach my $Table (keys %DBCreate) {
230 &CreateTable($Table);
231 };
232 print "Database table generation done.\n";
dfc2b81c 233
f6d15ca7
TH
234 # Display install instructions
235 print $Install;
236} else {
237 ##### upgrade mode
238 print "----------\nStarting upgrade process.\n";
59d0c5ef 239 $PackageVersion = '0.03';
1fa94799
TH
240 if ($OptUpdate < $PackageVersion) {
241 if ($OptUpdate < 0.02) {
59d0c5ef
TH
242 # 0.01 -> 0.02
243 # &DoMySQL('...;');
244 # print "v0.02: Database upgrades ...\n";
245 # &PrintInstructions('0.02',<<" INSTRUCTIONS");
246 # INSTRUCTIONS
f6d15ca7
TH
247 };
248 };
59d0c5ef 249 # Display general upgrade instructions
f6d15ca7
TH
250 print $Upgrade;
251};
252
253# close handle
254$DBHandle->disconnect;
255
256exit(0);
257
258################################# Subroutines ##################################
259
59d0c5ef 260sub CreateTable {
f6d15ca7
TH
261 my $Table = shift;
262 if (defined($TablesInDB{$Conf{$Table}})) {
1fa94799
TH
263 printf("Database table %s.%s already exists, skipping ....\n",
264 $Conf{'DBDatabase'},$Conf{$Table});
f6d15ca7
TH
265 return;
266 };
267 my $DBQuery = $DBHandle->prepare($DBCreate{$Table});
1fa94799
TH
268 $DBQuery->execute() or
269 &Bleat(2, sprintf("Can't create table %s in database %s: %s%\n",$Table,
270 $Conf{'DBDatabase'},$DBI::errstr));
271 printf("Database table %s.%s created succesfully.\n",
272 $Conf{'DBDatabase'},$Conf{$Table});
f6d15ca7
TH
273 return;
274};
275
59d0c5ef 276sub DoMySQL {
f6d15ca7
TH
277 my $SQL = shift;
278 my $DBQuery = $DBHandle->prepare($SQL);
1fa94799 279 $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr));
f6d15ca7
TH
280 return;
281};
282
59d0c5ef
TH
283sub PrintInstructions {
284 my ($UpVersion,$Instructions) = @_;
285 print "v$UpVersion: Upgrade Instructions >>>>>\n";
286 my $Padding = ' ' x (length($UpVersion) + 3);
287 $Instructions =~ s/^ /$Padding/mg;
288 print $Instructions;
289 print "<" x (length($UpVersion) + 29) . "\n";
290};
291
2832c235
TH
292
293__END__
294
295################################ Documentation #################################
296
297=head1 NAME
298
299install - installation script
300
301=head1 SYNOPSIS
302
e39d4207 303B<install> [B<-Vh> [--update I<version>] [B<--conffile> I<filename>]
2832c235
TH
304
305=head1 REQUIREMENTS
306
1fa94799 307See L<doc/README>.
2832c235
TH
308
309=head1 DESCRIPTION
310
311This script will create database tables as necessary and configured.
312
313=head2 Configuration
314
44c19709
TH
315B<install> will read its configuration from F<newsstats.conf> which should
316be present in etc/ via Config::Auto or from a configuration file submitted
317by the B<--conffile> option.
2832c235 318
1fa94799 319See L<doc/INSTALL> for an overview of possible configuration options.
2832c235
TH
320
321=head1 OPTIONS
322
323=over 3
324
1fa94799 325=item B<-V>, B<--version>
2832c235 326
1fa94799 327Print out version and copyright information and exit.
2832c235 328
1fa94799 329=item B<-h>, B<--help>
2832c235
TH
330
331Print this man page and exit.
332
1fa94799
TH
333=item B<-u>, B<--update> I<version>
334
335Don't do a fresh install, but update from I<version>.
336
23ab67a0
TH
337=item B<--conffile> I<filename>
338
339Load configuration from I<filename> instead of F<newsstats.conf>.
340
2832c235
TH
341=back
342
343=head1 FILES
344
345=over 4
346
2ad99c20 347=item F<install/install.pl>
2832c235
TH
348
349The script itself.
350
2ad99c20 351=item F<lib/NewsStats.pm>
2832c235
TH
352
353Library functions for the NewsStats package.
354
2ad99c20 355=item F<etc/newsstats.conf>
2832c235 356
1fa94799 357Runtime configuration file.
2832c235
TH
358
359=back
360
361=head1 BUGS
362
363Please report any bugs or feature requests to the author or use the
364bug tracker at L<http://bugs.th-h.de/>!
365
366=head1 SEE ALSO
367
368=over 2
369
370=item -
371
1fa94799 372L<doc/README>
2832c235
TH
373
374=item -
375
1fa94799 376L<doc/INSTALL>
2832c235
TH
377
378=back
379
380This script is part of the B<NewsStats> package.
381
382=head1 AUTHOR
383
384Thomas Hochstein <thh@inter.net>
385
386=head1 COPYRIGHT AND LICENSE
387
28717921 388Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net>
2832c235
TH
389
390This program is free software; you may redistribute it and/or modify it
391under the same terms as Perl itself.
392
393=cut
This page took 0.034139 seconds and 4 git commands to generate.