Zend_Feed_Reader.xml 74 KB

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