root/mt-cats2tags/trunk/mt-cats2tags.cgi

Revision 357, 1.2 kB (checked in by ogawa, 21 months ago)

Uses add_tags instead of set_tags.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
Line 
1#!/usr/bin/perl -w
2#
3# $Id$
4# A simple tool for adding tags to each entries based on their categories
5#
6# This software is provided as-is. You may use it for commercial or
7# personal use. If you distribute it, please keep this notice intact.
8#
9# Copyright (c) 2006,2007 Hirotaka Ogawa
10
11use strict;
12sub BEGIN {
13    my $dir;
14    require File::Spec;
15    if (!($dir = $ENV{MT_HOME})) {
16        if ($0 =~ m!(.*[/\\])!) {
17            $dir = $1;
18        } else {
19            $dir = './';
20        }
21        $ENV{MT_HOME} = $dir;
22    }
23    unshift @INC, File::Spec->catdir($dir, 'lib');
24    unshift @INC, File::Spec->catdir($dir, 'extlib');
25}
26
27$|=1;
28print "Content-Type: text/html\n\n";
29print <<HTML;
30<html>
31<head><title>mt-cats2tags</title></head>
32<body>
33<h1>mt-cats2tags</h1>
34
35<pre>
36HTML
37
38use MT;
39use MT::Entry;
40use MT::Category;
41
42my $mt = MT->new or die MT->errstr;
43my $iter = MT::Entry->load_iter;
44
45while (my $e = $iter->()) {
46    my $cats = $e->categories;
47    next unless $cats && @$cats;
48    my @tags = map { $_->label } @$cats;
49    $e->add_tags(@tags);
50    $e->save_tags;
51    print $e->id . ": " . join(', ', @tags) . "\n";
52}
53
54print <<HTML;
55</pre>
56<p><strong>Successfully added tags.</strong></p>
57</body>
58</html>
59HTML
Note: See TracBrowser for help on using the browser.