5 * @author Benjamin Nowack
6 * @license <http://arc.semsol.org/license>
7 * @homepage <http://arc.semsol.org/>
14 class ARC2_StoreDumper extends ARC2_Class {
16 function __construct($a = '', &$caller) {
17 parent::__construct($a, $caller);
20 function ARC2_StoreDumper($a = '', &$caller) {
21 $this->__construct($a, $caller);
26 $this->store =& $this->caller;
27 $this->keep_time_limit = $this->v('keep_time_limit', 0, $this->a);
28 $this->limit = 100000;
34 header('Content-Type: application/sparql-results+xml');
35 if ($this->v('store_use_dump_dir', 0, $this->a)) {
36 $path = $this->v('store_dump_dir', 'dumps', $this->a);
37 /* default: monthly dumps */
38 $path_suffix = $this->v('store_dump_suffix', date('Y_m'), $this->a);
39 $path .= '/dump_' . $path_suffix . '.spog';
40 if (!file_exists($path)) {
41 $this->saveSPOG($path);
46 echo $this->getHeader();
50 $rs = $this->getRecordset($offset);
52 while ($row = mysql_fetch_array($rs)) {
53 echo $this->getEntry($row);
56 $offset += $this->limit;
58 echo $this->getFooter();
63 function saveSPOG($path, $q = '') {
64 if ($q) return $this->saveCustomSPOG($path, $q);
65 if (!$fp = @fopen($path, 'w')) return $this->addError('Could not create backup file at ' . realpath($path));
66 fwrite($fp, $this->getHeader());
70 $rs = $this->getRecordset($offset);
72 while ($row = mysql_fetch_array($rs)) {
73 fwrite($fp, $this->getEntry($row));
76 $offset += $this->limit;
78 fwrite($fp, $this->getFooter());
85 function saveCustomSPOG($path, $q) {
86 if (!$fp = @fopen($path, 'w')) return $this->addError('Could not create backup file at ' . realpath($path));
87 fwrite($fp, $this->getHeader());
88 $rows = $this->store->query($q, 'rows');
89 foreach ($rows as $row) {
90 fwrite($fp, $this->getEntry($row));
92 fwrite($fp, $this->getFooter());
98 function getRecordset($offset) {
99 $prefix = $this->store->getTablePrefix();
100 $con = $this->store->getDBCon();
104 T.s_type AS `s type`,
108 T.o_type AS `o type`,
109 VLDT.val as `o lang_dt`,
113 ' . $prefix . 'triple T
114 JOIN ' . $prefix . 's2val VS ON (T.s = VS.id)
115 JOIN ' . $prefix . 'id2val VP ON (T.p = VP.id)
116 JOIN ' . $prefix . 'o2val VO ON (T.o = VO.id)
117 JOIN ' . $prefix . 'id2val VLDT ON (T.o_lang_dt = VLDT.id)
118 JOIN ' . $prefix . 'g2t G2T ON (T.t = G2T.t)
119 JOIN ' . $prefix . 'id2val VG ON (G2T.g = VG.id)
121 if ($this->limit) $sql .= ' LIMIT ' . $this->limit;
122 if ($offset) $sql .= ' OFFSET ' . $offset;
123 $rs = mysql_unbuffered_query($sql, $con);
124 if (($err = mysql_error($con))) {
125 return $this->addError($err);
132 function getHeader() {
135 '<?xml version="1.0"?>' .
136 $n . '<sparql xmlns="http://www.w3.org/2005/sparql-results#">' .
138 $n . ' <variable name="s"/>' .
139 $n . ' <variable name="p"/>' .
140 $n . ' <variable name="o"/>' .
141 $n . ' <variable name="g"/>' .
147 function getEntry($row) {
148 if (!$this->keep_time_limit) @set_time_limit($this->v('time_limit', 1200, $this->a));
151 $r .= $n . ' <result>';
152 foreach (array('s', 'p', 'o', 'g') as $var) {
153 if (isset($row[$var])) {
154 $type = (string) $row[$var . ' type'];
155 $r .= $n . ' <binding name="' . $var . '">';
156 $val = $this->toUTF8($row[$var]);
157 if (($type == '0') || ($type == 'uri')) {
158 $r .= $n . ' <uri>' . $this->getSafeValue($val) . '</uri>';
160 elseif (($type == '1') || ($type == 'bnode')) {
161 $r .= $n . ' <bnode>' . substr($val, 2) . '</bnode>';
165 foreach (array('o lang_dt', 'o lang', 'o datatype') as $k) {
166 if (($var == 'o') && isset($row[$k]) && $row[$k]) $lang_dt = $row[$k];
168 $is_lang = preg_match('/^([a-z]+(\-[a-z0-9]+)*)$/i', $lang_dt);
169 list($lang, $dt) = $is_lang ? array($lang_dt, '') : array('', $lang_dt);
170 $lang = $lang ? ' xml:lang="' . $lang . '"' : '';
171 $dt = $dt ? ' datatype="' . htmlspecialchars($dt) . '"' : '';
172 $r .= $n . ' <literal' . $dt . $lang . '>' . $this->getSafeValue($val) . '</literal>';
174 $r .= $n . ' </binding>';
177 $r .= $n . ' </result>';
181 function getSafeValue($val) {/* mainly for fixing json_decode bugs */
202 $froms = array_keys($mappings);
203 $tos = array_values($mappings);
204 foreach ($froms as $i => $from) $froms[$i] = urldecode($from);
205 $val = str_replace($froms, $tos, $val);
206 if (strpos($val, '</') !== false) {
207 $val = "\n<![CDATA[\n" . $val . "\n]]>\n";
210 $val = htmlspecialchars($val);
215 function getFooter() {