document closed Debian bug
[davical.git] / inc / AtomFeed.php
blob6e1dbf4722b4eee2608dc63bb2c30a14156cb756
1 <?php
3 require_once("XMLDocument.php");
5 define('ATOM_XMLNS','http://www.w3.org/2005/Atom');
6 define('XHTML_XMLNS','http://www.w3.org/1999/xhtml');
9 /**
10 * These two classes here sort of emulate the interface from the Zend Framework API
11 * with regard to ZendFeedWriteFeed for constructing an Atom feed. Except we do it
12 * in a DAViCal way, and we have some huge limitations:
13 * - We *only* support Atom feeds.
14 * - We *only* support creating them.
16 * @author Andrew McMillan <andrew@morphoss.com>
20 class AtomXHTMLContent /* extends XMLElement */ {
21 private $content_string;
23 function __construct($xhtml) {
24 $this->content_string = $xhtml;
27 function Render( $ignore1, $ignore2, $ignore3 ) {
28 return $this->content_string . "\n";
33 class AtomEntry {
34 /**
35 <entry xmlns:xhtml="http://www.w3.org/1999/xhtml">
36 <title type="html"><![CDATA[Woohoo! Time to Par-tay! (1/1)]]></title>
38 <summary type="html"><![CDATA[Have a microparty. All the best parties are monthly!]]></summary>
39 <published>2008-10-25T11:07:49+13:00</published>
40 <updated>2010-12-27T06:49:16+13:00</updated>
41 <id>http://mycaldav/feed.php/user1/home/MICROPARTY-77C6-4FB7-BDD3-6882E2F1BE74.ics#UID:MICROPARTY-77C6-4FB7-BDD3-6882E2F1BE74</id>
42 <content xmlns:xhtml="http://www.w3.org/1999/xhtml" type="xhtml">
43 <xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml"><xhtml:strong>Time:</xhtml:strong> 2008-11-21 16:00:00<xhtml:br/><xhtml:br/><xhtml:strong>Description</xhtml:strong>:<xhtml:br/>Have a microparty. All the best parties are monthly!</xhtml:div>
45 </content>
46 </entry>
48 private $id;
49 private $title;
50 private $updated;
51 private $nodes;
53 function __construct( $id, $title, $published, $updated ) {
54 $this->nodes = array( 'id', 'title', 'updated' ); // placeholders
57 public function setId( $new_value ) {
58 $this->id = new XMLElement('id', rtrim($new_value,"\r\n"));
59 return $this->id;
62 public function setTitle( $new_value, $type = 'text' ) {
63 $this->title = new XMLElement('title', $new_value, array( 'type' => $type ));
64 return $this->title;
67 public static function setDate( $tagname, $epoch ) {
68 // e.g. 2010-12-26T17:49:16+13:00
69 return new XMLElement($tagname, date('Y-m-d\TH:i:sP',$epoch));
72 public function setDateModified( $epoch ) {
73 $this->updated = self::setDate('updated', $epoch);
74 return $this->updated;
77 public function setDateCreated( $epoch ) {
78 $node = self::setDate('published', $epoch);
79 $this->nodes[] = $node;
80 return $node;
83 public function setLink( $new_value, $type="text/calendar", $rel='alternate' ) {
84 return $this->addNode('link', $new_value, array( 'rel' => $rel, 'type' => $type ) );
87 public function addAuthor( $new_value ) {
88 if ( is_array($new_value) && isset($new_value['name']) ) {
89 $author = $this->addNode('author' );
90 foreach( $new_value AS $k => $v ) {
91 $author->NewElement($k, $v);
93 return $author;
95 throw new Exception("AtomFeed::addAuthor(\$new_value) the \$new_value MUST be an array with at least a 'name' element. RFC4287-3.2");
99 public function addCategory( $new_value ) {
100 if ( is_array($new_value) && isset($new_value['term']) ) {
101 $category = $this->addNode('category', null, $new_value );
102 return $category;
104 throw new Exception("AtomFeed::addCategory(\$new_value) the \$new_value MUST be an array with at least a 'term' element, and potentially a 'scheme' and a 'label' element. RFC4287-4.2.2");
108 public function setDescription( $new_value, $type = 'text' ) {
109 return $this->addNode('summary', $new_value, array( 'type' => $type ) );
112 public function setContent( $new_value, $type = 'xhtml' ) {
113 $content = $this->addNode('content', null, array( 'type' => $type ) );
114 if ( $type == 'xhtml' ) {
115 $content->NewElement('div', array( new AtomXHTMLContent($new_value) ), array('xmlns' => XHTML_XMLNS));
117 else {
118 $content->SetContent($new_value);
120 return $content;
123 public function addNode( $in_tag, $content=false, $attributes=false, $xmlns=null ) {
124 $node = new XMLElement($in_tag,$content,$attributes,$xmlns);
125 if ( !isset($node) ) return null;
126 $this->nodes[] = $node;
127 return $node;
130 public function getXML() {
131 $this->nodes[0] = $this->id;
132 $this->nodes[1] = $this->title;
133 $this->nodes[2] = $this->updated;
134 return $this->nodes;
139 class AtomFeed extends XMLDocument {
141 private $id;
142 private $title;
143 private $updated;
144 private $nodes;
146 public function __construct() {
147 global $c;
148 parent::__construct( array( ATOM_XMLNS => null, XHTML_XMLNS => 'xhtml' ) );
149 $this->title = 'DAViCal Atom Feed';
150 $this->nodes = array( 'id', 'title', 'updated', // placeholders
151 new XMLElement('generator', 'DAViCal', array('uri' => 'http://www.davical.org/', 'version' => $c->version_string ) )
156 <id>http://mycaldav/feed.php/user1/home.ics</id>
157 <title type="text">CalDAV Feed: User 1's Calendaranza</title>
158 <updated>2010-12-26T17:49:16+13:00</updated>
159 <generator uri="http://framework.zend.com" version="1.10.7">Zend_Feed_Writer</generator>
160 <link rel="alternate" type="text/html" href="http://mycaldav/feed.php/user1/home.ics"/>
161 <link rel="self" type="application/atom+xml" href="http://mycaldav/feed.php/user1/home/"/>
162 <author>
163 <name>User 1</name>
164 <email>user1@example.net</email>
165 <uri>http://mycaldav/feed.php/caldav.php/user1/</uri>
166 </author>
169 public function setId( $new_value ) {
170 $this->id = $this->NewXMLElement('id', $new_value);
171 return $this->id;
174 public function setTitle( $new_value, $type = 'text' ) {
175 $this->title = $this->NewXMLElement('title', $new_value, array( 'type' => $type ));
176 return $this->title;
179 public function setDateModified( $epoch ) {
180 $this->updated = AtomEntry::setDate('updated', $epoch);
181 return $this->updated;
184 public function setLink( $new_value, $type="text/calendar", $rel='alternate' ) {
185 return $this->addNode('link', $new_value, array( 'rel' => $rel, 'type' => $type ) );
190 * Sets the feed link (rel=self), ignoring the parameter which is for
191 * compatibility with the Zend library API, although we use this for
192 * the Id, whereas they use the first link that is set.
193 * @param uri $new_value The link target
194 * @return XMLElement the node that was added.
196 public function setFeedLink( $new_value, $type = null ) {
197 $this->setId($new_value);
198 return $this->setLink($new_value , 'application/atom+xml', 'self' );
201 public function addAuthor( $new_value ) {
202 if ( is_array($new_value) && isset($new_value['name']) ) {
203 $author = $this->addNode('author' );
204 foreach( $new_value AS $k => $v ) {
205 $this->NSElement($author, $k, $v);
207 return $author;
209 throw new Exception("AtomFeed::addAuthor(\$new_value) the \$new_value MUST be an array with at leas a 'name' element. RFC4287-3.2");
213 public function setDescription( $new_value, $type = 'text' ) {
214 return $this->addNode('subtitle', $new_value, array( 'type' => $type ) );
217 public function addNode( $in_tag, $content=false, $attributes=false, $xmlns=null ) {
218 $node = $this->NewXMLElement($in_tag,$content,$attributes,$xmlns);
219 if ( !isset($node) ) return null;
220 $this->nodes[] = $node;
221 return $node;
224 public function addEntry( $new_entry ) {
225 if ( !isset($new_entry) ) return;
226 $this->nodes[] = new XMLElement('entry', $new_entry->getXML() );
229 public function createEntry( $id=null, $title=null, $published=null, $updated=null ) {
230 return new AtomEntry($id,$title,$published,$updated);
233 public function export( $format='atom' ) {
234 if ( $format != 'atom' ) throw new Exception("AtomFeed class only supports creation of Atom 1.0 format feeds.");
235 $this->nodes[0] = $this->id;
236 $this->nodes[1] = $this->title;
237 $this->nodes[2] = $this->updated;
238 return $this->Render('feed', $this->nodes );