/usr/share/php/arc/store/ARC2_StoreDumper.php is in libarc-php 2.2.5-1ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | <?php
/**
* ARC2 Store Dumper
*
* @author Benjamin Nowack
* @license <http://arc.semsol.org/license>
* @homepage <http://arc.semsol.org/>
* @package ARC2
* @version 2010-11-16
*/
ARC2::inc('Class');
class ARC2_StoreDumper extends ARC2_Class {
function __construct($a, &$caller) {
parent::__construct($a, $caller);
}
function __init() {
parent::__init();
$this->store = $this->caller;
$this->keep_time_limit = $this->v('keep_time_limit', 0, $this->a);
$this->limit = 100000;
}
/* */
function dumpSPOG() {
header('Content-Type: application/sparql-results+xml');
if ($this->v('store_use_dump_dir', 0, $this->a)) {
$path = $this->v('store_dump_dir', 'dumps', $this->a);
/* default: monthly dumps */
$path_suffix = $this->v('store_dump_suffix', date('Y_m'), $this->a);
$path .= '/dump_' . $path_suffix . '.spog';
if (!file_exists($path)) {
$this->saveSPOG($path);
}
readfile($path);
exit;
}
echo $this->getHeader();
$offset = 0;
do {
$proceed = 0;
$rs = $this->getRecordset($offset);
if (!$rs) break;
while ($row = mysqli_fetch_array($rs)) {
echo $this->getEntry($row);
$proceed = 1;
}
$offset += $this->limit;
} while ($proceed);
echo $this->getFooter();
}
/* */
function saveSPOG($path, $q = '') {
if ($q) return $this->saveCustomSPOG($path, $q);
if (!$fp = @fopen($path, 'w')) return $this->addError('Could not create backup file at ' . realpath($path));
fwrite($fp, $this->getHeader());
$offset = 0;
do {
$proceed = 0;
$rs = $this->getRecordset($offset);
if (!$rs) break;
while ($row = mysqli_fetch_array($rs)) {
fwrite($fp, $this->getEntry($row));
$proceed = 1;
}
$offset += $this->limit;
} while ($proceed);
fwrite($fp, $this->getFooter());
@fclose($fp);
return 1;
}
/* */
function saveCustomSPOG($path, $q) {
if (!$fp = @fopen($path, 'w')) return $this->addError('Could not create backup file at ' . realpath($path));
fwrite($fp, $this->getHeader());
$rows = $this->store->query($q, 'rows');
foreach ($rows as $row) {
fwrite($fp, $this->getEntry($row));
}
fwrite($fp, $this->getFooter());
@fclose($fp);
}
/* */
function getRecordset($offset) {
$prefix = $this->store->getTablePrefix();
$con = $this->store->getDBCon();
$sql = '
SELECT
VS.val AS s,
T.s_type AS `s type`,
VP.val AS p,
0 AS `p type`,
VO.val AS o,
T.o_type AS `o type`,
VLDT.val as `o lang_dt`,
VG.val as g,
0 AS `g type`
FROM
' . $prefix . 'triple T
JOIN ' . $prefix . 's2val VS ON (T.s = VS.id)
JOIN ' . $prefix . 'id2val VP ON (T.p = VP.id)
JOIN ' . $prefix . 'o2val VO ON (T.o = VO.id)
JOIN ' . $prefix . 'id2val VLDT ON (T.o_lang_dt = VLDT.id)
JOIN ' . $prefix . 'g2t G2T ON (T.t = G2T.t)
JOIN ' . $prefix . 'id2val VG ON (G2T.g = VG.id)
';
if ($this->limit) $sql .= ' LIMIT ' . $this->limit;
if ($offset) $sql .= ' OFFSET ' . $offset;
$rs = mysqli_query( $con, $sql, MYSQLI_USE_RESULT);
$err = mysqli_error($con);
if (!empty($err)) {
return $this->addError($err);
}
return $rs;
}
/* */
function getHeader() {
$n = "\n";
return '' .
'<?xml version="1.0"?>' .
$n . '<sparql xmlns="http://www.w3.org/2005/sparql-results#">' .
$n . ' <head>' .
$n . ' <variable name="s"/>' .
$n . ' <variable name="p"/>' .
$n . ' <variable name="o"/>' .
$n . ' <variable name="g"/>' .
$n . ' </head>' .
$n . ' <results>' .
'';
}
function getEntry($row) {
if (!$this->keep_time_limit) @set_time_limit($this->v('time_limit', 1200, $this->a));
$n = "\n";
$r = '';
$r .= $n . ' <result>';
foreach (array('s', 'p', 'o', 'g') as $var) {
if (isset($row[$var])) {
$type = (string) $row[$var . ' type'];
$r .= $n . ' <binding name="' . $var . '">';
$val = $this->toUTF8($row[$var]);
if (($type == '0') || ($type == 'uri')) {
$r .= $n . ' <uri>' . $this->getSafeValue($val) . '</uri>';
}
elseif (($type == '1') || ($type == 'bnode')) {
$r .= $n . ' <bnode>' . substr($val, 2) . '</bnode>';
}
else {
$lang_dt = '';
foreach (array('o lang_dt', 'o lang', 'o datatype') as $k) {
if (($var == 'o') && isset($row[$k]) && $row[$k]) $lang_dt = $row[$k];
}
$is_lang = preg_match('/^([a-z]+(\-[a-z0-9]+)*)$/i', $lang_dt);
list($lang, $dt) = $is_lang ? array($lang_dt, '') : array('', $lang_dt);
$lang = $lang ? ' xml:lang="' . $lang . '"' : '';
$dt = $dt ? ' datatype="' . htmlspecialchars($dt) . '"' : '';
$r .= $n . ' <literal' . $dt . $lang . '>' . $this->getSafeValue($val) . '</literal>';
}
$r .= $n . ' </binding>';
}
}
$r .= $n . ' </result>';
return $r;
}
function getSafeValue($val) {/* mainly for fixing json_decode bugs */
$mappings = array(
'%00' => '',
'%01' => '',
'%02' => '',
'%03' => '',
'%04' => '',
'%05' => '',
'%06' => '',
'%07' => '',
'%08' => '',
'%09' => '',
'%0B' => '',
'%0C' => '',
'%0E' => '',
'%0F' => '',
'%15' => '',
'%17' => 'ė',
'%1A' => ',',
'%1F' => '',
);
$froms = array_keys($mappings);
$tos = array_values($mappings);
foreach ($froms as $i => $from) $froms[$i] = urldecode($from);
$val = str_replace($froms, $tos, $val);
if (strpos($val, '</') !== false) {
$val = "\n<![CDATA[\n" . $val . "\n]]>\n";
}
else {
$val = htmlspecialchars($val);
}
return $val;
}
function getFooter() {
$n = "\n";
return '' .
$n . ' </results>' .
$n . '</sparql>' .
$n .
'';
}
/* */
}
|