[ PHPXref.com ] [ Generated: Sun Jul 20 17:02:14 2008 ] [ Cognito CMS 1.1 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> rss_parse.inc (source)

   1  <?php
   2  /*
   3   * Project:     MagpieRSS: a simple RSS integration tool
   4   * File:        rss_parse.inc includes code for parsing
   5   *                RSS, and returning an RSS object
   6   * Author:      Kellan Elliott-McCrea <kellan@protest.net>
   7   * Version:        0.51
   8   * License:        GPL
   9   *
  10   * The lastest version of MagpieRSS can be obtained from:
  11   * http://magpierss.sourceforge.net
  12   *
  13   * For questions, help, comments, discussion, etc., please join the
  14   * Magpie mailing list:
  15   * magpierss-general@lists.sourceforge.net
  16   *
  17   */
  18   
  19  
  20  /* 
  21   * NOTES ON RSS PARSING PHILOSOPHY (moderately important):
  22   * MagpieRSS parse all versions of RSS with a few limitation (mod_content, and
  23   * mod_taxonomy support is shaky) into a simple object, with 2 fields, 
  24   * the hash 'channel', and the array 'items'.
  25   *
  26   * MagpieRSS is a forgiving and inclusive parser.  It currently makes no
  27   * attempt to enforce the validity on an RSS feed.  It will include any
  28   * properly formatted tags it finds, allowing to you to mix RSS 0.93, with RSS
  29   * 1.0, with tags or your own imagining.  This sort of witches brew is a bad
  30   * bad idea!  But Magpie is less pendantic then I am.
  31   *
  32   * RSS validators are readily available on the web at:
  33   * http://feeds.archive.org/validator/
  34   * http://www.ldodds.com/rss_validator/1.0/validator.html
  35   *
  36   */
  37  
  38  /*
  39   * EXAMPLE PARSE RESULTS:
  40   *
  41   * Magpie tries to parse RSS into ease to use PHP datastructures.
  42   *
  43   * For example, Magpie on encountering RSS 1.0 item entry:
  44   *
  45   * <item rdf:about="http://protest.net/NorthEast/calendrome.cgi?span=event&#38;ID=210257">
  46   * <title>Weekly Peace Vigil</title>
  47   * <link>http://protest.net/NorthEast/calendrome.cgi?span=event&#38;ID=210257</link>
  48   * <description>Wear a white ribbon</description>
  49   * <dc:subject>Peace</dc:subject>
  50   * <ev:startdate>2002-06-01T11:00:00</ev:startdate>
  51   * <ev:location>Northampton, MA</ev:location>
  52   * <ev:enddate>2002-06-01T12:00:00</ev:enddate>
  53   * <ev:type>Protest</ev:type>
  54   * </item>
  55   * 
  56   * Would transform it into the following associative array, and push it
  57   * onto the array $rss-items
  58   *
  59   * array(
  60   *    title => 'Weekly Peace Vigil',
  61   *    link =>
  62   *    'http://protest.net/NorthEast/calendrome.cgi?span=event&#38;ID=210257',
  63   *    description => 'Wear a white ribbon',
  64   *    dc => array (
  65   *            subject => 'Peace'
  66   *        ),
  67   *    ev => array (
  68   *        startdate => '2002-06-01T11:00:00',
  69   *        enddate => '2002-06-01T12:00:00',
  70   *        type => 'Protest',
  71   *        location => 'Northampton, MA'
  72   *    )
  73   * )
  74   *
  75   */
  76  
  77  class MagpieRSS {
  78      /*
  79       * Hybrid parser, and object.  (probably a bad idea! :)
  80       *
  81       * Useage Example:
  82       *
  83       * $some_rss = "<?xml version="1.0"......
  84       *
  85       * $rss = new MagpieRSS( $some_rss );
  86       *
  87       * // print rss chanel title
  88       * echo $rss->channel['title'];
  89       *
  90       * // print the title of each item
  91       * foreach ($rss->items as $item ) {
  92       *      echo $item[title];
  93       * }
  94       *
  95       * see rss_fetch.inc for a simpler interface
  96       */
  97       
  98      var $parser;
  99      
 100      var $current_item    = array();    // item currently being parsed
 101      var $items            = array();    // collection of parsed items
 102      var $channel        = array();    // hash of channel fields
 103      var $textinput        = array();
 104      var $image            = array();
 105      
 106      var $parent_field    = array('RDF');
 107      var $current_field    = '';
 108      var $current_namespace    = false;
 109      
 110      var $ERROR = "";
 111      
 112  /*======================================================================*\
 113      Function: MagpieRSS
 114      Purpose:  Constructor, sets up XML parser,parses source,
 115                and populates object.. 
 116      Input:      String containing the RSS to be parsed
 117  \*======================================================================*/
 118  	function MagpieRSS ($source) {
 119          
 120          # if PHP xml isn't compiled in, die
 121          #
 122          if (!function_exists('xml_parser_create')) {
 123              $this->error( "Failed to load PHP's XML Extension. " . 
 124                            "http://www.php.net/manual/en/ref.xml.php",
 125                             E_USER_ERROR );
 126          }
 127          
 128          $parser = @xml_parser_create();
 129          
 130          if (!is_resource($parser))
 131          {
 132              $this->error( "Failed to create an instance of PHP's XML parser. " .
 133                            "http://www.php.net/manual/en/ref.xml.php",
 134                            E_USER_ERROR );
 135          }
 136  
 137          
 138          $this->parser = $parser;
 139          
 140          # pass in parser, and a reference to this object
 141          # setup handlers
 142          #
 143          xml_set_object( $this->parser, $this );
 144          xml_set_element_handler($this->parser, 'start_element', 'end_element');
 145          xml_set_character_data_handler( $this->parser, 'cdata' ); 
 146      
 147          
 148          $status = xml_parse( $this->parser, $source );
 149          
 150          if (! $status ) {
 151              $errorcode = xml_get_error_code( $this->parser );
 152              if ( $errorcode != XML_ERROR_NONE ) {
 153                  $xml_error = xml_error_string( $errorcode );
 154                  $error_line = xml_get_current_line_number($this->parser);
 155                  $error_col = xml_get_current_column_number($this->parser);
 156                  $errormsg = "$xml_error at line $error_line, column $error_col";
 157  
 158                  $this->error( $errormsg );
 159              }
 160          }
 161          
 162          xml_parser_free( $this->parser );
 163      }
 164      
 165  	function start_element ($p, $element, &$attrs) {
 166          $element     = strtolower( $element );
 167                  
 168          # check for a namespace, and split if found
 169          #
 170          $namespace    = false;
 171          if ( strpos( $element, ':' ) ) {
 172              list($namespace, $element) = split( ':', $element, 2); 
 173          }
 174          $this->current_field = $element;
 175          if ( $namespace and $namespace != 'rdf' ) {
 176              $this->current_namespace = $namespace;
 177          }
 178          
 179          if ( $element == 'channel' ) {
 180              array_unshift( $this->parent_field, 'channel' );
 181          }
 182          elseif ( $element == 'items' ) {
 183              array_unshift( $this->parent_field, 'items' );
 184          }
 185          elseif ( $element == 'item' ) {
 186              array_unshift( $this->parent_field, 'item' );
 187          }
 188          elseif ( $element == 'textinput' ) {
 189              array_unshift( $this->parent_field, 'textinput' );
 190          }
 191          elseif ( $element == 'image' ) {
 192              array_unshift( $this->parent_field, 'image' );
 193          }
 194          
 195      }
 196      
 197  	function end_element ($p, $element) {
 198          $element = strtolower($element);
 199                              
 200          if ( $element == 'item' ) {        
 201              $this->items[] = $this->current_item;
 202              $this->current_item = array();
 203              array_shift( $this->parent_field );
 204          }
 205          elseif ( $element == 'channel' or $element == 'items' or 
 206                   $element == 'textinput' or $element == 'image' ) {
 207              array_shift( $this->parent_field );
 208          }
 209          
 210          $this->current_field = '';
 211          $this->current_namespace = false;
 212      }
 213      
 214  	function cdata ($p, $text) {
 215          # skip item, channel, items first time we see them
 216          #
 217          if ( $this->parent_field[0] == $this->current_field or
 218               ! $this->current_field ) {
 219              return;
 220          }
 221          elseif ( $this->parent_field[0] == 'channel') {
 222              if ( $this->current_namespace ) {
 223                  $this->append(
 224                      $this->channel[ $this->current_namespace ][ $this->current_field ],
 225                      $text);
 226              }
 227              else {
 228                  $this->append($this->channel[ $this->current_field ], $text);
 229              }
 230          
 231          }
 232          elseif ( $this->parent_field[0] == 'item' ) {
 233              if ( $this->current_namespace ) {
 234                  $this->append(
 235                      $this->current_item[ $this->current_namespace ][$this->current_field ],
 236                      $text);
 237              }
 238              else {
 239                  $this->append(
 240                      $this->current_item[ $this->current_field ],
 241                      $text );
 242              }
 243          }
 244          elseif ( $this->parent_field[0] == 'textinput' ) {
 245              if ( $this->current_namespace ) {
 246                  $this->append(
 247                      $this->textinput[ $this->current_namespace ][ $this->current_field ],
 248                       $text );
 249              }
 250              else {
 251                  $this->append(
 252                      $this->textinput[ $this->current_field ],
 253                      $text );
 254              }
 255          }
 256          elseif ( $this->parent_field[0] == 'image' ) {
 257              if ( $this->current_namespace ) {
 258                  $this->append(
 259                      $this->image[ $this->current_namespace ][ $this->current_field ],
 260                      $text );
 261              }
 262              else {
 263                  $this->append(
 264                      $this->image[ $this->current_field ],
 265                      $text );
 266              }
 267          }
 268      }
 269      
 270  	function append (&$str1, $str2="") {
 271          if (!isset($str1) ) {
 272              $str1="";
 273          }
 274          $str1 .= $str2;
 275      }
 276      
 277  	function error ($errormsg, $lvl=E_USER_WARNING) {
 278          // append PHP's error message if track_errors enabled
 279          if ( $php_errormsg ) { 
 280              $errormsg .= " ($php_errormsg)";
 281          }
 282          $this->ERROR = $errormsg;
 283          if ( MAGPIE_DEBUG ) {
 284              trigger_error( $errormsg, $lvl);        
 285          }
 286          else {
 287              error_log( $errormsg, 0);
 288          }
 289      }
 290          
 291  
 292  /*======================================================================*\
 293      EVERYTHING BELOW HERE IS FOR DEBUGGING PURPOSES
 294  \*======================================================================*/
 295  	function show_list () {
 296          echo "<ol>\n";
 297          foreach ($this->items as $item) {
 298              echo "<li>", $this->show_item( $item );
 299          }
 300          echo "</ol>";
 301      }
 302      
 303  	function show_channel () {
 304          echo "channel:<br>";
 305          echo "<ul>";
 306          while ( list($key, $value) = each( $this->channel ) ) {
 307              echo "<li> $key: $value";
 308          }
 309          echo "</ul>";
 310      }
 311      
 312  	function show_item ($item) {
 313          echo "item: $item[title]";
 314          echo "<ul>";
 315          while ( list($key, $value) = each($item) ) {
 316              if ( is_array($value) ) {
 317                  echo "<br><b>$key</b>";
 318                  echo "<ul>";
 319                  while ( list( $ns_key, $ns_value) = each( $value ) ) {
 320                      echo "<li>$ns_key: $ns_value";
 321                  }
 322                  echo "</ul>";
 323              }
 324              else {
 325                  echo "<li> $key: $value";
 326              }
 327          }
 328          echo "</ul>";
 329      }
 330  
 331  /*======================================================================*\
 332      END DEBUGGING FUNCTIONS    
 333  \*======================================================================*/
 334      
 335  
 336  
 337  } # end class RSS
 338  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]