root/branches/release-34/lib/MT/Atom.pm @ 1823

Revision 1823, 3.4 kB (checked in by takayama, 20 months ago)

Fixed BugId:67959
* Added check for result of object loading

  • Property svn:keywords set to Author Date Id Revision
Line 
1# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
4#
5# $Id$
6
7package MT::Atom;
8use strict;
9
10package MT::Atom::Entry;
11use MT::I18N qw( encode_text );
12use base qw( XML::Atom::Entry );
13
14sub _create_issued {
15    my ($ts, $blog) = @_;
16    my @co_list = unpack 'A4A2A2A2A2A2', $ts;
17    my $co = sprintf "%04d-%02d-%02dT%02d:%02d:%02d", @co_list;
18    my $epoch = Time::Local::timegm($co_list[5], $co_list[4], $co_list[3],
19                                    $co_list[2], $co_list[1]-1, $co_list[0]);
20    my $so = $blog->server_offset;
21    $so += 1 if (localtime $epoch)[8];
22    $so = sprintf("%s%02d:%02d", $so < 0 ? '-' : '+', 
23                  abs(int $so), abs($so - int $so)*60);
24    $co .= $so;
25}
26
27sub new_with_entry {
28    my $class = shift;
29    my($entry, %param) = @_;
30    my $rfc_compat = $param{Version} && $param{Version} eq '1';
31
32    my $atom = $class->new(%param);
33    $atom->title(encode_text($entry->title, undef, 'utf-8'));
34    $atom->summary(encode_text($entry->excerpt, undef, 'utf-8'));
35    $atom->content(encode_text($entry->text, undef, 'utf-8'));
36    # Old Atom API gets application/xhtml+xml for compatibility -- but why
37    # do we say it's that when all we're guaranteed is it's an opaque blob
38    # of text? So use 'html' for new RFC compatible output.
39    # XML::Atom::Content intelligently determines content-type for rfc compat.
40    unless ( $rfc_compat ) {
41        $atom->content->type('application/xhtml+xml');
42    }
43
44    my $mt_author = MT::Author->load($entry->author_id)
45        or return undef;
46    my $atom_author = new XML::Atom::Person(%param);
47    $atom_author->name(encode_text($mt_author->nickname, undef, 'utf-8'));
48    $atom_author->email($mt_author->email) if $mt_author->email;
49    my $author_url_field = $rfc_compat ? 'uri' : 'url';
50    $atom_author->$author_url_field($mt_author->url) if $mt_author->url;
51    $atom->author($atom_author);
52
53    for my $cat (@{ $entry->categories }) {
54        my $atom_cat = XML::Atom::Category->new(%param);
55        $atom_cat->term($cat->label);
56        $atom->add_category($atom_cat);
57    }
58
59    my $blog = MT::Blog->load($entry->blog_id)
60        or return undef;
61    my $co = _create_issued($entry->authored_on, $blog);
62    $atom->issued($co);
63    my $upd = $entry->modified_on;
64    if ( $upd ) {
65        $atom->updated( _create_issued( $upd, $blog ) );
66    }
67    else {
68        $atom->updated( $co );
69    }
70    $atom->add_link({ rel => 'alternate', type => 'text/html',
71                      href => $entry->permalink });
72    my ($host) = $blog->site_url =~ m!^https?://([^/:]+)(:\d+)?/!;
73
74    $atom->id($entry->atom_id);
75    #$atom->draft('true') if $entry->status != MT::Entry::RELEASE();
76
77    $atom;
78}
79
80sub new_with_asset { 
81    my $class = shift; 
82    my($asset, %param) = @_; 
83    my $atom = $class->new(%param); 
84    $atom->title($asset->label); 
85    $atom->summary($asset->description);
86    my $blog = MT::Blog->load($asset->blog_id)
87        or return undef;
88    $atom->issued(_create_issued($asset->created_on, $blog)); 
89    $atom->add_link({ rel => 'alternate', type => $asset->mime_type, 
90                      href => $asset->url, title => $asset->label }); 
91    my ($host) = $blog->site_url =~ m!^https?://([^/:]+)(:\d+)?/!;
92    $atom->id('tag:' . $host . ':asset-' . $asset->id);
93    return $atom; 
94} 
95
961;
97__END__
98
99=head1 NAME
100
101MT::Atom
102
103=head1 AUTHOR & COPYRIGHT
104
105Please see L<MT/AUTHOR & COPYRIGHT>.
106
107=cut
Note: See TracBrowser for help on using the browser.