XML_RSS
[ class tree: XML_RSS ] [ index: XML_RSS ] [ all elements ]

Source for file RSS.php

Documentation is available at RSS.php

  1. <?php
  2. // vim: set expandtab tabstop=4 shiftwidth=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: RSS.php,v 1.28 2006/09/14 08:40:05 clay Exp $
  21. //
  22.  
  23. require_once 'XML/Parser.php';
  24.  
  25. /**
  26. * RSS parser class.
  27. *
  28. * This class is a parser for Resource Description Framework (RDF) Site
  29. * Summary (RSS) documents. For more information on RSS see the
  30. * website of the RSS working group (http://www.purl.org/rss/).
  31. *
  32. @author Martin Jansen <mj@php.net>
  33. @version $Revision: 1.28 $
  34. @access  public
  35. */
  36. class XML_RSS extends XML_Parser
  37. {
  38.     // {{{ properties
  39.  
  40.     /**
  41.      * @var string 
  42.      */
  43.     var $insideTag '';
  44.  
  45.     /**
  46.      * @var array 
  47.      */
  48.     var $insideTagStack = array();
  49.  
  50.     /**
  51.      * @var string 
  52.      */
  53.     var $activeTag '';
  54.  
  55.     /**
  56.      * @var array 
  57.      */
  58.     var $channel = array();
  59.  
  60.     /**
  61.      * @var array 
  62.      */
  63.     var $items = array();
  64.  
  65.     /**
  66.      * @var array 
  67.      */
  68.     var $item = array();
  69.  
  70.     /**
  71.      * @var array 
  72.      */
  73.     var $image = array();
  74.  
  75.     /**
  76.      * @var array 
  77.      */
  78.     var $textinput = array();
  79.  
  80.     /**
  81.      * @var array 
  82.      */
  83.     var $textinputs = array();
  84.  
  85.     /**
  86.      * @var array 
  87.      */
  88.     var $attribs;
  89.  
  90.     /**
  91.      * @var array 
  92.      */
  93.     var $parentTags = array('CHANNEL''ITEM''IMAGE''TEXTINPUT');
  94.  
  95.     /**
  96.      * @var array 
  97.      */
  98.     var $channelTags = array('TITLE''LINK''DESCRIPTION''IMAGE',
  99.                               'ITEMS''TEXTINPUT''LANGUAGE''COPYRIGHT',
  100.                               'MANAGINGEditor''WEBMASTER''PUBDATE''LASTBUILDDATE',
  101.                               'CATEGORY''GENERATOR''DOCS''CLOUD''TTL',
  102.                               'RATING');
  103.  
  104.     /**
  105.      * @var array 
  106.      */
  107.     var $itemTags = array('TITLE''LINK''DESCRIPTION''PUBDATE''AUTHOR''CATEGORY',
  108.                           'COMMENTS''ENCLOSURE''GUID''PUBDATE''SOURCE',
  109.                           'CONTENT:ENCODED');
  110.  
  111.     /**
  112.      * @var array 
  113.      */
  114.     var $imageTags = array('TITLE''URL''LINK''WIDTH''HEIGHT');
  115.  
  116.  
  117.     var $textinputTags = array('TITLE''DESCRIPTION''NAME''LINK');
  118.  
  119.     /**
  120.      * List of allowed module tags
  121.      *
  122.      * Currently supported:
  123.      *
  124.      *   Dublin Core Metadata
  125.      *   blogChannel RSS module
  126.      *   CreativeCommons
  127.      *   Content
  128.      *   Syndication
  129.      *   Trackback
  130.      *   GeoCoding
  131.      *   Media
  132.      *   iTunes
  133.      *
  134.      * @var array 
  135.      */
  136.     var $moduleTags = array('DC:TITLE''DC:CREATOR''DC:SUBJECT''DC:DESCRIPTION',
  137.                             'DC:PUBLISHER''DC:CONTRIBUTOR''DC:DATE''DC:TYPE',
  138.                             'DC:FORMAT''DC:IDENTIFIER''DC:SOURCE''DC:LANGUAGE',
  139.                             'DC:RELATION''DC:COVERAGE''DC:RIGHTS',
  140.                             'BLOGCHANNEL:BLOGROLL''BLOGCHANNEL:MYSUBSCRIPTIONS',
  141.                             'BLOGCHANNEL:BLINK''BLOGCHANNEL:CHANGES',
  142.                             'CREATIVECOMMONS:LICENSE''CC:LICENSE''CONTENT:ENCODED',
  143.                             'SY:UPDATEPERIOD''SY:UPDATEFREQUENCY''SY:UPDATEBASE',
  144.                             'TRACKBACK:PING''GEO:LAT''GEO:LONG',
  145.                             'MEDIA:GROUP''MEDIA:CONTENT''MEDIA:ADULT',
  146.                             'MEDIA:RATING''MEDIA:TITLE''MEDIA:DESCRIPTION',
  147.                             'MEDIA:KEYWORDS''MEDIA:THUMBNAIL''MEDIA:CATEGORY',
  148.                             'MEDIA:HASH''MEDIA:PLAYER''MEDIA:CREDIT',
  149.                             'MEDIA:COPYRIGHT''MEDIA:TEXT''MEDIA:RESTRICTION',
  150.                             'ITUNES:AUTHOR''ITUNES:BLOCK''ITUNES:CATEGORY',
  151.                             'ITUNES:DURATION''ITUNES:EXPLICIT''ITUNES:IMAGE',
  152.                             'ITUNES:KEYWORDS''ITUNES:NEW-FEED-URL''ITUNES:OWNER',
  153.                             'ITUNES:PUBDATE''ITUNES:SUBTITLE''ITUNES:SUMMARY'
  154.                             );
  155.  
  156.     /**
  157.      * @var array 
  158.      */
  159.     var $last = array();
  160.  
  161.     // }}}
  162.     // {{{ Constructor
  163.  
  164.     /**
  165.      * Constructor
  166.      *
  167.      * @access public
  168.      * @param mixed File pointer, name of the RSS file, or an RSS string.
  169.      * @param string  Source charset encoding, use null (default) to use
  170.      *                 default encoding (ISO-8859-1)
  171.      * @param string  Target charset encoding, use null (default) to use
  172.      *                 default encoding (ISO-8859-1)
  173.      * @return void 
  174.      */
  175.     function XML_RSS($handle ''$srcenc = null$tgtenc = null)
  176.     {
  177.         if ($srcenc === null && $tgtenc === null{
  178.             $this->XML_Parser();
  179.         else {
  180.             $this->XML_Parser($srcenc'event'$tgtenc);
  181.         }
  182.  
  183.         $this->setInput($handle);
  184.  
  185.         if ($handle == ''{
  186.             $this->raiseError('No input passed.');
  187.         }
  188.     }
  189.  
  190.     // }}}
  191.     // {{{ startHandler()
  192.  
  193.     /**
  194.      * Start element handler for XML parser
  195.      *
  196.      * @access private
  197.      * @param  object XML parser object
  198.      * @param  string XML element
  199.      * @param  array  Attributes of XML tag
  200.      * @return void 
  201.      */
  202.     function startHandler($parser$element$attribs)
  203.     {
  204.         if (substr($element04== "RSS:"{
  205.             $element substr($element4);
  206.         }
  207.  
  208.         switch ($element{
  209.             case 'CHANNEL':
  210.             case 'ITEM':
  211.             case 'IMAGE':
  212.             case 'TEXTINPUT':
  213.                 $this->insideTag $element;
  214.                 array_push($this->insideTagStack$element);
  215.                 break;
  216.  
  217.             case 'ENCLOSURE' :
  218.                 $this->attribs $attribs;
  219.                 break;
  220.  
  221.             default:
  222.                 $this->activeTag $element;
  223.         }
  224.     }
  225.  
  226.     // }}}
  227.     // {{{ endHandler()
  228.  
  229.     /**
  230.      * End element handler for XML parser
  231.      *
  232.      * If the end of <item>, <channel>, <image> or <textinput>
  233.      * is reached, this method updates the structure array
  234.      * $this->struct[] and adds the field "type" to this array,
  235.      * that defines the type of the current field.
  236.      *
  237.      * @access private
  238.      * @param  object XML parser object
  239.      * @param  string 
  240.      * @return void 
  241.      */
  242.     function endHandler($parser$element)
  243.     {
  244.         if (substr($element04== "RSS:"{
  245.             $element substr($element4);
  246.         }
  247.  
  248.         if ($element == $this->insideTag{
  249.             array_pop($this->insideTagStack);
  250.             $this->insideTag end($this->insideTagStack);
  251.  
  252.             $this->struct[array_merge(array('type' => strtolower($element)),
  253.                                           $this->last);
  254.         }
  255.  
  256.         if ($element == 'ITEM'{
  257.             $this->items[$this->item;
  258.             $this->item '';
  259.         }
  260.  
  261.         if ($element == 'IMAGE'{
  262.             $this->images[$this->image;
  263.             $this->image '';
  264.         }
  265.  
  266.         if ($element == 'TEXTINPUT'{
  267.             $this->textinputs $this->textinput;
  268.             $this->textinput '';
  269.         }
  270.  
  271.         if ($element == 'ENCLOSURE'{
  272.             if (!isset($this->item['enclosures'])) {
  273.                 $this->item['enclosures'= array();
  274.             }
  275.  
  276.             $this->item['enclosures'][array_change_key_case($this->attribsCASE_LOWER);
  277.             $this->attribs = array();
  278.         }
  279.  
  280.         $this->activeTag '';
  281.     }
  282.  
  283.     // }}}
  284.     // {{{ cdataHandler()
  285.  
  286.     /**
  287.      * Handler for character data
  288.      *
  289.      * @access private
  290.      * @param  object XML parser object
  291.      * @param  string CDATA
  292.      * @return void 
  293.      */
  294.     function cdataHandler($parser$cdata)
  295.     {
  296.         if (in_array($this->insideTag$this->parentTags)) {
  297.             $tagName strtolower($this->insideTag);
  298.             $var $this->{$tagName 'Tags'};
  299.  
  300.             if (in_array($this->activeTag$var||
  301.                 in_array($this->activeTag$this->moduleTags)) {
  302.                 $this->_add($tagNamestrtolower($this->activeTag),
  303.                             $cdata);
  304.             }
  305.  
  306.         }
  307.     }
  308.  
  309.     // }}}
  310.     // {{{ defaultHandler()
  311.  
  312.     /**
  313.      * Default handler for XML parser
  314.      *
  315.      * @access private
  316.      * @param  object XML parser object
  317.      * @param  string CDATA
  318.      * @return void 
  319.      */
  320.     function defaultHandler($parser$cdata)
  321.     {
  322.         return;
  323.     }
  324.  
  325.     // }}}
  326.     // {{{ _add()
  327.  
  328.     /**
  329.      * Add element to internal result sets
  330.      *
  331.      * @access private
  332.      * @param  string Name of the result set
  333.      * @param  string Fieldname
  334.      * @param  string Value
  335.      * @return void 
  336.      * @see    cdataHandler
  337.      */
  338.     function _add($type$field$value)
  339.     {
  340.         if (empty($this->{$type}|| empty($this->{$type}[$field])) {
  341.             $this->{$type}[$field$value;
  342.         else {
  343.             $this->{$type}[$field.= $value;
  344.         }
  345.  
  346.         $this->last = $this->{$type};
  347.     }
  348.  
  349.     // }}}
  350.     // {{{ getStructure()
  351.  
  352.     /**
  353.      * Get complete structure of RSS file
  354.      *
  355.      * @access public
  356.      * @return array 
  357.      */
  358.     function getStructure()
  359.     {
  360.         return (array)$this->struct;
  361.     }
  362.  
  363.     // }}}
  364.     // {{{ getchannelInfo()
  365.  
  366.     /**
  367.      * Get general information about current channel
  368.      *
  369.      * This method returns an array containing the information
  370.      * that has been extracted from the <channel>-tag while parsing
  371.      * the RSS file.
  372.      *
  373.      * @access public
  374.      * @return array 
  375.      */
  376.     function getChannelInfo()
  377.     {
  378.         return (array)$this->channel;
  379.     }
  380.  
  381.     // }}}
  382.     // {{{ getItems()
  383.  
  384.     /**
  385.      * Get items from RSS file
  386.      *
  387.      * This method returns an array containing the set of items
  388.      * that are provided by the RSS file.
  389.      *
  390.      * @access public
  391.      * @return array 
  392.      */
  393.     function getItems()
  394.     {
  395.         return (array)$this->items;
  396.     }
  397.  
  398.     // }}}
  399.     // {{{ getImages()
  400.  
  401.     /**
  402.      * Get images from RSS file
  403.      *
  404.      * This method returns an array containing the set of images
  405.      * that are provided by the RSS file.
  406.      *
  407.      * @access public
  408.      * @return array 
  409.      */
  410.     function getImages()
  411.     {
  412.         return (array)$this->images;
  413.     }
  414.  
  415.     // }}}
  416.     // {{{ getTextinputs()
  417.  
  418.     /**
  419.      * Get text input fields from RSS file
  420.      *
  421.      * @access public
  422.      * @return array 
  423.      */
  424.     function getTextinputs()
  425.     {
  426.         return (array)$this->textinputs;
  427.     }
  428.  
  429.     // }}}
  430.  
  431. }
  432. ?>

Documentation generated on Mon, 11 Mar 2019 14:44:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.