Lines 40-46
BEGIN {
Link Here
|
40 |
&add_tags &add_tag |
40 |
&add_tags &add_tag |
41 |
&delete_tag_row_by_id |
41 |
&delete_tag_row_by_id |
42 |
&remove_tag |
42 |
&remove_tag |
43 |
&delete_tag_rows_by_ids |
|
|
44 |
&get_approval_rows |
43 |
&get_approval_rows |
45 |
&blacklist |
44 |
&blacklist |
46 |
&whitelist |
45 |
&whitelist |
Lines 74-107
INIT {
Link Here
|
74 |
$debug and print STDERR "\$Lingua::Ispell::path = $Lingua::Ispell::path\n"; |
73 |
$debug and print STDERR "\$Lingua::Ispell::path = $Lingua::Ispell::path\n"; |
75 |
} |
74 |
} |
76 |
|
75 |
|
77 |
sub get_filters { |
|
|
78 |
my $query = "SELECT * FROM tags_filters "; |
79 |
my ($sth); |
80 |
if (@_) { |
81 |
$sth = C4::Context->dbh->prepare($query . " WHERE filter_id = ? "); |
82 |
$sth->execute(shift); |
83 |
} else { |
84 |
$sth = C4::Context->dbh->prepare($query); |
85 |
$sth->execute; |
86 |
} |
87 |
return $sth->fetchall_arrayref({}); |
88 |
} |
89 |
|
90 |
# (SELECT count(*) FROM tags_all ) as tags_all, |
91 |
# (SELECT count(*) FROM tags_index ) as tags_index, |
92 |
|
93 |
sub approval_counts { |
76 |
sub approval_counts { |
94 |
my $query = "SELECT |
77 |
my $rs = Koha::Database->new()->schema()->resultset('TagsApproval')->search( |
95 |
(SELECT count(*) FROM tags_approval WHERE approved= 1) as approved_count, |
78 |
{ |
96 |
(SELECT count(*) FROM tags_approval WHERE approved=-1) as rejected_count, |
79 |
approved => [ 1, -1, 0 ] |
97 |
(SELECT count(*) FROM tags_approval WHERE approved= 0) as unapproved_count |
80 |
} |
98 |
"; |
81 |
); |
99 |
my $sth = C4::Context->dbh->prepare($query); |
82 |
|
100 |
$sth->execute; |
83 |
return { |
101 |
my $result = $sth->fetchrow_hashref(); |
84 |
approved_total => $rs->count(), |
102 |
$result->{approved_total} = $result->{approved_count} + $result->{rejected_count} + $result->{unapproved_count}; |
85 |
approved_count => $rs->count( { approved => 1 } ), |
103 |
$debug and warn "counts returned: " . Dumper $result; |
86 |
rejected_count => $rs->count( { approved => -1 } ), |
104 |
return $result; |
87 |
unapproved_count => $rs->count( { approved => 0 } ), |
|
|
88 |
} |
105 |
} |
89 |
} |
106 |
|
90 |
|
107 |
=head2 get_count_by_tag_status |
91 |
=head2 get_count_by_tag_status |
Lines 112-125
Takes a status and gets a count of tags with that status
Link Here
|
112 |
|
96 |
|
113 |
=cut |
97 |
=cut |
114 |
|
98 |
|
115 |
sub get_count_by_tag_status { |
99 |
sub get_count_by_tag_status { |
116 |
my ($status) = @_; |
100 |
my ($status) = @_; |
117 |
my $dbh = C4::Context->dbh; |
101 |
return Koha::Database->new()->schema()->resultset('TagsApproval')->count( |
118 |
my $query = |
102 |
{ |
119 |
"SELECT count(*) FROM tags_approval WHERE approved=?"; |
103 |
approved => $status |
120 |
my $sth = $dbh->prepare($query); |
104 |
} |
121 |
$sth->execute( $status ); |
105 |
); |
122 |
return $sth->fetchrow; |
|
|
123 |
} |
106 |
} |
124 |
|
107 |
|
125 |
sub remove_tag { |
108 |
sub remove_tag { |
Lines 151-182
sub remove_tag {
Link Here
|
151 |
} |
134 |
} |
152 |
|
135 |
|
153 |
sub delete_tag_index { |
136 |
sub delete_tag_index { |
154 |
(@_) or return; |
137 |
my ( $term, $biblionumber ) = @_; |
155 |
my $sth = C4::Context->dbh->prepare("DELETE FROM tags_index WHERE term = ? AND biblionumber = ? LIMIT 1"); |
138 |
return Koha::Database->new()->schema()->resultset('TagsIndex')->search( |
156 |
$sth->execute(@_); |
139 |
{ |
157 |
return $sth->rows || 0; |
140 |
term => $term, |
|
|
141 |
biblionumber => $biblionumber, |
142 |
} |
143 |
)->delete(); |
158 |
} |
144 |
} |
|
|
145 |
|
159 |
sub delete_tag_approval { |
146 |
sub delete_tag_approval { |
160 |
(@_) or return; |
147 |
my ($term) = @_; |
161 |
my $sth = C4::Context->dbh->prepare("DELETE FROM tags_approval WHERE term = ? LIMIT 1"); |
148 |
my $t = Koha::Database->new()->schema()->resultset('TagsApproval')->find( $term ); |
162 |
$sth->execute(shift); |
149 |
return $t ? $t->delete() : 0; |
163 |
return $sth->rows || 0; |
|
|
164 |
} |
150 |
} |
|
|
151 |
|
165 |
sub delete_tag_row_by_id { |
152 |
sub delete_tag_row_by_id { |
166 |
(@_) or return; |
153 |
my ( $tag_id ) = @_; |
167 |
my $sth = C4::Context->dbh->prepare("DELETE FROM tags_all WHERE tag_id = ? LIMIT 1"); |
154 |
my $t = Koha::Database->new()->schema()->resultset('TagAll')->find( $tag_id ); |
168 |
$sth->execute(shift); |
155 |
return $t ? $t->delete() : 0; |
169 |
return $sth->rows || 0; |
|
|
170 |
} |
171 |
sub delete_tag_rows_by_ids { |
172 |
(@_) or return; |
173 |
my $i=0; |
174 |
foreach(@_) { |
175 |
$i += delete_tag_row_by_id($_); |
176 |
} |
177 |
($i == scalar(@_)) or |
178 |
warn sprintf "delete_tag_rows_by_ids tried %s tag_ids, only succeeded on $i", scalar(@_); |
179 |
return $i; |
180 |
} |
156 |
} |
181 |
|
157 |
|
182 |
sub get_tag_rows { |
158 |
sub get_tag_rows { |
Lines 365-391
sub get_approval_rows { # i.e., from tags_approval
Link Here
|
365 |
} |
341 |
} |
366 |
|
342 |
|
367 |
sub is_approved { |
343 |
sub is_approved { |
368 |
my $term = shift or return; |
344 |
my $term = shift or return; |
369 |
my $sth = C4::Context->dbh->prepare("SELECT approved FROM tags_approval WHERE term = ?"); |
|
|
370 |
$sth->execute($term); |
371 |
unless ($sth->rows) { |
372 |
$ext_dict and return (spellcheck($term) ? 0 : 1); # spellcheck returns empty on OK word |
373 |
return 0; |
374 |
} |
375 |
return $sth->fetchrow; |
376 |
} |
377 |
|
345 |
|
378 |
sub get_tag_index { |
346 |
my $tag = Koha::Database->new()->schema()->resultset('TagsIndex')->find( |
379 |
my $term = shift or return; |
347 |
{ term => $term }, |
380 |
my $sth; |
348 |
{ result_class => 'DBIx::Class::ResultClass::HashRefInflator' } |
381 |
if (@_) { |
349 |
); |
382 |
$sth = C4::Context->dbh->prepare("SELECT * FROM tags_index WHERE term = ? AND biblionumber = ?"); |
350 |
|
383 |
$sth->execute($term,shift); |
351 |
unless ($tag) { |
384 |
} else { |
352 |
return $ext_dict ? spellcheck($term) ? 0 : 1 : 0; |
385 |
$sth = C4::Context->dbh->prepare("SELECT * FROM tags_index WHERE term = ?"); |
353 |
} |
386 |
$sth->execute($term); |
354 |
|
387 |
} |
355 |
return $tag; |
388 |
return $sth->fetchrow_hashref; |
|
|
389 |
} |
356 |
} |
390 |
|
357 |
|
391 |
sub whitelist { |
358 |
sub whitelist { |
Lines 407-412
sub whitelist {
Link Here
|
407 |
} |
374 |
} |
408 |
return scalar @_; |
375 |
return scalar @_; |
409 |
} |
376 |
} |
|
|
377 |
|
410 |
# note: there is no "unwhitelist" operation because there is no remove for Ispell. |
378 |
# note: there is no "unwhitelist" operation because there is no remove for Ispell. |
411 |
# The blacklist regexps should operate "in front of" the whitelist, so if you approve |
379 |
# The blacklist regexps should operate "in front of" the whitelist, so if you approve |
412 |
# a term mistakenly, you can still reverse it. But there is no going back to "neutral". |
380 |
# a term mistakenly, you can still reverse it. But there is no going back to "neutral". |
Lines 423-576
sub blacklist {
Link Here
|
423 |
} |
391 |
} |
424 |
return scalar @_; |
392 |
return scalar @_; |
425 |
} |
393 |
} |
426 |
sub add_filter { |
|
|
427 |
my $operator = shift; |
428 |
defined $operator or return; # have to test defined to allow =0 (kohaadmin) |
429 |
my $query = "INSERT INTO tags_blacklist (regexp,y,z) VALUES (?,?,?)"; |
430 |
# my $sth = C4::Context->dbh->prepare($query); |
431 |
return scalar @_; |
432 |
} |
433 |
sub remove_filter { |
434 |
my $operator = shift; |
435 |
defined $operator or return; # have to test defined to allow =0 (kohaadmin) |
436 |
my $query = "REMOVE FROM tags_blacklist WHERE blacklist_id = ?"; |
437 |
# my $sth = C4::Context->dbh->prepare($query); |
438 |
# $sth->execute($term); |
439 |
return scalar @_; |
440 |
} |
441 |
|
394 |
|
442 |
sub add_tag_approval { # or disapproval |
395 |
sub add_tag_approval { # or disapproval |
443 |
$debug and warn "add_tag_approval(" . join(", ",map {defined($_) ? $_ : 'UNDEF'} @_) . ")"; |
396 |
$debug and warn "add_tag_approval(" . join(", ",map {defined($_) ? $_ : 'UNDEF'} @_) . ")"; |
444 |
my $term = shift or return; |
397 |
my $term = shift or return; |
445 |
my $query = "SELECT * FROM tags_approval WHERE term = ?"; |
398 |
my $operator = shift || 0; |
446 |
my $sth = C4::Context->dbh->prepare($query); |
399 |
my $approval = ( @_ ? shift : 0 ); # default is unapproved |
447 |
$sth->execute($term); |
400 |
|
448 |
($sth->rows) and return increment_weight_total($term); |
401 |
my $rs = Koha::Database->new()->schema()->resultset('TagsApproval'); |
449 |
my $operator = shift || 0; |
402 |
|
450 |
my $approval = (@_ ? shift : 0); # default is unapproved |
403 |
my $count = $rs->count( { term => $term } ); |
451 |
my @exe_args = ($term); # all 3 queries will use this argument |
404 |
return increment_weight_total($term) if $count; |
452 |
if ($operator) { |
405 |
|
453 |
$query = "INSERT INTO tags_approval (term,approved_by,approved,date_approved) VALUES (?,?,?,NOW())"; |
406 |
return $rs->create( |
454 |
push @exe_args, $operator, $approval; |
407 |
{ |
455 |
} elsif ($approval) { |
408 |
term => $term, |
456 |
$query = "INSERT INTO tags_approval (term,approved,date_approved) VALUES (?,?,NOW())"; |
409 |
approved => $approval, |
457 |
push @exe_args, $approval; |
410 |
approved_by => $operator || undef, |
458 |
} else { |
411 |
date_approved => \'NOW()', |
459 |
$query = "INSERT INTO tags_approval (term,date_approved) VALUES (?,NOW())"; |
412 |
} |
460 |
} |
413 |
); |
461 |
$debug and print STDERR "add_tag_approval query: $query\nadd_tag_approval args: (" . join(", ", @exe_args) . ")\n"; |
|
|
462 |
$sth = C4::Context->dbh->prepare($query); |
463 |
$sth->execute(@exe_args); |
464 |
return $sth->rows; |
465 |
} |
414 |
} |
466 |
|
415 |
|
467 |
sub mod_tag_approval { |
416 |
sub mod_tag_approval { |
468 |
my $operator = shift; |
417 |
my $operator = shift; |
469 |
defined $operator or return; # have to test defined to allow =0 (kohaadmin) |
418 |
defined $operator or return; # have to test defined to allow = 0 (kohaadmin) |
470 |
my $term = shift or return; |
419 |
my $term = shift or return; |
471 |
my $approval = (scalar @_ ? shift : 1); # default is to approve |
420 |
my $approval = (scalar @_ ? shift : 1); # default is to approve |
472 |
my $query = "UPDATE tags_approval SET approved_by=?, approved=?, date_approved=NOW() WHERE term = ?"; |
421 |
|
473 |
$debug and print STDERR "mod_tag_approval query: $query\nmod_tag_approval args: ($operator,$approval,$term)\n"; |
422 |
return Koha::Database->new()->schema()->resultset('TagsApproval') |
474 |
my $sth = C4::Context->dbh->prepare($query); |
423 |
->search( { term => $term } )->update( |
475 |
$sth->execute($operator,$approval,$term); |
424 |
{ |
|
|
425 |
approved_by => $operator, |
426 |
approved => $approval, |
427 |
date_approved => \'NOW()', |
428 |
} |
429 |
); |
476 |
} |
430 |
} |
477 |
|
431 |
|
478 |
sub add_tag_index { |
432 |
sub add_tag_index { |
479 |
my $term = shift or return; |
433 |
my $term = shift or return; |
480 |
my $biblionumber = shift or return; |
434 |
my $biblionumber = shift or return; |
481 |
my $query = "SELECT * FROM tags_index WHERE term = ? AND biblionumber = ?"; |
435 |
|
482 |
my $sth = C4::Context->dbh->prepare($query); |
436 |
my $rs = |
483 |
$sth->execute($term,$biblionumber); |
437 |
Koha::Database->new()->schema()->resultset('TagsIndex'); |
484 |
($sth->rows) and return increment_weight($term,$biblionumber); |
438 |
|
485 |
$query = "INSERT INTO tags_index (term,biblionumber) VALUES (?,?)"; |
439 |
my $count = $rs->count( |
486 |
$debug and print STDERR "add_tag_index query: $query\nadd_tag_index args: ($term,$biblionumber)\n"; |
440 |
{ |
487 |
$sth = C4::Context->dbh->prepare($query); |
441 |
term => $term, |
488 |
$sth->execute($term,$biblionumber); |
442 |
biblionumber => $biblionumber, |
489 |
return $sth->rows; |
443 |
} |
|
|
444 |
); |
445 |
|
446 |
return increment_weight( $term, $biblionumber ) if $count; |
447 |
|
448 |
return $rs->create( |
449 |
{ |
450 |
term => $term, |
451 |
biblionumber => $biblionumber |
452 |
} |
453 |
); |
490 |
} |
454 |
} |
491 |
|
455 |
|
492 |
sub get_tag { # by tag_id |
456 |
sub get_tag { # by tag_id |
493 |
(@_) or return; |
457 |
my ($tag_id) = @_; |
494 |
my $sth = C4::Context->dbh->prepare(TAG_SELECT . "WHERE tag_id = ?"); |
458 |
|
495 |
$sth->execute(shift); |
459 |
return Koha::Database->new()->schema()->resultset('TagAll')->find( |
496 |
return $sth->fetchrow_hashref; |
460 |
$tag_id, |
|
|
461 |
{ |
462 |
result_class => 'DBIx::Class::ResultClass::HashRefInflator', |
463 |
} |
464 |
); |
497 |
} |
465 |
} |
498 |
|
466 |
|
499 |
sub increment_weights { |
467 |
sub increment_weights { |
500 |
increment_weight(@_); |
468 |
increment_weight(@_); |
501 |
increment_weight_total(shift); |
469 |
increment_weight_total(shift); |
502 |
} |
470 |
} |
|
|
471 |
|
503 |
sub decrement_weights { |
472 |
sub decrement_weights { |
504 |
decrement_weight(@_); |
473 |
decrement_weight(@_); |
505 |
decrement_weight_total(shift); |
474 |
decrement_weight_total(shift); |
506 |
} |
475 |
} |
|
|
476 |
|
507 |
sub increment_weight_total { |
477 |
sub increment_weight_total { |
508 |
_set_weight_total('weight_total+1',shift); |
478 |
_set_weight_total('weight_total+1',shift); |
509 |
} |
479 |
} |
|
|
480 |
|
510 |
sub increment_weight { |
481 |
sub increment_weight { |
511 |
_set_weight('weight+1',shift,shift); |
482 |
_set_weight('weight+1',shift,shift); |
512 |
} |
483 |
} |
|
|
484 |
|
513 |
sub decrement_weight_total { |
485 |
sub decrement_weight_total { |
514 |
_set_weight_total('weight_total-1',shift); |
486 |
_set_weight_total('weight_total-1',shift); |
515 |
} |
487 |
} |
|
|
488 |
|
516 |
sub decrement_weight { |
489 |
sub decrement_weight { |
517 |
_set_weight('weight-1',shift,shift); |
490 |
_set_weight('weight-1',shift,shift); |
518 |
} |
491 |
} |
|
|
492 |
|
519 |
sub _set_weight_total { |
493 |
sub _set_weight_total { |
520 |
my $sth = C4::Context->dbh->prepare(" |
494 |
my ( $weight_total, $term ) = @_; |
521 |
UPDATE tags_approval |
495 |
|
522 |
SET weight_total=" . (shift) . " |
496 |
$term = |
523 |
WHERE term=? |
497 |
Koha::Database->new()->schema()->resultset('TagsApproval')->find($term); |
524 |
"); # note: CANNOT use "?" for weight_total (see the args above). |
498 |
|
525 |
$sth->execute(shift); # just the term |
499 |
$term->update( { weight_total => $weight_total } ) if $term; |
526 |
} |
500 |
} |
|
|
501 |
|
527 |
sub _set_weight { |
502 |
sub _set_weight { |
528 |
my $dbh = C4::Context->dbh; |
503 |
my ( $weight, $term, $biblionumber ) = @_; |
529 |
my $sth = $dbh->prepare(" |
504 |
|
530 |
UPDATE tags_index |
505 |
my $t = |
531 |
SET weight=" . (shift) . " |
506 |
Koha::Database->new()->schema()->resultset('TagsIndex') |
532 |
WHERE term=? |
507 |
->find( { term => $term, biblionumber => $biblionumber } ); |
533 |
AND biblionumber=? |
508 |
|
534 |
"); |
509 |
$term->update( { weight => $weight } ) if $t; |
535 |
$sth->execute(@_); |
|
|
536 |
} |
510 |
} |
537 |
|
511 |
|
538 |
sub add_tag { # biblionumber,term,[borrowernumber,approvernumber] |
512 |
sub add_tag { # biblionumber,term,[borrowernumber,approvernumber] |
539 |
my $biblionumber = shift or return; |
513 |
my $biblionumber = shift or return; |
540 |
my $term = shift or return; |
514 |
my $term = shift or return; |
541 |
my $borrowernumber = (@_) ? shift : 0; # the user, default to kohaadmin |
515 |
my $borrowernumber = (@_) ? shift : 0; # the user, default to kohaadmin |
542 |
$term =~ s/^\s+//; |
516 |
|
543 |
$term =~ s/\s+$//; |
517 |
$term =~ s/^\s+//; |
544 |
($term) or return; # must be more than whitespace |
518 |
$term =~ s/\s+$//; |
545 |
my $rows = get_tag_rows({biblionumber=>$biblionumber, borrowernumber=>$borrowernumber, term=>$term, limit=>1}); |
519 |
($term) or return; # must be more than whitespace |
546 |
my $query = "INSERT INTO tags_all |
520 |
|
547 |
(borrowernumber,biblionumber,term,date_created) |
521 |
my $rows = get_tag_rows( |
548 |
VALUES (?,?,?,NOW())"; |
522 |
{ |
549 |
$debug and print STDERR "add_tag query: $query\n", |
523 |
biblionumber => $biblionumber, |
550 |
"add_tag query args: ($borrowernumber,$biblionumber,$term)\n"; |
524 |
borrowernumber => $borrowernumber, |
551 |
if (scalar @$rows) { |
525 |
term => $term, |
552 |
$debug and carp "Duplicate tag detected. Tag not added."; |
526 |
limit => 1 |
553 |
return; |
527 |
} |
554 |
} |
528 |
); |
555 |
# add to tags_all regardless of approaval |
529 |
|
556 |
my $sth = C4::Context->dbh->prepare($query); |
530 |
if ( scalar @$rows ) { |
557 |
$sth->execute($borrowernumber,$biblionumber,$term); |
531 |
$debug and carp "Duplicate tag detected. Tag not added."; |
558 |
|
532 |
return; |
559 |
# then |
533 |
} |
560 |
if (scalar @_) { # if arg remains, it is the borrowernumber of the approver: tag is pre-approved. |
534 |
|
561 |
my $approver = shift; |
535 |
my $t = Koha::Database->new()->schema()->resultset('TagAll')->create( |
562 |
$debug and print STDERR "term '$term' pre-approved by borrower #$approver\n"; |
536 |
{ |
563 |
add_tag_approval($term,$approver,1); |
537 |
borrowernumber => $borrowernumber, |
564 |
add_tag_index($term,$biblionumber,$approver); |
538 |
biblionumber => $biblionumber, |
565 |
} elsif (is_approved($term) >= 1) { |
539 |
term => $term, |
566 |
$debug and print STDERR "term '$term' approved by whitelist\n"; |
540 |
date_created => \'NOW()', |
567 |
add_tag_approval($term,0,1); |
541 |
} |
568 |
add_tag_index($term,$biblionumber,1); |
542 |
); |
569 |
} else { |
543 |
|
570 |
$debug and print STDERR "term '$term' NOT approved (yet)\n"; |
544 |
|
571 |
add_tag_approval($term); |
545 |
if ( scalar @_ ) |
572 |
add_tag_index($term,$biblionumber); |
546 |
{ # if arg remains, it is the borrowernumber of the approver: tag is pre-approved. |
573 |
} |
547 |
my $approver = shift; |
|
|
548 |
$debug |
549 |
and print STDERR "term '$term' pre-approved by borrower #$approver\n"; |
550 |
add_tag_approval( $term, $approver, 1 ); |
551 |
add_tag_index( $term, $biblionumber, $approver ); |
552 |
} |
553 |
elsif ( is_approved($term) >= 1 ) { |
554 |
$debug and print STDERR "term '$term' approved by whitelist\n"; |
555 |
add_tag_approval( $term, 0, 1 ); |
556 |
add_tag_index( $term, $biblionumber, 1 ); |
557 |
} |
558 |
else { |
559 |
$debug and print STDERR "term '$term' NOT approved (yet)\n"; |
560 |
add_tag_approval($term); |
561 |
add_tag_index( $term, $biblionumber ); |
562 |
} |
574 |
} |
563 |
} |
575 |
|
564 |
|
576 |
# This takes a set of tags, as returned by C<get_approval_rows> and divides |
565 |
# This takes a set of tags, as returned by C<get_approval_rows> and divides |
577 |
- |
|
|