www-apps/pmwiki - cookbook - xmlpagestore - version number
[anomen-overlay.git] / www-apps / pmwiki / cookbook / XMLPageStore / XMLPageStore.php
blobd66c794ef730d8bf1b6c52bcfc9dc25c2c255062
1 <?php if (!defined('PmWiki')) exit();
3 /*
4 XMLPageStore
6 Copyright 2011 Anomen (ludek_h@seznam.cz)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 $RecipeInfo['XMLPageStore']['Version'] = '2012-03-04';
14 SDV($EnablePageStoreXML,false);
17 class XMLPageStore extends PageStore {
19 function read_xml($data, $since=0)
21 global $Charset;
22 $p = xml_parser_create();
23 xml_parser_set_option($p,XML_OPTION_CASE_FOLDING, false);
24 xml_parser_set_option($p,XML_OPTION_TARGET_ENCODING, $Charset);
26 # bug http://bugs.php.net/bug.php?id=33240
27 # xml_parser_set_option($p,XML_OPTION_SKIP_WHITE, true);
29 xml_parse_into_struct($p, $data, $vals, $index);
30 xml_parser_free($p);
31 foreach($vals as $v) {
32 if (($v['type'] == 'complete') && ($v['level'] == 2)) {
33 $k = $v['tag'];
34 $t = $v['attributes']['time'];
35 $p = $v['attributes']['prev'];
38 if (!empty($t)) {
39 if ($since > 0 && $t < $since) {
40 continue;
42 $k = "$k:$t";
44 if (!empty($p)) {
45 $k = "$k:$p:";
47 $page[$k] = $v['value'];
50 return @$page;
53 function read($pagename, $since=0) {
54 $newline = '';
55 $urlencoded = false;
56 $pagefile = $this->pagefile($pagename);
57 if ($pagefile && ($fp=@fopen($pagefile, "r"))) {
58 while (!feof($fp)) {
59 $line = fgets($fp, 512);
60 $isXML = (substr($line,0,5) == '<?xml') || (substr(ltrim($line),0,5) == '<page');
61 if (trim($line) != '') {
62 break; // we found non-empty line
65 if ($isXML) {
66 fseek($fp, 0, SEEK_SET);
67 $pagefilesize = filesize($pagefile);
68 $data = fread($fp, $pagefilesize);
69 @fclose($fp);
70 return $this->read_xml($data, $since);
71 } else {
72 @fclose($fp);
73 return parent::read($pagename,$page);
78 function write($pagename,$page) {
79 global $EnablePageStoreXML;
80 ($EnablePageStoreXML == true) ? $this->write_xml($pagename,$page) : parent::write($pagename,$page);
83 function write_xml($pagename,$page) {
84 global $Now, $Version, $Charset;
85 $page['name'] = $pagename;
86 $page['time'] = $Now;
87 $page['host'] = $_SERVER['REMOTE_ADDR'];
88 $page['agent'] = @$_SERVER['HTTP_USER_AGENT'];
89 $page['rev'] = @$page['rev']+1;
90 unset($page['version']); unset($page['newline']);
91 uksort($page, 'CmpPageAttr');
92 $s = false;
93 $pagefile = $this->pagefile($pagename);
94 $dir = dirname($pagefile); mkdirp($dir);
95 if ($pagefile && ($fp=fopen("$pagefile,new","w"))) {
96 $x = "<?xml version=\"1.0\" encoding=\"$Charset\"?>\n<page xmlns=\"http://www.pmwiki.org/cookbook/xmlpage\" version=\"$Version\">\n";
97 $s = true && fputs($fp, $x); $sz = strlen($x);
98 foreach($page as $k=>$v)
99 if ($k > '' && $k{0} != '=') {
100 $v = htmlspecialchars($v, ENT_NOQUOTES, $Charset);
101 if (preg_match("/^([a-z]+)(?::(\d+))(?::(\d+):)?$/", $k, $m)) {
102 $p = empty($m[3]) ? "" : " prev=\"${m[3]}\"";
103 $x = "<${m[1]} time=\"${m[2]}\"$p>$v</${m[1]}>\n";
104 } else {
105 $x = "<$k>$v</$k>\n";
107 $s = $s && fputs($fp, $x); $sz += strlen($x);
109 $x = "</page>\n";
110 $s = $s && fputs($fp, $x); $sz += strlen($x);
112 $s = fclose($fp) && $s;
113 $s = $s && (filesize("$pagefile,new") > $sz * 0.95);
114 if (file_exists($pagefile)) $s = $s && unlink($pagefile);
115 $s = $s && rename("$pagefile,new", $pagefile);
117 $s && fixperms($pagefile);
118 if (!$s)
119 Abort("Cannot write page to $pagename ($pagefile)...changes not saved");
120 PCache($pagename, $page);