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.26 2005/05/25 20:48:46 mj 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.26 $
  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 Dublin Core Metadata, blogChannel RSS module, CreativeCommons,
  123.      * Content and Syndication are supported.
  124.      *
  125.      * @var array 
  126.      */
  127.     var $moduleTags = array('DC:TITLE''DC:CREATOR''DC:SUBJECT''DC:DESCRIPTION',
  128.                             'DC:PUBLISHER''DC:CONTRIBUTOR''DC:DATE''DC:TYPE',
  129.                             'DC:FORMAT''DC:IDENTIFIER''DC:SOURCE''DC:LANGUAGE',
  130.                             'DC:RELATION''DC:COVERAGE''DC:RIGHTS',
  131.                             'BLOGCHANNEL:BLOGROLL''BLOGCHANNEL:MYSUBSCRIPTIONS',
  132.                             'BLOGCHANNEL:MYSUBSCRIPTIONS''BLOGCHANNEL:CHANGES',
  133.                             'CC:LICENSE''CONTENT:ENCODED'
  134.                             'SY:UPDATEPERIOD''SY:UPDATEFREQUENCY''SY:UPDATEBASE'
  135.                             );
  136.  
  137.     // }}}
  138.     // {{{ Constructor
  139.  
  140.     /**
  141.      * Constructor
  142.      *
  143.      * @access public
  144.      * @param mixed File pointer, name of the RSS file, or an RSS string.
  145.      * @param string  Source charset encoding, use null (default) to use
  146.      *                 default encoding (ISO-8859-1)
  147.      * @param string  Target charset encoding, use null (default) to use
  148.      *                 default encoding (ISO-8859-1)
  149.      * @return void 
  150.      */
  151.     function XML_RSS($handle ''$srcenc = null$tgtenc = null)
  152.     {
  153.         if ($srcenc === null && $tgtenc === null{
  154.             $this->XML_Parser();
  155.         else {
  156.             $this->XML_Parser($srcenc'event'$tgtenc);
  157.         }
  158.  
  159.         $this->setInput($handle);
  160.  
  161.         if ($handle == ''{
  162.             $this->raiseError('No input passed.');
  163.         }
  164.     }
  165.  
  166.     // }}}
  167.     // {{{ startHandler()
  168.  
  169.     /**
  170.      * Start element handler for XML parser
  171.      *
  172.      * @access private
  173.      * @param  object XML parser object
  174.      * @param  string XML element
  175.      * @param  array  Attributes of XML tag
  176.      * @return void 
  177.      */
  178.     function startHandler($parser$element$attribs)
  179.     {
  180.         if (substr($element04== "RSS:"{
  181.             $element substr($element4);
  182.         }
  183.  
  184.         switch ($element{
  185.             case 'CHANNEL':
  186.             case 'ITEM':
  187.             case 'IMAGE':
  188.             case 'TEXTINPUT':
  189.                 $this->insideTag $element;
  190.                 array_push($this->insideTagStack$element);
  191.                 break;
  192.  
  193.             case 'ENCLOSURE' :
  194.                 $this->attribs $attribs;
  195.                 break;
  196.  
  197.             default:
  198.                 $this->activeTag $element;
  199.         }
  200.     }
  201.  
  202.     // }}}
  203.     // {{{ endHandler()
  204.  
  205.     /**
  206.      * End element handler for XML parser
  207.      *
  208.      * If the end of <item>, <channel>, <image> or <textinput>
  209.      * is reached, this method updates the structure array
  210.      * $this->struct[] and adds the field "type" to this array,
  211.      * that defines the type of the current field.
  212.      *
  213.      * @access private
  214.      * @param  object XML parser object
  215.      * @param  string 
  216.      * @return void 
  217.      */
  218.     function endHandler($parser$element)
  219.     {
  220.         if (substr($element04== "RSS:"{
  221.             $element substr($element4);
  222.         }
  223.  
  224.         if ($element == $this->insideTag{
  225.             array_pop($this->insideTagStack);
  226.             $this->insideTag end($this->insideTagStack);
  227.  
  228.             $this->struct[array_merge(array('type' => strtolower($element)),
  229.                                           $this->last);
  230.         }
  231.  
  232.         if ($element == 'ITEM'{
  233.             $this->items[$this->item;
  234.             $this->item '';
  235.         }
  236.  
  237.         if ($element == 'IMAGE'{
  238.             $this->images[$this->image;
  239.             $this->image '';
  240.         }
  241.  
  242.         if ($element == 'TEXTINPUT'{
  243.             $this->textinputs $this->textinput;
  244.             $this->textinput '';
  245.         }
  246.  
  247.         if ($element == 'ENCLOSURE'{
  248.             if (!isset($this->item['enclosures'])) {
  249.                 $this->item['enclosures'= array();
  250.             }
  251.  
  252.             $this->item['enclosures'][array_change_key_case($this->attribsCASE_LOWER);
  253.             $this->attribs = array();
  254.         }
  255.  
  256.         $this->activeTag '';
  257.     }
  258.  
  259.     // }}}
  260.     // {{{ cdataHandler()
  261.  
  262.     /**
  263.      * Handler for character data
  264.      *
  265.      * @access private
  266.      * @param  object XML parser object
  267.      * @param  string CDATA
  268.      * @return void 
  269.      */
  270.     function cdataHandler($parser$cdata)
  271.     {
  272.         if (in_array($this->insideTag$this->parentTags)) {
  273.             $tagName strtolower($this->insideTag);
  274.             $var $this->{$tagName 'Tags'};
  275.  
  276.             if (in_array($this->activeTag$var||
  277.                 in_array($this->activeTag$this->moduleTags)) {
  278.                 $this->_add($tagNamestrtolower($this->activeTag),
  279.                             $cdata);
  280.             }
  281.             
  282.         }
  283.     }
  284.  
  285.     // }}}
  286.     // {{{ defaultHandler()
  287.  
  288.     /**
  289.      * Default handler for XML parser
  290.      *
  291.      * @access private
  292.      * @param  object XML parser object
  293.      * @param  string CDATA
  294.      * @return void 
  295.      */
  296.     function defaultHandler($parser$cdata)
  297.     {
  298.         return;
  299.     }
  300.  
  301.     // }}}
  302.     // {{{ _add()
  303.  
  304.     /**
  305.      * Add element to internal result sets
  306.      *
  307.      * @access private
  308.      * @param  string Name of the result set
  309.      * @param  string Fieldname
  310.      * @param  string Value
  311.      * @return void 
  312.      * @see    cdataHandler
  313.      */
  314.     function _add($type$field$value)
  315.     {
  316.         if (empty($this->{$type}|| empty($this->{$type}[$field])) {
  317.             $this->{$type}[$field$value;
  318.         else {
  319.             $this->{$type}[$field.= $value;
  320.         }
  321.  
  322.         $this->last = $this->{$type};
  323.     }
  324.  
  325.     // }}}
  326.     // {{{ getStructure()
  327.  
  328.     /**
  329.      * Get complete structure of RSS file
  330.      *
  331.      * @access public
  332.      * @return array 
  333.      */
  334.     function getStructure()
  335.     {
  336.         return (array)$this->struct;
  337.     }
  338.  
  339.     // }}}
  340.     // {{{ getchannelInfo()
  341.  
  342.     /**
  343.      * Get general information about current channel
  344.      *
  345.      * This method returns an array containing the information
  346.      * that has been extracted from the <channel>-tag while parsing
  347.      * the RSS file.
  348.      *
  349.      * @access public
  350.      * @return array 
  351.      */
  352.     function getChannelInfo()
  353.     {
  354.         return (array)$this->channel;
  355.     }
  356.  
  357.     // }}}
  358.     // {{{ getItems()
  359.  
  360.     /**
  361.      * Get items from RSS file
  362.      *
  363.      * This method returns an array containing the set of items
  364.      * that are provided by the RSS file.
  365.      *
  366.      * @access public
  367.      * @return array 
  368.      */
  369.     function getItems()
  370.     {
  371.         return (array)$this->items;
  372.     }
  373.  
  374.     // }}}
  375.     // {{{ getImages()
  376.  
  377.     /**
  378.      * Get images from RSS file
  379.      *
  380.      * This method returns an array containing the set of images
  381.      * that are provided by the RSS file.
  382.      *
  383.      * @access public
  384.      * @return array 
  385.      */
  386.     function getImages()
  387.     {
  388.         return (array)$this->images;
  389.     }
  390.  
  391.     // }}}
  392.     // {{{ getTextinputs()
  393.  
  394.     /**
  395.      * Get text input fields from RSS file
  396.      *
  397.      * @access public
  398.      * @return array 
  399.      */
  400.     function getTextinputs()
  401.     {
  402.         return (array)$this->textinputs;
  403.     }
  404.  
  405.     // }}}
  406.  
  407. }
  408. ?>

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