/
Delete.pm
172 lines (140 loc) · 4.95 KB
/
Delete.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
# This script Copyright (c) 2009 Oliver Krueger, (wiki-one.net)
# and distributed under the GPL (see below)
#
# 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.
#
# 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. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html
#
# Author(s): Oliver Krueger
package Foswiki::Plugins::TagsPlugin::Delete;
use strict;
use warnings;
use Error qw(:try);
use constant DEBUG => 0; # toggle me
=begin TML
---++ rest( $session )
see Foswiki::Plugins::TagsPlugin::deleteCall()
=cut
sub rest {
my $session = shift;
my $query = Foswiki::Func::getCgiQuery();
my $charset = $Foswiki::cfg{Site}{CharSet};
my $tag_text = $query->param('tag') || '';
my $redirectto = $query->param('redirectto') || '';
$tag_text = Foswiki::Sandbox::untaintUnchecked($tag_text);
$redirectto = Foswiki::Sandbox::untaintUnchecked($redirectto);
# input data is assumed to be utf8 (usually in AJAX environments)
require Unicode::MapUTF8;
$tag_text = Unicode::MapUTF8::from_utf8( { -string => $tag_text, -charset => $charset } );
$redirectto = Unicode::MapUTF8::from_utf8( { -string => $redirectto, -charset => $charset } );
# sanatize the tag_text
use Foswiki::Plugins::TagsPlugin::Func;
$tag_text = Foswiki::Plugins::TagsPlugin::Func::normalizeTagname( $tag_text );
#
# checking prerequisites
#
# first check the existence of all necessary url parameters
#
if ( !$tag_text ) {
$session->{response}->status(400);
return "<h1>400 'tag' parameter missing</h1>";
}
# check if current user is allowed to do so
#
my $tagAdminGroup = $Foswiki::cfg{TagsPlugin}{TagAdminGroup} || "AdminGroup";
if ( !Foswiki::Func::isGroupMember( $tagAdminGroup, Foswiki::Func::getWikiName()) ) {
$session->{response}->status(403);
return "<h1>403 Forbidden</h1>";
}
#
# actioning
#
$session->{response}->status(200);
# handle errors and return the number of affected tags
my $retval;
try {
$retval = Foswiki::Plugins::TagsPlugin::Delete::do( $tag_text );
} catch Error::Simple with {
my $e = shift;
my $n = $e->{'-value'};
if ( $n == 1 ) {
$session->{response}->status(404);
return "<h1>404 " . $e->{'-text'} . "</h1>";
} else {
$e->throw();
}
};
# redirect on request
if ( $redirectto ) {
Foswiki::Func::redirectCgiQuery( undef, $redirectto );
}
return $retval;
}
=begin TML
---++ do( $tag_text )
This does the delete including all (user) instances of the tag.
Takes the following parameters:
tag_text : name of the tag
This routine does not check any prerequisites and/or priviledges. It returns 0, if
the given tag_text was not found.
Note: Only use normalized tagnames!
Return:
number of affected tags in the format "n+m" with n as the number of tags and m as the number of (user) tag instances.
=cut
sub do {
my ( $tag_text ) = @_;
my $db = new Foswiki::Contrib::DbiContrib;
my $retval = "";
# determine tag_id for given tag_text and exit if its not there
#
my $tag_id;
my $statement = sprintf( 'SELECT %s from %s WHERE %s = ? AND %s = ?',
qw( item_id Items item_name item_type) );
my $arrayRef = $db->dbSelect( $statement, $tag_text, 'tag' );
if ( defined( $arrayRef->[0][0] ) ) {
$tag_id = $arrayRef->[0][0];
}
else {
throw Error::Simple("Database error: tag not found.", 1);
}
# now we are ready to actually delete
#
# delete tag instances first
$statement =
sprintf( 'DELETE from %s WHERE %s = ?',
qw( UserItemTag tag_id ) );
my $affected_rows = $db->dbDelete( $statement, $tag_id );
if ( $affected_rows eq "0E0" ) { $affected_rows=0; };
$retval = "$affected_rows";
# then delete the tag itself
$statement =
sprintf( 'DELETE from %s WHERE %s = ?',
qw( Items item_id ) );
$affected_rows = $db->dbDelete( $statement, $tag_id );
if ( $affected_rows eq "0E0" ) { $affected_rows=0; };
$retval = "$affected_rows+$retval";
# update statistics
#
# ...in UserTagStat
$statement =
sprintf( 'DELETE from %s WHERE %s = ?',
qw( UserTagStat tag_id ) );
$affected_rows = $db->dbDelete( $statement, $tag_id );
# ...in TagStat
$statement =
sprintf( 'DELETE from %s WHERE %s = ?',
qw( TagStat item_id ) );
$affected_rows = $db->dbDelete( $statement, $tag_id );
# flushing data to dbms
#
$db->commit();
return $retval;
}
1;