Bulk update Topics?

Hi folks.

Anyone know whether it would be possible to bulk update the Topic associated with a set of Articles? Looking at the information included in the API, I don’t see Topics either as a standalone category in the documentation or included in the results when GET the article information. Any suggestions?

Thanks!

I had a peek at the RT 5.0.7 code and there’s no mention of topics in the REST2 API modules I’m afraid.

If you’re just looking to script some change and can run code on your RT server itself, the Perl API should allow you to do this… maybe something like this?

#!/usr/bin/perl
use strict;
use warnings;

# fix lib paths, some may be relative
BEGIN { # BEGIN RT CMD BOILERPLATE
    require File::Spec;
    require Cwd;
    my @libs = ("/opt/rt5/lib", "/opt/rt5/local/lib");
    my $bin_path;

    for my $lib (@libs) {
        unless ( File::Spec->file_name_is_absolute($lib) ) {
            $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
            $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
        }
        unshift @INC, $lib;
    }

}

use RT::Interface::CLI qw(Init);
use RT::Topics;
use RT::Topic;
use RT::ObjectTopics;

my %OPT;
Init(
    \%OPT,
    'topic=s',
    'newtopic=s',
    'help'
    );

if($OPT{'help'}) {
    print "Usage: $0 --topic <topic name> [--newtopic <new topic name>]\n" .
	"\t[--help]\n";
    exit;
}

if(!$OPT{'topic'}) {
  die "Need a --topic\n";
}
my $topicName = $OPT{'topic'};
my $newTopicName = $OPT{'newtopic'};

my $topics = RT::Topics->new( RT::SystemUser );
$topics->UnLimit();
my $newTopicId;
if($newTopicName) {
    while( my $topic = $topics->Next ) {
	if($topic->Name eq $newTopicName) {
	    $newTopicId = $topic->id;
	}
    }
    $topics->GotoFirstItem();
}

while( my $topic = $topics->Next ) {
    if($topic->Name eq $topicName) {
	print "Topic: ".$topic->Name."\n";
	my $objTopics = RT::ObjectTopics->new( RT::SystemUser );
	$objTopics->LimitToTopic($topic->id);
	while(my $objTopic = $objTopics->Next) {
	    if($objTopic->ObjectType eq 'RT::Article') {
		my $article = RT::Article->new( RT::SystemUser );
		$article->Load($objTopic->ObjectId());
		if($newTopicId) {
		    print "Changing article " . $article->id . " (" .
			$article->Name . " from topic ID " .
			$topic->id . " to new topic ID $newTopicId\n";
		    $objTopic->SetTopic($newTopicId);
		}
	    }
	}
    }
}

1 Like

This is great, thanks so much for putting it together. We’ll go experiment and report back.