Skip to content

Commit

Permalink
Item1918: adding delete, fixing untags statistics
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 18 deletions.
17 changes: 10 additions & 7 deletions lib/Foswiki/Plugins/TagsPlugin.pm
Expand Up @@ -52,13 +52,11 @@ sub initPlugin {
Foswiki::Func::registerTagHandler( 'TAGENTRY', \&_TAGENTRY );
Foswiki::Func::registerTagHandler( 'TAGCLOUD', \&_TAGCLOUD );

Foswiki::Func::registerRESTHandler( 'tag', \&tagCall );
Foswiki::Func::registerRESTHandler( 'untag', \&untagCall );

# Foswiki::Func::registerRESTHandler('updateGeoTags', \&updateGeoTags);

Foswiki::Func::registerRESTHandler( 'initialiseDatabase',
\&initialiseDatabase );
Foswiki::Func::registerRESTHandler( 'tag', \&tagCall );
Foswiki::Func::registerRESTHandler( 'untag', \&untagCall );
Foswiki::Func::registerRESTHandler( 'delete', \&deleteCall );
# Foswiki::Func::registerRESTHandler('updateGeoTags', \&updateGeoTags);
Foswiki::Func::registerRESTHandler( 'initialiseDatabase', \&initialiseDatabase );

#TODO: augment the IfParser and the QuerySearch Parsers to add Tags?

Expand Down Expand Up @@ -322,6 +320,11 @@ sub untagCall {
return Foswiki::Plugins::TagsPlugin::Untag::rest( @_ );
}

sub deleteCall {
use Foswiki::Plugins::TagsPlugin::Delete;
return Foswiki::Plugins::TagsPlugin::Delete::rest( @_ );
}

sub getUserId {
my $session = shift;

Expand Down
6 changes: 5 additions & 1 deletion lib/Foswiki/Plugins/TagsPlugin/Config.spec
Expand Up @@ -10,4 +10,8 @@ $Foswiki::cfg{TagsPlugin}{EnableCategories} = 1;

# **BOOLEAN**
# automatically create tags for attached DataForms
$Foswiki::cfg{TagsPlugin}{EnableDataForms} = 1;
$Foswiki::cfg{TagsPlugin}{EnableDataForms} = 1;

# **STRING**
# Name of the TagAdminGroup (which is allowed to delete and merge tags)
$Foswiki::cfg{TagsPlugin}{TagAdminGroup} = "AdminGroup";
150 changes: 150 additions & 0 deletions lib/Foswiki/Plugins/TagsPlugin/Delete.pm
@@ -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;
28 changes: 18 additions & 10 deletions lib/Foswiki/Plugins/TagsPlugin/Untag.pm
Expand Up @@ -26,23 +26,25 @@ use Error qw(:try);
This is the REST wrapper for untag.
Takes the following url parameters:
item : name of the topic to be untagged (format: Sandbox.TestTopic)
tag : name of the tag
user : (optional) Wikiname of the user or group, whose tag shall be deleted (format: JoeDoe)
item : name of the topic to be untagged (format: Sandbox.TestTopic)
tag : name of the tag
user : (optional) Wikiname of the user or group, whose tag shall be deleted (format: JoeDoe)
If "user" is a groupname, the currently logged in user has to be member of that group.
Guest user is only permitted, if he wants to delete his own tags.
It checks the prerequisites and sets the following status codes:
200 : Ok
400 : url parameter(s) are missing
401 : access denied for unauthorized user
403 : the user is not allowed to untag
500 : server error (presumably db related)
200 : Ok
400 : url parameter(s) are missing
401 : access denied for unauthorized user
403 : the user is not allowed to untag
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 {
Expand Down Expand Up @@ -172,19 +174,25 @@ sub do {

# update statistics
#
# altering only UserItemStat
if ( $affected_rows > 0 ) {
# ...in UserTagStat
$statement =
sprintf( 'UPDATE %s SET %s=%s-1 WHERE %s = ? AND %s = ?',
qw( UserTagStat num_items num_items tag_id user_id) );
my $modified = $db->dbInsert( $statement, $tag_id, $cuid );
# Foswiki::Func::writeDebug("Untag: $statement; ($tag_id, $cuid) -> $modified") if $debug;

# ... in TagStat
$statement =
sprintf( 'UPDATE %s SET %s=%s-1 WHERE %s = ?',
qw( TagStat num_items num_items tag_id) );
$modified = $db->dbInsert( $statement, $tag_id );
}

# 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";
}

Expand Down

0 comments on commit 1a6e17b

Please sign in to comment.