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