/usr/share/php/arc/store/ARC2_StoreDeleteQueryHandler.php is in libarc-php 2.2.5-1ubuntu1.
This file is owned by root:root, with mode 0o644.
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 224 225 226 227 228 229 230 231 232 233 234 | <?php
/**
* ARC2 RDF Store DELETE Query Handler
*
* @author Benjamin Nowack <bnowack@semsol.com>
* @license http://arc.semsol.org/license
* @homepage <http://arc.semsol.org/>
* @package ARC2
*/
ARC2::inc('StoreQueryHandler');
class ARC2_StoreDeleteQueryHandler extends ARC2_StoreQueryHandler {
function __construct($a, &$caller) {/* caller has to be a store */
parent::__construct($a, $caller);
}
function __init() {/* db_con */
parent::__init();
$this->store = $this->caller;
$this->handler_type = 'delete';
}
/* */
function runQuery($infos) {
$this->infos = $infos;
$con = $this->store->getDBCon();
$t1 = ARC2::mtime();
/* delete */
$this->refs_deleted = false;
/* graph(s) only */
if (!$this->v('construct_triples', array(), $this->infos['query'])) {
$tc = $this->deleteTargetGraphs();
}
/* graph(s) + explicit triples */
elseif (!$this->v('pattern', array(), $this->infos['query'])) {
$tc = $this->deleteTriples();
}
/* graph(s) + constructed triples */
else {
$tc = $this->deleteConstructedGraph();
}
$t2 = ARC2::mtime();
/* clean up */
if ($tc && ($this->refs_deleted || (rand(1, 100) == 1))) $this->cleanTableReferences();
if ($tc && (rand(1, 100) == 1)) $this->store->optimizeTables();
if ($tc && (rand(1, 500) == 1)) $this->cleanValueTables();
$t3 = ARC2::mtime();
$index_dur = round($t3 - $t2, 4);
$dur = round($t3 - $t1, 4);
return array(
't_count' => $tc,
'delete_time' => $dur,
'index_update_time' => $index_dur,
);
}
/* */
function deleteTargetGraphs() {
$tbl_prefix = $this->store->getTablePrefix();
$r = 0;
$con = $this->store->getDBCon();
foreach ($this->infos['query']['target_graphs'] as $g) {
if ($g_id = $this->getTermID($g, 'g')) {
$rs = mysqli_query( $con, 'DELETE FROM ' . $tbl_prefix . 'g2t WHERE g = ' .$g_id);
$r += mysqli_affected_rows($con);
}
}
$this->refs_deleted = $r ? 1 : 0;
return $r;
}
/* */
function deleteTriples() {
$r = 0;
$dbv = $this->store->getDBVersion();
$tbl_prefix = $this->store->getTablePrefix();
$con = $this->store->getDBCon();
/* graph restriction */
$tgs = $this->infos['query']['target_graphs'];
$gq = '';
foreach ($tgs as $g) {
if ($g_id = $this->getTermID($g, 'g')) {
$gq .= $gq ? ', ' . $g_id : $g_id;
}
}
$gq = $gq ? ' AND G.g IN (' . $gq . ')' : '';
/* triples */
foreach ($this->infos['query']['construct_triples'] as $t) {
$q = '';
$skip = 0;
foreach (array('s', 'p', 'o') as $term) {
if (isset($t[$term . '_type']) && preg_match('/(var)/', $t[$term . '_type'])) {
//$skip = 1;
}
else {
$term_id = $this->getTermID($t[$term], $term);
$q .= ($q ? ' AND ' : '') . 'T.' . $term . '=' . $term_id;
/* explicit lang/dt restricts the matching */
if ($term == 'o') {
$o_lang = $this->v1('o_lang', '', $t);
$o_lang_dt = $this->v1('o_datatype', $o_lang, $t);
if ($o_lang_dt) {
$q .= ($q ? ' AND ' : '') . 'T.o_lang_dt=' . $this->getTermID($o_lang_dt, 'lang_dt');
}
}
}
}
if ($skip) {
continue;
}
if ($gq) {
$sql = ($dbv < '04-01') ? 'DELETE ' . $tbl_prefix . 'g2t' : 'DELETE G';
$sql .= '
FROM ' . $tbl_prefix . 'g2t G
JOIN ' . $this->getTripleTable() . ' T ON (T.t = G.t' . $gq . ')
WHERE ' . $q . '
';
$this->refs_deleted = 1;
}
else {/* triples only */
$sql = ($dbv < '04-01') ? 'DELETE ' . $this->getTripleTable() : 'DELETE T';
$sql .= ' FROM ' . $this->getTripleTable() . ' T WHERE ' . $q;
}
//$rs = mysql_query($sql, $con);
$rs = $this->queryDB($sql, $con);
$er = mysqli_error($con);
if (!empty($er)) {
$this->addError($er .' in ' . $sql);
}
$r += mysqli_affected_rows($con);
}
return $r;
}
/* */
function deleteConstructedGraph() {
ARC2::inc('StoreConstructQueryHandler');
$h = new ARC2_StoreConstructQueryHandler($this->a, $this->store);
$sub_r = $h->runQuery($this->infos);
$triples = ARC2::getTriplesFromIndex($sub_r);
$tgs = $this->infos['query']['target_graphs'];
$this->infos = array('query' => array('construct_triples' => $triples, 'target_graphs' => $tgs));
return $this->deleteTriples();
}
/* */
function cleanTableReferences() {
/* lock */
if (!$this->store->getLock()) return $this->addError('Could not get lock in "cleanTableReferences"');
$con = $this->store->getDBCon();
$tbl_prefix = $this->store->getTablePrefix();
$dbv = $this->store->getDBVersion();
/* check for unconnected triples */
$sql = '
SELECT T.t FROM '. $tbl_prefix . 'triple T LEFT JOIN '. $tbl_prefix . 'g2t G ON ( G.t = T.t )
WHERE G.t IS NULL LIMIT 1
';
if (($rs = mysqli_query( $con, $sql)) && mysqli_num_rows($rs)) {
/* delete unconnected triples */
$sql = ($dbv < '04-01') ? 'DELETE ' . $tbl_prefix . 'triple' : 'DELETE T';
$sql .= '
FROM ' . $tbl_prefix . 'triple T
LEFT JOIN ' . $tbl_prefix . 'g2t G ON (G.t = T.t)
WHERE G.t IS NULL
';
mysqli_query( $con, $sql);
}
/* check for unconnected graph refs */
if ((rand(1, 10) == 1)) {
$sql = '
SELECT G.g FROM '. $tbl_prefix . 'g2t G LEFT JOIN '. $tbl_prefix . 'triple T ON ( T.t = G.t )
WHERE T.t IS NULL LIMIT 1
';
if (($rs = mysqli_query( $con, $sql)) && mysqli_num_rows($rs)) {
/* delete unconnected graph refs */
$sql = ($dbv < '04-01') ? 'DELETE ' . $tbl_prefix . 'g2t' : 'DELETE G';
$sql .= '
FROM ' . $tbl_prefix . 'g2t G
LEFT JOIN ' . $tbl_prefix . 'triple T ON (T.t = G.t)
WHERE T.t IS NULL
';
mysqli_query( $con, $sql);
}
}
/* release lock */
$this->store->releaseLock();
}
/* */
function cleanValueTables() {
/* lock */
if (!$this->store->getLock()) return $this->addError('Could not get lock in "cleanValueTables"');
$con = $this->store->getDBCon();
$tbl_prefix = $this->store->getTablePrefix();
$dbv = $this->store->getDBVersion();
/* o2val */
$sql = ($dbv < '04-01') ? 'DELETE ' . $tbl_prefix . 'o2val' : 'DELETE V';
$sql .= '
FROM ' . $tbl_prefix . 'o2val V
LEFT JOIN ' . $tbl_prefix . 'triple T ON (T.o = V.id)
WHERE T.t IS NULL
';
mysqli_query( $con, $sql);
/* s2val */
$sql = ($dbv < '04-01') ? 'DELETE ' . $tbl_prefix . 's2val' : 'DELETE V';
$sql .= '
FROM ' . $tbl_prefix . 's2val V
LEFT JOIN ' . $tbl_prefix . 'triple T ON (T.s = V.id)
WHERE T.t IS NULL
';
mysqli_query( $con, $sql);
/* id2val */
$sql = ($dbv < '04-01') ? 'DELETE ' . $tbl_prefix . 'id2val' : 'DELETE V';
$sql .= '
FROM ' . $tbl_prefix . 'id2val V
LEFT JOIN ' . $tbl_prefix . 'g2t G ON (G.g = V.id)
LEFT JOIN ' . $tbl_prefix . 'triple T1 ON (T1.p = V.id)
LEFT JOIN ' . $tbl_prefix . 'triple T2 ON (T2.o_lang_dt = V.id)
WHERE G.g IS NULL AND T1.t IS NULL AND T2.t IS NULL
';
//mysql_query($sql, $con);
}
/* */
}
|