| 1 | package OpenInteract2::Action::RSS; | ||
| 2 | |||
| 3 | # $Id: RSS.pm,v 1.2 2004/12/02 04:31:30 cwinters Exp $ | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use base qw( OpenInteract2::Action ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Digest::MD5 qw( md5_hex ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use File::Spec::Functions qw( catfile ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Log::Log4perl qw( get_logger ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use OpenInteract2::Constants qw( :log ); | ||
| 11 | use OpenInteract2::Context qw( CTX ); | ||
| 12 | use OpenInteract2::Exception qw( oi_error ); | ||
| 13 | use URI; | ||
| 14 | use XML::Feed; | ||
| 15 | |||
| 16 | $OpenInteract2::Action::RSS::VERSION = '0.10'; | ||
| 17 | |||
| 18 | # Use this if the action does not define its own template | ||
| 19 | $OpenInteract2::Action::RSS::DEFAULT_TEMPLATE = 'rss_feed_default'; | ||
| 20 | |||
| 21 | # Use this if the action does not define its own title or title_key | ||
| 22 | $OpenInteract2::Action::RSS::DEFAULT_TITLE = 'Feed'; | ||
| 23 | |||
| 24 | my ( $log ); | ||
| 25 | |||
| 26 | # always run the 'process' task | ||
| 27 | |||
| 28 | sub _find_task { | ||
| 29 | return 'process'; | ||
| 30 | } | ||
| 31 | |||
| 32 | # NOTE: we don't need to bother with caching the output content here | ||
| 33 | # -- execute() in the parent class takes care of it for us | ||
| 34 | |||
| 35 | sub process { | ||
| 36 | my ( $self ) = @_; | ||
| 37 | $log ||= get_logger( LOG_ACTION ); | ||
| 38 | |||
| 39 | my ( $url, $template, $feed ); | ||
| 40 | eval { | ||
| 41 | $url = $self->_get_feed_url(); | ||
| 42 | $log->is_debug && | ||
| 43 | $log->debug( "Will load from feed URL '$url'" ); | ||
| 44 | $feed = $self->_load_feed( $url ); | ||
| 45 | $template = $self->_get_template_name(); | ||
| 46 | $log->is_debug && | ||
| 47 | $log->debug( "Will send feed and data to template '$template'" ); | ||
| 48 | }; | ||
| 49 | return "$@" if ( $@ ); | ||
| 50 | $log->is_info && $log->info( "Got all necessary data, processing feed..." ); | ||
| 51 | my $title = $self->_get_title( $feed ) || $feed->title; | ||
| 52 | my %params = ( | ||
| 53 | feed => $feed, | ||
| 54 | title => $title, | ||
| 55 | ); | ||
| 56 | $self->_modify_template_params( \%params ); | ||
| 57 | return $self->generate_content( \%params, { name => $template } ); | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub _get_feed_url { | ||
| 62 | my ( $self ) = @_; | ||
| 63 | my $class = ref( $self ); | ||
| 64 | my $url = $self->param( 'feed_url' ); | ||
| 65 | unless ( $url ) { | ||
| 66 | $log->error( "No 'feed_url' defined for action ", $self->name ); | ||
| 67 | die "Cannot get feed: parameter 'feed_url' must be defined " . | ||
| 68 | "for action '", $self->name, "'. (See $class for details).\n"; | ||
| 69 | } | ||
| 70 | return $url; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub _load_feed { | ||
| 75 | my ( $self, $url ) = @_; | ||
| 76 | my ( $feed ); | ||
| 77 | if ( $url =~ m|^file://| ) { | ||
| 78 | my $file = $url; | ||
| 79 | $file =~ s|^file://||; | ||
| 80 | $log->is_info && $log->info( "Loading feed from file '$file'" ); | ||
| 81 | $feed = XML::Feed->parse( $file ); | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | $log->is_info && $log->info( "Loading feed from URL '$url'" ); | ||
| 85 | $feed = XML::Feed->parse( URI->new( $url ) ) | ||
| 86 | } | ||
| 87 | unless ( $feed ) { | ||
| 88 | my $error = XML::Feed->errstr(); | ||
| 89 | $log->error( "Failed to load feed: ", ); | ||
| 90 | die "Failed to load feed from '$url': $error\n"; | ||
| 91 | } | ||
| 92 | return $feed; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | sub _get_template_name { | ||
| 97 | my ( $self ) = @_; | ||
| 98 | my $template = $self->param( 'template' ); | ||
| 99 | if ( $template ) { | ||
| 100 | $log->is_debug && | ||
| 101 | $log->debug( "Found template '$template' in action params" ); | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | $log->is_debug && | ||
| 105 | $log->debug( "No RSS template configured, using default" ); | ||
| 106 | $template = $OpenInteract2::Action::RSS::DEFAULT_TEMPLATE; | ||
| 107 | my $full_template_path = | ||
| 108 | catfile( CTX->lookup_directory( 'template' ), $template ); | ||
| 109 | unless ( -f $full_template_path ) { | ||
| 110 | $log->is_debug && | ||
| 111 | $log->debug( "Default template file does not exist, ", | ||
| 112 | "writing for the first time" ); | ||
| 113 | open( DEFAULT, '>', $full_template_path ) | ||
| 114 | || oi_error "Cannot create default RSS template at ", | ||
| 115 | "'$full_template_path': $!"; | ||
| 116 | print DEFAULT _get_default_template_content(); | ||
| 117 | close( DEFAULT ); | ||
| 118 | $log->is_debug && | ||
| 119 | $log->debug( "Wrote default RSS template ok" ); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | return $template; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub _get_default_template_content { | ||
| 127 | return <<'CONTENT'; | ||
| 128 | [%- rss_display_cap = OI.action_param( 'num_display' ) || feed.entries.size; | ||
| 129 | count = 0; -%] | ||
| 130 | <h3>[% title %]</h3> | ||
| 131 | [% FOREACH entry = feed.entries; | ||
| 132 | IF count < rss_display_cap; -%] | ||
| 133 | - [% OI.date_format( entry.issued, '%b %d, %r' ) %]: | ||
| 134 | <a href="[% entry.link %]">[% entry.title %]</a><br /> | ||
| 135 | [% END; | ||
| 136 | count = count + 1; | ||
| 137 | END -%] | ||
| 138 | CONTENT | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _get_title { | ||
| 142 | my ( $self, $feed ) = @_; | ||
| 143 | my $title = $self->param( 'title' ); | ||
| 144 | unless ( $title ) { | ||
| 145 | my $title_key = $self->param( 'title_key' ); | ||
| 146 | if ( $title_key ) { | ||
| 147 | $title = CTX->request->language_handle->maketext( $title_key ); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | return $title; | ||
| 151 | } | ||
| 152 | |||
| 153 | # For overriding... | ||
| 154 | sub _modify_template_params {} | ||
| 155 | |||
| 156 | 1; | ||
| 157 | |||
| 158 | __END__ | ||