/
Listener.pm
279 lines (233 loc) · 7.8 KB
/
Listener.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# See bottom of file for license and copyright information
package Foswiki::Contrib::DBIStoreContrib::Listener;
=begin TML
---+ package Foswiki::Contrib::DBIStoreContrib::Listener;
Implements Foswiki::Store::Listener.
Object that listens to low level store events, and maintains an SQL
database (on the other side of DBI).
=cut
use strict;
use warnings;
use DBI;
use Foswiki::Meta;
use Error ':try';
use Assert;
use constant MONITOR => 0;
our $db;
our @TABLES = keys(%Foswiki::Meta::VALIDATE); # META: types
our $ATTRS;
# @ISA not required (base class is empty)
#our @ISA = ('Foswiki::Store::Listener');
# Construct object, just used as a handle
sub new {
my $class = shift;
unless ($ATTRS) {
$ATTRS = {};
foreach my $type (@TABLES) {
my @keys;
foreach my $g qw(require allow other) {
if (defined $Foswiki::Meta::VALIDATE{$type}->{$g}) {
push(@keys, @{$Foswiki::Meta::VALIDATE{$type}->{$g}});
}
}
foreach my $key (@keys) {
$ATTRS->{$type}->{$key} = 1;
}
}
}
unless ($db) {
$db = bless({}, $class);
}
return $db;
}
# Connect on demand
sub _connect {
my ($this, $session) = @_;
return 1 if $this->{handle};
if ($Foswiki::inUnitTestMode) {
# Change the DSN to a SQLite test db, which is held in the data
# area; that way it will be ripped down by -clean
$Foswiki::cfg{Extensions}{DBIStoreContrib}{DSN} =
$Foswiki::cfg{Extensions}{DBIStoreContrib}{DSN} =
"dbi:SQLite:dbname=$Foswiki::cfg{DataDir}/TemporarySQLiteCache";
$Foswiki::cfg{Extensions}{DBIStoreContrib}{Username} = '';
$Foswiki::cfg{Extensions}{DBIStoreContrib}{Password} = '';
}
print STDERR "CONNECT $Foswiki::cfg{Extensions}{DBIStoreContrib}{DSN}..."
if MONITOR;
$this->{handle} = DBI->connect(
$Foswiki::cfg{Extensions}{DBIStoreContrib}{DSN},
$Foswiki::cfg{Extensions}{DBIStoreContrib}{Username},
$Foswiki::cfg{Extensions}{DBIStoreContrib}{Password},
{ RaiseError => 1 });
# Check if the DB is initialised with a quick sniff of the metatypes
eval {
$this->{handle}->selectrow_array('SELECT * from metatypes');
};
if( $@ ) {
$this->_createTables();
$this->_preload($session);
}
print STDERR "connected $this->{handle}\n" if MONITOR;
return 1;
}
# Does the table exist in the DB?
sub _tableExists {
my ($this, $type) = @_;
return 1 if $Foswiki::Meta::VALIDATE{$type}->{_default}
|| grep { /^$type$/ } @TABLES;
my $check = $this->{handle}->selectcol_arrayref(<<SQL, {}, $type);
SELECT name FROM 'metatypes' WHERE name=?
SQL
return SCALAR(@$check);
}
# Create the table for the given META:
sub _createTableForMETA {
my ($this, $t) = @_;
my $cols = join(
",\n", map { " '$_' TEXT" } keys %{$ATTRS->{$t}});
$this->{handle}->do(<<SQL);
CREATE TABLE '$t' (
'tid' TEXT,
$cols
);
SQL
# Add the table to the table of tables
$this->{handle}->do(<<SQL, {}, $t);
INSERT INTO 'metatypes' (name) VALUES (?);
SQL
# If it's not a default table, add it to the list of tables.
push(@TABLES, $t) unless $Foswiki::Meta::VALIDATE{$t}->{_default};
}
# Create all the base tables in the DB (including all default META: tables)
sub _createTables {
my $this = shift;
# Create the topic table. This links the web name, topic name,
# topic text and raw text of the topic.
$this->{handle}->do(<<SQL);
CREATE TABLE 'topic' (
'tid' TEXT,
'web' TEXT,
'name' TEXT,
'text' TEXT,
'raw' TEXT,
UNIQUE (tid)
);
SQL
# Now create the meta-table of known META: tables
$this->{handle}->do(<<SQL);
CREATE TABLE 'metatypes' (
'name' TEXT,
UNIQUE (name)
);
SQL
# Create the tables for each known META: type
foreach my $t (@TABLES) {
$this->_createTableForMETA($t);
}
}
# Load all existing webs and topics into the cache DB (expensive)
sub _preload {
my ($this, $session) = @_;
my $root = Foswiki::Meta->new($session);
my $wit = $root->eachWeb();
while ($wit->hasNext()) {
my $w = $wit->next();
print STDERR "PRELOAD $w\n" if MONITOR;
my $web = Foswiki::Meta->new($session , $w );
my $tit = $web->eachTopic();
while ($tit->hasNext()) {
my $t = $tit->next();
my $topic = Foswiki::Meta->load( $session, $w, $t );
$this->insert($topic);
}
}
}
sub _makeTID {
my $tob = shift;
return $tob->web().'/'.$tob->topic();
}
# Implements Foswiki::Store::Listener
sub insert {
my ($this, $mo) = @_;
if (defined $mo->topic()) {
my $tid = _makeTID($mo);
#print STDERR "\tInsert $tid\n" if MONITOR;
$this->_connect($mo->session());
$this->{handle}->do(
'INSERT INTO topic (tid,web,name,text,raw) VALUES (?,?,?,?,?);',
{}, $tid, $mo->web(), $mo->topic(),
$mo->text(), $mo->getEmbeddedStoreForm());
foreach my $type (keys %$mo) {
# Make sure it's registered.
next unless (defined $Foswiki::Meta::VALIDATE{$type});
# If it's not default, we may have to create the table
$this->_createTableForMETA($type)
unless $this->_tableExists($type);
# Insert this row
my $data = $mo->{$type};
foreach my $item (@$data) {
# Filter attrs by those legal in the schema
my @kn = grep { $ATTRS->{$type}->{$_} } keys(%$item);
my @kl = ('tid', @kn);
my $sql = "INSERT INTO $type (" . join(',', map { "'$_'" } @kl)
. ") VALUES (".join(',', map { '?' } @kl).");";
$this->{handle}->do($sql, {}, $tid,
map { $item->{$_} } @kn);
}
}
}
}
# Implements Foswiki::Store::Listener
sub update {
my ($this, $old, $new) = @_;
# SMELL: there's got to be a better way
$this->remove($old);
$this->insert($new || $old);
}
# Implements Foswiki::Store::Listener
sub remove {
my ($this, $mo) = @_;
my $tids;
$this->_connect($mo->session());
if (defined $mo->topic()) {
push(@$tids, _makeTID($mo));
} else {
$tids = $this->{handle}->selectcol_arrayref(<<SQL, {}, $mo->web());
SELECT tid FROM topic WHERE web=?;
SQL
}
my $ph = join(',', map { '?' } @$tids);
#print STDERR "\tRemove ".join(',',@$tids)."\n" if MONITOR;
foreach my $table ('topic', @TABLES) {
$this->{handle}->do(<<SQL, {}, @$tids);
DELETE FROM $table WHERE tid IN ($ph);
SQL
}
}
# Static method invoked by Foswiki::Store::QueryAlgorithms::DBIStoreContrib
# to perform the actual database query
sub query {
my ($session, $sql) = @_;
$db->_connect($session);
print STDERR "$sql\n" if MONITOR;
my $names = $db->{handle}->selectcol_arrayref($sql);
print STDERR "HITS: ".scalar(@$names)."\n" if MONITOR;
return $names;
}
1;
__DATA__
Author: Crawford Currie http://c-dot.co.uk
Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/, http://Foswiki.org/
Copyright (C) 2010 Foswiki Contributors. All Rights Reserved.
Foswiki Contributors are listed in the AUTHORS file in the root
of this distribution. NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.