Zend_Feed_Reader.xml 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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 wlebsite'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. <programlisting language="php"><![CDATA[
  345. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  346. if(isset($links->rdf)) {
  347. echo $links->rdf, "\n"; // http://www.planet-php.org/rdf/
  348. }
  349. if(isset($links->rss)) {
  350. echo $links->rss, "\n"; // http://www.planet-php.org/rss/
  351. }
  352. if(isset($links->atom)) {
  353. echo $links->atom, "\n"; // http://www.planet-php.org/atom/
  354. }
  355. ]]></programlisting>
  356. <para>
  357. Based on these links, you can then import from whichever source you
  358. wish in the usual manner.
  359. </para>
  360. </sect2>
  361. <sect2 id="zend.feed.reader.retrieve-info">
  362. <title>Retrieving Feed Information</title>
  363. <para>
  364. Retrieving information from a feed (we'll cover entries/items in the
  365. next section though they follow identical principals) uses a clearly
  366. defined <acronym>API</acronym> which is exactly the same regardless of whether the feed
  367. in question is <acronym>RSS</acronym>/<acronym>RDF</acronym>/Atom. The same goes for
  368. sub-versions of these standards and we've tested every single
  369. <acronym>RSS</acronym> and Atom version. While
  370. the underlying feed <acronym>XML</acronym> can differ substantially in terms of the
  371. tags and elements they present, they nonetheless are all trying to
  372. convey similar information and to reflect this all the differences
  373. and wrangling over alternative tags are handled internally by
  374. <classname>Zend_Feed_Reader</classname> presenting you with an
  375. identical interface for each. Ideally, you should not have to care
  376. whether a feed is <acronym>RSS</acronym> or Atom so long as you can extract the
  377. information you want.
  378. </para>
  379. <para>
  380. Of course, we don't live in an ideal world so there may be times the
  381. <acronym>API</acronym> just does not cover what you're looking for. To assist you,
  382. <classname>Zend_Feed_Reader</classname> offers a plugin system which
  383. allows you to write Extensions to expand the core <acronym>API</acronym> and cover any
  384. additional data you are trying to extract from feeds. If writing
  385. another Extension is too much trouble, you can simply grab the
  386. underlying <acronym>DOM</acronym> or XPath objects and do it by hand in your
  387. application. Of course, we really do encourage writing an Extension
  388. simply to make it more portable and reusable.
  389. </para>
  390. <para>
  391. Here's a summary of the Core <acronym>API</acronym> for Feeds. You should note it
  392. comprises not only the basic <acronym>RSS</acronym> and Atom standards, but also
  393. accounts for a number of included Extensions bundled with
  394. <classname>Zend_Feed_Reader</classname>. The naming of these
  395. Extension sourced methods remain fairly generic - all Extension
  396. methods operate at the same level as the Core <acronym>API</acronym> though we do allow
  397. you to retrieve any specific Extension object separately if
  398. required.
  399. </para>
  400. <table>
  401. <title>Feed Level API Methods</title>
  402. <tgroup cols="2">
  403. <tbody>
  404. <row>
  405. <entry><methodname>getId()</methodname></entry>
  406. <entry>Returns a unique ID associated with this feed</entry>
  407. </row>
  408. <row>
  409. <entry><methodname>getTitle()</methodname></entry>
  410. <entry>Returns the title of the feed</entry>
  411. </row>
  412. <row>
  413. <entry><methodname>getDescription()</methodname></entry>
  414. <entry>Returns the text description of the feed</entry>
  415. </row>
  416. <row>
  417. <entry><methodname>getLink()</methodname></entry>
  418. <entry>
  419. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
  420. containing the same or
  421. similar information as this feed (i.e. if the feed is from a blog,
  422. it should provide the blog's <acronym>URI</acronym> where the
  423. <acronym>HTML</acronym> version of the entries can be read).
  424. </entry>
  425. </row>
  426. <row>
  427. <entry><methodname>getFeedLink()</methodname></entry>
  428. <entry>
  429. Returns the <acronym>URI</acronym> of this feed, which should be the
  430. same as the <acronym>URI</acronym> used to import the feed
  431. </entry>
  432. </row>
  433. <row>
  434. <entry><methodname>getAuthors()</methodname></entry>
  435. <entry>
  436. Returns an array of all authors associated with this feed
  437. including email address in the author string if available
  438. </entry>
  439. </row>
  440. <row>
  441. <entry><methodname>getAuthor(integer $index = 0)</methodname></entry>
  442. <entry>
  443. Returns either the first author known, or with the
  444. optional <varname>$index</varname> parameter any specific
  445. index on the array of Authors (returning null if an
  446. invalid index).
  447. </entry>
  448. </row>
  449. <row>
  450. <entry><methodname>getDateCreated()</methodname></entry>
  451. <entry>
  452. Returns the date on which this feed was created. Generally
  453. only applicable to Atom where it represents the date the resource
  454. described by an Atom 1.0 document was created.
  455. </entry>
  456. </row>
  457. <row>
  458. <entry><methodname>getDateModified()</methodname></entry>
  459. <entry>
  460. Returns the date on which this feed was last modified
  461. </entry>
  462. </row>
  463. <row>
  464. <entry><methodname>getLanguage()</methodname></entry>
  465. <entry>
  466. Returns the language of the feed (if defined) or simply the
  467. language noted in the <acronym>XML</acronym> document
  468. </entry>
  469. </row>
  470. <row>
  471. <entry><methodname>getGenerator()</methodname></entry>
  472. <entry>
  473. Returns the generator of the feed, e.g. the software which
  474. generated it. This may differ between <acronym>RSS</acronym> and Atom
  475. since Atom defines a different notation.
  476. </entry>
  477. </row>
  478. <row>
  479. <entry><methodname>getCopyright()</methodname></entry>
  480. <entry>
  481. Returns any copyright notice associated with the feed
  482. </entry>
  483. </row>
  484. </tbody>
  485. </tgroup>
  486. </table>
  487. <para>
  488. Given the variety of feeds in the wild, some of these methods will
  489. undoubtedly return <constant>NULL</constant> indicating the relevant information
  490. couldn't be located. Where possible, <classname>Zend_Feed_Reader</classname>
  491. will fall back on alternative elements during its search. For
  492. example, searching an <acronym>RSS</acronym> feed for a modification date is more
  493. complicated than it looks. <acronym>RSS</acronym> 2.0 feeds should include a
  494. <code>&lt;lastBuildDate&gt;</code> tag and/or a
  495. <code>&lt;pubDate&gt;</code> element. But what if it doesn't, maybe
  496. this is an <acronym>RSS</acronym> 1.0 feed? Perhaps it instead has an
  497. <code>&lt;atom:updated&gt;</code> element with identical information
  498. (Atom may be used to supplement <acronym>RSS</acronym>'s syntax)? Failing that, we
  499. could simply look at the entries, pick the most recent, and use its
  500. <code>&lt;pubDate&gt;</code> element. Assuming it exists... Many
  501. feeds also use Dublin Core 1.0/1.1 <code>&lt;dc:date&gt;</code>
  502. elements for feeds/entries. Or we could find Atom lurking again.
  503. </para>
  504. <para>
  505. The point is, <classname>Zend_Feed_Reader</classname> was designed
  506. to know this. When you ask for the modification date (or anything
  507. else), it will run off and search for all these alternatives until
  508. it either gives up and returns <constant>NULL</constant>, or finds an
  509. alternative that should have the right answer.
  510. </para>
  511. <para>
  512. In addition to the above methods, all Feed objects implement methods
  513. for retrieving the <acronym>DOM</acronym> and XPath objects for the current feeds as
  514. described earlier. Feed objects also implement the <acronym>SPL</acronym> Iterator and
  515. Countable interfaces. The extended <acronym>API</acronym> is summarised below.
  516. </para>
  517. <table>
  518. <title>Extended Feed Level API Methods</title>
  519. <tgroup cols="2">
  520. <tbody>
  521. <row>
  522. <entry><methodname>getDomDocument()</methodname></entry>
  523. <entry>
  524. Returns the parent
  525. <classname>DOMDocument</classname> object for the
  526. entire source <acronym>XML</acronym> document
  527. </entry>
  528. </row>
  529. <row>
  530. <entry><methodname>getElement()</methodname></entry>
  531. <entry>
  532. Returns the current feed level
  533. <classname>DOMElement</classname> object
  534. </entry>
  535. </row>
  536. <row>
  537. <entry><methodname>saveXml()</methodname></entry>
  538. <entry>
  539. Returns a string containing an <acronym>XML</acronym> document of the
  540. entire feed element (this is not the original
  541. document but a rebuilt version)
  542. </entry>
  543. </row>
  544. <row>
  545. <entry><methodname>getXpath()</methodname></entry>
  546. <entry>
  547. Returns the <classname>DOMXPath</classname> object
  548. used internally to run queries on the
  549. <classname>DOMDocument</classname> object (this
  550. includes core and Extension namespaces
  551. pre-registered)
  552. </entry>
  553. </row>
  554. <row>
  555. <entry><methodname>getXpathPrefix()</methodname></entry>
  556. <entry>
  557. Returns the valid <acronym>DOM</acronym> path prefix prepended
  558. to all XPath queries matching the feed being queried
  559. </entry>
  560. </row>
  561. <row>
  562. <entry><methodname>getEncoding()</methodname></entry>
  563. <entry>
  564. Returns the encoding of the source <acronym>XML</acronym> document
  565. (note: this cannot account for errors such as the
  566. server sending documents in a different encoding)
  567. </entry>
  568. </row>
  569. <row>
  570. <entry><methodname>count()</methodname></entry>
  571. <entry>
  572. Returns a count of the entries or items this feed contains
  573. (implements <acronym>SPL</acronym> <classname>Countable</classname>
  574. interface)
  575. </entry>
  576. </row>
  577. <row>
  578. <entry><methodname>current()</methodname></entry>
  579. <entry>
  580. Returns either the current entry (using the current index
  581. from <methodname>key()</methodname>)
  582. </entry>
  583. </row>
  584. <row>
  585. <entry><methodname>key()</methodname></entry>
  586. <entry>Returns the current entry index</entry>
  587. </row>
  588. <row>
  589. <entry><methodname>next()</methodname></entry>
  590. <entry>Increments the entry index value by one</entry>
  591. </row>
  592. <row>
  593. <entry><methodname>rewind()</methodname></entry>
  594. <entry>Resets the entry index to 0</entry>
  595. </row>
  596. <row>
  597. <entry><methodname>valid()</methodname></entry>
  598. <entry>
  599. Checks that the current entry index is valid, i.e.
  600. it does fall below 0 and does not exceed the number
  601. of entries existing.
  602. </entry>
  603. </row>
  604. <row>
  605. <entry><methodname>getExtensions()</methodname></entry>
  606. <entry>
  607. Returns an array of all Extension objects loaded for
  608. the current feed (note: both feed-level and entry-level Extensions
  609. exist, and only feed-level Extensions are returned here).
  610. The array keys are of the form {ExtensionName}_Feed.
  611. </entry>
  612. </row>
  613. <row>
  614. <entry><methodname>getExtension(string $name)</methodname></entry>
  615. <entry>
  616. Returns an Extension object for the feed registered under the
  617. provided name. This allows more fine-grained access to
  618. Extensions which may otherwise be hidden within the implementation
  619. of the standard <acronym>API</acronym> methods.
  620. </entry>
  621. </row>
  622. <row>
  623. <entry><methodname>getType()</methodname></entry>
  624. <entry>
  625. Returns a static class constant (e.g.
  626. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  627. i.e. Atom 0.3) indicating exactly what kind of feed
  628. is being consumed.
  629. </entry>
  630. </row>
  631. </tbody>
  632. </tgroup>
  633. </table>
  634. </sect2>
  635. <sect2 id="zend.feed.reader.entry">
  636. <title>Retrieving Entry/Item Information</title>
  637. <para>
  638. Retrieving information for specific entries or items (depending on
  639. whether you speak Atom or <acronym>RSS</acronym>) is identical to feed level data.
  640. Accessing entries is simply a matter of iterating over a Feed object
  641. or using the <acronym>SPL</acronym> <classname>Iterator</classname> interface Feed
  642. objects implement and calling the appropriate method on each.
  643. </para>
  644. <table>
  645. <title>Entry Level API Methods</title>
  646. <tgroup cols="2">
  647. <tbody>
  648. <row>
  649. <entry><methodname>getId()</methodname></entry>
  650. <entry>Returns a unique ID for the current entry</entry>
  651. </row>
  652. <row>
  653. <entry><methodname>getTitle()</methodname></entry>
  654. <entry>Returns the title of the current entry</entry>
  655. </row>
  656. <row>
  657. <entry><methodname>getDescription()</methodname></entry>
  658. <entry>Returns a description of the current entry</entry>
  659. </row>
  660. <row>
  661. <entry><methodname>getLink()</methodname></entry>
  662. <entry>
  663. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> version
  664. of the current entry
  665. </entry>
  666. </row>
  667. <row>
  668. <entry><methodname>getPermaLink()</methodname></entry>
  669. <entry>
  670. Returns the permanent link to the current entry
  671. </entry>
  672. </row>
  673. <row>
  674. <entry><methodname>getAuthors()</methodname></entry>
  675. <entry>
  676. Returns an array of all authors associated with this entry
  677. including email address in the author string if available
  678. </entry>
  679. </row>
  680. <row>
  681. <entry><methodname>getAuthor($index = 0)</methodname></entry>
  682. <entry>
  683. Returns either the first author known, or with the
  684. optional <varname>$index</varname> parameter any specific
  685. index on the array of Authors (returning null if an
  686. invalid index).
  687. </entry>
  688. </row>
  689. <row>
  690. <entry><methodname>getDateCreated()</methodname></entry>
  691. <entry>
  692. Returns the date on which the current entry was
  693. created. Generally only applicable to Atom where it
  694. represents the date the resource described by an
  695. Atom 1.0 document was created.
  696. </entry>
  697. </row>
  698. <row>
  699. <entry><methodname>getDateModified()</methodname></entry>
  700. <entry>
  701. Returns the date on which the current entry was last
  702. modified
  703. </entry>
  704. </row>
  705. <row>
  706. <entry><methodname>getContent()</methodname></entry>
  707. <entry>
  708. Returns the content of the current entry (this has any
  709. entities reversed if possible assuming the content type is
  710. <acronym>HTML</acronym>). The description is returned if a
  711. separate content element does not exist.
  712. </entry>
  713. </row>
  714. <row>
  715. <entry><methodname>getCommentCount()</methodname></entry>
  716. <entry>
  717. Returns the number of comments made on this entry at the
  718. time the feed was last generated
  719. </entry>
  720. </row>
  721. <row>
  722. <entry><methodname>getCommentLink()</methodname></entry>
  723. <entry>
  724. Returns a <acronym>URI</acronym> pointing to the <acronym>HTML</acronym>
  725. page where comments can be made on this entry
  726. </entry>
  727. </row>
  728. <row>
  729. <entry>
  730. <methodname>getCommentFeedLink(string $type =
  731. 'atom'|'rss')</methodname>
  732. </entry>
  733. <entry>
  734. Returns a <acronym>URI</acronym> pointing to a feed of the provided type
  735. containing all comments for this entry (type defaults to
  736. Atom/<acronym>RSS</acronym> depending on current feed type).
  737. </entry>
  738. </row>
  739. </tbody>
  740. </tgroup>
  741. </table>
  742. <para>
  743. The extended <acronym>API</acronym> for entries is identical to that for feeds with the
  744. exception of the Iterator methods which are not needed here.
  745. </para>
  746. <caution>
  747. <para>
  748. There is often confusion over the concepts of modified and
  749. created dates. In Atom, these are two clearly defined concepts
  750. (so knock yourself out) but in <acronym>RSS</acronym> they are vague.
  751. <acronym>RSS</acronym> 2.0
  752. defines a single <emphasis>&lt;pubDate&gt;</emphasis> element
  753. which typically refers to the date this entry was published,
  754. i.e. a creation date of sorts. This is not always the case, and
  755. it may change with updates or not. As a result, if you really
  756. want to check whether an entry has changed, don't rely on the
  757. results of <methodname>getDateModified()</methodname>. Instead,
  758. consider tracking the <acronym>MD5</acronym> hash of three other elements
  759. concatenated, e.g. using <methodname>getTitle()</methodname>,
  760. <methodname>getDescription()</methodname> and
  761. <methodname>getContent()</methodname>. If the entry was trully
  762. updated, this hash computation will give a different result than
  763. previously saved hashes for the same entry. Further muddying the
  764. waters, dates in feeds may follow different standards. Atom and
  765. Dublin Core dates should follow <acronym>ISO</acronym> 8601,
  766. and <acronym>RSS</acronym> dates should
  767. follow <acronym>RFC</acronym> 822 or <acronym>RFC</acronym> 2822
  768. which is also common. Date methods
  769. will throw an exception if <classname>Zend_Date</classname>
  770. cannot load the date string using one of the above standards.
  771. </para>
  772. </caution>
  773. <warning>
  774. <para>
  775. The values returned from these methods are not validated. This
  776. means users must perform validation on all retrieved data
  777. including the filtering of any <acronym>HTML</acronym> such as from
  778. <methodname>getContent()</methodname> before it is output from
  779. your application. Remember that most feeds come from external
  780. sources, and therefore the default assumption should be that
  781. they cannot be trusted.
  782. </para>
  783. </warning>
  784. <table>
  785. <title>Extended Entry Level API Methods</title>
  786. <tgroup cols="2">
  787. <tbody>
  788. <row>
  789. <entry><methodname>getDomDocument()</methodname></entry>
  790. <entry>
  791. Returns the parent
  792. <classname>DOMDocument</classname> object for the
  793. entire feed (not just the current entry)
  794. </entry>
  795. </row>
  796. <row>
  797. <entry><methodname>getElement()</methodname></entry>
  798. <entry>
  799. Returns the current entry level
  800. <classname>DOMElement</classname> object
  801. </entry>
  802. </row>
  803. <row>
  804. <entry><methodname>getXpath()</methodname></entry>
  805. <entry>
  806. Returns the <classname>DOMXPath</classname> object
  807. used internally to run queries on the
  808. <classname>DOMDocument</classname> object (this
  809. includes core and Extension namespaces
  810. pre-registered)
  811. </entry>
  812. </row>
  813. <row>
  814. <entry><methodname>getXpathPrefix()</methodname></entry>
  815. <entry>
  816. Returns the valid <acronym>DOM</acronym> path prefix prepended
  817. to all XPath queries matching the entry being queried
  818. </entry>
  819. </row>
  820. <row>
  821. <entry><methodname>getEncoding()</methodname></entry>
  822. <entry>
  823. Returns the encoding of the source <acronym>XML</acronym> document
  824. (note: this cannot account for errors such as the server sending
  825. documents in a different encoding)
  826. </entry>
  827. </row>
  828. <row>
  829. <entry><methodname>getExtensions()</methodname></entry>
  830. <entry>
  831. Returns an array of all Extension objects loaded for
  832. the current entry (note: both feed-level and entry-level
  833. Extensions exist, and only entry-level Extensions are returned
  834. here). The array keys are in the form {ExtensionName}_Entry.
  835. </entry>
  836. </row>
  837. <row>
  838. <entry><methodname>getExtension(string $name)</methodname></entry>
  839. <entry>
  840. Returns an Extension object for the entry registered under the
  841. provided name. This allows more fine-grained access to
  842. Extensions which may otherwise be hidden within the implementation
  843. of the standard <acronym>API</acronym> methods.
  844. </entry>
  845. </row>
  846. <row>
  847. <entry><methodname>getType()</methodname></entry>
  848. <entry>
  849. Returns a static class constant (e.g.
  850. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  851. i.e. Atom 0.3) indicating exactly what kind
  852. of feed is being consumed.
  853. </entry>
  854. </row>
  855. </tbody>
  856. </tgroup>
  857. </table>
  858. </sect2>
  859. <sect2 id="zend.feed.reader.extending">
  860. <title>Extending Feed and Entry APIs</title>
  861. <para>
  862. Extending <classname>Zend_Feed_Reader</classname> allows you to add
  863. methods at both the feed and entry level which cover the retrieval
  864. of information not already supported by
  865. <classname>Zend_Feed_Reader</classname>. Given the number of
  866. <acronym>RSS</acronym> and
  867. Atom extensions that exist, this is a good thing since
  868. <classname>Zend_Feed_Reader</classname> couldn't possibly add
  869. everything.
  870. </para>
  871. <para>
  872. There are two types of Extensions possible, those which retrieve
  873. information from elements which are immediate children of the root
  874. element (e.g. <code>&lt;channel&gt;</code> for <acronym>RSS</acronym> or
  875. <code>&lt;feed&gt;</code> for Atom) and those who retrieve
  876. information from child elements of an entry (e.g.
  877. <code>&lt;item&gt;</code> for <acronym>RSS</acronym> or <code>&lt;entry&gt;</code> for
  878. Atom). On the filesystem these are grouped as classes within
  879. a namespace based on the extension standard's name. For
  880. example, internally we have
  881. <classname>Zend_Feed_Reader_Extension_DublinCore_Feed</classname>
  882. and <classname>Zend_Feed_Reader_Extension_DublinCore_Entry</classname>
  883. classes which are two Extensions implementing Dublin Core
  884. 1.0/1.1 support.
  885. </para>
  886. <para>
  887. Extensions are loaded into <classname>Zend_Feed_Reader</classname>
  888. using <classname>Zend_Loader_PluginLoader</classname>, so their operation
  889. will be familiar from other Zend Framework components.
  890. <classname>Zend_Feed_Reader</classname> already bundles a number of
  891. these Extensions, however those which are not used internally and
  892. registered by default (so called Core Extensions) must be registered
  893. to <classname>Zend_Feed_Reader</classname> before they are used. The
  894. bundled Extensions include:
  895. </para>
  896. <table>
  897. <title>Core Extensions (pre-registered)</title>
  898. <tgroup cols="2">
  899. <tbody>
  900. <row>
  901. <entry>DublinCore (Feed and Entry)</entry>
  902. <entry>Implements support for Dublin Core Metadata Element Set 1.0
  903. and 1.1 </entry>
  904. </row>
  905. <row>
  906. <entry>Content (Entry only)</entry>
  907. <entry>Implements support for Content 1.0</entry>
  908. </row>
  909. <row>
  910. <entry>Atom (Feed and Entry)</entry>
  911. <entry>Implements support for Atom 0.3 and Atom 1.0</entry>
  912. </row>
  913. <row>
  914. <entry>Slash</entry>
  915. <entry>
  916. Implements support for the Slash <acronym>RSS</acronym> 1.0 module
  917. </entry>
  918. </row>
  919. <row>
  920. <entry>WellFormedWeb</entry>
  921. <entry>Implements support for the Well Formed Web CommentAPI 1.0</entry>
  922. </row>
  923. <row>
  924. <entry>Thread</entry>
  925. <entry>Implements support for Atom Threading Extensions as described
  926. in <acronym>RFC</acronym> 4685</entry>
  927. </row>
  928. <row>
  929. <entry>Podcast</entry>
  930. <entry>
  931. Implements support for the Podcast 1.0 <constant>DTD</constant> from
  932. Apple
  933. </entry>
  934. </row>
  935. </tbody>
  936. </tgroup>
  937. </table>
  938. <para>
  939. The Core Extensions are somewhat special since they are extremely
  940. common and multi-faceted. For example, we have a Core Extension for Atom.
  941. Atom is implemented as an Extension (not just a base class) because it
  942. doubles as a valid <acronym>RSS</acronym> module - you can insert
  943. Atom elements into <acronym>RSS</acronym> feeds. I've even seen
  944. <acronym>RDF</acronym> feeds which use a lot of Atom in place of more
  945. common Extensions like Dublin Core.
  946. </para>
  947. <table>
  948. <title>Non-Core Extensions (must register manually)</title>
  949. <tgroup cols="2">
  950. <tbody>
  951. <row>
  952. <entry>Syndication</entry>
  953. <entry>
  954. Implements Syndication 1.0 support for <acronym>RSS</acronym> feeds
  955. </entry>
  956. </row>
  957. <row>
  958. <entry>CreativeCommons</entry>
  959. <entry>
  960. A <acronym>RSS</acronym> module that adds an element at the
  961. &lt;channel&gt; or &lt;item&gt; level that specifies which Creative
  962. Commons license applies.
  963. </entry>
  964. </row>
  965. </tbody>
  966. </tgroup>
  967. </table>
  968. <para>
  969. The additional non-Core Extensions are offered but not registered to
  970. <classname>Zend_Feed_Reader</classname> by default. If you want to
  971. use them, you'll need to tell
  972. <classname>Zend_Feed_Reader</classname> to load them in advance of
  973. importing a feed. Additional non-Core Extensions will be included
  974. in future iterations of the component.
  975. </para>
  976. <para>
  977. Registering an Extension with
  978. <classname>Zend_Feed_Reader</classname>, so it is loaded and its <acronym>API</acronym>
  979. is available to Feed and Entry objects, is a simple affair using the
  980. <classname>Zend_Loader_PluginLoader</classname>. Here we register
  981. the optional Slash Extension, and discover that it can be directly
  982. called from the Entry level <acronym>API</acronym> without any effort. Note that
  983. Extension names are case sensitive and use camel casing for multiple
  984. terms.
  985. </para>
  986. <programlisting language="php"><![CDATA[
  987. Zend_Feed_Reader::registerExtension('Syndication');
  988. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  989. $updatePeriod = $feed->current()->getUpdatePeriod();
  990. ]]></programlisting>
  991. <para>
  992. In the simple example above, we checked how frequently a feed is being updated
  993. using the <methodname>getUpdatePeriod()</methodname>
  994. method. Since it's not part of
  995. <classname>Zend_Feed_Reader</classname>'s core <acronym>API</acronym>, it could only be
  996. a method supported by the newly registered Syndication Extension.
  997. </para>
  998. <para>
  999. As you can also notice, the new methods from Extensions are accessible from the main
  1000. <acronym>API</acronym> using <acronym>PHP</acronym>'s magic methods. As an alternative,
  1001. you can also directly access any Extension object for a similar result as seen below.
  1002. </para>
  1003. <programlisting language="php"><![CDATA[
  1004. Zend_Feed_Reader::registerExtension('Syndication');
  1005. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  1006. $syndication = $feed->getExtension('Syndication');
  1007. $updatePeriod = $syndication->getUpdatePeriod();
  1008. ]]></programlisting>
  1009. <sect3 id="zend.feed.reader.extending.feed">
  1010. <title>Writing Zend_Feed_Reader Extensions</title>
  1011. <para>
  1012. Inevitably, there will be times when the
  1013. <classname>Zend_Feed_Reader</classname> <acronym>API</acronym> is just not capable
  1014. of getting something you need from a feed or entry. You can use
  1015. the underlying source objects, like
  1016. <classname>DOMDocument</classname>, to get these by hand however
  1017. there is a more reusable method available by writing Extensions
  1018. supporting these new queries.
  1019. </para>
  1020. <para>
  1021. As an example, let's take the case of a purely fictitious
  1022. corporation named Jungle Books. Jungle Books have been
  1023. publishing a lot of reviews on books they sell (from external
  1024. sources and customers), which are distributed as an <acronym>RSS</acronym> 2.0
  1025. feed. Their marketing department realises that web applications
  1026. using this feed cannot currently figure out exactly what book is
  1027. being reviewed. To make life easier for everyone, they determine
  1028. that the geek department needs to extend <acronym>RSS</acronym> 2.0 to include a
  1029. new element per entry supplying the <acronym>ISBN</acronym>-10 or
  1030. <acronym>ISBN</acronym>-13 number of
  1031. the publication the entry concerns. They define the new
  1032. <code>&lt;isbn&gt;</code> element quite simply with a standard
  1033. name and namespace <acronym>URI</acronym>:
  1034. </para>
  1035. <programlisting language="php"><![CDATA[
  1036. JungleBooks 1.0:
  1037. http://example.com/junglebooks/rss/module/1.0/
  1038. ]]></programlisting>
  1039. <para>
  1040. A snippet of <acronym>RSS</acronym> containing this extension in practice could be
  1041. something similar to:
  1042. </para>
  1043. <programlisting language="php"><![CDATA[
  1044. <?xml version="1.0" encoding="utf-8" ?>
  1045. <rss version="2.0"
  1046. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  1047. xmlns:jungle="http://example.com/junglebooks/rss/module/1.0/">
  1048. <channel>
  1049. <title>Jungle Books Customer Reviews</title>
  1050. <link>http://example.com/junglebooks</link>
  1051. <description>Many book reviews!</description>
  1052. <pubDate>Fri, 26 Jun 2009 19:15:10 GMT</pubDate>
  1053. <jungle:dayPopular>http://example.com/junglebooks/book/938</jungle:dayPopular>
  1054. <item>
  1055. <title>Review Of Flatland: A Romance of Many Dimensions</title>
  1056. <link>http://example.com/junglebooks/review/987</link>
  1057. <author>Confused Physics Student</author>
  1058. <content:encoded>
  1059. A romantic square?!
  1060. </content:encoded>
  1061. <pubDate>Thu, 25 Jun 2009 20:03:28 -0700</pubDate>
  1062. <jungle:isbn>048627263X</jungle:isbn>
  1063. </item>
  1064. </channel>
  1065. </rss>
  1066. ]]></programlisting>
  1067. <para>
  1068. Implementing this new <acronym>ISBN</acronym> element as a simple entry level
  1069. extension would require the following class (using your own class
  1070. namespace outside of Zend).
  1071. </para>
  1072. <programlisting language="php"><![CDATA[
  1073. class My_FeedReader_Extension_JungleBooks_Entry
  1074. extends Zend_Feed_Reader_Extension_EntryAbstract
  1075. {
  1076. public function getIsbn()
  1077. {
  1078. if (isset($this->_data['isbn'])) {
  1079. return $this->_data['isbn'];
  1080. }
  1081. $isbn = $this->_xpath->evaluate(
  1082. 'string(' . $this->getXpathPrefix() . '/jungle:isbn)'
  1083. );
  1084. if (!$isbn) {
  1085. $isbn = null;
  1086. }
  1087. $this->_data['isbn'] = $isbn;
  1088. return $this->_data['isbn'];
  1089. }
  1090. protected function _registerNamespaces()
  1091. {
  1092. $this->_xpath->registerNamespace(
  1093. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1094. );
  1095. }
  1096. }
  1097. ]]></programlisting>
  1098. <para>
  1099. This extension is easy enough to follow. It creates a new method
  1100. <methodname>getIsbn()</methodname> which runs an XPath query on
  1101. the current entry to extract the <acronym>ISBN</acronym> number enclosed by the
  1102. <code>&lt;jungle:isbn&gt;</code> element. It can optionally
  1103. store this to the internal non-persistent cache (no need to keep
  1104. querying the <acronym>DOM</acronym> if it's called again on the same entry). The
  1105. value is returned to the caller. At the end we have a protected
  1106. method (it's abstract so it must exist) which registers the
  1107. Jungle Books namespace for their custom <acronym>RSS</acronym> module. While we
  1108. call this an <acronym>RSS</acronym> module, there's nothing to prevent the same
  1109. element being used in Atom feeds - and all Extensions which use
  1110. the prefix provided by <methodname>getXpathPrefix()</methodname>
  1111. are actually neutral and work on <acronym>RSS</acronym> or Atom feeds with no
  1112. extra code.
  1113. </para>
  1114. <para>
  1115. Since this Extension is stored outside of Zend Framework, you'll
  1116. need to register the path prefix for your Extensions so
  1117. <classname>Zend_Loader_PluginLoader</classname> can find them.
  1118. After that, it's merely a matter of registering the Extension,
  1119. if it's not already loaded, and using it in practice.
  1120. </para>
  1121. <programlisting language="php"><![CDATA[
  1122. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1123. Zend_Feed_Reader::addPrefixPath(
  1124. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1125. );
  1126. Zend_Feed_Reader::registerExtension('JungleBooks');
  1127. }
  1128. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1129. // ISBN for whatever book the first entry in the feed was concerned with
  1130. $firstIsbn = $feed->current()->getIsbn();
  1131. ]]></programlisting>
  1132. <para>
  1133. Writing a feed level Extension is not much different. The
  1134. example feed from earlier included an unmentioned
  1135. <code>&lt;jungle:dayPopular&gt;</code> element which Jungle
  1136. Books have added to their standard to include a link to the
  1137. day's most popular book (in terms of visitor traffic). Here's
  1138. an Extension which adds a
  1139. <methodname>getDaysPopularBookLink()</methodname> method to the
  1140. feel level <acronym>API</acronym>.
  1141. </para>
  1142. <programlisting language="php"><![CDATA[
  1143. class My_FeedReader_Extension_JungleBooks_Feed
  1144. extends Zend_Feed_Reader_Extension_FeedAbstract
  1145. {
  1146. public function getDaysPopularBookLink()
  1147. {
  1148. if (isset($this->_data['dayPopular'])) {
  1149. return $this->_data['dayPopular'];
  1150. }
  1151. $dayPopular = $this->_xpath->evaluate(
  1152. 'string(' . $this->getXpathPrefix() . '/jungle:dayPopular)'
  1153. );
  1154. if (!$dayPopular) {
  1155. $dayPopular = null;
  1156. }
  1157. $this->_data['dayPopular'] = $dayPopular;
  1158. return $this->_data['dayPopular'];
  1159. }
  1160. protected function _registerNamespaces()
  1161. {
  1162. $this->_xpath->registerNamespace(
  1163. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1164. );
  1165. }
  1166. }
  1167. ]]></programlisting>
  1168. <para>
  1169. Let's repeat the last example using a custom Extension to show the
  1170. method being used.
  1171. </para>
  1172. <programlisting language="php"><![CDATA[
  1173. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1174. Zend_Feed_Reader::addPrefixPath(
  1175. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1176. );
  1177. Zend_Feed_Reader::registerExtension('JungleBooks');
  1178. }
  1179. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1180. // URI to the information page of the day's most popular book with visitors
  1181. $daysPopularBookLink = $feed->getDaysPopularBookLink();
  1182. // ISBN for whatever book the first entry in the feed was concerned with
  1183. $firstIsbn = $feed->current()->getIsbn();
  1184. ]]></programlisting>
  1185. <para>
  1186. Going through these examples, you'll note that we don't register
  1187. feed and entry Extensions separately. Extensions within the same
  1188. standard may or may not include both a feed and entry class, so
  1189. <classname>Zend_Feed_Reader</classname> only requires you to
  1190. register the overall parent name, e.g. JungleBooks, DublinCore,
  1191. Slash. Internally, it can check at what level Extensions exist
  1192. and load them up if found. In our case, we have a full set of
  1193. Extensions now: <classname>JungleBooks_Feed</classname> and
  1194. <classname>JungleBooks_Entry</classname>.
  1195. </para>
  1196. </sect3>
  1197. </sect2>
  1198. </sect1>