Zend_Feed_Reader.xml 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.feed.reader">
  4. <title>Zend_Feed_Reader</title>
  5. <sect2 id="zend.feed.reader.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Feed_Reader</classname> is a component used to
  9. consume <acronym>RSS</acronym> and Atom feeds of any version, including
  10. <acronym>RDF</acronym>/<acronym>RSS</acronym> 1.0,
  11. <acronym>RSS</acronym> 2.0 and Atom 0.3/1.0. The <acronym>API</acronym> for
  12. retrieving feed data is
  13. deliberately simple since <classname>Zend_Feed_Reader</classname> is
  14. capable of searching any feed of any type for the information
  15. requested through the <acronym>API</acronym>. If the typical elements containing this
  16. information are not present, it will adapt and fall back on a
  17. variety of alternative elements instead. This ability to choose from
  18. alternatives removes the need for users to create their own
  19. abstraction layer on top of the component to make it useful or have
  20. any in-depth knowledge of the underlying standards, current
  21. alternatives, and namespaced extensions.
  22. </para>
  23. <para>
  24. Internally, <classname>Zend_Feed_Reader</classname> works almost
  25. entirely on the basis of making XPath queries against the feed <acronym>XML</acronym>'s
  26. Document Object Model. The <acronym>DOM</acronym> is not exposed though a chained
  27. property <acronym>API</acronym> like <classname>Zend_Feed</classname> though the
  28. underlying <classname>DOMDocument</classname>,
  29. <classname>DOMElement</classname> and
  30. <classname>DOMXPath</classname> objects are exposed for external
  31. manipulation. This singular approach to parsing is consistent and
  32. the component offers a plugin system to add to the Feed and Entry
  33. level <acronym>API</acronym> by writing Extensions on a similar basis.
  34. </para>
  35. <para>
  36. Performance is assisted in three ways. First of all,
  37. <classname>Zend_Feed_Reader</classname> supports caching using
  38. <classname>Zend_Cache</classname> to maintain a copy of the original
  39. feed <acronym>XML</acronym>. This allows you to skip network requests for a feed
  40. <acronym>URI</acronym> if
  41. the cache is valid. Second, the Feed and Entry level <acronym>API</acronym> is backed
  42. by an internal cache (non-persistant) so repeat <acronym>API</acronym> calls for the
  43. same feed will avoid additional <acronym>DOM</acronym>/XPath use. Thirdly, importing
  44. feeds from a <acronym>URI</acronym> can take advantage of
  45. <acronym>HTTP</acronym> Conditional GET requests
  46. which allow servers to issue an empty 304 response when the
  47. requested feed has not changed since the last time you requested it.
  48. In the final case, an instance of <classname>Zend_Cache</classname>
  49. will hold the last received feed along with the ETag and
  50. Last-Modified header values sent in the <acronym>HTTP</acronym> response.
  51. </para>
  52. <para>
  53. In relation to <classname>Zend_Feed</classname>,
  54. <classname>Zend_Feed_Reader</classname> was formulated as a free
  55. standing replacement for <classname>Zend_Feed</classname> but it is
  56. not backwards compatible with <classname>Zend_Feed</classname>.
  57. Rather it is an alternative following a different ideology focused
  58. on being simple to use, flexible, consistent and extendable through
  59. the plugin system. <classname>Zend_Feed_Reader</classname> is also
  60. not capable of constructing feeds through this will be addressed at
  61. a future date.
  62. </para>
  63. </sect2>
  64. <sect2 id="zend.feed.reader.import">
  65. <title>Importing Feeds</title>
  66. <para>
  67. Importing a feed with <classname>Zend_Feed_Reader</classname> is not
  68. that much different to <classname>Zend_Feed</classname>. Feeds can
  69. be imported from a string, file, <acronym>URI</acronym> or an instance of type
  70. <classname>Zend_Feed_Abstract</classname>. Importing from a <acronym>URI</acronym> can
  71. additionally utilise a <acronym>HTTP</acronym> Conditional GET request. If importing
  72. fails, an exception will be raised. The end result will be an object
  73. of type <classname>Zend_Feed_Reader_FeedInterface</classname>, the
  74. core implementations of which are
  75. <classname>Zend_Feed_Reader_Feed_Rss</classname> and
  76. <classname>Zend_Feed_Reader_Feed_Atom</classname>
  77. (<classname>Zend_Feed</classname> took all the short names!). Both
  78. objects support multiple (all existing) versions of these broad feed
  79. types.
  80. </para>
  81. <para>
  82. In the following example, we import an <acronym>RDF</acronym>/<acronym>RSS</acronym> 1.0
  83. feed and extract some basic information that can be saved to a database or
  84. elsewhere.
  85. </para>
  86. <programlisting language="php"><![CDATA[
  87. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  88. $data = array(
  89. 'title' => $feed->getTitle(),
  90. 'link' => $feed->getLink(),
  91. 'dateModified' => $feed->getDateModified(),
  92. 'description' => $feed->getDescription(),
  93. 'language' => $feed->getLanguage(),
  94. 'entries' => array(),
  95. );
  96. foreach ($feed as $entry) {
  97. $edata = array(
  98. 'title' => $entry->getTitle(),
  99. 'description' => $entry->getDescription(),
  100. 'dateModified' => $entry->getDateModified(),
  101. 'author' => $entry->getAuthor(),
  102. 'link' => $entry->getLink(),
  103. 'content' => $entry->getContent()
  104. );
  105. $data['entries'][] = $edata;
  106. }
  107. ]]></programlisting>
  108. <para>
  109. The example above demonstrates
  110. <classname>Zend_Feed_Reader</classname>'s <acronym>API</acronym>, and it also
  111. demonstrates some of it's internal operation. In reality, the <acronym>RDF</acronym>
  112. feed selected does not have any native date or author elements,
  113. however it does utilise the Dublin Core 1.1 module which offers
  114. namespaced creator and date elements.
  115. <classname>Zend_Feed_Reader</classname> falls back on these and
  116. similar options if no relevant native elements exist. If it
  117. absolutely cannot find an alternative it will return <constant>NULL</constant>,
  118. indicating the information could not be found in the feed. You
  119. should note that classes implementing
  120. <classname>Zend_Feed_Reader_FeedInterface</classname> also implement
  121. the <acronym>SPL</acronym> <classname>Iterator</classname> and
  122. <classname>Countable</classname> interfaces.
  123. </para>
  124. <para>
  125. Feeds can also be imported from strings, files, and even objects of
  126. type <classname>Zend_Feed_Abstract</classname>.
  127. </para>
  128. <programlisting language="php"><![CDATA[
  129. // from a URI
  130. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  131. // from a String
  132. $feed = Zend_Feed_Reader::importString($feedXmlString);
  133. // from a file
  134. $feed = Zend_Feed_Reader::importFile('./feed.xml');
  135. // from a Zend_Feed_Abstract object
  136. $zfeed = Zend_Feed::import('http://www.planet-php.net/atom/');
  137. $feed = Zend_Feed_Reader::importFeed($zfeed);
  138. ]]></programlisting>
  139. </sect2>
  140. <sect2 id="zend.feed.reader.sources">
  141. <title>Retrieving Underlying Feed and Entry Sources</title>
  142. <para>
  143. <classname>Zend_Feed_Reader</classname> does it's best not to stick
  144. you in a narrow confine. If you need to work on a feed outside of
  145. <classname>Zend_Feed_Reader</classname>, you can extract the base
  146. <classname>DOMDocument</classname> or
  147. <classname>DOMElement</classname> objects from any class, or even an
  148. <acronym>XML</acronym> string containing these. Also provided are methods to extract
  149. the current <classname>DOMXPath</classname> object (with all core
  150. and Extension namespaces registered) and the correct prefix used in
  151. all XPath queries for the current Feed or Entry. The basic methods
  152. to use (on any object) are <methodname>saveXml()</methodname>,
  153. <methodname>getDomDocument()</methodname>,
  154. <methodname>getElement()</methodname>,
  155. <methodname>getXpath()</methodname> and
  156. <methodname>getXpathPrefix()</methodname>. These will let you break
  157. free of <classname>Zend_Feed_Reader</classname> and do whatever else
  158. you want.
  159. </para>
  160. <itemizedlist>
  161. <listitem>
  162. <para>
  163. <methodname>saveXml()</methodname> returns an <acronym>XML</acronym> string
  164. containing only the element representing the current object.
  165. </para>
  166. </listitem>
  167. <listitem>
  168. <para>
  169. <methodname>getDomDocument()</methodname> returns the
  170. <classname>DOMDocument</classname> object representing the
  171. entire feed (even if called from an Entry object).
  172. </para>
  173. </listitem>
  174. <listitem>
  175. <para>
  176. <methodname>getElement()</methodname> returns the
  177. <classname>DOMElement</classname> of the current object
  178. (i.e. the Feed or current Entry).
  179. </para>
  180. </listitem>
  181. <listitem>
  182. <para>
  183. <methodname>getXpath()</methodname> returns the
  184. <classname>DOMXPath</classname> object for the current feed
  185. (even if called from an Entry object) with the namespaces of
  186. the current feed type and all loaded Extensions
  187. pre-registered.
  188. </para>
  189. </listitem>
  190. <listitem>
  191. <para>
  192. <methodname>getXpathPrefix()</methodname> returns the query
  193. prefix for the current object (i.e. the Feed or current
  194. Entry) which includes the correct XPath query path for that
  195. specific Feed or Entry.
  196. </para>
  197. </listitem>
  198. </itemizedlist>
  199. <para>
  200. Here's an example where a feed might include an <acronym>RSS</acronym> Extension not
  201. supported by <classname>Zend_Feed_Reader</classname> out of the box.
  202. Notably, you could write and register an Extension (covered later)
  203. to do this, but that's not always warranted for a quick check. You
  204. must register any new namespaces on the
  205. <classname>DOMXPath</classname> object before use unless they are
  206. registered by <classname>Zend_Feed_Reader</classname> or an
  207. Extension beforehand.
  208. </para>
  209. <programlisting language="php"><![CDATA[
  210. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  211. $xpathPrefix = $feed->getXpathPrefix();
  212. $xpath = $feed->getXpath();
  213. $xpath->registerNamespace('admin', 'http://webns.net/mvcb/');
  214. $reportErrorsTo = $xpath->evaluate('string('
  215. . $xpathPrefix
  216. . '/admin:errorReportsTo)');
  217. ]]></programlisting>
  218. <warning>
  219. <para>
  220. If you register an already registered namespace with a different
  221. prefix name to that used internally by
  222. <classname>Zend_Feed_Reader</classname>, it will break the
  223. internal operation of this component.
  224. </para>
  225. </warning>
  226. </sect2>
  227. <sect2 id="zend.feed.reader.cache-request">
  228. <title>Cache Support and Intelligent Requests</title>
  229. <sect3 id="zend.feed.reader.cache-request.cache">
  230. <title>Adding Cache Support to Zend_Feed_Reader</title>
  231. <para>
  232. <classname>Zend_Feed_Reader</classname> supports using an
  233. instance of <classname>Zend_Cache</classname> to cache feeds (as
  234. <acronym>XML</acronym>) to avoid unnecessary network requests. Adding a cache is as
  235. simple here as it is for other Zend Framework components, create
  236. and configure your cache and then tell
  237. <classname>Zend_Feed_Reader</classname> to use it! The cache key
  238. used is "<classname>Zend_Feed_Reader_</classname>" followed by the
  239. <acronym>MD5</acronym> hash of the feed's <acronym>URI</acronym>.
  240. </para>
  241. <programlisting language="php"><![CDATA[
  242. $frontendOptions = array(
  243. 'lifetime' => 7200,
  244. 'automatic_serialization' => true
  245. );
  246. $backendOptions = array('cache_dir' => './tmp/');
  247. $cache = Zend_Cache::factory(
  248. 'Core', 'File', $frontendOptions, $backendOptions
  249. );
  250. Zend_Feed_Reader::setCache($cache);
  251. ]]></programlisting>
  252. <note>
  253. <para>
  254. While it's a little off track, you should also consider
  255. adding a cache to
  256. <classname>Zend_Loader_PluginLoader</classname> which is
  257. used by <classname>Zend_Feed_Reader</classname> to load
  258. Extensions.
  259. </para>
  260. </note>
  261. </sect3>
  262. <sect3 id="zend.feed.reader.cache-request.http-conditional-get">
  263. <title>HTTP Conditional GET Support</title>
  264. <para>
  265. The big question often asked when importing a feed frequently, is
  266. if it has even changed. With a cache enabled, you can add <acronym>HTTP</acronym>
  267. Conditional GET support to your arsenal to answer that question.
  268. </para>
  269. <para>
  270. Using this method, you can request feeds from <acronym>URI</acronym>s and include
  271. their last known ETag and Last-Modified response header values
  272. with the request (using the If-None-Match and If-Modified-Since
  273. headers). If the feed on the server remains unchanged, you
  274. should receive a 304 response which tells
  275. <classname>Zend_Feed_Reader</classname> to use the cached
  276. version. If a full feed is sent in a response with a status code
  277. of 200, this means the feed has changed and
  278. <classname>Zend_Feed_Reader</classname> will parse the new
  279. version and save it to the cache. It will also cache the new
  280. ETag and Last-Modified header values for future use.
  281. </para>
  282. <para>
  283. These "conditional" requests are not guaranteed to be supported
  284. by the server you request a <acronym>URI</acronym> of, but can be attempted
  285. regardless. Most common feed sources like blogs should however
  286. have this supported. To enable conditional requests, you will
  287. need to provide a cache to <classname>Zend_Feed_Reader</classname>.
  288. </para>
  289. <programlisting language="php"><![CDATA[
  290. $frontendOptions = array(
  291. 'lifetime' => 86400,
  292. 'automatic_serialization' => true
  293. );
  294. $backendOptions = array('cache_dir' => './tmp/');
  295. $cache = Zend_Cache::factory(
  296. 'Core', 'File', $frontendOptions, $backendOptions
  297. );
  298. Zend_Feed_Reader::setCache($cache);
  299. Zend_Feed_Reader::useHttpConditionalGet();
  300. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  301. ]]></programlisting>
  302. <para>
  303. In the example above, with <acronym>HTTP</acronym> Conditional GET requests enabled,
  304. the response header values for ETag and Last-Modified will be cached
  305. along with the feed. For the next 24hrs (the cache lifetime), feeds will
  306. only be updated on the cache if a non-304 response is received
  307. containing a valid <acronym>RSS</acronym> or Atom <acronym>XML</acronym> document.
  308. </para>
  309. <para>
  310. If you intend on managing request headers from outside
  311. <classname>Zend_Feed_Reader</classname>, you can set the
  312. relevant If-None-Matches and If-Modified-Since request headers
  313. via the <acronym>URI</acronym> import method.
  314. </para>
  315. <programlisting language="php"><![CDATA[
  316. $lastEtagReceived = '5e6cefe7df5a7e95c8b1ba1a2ccaff3d';
  317. $lastModifiedDateReceived = 'Wed, 08 Jul 2009 13:37:22 GMT';
  318. $feed = Zend_Feed_Reader::import(
  319. $uri, $lastEtagReceived, $lastModifiedDateReceived
  320. );
  321. ]]></programlisting>
  322. </sect3>
  323. </sect2>
  324. <sect2 id="zend.feed.reader.locate">
  325. <title>Locating Feed URIs from Websites</title>
  326. <para>
  327. These days, many websites are aware that the location of their <acronym>XML</acronym>
  328. feeds is not always obvious. A small <acronym>RDF</acronym>, <acronym>RSS</acronym> or
  329. Atom graphic helps when the user is reading the page, but what about when a machine
  330. visits trying to identify where your feeds are located? To assist in
  331. this, websites may point to their feeds using &lt;link&gt; tags in
  332. the &lt;head&gt; section of their <acronym>HTML</acronym>. To take advantage of this,
  333. you can use <classname>Zend_Feed_Reader</classname> to locate these
  334. feeds using the static <methodname>findFeedLinks()</methodname>
  335. method.
  336. </para>
  337. <para>
  338. This method calls any <acronym>URI</acronym> and searches for the location of
  339. <acronym>RSS</acronym>, <acronym>RDF</acronym>
  340. and Atom feeds assuming the website's <acronym>HTML</acronym> contains the relevant
  341. links. It then returns a value object where you can check for the existence of a
  342. <acronym>RSS</acronym>, <acronym>RDF</acronym> or Atom feed <acronym>URI</acronym>.
  343. </para>
  344. <para>
  345. The returned object is an <classname>ArrayObject</classname>
  346. called <classname>Zend_Feed_Reader_FeedSet</classname> so you can cast
  347. it to an array, or iterate over it, to access all the detected links.
  348. However, as a simple shortcut, you can just grab the first RSS, RDF
  349. or Atom link using its public properties as in the example below.
  350. </para>
  351. <programlisting language="php"><![CDATA[
  352. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  353. if(isset($links->rdf)) {
  354. echo $links->rdf, "\n"; // http://www.planet-php.org/rdf/
  355. }
  356. if(isset($links->rss)) {
  357. echo $links->rss, "\n"; // http://www.planet-php.org/rss/
  358. }
  359. if(isset($links->atom)) {
  360. echo $links->atom, "\n"; // http://www.planet-php.org/atom/
  361. }
  362. ]]></programlisting>
  363. <para>
  364. Based on these links, you can then import from whichever source you
  365. wish in the usual manner.
  366. </para>
  367. <para>
  368. This quick method only gives you one link for each feed type, but
  369. websites may indicate many links of any type. Perhaps it's a news
  370. site with a RSS feed for each news category. You can iterate over
  371. all links using the ArrayObject's iterator.
  372. </para>
  373. <programlisting language="php"><![CDATA[
  374. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  375. foreach ($links as $link) {
  376. echo $link['href'], "\n";
  377. }
  378. ]]></programlisting>
  379. <para>The available keys are <emphasis>href</emphasis>, <emphasis>rel</emphasis>
  380. which will always be 'alternate', <emphasis>type</emphasis> which will
  381. be one of <code>application/rss+xml</code>, <code>application/rdf+xml</code>
  382. or <code>application/atom+xml</code> and <emphasis>feed</emphasis>.
  383. <emphasis>feed</emphasis> is only available if you preserve the
  384. <classname>ArrayObject</classname> (i.e. do not cast it to an array)
  385. and using it triggers an attempt to load the feed into a
  386. <classname>Zend_Feed_Reader_FeedAbstract</classname> instance. This
  387. is a lazy loaded attempt - feeds are never loaded until you try to
  388. access them using this method.</para>
  389. </sect2>
  390. <sect2 id="zend.feed.reader.retrieve-info">
  391. <title>Retrieving Feed Information</title>
  392. <para>
  393. Retrieving information from a feed (we'll cover entries/items in the
  394. next section though they follow identical principals) uses a clearly
  395. defined <acronym>API</acronym> which is exactly the same regardless of whether the feed
  396. in question is <acronym>RSS</acronym>/<acronym>RDF</acronym>/Atom. The same goes for
  397. sub-versions of these standards and we've tested every single
  398. <acronym>RSS</acronym> and Atom version. While
  399. the underlying feed <acronym>XML</acronym> can differ substantially in terms of the
  400. tags and elements they present, they nonetheless are all trying to
  401. convey similar information and to reflect this all the differences
  402. and wrangling over alternative tags are handled internally by
  403. <classname>Zend_Feed_Reader</classname> presenting you with an
  404. identical interface for each. Ideally, you should not have to care
  405. whether a feed is <acronym>RSS</acronym> or Atom so long as you can extract the
  406. information you want.
  407. </para>
  408. <para>
  409. Of course, we don't live in an ideal world so there may be times the
  410. <acronym>API</acronym> just does not cover what you're looking for. To assist you,
  411. <classname>Zend_Feed_Reader</classname> offers a plugin system which
  412. allows you to write Extensions to expand the core <acronym>API</acronym> and cover any
  413. additional data you are trying to extract from feeds. If writing
  414. another Extension is too much trouble, you can simply grab the
  415. underlying <acronym>DOM</acronym> or XPath objects and do it by hand in your
  416. application. Of course, we really do encourage writing an Extension
  417. simply to make it more portable and reusable.
  418. </para>
  419. <para>
  420. Here's a summary of the Core <acronym>API</acronym> for Feeds. You should note it
  421. comprises not only the basic <acronym>RSS</acronym> and Atom standards, but also
  422. accounts for a number of included Extensions bundled with
  423. <classname>Zend_Feed_Reader</classname>. The naming of these
  424. Extension sourced methods remain fairly generic - all Extension
  425. methods operate at the same level as the Core <acronym>API</acronym> though we do allow
  426. you to retrieve any specific Extension object separately if
  427. required.
  428. </para>
  429. <table>
  430. <title>Feed Level API Methods</title>
  431. <tgroup cols="2">
  432. <tbody>
  433. <row>
  434. <entry><methodname>getId()</methodname></entry>
  435. <entry>Returns a unique ID associated with this feed</entry>
  436. </row>
  437. <row>
  438. <entry><methodname>getTitle()</methodname></entry>
  439. <entry>Returns the title of the feed</entry>
  440. </row>
  441. <row>
  442. <entry><methodname>getDescription()</methodname></entry>
  443. <entry>Returns the text description of the feed</entry>
  444. </row>
  445. <row>
  446. <entry><methodname>getLink()</methodname></entry>
  447. <entry>
  448. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
  449. containing the same or
  450. similar information as this feed (i.e. if the feed is from a blog,
  451. it should provide the blog's <acronym>URI</acronym> where the
  452. <acronym>HTML</acronym> version of the entries can be read).
  453. </entry>
  454. </row>
  455. <row>
  456. <entry><methodname>getFeedLink()</methodname></entry>
  457. <entry>
  458. Returns the <acronym>URI</acronym> of this feed, which should be the
  459. same as the <acronym>URI</acronym> used to import the feed
  460. </entry>
  461. </row>
  462. <row>
  463. <entry><methodname>getAuthors()</methodname></entry>
  464. <entry>
  465. Returns an array of all authors associated with this feed
  466. including email address in the author string if available
  467. </entry>
  468. </row>
  469. <row>
  470. <entry><methodname>getAuthor(integer $index = 0)</methodname></entry>
  471. <entry>
  472. Returns either the first author known, or with the
  473. optional <varname>$index</varname> parameter any specific
  474. index on the array of Authors (returning null if an
  475. invalid index).
  476. </entry>
  477. </row>
  478. <row>
  479. <entry><methodname>getDateCreated()</methodname></entry>
  480. <entry>
  481. Returns the date on which this feed was created. Generally
  482. only applicable to Atom where it represents the date the resource
  483. described by an Atom 1.0 document was created.
  484. </entry>
  485. </row>
  486. <row>
  487. <entry><methodname>getDateModified()</methodname></entry>
  488. <entry>
  489. Returns the date on which this feed was last modified
  490. </entry>
  491. </row>
  492. <row>
  493. <entry><methodname>getLanguage()</methodname></entry>
  494. <entry>
  495. Returns the language of the feed (if defined) or simply the
  496. language noted in the <acronym>XML</acronym> document
  497. </entry>
  498. </row>
  499. <row>
  500. <entry><methodname>getGenerator()</methodname></entry>
  501. <entry>
  502. Returns the generator of the feed, e.g. the software which
  503. generated it. This may differ between <acronym>RSS</acronym> and Atom
  504. since Atom defines a different notation.
  505. </entry>
  506. </row>
  507. <row>
  508. <entry><methodname>getCopyright()</methodname></entry>
  509. <entry>
  510. Returns any copyright notice associated with the feed
  511. </entry>
  512. </row>
  513. <row>
  514. <entry><methodname>getHubs()</methodname></entry>
  515. <entry>
  516. Returns an array of all Hub Server <acronym>URI</acronym> endpoints which
  517. are advertised by the feed for using with the Pubsubhubbub
  518. Protocol, allowing subscriptions to the feed for real-time updates.
  519. </entry>
  520. </row>
  521. </tbody>
  522. </tgroup>
  523. </table>
  524. <para>
  525. Given the variety of feeds in the wild, some of these methods will
  526. undoubtedly return <constant>NULL</constant> indicating the relevant information
  527. couldn't be located. Where possible, <classname>Zend_Feed_Reader</classname>
  528. will fall back on alternative elements during its search. For
  529. example, searching an <acronym>RSS</acronym> feed for a modification date is more
  530. complicated than it looks. <acronym>RSS</acronym> 2.0 feeds should include a
  531. <code>&lt;lastBuildDate&gt;</code> tag and/or a
  532. <code>&lt;pubDate&gt;</code> element. But what if it doesn't, maybe
  533. this is an <acronym>RSS</acronym> 1.0 feed? Perhaps it instead has an
  534. <code>&lt;atom:updated&gt;</code> element with identical information
  535. (Atom may be used to supplement <acronym>RSS</acronym>'s syntax)? Failing that, we
  536. could simply look at the entries, pick the most recent, and use its
  537. <code>&lt;pubDate&gt;</code> element. Assuming it exists... Many
  538. feeds also use Dublin Core 1.0/1.1 <code>&lt;dc:date&gt;</code>
  539. elements for feeds/entries. Or we could find Atom lurking again.
  540. </para>
  541. <para>
  542. The point is, <classname>Zend_Feed_Reader</classname> was designed
  543. to know this. When you ask for the modification date (or anything
  544. else), it will run off and search for all these alternatives until
  545. it either gives up and returns <constant>NULL</constant>, or finds an
  546. alternative that should have the right answer.
  547. </para>
  548. <para>
  549. In addition to the above methods, all Feed objects implement methods
  550. for retrieving the <acronym>DOM</acronym> and XPath objects for the current feeds as
  551. described earlier. Feed objects also implement the <acronym>SPL</acronym> Iterator and
  552. Countable interfaces. The extended <acronym>API</acronym> is summarised below.
  553. </para>
  554. <table>
  555. <title>Extended Feed Level API Methods</title>
  556. <tgroup cols="2">
  557. <tbody>
  558. <row>
  559. <entry><methodname>getDomDocument()</methodname></entry>
  560. <entry>
  561. Returns the parent
  562. <classname>DOMDocument</classname> object for the
  563. entire source <acronym>XML</acronym> document
  564. </entry>
  565. </row>
  566. <row>
  567. <entry><methodname>getElement()</methodname></entry>
  568. <entry>
  569. Returns the current feed level
  570. <classname>DOMElement</classname> object
  571. </entry>
  572. </row>
  573. <row>
  574. <entry><methodname>saveXml()</methodname></entry>
  575. <entry>
  576. Returns a string containing an <acronym>XML</acronym> document of the
  577. entire feed element (this is not the original
  578. document but a rebuilt version)
  579. </entry>
  580. </row>
  581. <row>
  582. <entry><methodname>getXpath()</methodname></entry>
  583. <entry>
  584. Returns the <classname>DOMXPath</classname> object
  585. used internally to run queries on the
  586. <classname>DOMDocument</classname> object (this
  587. includes core and Extension namespaces
  588. pre-registered)
  589. </entry>
  590. </row>
  591. <row>
  592. <entry><methodname>getXpathPrefix()</methodname></entry>
  593. <entry>
  594. Returns the valid <acronym>DOM</acronym> path prefix prepended
  595. to all XPath queries matching the feed being queried
  596. </entry>
  597. </row>
  598. <row>
  599. <entry><methodname>getEncoding()</methodname></entry>
  600. <entry>
  601. Returns the encoding of the source <acronym>XML</acronym> document
  602. (note: this cannot account for errors such as the
  603. server sending documents in a different encoding)
  604. </entry>
  605. </row>
  606. <row>
  607. <entry><methodname>count()</methodname></entry>
  608. <entry>
  609. Returns a count of the entries or items this feed contains
  610. (implements <acronym>SPL</acronym> <classname>Countable</classname>
  611. interface)
  612. </entry>
  613. </row>
  614. <row>
  615. <entry><methodname>current()</methodname></entry>
  616. <entry>
  617. Returns either the current entry (using the current index
  618. from <methodname>key()</methodname>)
  619. </entry>
  620. </row>
  621. <row>
  622. <entry><methodname>key()</methodname></entry>
  623. <entry>Returns the current entry index</entry>
  624. </row>
  625. <row>
  626. <entry><methodname>next()</methodname></entry>
  627. <entry>Increments the entry index value by one</entry>
  628. </row>
  629. <row>
  630. <entry><methodname>rewind()</methodname></entry>
  631. <entry>Resets the entry index to 0</entry>
  632. </row>
  633. <row>
  634. <entry><methodname>valid()</methodname></entry>
  635. <entry>
  636. Checks that the current entry index is valid, i.e.
  637. it does fall below 0 and does not exceed the number
  638. of entries existing.
  639. </entry>
  640. </row>
  641. <row>
  642. <entry><methodname>getExtensions()</methodname></entry>
  643. <entry>
  644. Returns an array of all Extension objects loaded for
  645. the current feed (note: both feed-level and entry-level Extensions
  646. exist, and only feed-level Extensions are returned here).
  647. The array keys are of the form {ExtensionName}_Feed.
  648. </entry>
  649. </row>
  650. <row>
  651. <entry><methodname>getExtension(string $name)</methodname></entry>
  652. <entry>
  653. Returns an Extension object for the feed registered under the
  654. provided name. This allows more fine-grained access to
  655. Extensions which may otherwise be hidden within the implementation
  656. of the standard <acronym>API</acronym> methods.
  657. </entry>
  658. </row>
  659. <row>
  660. <entry><methodname>getType()</methodname></entry>
  661. <entry>
  662. Returns a static class constant (e.g.
  663. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  664. i.e. Atom 0.3) indicating exactly what kind of feed
  665. is being consumed.
  666. </entry>
  667. </row>
  668. </tbody>
  669. </tgroup>
  670. </table>
  671. </sect2>
  672. <sect2 id="zend.feed.reader.entry">
  673. <title>Retrieving Entry/Item Information</title>
  674. <para>
  675. Retrieving information for specific entries or items (depending on
  676. whether you speak Atom or <acronym>RSS</acronym>) is identical to feed level data.
  677. Accessing entries is simply a matter of iterating over a Feed object
  678. or using the <acronym>SPL</acronym> <classname>Iterator</classname> interface Feed
  679. objects implement and calling the appropriate method on each.
  680. </para>
  681. <table>
  682. <title>Entry Level API Methods</title>
  683. <tgroup cols="2">
  684. <tbody>
  685. <row>
  686. <entry><methodname>getId()</methodname></entry>
  687. <entry>Returns a unique ID for the current entry</entry>
  688. </row>
  689. <row>
  690. <entry><methodname>getTitle()</methodname></entry>
  691. <entry>Returns the title of the current entry</entry>
  692. </row>
  693. <row>
  694. <entry><methodname>getDescription()</methodname></entry>
  695. <entry>Returns a description of the current entry</entry>
  696. </row>
  697. <row>
  698. <entry><methodname>getLink()</methodname></entry>
  699. <entry>
  700. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> version
  701. of the current entry
  702. </entry>
  703. </row>
  704. <row>
  705. <entry><methodname>getPermaLink()</methodname></entry>
  706. <entry>
  707. Returns the permanent link to the current entry
  708. </entry>
  709. </row>
  710. <row>
  711. <entry><methodname>getAuthors()</methodname></entry>
  712. <entry>
  713. Returns an array of all authors associated with this entry
  714. including email address in the author string if available
  715. </entry>
  716. </row>
  717. <row>
  718. <entry><methodname>getAuthor($index = 0)</methodname></entry>
  719. <entry>
  720. Returns either the first author known, or with the
  721. optional <varname>$index</varname> parameter any specific
  722. index on the array of Authors (returning null if an
  723. invalid index).
  724. </entry>
  725. </row>
  726. <row>
  727. <entry><methodname>getDateCreated()</methodname></entry>
  728. <entry>
  729. Returns the date on which the current entry was
  730. created. Generally only applicable to Atom where it
  731. represents the date the resource described by an
  732. Atom 1.0 document was created.
  733. </entry>
  734. </row>
  735. <row>
  736. <entry><methodname>getDateModified()</methodname></entry>
  737. <entry>
  738. Returns the date on which the current entry was last
  739. modified
  740. </entry>
  741. </row>
  742. <row>
  743. <entry><methodname>getContent()</methodname></entry>
  744. <entry>
  745. Returns the content of the current entry (this has any
  746. entities reversed if possible assuming the content type is
  747. <acronym>HTML</acronym>). The description is returned if a
  748. separate content element does not exist.
  749. </entry>
  750. </row>
  751. <row>
  752. <entry><methodname>getEnclosure()</methodname></entry>
  753. <entry>
  754. Returns an array containing the value of all
  755. attributes from a multi-media &lt;enclosure&gt; element including
  756. as array keys: <emphasis>url</emphasis>,
  757. <emphasis>length</emphasis>, <emphasis>type</emphasis>.
  758. </entry>
  759. </row>
  760. <row>
  761. <entry><methodname>getCommentCount()</methodname></entry>
  762. <entry>
  763. Returns the number of comments made on this entry at the
  764. time the feed was last generated
  765. </entry>
  766. </row>
  767. <row>
  768. <entry><methodname>getCommentLink()</methodname></entry>
  769. <entry>
  770. Returns a <acronym>URI</acronym> pointing to the <acronym>HTML</acronym>
  771. page where comments can be made on this entry
  772. </entry>
  773. </row>
  774. <row>
  775. <entry>
  776. <methodname>getCommentFeedLink(string $type =
  777. 'atom'|'rss')</methodname>
  778. </entry>
  779. <entry>
  780. Returns a <acronym>URI</acronym> pointing to a feed of the provided type
  781. containing all comments for this entry (type defaults to
  782. Atom/<acronym>RSS</acronym> depending on current feed type).
  783. </entry>
  784. </row>
  785. </tbody>
  786. </tgroup>
  787. </table>
  788. <para>
  789. The extended <acronym>API</acronym> for entries is identical to that for feeds with the
  790. exception of the Iterator methods which are not needed here.
  791. </para>
  792. <caution>
  793. <para>
  794. There is often confusion over the concepts of modified and
  795. created dates. In Atom, these are two clearly defined concepts
  796. (so knock yourself out) but in <acronym>RSS</acronym> they are vague.
  797. <acronym>RSS</acronym> 2.0
  798. defines a single <emphasis>&lt;pubDate&gt;</emphasis> element
  799. which typically refers to the date this entry was published,
  800. i.e. a creation date of sorts. This is not always the case, and
  801. it may change with updates or not. As a result, if you really
  802. want to check whether an entry has changed, don't rely on the
  803. results of <methodname>getDateModified()</methodname>. Instead,
  804. consider tracking the <acronym>MD5</acronym> hash of three other elements
  805. concatenated, e.g. using <methodname>getTitle()</methodname>,
  806. <methodname>getDescription()</methodname> and
  807. <methodname>getContent()</methodname>. If the entry was trully
  808. updated, this hash computation will give a different result than
  809. previously saved hashes for the same entry. Further muddying the
  810. waters, dates in feeds may follow different standards. Atom and
  811. Dublin Core dates should follow <acronym>ISO</acronym> 8601,
  812. and <acronym>RSS</acronym> dates should
  813. follow <acronym>RFC</acronym> 822 or <acronym>RFC</acronym> 2822
  814. which is also common. Date methods
  815. will throw an exception if <classname>Zend_Date</classname>
  816. cannot load the date string using one of the above standards.
  817. </para>
  818. </caution>
  819. <warning>
  820. <para>
  821. The values returned from these methods are not validated. This
  822. means users must perform validation on all retrieved data
  823. including the filtering of any <acronym>HTML</acronym> such as from
  824. <methodname>getContent()</methodname> before it is output from
  825. your application. Remember that most feeds come from external
  826. sources, and therefore the default assumption should be that
  827. they cannot be trusted.
  828. </para>
  829. </warning>
  830. <table>
  831. <title>Extended Entry Level API Methods</title>
  832. <tgroup cols="2">
  833. <tbody>
  834. <row>
  835. <entry><methodname>getDomDocument()</methodname></entry>
  836. <entry>
  837. Returns the parent
  838. <classname>DOMDocument</classname> object for the
  839. entire feed (not just the current entry)
  840. </entry>
  841. </row>
  842. <row>
  843. <entry><methodname>getElement()</methodname></entry>
  844. <entry>
  845. Returns the current entry level
  846. <classname>DOMElement</classname> object
  847. </entry>
  848. </row>
  849. <row>
  850. <entry><methodname>getXpath()</methodname></entry>
  851. <entry>
  852. Returns the <classname>DOMXPath</classname> object
  853. used internally to run queries on the
  854. <classname>DOMDocument</classname> object (this
  855. includes core and Extension namespaces
  856. pre-registered)
  857. </entry>
  858. </row>
  859. <row>
  860. <entry><methodname>getXpathPrefix()</methodname></entry>
  861. <entry>
  862. Returns the valid <acronym>DOM</acronym> path prefix prepended
  863. to all XPath queries matching the entry being queried
  864. </entry>
  865. </row>
  866. <row>
  867. <entry><methodname>getEncoding()</methodname></entry>
  868. <entry>
  869. Returns the encoding of the source <acronym>XML</acronym> document
  870. (note: this cannot account for errors such as the server sending
  871. documents in a different encoding)
  872. </entry>
  873. </row>
  874. <row>
  875. <entry><methodname>getExtensions()</methodname></entry>
  876. <entry>
  877. Returns an array of all Extension objects loaded for
  878. the current entry (note: both feed-level and entry-level
  879. Extensions exist, and only entry-level Extensions are returned
  880. here). The array keys are in the form {ExtensionName}_Entry.
  881. </entry>
  882. </row>
  883. <row>
  884. <entry><methodname>getExtension(string $name)</methodname></entry>
  885. <entry>
  886. Returns an Extension object for the entry registered under the
  887. provided name. This allows more fine-grained access to
  888. Extensions which may otherwise be hidden within the implementation
  889. of the standard <acronym>API</acronym> methods.
  890. </entry>
  891. </row>
  892. <row>
  893. <entry><methodname>getType()</methodname></entry>
  894. <entry>
  895. Returns a static class constant (e.g.
  896. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  897. i.e. Atom 0.3) indicating exactly what kind
  898. of feed is being consumed.
  899. </entry>
  900. </row>
  901. </tbody>
  902. </tgroup>
  903. </table>
  904. </sect2>
  905. <sect2 id="zend.feed.reader.extending">
  906. <title>Extending Feed and Entry APIs</title>
  907. <para>
  908. Extending <classname>Zend_Feed_Reader</classname> allows you to add
  909. methods at both the feed and entry level which cover the retrieval
  910. of information not already supported by
  911. <classname>Zend_Feed_Reader</classname>. Given the number of
  912. <acronym>RSS</acronym> and
  913. Atom extensions that exist, this is a good thing since
  914. <classname>Zend_Feed_Reader</classname> couldn't possibly add
  915. everything.
  916. </para>
  917. <para>
  918. There are two types of Extensions possible, those which retrieve
  919. information from elements which are immediate children of the root
  920. element (e.g. <code>&lt;channel&gt;</code> for <acronym>RSS</acronym> or
  921. <code>&lt;feed&gt;</code> for Atom) and those who retrieve
  922. information from child elements of an entry (e.g.
  923. <code>&lt;item&gt;</code> for <acronym>RSS</acronym> or <code>&lt;entry&gt;</code> for
  924. Atom). On the filesystem these are grouped as classes within
  925. a namespace based on the extension standard's name. For
  926. example, internally we have
  927. <classname>Zend_Feed_Reader_Extension_DublinCore_Feed</classname>
  928. and <classname>Zend_Feed_Reader_Extension_DublinCore_Entry</classname>
  929. classes which are two Extensions implementing Dublin Core
  930. 1.0/1.1 support.
  931. </para>
  932. <para>
  933. Extensions are loaded into <classname>Zend_Feed_Reader</classname>
  934. using <classname>Zend_Loader_PluginLoader</classname>, so their operation
  935. will be familiar from other Zend Framework components.
  936. <classname>Zend_Feed_Reader</classname> already bundles a number of
  937. these Extensions, however those which are not used internally and
  938. registered by default (so called Core Extensions) must be registered
  939. to <classname>Zend_Feed_Reader</classname> before they are used. The
  940. bundled Extensions include:
  941. </para>
  942. <table>
  943. <title>Core Extensions (pre-registered)</title>
  944. <tgroup cols="2">
  945. <tbody>
  946. <row>
  947. <entry>DublinCore (Feed and Entry)</entry>
  948. <entry>Implements support for Dublin Core Metadata Element Set 1.0
  949. and 1.1 </entry>
  950. </row>
  951. <row>
  952. <entry>Content (Entry only)</entry>
  953. <entry>Implements support for Content 1.0</entry>
  954. </row>
  955. <row>
  956. <entry>Atom (Feed and Entry)</entry>
  957. <entry>Implements support for Atom 0.3 and Atom 1.0</entry>
  958. </row>
  959. <row>
  960. <entry>Slash</entry>
  961. <entry>
  962. Implements support for the Slash <acronym>RSS</acronym> 1.0 module
  963. </entry>
  964. </row>
  965. <row>
  966. <entry>WellFormedWeb</entry>
  967. <entry>Implements support for the Well Formed Web CommentAPI 1.0</entry>
  968. </row>
  969. <row>
  970. <entry>Thread</entry>
  971. <entry>Implements support for Atom Threading Extensions as described
  972. in <acronym>RFC</acronym> 4685</entry>
  973. </row>
  974. <row>
  975. <entry>Podcast</entry>
  976. <entry>
  977. Implements support for the Podcast 1.0 <constant>DTD</constant> from
  978. Apple
  979. </entry>
  980. </row>
  981. </tbody>
  982. </tgroup>
  983. </table>
  984. <para>
  985. The Core Extensions are somewhat special since they are extremely
  986. common and multi-faceted. For example, we have a Core Extension for Atom.
  987. Atom is implemented as an Extension (not just a base class) because it
  988. doubles as a valid <acronym>RSS</acronym> module - you can insert
  989. Atom elements into <acronym>RSS</acronym> feeds. I've even seen
  990. <acronym>RDF</acronym> feeds which use a lot of Atom in place of more
  991. common Extensions like Dublin Core.
  992. </para>
  993. <table>
  994. <title>Non-Core Extensions (must register manually)</title>
  995. <tgroup cols="2">
  996. <tbody>
  997. <row>
  998. <entry>Syndication</entry>
  999. <entry>
  1000. Implements Syndication 1.0 support for <acronym>RSS</acronym> feeds
  1001. </entry>
  1002. </row>
  1003. <row>
  1004. <entry>CreativeCommons</entry>
  1005. <entry>
  1006. A <acronym>RSS</acronym> module that adds an element at the
  1007. &lt;channel&gt; or &lt;item&gt; level that specifies which Creative
  1008. Commons license applies.
  1009. </entry>
  1010. </row>
  1011. </tbody>
  1012. </tgroup>
  1013. </table>
  1014. <para>
  1015. The additional non-Core Extensions are offered but not registered to
  1016. <classname>Zend_Feed_Reader</classname> by default. If you want to
  1017. use them, you'll need to tell
  1018. <classname>Zend_Feed_Reader</classname> to load them in advance of
  1019. importing a feed. Additional non-Core Extensions will be included
  1020. in future iterations of the component.
  1021. </para>
  1022. <para>
  1023. Registering an Extension with
  1024. <classname>Zend_Feed_Reader</classname>, so it is loaded and its <acronym>API</acronym>
  1025. is available to Feed and Entry objects, is a simple affair using the
  1026. <classname>Zend_Loader_PluginLoader</classname>. Here we register
  1027. the optional Slash Extension, and discover that it can be directly
  1028. called from the Entry level <acronym>API</acronym> without any effort. Note that
  1029. Extension names are case sensitive and use camel casing for multiple
  1030. terms.
  1031. </para>
  1032. <programlisting language="php"><![CDATA[
  1033. Zend_Feed_Reader::registerExtension('Syndication');
  1034. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  1035. $updatePeriod = $feed->current()->getUpdatePeriod();
  1036. ]]></programlisting>
  1037. <para>
  1038. In the simple example above, we checked how frequently a feed is being updated
  1039. using the <methodname>getUpdatePeriod()</methodname>
  1040. method. Since it's not part of
  1041. <classname>Zend_Feed_Reader</classname>'s core <acronym>API</acronym>, it could only be
  1042. a method supported by the newly registered Syndication Extension.
  1043. </para>
  1044. <para>
  1045. As you can also notice, the new methods from Extensions are accessible from the main
  1046. <acronym>API</acronym> using <acronym>PHP</acronym>'s magic methods. As an alternative,
  1047. you can also directly access any Extension object for a similar result as seen below.
  1048. </para>
  1049. <programlisting language="php"><![CDATA[
  1050. Zend_Feed_Reader::registerExtension('Syndication');
  1051. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  1052. $syndication = $feed->getExtension('Syndication');
  1053. $updatePeriod = $syndication->getUpdatePeriod();
  1054. ]]></programlisting>
  1055. <sect3 id="zend.feed.reader.extending.feed">
  1056. <title>Writing Zend_Feed_Reader Extensions</title>
  1057. <para>
  1058. Inevitably, there will be times when the
  1059. <classname>Zend_Feed_Reader</classname> <acronym>API</acronym> is just not capable
  1060. of getting something you need from a feed or entry. You can use
  1061. the underlying source objects, like
  1062. <classname>DOMDocument</classname>, to get these by hand however
  1063. there is a more reusable method available by writing Extensions
  1064. supporting these new queries.
  1065. </para>
  1066. <para>
  1067. As an example, let's take the case of a purely fictitious
  1068. corporation named Jungle Books. Jungle Books have been
  1069. publishing a lot of reviews on books they sell (from external
  1070. sources and customers), which are distributed as an <acronym>RSS</acronym> 2.0
  1071. feed. Their marketing department realises that web applications
  1072. using this feed cannot currently figure out exactly what book is
  1073. being reviewed. To make life easier for everyone, they determine
  1074. that the geek department needs to extend <acronym>RSS</acronym> 2.0 to include a
  1075. new element per entry supplying the <acronym>ISBN</acronym>-10 or
  1076. <acronym>ISBN</acronym>-13 number of
  1077. the publication the entry concerns. They define the new
  1078. <code>&lt;isbn&gt;</code> element quite simply with a standard
  1079. name and namespace <acronym>URI</acronym>:
  1080. </para>
  1081. <programlisting language="php"><![CDATA[
  1082. JungleBooks 1.0:
  1083. http://example.com/junglebooks/rss/module/1.0/
  1084. ]]></programlisting>
  1085. <para>
  1086. A snippet of <acronym>RSS</acronym> containing this extension in practice could be
  1087. something similar to:
  1088. </para>
  1089. <programlisting language="php"><![CDATA[
  1090. <?xml version="1.0" encoding="utf-8" ?>
  1091. <rss version="2.0"
  1092. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  1093. xmlns:jungle="http://example.com/junglebooks/rss/module/1.0/">
  1094. <channel>
  1095. <title>Jungle Books Customer Reviews</title>
  1096. <link>http://example.com/junglebooks</link>
  1097. <description>Many book reviews!</description>
  1098. <pubDate>Fri, 26 Jun 2009 19:15:10 GMT</pubDate>
  1099. <jungle:dayPopular>http://example.com/junglebooks/book/938</jungle:dayPopular>
  1100. <item>
  1101. <title>Review Of Flatland: A Romance of Many Dimensions</title>
  1102. <link>http://example.com/junglebooks/review/987</link>
  1103. <author>Confused Physics Student</author>
  1104. <content:encoded>
  1105. A romantic square?!
  1106. </content:encoded>
  1107. <pubDate>Thu, 25 Jun 2009 20:03:28 -0700</pubDate>
  1108. <jungle:isbn>048627263X</jungle:isbn>
  1109. </item>
  1110. </channel>
  1111. </rss>
  1112. ]]></programlisting>
  1113. <para>
  1114. Implementing this new <acronym>ISBN</acronym> element as a simple entry level
  1115. extension would require the following class (using your own class
  1116. namespace outside of Zend).
  1117. </para>
  1118. <programlisting language="php"><![CDATA[
  1119. class My_FeedReader_Extension_JungleBooks_Entry
  1120. extends Zend_Feed_Reader_Extension_EntryAbstract
  1121. {
  1122. public function getIsbn()
  1123. {
  1124. if (isset($this->_data['isbn'])) {
  1125. return $this->_data['isbn'];
  1126. }
  1127. $isbn = $this->_xpath->evaluate(
  1128. 'string(' . $this->getXpathPrefix() . '/jungle:isbn)'
  1129. );
  1130. if (!$isbn) {
  1131. $isbn = null;
  1132. }
  1133. $this->_data['isbn'] = $isbn;
  1134. return $this->_data['isbn'];
  1135. }
  1136. protected function _registerNamespaces()
  1137. {
  1138. $this->_xpath->registerNamespace(
  1139. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1140. );
  1141. }
  1142. }
  1143. ]]></programlisting>
  1144. <para>
  1145. This extension is easy enough to follow. It creates a new method
  1146. <methodname>getIsbn()</methodname> which runs an XPath query on
  1147. the current entry to extract the <acronym>ISBN</acronym> number enclosed by the
  1148. <code>&lt;jungle:isbn&gt;</code> element. It can optionally
  1149. store this to the internal non-persistent cache (no need to keep
  1150. querying the <acronym>DOM</acronym> if it's called again on the same entry). The
  1151. value is returned to the caller. At the end we have a protected
  1152. method (it's abstract so it must exist) which registers the
  1153. Jungle Books namespace for their custom <acronym>RSS</acronym> module. While we
  1154. call this an <acronym>RSS</acronym> module, there's nothing to prevent the same
  1155. element being used in Atom feeds - and all Extensions which use
  1156. the prefix provided by <methodname>getXpathPrefix()</methodname>
  1157. are actually neutral and work on <acronym>RSS</acronym> or Atom feeds with no
  1158. extra code.
  1159. </para>
  1160. <para>
  1161. Since this Extension is stored outside of Zend Framework, you'll
  1162. need to register the path prefix for your Extensions so
  1163. <classname>Zend_Loader_PluginLoader</classname> can find them.
  1164. After that, it's merely a matter of registering the Extension,
  1165. if it's not already loaded, and using it in practice.
  1166. </para>
  1167. <programlisting language="php"><![CDATA[
  1168. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1169. Zend_Feed_Reader::addPrefixPath(
  1170. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1171. );
  1172. Zend_Feed_Reader::registerExtension('JungleBooks');
  1173. }
  1174. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1175. // ISBN for whatever book the first entry in the feed was concerned with
  1176. $firstIsbn = $feed->current()->getIsbn();
  1177. ]]></programlisting>
  1178. <para>
  1179. Writing a feed level Extension is not much different. The
  1180. example feed from earlier included an unmentioned
  1181. <code>&lt;jungle:dayPopular&gt;</code> element which Jungle
  1182. Books have added to their standard to include a link to the
  1183. day's most popular book (in terms of visitor traffic). Here's
  1184. an Extension which adds a
  1185. <methodname>getDaysPopularBookLink()</methodname> method to the
  1186. feel level <acronym>API</acronym>.
  1187. </para>
  1188. <programlisting language="php"><![CDATA[
  1189. class My_FeedReader_Extension_JungleBooks_Feed
  1190. extends Zend_Feed_Reader_Extension_FeedAbstract
  1191. {
  1192. public function getDaysPopularBookLink()
  1193. {
  1194. if (isset($this->_data['dayPopular'])) {
  1195. return $this->_data['dayPopular'];
  1196. }
  1197. $dayPopular = $this->_xpath->evaluate(
  1198. 'string(' . $this->getXpathPrefix() . '/jungle:dayPopular)'
  1199. );
  1200. if (!$dayPopular) {
  1201. $dayPopular = null;
  1202. }
  1203. $this->_data['dayPopular'] = $dayPopular;
  1204. return $this->_data['dayPopular'];
  1205. }
  1206. protected function _registerNamespaces()
  1207. {
  1208. $this->_xpath->registerNamespace(
  1209. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1210. );
  1211. }
  1212. }
  1213. ]]></programlisting>
  1214. <para>
  1215. Let's repeat the last example using a custom Extension to show the
  1216. method being used.
  1217. </para>
  1218. <programlisting language="php"><![CDATA[
  1219. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1220. Zend_Feed_Reader::addPrefixPath(
  1221. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1222. );
  1223. Zend_Feed_Reader::registerExtension('JungleBooks');
  1224. }
  1225. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1226. // URI to the information page of the day's most popular book with visitors
  1227. $daysPopularBookLink = $feed->getDaysPopularBookLink();
  1228. // ISBN for whatever book the first entry in the feed was concerned with
  1229. $firstIsbn = $feed->current()->getIsbn();
  1230. ]]></programlisting>
  1231. <para>
  1232. Going through these examples, you'll note that we don't register
  1233. feed and entry Extensions separately. Extensions within the same
  1234. standard may or may not include both a feed and entry class, so
  1235. <classname>Zend_Feed_Reader</classname> only requires you to
  1236. register the overall parent name, e.g. JungleBooks, DublinCore,
  1237. Slash. Internally, it can check at what level Extensions exist
  1238. and load them up if found. In our case, we have a full set of
  1239. Extensions now: <classname>JungleBooks_Feed</classname> and
  1240. <classname>JungleBooks_Entry</classname>.
  1241. </para>
  1242. </sect3>
  1243. </sect2>
  1244. </sect1>