3 * Atom Syndication Format PHP Library
6 * @link http://code.google.com/p/phpatomlib/
8 * @author Elias Torres <elias@torrez.us>
14 * Structure that store common Atom Feed Properties
30 var $categories = array();
37 var $entries = array();
41 * Structure that store Atom Entry Properties
57 var $categories = array();
61 * AtomLib Atom Parser API
67 var $NS = 'http://www.w3.org/2005/Atom';
68 var $ATOM_CONTENT_ELEMENTS = array('content','summary','title','subtitle','rights');
69 var $ATOM_SIMPLE_ELEMENTS = array('id','updated','published','draft');
76 var $ns_contexts = array();
77 var $ns_decls = array();
78 var $content_ns_decls = array();
79 var $content_ns_contexts = array();
80 var $is_xhtml = false;
83 var $skipped_div = false;
85 var $FILE = "php://input";
93 function __construct() {
95 $this->feed = new AtomFeed();
96 $this->current = null;
97 $this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";');
98 $this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";');
104 public function AtomParser() {
110 print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";
114 function error_handler($log_level, $log_text, $error_file, $error_line) {
115 $this->error = $log_text;
120 set_error_handler(array(&$this, 'error_handler'));
122 array_unshift($this->ns_contexts, array());
124 $parser = xml_parser_create_ns();
125 xml_set_object($parser, $this);
126 xml_set_element_handler($parser, "start_element", "end_element");
127 xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
128 xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
129 xml_set_character_data_handler($parser, "cdata");
130 xml_set_default_handler($parser, "_default");
131 xml_set_start_namespace_decl_handler($parser, "start_ns");
132 xml_set_end_namespace_decl_handler($parser, "end_ns");
138 $fp = fopen($this->FILE, "r");
139 while ($data = fread($fp, 4096)) {
140 if($this->debug) $this->content .= $data;
142 if(!xml_parse($parser, $data, feof($fp))) {
143 /* translators: 1: error message, 2: line number */
144 trigger_error(sprintf(__('XML Error: %1$s at line %2$s')."\n",
145 xml_error_string(xml_get_error_code($parser)),
146 xml_get_current_line_number($parser)));
153 xml_parser_free($parser);
155 restore_error_handler();
160 function start_element($parser, $name, $attrs) {
162 $tag = array_pop(split(":", $name));
165 case $this->NS . ':feed':
166 $this->current = $this->feed;
168 case $this->NS . ':entry':
169 $this->current = new AtomEntry();
173 $this->_p("start_element('$name')");
174 #$this->_p(print_r($this->ns_contexts,true));
175 #$this->_p('current(' . $this->current . ')');
177 array_unshift($this->ns_contexts, $this->ns_decls);
181 if(!empty($this->in_content)) {
183 $this->content_ns_decls = array();
185 if($this->is_html || $this->is_text)
186 trigger_error("Invalid content in element found. Content must not be of type text or html if it contains markup.");
188 $attrs_prefix = array();
190 // resolve prefixes for attributes
191 foreach($attrs as $key => $value) {
192 $with_prefix = $this->ns_to_prefix($key, true);
193 $attrs_prefix[$with_prefix[1]] = $this->xml_escape($value);
196 $attrs_str = join(' ', array_map($this->map_attrs_func, array_keys($attrs_prefix), array_values($attrs_prefix)));
197 if(strlen($attrs_str) > 0) {
198 $attrs_str = " " . $attrs_str;
201 $with_prefix = $this->ns_to_prefix($name);
203 if(!$this->is_declared_content_ns($with_prefix[0])) {
204 array_push($this->content_ns_decls, $with_prefix[0]);
208 if(count($this->content_ns_decls) > 0) {
209 array_unshift($this->content_ns_contexts, $this->content_ns_decls);
210 $xmlns_str .= join(' ', array_map($this->map_xmlns_func, array_keys($this->content_ns_contexts[0]), array_values($this->content_ns_contexts[0])));
211 if(strlen($xmlns_str) > 0) {
212 $xmlns_str = " " . $xmlns_str;
216 array_push($this->in_content, array($tag, $this->depth, "<". $with_prefix[1] ."{$xmlns_str}{$attrs_str}" . ">"));
218 } else if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS) || in_array($tag, $this->ATOM_SIMPLE_ELEMENTS)) {
219 $this->in_content = array();
220 $this->is_xhtml = $attrs['type'] == 'xhtml';
221 $this->is_html = $attrs['type'] == 'html' || $attrs['type'] == 'text/html';
222 $this->is_text = !in_array('type',array_keys($attrs)) || $attrs['type'] == 'text';
223 $type = $this->is_xhtml ? 'XHTML' : ($this->is_html ? 'HTML' : ($this->is_text ? 'TEXT' : $attrs['type']));
225 if(in_array('src',array_keys($attrs))) {
226 $this->current->$tag = $attrs;
228 array_push($this->in_content, array($tag,$this->depth, $type));
230 } else if($tag == 'link') {
231 array_push($this->current->links, $attrs);
232 } else if($tag == 'category') {
233 array_push($this->current->categories, $attrs);
236 $this->ns_decls = array();
239 function end_element($parser, $name) {
241 $tag = array_pop(split(":", $name));
243 $ccount = count($this->in_content);
245 # if we are *in* content, then let's proceed to serialize it
246 if(!empty($this->in_content)) {
247 # if we are ending the original content element
248 # then let's finalize the content
249 if($this->in_content[0][0] == $tag &&
250 $this->in_content[0][1] == $this->depth) {
251 $origtype = $this->in_content[0][2];
252 array_shift($this->in_content);
253 $newcontent = array();
254 foreach($this->in_content as $c) {
256 array_push($newcontent, $c[2]);
258 if($this->is_xhtml || $this->is_text) {
259 array_push($newcontent, $this->xml_escape($c));
261 array_push($newcontent, $c);
265 if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS)) {
266 $this->current->$tag = array($origtype, join('',$newcontent));
268 $this->current->$tag = join('',$newcontent);
270 $this->in_content = array();
271 } else if($this->in_content[$ccount-1][0] == $tag &&
272 $this->in_content[$ccount-1][1] == $this->depth) {
273 $this->in_content[$ccount-1][2] = substr($this->in_content[$ccount-1][2],0,-1) . "/>";
275 # else, just finalize the current element's content
276 $endtag = $this->ns_to_prefix($name);
277 array_push($this->in_content, array($tag, $this->depth, "</$endtag[1]>"));
281 array_shift($this->ns_contexts);
285 if($name == ($this->NS . ':entry')) {
286 array_push($this->feed->entries, $this->current);
287 $this->current = null;
290 $this->_p("end_element('$name')");
293 function start_ns($parser, $prefix, $uri) {
294 $this->_p("starting: " . $prefix . ":" . $uri);
295 array_push($this->ns_decls, array($prefix,$uri));
298 function end_ns($parser, $prefix) {
299 $this->_p("ending: #" . $prefix . "#");
302 function cdata($parser, $data) {
303 $this->_p("data: #" . str_replace(array("\n"), array("\\n"), trim($data)) . "#");
304 if(!empty($this->in_content)) {
305 array_push($this->in_content, $data);
309 function _default($parser, $data) {
310 # when does this gets called?
314 function ns_to_prefix($qname, $attr=false) {
315 # split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div')
316 $components = split(":", $qname);
318 # grab the last one (e.g 'div')
319 $name = array_pop($components);
321 if(!empty($components)) {
322 # re-join back the namespace component
323 $ns = join(":",$components);
324 foreach($this->ns_contexts as $context) {
325 foreach($context as $mapping) {
326 if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
327 return array($mapping, "$mapping[0]:$name");
334 return array(null, $name);
336 foreach($this->ns_contexts as $context) {
337 foreach($context as $mapping) {
338 if(strlen($mapping[0]) == 0) {
339 return array($mapping, $name);
346 function is_declared_content_ns($new_mapping) {
347 foreach($this->content_ns_contexts as $context) {
348 foreach($context as $mapping) {
349 if($new_mapping == $mapping) {
357 function xml_escape($string)
359 return str_replace(array('&','"',"'",'<','>'),
360 array('&','"',''','<','>'),