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: RssParser.php 301 2006-06-26 10:22:25Z tgauss $
|
---|
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 | $modelFactory = new ModelFactory();
|
---|
42 | $model = $modelFactory->getMemModel();
|
---|
43 | $bn = new Resource($url);
|
---|
44 |
|
---|
45 | $this->type = $rss->feed_type;
|
---|
46 | $version = $rss->feed_version;
|
---|
47 |
|
---|
48 | $nmsp = null;
|
---|
49 | if($version == "1.0")
|
---|
50 | $nmsp = "http://purl.org/rss/1.0/";
|
---|
51 | if($version == "0.91")
|
---|
52 | $nmsp = "http://my.netscape.com/publish/formats/rss-spec-0.91.html#";
|
---|
53 | if($version == "2.0")
|
---|
54 | $nmsp = "http://purl.org/rss/1.0/";
|
---|
55 | if($this->type == "Atom")
|
---|
56 | $nmsp = "http://www.w3.org/2005/Atom";
|
---|
57 |
|
---|
58 | if($this->type == "Atom")
|
---|
59 | $this->channel($model,$rss,$bn,$nmsp,"feed");
|
---|
60 | else
|
---|
61 | $this->channel($model,$rss,$bn,$nmsp,"channel");
|
---|
62 | $this->image($model,$rss,$bn,$nmsp);
|
---|
63 | $this->items($model,$rss,$bn,$nmsp);
|
---|
64 | $model->addNamespace("rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#");
|
---|
65 | if($nmsp!= null)
|
---|
66 | $model->addNamespace($this->type,$nmsp);
|
---|
67 | return $model;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Parses the image if there is one.
|
---|
72 | *
|
---|
73 | * @param $model the model
|
---|
74 | * @param $rss the magpie parser object
|
---|
75 | * @param $bnOrg the node which represents the channel
|
---|
76 | * @param $nmsp the
|
---|
77 | */
|
---|
78 | function image(&$model,&$rss,&$bnOrg,&$nmsp){
|
---|
79 | if(count($rss->image)>0){
|
---|
80 | if(isset($rss->image['about'])){
|
---|
81 | $bn = new Resource($rss->image['about']);
|
---|
82 | }else if(isset($rss->image['url'])){
|
---|
83 | $bn = new Resource($rss->image['url']);
|
---|
84 | }else{
|
---|
85 | $bn = new BlankNode("image");
|
---|
86 | }
|
---|
87 | $model->add(new Statement($bnOrg,new Resource($nmsp."image"),$bn));
|
---|
88 | $model->add(new Statement($bn,new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),new Resource($nmsp."image")));
|
---|
89 | }
|
---|
90 | foreach($rss->image as $key => $value){
|
---|
91 | $statement = null;
|
---|
92 | switch($key){
|
---|
93 | case "dc":
|
---|
94 | $this->dc($bn,$model,$value);
|
---|
95 | break;
|
---|
96 | case "sy":
|
---|
97 | $this->sy($bn,$model,$value);
|
---|
98 | break;
|
---|
99 | default :
|
---|
100 | $statement = new Statement($bn,new Resource($nmsp.$key), new Literal($value));
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | if($statement != null){
|
---|
104 | $model->add($statement);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Parses the rss items/ atom entries.
|
---|
112 | *
|
---|
113 | * @param $model the model
|
---|
114 | * @param $rss the magpie parser object
|
---|
115 | * @param $bnOrg the node which represents the channel
|
---|
116 | * @param $nmsp the
|
---|
117 | */
|
---|
118 | function items(&$model,&$rss,&$bnOrg,&$nmsp){
|
---|
119 | $items = new BlankNode("items");
|
---|
120 | $model->add(new Statement($bnOrg,new Resource($nmsp."items"),$items));
|
---|
121 | $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")));
|
---|
122 | $i = 1;
|
---|
123 | foreach($rss->items as $outerKey => $outerValue){
|
---|
124 | if(isset($outerValue['about'])){
|
---|
125 | $bn = new Resource($outerValue['about']);
|
---|
126 | }else if(isset($outerValue['guid'])){
|
---|
127 | $bn = new Resource($outerValue['guid']);
|
---|
128 | }else if(isset($outerValue['id'])){
|
---|
129 | $bn = new Resource($outerValue['id']);
|
---|
130 | }else{
|
---|
131 | $bn = new Blanknode("item".$i);
|
---|
132 | }
|
---|
133 | $model->add(new Statement($items,new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#_".$i),$bn));
|
---|
134 | if($this->type == "Atom"){
|
---|
135 | $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")));
|
---|
136 | }else{
|
---|
137 | $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")));
|
---|
138 | }
|
---|
139 | foreach($outerValue as $key => $value){
|
---|
140 | $statement = null;
|
---|
141 | switch($key){
|
---|
142 | case "about":
|
---|
143 | break;
|
---|
144 | case "guid":
|
---|
145 | break;
|
---|
146 | case "dc":
|
---|
147 | $this->dc($bn,$model,$value);
|
---|
148 | break;
|
---|
149 | case "sy":
|
---|
150 | $this->sy($bn,$model,$value);
|
---|
151 | break;
|
---|
152 | default :
|
---|
153 | if($value != null)
|
---|
154 | $statement = new Statement($bn,new Resource($nmsp.$key), new Literal($value));
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | if($statement != null){
|
---|
158 | $model->add($statement);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | $i++;
|
---|
162 | }
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Adds the dc namespace.
|
---|
168 | *
|
---|
169 | * @param $node the node
|
---|
170 | * @param $model the model
|
---|
171 | * @param $dc Array which contains the dc objects
|
---|
172 | */
|
---|
173 | function dc(&$node,&$model,&$dc){
|
---|
174 | $model->addNamespace("dc","http://purl.org/dc/elements/1.1/");
|
---|
175 | foreach($dc as $key => $value){
|
---|
176 | $statement = null;
|
---|
177 | $statement = new Statement($node,new Resource("http://purl.org/dc/elements/1.1/".$key), new Literal($value));
|
---|
178 | if($statement != null){
|
---|
179 | $model->add($statement);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Adds the sy namespace.
|
---|
186 | *
|
---|
187 | * @param $node the node
|
---|
188 | * @param $model the model
|
---|
189 | * @param $sy Array which contains the sy objects
|
---|
190 | */
|
---|
191 | function sy(&$node,&$model,&$sy){
|
---|
192 | $model->addNamespace("sy","http://purl.org/rss/1.0/modules/syndication/");
|
---|
193 | foreach($sy as $key => $value){
|
---|
194 | $statement = null;
|
---|
195 | $statement = new Statement($node,new Resource("http://purl.org/rss/1.0/modules/syndication/".$key), new Literal($value));
|
---|
196 | if($statement != null){
|
---|
197 | $model->add($statement);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Parses the rss channel/ atom feed.
|
---|
206 | *
|
---|
207 | * @param $model the model
|
---|
208 | * @param $rss the magpie parser object
|
---|
209 | * @param $node the node which represents the channel
|
---|
210 | * @param $nmsp the
|
---|
211 | */
|
---|
212 | function channel(&$model,&$rss,&$node,&$nmsp,$type){
|
---|
213 | // Channel
|
---|
214 | $statement = new Statement($node,new Resource('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), new Resource($nmsp.$type));
|
---|
215 | $model->add($statement);
|
---|
216 |
|
---|
217 | foreach($rss->channel as $key => $value){
|
---|
218 | $statement = null;
|
---|
219 | switch($key){
|
---|
220 | case "dc":
|
---|
221 | $this->dc($node,$model,$value);
|
---|
222 | break;
|
---|
223 | case "sy":
|
---|
224 | $this->sy($node,$model,$value);
|
---|
225 | break;
|
---|
226 | case "items":
|
---|
227 | break;
|
---|
228 | case "tagline":
|
---|
229 | break;
|
---|
230 | case "items_seq":
|
---|
231 | break;
|
---|
232 | default :
|
---|
233 | $statement = new Statement($node,new Resource($nmsp.$key), new Literal($value));
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | if($statement != null){
|
---|
237 | $model->add($statement);
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 |
|
---|
244 | } //end: RssParser
|
---|
245 |
|
---|
246 | ?>
|
---|