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