/
Rename.pm
145 lines (116 loc) · 4.17 KB
/
Rename.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
# 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::Rename;
use strict;
use warnings;
use Error qw(:try);
=begin TML
---++ rest( $session )
This is the REST wrapper for rename.
Takes the following url parameters:
oldtag : name of the tag to be renamed
newtag : new name for the old tag
It checks the prerequisites and sets the following status codes:
200 : Ok
400 : url parameter(s) are missing
403 : the user is not allowed to rename
Return:
In case of an error (!=200) just the status code incl. short description is returned.
Otherwise a 200 and the number of affected tags (usually 0 or 1) is returned.
TODO:
force http POST method
=cut
sub rest {
my $session = shift;
my $query = Foswiki::Func::getCgiQuery();
my $tag_old = $query->param('oldtag') || '';
my $tag_new = $query->param('newtag') || '';
$tag_old = Foswiki::Sandbox::untaintUnchecked($tag_old);
$tag_new = Foswiki::Sandbox::untaintUnchecked($tag_new);
#
# checking prerequisites
#
# first check the existence of all necessary url parameters
#
if ( !$tag_old ) {
$session->{response}->status(400);
return "<h1>400 'oldtag' parameter missing</h1>";
}
if ( !$tag_new ) {
$session->{response}->status(400);
return "<h1>400 'newtag' 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);
# returning the number of affected tags
return Foswiki::Plugins::TagsPlugin::Rename::do( $tag_old, $tag_new );
}
=begin TML
---++ do( $tag_old, $tag_new )
This does untagging.
Takes the following parameters:
tag_old : name of tag to be renamed
tag_new : new name for the old tag
This routine does not check any prerequisites and/or priviledges. It returns 0, if
the old tagname was not found or the new tagname already exists.
Return:
number of affected tags.
=cut
sub do {
my ( $tag_old, $tag_new ) = @_;
my $db = new Foswiki::Contrib::DbiContrib;
# determine tag_id for given tag_old 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_old, 'tag' );
if ( defined( $arrayRef->[0][0] ) ) {
$tag_id = $arrayRef->[0][0];
}
else { return " 0"; }
# check if new tagname already exists by probing for an tag_id
#
$statement = sprintf( 'SELECT %s from %s WHERE %s = ? AND %s = ?',
qw( item_id Items item_name item_type) );
$arrayRef = $db->dbSelect( $statement, $tag_new, 'tag' );
if ( defined( $arrayRef->[0][0] ) ) {
return " 0";
}
# now we are ready to actually rename
#
$statement =
sprintf( 'UPDATE %s SET %s = ? WHERE %s = ?',
qw( Items item_name item_id ) );
Foswiki::Func::writeDebug("Rename: $statement; ($tag_new, $tag_id)");
my $affected_rows = $db->dbInsert( $statement, $tag_new, $tag_id );
if ( $affected_rows eq "0E0" ) { $affected_rows=0; };
# flushing data to dbms
#
$db->commit();
# add extra space, so that zero affected rows does not clash with returning "0" from rest invocation
return " $affected_rows";
}
1;