root/trunk/plagger/lib/Plagger/Plugin/Publish/MTWidget.pm

Revision 1173 (checked in by miyagawa, 14 years ago)

Merge from refactoring-planet. YAY!

Line 
1 package Plagger::Plugin::Publish::MTWidget;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use File::Spec;
6
7 sub init {
8     my $self = shift;
9     $self->SUPER::init(@_);
10
11     my $mt_home = $self->conf->{mt_path}
12         or Plagger->context->error('mt_path is missing');
13
14     $ENV{MT_HOME} = $mt_home;
15     unshift @INC, File::Spec->catfile($mt_home, 'lib');
16
17     eval {
18         require MT;
19         require MT::Template;
20         $self->{mt} = MT->new or Plagger->context->error(MT->errstr);
21     };
22     if ($@) {
23         Plagger->context->error("Error loading MT: $@");
24     }
25 }
26
27 sub register {
28     my($self, $context) = @_;
29     $context->register_hook(
30         $self,
31         'publish.feed' => \&feed,
32         'publish.finalize' => \&finalize,
33     );
34 }
35
36 sub feed {
37     my($self, $context, $args) = @_;
38
39     my $blog_id = $self->conf->{blog_id} || 1;
40     my $title = $self->conf->{title} || $args->{feed}->title;
41     my $body  = $self->templatize('mt_widget.tt', $args);
42
43     my $trimed_title = substr($title, 0, 10);
44        $trimed_title .= '..' if $trimed_title ne $title;
45     my $widget_title = "Sidebar: $trimed_title";
46
47     my $tmpl = MT::Template->load({ name => $widget_title });
48
49     if ($tmpl) {
50         $context->log(info => "Updating MT Widget for $title on blog_id $blog_id");
51     } else {
52         $context->log(info => "Creating MT Widget for $title on blog_id $blog_id");
53         $tmpl = MT::Template->new;
54         $tmpl->blog_id($blog_id);
55         $tmpl->type('custom');
56         $tmpl->name($widget_title);
57     }
58
59     $tmpl->text($body);
60     $tmpl->save or $context->error($tmpl->errstr);
61 }
62
63 sub finalize {
64     my($self, $context) = @_;
65
66     my $rebuild = $self->conf->{rebuild} or return;
67        $rebuild = [ $rebuild ] unless ref($rebuild);
68     my $blog_id = $self->conf->{blog_id};
69
70     for my $tmpl (@{$rebuild}) {
71         $context->log(info => "Rebuilding Template $tmpl");
72
73         my $template = MT::Template->load({
74             name => $tmpl, blog_id => $blog_id,
75         });
76         unless ($template) {
77             $context->log(error => "Can't load template $tmpl");
78             next;
79         }
80
81         $self->{mt}->rebuild_indexes( BlogID => $blog_id, Template => $template, Force => 1 )
82             or $context->log(error => "Rebuild error: " . $self->{mt}->errstr);
83     }
84 }
85
86 1;
87
88 __END__
89
90 =head1 NAME
91
92 Plagger::Plugin::Publish::MTWidget - Publish feeds as MT widget
93
94 =head1 SYNOPSIS
95
96   - module: Publish::MTWidget
97     config:
98       blog_id: 1
99       mt_path: /path/to/mt
100       rebuild:
101         - Main Index
102
103 =head1 DESCRIPTION
104
105 This plugin automatically creates Movable Type's Sidebar Manager
106 compatible Widget using MT Perl API. You need to run Plagger in a box
107 where MT is installed.
108
109 =head1 AUTHOR
110
111 Tatsuhiko Miyagawa
112
113 Thanks to Benjamin Trott and Anil Dash for the idea, and Byrne Reese
114 for creating MT Sidebar Manager.
115
116 =head1 SEE ALSO
117
118 L<Plagger>, L<http://www.majordojo.com/projects/SidebarManager/>
119
120 =cut
121
Note: See TracBrowser for help on using the browser.