root/trunk/plagger/lib/Plagger/Plugin/Subscription/DBI.pm

Revision 404 (checked in by franck, 14 years ago)

This plugin allows you to configure your subscription in
a database.

Line 
1 package Plagger::Plugin::Subscription::DBI;
2 use strict;
3 use base qw( Plagger::Plugin Class::Accessor::Fast);
4
5 __PACKAGE__->mk_accessors(qw/schema/);
6
7 sub register {
8     my ( $self, $context ) = @_;
9
10     unless ( $self->conf->{schema_class} and $self->conf->{connect_info} ) {
11         $context->error('schema_class and connect_info are required');
12     }
13
14     $self->conf->{schema_class}->require
15         or $context->error(
16         qq/Can't load schema class "@{[ $self->conf->{schema_class} ]}", $!/);
17
18     $self->schema( $self->conf->{schema_class}
19             ->connect( @{ $self->conf->{connect_info} } ) );
20
21     $context->register_hook( $self, 'subscription.load' => \&load, );
22 }
23
24 sub load {
25     my ( $self, $context ) = @_;
26
27     my $rs = $self->schema->resultset('Feed')->search();
28
29     while ( my $rs_feed = $rs->next ) {
30         my $feed = Plagger::Feed->new;
31         $feed->url( $rs_feed->url ) or $context->error("Feed URL is missing");
32         $feed->link( $rs_feed->link )   if $rs_feed->link;
33         $feed->title( $rs_feed->title ) if $rs_feed->title;
34
35         my $rs_tag = $self->schema->resultset('Tag')->search(
36             { 'feed_tag_map.feed' => $rs_feed->id },
37             { join                => [qw/feed_tag_map/], }
38         );
39         while ( my $tag = $rs_tag->next ) {
40             $feed->tags( [ $tag->name ] );
41         }
42
43         $context->subscription->add($feed);
44     }
45 }
46
47 1;
48
49 __END__
50
51 =head1 NAME
52
53 Plagger::Plugin::Subscription::DBI - Subscription in database
54
55 =head1 SYNOPSIS
56
57     - module: Subscription::DBI
58       config:
59         schema_class: 'My::Schema'
60         connect_info: ['dbi:SQLite:/path/to/plagger.db']
61
62 =head1 DESCRIPTION
63
64 This plugin allows you to configure your subscription in
65 a database.
66
67 You will need the following:
68
69 =head2 SQL
70
71     CREATE TABLE feed (
72         id INTEGER NOT NULL PRIMARY KEY,
73         url TEXT,
74         link TEXT,
75         title TEXT
76     );
77     
78     CREATE TABLE tag (
79         id INTEGER NOT NULL PRIMARY KEY,
80         name TEXT NOT NULL
81     );
82     
83     CREATE TABLE feed_tag_map (
84         feed INTEGER NOT NULL,
85         tag INTEGER NOT NULL,
86         PRIMARY KEY (feed, tag)
87     );
88
89 and the following DBIx::Class::Schema
90
91 =head2 My::Schema
92
93     package My::Schema;
94     use strict;
95     use warnings;
96     use base qw/DBIx::Class::Schema/;
97     
98     __PACKAGE__->load_classes();
99     
100     1;
101
102 =head2 My::Schema::Feed
103
104     package My::Schema::Feed;
105     use strict;
106     use warnings;
107     use base qw/DBIx::Class/;
108     
109     __PACKAGE__->load_components(qw/Core/);
110     
111     __PACKAGE__->table('feed');
112     __PACKAGE__->add_columns(qw(
113             id
114             url
115             link
116             title
117     ));
118     __PACKAGE__->set_primary_key(qw/id/);
119     
120     1;
121
122 =head2 My::Schema::FeedTagMap
123
124     package My::Schema::FeedTagMap;
125     
126     use strict;
127     use warnings;
128     use base qw/DBIx::Class/;
129     
130     __PACKAGE__->load_components(qw/Core/);
131     
132     __PACKAGE__->table('feed_tag_map');
133     __PACKAGE__->add_columns(qw(
134             feed
135             tag
136     ));
137     
138     __PACKAGE__->set_primary_key(qw/feed tag/);
139     
140     __PACKAGE__->belongs_to( feed => 'TEST::Schema::Feed' );
141     __PACKAGE__->belongs_to( tag  => 'TEST::Schema::Tag' );
142     
143     1;
144
145 =head2 My::Schema::Tag
146
147     package TEST::Schema::Tag;
148     use strict;
149     use warnings;
150     use base qw/DBIx::Class/;
151     
152     __PACKAGE__->load_components(qw/Core/);
153     
154     __PACKAGE__->table('tag');
155     __PACKAGE__->add_columns(qw(
156             id
157             name
158     ));
159     __PACKAGE__->set_primary_key(qw/id/);
160     
161     __PACKAGE__->has_many( feed_tag_map => 'TEST::Schema::FeedTagMap', 'tag' );
162     __PACKAGE__->many_to_many( feeds => feed_tag_map => 'feed' );
163     
164     1;
165
166 =head1 AUTHOR
167
168 Franck Cuny
169
170 Based on the plugin Plagger::Plugin::Subscription::Config by Tatsuhiko Miyagawa
171
172 The schema is inspired by the work of Daisuke Murase for Plagger::Plugin::Store::DBIC
173
174 =head1 SEE ALSO
175
176 L<Plagger>
177
178 =cut
179
Note: See TracBrowser for help on using the browser.