]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/AtomParser.php
New FSF address
[SourceForge/phpwiki.git] / lib / AtomParser.php
1 <?php // -*-php-*-
2 // $Id$
3 /*
4  * Copyright 2010 Sébastien Le Callonnec
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 /**
23  * This class is a poor-man Atom parser, it does no validation of the feed.
24  * The content of an entry ("payload") is not parsed but rather returned "as-is",
25  * as its format can be text, html or xhtml.
26  *
27  * @author: Sébastien Le Callonnec
28  */
29 require_once('lib/XmlParser.php');
30
31 class AtomParser
32 extends XmlParser
33 {
34     // Feed
35     var $feed = array();
36     var $feed_title = '';
37     var $feed_links = array();
38     var $feed_subtitle = '';
39     var $feed_id = '';
40     var $feed_updated = '';
41     var $feed_authors = array();
42     var $feed_contributors = array();
43     var $generator = '';
44     var $icon = '';
45     var $rights = '';
46     var $logo = '';
47
48     var $categories = array();
49
50     var $authors = array();
51     var $contributors = array();
52
53     // Author, Contributor
54     var $name = '';
55     var $email = '';
56     var $uri = '';
57
58     // Entries
59     var $entries = array();
60     var $inside_entry = false;
61     var $title = '';
62     var $updated = '';
63     var $published = '';
64     var $id = '';
65     var $links = array();
66     var $summary = '';
67
68     var $inside_content = false;
69     var $content = '';
70
71     function tag_open($parser, $name, $attrs='') {
72         global $current_tag, $current_attrs;
73
74         $current_tag = $name;
75         $current_attrs = $attrs;
76
77         if ($name == "ENTRY") {
78             $this->inside_entry = true;
79         } elseif ($this->inside_content) {
80             $this->content .= $this->serialize_tag(strtolower($name), $attrs);
81         } elseif ($name == "CONTENT") {
82             $this->inside_content = true;
83         }
84     }
85
86     function tag_close($parser, $name, $attrs='') {
87         if ($name == "AUTHOR") {
88             $an_author = $this->trim_data(array(
89                 "name" => $this->name,
90                 "email" => $this->email,
91                 "uri" => $this->uri
92             ));
93             if ($this->inside_entry) {
94                 $this->authors[] = $an_author;
95             } else {
96                 $this->feed_authors[] = $an_author;
97             }
98             $this->name = '';
99             $this->email = '';
100             $this->uri = '';
101         } elseif ($name == "FEED") {
102             $this->feed[] = $this->trim_data(array(
103                 "id" => $this->feed_id,
104                 "title" => $this->feed_title,
105                 "links" => $this->feed_links,
106                 "subtitle" => $this->feed_subtitle,
107                 "updated" => $this->feed_updated,
108                 "generator" => $this->generator,
109                 "icon" => $this->icon,
110                 "rights" => $this->rights,
111                 "logo" => $this->logo,
112                 "authors" => $this->feed_authors,
113                 "contributors" => $this->feed_contributors
114             ));
115             $this->feed_title = '';
116             $this->feed_id = '';
117             $this->feed_links = array();
118             $this->feed_subtitle = '';
119             $this->feed_updated = '';
120             $this->feed_authors = array();
121             $this->feed_contributors = array();
122             $this->generator = '';
123             $this->icon = '';
124             $this->rights = '';
125             $this->logo = '';
126         } elseif ($name == "ENTRY") {
127             $this->entries[] = $this->trim_data(array(
128                 "id" => $this->id,
129                 "title" => $this->title,
130                 "updated" => $this->updated,
131                 "links" => $this->links,
132                 "published" => $this->published,
133                 "content" => $this->content,
134                 "summary" => $this->summary,
135                 "authors" => $this->authors,
136                 "contributors" => $this->contributors
137             ));
138             $this->id = '';
139             $this->title = '';
140             $this->updated = '';
141             $this->links = '';
142             $this->published = '';
143             $this->content = '';
144             $this->authors = array();
145             $this->contributors = array();
146             $this->inside_entry = false;
147         } elseif ($name == "CONTENT") {
148             $this->inside_content = false;
149         } elseif ($name == "CONTRIBUTOR") {
150             $a_contributor = $this->trim_data(array(
151                 "name" => $this->name,
152                 "email" => $this->email
153             ));
154             if ($this->inside_entry) {
155                 $this->contributors[] = $a_contributor;
156             } else {
157                 $this->feed_contributors[] = $a_contributor;
158             }
159             $this->name = '';
160             $this->email = '';
161         } elseif ($this->inside_content) {
162             $this->content .= "</" . strtolower($name) . ">";
163         }
164     }
165
166     function cdata($parser, $data) {
167         global $current_tag, $current_attrs;
168
169         if ($this->inside_content) {
170             $this->content .= $data;
171         } else {
172             switch ($current_tag) {
173                 case "ID":
174                     if ($this->inside_entry)
175                         $this->id .= $data;
176                     else
177                         $this->feed_id .= $data;
178                     break;
179                 case "LINK":
180                     $a_link = array();
181                     foreach ($current_attrs as $k => $v) {
182                         $a_link[strtolower($k)] = $v;
183                     }
184                     if ($this->inside_entry) {
185                         $this->links[] = $a_link;
186                     } else {
187                         $this->feed_links[] = $a_link;
188                     }
189                     break;
190                 case "NAME":
191                     $this->name .= $data;
192                     break;
193                 case "EMAIL":
194                     $this->email .= $data;
195                     break;
196                 case "TITLE" :
197                     if ($this->inside_entry)
198                         $this->title .= $data;
199                     else
200                         $this->feed_title .= $data;
201                     break;
202                 case "UPDATED":
203                     if ($this->inside_entry)
204                         $this->updated .= $data;
205                     else
206                         $this->feed_updated .= $data;
207                     break;
208                 case "SUBTITLE":
209                     $this->feed_subtitle .= $data;
210                     break;
211                 case "PUBLISHED":
212                     $this->published .= $data;
213                     break;
214                 case "SUMMARY":
215                     $this->summary .= $data;
216                     break;
217                 case "URI":
218                     $this->uri .= $data;
219                     break;
220                 case "GENERATOR":
221                     $this->generator .= $data;
222                     break;
223                 case "ICON":
224                     $this->icon .= $data;
225                     break;
226                 case "LOGO":
227                     $this->logo .= $data;
228                     break;
229                 case "RIGHTS":
230                     $this->rights .= $data;
231                     break;
232             }
233         }
234     }
235
236     function trim_data($array) {
237         return array_map(array("self", "trim_element"), $array);
238     }
239
240     function trim_element($element) {
241         if (is_array($element)) {
242             return $this->trim_data($element);
243         } elseif (is_string($element)) {
244             return trim($element);
245         }
246     }
247
248     function serialize_tag($tag_name, $attributes) {
249         $tag = "<" . $tag_name;
250         foreach ($attributes as $k => $v) {
251             $tag .= " " . strtolower($k). "=\"$v\"";
252         }
253         $tag .= ">";
254         return $tag;
255     }
256 }
257 ?>