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

Revision 387 (checked in by miyagawa, 15 years ago)

Fix MTWidget template bug. Thanks to charsbar

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($context, $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 templatize {
64     my($self, $context, $args) = @_;
65
66     my $tt = $context->template();
67     $tt->process('mt_widget.tt', $args, \my $out)
68         or $context->error($tt->error);
69
70     $out;
71 }
72
73 sub finalize {
74     my($self, $context) = @_;
75
76     my $rebuild = $self->conf->{rebuild} or return;
77        $rebuild = [ $rebuild ] unless ref($rebuild);
78     my $blog_id = $self->conf->{blog_id};
79
80     for my $tmpl (@{$rebuild}) {
81         $context->log(info => "Rebuilding Template $tmpl");
82
83         my $template = MT::Template->load({
84             name => $tmpl, blog_id => $blog_id,
85         });
86         unless ($template) {
87             $context->log(error => "Can't load template $tmpl");
88             next;
89         }
90
91         $self->{mt}->rebuild_indexes( BlogID => $blog_id, Template => $template, Force => 1 )
92             or $context->log(error => "Rebuild error: " . $self->{mt}->errstr);
93     }
94 }
95
96 1;
97
98 __END__
99
100 =head1 NAME
101
102 Plagger::Plugin::Publish::MTWidget - Publish feeds as MT widget
103
104 =head1 SYNOPSIS
105
106   - module: Publish::MTWidget
107     config:
108       blog_id: 1
109       mt_path: /path/to/mt
110       rebuild:
111         - Main Index
112
113 =head1 DESCRIPTION
114
115 This plugin automatically creates Movable Type's Sidebar Manager
116 compatible Widget using MT Perl API. You need to run Plagger in a box
117 where MT is installed.
118
119 =head1 AUTHOR
120
121 Tatsuhiko Miyagawa
122
123 Thanks to Benjamin Trott and Anil Dash for the idea, and Byrne Reese
124 for creating MT Sidebar Manager.
125
126 =head1 SEE ALSO
127
128 L<Plagger>, L<http://www.majordojo.com/projects/SidebarManager/>
129
130 =cut
131
Note: See TracBrowser for help on using the browser.