Source for file RssParser.php

Documentation is available at RssParser.php

  1. <?php
  2.  
  3. // ----------------------------------------------------------------------------------
  4. // Class: RSS/ATOM Parser
  5. // ----------------------------------------------------------------------------------
  6.  
  7.  
  8. /**
  9.  * RSS/ATOM Parser
  10.  * 
  11.  * This class uses the MagpieRSS Parser by Kellan Elliott-McCrea <kellan@protest.net>. MagpieRSS is
  12.  * compatible with RSS 0.9 through RSS 1.0. Also parses RSS 1.0's modules, RSS 2.0, and Atom (with a few exceptions).
  13.  * Magpie is distributed under the GPL license. (http://magpierss.sourceforge.net/)
  14.  * 
  15.  *
  16.  *
  17.  * @author Tobias Gauß <tobias.gauss@web.de>
  18.  * @version $Id: fsource_syntax__syntaxRssParser.php.html 443 2007-06-01 16:25:38Z cax $
  19.  * @package syntax
  20.  * @access public
  21.  ***/
  22.  
  23. class RssParser extends Object {
  24.     var $type;
  25.     
  26.     /**
  27.     * Main function
  28.     *
  29.     * @param $url location of the document
  30.     * @return parsed model
  31.     */
  32.     function generateModel($url){
  33.         $rss null;
  34.         if(substr($url,0,7)=="http://"){
  35.             $rss fetch_rss($url);
  36.         }else{
  37.                 $string file_get_contents($url);
  38.                 $rss new MagpieRSS($string);    
  39.         }
  40.         
  41.         $model ModelFactory::getMemModel();    
  42.         $bn new Resource($url);
  43.         
  44.         $this->type = $rss->feed_type;
  45.         $version $rss->feed_version;
  46.         
  47.         $nmsp null;
  48.          if($version == "1.0")
  49.                   $nmsp "http://purl.org/rss/1.0/";
  50.          if($version == "0.91")
  51.                   $nmsp "http://my.netscape.com/publish/formats/rss-spec-0.91.html#";
  52.          if($version == "2.0")
  53.                   $nmsp "http://purl.org/rss/1.0/";
  54.          if($this->type == "Atom")
  55.                   $nmsp "http://www.w3.org/2005/Atom";
  56.           
  57.         if($this->type == "Atom")
  58.             $this->channel($model,$rss,$bn,$nmsp,"feed");
  59.         else 
  60.             $this->channel($model,$rss,$bn,$nmsp,"channel");
  61.         $this->image($model,$rss,$bn,$nmsp);
  62.         $this->items($model,$rss,$bn,$nmsp);
  63.         $model->addNamespace("rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#");
  64.         if($nmsp!= null)
  65.             $model->addNamespace($this->type,$nmsp);
  66.         return $model;
  67.     }
  68.  
  69.     /**
  70.     * Parses the image if there is one.
  71.     *
  72.     * @param $model the model
  73.     * @param $rss the magpie parser object
  74.     * @param $bnOrg the node which represents the channel
  75.     * @param $nmsp the
  76.     */
  77.     function image(&$model,&$rss,&$bnOrg,&$nmsp){        
  78.         if(count($rss->image)>0){
  79.             if(isset($rss->image['about'])){
  80.                 $bn new Resource($rss->image['about']);
  81.             }else if(isset($rss->image['url'])){
  82.                 $bn new Resource($rss->image['url']);
  83.             }else{
  84.                 $bn new BlankNode("image");
  85.             }
  86.             $model->add(new Statement($bnOrg,new Resource($nmsp."image"),$bn));
  87.             $model->add(new Statement($bn,new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),new Resource($nmsp."image")));
  88.         }
  89.         foreach($rss->image as $key => $value){
  90.         $statement null;    
  91.         switch($key){
  92.                     case "dc":
  93.                         $this->dc($bn,$model,$value);
  94.                     break;
  95.                     case "sy":
  96.                         $this->sy($bn,$model,$value);
  97.                     break;
  98.                     default :
  99.                         $statement new Statement($bn,new Resource($nmsp.$key)new Literal($value));
  100.                     break;
  101.                 }
  102.             if($statement != null){
  103.                 $model->add($statement);
  104.             }
  105.         }
  106.     
  107.     }
  108.     
  109.     /**
  110.     * Parses the rss items/ atom entries.
  111.     *
  112.     * @param $model the model
  113.     * @param $rss the magpie parser object
  114.     * @param $bnOrg the node which represents the channel
  115.     * @param $nmsp the
  116.     */
  117.     function items(&$model,&$rss,&$bnOrg,&$nmsp){        
  118.         $items new BlankNode("items");
  119.         $model->add(new Statement($bnOrg,new Resource($nmsp."items"),$items));
  120.         $model->add(new Statement($items,new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq")));
  121.         $i 1;
  122.         foreach($rss->items as $outerKey => $outerValue){
  123.             if(isset($outerValue['about'])){
  124.                 $bn new Resource($outerValue['about']);
  125.             }else if(isset($outerValue['guid'])){
  126.                 $bn new Resource($outerValue['guid']);
  127.             }else if(isset($outerValue['id'])){
  128.                 $bn new Resource($outerValue['id']);
  129.             }else{
  130.                 $bn new Blanknode("item".$i);
  131.             }
  132.             $model->add(new Statement($items,new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#_".$i),$bn));
  133.             if($this->type == "Atom"){
  134.                 $model->add(new Statement($bn,new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),new Resource("http://www.w3.org/2005/Atomentry")));
  135.             }else{
  136.                 $model->add(new Statement($bn,new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),new Resource("http://purl.org/rss/1.0/item")));
  137.             }
  138.             foreach($outerValue as $key => $value){
  139.                 $statement null;
  140.                 switch($key){
  141.                     case "about":
  142.                         break;
  143.                     case "guid":
  144.                         break;
  145.                     case "dc":
  146.                         $this->dc($bn,$model,$value);
  147.                     break;
  148.                     case "sy":
  149.                         $this->sy($bn,$model,$value);
  150.                     break;
  151.                     default :
  152.                         if($value != null)
  153.                         $statement new Statement($bn,new Resource($nmsp.$key)new Literal($value));
  154.                     break;
  155.                 }
  156.                 if($statement != null){
  157.                     $model->add($statement);
  158.                 }
  159.             }
  160.             $i++;
  161.         }
  162.         
  163.     }
  164.     
  165.     /**
  166.     * Adds the dc namespace.
  167.     *
  168.     * @param $node the node
  169.     * @param $model the model
  170.     * @param $dc Array which contains the dc objects
  171.     */
  172.     function dc(&$node,&$model,&$dc){
  173.         $model->addNamespace("dc","http://purl.org/dc/elements/1.1/");
  174.         foreach($dc as $key => $value){
  175.                 $statement null;
  176.                 $statement new Statement($node,new Resource("http://purl.org/dc/elements/1.1/".$key)new Literal($value));
  177.             if($statement != null){
  178.                 $model->add($statement);
  179.             }
  180.         }
  181.     }
  182.     
  183.     /**
  184.     * Adds the sy namespace.
  185.     *
  186.     * @param $node the node
  187.     * @param $model the model
  188.     * @param $sy Array which contains the sy objects
  189.     */
  190.     function sy(&$node,&$model,&$sy){
  191.         $model->addNamespace("sy","http://purl.org/rss/1.0/modules/syndication/");
  192.         foreach($sy as $key => $value){
  193.                 $statement null;
  194.                 $statement new Statement($node,new Resource("http://purl.org/rss/1.0/modules/syndication/".$key)new Literal($value));
  195.             if($statement != null){
  196.                 $model->add($statement);
  197.             }
  198.         }
  199.         
  200.     }
  201.     
  202.     
  203.     /**
  204.     * Parses the rss channel/ atom feed.
  205.     *
  206.     * @param $model the model
  207.     * @param $rss the magpie parser object
  208.     * @param $node the node which represents the channel
  209.     * @param $nmsp the
  210.     */
  211.     function channel(&$model,&$rss,&$node,&$nmsp,$type){
  212.          // Channel
  213.          $statement new Statement($node,new Resource('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')new Resource($nmsp.$type));
  214.         $model->add($statement);
  215.         
  216.          foreach($rss->channel as $key => $value){
  217.         $statement null;    
  218.         switch($key){
  219.                     case "dc":
  220.                         $this->dc($node,$model,$value);
  221.                     break;
  222.                     case "sy":
  223.                         $this->sy($node,$model,$value);
  224.                     break;
  225.                     case "items":    
  226.                     break;
  227.                     case "tagline":    
  228.                     break;
  229.                     case "items_seq":
  230.                     break;
  231.                     default :
  232.                         $statement new Statement($node,new Resource($nmsp.$key)new Literal($value));
  233.                     break;
  234.                 }
  235.         if($statement != null){
  236.             $model->add($statement);
  237.         }
  238.     }
  239. }
  240.  
  241.   
  242.   
  243. //end: RssParser
  244.  
  245. ?>

Documentation generated on Fri, 1 Jun 2007 16:51:43 +0200 by phpDocumentor 1.3.2