Zend_Feed_Writer.xml 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.feed.writer">
  4. <title>Zend_Feed_Writer</title>
  5. <sect2 id="zend.feed.writer.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Feed_Writer</classname> is the sibling component to
  9. <classname>Zend_Feed_Reader</classname> responsible for generating feeds for output. It
  10. supports the Atom 1.0 specification (RFC 4287) and <acronym>RSS</acronym> 2.0 as
  11. specified by the <acronym>RSS</acronym> Advisory Board (<acronym>RSS</acronym> 2.0.11).
  12. It does not deviate from these standards. It does, however, offer a simple Extension
  13. system which allows for any extension and module for either of these two specifications
  14. to be implemented if they are not provided out of the box.
  15. </para>
  16. <para>
  17. In many ways, <classname>Zend_Feed_Writer</classname> is the inverse of
  18. <classname>Zend_Feed_Reader</classname>. Where <classname>Zend_Feed_Reader</classname>
  19. focused on providing an easy to use architecture fronted by getter methods,
  20. <classname>Zend_Feed_Writer</classname> is fronted by similarly named setters or
  21. mutators. This ensures the <acronym>API</acronym> won't pose a learning curve to anyone
  22. familiar with <classname>Zend_Feed_Reader</classname>.
  23. </para>
  24. <para>
  25. As a result of this design, the rest may even be obvious. Behind the scenes, data set on
  26. any <classname>Zend_Feed_Reader</classname> object is translated at render time onto a
  27. DOMDocument object using the necessary feed elements. For each
  28. supported feed type there is both an Atom 1.0 and <acronym>RSS</acronym> 2.0 renderer.
  29. Using a DOMDocument rather a templating solution has numerous advantages,
  30. the most obvious being the ability to export the DOMDocument for
  31. additional processing and relying on <acronym>PHP</acronym> <acronym>DOM</acronym> for
  32. correct and valid rendering.
  33. </para>
  34. <para>
  35. As with <classname>Zend_Feed_Reader</classname>, <classname>Zend_Feed_Writer</classname>
  36. is a standalone replacement for <classname>Zend_Feed</classname>'s Builder architecture
  37. and is not compatible with those classes.
  38. </para>
  39. </sect2>
  40. <sect2 id="zend.feed.writer.architecture">
  41. <title>Architecture</title>
  42. <para>
  43. The architecture of <classname>Zend_Feed_Writer</classname> is very simple. It has two
  44. core sets of classes: containers and renderers.
  45. </para>
  46. <para>
  47. The containers include the <classname>Zend_Feed_Writer_Feed</classname> and
  48. <classname>Zend_Feed_Writer_Entry</classname> classes. The Entry classes can be attached
  49. to any Feed class. The sole purpose of these containers is to collect data about the
  50. feed to generate using a simple interface of setter methods. These methods perform some
  51. data validity testing. For example, it will validate any passed <acronym>URI</acronym>s,
  52. dates, etc. These checks are not tied to any of the feed standards. The container
  53. objects also contain methods to allow for fast rendering and export of the final feed,
  54. and these can be reused at will.
  55. </para>
  56. <para>
  57. While there are two data containers, there are four renderers - two matching container
  58. renderers per support feed type. The renderer accept a container, and based on its
  59. content attempt to generate a valid feed. If the renderer is unable to generate a valid
  60. feed, perhaps due to the container missing an obligatory data point, it will report this
  61. by throwing an <classname>Exception</classname>. While it is possible to ignore
  62. <classname>Exception</classname>s, this removes the default safeguard of ensuring you
  63. have sufficient data set to render a wholly valid feed.
  64. </para>
  65. <para>
  66. Due to the system being divided between data containers and renderers, it can make
  67. Extensions somewhat interesting. A typical Extension offering namespaced feed and entry
  68. level elements, must itself reflect the exact same architecture, i.e. offer feed and
  69. entry level data containers, and matching renderers. There is, fortunately, no complex
  70. integration work required since all Extension classes are simply registered and
  71. automatically used by the core classes. We'll meet Extensions in more detail at the end
  72. of this section.
  73. </para>
  74. </sect2>
  75. <sect2 id="zend.feed.writer.getting.started">
  76. <title>Getting Started</title>
  77. <para>
  78. Using <classname>Zend_Feed_Reader</classname> is as simple as setting data and
  79. triggering the renderer. Here is an example to generate a minimal Atom 1.0 feed.
  80. </para>
  81. <programlisting language="php"><![CDATA[
  82. /**
  83. * Create the parent feed
  84. */
  85. $feed = new Zend_Feed_Writer_Feed;
  86. $feed->setTitle('Paddy\'s Blog');
  87. $feed->setLink('http://www.example.com');
  88. $feed->setFeedLink('http://www.example.com/atom', 'atom');
  89. $feed->addAuthor(array(
  90. 'name' => 'Paddy',
  91. 'email' => 'paddy@example.com',
  92. 'uri' => 'http://www.example.com',
  93. ));
  94. $feed->setDateModified(time());
  95. $feed->addHub('http://pubsubhubbub.appspot.com/');
  96. /**
  97. * Add one or more entries. Note that entries must
  98. * be manually added once created.
  99. */
  100. $entry = $feed->createEntry();
  101. $entry->setTitle('All Your Base Are Belong To Us');
  102. $entry->setLink('http://www.example.com/all-your-base-are-belong-to-us');
  103. $entry->addAuthor(array(
  104. 'name' => 'Paddy',
  105. 'email' => 'paddy@example.com',
  106. 'uri' => 'http://www.example.com',
  107. ));
  108. $entry->setDateModified(time());
  109. $entry->setDateCreated(time());
  110. $entry->setDescription('Exposing the difficultly of porting games to English.');
  111. $entry->setContent(
  112. 'I am not writing the article. The example is long enough as is ;).'
  113. );
  114. $feed->addEntry($entry);
  115. /**
  116. * Render the resulting feed to Atom 1.0 and assign to $out.
  117. * You can substitute "atom" with "rss" to generate an RSS 2.0 feed.
  118. */
  119. $out = $feed->export('atom');
  120. ]]></programlisting>
  121. <para>
  122. The output rendered should be as follows:
  123. </para>
  124. <programlisting language="xml"><![CDATA[
  125. <?xml version="1.0" encoding="utf-8"?>
  126. <feed xmlns="http://www.w3.org/2005/Atom">
  127. <title type="text">Paddy's Blog</title>
  128. <subtitle type="text">Writing about PC Games since 176 BC.</subtitle>
  129. <updated>2009-12-14T20:28:18+00:00</updated>
  130. <generator uri="http://framework.zend.com" version="1.10.0alpha">
  131. Zend_Feed_Writer
  132. </generator>
  133. <link rel="alternate" type="text/html" href="http://www.example.com"/>
  134. <link rel="self" type="application/atom+xml"
  135. href="http://www.example.com/atom"/>
  136. <id>http://www.example.com</id>
  137. <author>
  138. <name>Paddy</name>
  139. <email>paddy@example.com</email>
  140. <uri>http://www.example.com</uri>
  141. </author>
  142. <link rel="hub" href="http://pubsubhubbub.appspot.com/"/>
  143. <entry>
  144. <title type="html"><![CDATA[All Your Base Are Belong To Us]]]]><![CDATA[></title>
  145. <summary type="html">
  146. <![CDATA[Exposing the difficultly of porting games to English.]]]]><![CDATA[>
  147. </summary>
  148. <published>2009-12-14T20:28:18+00:00</published>
  149. <updated>2009-12-14T20:28:18+00:00</updated>
  150. <link rel="alternate" type="text/html"
  151. href="http://www.example.com/all-your-base-are-belong-to-us"/>
  152. <id>http://www.example.com/all-your-base-are-belong-to-us</id>
  153. <author>
  154. <name>Paddy</name>
  155. <email>paddy@example.com</email>
  156. <uri>http://www.example.com</uri>
  157. </author>
  158. <content type="html">
  159. <![CDATA[I am not writing the article.
  160. The example is long enough as is ;).]]]]><![CDATA[>
  161. </content>
  162. </entry>
  163. </feed>
  164. ]]></programlisting>
  165. <para>
  166. This is a perfectly valid Atom 1.0 example. It should be noted that omitting an
  167. obligatory point of data, such as a title, will trigger an
  168. <classname>Exception</classname> when rendering as Atom 1.0. This will differ for
  169. <acronym>RSS</acronym> 2.0 since a title may be omitted so long as a description is
  170. present. This gives rise to Exceptions that differ between the two standards depending
  171. on the renderer in use. By design, <classname>Zend_Feed_Writer</classname> will not
  172. render an invalid feed for either standard unless the end-user deliberately elects to
  173. ignore all Exceptions.
  174. </para>
  175. </sect2>
  176. <sect2 id="zend.feed.writer.setting.feed.data.points">
  177. <title>Setting Feed Data Points</title>
  178. <para>
  179. Before you can render a feed, you must first setup the data necessary for
  180. the feed being rendered. This utilises a simple setter style <acronym>API</acronym>
  181. which doubles as an initial method for validating the data being set. By design, the
  182. <acronym>API</acronym> closely matches that for <classname>Zend_Feed_Reader</classname>
  183. to avoid undue confusion and uncertainty.
  184. </para>
  185. <para>
  186. <classname>Zend_Feed_Writer</classname> offers this <acronym>API</acronym> via its data
  187. container classes <classname>Zend_Feed_Writer_Feed</classname> and
  188. <classname>Zend_Feed_Writer_Entry</classname>. These classes merely store
  189. all feed data in type-agnostic manner, meaning you may reuse any data
  190. container with any renderer without requiring additional work. Both classes
  191. are also amenable to Extensions, meaning that an Extension may define its own
  192. container classes which are registered to the base container classes as extensions, and
  193. are checked when any method call triggers the base container's
  194. <methodname>__call()</methodname> method.
  195. </para>
  196. <para>
  197. Here's a summary of the Core <acronym>API</acronym> for Feeds. You should note it
  198. comprises not only the basic <acronym>RSS</acronym> and Atom standards, but also
  199. accounts for a number of included Extensions bundled with
  200. <classname>Zend_Feed_Writer</classname>. The naming of these
  201. Extension sourced methods remain fairly generic - all Extension
  202. methods operate at the same level as the Core <acronym>API</acronym> though we do allow
  203. you to retrieve any specific Extension object separately if required.
  204. </para>
  205. <table>
  206. <title>Feed Level API Methods</title>
  207. <tgroup cols="2">
  208. <tbody>
  209. <row>
  210. <entry><methodname>setId()</methodname></entry>
  211. <entry>
  212. Set a unique ID associated with this feed. For Atom 1.0
  213. this is an atom:id element, whereas for <acronym>RSS</acronym> 2.0 it
  214. is added as a guid element. These are optional so long as a link is
  215. added, i.e. the link is set as the ID.
  216. </entry>
  217. </row>
  218. <row>
  219. <entry><methodname>setTitle()</methodname></entry>
  220. <entry>Set the title of the feed.</entry>
  221. </row>
  222. <row>
  223. <entry><methodname>setDescription()</methodname></entry>
  224. <entry>Set the text description of the feed.</entry>
  225. </row>
  226. <row>
  227. <entry><methodname>setLink()</methodname></entry>
  228. <entry>
  229. Set a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
  230. containing the same or
  231. similar information as this feed (i.e. if the feed is from a blog,
  232. it should provide the blog's <acronym>URI</acronym> where the
  233. <acronym>HTML</acronym> version of the entries can be read).
  234. </entry>
  235. </row>
  236. <row>
  237. <entry><methodname>setFeedLinks()</methodname></entry>
  238. <entry>
  239. Add a link to an <acronym>XML</acronym> feed, whether the feed being
  240. generated or an alternate <acronym>URI</acronym> pointing to the same
  241. feed but in a different format. At a minimum, it is recommended to
  242. include a link to the feed being generated so it has an identifiable
  243. final <acronym>URI</acronym> allowing a client to track its location
  244. changes without necessitating constant redirects. The parameter is an
  245. array of arrays, where each sub-array contains the keys "type" and
  246. "uri". The type should be one of "atom", "rss", or "rdf". If a type is
  247. omitted, it defaults to the type used when rendering the feed.
  248. </entry>
  249. </row>
  250. <row>
  251. <entry><methodname>setAuthors()</methodname></entry>
  252. <entry>
  253. Sets the data for authors. The parameter is an array of arrays
  254. where each sub-array may contain the keys "name", "email" and
  255. "uri". The "uri" value is only applicable for Atom feeds since
  256. <acronym>RSS</acronym> contains no facility to show it. For
  257. <acronym>RSS</acronym> 2.0, rendering will create two elements - an
  258. author element containing the email reference with the name in brackets,
  259. and a Dublin Core creator element only containing the name.
  260. </entry>
  261. </row>
  262. <row>
  263. <entry><methodname>setAuthor()</methodname></entry>
  264. <entry>
  265. Sets the data for a single author following the same
  266. format as described above for a single sub-array.
  267. </entry>
  268. </row>
  269. <row>
  270. <entry><methodname>setDateCreated()</methodname></entry>
  271. <entry>
  272. Sets the date on which this feed was created. Generally
  273. only applicable to Atom where it represents the date the resource
  274. described by an Atom 1.0 document was created. The expected parameter
  275. may be a <acronym>UNIX</acronym> timestamp or a
  276. <classname>Zend_Date</classname> object.
  277. </entry>
  278. </row>
  279. <row>
  280. <entry><methodname>getDateModified()</methodname></entry>
  281. <entry>
  282. Sets the date on which this feed was last modified. The expected
  283. parameter may be a <acronym>UNIX</acronym> timestamp or a
  284. <classname>Zend_Date</classname> object.
  285. </entry>
  286. </row>
  287. <row>
  288. <entry><methodname>setLanguage()</methodname></entry>
  289. <entry>
  290. Sets the language of the feed. This will be omitted unless set.
  291. </entry>
  292. </row>
  293. <row>
  294. <entry><methodname>getGenerator()</methodname></entry>
  295. <entry>
  296. Allows the setting of a generator. The parameter should be an
  297. array containing the keys "name", "version" and "uri". If omitted
  298. a default generator will be added referencing
  299. <classname>Zend_Feed_Writer</classname>, the current Zend Framework
  300. version and the Framework's <acronym>URI</acronym>.
  301. </entry>
  302. </row>
  303. <row>
  304. <entry><methodname>setCopyright()</methodname></entry>
  305. <entry>Sets a copyright notice associated with the feed.</entry>
  306. </row>
  307. <row>
  308. <entry><methodname>setHubs()</methodname></entry>
  309. <entry>
  310. Accepts an array of Pubsubhubbub Hub Endpoints to be rendered in
  311. the feed as Atom links so that PuSH Subscribers may subscribe to
  312. your feed. Note that you must implement a Pubsubhubbub Publisher in
  313. order for real-time updates to be enabled. A Publisher may be
  314. implemented using
  315. <classname>Zend_Feed_Pubsubhubbub_Publisher</classname>.
  316. </entry>
  317. </row>
  318. <row>
  319. <entry><methodname>setCategories()</methodname></entry>
  320. <entry>
  321. Accepts an array of categories for rendering, where each element is
  322. itself an array whose possible keys include "term", "label" and
  323. "scheme". The "term" is a typically a category name suitable for
  324. inclusion in a <acronym>URI</acronym>. The "label" may be a human
  325. readable category name supporting special characters (it is encoded
  326. during rendering) and is a required key. The "scheme" (called the domain
  327. in <acronym>RSS</acronym>) is optional but must be a valid
  328. <acronym>URI</acronym>.
  329. </entry>
  330. </row>
  331. </tbody>
  332. </tgroup>
  333. </table>
  334. </sect2>
  335. <sect2 id="zend.feed.writer.setting.entry.data.points">
  336. <title>Setting Entry Data Points</title>
  337. <para>
  338. Here's a summary of the Core <acronym>API</acronym> for Entries and Items. You should
  339. note it comprises not only the basic <acronym>RSS</acronym> and Atom standards, but
  340. also accounts for a number of included Extensions bundled with
  341. <classname>Zend_Feed_Writer</classname>. The naming of these
  342. Extension sourced methods remain fairly generic - all Extension
  343. methods operate at the same level as the Core <acronym>API</acronym> though we do allow
  344. you to retrieve any specific Extension object separately if required.
  345. </para>
  346. <table>
  347. <title>Entry Level API Methods</title>
  348. <tgroup cols="2">
  349. <tbody>
  350. <row>
  351. <entry><methodname>setId()</methodname></entry>
  352. <entry>
  353. Set a unique ID associated with this feed. For Atom 1.0
  354. this is an atom:id element, whereas for <acronym>RSS</acronym> 2.0 it is
  355. added as a guid element. These are optional so long as a link is
  356. added, i.e. the link is set as the ID.
  357. </entry>
  358. </row>
  359. <row>
  360. <entry><methodname>setTitle()</methodname></entry>
  361. <entry>Set the title of the feed.</entry>
  362. </row>
  363. <row>
  364. <entry><methodname>setDescription()</methodname></entry>
  365. <entry>Set the text description of the feed.</entry>
  366. </row>
  367. <row>
  368. <entry><methodname>setLink()</methodname></entry>
  369. <entry>
  370. Set a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
  371. containing the same or
  372. similar information as this feed (i.e. if the feed is from a blog,
  373. it should provide the blog's <acronym>URI</acronym> where the
  374. <acronym>HTML</acronym> version of the entries can be read).
  375. </entry>
  376. </row>
  377. <row>
  378. <entry><methodname>setFeedLinks()</methodname></entry>
  379. <entry>
  380. Add a link to an <acronym>XML</acronym> feed, whether the feed being
  381. generated or an alternate <acronym>URI</acronym> pointing to the same
  382. feed but in a different format. At a minimum, it is recommended to
  383. include a link to the feed being generated so it has an identifiable
  384. final <acronym>URI</acronym> allowing a client to track its location
  385. changes without necessitating constant redirects. The parameter is an
  386. array of arrays, where each sub-array contains the keys "type" and
  387. "uri". The type should be one of "atom", "rss", or "rdf". If a type is
  388. omitted, it defaults to the type used when rendering the feed.
  389. </entry>
  390. </row>
  391. <row>
  392. <entry><methodname>setAuthors()</methodname></entry>
  393. <entry>
  394. Sets the data for authors. The parameter is an array of arrays
  395. where each sub-array may contain the keys "name", "email" and
  396. "uri". The "uri" value is only applicable for Atom feeds since
  397. <acronym>RSS</acronym> contains no facility to show it. For
  398. <acronym>RSS</acronym> 2.0, rendering will create two elements - an
  399. author element containing the email reference with the name in brackets,
  400. and a Dublin Core creator element only containing the name.
  401. </entry>
  402. </row>
  403. <row>
  404. <entry><methodname>setAuthor()</methodname></entry>
  405. <entry>
  406. Sets the data for a single author following the same
  407. format as described above for a single sub-array.
  408. </entry>
  409. </row>
  410. <row>
  411. <entry><methodname>setDateCreated()</methodname></entry>
  412. <entry>
  413. Sets the date on which this feed was created. Generally
  414. only applicable to Atom where it represents the date the resource
  415. described by an Atom 1.0 document was created. The expected parameter
  416. may be a <acronym>UNIX</acronym> timestamp or a
  417. <classname>Zend_Date</classname> object.
  418. </entry>
  419. </row>
  420. <row>
  421. <entry><methodname>getDateModified()</methodname></entry>
  422. <entry>
  423. Sets the date on which this feed was last modified. The expected
  424. parameter may be a <acronym>UNIX</acronym> timestamp or a
  425. <classname>Zend_Date</classname> object.
  426. </entry>
  427. </row>
  428. <row>
  429. <entry><methodname>setLanguage()</methodname></entry>
  430. <entry>
  431. Sets the language of the feed. This will be omitted unless set.
  432. </entry>
  433. </row>
  434. <row>
  435. <entry><methodname>getGenerator()</methodname></entry>
  436. <entry>
  437. Allows the setting of a generator. The parameter should be an
  438. array containing the keys "name", "version" and "uri". If omitted
  439. a default generator will be added referencing
  440. <classname>Zend_Feed_Writer</classname>, the current Zend Framework
  441. version and the Framework's <acronym>URI</acronym>.
  442. </entry>
  443. </row>
  444. <row>
  445. <entry><methodname>setCopyright()</methodname></entry>
  446. <entry>Sets a copyright notice associated with the feed.</entry>
  447. </row>
  448. <row>
  449. <entry><methodname>setHubs()</methodname></entry>
  450. <entry>
  451. Accepts an array of Pubsubhubbub Hub Endpoints to be rendered in
  452. the feed as Atom links so that PuSH Subscribers may subscribe to
  453. your feed. Note that you must implement a Pubsubhubbub Publisher in
  454. order for real-time updates to be enabled. A Publisher may be
  455. implemented using
  456. <classname>Zend_Feed_Pubsubhubbub_Publisher</classname>.
  457. </entry>
  458. </row>
  459. <row>
  460. <entry><methodname>setCategories()</methodname></entry>
  461. <entry>
  462. Accepts an array of categories for rendering, where each element is
  463. itself an array whose possible keys include "term", "label" and
  464. "scheme". The "term" is a typically a category name suitable for
  465. inclusion in a <acronym>URI</acronym>. The "label" may be a human
  466. readable category name supporting special characters (it is encoded
  467. during rendering) and is a required key. The "scheme" (called the domain
  468. in <acronym>RSS</acronym>) is optional but must be a valid
  469. <acronym>URI</acronym>.
  470. </entry>
  471. </row>
  472. </tbody>
  473. </tgroup>
  474. </table>
  475. </sect2>
  476. </sect1>