5 * @author Benjamin Nowack <bnowack@semsol.com>
6 * @license http://arc.semsol.org/license
7 * @homepage <http://arc.semsol.org/>
14 class ARC2_Store extends ARC2_Class {
16 function __construct($a = '', &$caller) {
17 parent::__construct($a, $caller);
20 function ARC2_Store($a = '', &$caller) {
21 $this->__construct($a, $caller);
24 function __init() {/* db_con */
26 $this->table_lock = 0;
27 $this->triggers = $this->v('store_triggers', array(), $this->a);
28 $this->queue_queries = $this->v('store_queue_queries', 0, $this->a);
29 $this->is_win = (strtolower(substr(PHP_OS, 0, 3)) == 'win') ? true : false;
30 $this->max_split_tables = $this->v('store_max_split_tables', 10, $this->a);
31 $this->split_predicates = $this->v('store_split_predicates', array(), $this->a);
37 return $this->v('store_name', 'arc', $this->a);
40 function getTablePrefix() {
41 if (!isset($this->tbl_prefix)) {
42 $r = $this->v('db_table_prefix', '', $this->a);
44 $r .= $this->getName() . '_';
45 $this->tbl_prefix = $r;
47 return $this->tbl_prefix;;
52 function createDBCon() {
53 foreach (array('db_host' => 'localhost', 'db_user' => '', 'db_pwd' => '', 'db_name' => '') as $k => $v) {
54 $this->a[$k] = $this->v($k, $v, $this->a);
56 if (!$db_con = mysql_connect($this->a['db_host'], $this->a['db_user'], $this->a['db_pwd'])) {
57 return $this->addError(mysql_error());
59 $this->a['db_con'] =& $db_con;
60 if (!mysql_select_db($this->a['db_name'], $db_con)) {
61 return $this->addError(mysql_error($db_con));
63 if (preg_match('/^utf8/', $this->getCollation())) {
64 $this->queryDB("SET NAMES 'utf8'", $db_con);
69 function getDBCon($force = 0) {
70 if ($force || !isset($this->a['db_con'])) {
71 if (!$this->createDBCon()) {
75 if (!$force && !@mysql_thread_id($this->a['db_con'])) return $this->getDBCon(1);
76 return $this->a['db_con'];
79 function closeDBCon() {
80 if ($this->v('db_con', false, $this->a)) {
81 @mysql_close($this->a['db_con']);
83 unset($this->a['db_con']);
86 function getDBVersion() {
87 if (!$this->v('db_version')) {
88 $this->db_version = preg_match("/^([0-9]+)\.([0-9]+)\.([0-9]+)/", mysql_get_server_info($this->getDBCon()), $m) ? sprintf("%02d-%02d-%02d", $m[1], $m[2], $m[3]) : '00-00-00';
90 return $this->db_version;
95 function getCollation() {
96 $rs = $this->queryDB('SHOW TABLE STATUS LIKE "' . $this->getTablePrefix(). 'setting"', $this->getDBCon());
97 return ($rs && ($row = mysql_fetch_array($rs)) && isset($row['Collation'])) ? $row['Collation'] : '';
100 function getColumnType() {
101 if (!$this->v('column_type')) {
102 $tbl = $this->getTablePrefix() . 'g2t';
103 $rs = $this->queryDB('SHOW COLUMNS FROM ' . $tbl . ' LIKE "t"', $this->getDBCon());
104 $row = $rs ? mysql_fetch_array($rs) : array('Type' => 'mediumint');
105 $this->column_type = preg_match('/mediumint/', $row['Type']) ? 'mediumint' : 'int';
107 return $this->column_type;
112 function hasHashColumn($tbl) {
113 $var_name = 'has_hash_column_' . $tbl;
114 if (!isset($this->$var_name)) {
115 $tbl = $this->getTablePrefix() . $tbl;
116 $rs = $this->queryDB('SHOW COLUMNS FROM ' . $tbl . ' LIKE "val_hash"', $this->getDBCon());
117 $this->$var_name = ($rs && mysql_fetch_array($rs));
119 return $this->$var_name;
124 function hasFulltextIndex() {
125 if (!isset($this->has_fulltext_index)) {
126 $this->has_fulltext_index = 0;
127 $tbl = $this->getTablePrefix() . 'o2val';
128 $rs = $this->queryDB('SHOW INDEX FROM ' . $tbl, $this->getDBCon());
129 while ($row = mysql_fetch_array($rs)) {
130 if ($row['Column_name'] != 'val') continue;
131 if ($row['Index_type'] != 'FULLTEXT') continue;
132 $this->has_fulltext_index = 1;
136 return $this->has_fulltext_index;
139 function enableFulltextSearch() {
140 if ($this->hasFulltextIndex()) return 1;
141 $tbl = $this->getTablePrefix() . 'o2val';
142 $this->queryDB('CREATE FULLTEXT INDEX vft ON ' . $tbl . '(val(128))', $this->getDBCon(), 1);
145 function disableFulltextSearch() {
146 if (!$this->hasFulltextIndex()) return 1;
147 $tbl = $this->getTablePrefix() . 'o2val';
148 $this->queryDB('DROP INDEX vft ON ' . $tbl, $this->getDBCon());
153 function countDBProcesses() {
154 return ($rs = $this->queryDB('SHOW PROCESSLIST', $this->getDBCon())) ? mysql_num_rows($rs) : 0;
159 function getTables() {
160 return array('triple', 'g2t', 'id2val', 's2val', 'o2val', 'setting');
166 if (($con = $this->getDBCon())) {
167 $tbl = $this->getTablePrefix() . 'setting';
168 return $this->queryDB("SELECT 1 FROM " . $tbl . " LIMIT 0", $con) ? 1 : 0;
172 function setUp($force = 0) {
173 if (($force || !$this->isSetUp()) && ($con = $this->getDBCon())) {
174 if ($this->getDBVersion() < '04-00-04') {
176 return $this->addError('MySQL version not supported. ARC requires version 4.0.4 or higher.');
178 ARC2::inc('StoreTableManager');
179 $mgr = new ARC2_StoreTableManager($this->a, $this);
180 $mgr->createTables();
184 function extendColumns() {
185 ARC2::inc('StoreTableManager');
186 $mgr = new ARC2_StoreTableManager($this->a, $this);
187 $mgr->extendColumns();
188 $this->column_type = 'int';
191 function splitTables() {
192 ARC2::inc('StoreTableManager');
193 $mgr = new ARC2_StoreTableManager($this->a, $this);
199 function hasSetting($k) {
200 $tbl = $this->getTablePrefix() . 'setting';
201 $sql = "SELECT val FROM " . $tbl . " WHERE k = '" .md5($k). "'";
202 $rs = $this->queryDB($sql, $this->getDBCon());
203 return ($rs && ($row = mysql_fetch_array($rs))) ? 1 : 0;
206 function getSetting($k, $default = 0) {
207 $tbl = $this->getTablePrefix() . 'setting';
208 $sql = "SELECT val FROM " . $tbl . " WHERE k = '" .md5($k). "'";
209 $rs = $this->queryDB($sql, $this->getDBCon());
210 if ($rs && ($row = mysql_fetch_array($rs))) {
211 return unserialize($row['val']);
216 function setSetting($k, $v) {
217 $con = $this->getDBCon();
218 $tbl = $this->getTablePrefix() . 'setting';
219 if ($this->hasSetting($k)) {
220 $sql = "UPDATE " .$tbl . " SET val = '" . mysql_real_escape_string(serialize($v), $con) . "' WHERE k = '" . md5($k) . "'";
223 $sql = "INSERT INTO " . $tbl . " (k, val) VALUES ('" . md5($k) . "', '" . mysql_real_escape_string(serialize($v), $con) . "')";
225 return $this->queryDB($sql, $con);
228 function removeSetting($k) {
229 $tbl = $this->getTablePrefix() . 'setting';
230 return $this->queryDB("DELETE FROM " . $tbl . " WHERE k = '" . md5($k) . "'", $this->getDBCon());
233 function getQueueTicket() {
234 if (!$this->queue_queries) return 1;
235 $t = 'ticket_' . substr(md5(uniqid(rand())), 0, 10);
236 $con = $this->getDBCon();
238 $rs = $this->queryDB('LOCK TABLES ' . $this->getTablePrefix() . 'setting WRITE', $con);
240 $queue = $this->getSetting('query_queue', array());
242 $this->setSetting('query_queue', $queue);
243 $this->queryDB('UNLOCK TABLES', $con);
246 $queue = $this->getSetting('query_queue', array());
247 while ($queue && ($queue[0] != $t) && ($lc < 30)) {
256 $queue = $this->getSetting('query_queue', array());
258 return ($lc < 30) ? $t : 0;
261 function removeQueueTicket($t) {
262 if (!$this->queue_queries) return 1;
263 $con = $this->getDBCon();
265 $this->queryDB('LOCK TABLES ' . $this->getTablePrefix() . 'setting WRITE', $con);
267 $vals = $this->getSetting('query_queue', array());
268 $pos = array_search($t, $vals);
269 $queue = ($pos < (count($vals) - 1)) ? array_slice($vals, $pos + 1) : array();
270 $this->setSetting('query_queue', $queue);
271 $this->queryDB('UNLOCK TABLES', $con);
276 function reset($keep_settings = 0) {
277 $con = $this->getDBCon();
278 $tbls = $this->getTables();
279 $prefix = $this->getTablePrefix();
280 /* remove split tables */
281 $ps = $this->getSetting('split_predicates', array());
282 foreach ($ps as $p) {
283 $tbl = 'triple_' . abs(crc32($p));
284 $this->queryDB('DROP TABLE ' . $prefix . $tbl, $con);
286 $this->removeSetting('split_predicates');
287 /* truncate tables */
288 foreach ($tbls as $tbl) {
289 if ($keep_settings && ($tbl == 'setting')) {
292 $this->queryDB('TRUNCATE ' . $prefix . $tbl, $con);
297 $con = $this->getDBCon();
298 $tbls = $this->getTables();
299 $prefix = $this->getTablePrefix();
300 foreach ($tbls as $tbl) {
301 $this->queryDB('DROP TABLE ' . $prefix . $tbl, $con);
305 function insert($doc, $g, $keep_bnode_ids = 0) {
306 $doc = is_array($doc) ? $this->toTurtle($doc) : $doc;
307 $infos = array('query' => array('url' => $g, 'target_graph' => $g));
308 ARC2::inc('StoreLoadQueryHandler');
309 $h =& new ARC2_StoreLoadQueryHandler($this->a, $this);
310 $r = $h->runQuery($infos, $doc, $keep_bnode_ids);
311 $this->processTriggers('insert', $infos);
315 function delete($doc, $g) {
317 $infos = array('query' => array('target_graphs' => array($g)));
318 ARC2::inc('StoreDeleteQueryHandler');
319 $h =& new ARC2_StoreDeleteQueryHandler($this->a, $this);
320 $r = $h->runQuery($infos);
321 $this->processTriggers('delete', $infos);
326 function replace($doc, $g, $doc_2) {
327 return array($this->delete($doc, $g), $this->insert($doc_2, $g));
331 ARC2::inc('StoreDumper');
332 $d =& new ARC2_StoreDumper($this->a, $this);
336 function createBackup($path, $q = '') {
337 ARC2::inc('StoreDumper');
338 $d =& new ARC2_StoreDumper($this->a, $this);
339 $d->saveSPOG($path, $q);
342 function renameTo($name) {
343 $con = $this->getDBCon();
344 $tbls = $this->getTables();
345 $old_prefix = $this->getTablePrefix();
346 $new_prefix = $this->v('db_table_prefix', '', $this->a);
347 $new_prefix .= $new_prefix ? '_' : '';
348 $new_prefix .= $name . '_';
349 foreach ($tbls as $tbl) {
350 $rs = $this->queryDB('RENAME TABLE ' . $old_prefix . $tbl .' TO ' . $new_prefix . $tbl, $con);
351 if ($er = mysql_error($con)) {
352 return $this->addError($er);
355 $this->a['store_name'] = $name;
356 unset($this->tbl_prefix);
359 function replicateTo($name) {
360 $conf = array_merge($this->a, array('store_name' => $name));
361 $new_store = ARC2::getStore($conf);
364 $con = $this->getDBCon();
365 $tbls = $this->getTables();
366 $old_prefix = $this->getTablePrefix();
367 $new_prefix = $new_store->getTablePrefix();
368 foreach ($tbls as $tbl) {
369 $rs = $this->queryDB('INSERT IGNORE INTO ' . $new_prefix . $tbl .' SELECT * FROM ' . $old_prefix . $tbl, $con);
370 if ($er = mysql_error($con)) {
371 return $this->addError($er);
374 return $new_store->query('SELECT COUNT(*) AS t_count WHERE { ?s ?p ?o}', 'row');
379 function query($q, $result_format = '', $src = '', $keep_bnode_ids = 0, $log_query = 0) {
380 if ($log_query) $this->logQuery($q);
381 $con = $this->getDBCon();
382 if (preg_match('/^dump/i', $q)) {
383 $infos = array('query' => array('type' => 'dump'));
386 ARC2::inc('SPARQLPlusParser');
387 $p = & new ARC2_SPARQLPlusParser($this->a, $this);
389 $infos = $p->getQueryInfos();
391 if ($result_format == 'infos') return $infos;
392 $infos['result_format'] = $result_format;
393 if (!isset($p) || !$p->getErrors()) {
394 $qt = $infos['query']['type'];
395 if (!in_array($qt, array('select', 'ask', 'describe', 'construct', 'load', 'insert', 'delete', 'dump'))) {
396 return $this->addError('Unsupported query type "'.$qt.'"');
399 $r = array('query_type' => $qt, 'result' => $this->runQuery($infos, $qt, $keep_bnode_ids, $q));
401 $r['query_time'] = $t2 - $t1;
403 if ($result_format == 'raw') {
406 if ($result_format == 'rows') {
407 return $r['result']['rows'] ? $r['result']['rows'] : array();
409 if ($result_format == 'row') {
410 return $r['result']['rows'] ? $r['result']['rows'][0] : array();
417 function runQuery($infos, $type, $keep_bnode_ids = 0, $q = '') {
418 ARC2::inc('Store' . ucfirst($type) . 'QueryHandler');
419 $cls = 'ARC2_Store' . ucfirst($type) . 'QueryHandler';
420 $h =& new $cls($this->a, $this);
423 if ($q && ($type == 'select')) $ticket = $this->getQueueTicket($q);
425 if ($type == 'load') {/* the LoadQH supports raw data as 2nd parameter */
426 $r = $h->runQuery($infos, '', $keep_bnode_ids);
429 $r = $h->runQuery($infos, $keep_bnode_ids);
432 if ($q && ($type == 'select')) $this->removeQueueTicket($ticket);
433 $trigger_r = $this->processTriggers($type, $infos);
437 function processTriggers($type, $infos) {
439 $trigger_defs = $this->triggers;
440 $this->triggers = array();
441 if ($triggers = $this->v($type, array(), $trigger_defs)) {
442 $r['trigger_results'] = array();
443 $triggers = is_array($triggers) ? $triggers : array($triggers);
444 $trigger_inc_path = $this->v('store_triggers_path', '', $this->a);
445 foreach ($triggers as $trigger) {
446 $trigger .= !preg_match('/Trigger$/', $trigger) ? 'Trigger' : '';
447 if (ARC2::inc(ucfirst($trigger), $trigger_inc_path)) {
448 $cls = 'ARC2_' . ucfirst($trigger);
449 $config = array_merge($this->a, array('query_infos' => $infos));
450 $trigger_obj = new $cls($config, $this);
451 if (method_exists($trigger_obj, 'go')) {
452 $r['trigger_results'][] = $trigger_obj->go();
457 $this->triggers = $trigger_defs;
463 function getValueHash($val) {
464 return abs(crc32($val));
467 function getTermID($val, $term = '') {
468 $tbl = preg_match('/^(s|o)$/', $term) ? $term . '2val' : 'id2val';
469 $con = $this->getDBCon();
471 if (preg_match('/^(s2val|o2val)$/', $tbl) && $this->hasHashColumn($tbl)) {
472 $sql = "SELECT id, val FROM " . $this->getTablePrefix() . $tbl . " WHERE val_hash = '" . $this->getValueHash($val) . "'";
473 if (($rs = $this->queryDB($sql, $con)) && mysql_num_rows($rs)) {
474 while ($row = mysql_fetch_array($rs)) {
475 if ($row['val'] == $val) {
483 $sql = "SELECT id FROM " . $this->getTablePrefix() . $tbl . " WHERE val = BINARY '" . mysql_real_escape_string($val, $con) . "' LIMIT 1";
484 if (($rs = $this->queryDB($sql, $con)) && mysql_num_rows($rs) && ($row = mysql_fetch_array($rs))) {
491 function getIDValue($id, $term = '') {
492 $tbl = preg_match('/^(s|o)$/', $term) ? $term . '2val' : 'id2val';
493 $con = $this->getDBCon();
494 $sql = "SELECT val FROM " . $this->getTablePrefix() . $tbl . " WHERE id = " . mysql_real_escape_string($id, $con) . " LIMIT 1";
495 if (($rs = $this->queryDB($sql, $con)) && mysql_num_rows($rs) && ($row = mysql_fetch_array($rs))) {
503 function getLock($t_out = 10, $t_out_init = '') {
504 if (!$t_out_init) $t_out_init = $t_out;
505 $con = $this->getDBCon();
506 $l_name = $this->a['db_name'] . '.' . $this->getTablePrefix() . '.write_lock';
507 if ($rs = $this->queryDB('SELECT IS_FREE_LOCK("' . $l_name. '") AS success', $con)) {
508 $row = mysql_fetch_array($rs);
509 if (!$row['success']) {
512 return $this->getLock($t_out - 1, $t_out_init);
515 elseif ($rs = $this->queryDB('SELECT GET_LOCK("' . $l_name. '", ' . $t_out_init. ') AS success', $con)) {
516 $row = mysql_fetch_array($rs);
517 return $row['success'];
523 function releaseLock() {
524 $con = $this->getDBCon();
525 return $this->queryDB('DO RELEASE_LOCK("' . $this->a['db_name'] . '.' . $this->getTablePrefix() . '.write_lock")', $con);
530 function processTables($level = 2, $operation = 'optimize') {/* 1: triple + g2t, 2: triple + *2val, 3: all tables */
531 $con = $this->getDBCon();
532 $pre = $this->getTablePrefix();
533 $tbls = $this->getTables();
535 foreach ($tbls as $tbl) {
536 if (($level < 3) && preg_match('/(backup|setting)$/', $tbl)) continue;
537 if (($level < 2) && preg_match('/(val)$/', $tbl)) continue;
538 $sql .= $sql ? ', ' : strtoupper($operation) . ' TABLE ';
541 $this->queryDB($sql, $con);
542 if ($err = mysql_error($con)) $this->addError($err . ' in ' . $sql);
545 function optimizeTables($level = 2) {
546 return $this->processTables($level, 'optimize');
549 function checkTables($level = 2) {
550 return $this->processTables($level, 'check');
553 function repairTables($level = 2) {
554 return $this->processTables($level, 'repair');
559 function changeNamespaceURI($old_uri, $new_uri) {
560 ARC2::inc('StoreHelper');
561 $c = new ARC2_StoreHelper($this->a, $this);
562 return $c->changeNamespaceURI($old_uri, $new_uri);
567 function getResourceLabel($res, $unnamed_label = 'An unnamed resource') {
568 if (!isset($this->resource_labels)) $this->resource_labels = array();
569 if (isset($this->resource_labels[$res])) return $this->resource_labels[$res];
570 if (!preg_match('/^[a-z0-9\_]+\:[^\s]+$/si', $res)) return $res;/* literal */
571 $ps = $this->getLabelProps();
572 if ($this->getSetting('store_label_properties', '-') != md5(serialize($ps))) {
573 $this->inferLabelProps($ps);
575 //$sub_q .= $sub_q ? ' || ' : '';
576 //$sub_q .= 'REGEX(str(?p), "(last_name|name|fn|title|label)$", "i")';
577 $q = 'SELECT ?label WHERE { <' . $res . '> ?p ?label . ?p a <http://semsol.org/ns/arc#LabelProperty> } LIMIT 3';
579 if ($rows = $this->query($q, 'rows')) {
580 foreach ($rows as $row) {
581 $r = strlen($row['label']) > strlen($r) ? $row['label'] : $r;
584 if (!$r && preg_match('/^\_\:/', $res)) {
585 return $unnamed_label;
587 $r = $r ? $r : preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('#self', '', $res));
588 $r = str_replace('_', ' ', $r);
589 $r = preg_replace('/([a-z])([A-Z])/e', '"\\1 " . strtolower("\\2")', $r);
590 $this->resource_labels[$res] = $r;
594 function getLabelProps() {
596 $this->v('rdf_label_properties' , array(), $this->a),
598 'http://www.w3.org/2000/01/rdf-schema#label',
599 'http://xmlns.com/foaf/0.1/name',
600 'http://purl.org/dc/elements/1.1/title',
601 'http://purl.org/rss/1.0/title',
602 'http://www.w3.org/2004/02/skos/core#prefLabel',
603 'http://xmlns.com/foaf/0.1/nick',
608 function inferLabelProps($ps) {
609 $this->query('DELETE FROM <label-properties>');
611 foreach ($ps as $p) {
612 $sub_q .= ' <' . $p . '> a <http://semsol.org/ns/arc#LabelProperty> . ';
614 $this->query('INSERT INTO <label-properties> { ' . $sub_q. ' }');
615 $this->setSetting('store_label_properties', md5(serialize($ps)));
620 function getResourcePredicates($res) {
622 if ($rows = $this->query('SELECT DISTINCT ?p WHERE { <' . $res . '> ?p ?o . }', 'rows')) {
623 foreach ($rows as $row) {
624 $r[$row['p']] = array();
630 function getDomains($p) {
632 foreach($this->query('SELECT DISTINCT ?type WHERE {?s <' . $p . '> ?o ; a ?type . }', 'rows') as $row) {
638 function getPredicateRange($p) {
639 $row = $this->query('SELECT ?val WHERE {<' . $p . '> rdfs:range ?val . } LIMIT 1', 'row');
640 return $row ? $row['val'] : '';
645 function logQuery($q) {
646 $fp = @fopen("arc_query_log.txt", "a");
647 @fwrite($fp, date('Y-m-d\TH:i:s\Z', time()) . ' : ' . $q . '' . "\n\n");