root/trunk/lib/sfFeed/sfAtom1Feed.class.php

Revision 2, 5.1 kB (checked in by fabien, 6 years ago)

initial import

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  *
13  * Specification: http://atompub.org/2005/07/11/draft-ietf-atompub-format-10.html
14  *
15  * @package    symfony
16  * @subpackage addon
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @version    SVN: $Id$
19  */
20 class sfAtom1Feed extends sfFeed
21 {
22   public function getFeed()
23   {
24     header('Content-Type: application/atom+xml');
25
26     $xml = array();
27     $xml[] = '<?xml version="1.0" encoding="UTF-8" ?>';
28
29     if ($this->getLanguage())
30     {
31       $xml[] = sprintf('<feed xmlns="%s" xml:lang="%s">', 'http://www.w3.org/2005/Atom', $this->getLanguage());
32     }
33     else
34     {
35       $xml[] = sprintf('<feed xmlns="%s">', 'http://www.w3.org/2005/Atom');
36     }
37
38     $xml[] = '  <title>'.$this->getTitle().'</title>';
39     $xml[] = '  <link rel="alternate" href="'.sfContext::getInstance()->getController()->genUrl($this->getLink(), true).'"></link>';
40     if ($this->getFeedUrl())
41     {
42       $xml[] = '  <link rel="self" href="'.sfContext::getInstance()->getController()->genUrl($this->getFeedUrl(), true).'"></link>';
43     }
44     $xml[] = '  <id>'.sfContext::getInstance()->getController()->genUrl($this->getLink(), true).'</id>';
45     $xml[] = '  <updated>'.strftime('%Y-%m-%dT%H:%M:%SZ', $this->getLatestPostDate()).'</updated>';
46
47     if ($this->getAuthorName())
48     {
49       $xml[] = '  <author>';
50       $xml[] = '    <name>'.$this->getAuthorName().'</name>';
51       if ($this->getAuthorEmail())
52       {
53         $xml[] = '    <author_email>'.$this->getAuthorEmail().'</author_email>';
54       }
55       if ($this->getAuthorLink())
56       {
57         $xml[] = '    <author_link>'.$this->getAuthorLink().'</author_link>';
58       }
59       $xml[] = '  </author>';
60     }
61
62     if ($this->getSubtitle())
63     {
64       $xml[] = '  <subtitle>'.$this->getSubtitle().'</subtitle>';
65     }
66
67     foreach ($this->getCategories() as $category)
68     {
69       $xml[] = '  <category term="'.$category.'"></category>';
70     }
71
72     $xml[] = $this->getFeedElements();
73
74     $xml[] = '</feed>';
75
76     return implode("\n", $xml);
77   }
78
79   private function getFeedElements()
80   {
81     $xml = '';
82
83     foreach ($this->getItems() as $item)
84     {
85       $xml[] = '<entry>';
86       $xml[] = '  <title>'.htmlspecialchars($this->getItemFeedTitle($item)).'</title>';
87       $xml[] = '  <link href="'.sfContext::getInstance()->getController()->genUrl($this->getItemFeedLink($item), true).'"></link>';
88       if ($this->getItemFeedPubdate($item))
89       {
90         $xml[] = '  <updated>'.strftime('%Y-%m-%dT%H:%M:%SZ', $this->getItemFeedPubdate($item)).'</updated>';
91       }
92
93       // author information
94       if ($this->getItemFeedAuthorName($item))
95       {
96         $xml[] = '  <author>';
97         $xml[] = '    <name>'.$this->getItemFeedAuthorName($item).'</name>';
98         if ($this->getItemFeedAuthorEmail($item))
99         {
100           $xml[] = '    <author_email>'.$this->getItemFeedAuthorEmail($item).'</author_email>';
101         }
102         if ($this->getItemFeedAuthorLink($item))
103         {
104           $xml[] = '    <author_link>'.sfContext::getInstance()->getController()->genUrl($this->getItemFeedAuthorLink($item), true).'</author_link>';
105         }
106         $xml[] = '  </author>';
107       }
108
109       // unique id
110       if ($this->getItemFeedUniqueId($item))
111       {
112         $uniqueId = $this->getItemFeedUniqueId($item);
113       }
114       else
115       {
116         $uniqueId = $this->getTagUri($this->getItemFeedLink($item), $this->getItemFeedPubdate($item));
117       }
118       $xml[] = '  <id>'.$uniqueId.'</id>';
119
120       // summary
121       if ($this->getItemFeedDescription($item))
122       {
123         $xml[] = sprintf('  <summary type="html">%s</summary>', htmlspecialchars($this->getItemFeedDescription($item)));
124       }
125
126       // enclosure
127       if ((method_exists($item, 'getFeedEnclosure')) && ($enclosure = $item->getFeedEnclosure()))
128       {
129         $xml[] = sprintf('  <link rel="enclosure" href="%s" length="%s" type="%s"></link>', $enclosure->getUrl(), $enclosure->getLength(), $enclosure->getMimeType());
130       }
131
132       // categories
133       foreach ($this->getItemFeedCategories($item) as $category)
134       {
135         $xml[] = '  <category term="'.$category.'"></category>';
136       }
137
138       $xml[] = '</entry>';
139     }
140
141     return implode("\n", $xml);
142   }
143
144   // Creates a TagURI. See http://diveintomark.org/archives/2004/05/28/howto-atom-id
145   private function getTagUri($url, $date)
146   {
147     $tag = preg_replace('#^http\://#', '', $url);
148     if ($date)
149     {
150       $tag = preg_replace('#/#', ','.strftime('%Y-%m-%d', $date).':/', $tag, 1);
151     }
152     $tag = preg_replace('/#/', '/', $tag);
153
154     return 'tag:'.$tag;
155   }
156
157   private function getLatestPostDate()
158   {
159     $updates = array();
160     foreach ($this->getItems() as $item)
161     {
162       if ($this->getItemFeedPubdate($item))
163       {
164         $updates[] = $this->getItemFeedPubdate($item);
165       }
166     }
167
168     if ($updates)
169     {
170       sort($updates);
171       return $updates[count($updates) - 1];
172     }
173     else
174     {
175       return time();
176     }
177   }
178
179 }
180
181 ?>
182
Note: See TracBrowser for help on using the browser.