Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Item1918: adding delete, fixing untags statistics
git-svn-id: http://svn.foswiki.org/trunk/TagsPlugin@4656 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
- Loading branch information
OliverKrueger
authored and
OliverKrueger
committed
Aug 12, 2009
1 parent
cf1f719
commit 1a6e17b
Showing
4 changed files
with
183 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# 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); | ||
|
||
=begin TML | ||
---++ rest( $session ) | ||
This is the REST wrapper for delete. | ||
Delete purges the given tag and all its instances from the database. | ||
Takes the following url parameters: | ||
tag : name of the 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 delete tags | ||
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_text = $query->param('tag') || ''; | ||
$tag_text = Foswiki::Sandbox::untaintUnchecked($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); | ||
|
||
# returning the number of affected tags | ||
return Foswiki::Plugins::TagsPlugin::Delete::do( $tag_text ); | ||
} | ||
|
||
=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. | ||
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 { return 0; } | ||
|
||
# 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters