Handle undefined previous version when installing.
[usenet/newsstats.git] / install / install.pl
CommitLineData
2832c235
TH
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#
1fa94799 9# Copyright (c) 2010-2012 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;
21
22use NewsStats qw(:DEFAULT);
23
24use Cwd;
25
26use DBI;
1fa94799
TH
27use Getopt::Long qw(GetOptions);
28Getopt::Long::config ('bundling');
2832c235
TH
29
30################################# Main program #################################
31
32### read commandline options
1fa94799
TH
33my ($OptUpdate);
34GetOptions ('u|update=s' => \$OptUpdate,
35 'h|help' => \&ShowPOD,
36 'V|version' => \&ShowVersion) or exit 1;
2832c235
TH
37
38### change working directory to .. (as we're in .../install)
39chdir dirname($0).'/..';
f6d15ca7 40my $Path = cwd();
2832c235
TH
41
42### read configuration
43print("Reading configuration.\n");
1fa94799 44my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')};
2832c235
TH
45
46##### --------------------------------------------------------------------------
47##### Database table definitions
48##### --------------------------------------------------------------------------
49
50my %DBCreate = ('DBTableRaw' => <<RAW, 'DBTableGrps' => <<GRPS);
51--
52-- Table structure for table DBTableRaw
53--
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
74--
75-- Table structure for table DBTableGrps
76--
77
78CREATE 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';
89GRPS
90
f6d15ca7
TH
91##### --------------------------------------------------------------------------
92##### Installation / upgrade instructions
93##### --------------------------------------------------------------------------
2832c235 94
f6d15ca7
TH
95my $Install = <<INSTALL;
96----------
2832c235
TH
97Things left to do:
98
991) Setup an INN feed to feedlog.pl
100
101 a) Edit your 'newsfeeds' file and insert something like
102
103 ## gather statistics for NewsStats
9c141b0c
TH
104 newsstats!\\
105 :!*,de.*\\
2832c235
TH
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
1282) Watch your $Conf{'DBTableRaw'} table fill.
129
1303) Read the documentation. ;)
131
132Enjoy!
133
134-thh <thh\@inter.net>
f6d15ca7
TH
135INSTALL
136
da6fc073
TH
137my $Upgrade ='';
138if ($OptUpdate) {
139 $Upgrade = <<UPGRADE;
f6d15ca7 140----------
1fa94799 141Your installation was upgraded from $OptUpdate to $PackageVersion.
f6d15ca7
TH
142
143Don't forget to restart your INN feed so that it can pick up the new version:
144
145 # ctlinnd begin 'newsstats!'
146
147(or whatever you called your feed).
148UPGRADE
da6fc073 149}
f6d15ca7 150
f6d15ca7
TH
151##### --------------------------- End of definitions ---------------------------
152
153### DB init, read list of tables
154print "Reading database information.\n";
155my $DBHandle = InitDB(\%Conf,1);
1fa94799
TH
156my %TablesInDB =
157 %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
f6d15ca7 158
1fa94799 159if (!$OptUpdate) {
f6d15ca7
TH
160 ##### installation mode
161 print "----------\nStarting database table generation.\n";
162 # check for tables and create them, if they don't exist yet
163 foreach my $Table (keys %DBCreate) {
164 &CreateTable($Table);
165 };
166 print "Database table generation done.\n";
167
168 # Display install instructions
169 print $Install;
170} else {
171 ##### upgrade mode
172 print "----------\nStarting upgrade process.\n";
59d0c5ef 173 $PackageVersion = '0.03';
1fa94799
TH
174 if ($OptUpdate < $PackageVersion) {
175 if ($OptUpdate < 0.02) {
59d0c5ef
TH
176 # 0.01 -> 0.02
177 # &DoMySQL('...;');
178 # print "v0.02: Database upgrades ...\n";
179 # &PrintInstructions('0.02',<<" INSTRUCTIONS");
180 # INSTRUCTIONS
f6d15ca7
TH
181 };
182 };
59d0c5ef 183 # Display general upgrade instructions
f6d15ca7
TH
184 print $Upgrade;
185};
186
187# close handle
188$DBHandle->disconnect;
189
190exit(0);
191
192################################# Subroutines ##################################
193
59d0c5ef 194sub CreateTable {
f6d15ca7
TH
195 my $Table = shift;
196 if (defined($TablesInDB{$Conf{$Table}})) {
1fa94799
TH
197 printf("Database table %s.%s already exists, skipping ....\n",
198 $Conf{'DBDatabase'},$Conf{$Table});
f6d15ca7
TH
199 return;
200 };
201 my $DBQuery = $DBHandle->prepare($DBCreate{$Table});
1fa94799
TH
202 $DBQuery->execute() or
203 &Bleat(2, sprintf("Can't create table %s in database %s: %s%\n",$Table,
204 $Conf{'DBDatabase'},$DBI::errstr));
205 printf("Database table %s.%s created succesfully.\n",
206 $Conf{'DBDatabase'},$Conf{$Table});
f6d15ca7
TH
207 return;
208};
209
59d0c5ef 210sub DoMySQL {
f6d15ca7
TH
211 my $SQL = shift;
212 my $DBQuery = $DBHandle->prepare($SQL);
1fa94799 213 $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr));
f6d15ca7
TH
214 return;
215};
216
59d0c5ef
TH
217sub PrintInstructions {
218 my ($UpVersion,$Instructions) = @_;
219 print "v$UpVersion: Upgrade Instructions >>>>>\n";
220 my $Padding = ' ' x (length($UpVersion) + 3);
221 $Instructions =~ s/^ /$Padding/mg;
222 print $Instructions;
223 print "<" x (length($UpVersion) + 29) . "\n";
224};
225
2832c235
TH
226
227__END__
228
229################################ Documentation #################################
230
231=head1 NAME
232
233install - installation script
234
235=head1 SYNOPSIS
236
1fa94799 237B<install> [B<-Vh> [--update I<version>]
2832c235
TH
238
239=head1 REQUIREMENTS
240
1fa94799 241See L<doc/README>.
2832c235
TH
242
243=head1 DESCRIPTION
244
245This script will create database tables as necessary and configured.
246
247=head2 Configuration
248
1fa94799 249B<install> will read its configuration from F<newsstats.conf> via
2832c235
TH
250Config::Auto.
251
1fa94799 252See L<doc/INSTALL> for an overview of possible configuration options.
2832c235
TH
253
254=head1 OPTIONS
255
256=over 3
257
1fa94799 258=item B<-V>, B<--version>
2832c235 259
1fa94799 260Print out version and copyright information and exit.
2832c235 261
1fa94799 262=item B<-h>, B<--help>
2832c235
TH
263
264Print this man page and exit.
265
1fa94799
TH
266=item B<-u>, B<--update> I<version>
267
268Don't do a fresh install, but update from I<version>.
269
2832c235
TH
270=back
271
272=head1 FILES
273
274=over 4
275
276=item F<install.pl>
277
278The script itself.
279
280=item F<NewsStats.pm>
281
282Library functions for the NewsStats package.
283
284=item F<newsstats.conf>
285
1fa94799 286Runtime configuration file.
2832c235
TH
287
288=back
289
290=head1 BUGS
291
292Please report any bugs or feature requests to the author or use the
293bug tracker at L<http://bugs.th-h.de/>!
294
295=head1 SEE ALSO
296
297=over 2
298
299=item -
300
1fa94799 301L<doc/README>
2832c235
TH
302
303=item -
304
1fa94799 305L<doc/INSTALL>
2832c235
TH
306
307=back
308
309This script is part of the B<NewsStats> package.
310
311=head1 AUTHOR
312
313Thomas Hochstein <thh@inter.net>
314
315=head1 COPYRIGHT AND LICENSE
316
1fa94799 317Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
2832c235
TH
318
319This program is free software; you may redistribute it and/or modify it
320under the same terms as Perl itself.
321
322=cut
This page took 0.026052 seconds and 4 git commands to generate.