Skip to content

Commit

Permalink
Tagboxes. Two new tables, tagboxes and tagbox_feederlog, and a task
Browse files Browse the repository at this point in the history
(tagbox.pl) that reads from the feederlog to update whatever each tagbox
needs updated.  Currently with manual dispatch of one hard-coded tagbox,
tag_count, but soon tags_update.pl will be rolled in, and there will be
a modular way to add tagboxes.
  • Loading branch information
jamiemccarthy committed May 3, 2006
1 parent 4fe9307 commit 0ea0e56
Show file tree
Hide file tree
Showing 9 changed files with 588 additions and 5 deletions.
6 changes: 5 additions & 1 deletion plugins/Tags/Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ use ExtUtils::MakeMaker;
WriteMakefile(
'NAME' => 'Slash::Tags',
'VERSION_FROM' => 'Tags.pm', # finds $VERSION
'PM' => { 'Tags.pm' => '$(INST_LIBDIR)/Tags.pm' },
'PM' =>
{
'Tags.pm' => '$(INST_LIBDIR)/Tags.pm',
'Tagbox.pm' => '$(INST_LIBDIR)/Tagbox.pm',
},
);
1 change: 1 addition & 0 deletions plugins/Tags/PLUGIN
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ htdoc=tags.pl
mysql_dump=mysql_dump.sql
mysql_schema=mysql_schema.sql
requiresplugin=Ajax
task=tagbox.pl
task=tags_update.pl
template=templates/data;tags;default
template=templates/index;tags;default
Expand Down
156 changes: 156 additions & 0 deletions plugins/Tags/Tagbox.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2005 by Open Source Technology Group. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$

package Slash::Tagbox;

use strict;
use Slash;
use Slash::Display;
use Slash::Utility;
use Slash::DB::Utility;
use Apache::Cookie;
use vars qw($VERSION);
use base 'Slash::DB::Utility';
use base 'Slash::DB::MySQL';

($VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;

# FRY: And where would a giant nerd be? THE LIBRARY!

#################################################################
sub new {
my($class, $user) = @_;
my $self = {};

my $plugin = getCurrentStatic('plugin');
return unless $plugin->{Tags};

bless($self, $class);
$self->{virtual_user} = $user;
$self->sqlConnect();

return $self;
}

#################################################################

# XXX need to cache this, except for last_tagid_logged/last_run_completed
sub getTagboxes {
my($self, $id) = @_;
my $ar = $self->sqlSelectAllHashrefArray('*', 'tagboxes');
if (!$id) {
return $ar;
} elsif ($id =~ /^\d+$/) {
my @tb = grep { $_->{tbid} == $id } @$ar;
return @tb ? $tb[0] : undef;
} else {
my @tb = grep { $_->{name} eq $id } @$ar;
return @tb ? $tb[0] : undef;
}
}

sub getMostImportantTagboxAffectedIDs {
my($self, $num) = @_;
$num ||= 10;
return $self->sqlSelectAllHashrefArray(
'tagboxes.tbid,
affected_id,
MAX(tagid) AS max_tagid,
SUM(importance*weight) AS sum_imp_weight',
'tagboxes, tagbox_feederlog',
'tagboxes.tbid=tagbox_feederlog.tbid',
"GROUP BY tagboxes.tbid, affected_id
ORDER BY sum_imp_weight DESC LIMIT $num");
}

sub getTagboxTags {
my($self, $tbid, $affected_id, $extra_levels) = @_;
$extra_levels ||= 0;
my $type = $self->getTagboxes($tbid)->{affected_type};
my $hr_ar = [ ];
my $colname = ($type eq 'user') ? 'uid' : 'globjid';
$hr_ar = $self->sqlSelectAllHashrefArray(
'*',
'tags',
"$colname=$affected_id",
'ORDER BY tagid');

# If extra_levels were requested, fetch them.
my $old_colname = $colname;
while ($extra_levels) {
#print STDERR "el $extra_levels\n";
my $new_colname = ($old_colname eq 'uid') ? 'globjid' : 'uid';
my %new_ids = ( map { ($_->{$new_colname}, 1) } @$hr_ar );
my $new_ids = join(',', sort { $a <=> $b } keys %new_ids);
#print STDERR "hr_ar=" . scalar(@$hr_ar) . " with $colname=$affected_id\n";
$hr_ar = $self->sqlSelectAllHashrefArray(
'*',
'tags',
"$new_colname IN ($new_ids)",
'ORDER BY tagid');
#print STDERR "new_colname=$new_colname new_ids=" . scalar(keys %new_ids) . " (" . substr($new_ids, 0, 20) . ") hr_ar=" . scalar(@$hr_ar) . "\n";
$old_colname = $new_colname;
--$extra_levels;
#print STDERR "el $extra_levels\n";
}
$self->addGlobjEssentialsToHashrefArray($hr_ar);
return $hr_ar;
}

sub addFeederInfo {
my($self, $tbid, $tagid, $affected_id, $importance) = @_;
return $self->sqlInsert('tagbox_feederlog', {
-tfid => 'NULL',
-created_at => 'NOW()',
tbid => $tbid,
tagid => $tagid,
affected_id => $affected_id,
importance => $importance,
});
}

sub markTagboxLogged {
my($self, $tbid, $last_tagid_logged) = @_;
$self->sqlUpdate('tagboxes',
{ last_tagid_logged => $last_tagid_logged },
"tbid=$tbid");
}

sub markTagboxRunComplete {
my($self, $tbid, $affected_id, $max_tagid) = @_;
print STDERR "markTagboxRunComplete: tbid=$tbid aff_id=$affected_id max=$max_tagid\n";
$self->sqlDelete('tagbox_feederlog',
"tbid=$tbid AND affected_id=$affected_id
AND tagid <= $max_tagid");
$self->sqlUpdate('tagboxes',
{ -last_run_completed => 'NOW()' },
"tbid=$tbid");
}

#################################################################
sub DESTROY {
my($self) = @_;
$self->{_dbh}->disconnect if $self->{_dbh} && !$ENV{GATEWAY_INTERFACE};
}

1;

=head1 NAME
Slash::Tagbox - Slash Tagbox module
=head1 SYNOPSIS
use Slash::Tagbox;
=head1 DESCRIPTION
This contains all of the routines currently used by Tagbox.
=head1 SEE ALSO
Slash(3).
=cut
53 changes: 49 additions & 4 deletions plugins/Tags/Tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ sub _setuptag {
}

sub createTag {
my($self, $hr, $options) = @_;
my($self, $hr, $options) = @_;

my $tag = $self->_setuptag($hr);
my $tag = $self->_setuptag($hr);
return 0 if !$tag;

my $check_dupe = (!$options || !$options->{dupe_ok});
Expand All @@ -106,7 +106,7 @@ sub createTag {
}

$self->sqlDo('SET AUTOCOMMIT=0');
my $rows = $self->sqlInsert('tags', $tag);
my $rows = $self->sqlInsert('tags', $tag);
my $tagid = $rows ? $self->getLastInsertId() : 0;

if ($rows && $check_dupe) {
Expand Down Expand Up @@ -156,7 +156,7 @@ sub createTag {
# Return AUTOCOMMIT to its original state in any case.
$self->sqlDo('SET AUTOCOMMIT=1');

return $rows ? 1 : 0;
return $rows ? $tagid : 0;
}

sub deactivateTag {
Expand Down Expand Up @@ -426,6 +426,50 @@ sub getTagsByNameAndIdArrayref {
return $ar;
}

# Given an arrayref of hashrefs representing tags, such as that
# returned by getTagsByNameAndIdArrayref, add three fields to each
# hashref: tag_clout, tagname_clout, user_clout.

sub addRoundedCloutsToTagArrayref {
my($self, $ar) = @_;

return if !$ar || !@$ar;

# Pull values from tag params named 'tag_clout'
my @tagids = sort { $a <=> $b } map { $_->{tagid} } @$ar;
my $tagids_in_str = join(',', @tagids);
my $tag_clout_hr = $self->sqlSelectAllKeyValue(
'tagid, value', 'tag_params',
"tagid IN ($tagids_in_str) AND name='tag_clout'");

# Pull values from tagname params named 'tag_clout'
my %tagnameid = map { ($_->{tagnameid}, 1) } @$ar;
my @tagnameids = sort { $a <=> $b } keys %tagnameid;
my $tagnameids_in_str = join(',', @tagnameids);
my $tagname_clout_hr = $self->sqlSelectAllKeyValue(
'tagnameid, value', 'tagname_params',
"tagnameid IN ($tagnameids_in_str) AND name='tag_clout'");

# Pull values from users_info.tag_clout
my %uid = map { ($_->{uid}, 1) } @$ar;
my @uids = sort { $a <=> $b } keys %uid;
my $uids_in_str = join(',', @uids);
my $uid_clout_hr = $self->sqlSelectAllKeyValue(
'uid, tag_clout', 'users_info',
"uid IN ($uids_in_str)");

for my $tag_hr (@$ar) {
$tag_hr->{tag_clout} = defined($tag_clout_hr ->{$tag_hr->{tagid}})
? sprintf("%.3g", $tag_clout_hr ->{$tag_hr->{tagid}})
: 1;
$tag_hr->{tagname_clout} = defined($tagname_clout_hr->{$tag_hr->{tagnameid}})
? sprintf("%.3g", $tagname_clout_hr->{$tag_hr->{tagnameid}})
: 1;
$tag_hr->{user_clout} =
sprintf("%.3g", $uid_clout_hr ->{$tag_hr->{uid}});
}
}

sub getAllTagsFromUser {
my($self, $uid, $options) = @_;
$options ||= {};
Expand Down Expand Up @@ -870,6 +914,7 @@ sub ajaxTagHistory {
my $tags_reader = getObject('Slash::Tags', { db_type => 'reader' });
my $tags_ar = [];
$tags_ar = $tags_reader->getTagsByNameAndIdArrayref($table, $id, { include_inactive => 1 }) if $table && $id;
$tags_reader->addRoundedCloutsToTagArrayref($tags_ar);
slashDisplay('taghistory', { tags => $tags_ar }, { Return => 1 } );
}
Expand Down
2 changes: 2 additions & 0 deletions plugins/Tags/mysql_dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ INSERT INTO menus VALUES (NULL, 'tagszg', 'Active', 'active', '[% gSkin.rootdir
INSERT INTO menus VALUES (NULL, 'tagszg', 'Recent', 'recent', '[% gSkin.rootdir %]/tags/recent', 1, 1, 2);
INSERT INTO menus VALUES (NULL, 'tagszg', 'All', 'all', '[% gSkin.rootdir %]/tags/all', 1, 1, 3);

INSERT INTO tagboxes VALUES (NULL, 'tag_count', 'user', '1.0', 0, NULL);

23 changes: 23 additions & 0 deletions plugins/Tags/mysql_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,26 @@ CREATE TABLE tagcommand_adminlog (

ALTER TABLE users_info ADD COLUMN tag_clout FLOAT UNSIGNED NOT NULL DEFAULT 1.0 AFTER created_at;

CREATE TABLE tagboxes (
tbid smallint UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(32) DEFAULT '' NOT NULL,
affected_type ENUM('user', 'globj') NOT NULL,
weight FLOAT UNSIGNED DEFAULT 1.0 NOT NULL,
last_tagid_logged int UNSIGNED NOT NULL,
last_run_completed datetime,
PRIMARY KEY tbid (tbid),
UNIQUE name (name)
) TYPE=InnoDB;

CREATE TABLE tagbox_feederlog (
tfid int UNSIGNED NOT NULL AUTO_INCREMENT,
created_at datetime NOT NULL,
tbid smallint UNSIGNED NOT NULL,
tagid int UNSIGNED NOT NULL,
affected_id int UNSIGNED NOT NULL,
importance FLOAT UNSIGNED DEFAULT 1.0 NOT NULL,
PRIMARY KEY tfid (tfid),
KEY tbid_tagid (tbid, tagid),
KEY tbid_affectedid (tbid, affected_id)
) TYPE=InnoDB;

Loading

0 comments on commit 0ea0e56

Please sign in to comment.