Skip to content

Commit

Permalink
Item1918: changed the impl of the public flag
Browse files Browse the repository at this point in the history
- decoupled public from user (WikiGuest)
- CHANGED the DB SCHEMA

git-svn-id: http://svn.foswiki.org/trunk/TagsPlugin@5360 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
OliverKrueger authored and OliverKrueger committed Oct 22, 2009
1 parent 500e2fc commit 00ffece
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 39 deletions.
2 changes: 1 addition & 1 deletion data/System/VarTAGCLOUDCLICK.txt
Expand Up @@ -12,5 +12,5 @@
| =sourceweb="Name"= <br /> =sourceweb="%USERSWEB%, Know"= <br /> =sourceweb="all"= | Comma-separated list of webs to render the tagcloud. The special word =all= means all webs (inlcuding those which have the =NOSEARCHALL= preference set to =on=). Use =%= as a wildcard. | ="all"= |
| =targetweb="Name"= | Webname of the target topic. This is where the tags are posted to. | =current web= |
| =targettopic="Name"= | Topicname of the target topic. This is where the tags are posted to. | =current topic= |
| =targetuser="wikiname"= | New tags are created with this ownership. | =%TAGPUBLIC%= |
| =targetuser="wikiname"= | New tags are created with this ownership. | =%<nop>TAGSPLUGIN_TAGUSER%= or current user |
| =cloudtopic="Web.Topic"= | This topic is used (included) to actually render the tagcloud. | =%SYSTEMWEB%.<nop>TagsPluginTagCloud= |
44 changes: 35 additions & 9 deletions lib/Foswiki/Plugins/TagsPlugin.pm
Expand Up @@ -45,13 +45,23 @@ sub initPlugin {
return 0;
}

# check db_schema_version
my $workArea = Foswiki::Func::getWorkArea( $pluginName );
if ( $workArea !~ m/.*\/$/ ) {
$workArea .= "/";
};
if ( Foswiki::Func::readFile( $workArea . "db_schema_version.txt" ) !~ m/^1\.1$/ ) {
Foswiki::Func::writeWarning(
"DB schema Version mismatch. Please convert your database.");
return 0;
}

Foswiki::Func::registerTagHandler( 'TAGLIST', \&_TAGLIST );
Foswiki::Func::registerTagHandler( 'TAGENTRY', \&_TAGENTRY );
Foswiki::Func::registerTagHandler( 'TAGCLOUD', \&_TAGCLOUD ) if ( defined($Foswiki::cfg{TagsPlugin}{EnableTagCloud}) && $Foswiki::cfg{TagsPlugin}{EnableTagCloud} );
Foswiki::Func::registerTagHandler( 'TAGCLOUDCLICK', \&_TAGCLOUDCLICK );
Foswiki::Func::registerTagHandler( 'TAGSEARCH', \&_TAGSEARCH );
Foswiki::Func::registerTagHandler( 'TAGGROUPS', \&_TAGGROUPS );
Foswiki::Func::registerTagHandler( 'TAGPUBLIC', \&_TAGPUBLIC );
Foswiki::Func::registerTagHandler( 'ISTAGADMIN', \&_ISTAGADMIN );
Foswiki::Func::registerTagHandler( 'TAGREQUIRE', \&_TAGREQUIRE );

Expand All @@ -61,6 +71,7 @@ sub initPlugin {
Foswiki::Func::registerRESTHandler( 'rename', \&renameCall );
Foswiki::Func::registerRESTHandler( 'merge', \&mergeCall );
Foswiki::Func::registerRESTHandler( 'initialiseDatabase', \&initialiseDatabase );
Foswiki::Func::registerRESTHandler( 'convertDatabase', \&convertDatabase );
# Foswiki::Func::registerRESTHandler('updateGeoTags', \&updateGeoTags);

#TODO: augment the IfParser and the QuerySearch Parsers to add Tags?
Expand All @@ -70,8 +81,7 @@ sub initPlugin {
# plus add some data through meta tags
my $tagweb = Foswiki::Func::getPreferencesValue("TAGWEB") || $web;
my $tagtopic = Foswiki::Func::getPreferencesValue("TAGTOPIC") || $topic;
my $header = '<meta name="foswiki.tagsplugin.public" content="%TAGPUBLIC%" />'."\n";
$header .= '<meta name="foswiki.tagsplugin.defaultuser" content="%TAGSPLUGIN_TAGUSER%" />'."\n";
my $header = '<meta name="foswiki.tagsplugin.defaultuser" content="%TAGSPLUGIN_TAGUSER%" />'."\n";
$header .= '<meta name="foswiki.tagsplugin.web" content="'.$tagweb.'" />'."\n";
$header .= '<meta name="foswiki.tagsplugin.topic" content="'.$tagtopic.'" />'."\n";
$header .= '<link rel="stylesheet" type="text/css" href="%PUBURL%/System/TagsPlugin/tagsplugin.css" media="all" />'."\n";
Expand Down Expand Up @@ -327,11 +337,6 @@ sub _ISTAGADMIN {
}
}

sub _TAGPUBLIC {
my $guest = $Foswiki::cfg{DefaultUserWikiName} || "!DefaultUserWikiName NOT DEFINED!";
return $guest;
}

sub _TAGREQUIRE {
my ( $session, $params, $theTopic, $theWeb ) = @_;
my $what = lc( $params->{"_DEFAULT"} || "" );
Expand Down Expand Up @@ -663,7 +668,8 @@ CREATE TABLE IF NOT EXISTS `UserItemTag` (
`user_id` int(10) unsigned NOT NULL,
`item_id` int(10) unsigned NOT NULL,
`tag_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`user_id`,`item_id`,`tag_id`),
`public` int(10) unsigned NOT NULL,
PRIMARY KEY (`user_id`,`item_id`,`tag_id`,`public`),
KEY `item_id` (`item_id`),
KEY `tag_id` (`tag_id`),
CONSTRAINT `UserItemTag_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `Users` (`CUID`),
Expand Down Expand Up @@ -703,7 +709,27 @@ END

$db->disconnect(); #force a commit

# write the version of the db schema to a file in the work area
my $workArea = Foswiki::Func::getWorkArea( $pluginName );
if ( $workArea !~ m/.*\/$/ ) {
$workArea .= "/";
};
Foswiki::Func::saveFile( $workArea . "db_schema_version.txt", "1.1" );

return 'ok ' . $count;
}

sub convertDatabase {

#TODO: if there are more than two schemata to convert between, this needs to be more complex
my $db = new Foswiki::Contrib::DbiContrib;
my $statement = <<'END';
ALTER TABLE `foswiki106`.`UserItemTag` ADD COLUMN `public` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `tag_id`;
END
my $arrayRef = $db->dbInsert($statement);
$db->disconnect(); #force a commit

return "ok";
}

1;
6 changes: 4 additions & 2 deletions lib/Foswiki/Plugins/TagsPlugin/TAGSEARCH.pm
Expand Up @@ -163,6 +163,7 @@ SELECT
t.item_name as tag,
i.item_name as topic,
u.FoswikicUID as user,
i2t.public as public,
ts.num_items as count
FROM Items t
INNER JOIN UserItemTag i2t ON i2t.tag_id=t.item_id
Expand All @@ -183,7 +184,8 @@ $order";
my $tag = $row->[0];
my $item = $row->[1];
my $cuid = $row->[2];
my $tag_count = $row->[3];
my $public = $row->[3];
my $tag_count = $row->[4];
my $user = Foswiki::Func::getWikiName($cuid);

# replace all variable occurrences
Expand Down Expand Up @@ -217,7 +219,7 @@ $order";
}

# flag this entry as public
if ( $user eq $Foswiki::cfg{DefaultUserWikiName} ) {
if ( $public ) {
$entry =~ s/\$public/tagsplugin_public/g;
} else {
$entry =~ s/\$public//g;
Expand Down
13 changes: 8 additions & 5 deletions lib/Foswiki/Plugins/TagsPlugin/Tag.pm
Expand Up @@ -41,6 +41,7 @@ sub rest {
my $tag_text = $query->param('tag');
my $redirectto = $query->param('redirectto') || '';
my $user = $query->param('user') || Foswiki::Func::getWikiName();
my $public = ( $query->param('public') eq "1" ) ? "1" : "0";
my $tagAdminGroup = $Foswiki::cfg{TagsPlugin}{TagAdminGroup} || "AdminGroup";

$item_name = Foswiki::Sandbox::untaintUnchecked($item_name);
Expand Down Expand Up @@ -115,7 +116,7 @@ sub rest {
Foswiki::Func::writeDebug("ID: $user_id") if DEBUG;

try {
$retval = Foswiki::Plugins::TagsPlugin::Tag::do( $item_type, $item_name, $tag_text, $user_id );
$retval = Foswiki::Plugins::TagsPlugin::Tag::do( $item_type, $item_name, $tag_text, $user_id, $public );
} catch Error::Simple with {
my $e = shift;
my $n = $e->{'-value'};
Expand Down Expand Up @@ -145,6 +146,7 @@ Takes the following parameters:
item_name : name of the topic to be tagged (format: Sandbox.TestTopic)
tag_text : name of the tag
user_id : tagsplugin user_id (not Foswiki cUID)
public : 0 or 1
This routine does not check any prerequisites and/or priviledges.
Expand All @@ -153,12 +155,13 @@ Return:
=cut

sub do {
my ( $item_type, $item_name, $tag_text, $user_id ) = @_;
my ( $item_type, $item_name, $tag_text, $user_id, $public ) = @_;

throw Error::Simple("tag parameter missing", 1) unless ( ( defined($tag_text) ) && ( $tag_text ne '' ) );
throw Error::Simple("item parameter missing", 2) unless ( ( defined($item_name) ) && ( $item_name ne '' ) );

my $db = new Foswiki::Contrib::DbiContrib;

my $item_id;
my $statement = sprintf( 'SELECT %s from %s WHERE %s = ? AND %s = ?',
qw( item_id Items item_name item_type) );
Expand Down Expand Up @@ -208,9 +211,9 @@ sub do {
qw(tag_id UserItemTag user_id item_id tag_id) );
$arrayRef = $db->dbSelect( $statement, $user_id, $item_id, $tag_id );
if ( !defined( $arrayRef->[0][0] ) ) {
$statement = sprintf( 'INSERT INTO %s (%s, %s, %s) VALUES (?,?,?)',
qw( UserItemTag user_id item_id tag_id) );
$rowCount = $db->dbInsert( $statement, $user_id, $item_id, $tag_id );
$statement = sprintf( 'INSERT INTO %s (%s, %s, %s, %s) VALUES (?,?,?,?)',
qw( UserItemTag user_id item_id tag_id public) );
$rowCount = $db->dbInsert( $statement, $user_id, $item_id, $tag_id, $public );

unless ($new_tag) {
$statement = sprintf( 'UPDATE %s SET %s=%s+1 WHERE %s = ?',
Expand Down
28 changes: 7 additions & 21 deletions pub/System/TagsPlugin/tagsplugin-base.js
Expand Up @@ -48,27 +48,12 @@
$("#tagsplugin_processing img").show();
var tag = $("#tagsplugin_taginput_input").val();
var user = $("div#tagsplugin_taginput form input[name=user]").attr("value");
tagsplugin_be_tag(tag, foswiki.web+'.'+foswiki.topic, user );
var pub = $("div#tagsplugin_taginput form input[name=public]").is(":checked") ? "1" : "0";
tagsplugin_be_tag(tag, foswiki.web+'.'+foswiki.topic, user, pub );
$("#tagsplugin_taginput_input").trigger("blur").val("").focus();
}
);

// public checkbox
$("div#tagsplugin_taginput form input[name=user]")
.attr("checked", "checked")
.val(foswiki.tagsplugin.public)
.removeAttr("disabled")
.bind(
'click',
function(event) {
if ( $(this).is(":checked") ) {
$(this).val(foswiki.tagsplugin.public);
} else {
$(this).val("");
}
}
);

// autocomplete for tagentry
$("#tagsplugin_taginput_input").autocomplete(
foswiki.scriptUrl+"/view/"+foswiki.systemWebName+"/TagsPluginAutoCompleteBackend", {
Expand Down Expand Up @@ -167,13 +152,14 @@
);
}

function tagsplugin_be_tag(tag, item, user) {
function tagsplugin_be_tag(tag, item, user, pub) {
$.ajax(
{ url: foswiki.scriptUrl+'/rest/TagsPlugin/tag',
type: "POST",
data: { tag : tag,
item : item,
user : user
data: { tag : tag,
item : item,
user : user,
public : pub
},
complete: function(xhr, statusText) {
switch (xhr.status) {
Expand Down
3 changes: 2 additions & 1 deletion templates/tagsplugin.tmpl
Expand Up @@ -50,8 +50,9 @@
<input type='text' id="tagsplugin_taginput_input" name='tag' value='enter new tag' onclick="if(this.value=='enter new tag'){this.value='';}"/>
<input type='hidden' name='item' value='%BASEWEB%.%BASETOPIC%' />
<span id='tagsplugin_taginput_public'>
<input type='checkbox' name='user' value='%TAGPUBLIC%' disabled='disabled' checked='checked' /> %MAKETEXT{"public"}%
<input type='checkbox' name='public' checked='checked' value="1" /> %MAKETEXT{"public"}%
</span>
<input type='hidden' name='user' value='%WIKINAME%' />
<input type='hidden' name='type' value='topic' />
<input type='hidden' name='redirectto' value='%SCRIPTURL{view}%/%BASEWEB%.%BASETOPIC%' />
</form>
Expand Down

0 comments on commit 00ffece

Please sign in to comment.