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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
package XXV::MODULES::KEYWORDS;
use strict;
use Encode;
use Tools;
# ------------------
sub module {
# ------------------
my $self = shift || return error('No object defined!');
my $args = {
Name => 'KEYWORDS',
Prereq => {
'XML::Simple' => 'Easy API to maintain XML (esp config files)'
},
Description => gettext('This module manages keywords and tag within timer and recordings.'),
# Status => sub{ $self->status(@_) },
Preferences => {
active => {
description => gettext('Activate this service'),
default => 'y',
type => 'confirm',
required => gettext('This is required!'),
},
},
Commands => {
tkeywords => {
description => gettext("Search timers 'keywords'"),
short => 'tk',
callback => sub{ $self->timer_keywords(@_) },
DenyClass => 'tlist',
},
tsuggestkeywords => {
hidden => 'yes',
callback => sub{ $self->suggest('timer',@_) },
DenyClass => 'tlist',
},
rkeywords => {
description => gettext("Search recordings 'keywords'"),
short => 'rk',
callback => sub{ $self->recording_keywords(@_) },
DenyClass => 'rlist',
},
rsuggestkeywords => {
hidden => 'yes',
callback => sub{ $self->suggest('recording',@_) },
DenyClass => 'rlist',
}
}
};
return $args;
}
# ------------------
sub new {
# ------------------
my($class, %attr) = @_;
my $self = {};
bless($self, $class);
$self->{charset} = delete $attr{'-charset'};
if($self->{charset} eq 'UTF-8'){
eval 'use utf8';
}
# paths
$self->{paths} = delete $attr{'-paths'};
# who am I
$self->{MOD} = $self->module;
# all configvalues to $self without parents (important for ConfigModule)
map {
$self->{$_} = $attr{'-config'}->{$self->{MOD}->{Name}}->{$_};
$self->{$_} = $self->{MOD}->{Preferences}->{$_}->{default} unless($self->{$_});
} keys %{$self->{MOD}->{Preferences}};
# Try to use the Requirments
map {
eval "use $_";
if($@) {
my $m = (split(/ /, $_))[0];
return panic("\nCouldn't load perl module: $m\nPlease install this module on your system:\nperl -MCPAN -e 'install $m'");
}
} keys %{$self->{MOD}->{Prereq}};
# read the DB Handle
$self->{dbh} = delete $attr{'-dbh'};
$self->{xml} = XML::Simple->new( NumericEscape => 2 )
|| return error("Can't create XML instance!");
# The Initprocess
my $erg = $self->_init or return error('Problem to initialize modul!');
return $self;
}
# ------------------
sub _init {
# ------------------
my $self = shift || return error('No object defined!');
unless($self->{dbh}) {
panic("Session to database is'nt connected");
return 0;
}
my $version = 30; # Must be increment if rows of table changed
# this tables hasen't handmade user data,
# therefore old table could dropped if updated rows
if(!tableUpdated($self->{dbh},'KEYWORDS',$version,1)) {
return 0;
}
# Look for table or create this table
$self->{dbh}->do(qq|
CREATE TABLE IF NOT EXISTS KEYWORDS (
id int(11) NOT NULL auto_increment,
hash varchar(32) NOT NULL,
keyword varchar(128) NOT NULL,
rank tinyint NOT NULL,
total tinyint NOT NULL,
source enum('recording', 'timer'),
PRIMARY KEY (id),
UNIQUE KEY (hash,keyword)
) COMMENT = '$version'
|);
1;
}
# ------------------
sub insert {
# ------------------
my $self = shift || return error('No object defined!');
my $type = shift || return error('No type defined!');
my $id = shift || return undef;
my $keywords = shift || return undef;
return unless($self->{active} eq 'y');
my $sth = $self->{dbh}->prepare(qq|REPLACE INTO KEYWORDS(hash, keyword, rank, total, source ) VALUES (?,?,?,?,?)|);
my @words = split(/[,;\r\n]/, $keywords);
my $total = scalar @words;
my $rank = $total + 1;
foreach my $w (@words) {
$rank --;
$w =~ s/^\s+//; # no leading white space
$w =~ s/\s+$//; # no trailing white space
next unless($w);
if(!$sth->execute($id,$w,$rank,$total,$type)) {
error sprintf("Couldn't insert keyword!: '%s' !",$w);
return undef;
}
}
return 1;
}
# ------------------
sub remove {
# ------------------
my $self = shift || return error('No object defined!');
my $type = shift || return error('No type defined!');
my $hash = shift || return undef;
return unless($self->{active} eq 'y');
my $sql = sprintf('DELETE FROM KEYWORDS WHERE hash IN (%s)', join(',' => ('?') x @$hash));
my $sth = $self->{dbh}->prepare($sql);
$sth->execute(@$hash)
or return error sprintf("Couldn't execute query: %s.",$sth->errstr);
return 1;
}
# ------------------
sub removesource {
# ------------------
my $self = shift || return error('No object defined!');
my $type = shift || return error('No type defined!');
return unless($self->{active} eq 'y');
my $sth = $self->{dbh}->prepare('DELETE FROM KEYWORDS WHERE source = ?');
$sth->execute($type)
or return error sprintf("Couldn't execute query: %s.",$sth->errstr);
return 1;
}
# ------------------
sub suggest {
# ------------------
my $self = shift || return error('No object defined!');
my $type = shift || return error('No type defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $search = shift;
my $params = shift;
if($search) {
my $sth = $self->{dbh}->prepare(
qq|SELECT SQL_CACHE keyword from KEYWORDS
WHERE source = ?
AND keyword LIKE ?
GROUP BY keyword
LIMIT 25|);
if(!$sth->execute($type, '%'.$search.'%')) {
error sprintf("Couldn't execute query: %s.",$sth->errstr);
} else {
my $result = $sth->fetchall_arrayref();
$console->table($result)
if(ref $console && $result);
}
}
}
# ------------------
sub list {
# ------------------
my $self = shift || return error('No object defined!');
my $type = shift || return error('No type defined!');
my $hash = shift;
return (undef,0,0) unless($self->{active} eq 'y');
# Get keywords with highest ranking
my $list = $self->_list($type,$hash);
return (undef,0,0) unless($list and scalar @$list);
# Remember highest and lowest ranking for scaling
my $keywordmax = $list->[0]->[1];
my $keywordmin = $list->[-1]->[1];
# sort keyworks by name
my $keywords;
@{$keywords} = sort {$a->[0] cmp $b->[0]} @$list;
return ($keywords,$keywordmax,$keywordmin);
}
# ------------------
sub _list {
# ------------------
my $self = shift || return error('No object defined!');
my $type = shift || return error('No type defined!');
my $hash = shift;
my $sth;
if($hash and ref $hash eq 'ARRAY') {
my $sql = sprintf(qq|SELECT SQL_CACHE keyword,sum(100/total*rank) as pos
FROM KEYWORDS
WHERE source = ? AND hash IN (%s)
GROUP BY keyword
ORDER BY pos desc
LIMIT 20|, join(',' => ('?') x @$hash));
unshift(@$hash,$type);
$sth = $self->{dbh}->prepare($sql);
$sth->execute(@$hash)
or return error sprintf("Couldn't execute query: %s.",$sth->errstr);
} else {
my $sql = qq|SELECT SQL_CACHE keyword,sum(100/total*rank) as pos
FROM KEYWORDS
WHERE source = ?
GROUP BY keyword
ORDER BY pos desc
LIMIT 20|;
$sth = $self->{dbh}->prepare($sql);
$sth->execute($type)
or return error sprintf("Couldn't execute query: %s.",$sth->errstr);
}
my $result = $sth->fetchall_arrayref();
return $result;
}
# ------------------
sub timer_keywords {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $text = shift;
my $params = shift;
my $tmod = main::getModule('TIMERS');
unless($text) {
return $tmod->list($console,$config);
}
my $term;
my $search;
my $query = buildsearch("k.keyword",$text);
$search = sprintf('AND ( %s ) AND ( t.id = k.hash )', $query->{query});
foreach(@{$query->{term}}) { push(@{$term},$_); }
return $tmod->_list($console,$config,$search,$term,$params,', KEYWORDS as k');
}
# ------------------
sub recording_keywords {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $text = shift;
my $params = shift;
my $rmod = main::getModule('RECORDS');
unless($text) {
return $rmod->list($console);
}
my $query = buildsearch("k.keyword",$text);
return $rmod->_search($console,$config,$query->{query}.' ) AND ( r.hash = k.hash ',$query->{term},$params,', KEYWORDS as k');
}
# ------------------
sub parsexml {
# ------------------
my $self = shift || return error('No object defined!');
my $aux = shift;
$aux =~ s/(\r|\n)//sg;
if($aux && $aux =~ /^<.*/ ) {
my $args = eval { $self->{xml}->XMLin($aux, KeepRoot => 1 ) } ;
if ($@) {
if($Tools::VERBOSE >= 4) {
error sprintf("Can't parse xml data : %s - %s", $@, $aux);
} elsif($Tools::VERBOSE >= 2) {
error ("Can't parse xml data");
}
return undef;
}
if(defined $args
&& defined $args->{'xxv'} ) {
if($self->{charset} eq 'UTF-8'){
foreach my $k (keys %{$args->{'xxv'}}) {
utf8::downgrade($args->{'xxv'}->{$k});
}
} else {
foreach my $k (keys %{$args->{'xxv'}}) {
$args->{'xxv'}->{$k} = encode($self->{charset},$args->{'xxv'}->{$k});
}
}
return $args->{'xxv'};
}
}
return undef;
}
# ------------------
sub createxml {
# ------------------
my $self = shift || return error('No object defined!');
my $xml = shift;
my $aux = '';
if($xml && keys %$xml) {
$aux = $self->{xml}->XMLout($xml, RootName => 'xxv');
}
return $aux;
}
# ------------------
sub mergexml {
# ------------------
my $self = shift || return error('No object defined!');
my $aux = shift;
my $topic = shift;
my $value = shift;
my $xml = $self->parsexml($aux);
if($value) {
# utf8::upgrade($value) if(!utf8::is_utf8($value));
$xml->{$topic} = $value;
} elsif($xml->{$topic}) {
delete $xml->{$topic};
}
return $self->createxml($xml);
}
1;
|