| 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 |
eval { |
|---|
| 36 |
my $rs_tag = $self->schema->resultset('Tag')->search( |
|---|
| 37 |
{ 'feed_tag_map.feed' => $rs_feed->id }, |
|---|
| 38 |
{ join => [qw/feed_tag_map/], } |
|---|
| 39 |
); |
|---|
| 40 |
while ( my $tag = $rs_tag->next ) { |
|---|
| 41 |
$feed->tags( [ $tag->name ] ); |
|---|
| 42 |
} |
|---|
| 43 |
}; |
|---|
| 44 |
|
|---|
| 45 |
$context->subscription->add($feed); |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
1; |
|---|
| 50 |
|
|---|
| 51 |
__END__ |
|---|
| 52 |
|
|---|
| 53 |
=head1 NAME |
|---|
| 54 |
|
|---|
| 55 |
Plagger::Plugin::Subscription::DBI - Subscription in database |
|---|
| 56 |
|
|---|
| 57 |
=head1 SYNOPSIS |
|---|
| 58 |
|
|---|
| 59 |
- module: Subscription::DBI |
|---|
| 60 |
config: |
|---|
| 61 |
schema_class: 'My::Schema' |
|---|
| 62 |
connect_info: ['dbi:SQLite:/path/to/plagger.db'] |
|---|
| 63 |
|
|---|
| 64 |
=head1 DESCRIPTION |
|---|
| 65 |
|
|---|
| 66 |
This plugin allows you to configure your subscription in |
|---|
| 67 |
a database. |
|---|
| 68 |
|
|---|
| 69 |
You will need the following: |
|---|
| 70 |
|
|---|
| 71 |
=head2 SQL |
|---|
| 72 |
|
|---|
| 73 |
CREATE TABLE feed ( |
|---|
| 74 |
id INTEGER NOT NULL PRIMARY KEY, |
|---|
| 75 |
url TEXT, |
|---|
| 76 |
link TEXT, |
|---|
| 77 |
title TEXT |
|---|
| 78 |
); |
|---|
| 79 |
|
|---|
| 80 |
CREATE TABLE tag ( |
|---|
| 81 |
id INTEGER NOT NULL PRIMARY KEY, |
|---|
| 82 |
name TEXT NOT NULL |
|---|
| 83 |
); |
|---|
| 84 |
|
|---|
| 85 |
CREATE TABLE feed_tag_map ( |
|---|
| 86 |
feed INTEGER NOT NULL, |
|---|
| 87 |
tag INTEGER NOT NULL, |
|---|
| 88 |
PRIMARY KEY (feed, tag) |
|---|
| 89 |
); |
|---|
| 90 |
|
|---|
| 91 |
and the following DBIx::Class::Schema |
|---|
| 92 |
|
|---|
| 93 |
=head2 My::Schema |
|---|
| 94 |
|
|---|
| 95 |
package My::Schema; |
|---|
| 96 |
use strict; |
|---|
| 97 |
use warnings; |
|---|
| 98 |
use base qw/DBIx::Class::Schema/; |
|---|
| 99 |
|
|---|
| 100 |
__PACKAGE__->load_classes(); |
|---|
| 101 |
|
|---|
| 102 |
1; |
|---|
| 103 |
|
|---|
| 104 |
=head2 My::Schema::Feed |
|---|
| 105 |
|
|---|
| 106 |
package My::Schema::Feed; |
|---|
| 107 |
use strict; |
|---|
| 108 |
use warnings; |
|---|
| 109 |
use base qw/DBIx::Class/; |
|---|
| 110 |
|
|---|
| 111 |
__PACKAGE__->load_components(qw/Core/); |
|---|
| 112 |
|
|---|
| 113 |
__PACKAGE__->table('feed'); |
|---|
| 114 |
__PACKAGE__->add_columns(qw( |
|---|
| 115 |
id |
|---|
| 116 |
url |
|---|
| 117 |
link |
|---|
| 118 |
title |
|---|
| 119 |
)); |
|---|
| 120 |
__PACKAGE__->set_primary_key(qw/id/); |
|---|
| 121 |
|
|---|
| 122 |
1; |
|---|
| 123 |
|
|---|
| 124 |
=head2 My::Schema::FeedTagMap |
|---|
| 125 |
|
|---|
| 126 |
package My::Schema::FeedTagMap; |
|---|
| 127 |
|
|---|
| 128 |
use strict; |
|---|
| 129 |
use warnings; |
|---|
| 130 |
use base qw/DBIx::Class/; |
|---|
| 131 |
|
|---|
| 132 |
__PACKAGE__->load_components(qw/Core/); |
|---|
| 133 |
|
|---|
| 134 |
__PACKAGE__->table('feed_tag_map'); |
|---|
| 135 |
__PACKAGE__->add_columns(qw( |
|---|
| 136 |
feed |
|---|
| 137 |
tag |
|---|
| 138 |
)); |
|---|
| 139 |
|
|---|
| 140 |
__PACKAGE__->set_primary_key(qw/feed tag/); |
|---|
| 141 |
|
|---|
| 142 |
__PACKAGE__->belongs_to( feed => 'TEST::Schema::Feed' ); |
|---|
| 143 |
__PACKAGE__->belongs_to( tag => 'TEST::Schema::Tag' ); |
|---|
| 144 |
|
|---|
| 145 |
1; |
|---|
| 146 |
|
|---|
| 147 |
=head2 My::Schema::Tag |
|---|
| 148 |
|
|---|
| 149 |
package TEST::Schema::Tag; |
|---|
| 150 |
use strict; |
|---|
| 151 |
use warnings; |
|---|
| 152 |
use base qw/DBIx::Class/; |
|---|
| 153 |
|
|---|
| 154 |
__PACKAGE__->load_components(qw/Core/); |
|---|
| 155 |
|
|---|
| 156 |
__PACKAGE__->table('tag'); |
|---|
| 157 |
__PACKAGE__->add_columns(qw( |
|---|
| 158 |
id |
|---|
| 159 |
name |
|---|
| 160 |
)); |
|---|
| 161 |
__PACKAGE__->set_primary_key(qw/id/); |
|---|
| 162 |
|
|---|
| 163 |
__PACKAGE__->has_many( feed_tag_map => 'TEST::Schema::FeedTagMap', 'tag' ); |
|---|
| 164 |
__PACKAGE__->many_to_many( feeds => feed_tag_map => 'feed' ); |
|---|
| 165 |
|
|---|
| 166 |
1; |
|---|
| 167 |
|
|---|
| 168 |
=head1 AUTHOR |
|---|
| 169 |
|
|---|
| 170 |
Franck Cuny |
|---|
| 171 |
|
|---|
| 172 |
Based on the plugin Plagger::Plugin::Subscription::Config by Tatsuhiko Miyagawa |
|---|
| 173 |
|
|---|
| 174 |
The schema is inspired by the work of Daisuke Murase for Plagger::Plugin::Store::DBIC |
|---|
| 175 |
|
|---|
| 176 |
=head1 SEE ALSO |
|---|
| 177 |
|
|---|
| 178 |
L<Plagger> |
|---|
| 179 |
|
|---|
| 180 |
=cut |
|---|
| 181 |
|
|---|