Retrieving Keywords

I need a little help with my perl script. I’m trying to write a function
that when passed a Ticket id, it returns the keywords in that ticket.

I’m having difficulty figuring out how to get the keywords from a ticket id.

Here’s the two functions. I call getkeywords with the ticket number and
try to get back a string that has all the keywords.

Much Thanks in advance,
Grant

(a working solution will be rewarded with a tasty case of beer or a fine
wine from the nearby Napa or Sonoma wineries)

sub getkeyword {

my($keywordid) = shift;
my($statement,$sth,@row,$keyname,$keyparent) = '';

$statement = "select * from Keywords where id='$keywordid'";
$sth = execute_sql_statement($statement);
@row = $sth->fetchrow_array;

$keyname = $row[1];
$keyparent = $row[3];
$sth->finish;

if ($keyparent == 0) {
    return($keyname);
} else {
    return(" " . &getkeyword($keyparent) . " / $keyname " );
}

}

sub getkeywords {
my($ticketid) = shift;
my($statement,$sth,$email,$statement1,$sth1,@row,@keywords) = ‘’;

$statement = "select * from ObjectKeywords where ObjectType='Ticket' AND Obj

ectId=’$ticketid’";
$sth = execute_sql_statement($statement);

while (@row = $sth->fetchrow_array) {
    $keyid = $row[1];
    push @keywords, &getkeyword($keyid);
}
$sth->finish;
return(@keywords);

}

-Grant Miller grant@pico.apple.com

Unix Systems Admin, Engineering Computer Services, Apple Computer

I need a little help with my perl script. I’m trying to write a function
that when passed a Ticket id, it returns the keywords in that ticket.

I’m having difficulty figuring out how to get the keywords from a ticket id.

sub getkeyword {

my($keywordid) = shift;
my($statement,$sth,@row,$keyname,$keyparent) = '';

$statement = "select * from Keywords where id='$keywordid'";
$sth = execute_sql_statement($statement);

I would strongly encourage you to use RT’s API instead of doing direct
SQL access to the database. (Otherwise, you will end up essentially
cutting and pasting code from RT anyhow.) That’s what it’s there for,
after all.

Here’s an example script that when called with a ticket ID will return
all of the keywords for that ticket:

------8<------ cut here ------8<------
#!/usr/bin/perl -w

use strict;
use lib “/opt/rt2/lib”;
use lib “/opt/rt2/etc”;
use RT::Ticket;
use RT::Interface::CLI qw(CleanEnv LoadConfig DBConnect GetCurrentUser);

CleanEnv();
LoadConfig();
DBConnect();
RT::DropSetGIDPermissions();

my $Ticket = new RT::Ticket(GetCurrentUser());
$Ticket->Load($ARGV[0]) || die “Can’t load ticket $ARGV[0]”;

cribbed from webrt/Ticket/Elements/ShowKeywordSelects:

my $KeywordSelects = $Ticket->QueueObj->KeywordSelects;

while ( my $KeywordSelect = $KeywordSelects->Next ) {
print $KeywordSelect->Name . “\n”;

my $Keywords = $Ticket->KeywordsObj($KeywordSelect->Id);
while (my $Keyword = $Keywords->Next) {
my $k = $Keyword->KeywordObj->RelativePath($KeywordSelect->KeywordObj);
print “\t$k\n”;
}
}
------8<------ cut here ------8<------

Cheers,

-Rich

Rich Lafferty --------------±----------------------------------------------
Ottawa, Ontario, Canada | Save the Pacific Northwest Tree Octopus!
http://www.lafferty.ca/ | http://zapatopi.net/treeoctopus.html
rich@lafferty.ca -----------±----------------------------------------------