| 1 |
package XML::Feed; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use v5.10; |
| 5 |
|
| 6 |
use base qw( Class::ErrorHandler ); |
| 7 |
use Feed::Find; |
| 8 |
use URI::Fetch; |
| 9 |
use LWP::UserAgent; |
| 10 |
use Carp; |
| 11 |
use Scalar::Util 'blessed'; |
| 12 |
use Module::Pluggable search_path => "XML::Feed::Format", |
| 13 |
require => 1, |
| 14 |
sub_name => 'formatters'; |
| 15 |
|
| 16 |
our $VERSION = '1.0.1'; |
| 17 |
our $MULTIPLE_ENCLOSURES = 0; |
| 18 |
our @formatters; |
| 19 |
our %formatters; |
| 20 |
BEGIN { |
| 21 |
@formatters = __PACKAGE__->formatters; |
| 22 |
for my $formatter (@formatters) { |
| 23 |
(my $name = $formatter) =~ s/^XML::Feed::Format:://; |
| 24 |
$formatters{$name} = 1; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
sub _parse_args { |
| 29 |
my $class = shift; |
| 30 |
my ($possible_args) = @_; |
| 31 |
|
| 32 |
my @valid_args = qw[useragent]; |
| 33 |
my $args = {}; |
| 34 |
|
| 35 |
for (@valid_args) { |
| 36 |
$args->{$_} = delete $possible_args->{$_} |
| 37 |
if exists $possible_args->{$_}; |
| 38 |
} |
| 39 |
|
| 40 |
unless (keys %$args) { |
| 41 |
Carp::croak('No valid keys passed to ' . __PACKAGE__ . 'new()'); |
| 42 |
} |
| 43 |
|
| 44 |
# Validate useragent parameter if provided |
| 45 |
if (exists $args->{useragent}) { |
| 46 |
unless (blessed($args->{useragent}) && $args->{useragent}->isa('LWP::UserAgent')) { |
| 47 |
Carp::croak("useragent must be an LWP::UserAgent object"); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return $args; |
| 52 |
} |
| 53 |
|
| 54 |
sub new { |
| 55 |
my $class = shift; |
| 56 |
my $format = shift // 'Atom'; |
| 57 |
|
| 58 |
# Handle optional args hash |
| 59 |
my $args = {}; |
| 60 |
|
| 61 |
if (@_ && ref($_[-1]) eq 'HASH') { |
| 62 |
$args = $class->_parse_args(pop @_); |
| 63 |
} |
| 64 |
|
| 65 |
Carp::croak("Unsupported format $format: not a recognised feed format") |
| 66 |
unless $formatters{$format}; |
| 67 |
my $format_class = 'XML::Feed::Format::' . $format; |
| 68 |
eval "use $format_class"; |
| 69 |
Carp::croak("Unsupported format $format: $@") if $@; |
| 70 |
my $feed = bless $args, join('::', __PACKAGE__, "Format", $format); |
| 71 |
|
| 72 |
$feed->init_empty(@_) or return $class->error($feed->errstr); |
| 73 |
$feed; |
| 74 |
} |
| 75 |
|
| 76 |
sub init_empty { 1 } |
| 77 |
|
| 78 |
sub parse { |
| 79 |
my $class = shift; |
| 80 |
my($stream) = @_; |
| 81 |
return $class->error("Stream parameter is required") unless $stream; |
| 82 |
|
| 83 |
my ($specified_format, $args) = (undef, {}); |
| 84 |
|
| 85 |
if (@_ == 3) { |
| 86 |
($specified_format, $args) = @_[1, 2]; |
| 87 |
} elsif (@_ == 2) { |
| 88 |
if (ref($_[-1]) eq 'HASH') { |
| 89 |
$args = $_[1]; |
| 90 |
} else { |
| 91 |
$specified_format = $_[1]; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (keys %$args) { |
| 96 |
$args = $class->_parse_args($args); |
| 97 |
} |
| 98 |
|
| 99 |
my $feed = bless $args, $class; |
| 100 |
my $xml = ''; |
| 101 |
if (blessed($stream) and $stream->isa('URI')) { |
| 102 |
$xml = $feed->get_uri($stream); |
| 103 |
} elsif (ref($stream) eq 'SCALAR') { |
| 104 |
$xml = $$stream; |
| 105 |
} elsif (ref($stream)) { |
| 106 |
$xml = $feed->get_fh($stream); |
| 107 |
} else { |
| 108 |
$xml = $feed->get_file($stream); |
| 109 |
} |
| 110 |
my $errstr = "Can't get feed XML content from $stream"; |
| 111 |
if ($class->errstr) { |
| 112 |
$errstr .= ": " . $class->errstr; |
| 113 |
} |
| 114 |
return $class->error($errstr) |
| 115 |
unless $xml; |
| 116 |
my $format; |
| 117 |
if ($specified_format) { |
| 118 |
$format = $specified_format; |
| 119 |
} else { |
| 120 |
$format = $feed->identify_format(\$xml) or return $class->error($feed->errstr); |
| 121 |
} |
| 122 |
|
| 123 |
my $format_class = join '::', __PACKAGE__, "Format", $format; |
| 124 |
return $class->error("Unsupported format $format: not a recognised feed format") |
| 125 |
unless $formatters{$format}; |
| 126 |
eval "use $format_class"; |
| 127 |
return $class->error("Unsupported format $format: $@") if $@; |
| 128 |
bless $feed, $format_class; |
| 129 |
$feed->init_string(\$xml) or return $class->error($feed->errstr); |
| 130 |
$feed; |
| 131 |
} |
| 132 |
|
| 133 |
sub get_file { |
| 134 |
my $class = shift; |
| 135 |
my ($filename) = @_; |
| 136 |
|
| 137 |
open my $fh, '<', $filename |
| 138 |
or return $class->error("Can't open $filename: $!"); |
| 139 |
my $xml = $class->get_fh($fh); |
| 140 |
close $fh; |
| 141 |
|
| 142 |
return $xml; |
| 143 |
} |
| 144 |
|
| 145 |
sub get_fh { |
| 146 |
my $class = shift; |
| 147 |
my ($fh) = @_; |
| 148 |
|
| 149 |
my $xml; |
| 150 |
while (read $fh, my($chunk), 8192) { |
| 151 |
$xml .= $chunk; |
| 152 |
} |
| 153 |
|
| 154 |
return $xml; |
| 155 |
} |
| 156 |
|
| 157 |
sub get_uri { |
| 158 |
my $class = shift; |
| 159 |
my ($stream) = @_; |
| 160 |
|
| 161 |
# Get or create UserAgent |
| 162 |
my $ua; |
| 163 |
if (blessed($class) && $class->{useragent}) { |
| 164 |
$ua = $class->{useragent}; |
| 165 |
} else { |
| 166 |
$ua = LWP::UserAgent->new; |
| 167 |
$ua->agent(__PACKAGE__ . "/$VERSION"); |
| 168 |
$ua->env_proxy; # force allowing of proxies |
| 169 |
} |
| 170 |
|
| 171 |
my $res = URI::Fetch->fetch($stream, UserAgent => $ua) |
| 172 |
or return $class->error(URI::Fetch->errstr); |
| 173 |
return $class->error("This feed has been permanently removed") |
| 174 |
if $res->status == URI::Fetch::URI_GONE(); |
| 175 |
return $res->content; |
| 176 |
} |
| 177 |
|
| 178 |
sub identify_format { |
| 179 |
my $feed = shift; |
| 180 |
my($xml) = @_; |
| 181 |
foreach my $class (@formatters) { |
| 182 |
my ($name) = ($class =~ m!([^:]+)$!); |
| 183 |
# TODO ugly |
| 184 |
my $tmp = $$xml; |
| 185 |
return $name if eval { $class->identify(\$tmp) }; |
| 186 |
return $feed->error($@) if $@; |
| 187 |
} |
| 188 |
return $feed->error("Cannot detect feed type"); |
| 189 |
} |
| 190 |
|
| 191 |
sub _get_first_tag { |
| 192 |
my $class = shift; |
| 193 |
my ($xml) = @_; |
| 194 |
|
| 195 |
|
| 196 |
## Auto-detect feed type based on first element. This is prone |
| 197 |
## to breakage, but then again we don't want to parse the whole |
| 198 |
## feed ourselves. |
| 199 |
my $tag; |
| 200 |
while ($$xml =~ /<(\S+)/sg) { |
| 201 |
(my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd; |
| 202 |
my $first = substr $t, 0, 1; |
| 203 |
$tag = $t, last unless $first eq '?' || $first eq '!'; |
| 204 |
} |
| 205 |
die ("Cannot find first element") unless $tag; |
| 206 |
$tag =~ s/^.*://; |
| 207 |
return $tag; |
| 208 |
} |
| 209 |
|
| 210 |
sub find_feeds { |
| 211 |
my $class = shift; |
| 212 |
my($uri) = @_; |
| 213 |
my @feeds = Feed::Find->find($uri) |
| 214 |
or return $class->error(Feed::Find->errstr); |
| 215 |
@feeds; |
| 216 |
} |
| 217 |
|
| 218 |
sub convert { |
| 219 |
my $feed = shift; |
| 220 |
my($format) = @_; |
| 221 |
my $new = XML::Feed->new($format); |
| 222 |
for my $field (qw( title link description language author copyright modified generator )) { |
| 223 |
my $val = $feed->$field(); |
| 224 |
next unless defined $val; |
| 225 |
$new->$field($val); |
| 226 |
} |
| 227 |
for my $entry ($feed->entries) { |
| 228 |
$new->add_entry($entry->convert($format)); |
| 229 |
} |
| 230 |
$new; |
| 231 |
} |
| 232 |
|
| 233 |
sub splice { |
| 234 |
my $feed = shift; |
| 235 |
my($other) = @_; |
| 236 |
my %ids = map { $_->id => 1 } $feed->entries; |
| 237 |
for my $entry ($other->entries) { |
| 238 |
$feed->add_entry($entry) unless $ids{$entry->id}++; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
sub _convert_entry { |
| 243 |
my $feed = shift; |
| 244 |
my $entry = shift; |
| 245 |
my $feed_format = ref($feed); $feed_format =~ s!^XML::Feed::Format::!!; |
| 246 |
my $entry_format = ref($entry); $entry_format =~ s!^XML::Feed::Entry::Format::!!; |
| 247 |
return $entry if $entry_format eq $feed_format; |
| 248 |
return $entry->convert($feed_format); |
| 249 |
} |
| 250 |
|
| 251 |
sub useragent { |
| 252 |
my $feed = shift; |
| 253 |
if (@_) { |
| 254 |
# Setter: validate the useragent |
| 255 |
my $ua = shift; |
| 256 |
unless (blessed($ua) && $ua->isa('LWP::UserAgent')) { |
| 257 |
Carp::croak("useragent must be an LWP::UserAgent object"); |
| 258 |
} |
| 259 |
$feed->{useragent} = $ua; |
| 260 |
return $ua; |
| 261 |
} else { |
| 262 |
# Getter: return existing or create new one |
| 263 |
unless ($feed->{useragent}) { |
| 264 |
$feed->{useragent} = LWP::UserAgent->new; |
| 265 |
$feed->{useragent}->agent(__PACKAGE__ . "/$VERSION"); |
| 266 |
$feed->{useragent}->env_proxy; |
| 267 |
} |
| 268 |
return $feed->{useragent}; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
sub base; |
| 273 |
sub format; |
| 274 |
sub title; |
| 275 |
sub link; |
| 276 |
sub self_link; |
| 277 |
sub description; |
| 278 |
sub language; |
| 279 |
sub author; |
| 280 |
sub copyright; |
| 281 |
sub modified; |
| 282 |
sub generator; |
| 283 |
sub add_entry; |
| 284 |
sub entries; |
| 285 |
sub as_xml; |
| 286 |
sub id; |
| 287 |
sub image; |
| 288 |
|
| 289 |
sub tagline { shift->description(@_) } |
| 290 |
sub items { $_[0]->entries } |
| 291 |
|
| 292 |
# RFC 5005 |
| 293 |
sub first_link; |
| 294 |
sub last_link; |
| 295 |
sub previous_link; |
| 296 |
sub next_link; |
| 297 |
sub current_link; |
| 298 |
sub prev_archive_link; |
| 299 |
sub next_archive_link; |
| 300 |
|
| 301 |
1; |
| 302 |
__END__ |
| 303 |
|
| 304 |
=head1 NAME |
| 305 |
|
| 306 |
XML::Feed - Syndication feed parser and auto-discovery |
| 307 |
|
| 308 |
=head1 SYNOPSIS |
| 309 |
|
| 310 |
use XML::Feed; |
| 311 |
my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml')) |
| 312 |
or die XML::Feed->errstr; |
| 313 |
print $feed->title, "\n"; |
| 314 |
for my $entry ($feed->entries) { |
| 315 |
} |
| 316 |
|
| 317 |
## Find all of the syndication feeds on a given page, using |
| 318 |
## auto-discovery. |
| 319 |
my @feeds = XML::Feed->find_feeds('http://example.com/'); |
| 320 |
|
| 321 |
=head1 DESCRIPTION |
| 322 |
|
| 323 |
I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It |
| 324 |
also implements feed auto-discovery for finding feeds, given a URI. |
| 325 |
|
| 326 |
I<XML::Feed> supports the following syndication feed formats: |
| 327 |
|
| 328 |
=over 4 |
| 329 |
|
| 330 |
=item * RSS 0.91 |
| 331 |
|
| 332 |
=item * RSS 1.0 |
| 333 |
|
| 334 |
=item * RSS 2.0 |
| 335 |
|
| 336 |
=item * Atom |
| 337 |
|
| 338 |
=back |
| 339 |
|
| 340 |
The goal of I<XML::Feed> is to provide a unified API for parsing and using |
| 341 |
the various syndication formats. The different flavors of RSS and Atom |
| 342 |
handle data in different ways: date handling; summaries and content; |
| 343 |
escaping and quoting; etc. This module attempts to remove those differences |
| 344 |
by providing a wrapper around the formats and the classes implementing |
| 345 |
those formats (L<XML::RSS> and L<XML::Atom::Feed>). For example, dates are |
| 346 |
handled differently in each of the above formats. To provide a unified API for |
| 347 |
date handling, I<XML::Feed> converts all date formats transparently into |
| 348 |
L<DateTime> objects, which it then returns to the caller. |
| 349 |
|
| 350 |
=head1 USAGE |
| 351 |
|
| 352 |
=head2 XML::Feed->new($format) |
| 353 |
|
| 354 |
=head2 XML::Feed->new($format, \%args) |
| 355 |
|
| 356 |
Creates a new empty I<XML::Feed> object using the format I<$format>. |
| 357 |
|
| 358 |
$feed = XML::Feed->new('Atom'); |
| 359 |
$feed = XML::Feed->new('RSS'); |
| 360 |
$feed = XML::Feed->new('RSS', version => '0.91'); |
| 361 |
|
| 362 |
An optional hashref of arguments can be provided as the last parameter: |
| 363 |
|
| 364 |
my $ua = LWP::UserAgent->new; |
| 365 |
$ua->timeout(30); |
| 366 |
$feed = XML::Feed->new('Atom', { useragent => $ua }); |
| 367 |
|
| 368 |
Currently supported arguments: |
| 369 |
|
| 370 |
=over 4 |
| 371 |
|
| 372 |
=item * useragent |
| 373 |
|
| 374 |
An L<LWP::UserAgent> object (or subclass) to use for fetching feeds via URI. |
| 375 |
If not provided, a default UserAgent will be created when needed. |
| 376 |
|
| 377 |
=back |
| 378 |
|
| 379 |
=head2 XML::Feed->parse($stream) |
| 380 |
|
| 381 |
=head2 XML::Feed->parse($stream, \%args) |
| 382 |
|
| 383 |
=head2 XML::Feed->parse($stream, $format) |
| 384 |
|
| 385 |
=head2 XML::Feed->parse($stream, $format, \%args) |
| 386 |
|
| 387 |
Parses a syndication feed identified by I<$stream> and returns an |
| 388 |
I<XML::Feed> object. I<$stream> can be any |
| 389 |
one of the following: |
| 390 |
|
| 391 |
=over 4 |
| 392 |
|
| 393 |
=item * Scalar reference |
| 394 |
|
| 395 |
A reference to string containing the XML body of the feed. |
| 396 |
|
| 397 |
=item * Filehandle |
| 398 |
|
| 399 |
An open filehandle from which the feed XML will be read. |
| 400 |
|
| 401 |
=item * File name |
| 402 |
|
| 403 |
The name of a file containing the feed XML. |
| 404 |
|
| 405 |
=item * URI object |
| 406 |
|
| 407 |
A URI from which the feed XML will be retrieved. |
| 408 |
|
| 409 |
=back |
| 410 |
|
| 411 |
I<$format> allows you to override format guessing. |
| 412 |
|
| 413 |
An optional hashref of arguments can be provided as the last parameter: |
| 414 |
|
| 415 |
my $ua = LWP::UserAgent->new; |
| 416 |
$ua->timeout(30); |
| 417 |
$feed = XML::Feed->parse('https://example.com/feed', { useragent => $ua }); |
| 418 |
|
| 419 |
Currently supported arguments: |
| 420 |
|
| 421 |
=over 4 |
| 422 |
|
| 423 |
=item * useragent |
| 424 |
|
| 425 |
An L<LWP::UserAgent> object (or subclass) to use for fetching feeds via URI. |
| 426 |
If not provided, a default UserAgent will be created when needed. |
| 427 |
|
| 428 |
=back |
| 429 |
|
| 430 |
=head2 XML::Feed->get_file($filename) |
| 431 |
|
| 432 |
Gets a feed from a file. |
| 433 |
|
| 434 |
=head3 XML::Feed->get_fh($fh) |
| 435 |
|
| 436 |
Gets a feed from pre-opened filehandle. |
| 437 |
|
| 438 |
=head2 XML::Feed->get_uri($uri) |
| 439 |
|
| 440 |
Gets a feed from a URI. |
| 441 |
|
| 442 |
=head2 XML::Feed->find_feeds($uri) |
| 443 |
|
| 444 |
Given a URI I<$uri>, use auto-discovery to find all of the feeds linked |
| 445 |
from that page (using I<E<lt>linkE<gt>> tags). |
| 446 |
|
| 447 |
Returns a list of feed URIs. |
| 448 |
|
| 449 |
=head2 XML::Feed->identify_format(\$xml) |
| 450 |
|
| 451 |
Given the xml of a feed return what format it is in, with C<Atom> or C<RSS> for |
| 452 |
all versions of RSS. Note that you pass in a scalar ref to the xml string. |
| 453 |
|
| 454 |
=head2 $feed->convert($format) |
| 455 |
|
| 456 |
Converts the I<XML::Feed> object into the I<$format> format, and returns |
| 457 |
the new object. |
| 458 |
|
| 459 |
=head2 $feed->splice($other_feed) |
| 460 |
|
| 461 |
Splices in all of the entries from the feed I<$other_feed> into I<$feed>, |
| 462 |
skipping posts that are already in I<$feed>. |
| 463 |
|
| 464 |
=head2 $feed->format |
| 465 |
|
| 466 |
Returns the format of the feed (C<Atom>, or some version of C<RSS>). |
| 467 |
|
| 468 |
=head2 $feed->title([ $title ]) |
| 469 |
|
| 470 |
The title of the feed/channel. |
| 471 |
|
| 472 |
=head2 $feed->base([ $base ]) |
| 473 |
|
| 474 |
The url base of the feed/channel. |
| 475 |
|
| 476 |
=head2 $feed->link([ $uri ]) |
| 477 |
|
| 478 |
The permalink of the feed/channel. |
| 479 |
|
| 480 |
=head2 $feed->tagline([ $tagline ]) |
| 481 |
|
| 482 |
The description or tagline of the feed/channel. |
| 483 |
|
| 484 |
=head2 $feed->description([ $description ]) |
| 485 |
|
| 486 |
Alias for I<$feed-E<gt>tagline>. |
| 487 |
|
| 488 |
=head2 $feed->author([ $author ]) |
| 489 |
|
| 490 |
The author of the feed/channel. |
| 491 |
|
| 492 |
=head2 $feed->language([ $language ]) |
| 493 |
|
| 494 |
The language of the feed. |
| 495 |
|
| 496 |
=head2 $feed->copyright([ $copyright ]) |
| 497 |
|
| 498 |
The copyright notice of the feed. |
| 499 |
|
| 500 |
=head2 $feed->modified([ $modified ]) |
| 501 |
|
| 502 |
A I<DateTime> object representing the last-modified date of the feed. |
| 503 |
|
| 504 |
If present, I<$modified> should be a I<DateTime> object. |
| 505 |
|
| 506 |
=head2 $feed->generator([ $generator ]) |
| 507 |
|
| 508 |
The generator of the feed. |
| 509 |
|
| 510 |
=head2 $feed->self_link ([ $uri ]) |
| 511 |
|
| 512 |
The Atom Self-link of the feed: |
| 513 |
|
| 514 |
L<http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html> |
| 515 |
|
| 516 |
A string. |
| 517 |
|
| 518 |
=head2 $feed->entries |
| 519 |
|
| 520 |
A list of the entries/items in the feed. Returns an array containing |
| 521 |
L<XML::Feed::Entry> objects. |
| 522 |
|
| 523 |
=head2 $feed->items |
| 524 |
|
| 525 |
A synonym (alias) for C<$feed-E<gt>entries>. |
| 526 |
|
| 527 |
=head2 $feed->add_entry($entry) |
| 528 |
|
| 529 |
Adds an entry to the feed. I<$entry> should be an L<XML::Feed::Entry> |
| 530 |
object in the correct format for the feed. |
| 531 |
|
| 532 |
=head2 $feed->as_xml |
| 533 |
|
| 534 |
Returns an XML representation of the feed, in the format determined by |
| 535 |
the current format of the I<$feed> object. |
| 536 |
|
| 537 |
=head2 $feed->useragent([ $ua ]) |
| 538 |
|
| 539 |
Get or set the L<LWP::UserAgent> object used for fetching feeds. |
| 540 |
|
| 541 |
If called without arguments, returns the current UserAgent object, |
| 542 |
creating a default one if it doesn't exist. |
| 543 |
|
| 544 |
If called with an L<LWP::UserAgent> object (or subclass), sets it as |
| 545 |
the UserAgent for this feed. |
| 546 |
|
| 547 |
my $ua = LWP::UserAgent->new; |
| 548 |
$ua->timeout(30); |
| 549 |
$feed->useragent($ua); |
| 550 |
|
| 551 |
# Later, retrieve it |
| 552 |
my $current_ua = $feed->useragent; |
| 553 |
|
| 554 |
=head2 $feed->first_link ([ $uri ]) |
| 555 |
|
| 556 |
The Atom First-link for feed paging and archiving (RFC 5005). |
| 557 |
|
| 558 |
L<http://tools.ietf.org/html/rfc5005> |
| 559 |
|
| 560 |
=head2 $feed->last_link ([ $uri ]) |
| 561 |
|
| 562 |
The Atom Last-link for feed paging and archiving. |
| 563 |
|
| 564 |
=head2 $feed->next_link ([ $uri ]) |
| 565 |
|
| 566 |
The Atom Next-link for feed paging and archiving. |
| 567 |
|
| 568 |
=head2 $feed->previous_link ([ $uri ]) |
| 569 |
|
| 570 |
The Atom Previous-link for feed paging and archiving. |
| 571 |
|
| 572 |
=head2 $feed->current_link ([ $uri ]) |
| 573 |
|
| 574 |
The Atom Current-link for feed paging and archiving. |
| 575 |
|
| 576 |
=head2 $feed->next_archive_link ([ $uri ]) |
| 577 |
|
| 578 |
The Atom Next-link for feed paging and archiving. |
| 579 |
|
| 580 |
=head2 $feed->prev_archive_link ([ $uri ]) |
| 581 |
|
| 582 |
The Atom Prev-Archive-link for feed paging and archiving. |
| 583 |
|
| 584 |
=head1 PACKAGE VARIABLES |
| 585 |
|
| 586 |
=over 4 |
| 587 |
|
| 588 |
=item C<$XML::Feed::Format::RSS::PREFERRED_PARSER> |
| 589 |
|
| 590 |
If you want to use another RSS parser class than XML::RSS (default), you can |
| 591 |
change the class by setting C<$PREFERRED_PARSER> variable in the |
| 592 |
XML::Feed::Format::RSS package. |
| 593 |
|
| 594 |
$XML::Feed::Format::RSS::PREFERRED_PARSER = "XML::RSS::LibXML"; |
| 595 |
|
| 596 |
B<Note:> this will only work for parsing feeds, not creating feeds. |
| 597 |
|
| 598 |
B<Note:> Only C<XML::RSS::LibXML> version 0.3004 is known to work at the moment. |
| 599 |
|
| 600 |
=item C<$XML::Feed::MULTIPLE_ENCLOSURES> |
| 601 |
|
| 602 |
Although the RSS specification states that there can be at most one enclosure per item |
| 603 |
some feeds break this rule. |
| 604 |
|
| 605 |
If this variable is set then C<XML::Feed> captures all of them and makes them available as a list. |
| 606 |
|
| 607 |
Otherwise it returns the last enclosure parsed. |
| 608 |
|
| 609 |
B<Note:> C<XML::RSS> version 1.44 is needed for this to work. |
| 610 |
|
| 611 |
=back |
| 612 |
|
| 613 |
=cut |
| 614 |
|
| 615 |
=head1 VALID FEEDS |
| 616 |
|
| 617 |
For reference, this cgi script will create valid, albeit nonsensical feeds |
| 618 |
(according to C<http://feedvalidator.org> anyway) for Atom 1.0 and RSS 0.90, |
| 619 |
0.91, 1.0 and 2.0. |
| 620 |
|
| 621 |
#!perl -w |
| 622 |
|
| 623 |
use strict; |
| 624 |
use CGI; |
| 625 |
use CGI::Carp qw(fatalsToBrowser); |
| 626 |
use DateTime; |
| 627 |
use XML::Feed; |
| 628 |
|
| 629 |
my $cgi = CGI->new; |
| 630 |
my @args = ( $cgi->param('format') // "Atom" ); |
| 631 |
push @args, ( version => $cgi->param('version') ) if $cgi->param('version'); |
| 632 |
|
| 633 |
my $feed = XML::Feed->new(@args); |
| 634 |
$feed->id("http://".time.rand()."/"); |
| 635 |
$feed->title('Test Feed'); |
| 636 |
$feed->link($cgi->url); |
| 637 |
$feed->self_link($cgi->url( -query => 1, -full => 1, -rewrite => 1) ); |
| 638 |
$feed->modified(DateTime->now); |
| 639 |
|
| 640 |
my $entry = XML::Feed::Entry->new(); |
| 641 |
$entry->id("http://".time.rand()."/"); |
| 642 |
$entry->link("http://example.com"); |
| 643 |
$entry->title("Test entry"); |
| 644 |
$entry->summary("Test summary"); |
| 645 |
$entry->content("Foo"); |
| 646 |
$entry->modified(DateTime->now); |
| 647 |
$entry->author('[email protected] (Testy McTesterson)'); |
| 648 |
$feed->add_entry($entry); |
| 649 |
|
| 650 |
my $mime = ("Atom" eq $feed->format) ? "application/atom+xml" : "application/rss+xml"; |
| 651 |
print $cgi->header($mime); |
| 652 |
print $feed->as_xml; |
| 653 |
|
| 654 |
|
| 655 |
=head1 LICENSE |
| 656 |
|
| 657 |
I<XML::Feed> is free software; you may redistribute it and/or modify it |
| 658 |
under the same terms as Perl itself. |
| 659 |
|
| 660 |
=head1 AUTHOR & COPYRIGHT |
| 661 |
|
| 662 |
Except where otherwise noted, I<XML::Feed> is Copyright 2004-2008 |
| 663 |
Six Apart. All rights reserved. |
| 664 |
|
| 665 |
=head1 SUPPORT |
| 666 |
|
| 667 |
For support contact the XML::Feed mailing list - [email protected]. |
| 668 |
|
| 669 |
=head1 SOURCE CODE |
| 670 |
|
| 671 |
The latest version of I<XML::Feed> can be found at |
| 672 |
|
| 673 |
http://github.com/davorg/XML-Feed |
| 674 |
|
| 675 |
=cut |