diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..3c44241c --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.php_cs b/.php_cs index f8bea4f1..7da6e235 100644 --- a/.php_cs +++ b/.php_cs @@ -14,6 +14,9 @@ return PhpCsFixer\Config::create() ->setFinder( PhpCsFixer\Finder::create() ->files() + ->in(__DIR__ . '/parsers') + ->in(__DIR__ . '/serializers') + ->in(__DIR__ . '/src') ->in(__DIR__ . '/store') ->name('*.php') ); diff --git a/.travis.yml b/.travis.yml index 0e0e9ea3..0de6b9cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,41 +1,193 @@ +sudo: required + language: php -dist: trusty -php: - - '5.6' - - '7.0' - - '7.1' - - '7.2' - - nightly +services: + - docker matrix: fast_finish: true include: - - php: hhvm - allow_failures: - - php: nightly - - php: hhvm + # + # Define versions of MySQL and MariaDB to test against. + # + # For each DB version also tests with different adapters are run (mysqli, PDO). + # + # + # mysql 5.5 + # + - php: 5.6 + env: DB=mysql:5.5 DB_ADAPTER=mysqli + - php: 5.6 + env: DB=mysql:5.5 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.0 + env: DB=mysql:5.5 DB_ADAPTER=mysqli + - php: 7.0 + env: DB=mysql:5.5 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.1 + env: DB=mysql:5.5 DB_ADAPTER=mysqli + - php: 7.1 + env: DB=mysql:5.5 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.2 + env: DB=mysql:5.5 DB_ADAPTER=mysqli + - php: 7.2 + env: DB=mysql:5.5 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + # + # mysql 5.6 + # + - php: 5.6 + env: DB=mysql:5.6 DB_ADAPTER=mysqli + - php: 5.6 + env: DB=mysql:5.6 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.0 + env: DB=mysql:5.6 DB_ADAPTER=mysqli + - php: 7.0 + env: DB=mysql:5.6 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.1 + env: DB=mysql:5.6 DB_ADAPTER=mysqli + - php: 7.1 + env: DB=mysql:5.6 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.2 + env: DB=mysql:5.6 DB_ADAPTER=mysqli + - php: 7.2 + env: DB=mysql:5.6 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + # + # mysql 5.7 + # + - php: 5.6 + env: DB=mysql:5.7 DB_ADAPTER=mysqli + - php: 5.6 + env: DB=mysql:5.7 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 5.6 + env: DB=mysql:5.7 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql CACHE_ENABLED=true + - php: 7.0 + env: DB=mysql:5.7 DB_ADAPTER=mysqli + - php: 7.0 + env: DB=mysql:5.7 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.0 + env: DB=mysql:5.7 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql CACHE_ENABLED=true + - php: 7.1 + env: DB=mysql:5.7 DB_ADAPTER=mysqli + - php: 7.1 + env: DB=mysql:5.7 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.1 + env: DB=mysql:5.7 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql CACHE_ENABLED=true + - php: 7.2 + env: DB=mysql:5.7 DB_ADAPTER=mysqli + - php: 7.2 + env: DB=mysql:5.7 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.2 + env: DB=mysql:5.7 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql CACHE_ENABLED=true + # + # mariadb 10 + # + - php: 5.6 + env: DB=mariadb:10.0 DB_ADAPTER=mysqli + - php: 5.6 + env: DB=mariadb:10.0 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.0 + env: DB=mariadb:10.0 DB_ADAPTER=mysqli + - php: 7.0 + env: DB=mariadb:10.0 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.1 + env: DB=mariadb:10.0 DB_ADAPTER=mysqli + - php: 7.1 + env: DB=mariadb:10.0 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.2 + env: DB=mariadb:10.0 DB_ADAPTER=mysqli + - php: 7.2 + env: DB=mariadb:10.0 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + # + # mariadb 10.1 + # + - php: 5.6 + env: DB=mariadb:10.1 DB_ADAPTER=mysqli + - php: 5.6 + env: DB=mariadb:10.1 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.0 + env: DB=mariadb:10.1 DB_ADAPTER=mysqli + - php: 7.0 + env: DB=mariadb:10.1 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.1 + env: DB=mariadb:10.1 DB_ADAPTER=mysqli + - php: 7.1 + env: DB=mariadb:10.1 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.2 + env: DB=mariadb:10.1 DB_ADAPTER=mysqli + - php: 7.2 + env: DB=mariadb:10.1 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + # + # mariadb 10.2 + # + - php: 5.6 + env: DB=mariadb:10.2 DB_ADAPTER=mysqli + - php: 5.6 + env: DB=mariadb:10.2 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.0 + env: DB=mariadb:10.2 DB_ADAPTER=mysqli + - php: 7.0 + env: DB=mariadb:10.2 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.1 + env: DB=mariadb:10.2 DB_ADAPTER=mysqli + - php: 7.1 + env: DB=mariadb:10.2 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.2 + env: DB=mariadb:10.2 DB_ADAPTER=mysqli + - php: 7.2 + env: DB=mariadb:10.2 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + # + # mariadb 10.3 + # + - php: 5.6 + env: DB=mariadb:10.3 DB_ADAPTER=mysqli + - php: 5.6 + env: DB=mariadb:10.3 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 5.6 + env: DB=mariadb:10.3 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql CACHE_ENABLED=true + - php: 7.0 + env: DB=mariadb:10.3 DB_ADAPTER=mysqli + - php: 7.0 + env: DB=mariadb:10.3 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.0 + env: DB=mariadb:10.3 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql CACHE_ENABLED=true + - php: 7.1 + env: DB=mariadb:10.3 DB_ADAPTER=mysqli + - php: 7.1 + env: DB=mariadb:10.3 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.1 + env: DB=mariadb:10.3 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql CACHE_ENABLED=true + - php: 7.2 + env: DB=mariadb:10.3 DB_ADAPTER=mysqli + - php: 7.2 + env: DB=mariadb:10.3 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql + - php: 7.2 + env: DB=mariadb:10.3 DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql CACHE_ENABLED=true git: depth: 1 cache: - directories: - - $HOME/.composer - - $HOME/bin + directories: + - $HOME/.composer/cache/files -sudo: false +before_script: + # + # install and init database (see matrix => include => env) + # + - ./.travis/install-and-init-db.sh -before_install: - - mysql -e 'CREATE DATABASE IF NOT EXISTS testdb;' + # + # setup and run tests + # # Install composer packages, will also trigger dump-autoload - - travis_retry composer install --no-interaction + - travis_retry composer install --no-interaction --prefer-dist # Install coveralls.phar - - wget -c -nc --retry-connrefused --tries=0 https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar + - wget -c -nc --retry-connrefused --tries=0 https://github.com/satooshi/php-coveralls/releases/download/v1.1.0/coveralls.phar - chmod +x coveralls.phar - php coveralls.phar --version -script: vendor/bin/phpunit --coverage-clover gen/coverage/clover.xml +script: + - vendor/bin/phpunit --coverage-clover gen/coverage/clover.xml after_success: # Submit coverage report to Coveralls servers, see .coveralls.yml diff --git a/.travis/install-and-init-db.sh b/.travis/install-and-init-db.sh new file mode 100755 index 00000000..13f56e22 --- /dev/null +++ b/.travis/install-and-init-db.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# +# acknowledgement: +# copied from https://github.com/PyMySQL/PyMySQL/blob/master/.travis/initializedb.sh +# + +# debug +set -x +# verbose +set -v + +if [ ! -z "${DB}" ]; then + # disable existing database server in case of accidential connection + sudo service mysql stop + + docker pull ${DB} + docker run -it --name=mysqld -d -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -p 3306:3306 ${DB} + sleep 10 + + mysql() { + docker exec mysqld mysql "${@}" + } + while : + do + sleep 5 + mysql -e 'select version()' + if [ $? = 0 ]; then + break + fi + echo "server logs" + docker logs --tail 5 mysqld + done + + mysql -e 'select VERSION()' + + if [ $DB == 'mysql:8.0' ]; then + WITH_PLUGIN='with mysql_native_password' + mysql -e 'SET GLOBAL local_infile=on' + docker cp mysqld:/var/lib/mysql/public_key.pem "${HOME}" + docker cp mysqld:/var/lib/mysql/ca.pem "${HOME}" + docker cp mysqld:/var/lib/mysql/server-cert.pem "${HOME}" + docker cp mysqld:/var/lib/mysql/client-key.pem "${HOME}" + docker cp mysqld:/var/lib/mysql/client-cert.pem "${HOME}" + else + WITH_PLUGIN='' + fi + + mysql -uroot -e 'create database testdb DEFAULT CHARACTER SET utf8mb4' +else + cat ~/.my.cnf + + mysql -e 'select VERSION()' + mysql -e 'create database testdb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;' +fi diff --git a/ARC2_Class.php b/ARC2_Class.php index 53bcb7d7..48763898 100644 --- a/ARC2_Class.php +++ b/ARC2_Class.php @@ -9,13 +9,14 @@ */ class ARC2_Class { - + protected $db_object; + function __construct($a, &$caller) { $this->a = is_array($a) ? $a : array(); $this->caller = $caller; $this->__init(); } - + function __init() {/* base, time_limit */ if (!$_POST && isset($GLOBALS['HTTP_RAW_POST_DATA'])) parse_str($GLOBALS['HTTP_RAW_POST_DATA'], $_POST); /* php5 bug */ $this->inc_path = ARC2::getIncPath(); @@ -34,7 +35,7 @@ function __init() {/* base, time_limit */ } /* */ - + function v($name, $default = false, $o = false) {/* value if set */ if ($o === false) $o = $this; if (is_array($o)) { @@ -42,7 +43,7 @@ function v($name, $default = false, $o = false) {/* value if set */ } return isset($o->$name) ? $o->$name : $default; } - + function v1($name, $default = false, $o = false) {/* value if 1 (= not empty) */ if ($o === false) $o = $this; if (is_array($o)) { @@ -50,7 +51,7 @@ function v1($name, $default = false, $o = false) {/* value if 1 (= not empty) */ } return (isset($o->$name) && $o->$name) ? $o->$name : $default; } - + function m($name, $a = false, $default = false, $o = false) {/* call method */ if ($o === false) $o = $this; return method_exists($o, $name) ? $o->$name($a) : $default; @@ -119,7 +120,7 @@ function getPrettyURL($r) { } /* */ - + function addError($v) { if (!in_array($v, $this->errors)) { $this->errors[] = $v; @@ -133,11 +134,11 @@ function addError($v) { } return false; } - + function getErrors() { return $this->errors; } - + function getWarnings() { return $this->warnings; } @@ -148,9 +149,9 @@ function resetErrors() { $this->caller->resetErrors(); } } - + /* */ - + function splitURI($v) { return ARC2::splitURI($v); } @@ -198,7 +199,7 @@ function setPrefix($prefix, $ns) { $this->nsp[$ns] = $prefix; return $this; } - + function getPrefix($ns) { if (!isset($this->nsp[$ns])) { $this->ns['ns' . $this->ns_count] = $ns; @@ -245,7 +246,7 @@ function expandPNames($index) { } /* */ - + function calcURI($path, $base = "") { /* quick check */ if (preg_match("/^[a-z0-9\_]+\:/i", $path)) {/* abs path or bnode */ @@ -290,9 +291,9 @@ function calcURI($path, $base = "") { } return $base . $path; } - + /* */ - + function calcBase($path) { $r = $path; $r = preg_replace('/\#.*$/', '', $r);/* remove hash */ @@ -319,7 +320,7 @@ function getResource($uri, $store_or_props = '') { } return $res; } - + function toIndex($v) { if (is_array($v)) { if (isset($v[0]) && isset($v[0]['s'])) return ARC2::getSimpleIndex($v, 0); @@ -358,14 +359,14 @@ function toNTriples($v, $ns = '', $raw = 0) { $ser = new ARC2_NTriplesSerializer(array_merge($this->a, array('ns' => $ns)), $this); return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw); } - + function toTurtle($v, $ns = '', $raw = 0) { ARC2::inc('TurtleSerializer'); if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array(); $ser = new ARC2_TurtleSerializer(array_merge($this->a, array('ns' => $ns)), $this); return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw); } - + function toRDFXML($v, $ns = '', $raw = 0) { ARC2::inc('RDFXMLSerializer'); if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array(); @@ -424,7 +425,7 @@ function getFilledTemplate($t, $vals, $g = '') { $parser->parse($g, $this->getTurtleHead() . $t); return $parser->getSimpleIndex(0, $vals); } - + function getTurtleHead() { $r = ''; $ns = $this->v('ns', array(), $this->a); @@ -433,7 +434,7 @@ function getTurtleHead() { } return $r; } - + function completeQuery($q, $ns = '') { if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array(); $added_prefixes = array(); @@ -468,7 +469,7 @@ function fromDataURI($str) { function checkRegex($str) { return addslashes($str); // @@todo extend } - + /* Microdata methods */ function getMicrodataAttrs($id, $type = '') { @@ -480,25 +481,65 @@ function mdAttrs($id, $type = '') { return $this->getMicrodataAttrs($id, $type); } - /* central DB query hook */ + /* central DB query hook */ + + public function getDBObjectFromARC2Class($con = null) + { + if (null == $this->db_object) { + if (false === class_exists('\\ARC2\\Store\\Adapter\\AdapterFactory')) { + require __DIR__.'/src/ARC2/Store/Adapter/AdapterFactory.php'; + } + if (false == isset($this->a['db_adapter'])) { + $this->a['db_adapter'] = 'mysqli'; + } + $factory = new \ARC2\Store\Adapter\AdapterFactory(); + $this->db_object = $factory->getInstanceFor($this->a['db_adapter'], $this->a); + if ($con) { + $this->db_object->connect($con); + } else { + $this->db_object->connect(); + } + } + return $this->db_object; + } + + /** + * Dont use this function to directly query the database. It currently works only with mysqli DB adapter. + * + * @param string $sql SQL query + * @param mysqli $con Connection + * @param int $log_errors 1 if you want to log errors. Default is 0 + * + * @return mysqli Result + * + * @deprecated since 2.4.0 + */ + public function queryDB($sql, $con, $log_errors = 0) + { + $t1 = ARC2::mtime(); + + // create connection using an adapter, if not available yet + $this->getDBObjectFromARC2Class($con); + + $r = $this->db_object->mysqliQuery($sql); + + // TODO check if this is ever called. it seems not and therefore could be removed. + if (0) { + $t2 = ARC2::mtime() - $t1; + $call_obj = $this; + $call_path = ''; + while ($call_obj) { + $call_path = get_class($call_obj) . ' / ' . $call_path; + $call_obj = isset($call_obj->caller) ? $call_obj->caller : false; + } + echo "\n" . $call_path . " needed " . $t2 . ' secs for ' . str_replace("\n" , ' ', $sql);; + } - function queryDB($sql, $con, $log_errors = 0) { - $t1 = ARC2::mtime(); - $r = mysqli_query( $con, $sql); - if (0) { - $t2 = ARC2::mtime() - $t1; - $call_obj = $this; - $call_path = ''; - while ($call_obj) { - $call_path = get_class($call_obj) . ' / ' . $call_path; - $call_obj = isset($call_obj->caller) ? $call_obj->caller : false; - } - echo "\n" . $call_path . " needed " . $t2 . ' secs for ' . str_replace("\n" , ' ', $sql);; + if ($log_errors && !empty($this->db_object->getErrorMessage())) { + $this->addError($this->db_object->getErrorMessage()); + } + return $r; } - $er = mysqli_error($con); - if ($log_errors && !empty($er)) $this->addError($er); - return $r; - } /** * Shortcut method to create an RDF/XML backup dump from an RDF Store object. diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..cbd60bad --- /dev/null +++ b/LICENSE @@ -0,0 +1,282 @@ +# LICENSE + +ARC2 is dual-licensed, under the following 2 licenses: + +* W3C Software License (https://www.w3.org/Consortium/Legal/2002/copyright-software-20021231) +* GPL 2 or later (http://www.gnu.org/copyleft/gpl.html) + +In the following you will find the license texts of both licenses. +First from the W3C Software License, afterwards from the GPL. + + + + +----------------------------------------------------------------------------------------------- +W3C Software License +----------------------------------------------------------------------------------------------- + +This work (and included software, documentation such as READMEs, or other related items) is being provided by the copyright holders under the following license. + +License +By obtaining, using and/or copying this work, you (the licensee) agree that you have read, understood, and will comply with the following terms and conditions. + +Permission to copy, modify, and distribute this software and its documentation, with or without modification, for any purpose and without fee or royalty is hereby granted, provided that you include the following on ALL copies of the software and documentation or portions thereof, including modifications: + +The full text of this NOTICE in a location viewable to users of the redistributed or derivative work. +Any pre-existing intellectual property disclaimers, notices, or terms and conditions. If none exist, the W3C Software Short Notice should be included (hypertext is preferred, text is permitted) within the body of any redistributed or derivative code. +Notice of any changes or modifications to the files, including the date changes were made. (We recommend you provide URIs to the location from which the code is derived.) +Disclaimers +THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. + +COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION. + +The name and trademarks of copyright holders may NOT be used in advertising or publicity pertaining to the software without specific, written prior permission. Title to copyright in this software and any associated documentation will at all times remain with copyright holders. + + + + +----------------------------------------------------------------------------------------------- +GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 +----------------------------------------------------------------------------------------------- + +Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. + +Preamble + +The GNU General Public License is a free, copyleft license for software and other kinds of works. + +The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. + +When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. + +To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. + +For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. + +Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. + +For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. + +Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. + +Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. + +The precise terms and conditions for copying, distribution and modification follow. + +TERMS AND CONDITIONS + +0. Definitions. + +"This License" refers to version 3 of the GNU General Public License. + +"Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. + +"The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. + +To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. + +A "covered work" means either the unmodified Program or a work based on the Program. + +To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. + +To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. + +An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. + +1. Source Code. + +The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. + +A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. + +The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. + +The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. + +The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same work. + +2. Basic Permissions. + +All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. + +You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. + +Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. + +3. Protecting Users' Legal Rights From Anti-Circumvention Law. + +No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. + +When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. + +4. Conveying Verbatim Copies. + +You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. + +5. Conveying Modified Source Versions. + +You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: + +a) The work must carry prominent notices stating that you modified it, and giving a relevant date. + +b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". + +c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. + +d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. + +A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. + +6. Conveying Non-Source Forms. + +You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: + +a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. + +b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. + +c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. + +d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. + +e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. + +A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. + +A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. + +"Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. + +If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). + +The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. + +Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. + +7. Additional Terms. + +"Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. + +When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. + +Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: + +a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or + +b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or + +c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or + +d) Limiting the use for publicity purposes of names of licensors or authors of the material; or + +e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or + +f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. + +All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. + +Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. + +8. Termination. + +You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). + +However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. + +Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. + +Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. + +9. Acceptance Not Required for Having Copies. + +You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. + +10. Automatic Licensing of Downstream Recipients. + +Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. + +An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. + +You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. + +11. Patents. + +A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". + +A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. + +In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. + +If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. + +If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. + +A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. + +Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. + +12. No Surrender of Others' Freedom. + +If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. + +13. Use with the GNU Affero General Public License. + +Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. + +14. Revised Versions of this License. + +The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. + +If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. + +Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. + +15. Disclaimer of Warranty. + +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + +16. Limitation of Liability. + +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +17. Interpretation of Sections 15 and 16. + +If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. + +END OF TERMS AND CONDITIONS + +How to Apply These Terms to Your New Programs + +If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. + +To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. + + Copyright (C) + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + +If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: + + Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". + +You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . + +The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..a5f49321 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +default: + @echo "Please run make test to start test environment." + +test: + @echo "" + @echo "##########" + @echo "Unit Tests" + @echo "##########" + @echo "" + vendor/bin/phpunit --testsuite unit + # run DB adapter depended test using different adapters each time + # this allows us to check coverage using the same tests + @echo "" + @echo "" + @echo "##############################################" + @echo "mysqli adapter depended tests (Store uncached)" + @echo "##############################################" + @echo "" + DB_ADAPTER=mysqli vendor/bin/phpunit --testsuite db_adapter_depended + @echo "" + @echo "" + @echo "#########################################################" + @echo "PDO adapter depended tests (Store uncached, PDO uncached)" + @echo "#########################################################" + @echo "" + DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql vendor/bin/phpunit --testsuite db_adapter_depended + @echo "" + @echo "" + @echo "#####################################################" + @echo "PDO adapter depended tests (Store cached, PDO cached)" + @echo "#####################################################" + @echo "" + DB_ADAPTER=pdo DB_PDO_PROTOCOL=mysql CACHE_ENABLED=true vendor/bin/phpunit --testsuite db_adapter_depended diff --git a/README.md b/README.md index 6ef8d2db..da2545d0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -ARC2 -==== +# ARC2 [![Build](https://travis-ci.org/semsol/arc2.svg?branch=master)](https://travis-ci.org/semsol/arc2) [![Coverage Status](https://coveralls.io/repos/github/semsol/arc2/badge.svg?branch=master)](https://coveralls.io/github/semsol/arc2?branch=master) @@ -8,11 +7,7 @@ ARC2 [![Latest Unstable Version](https://poser.pugx.org/semsol/arc2/v/unstable.svg)](https://packagist.org/packages/semsol/arc2) [![License](https://poser.pugx.org/semsol/arc2/license.svg)](https://packagist.org/packages/semsol/arc2) -ARC2 is a PHP 5.6+ library for working with RDF (It is compatible with PHP 5.3+ but old versions are no longer tested). -It also provides a MySQL-based triplestore with SPARQL support. - -Feature-wise, ARC2 is now in a stable state with no further feature additions planned. -Issues are still being fixed and Pull Requests are welcome, though. +ARC2 is a PHP 5.6+ library for working with RDF. It also provides a MySQL-based triplestore with SPARQL support. ## Installation @@ -20,4 +15,105 @@ Package available on [Composer](https://packagist.org/packages/semsol/arc2). If you're using Composer to manage dependencies, you can use - $ composer require "semsol/arc2": "dev-master" +```bash +composer require semsol/arc2:2.4.* +``` + +### Branches + +`2.3.1` was latest stable version for a long time. But recent developments lead to version `2.4`, which is the next minor version of the 2.x-branch, containing stabilizations, wider test coverage and a couple of new features (e.g. PDO adapter for RDF store). Primary focus was to keep backward compatibility, so that an upgrade doesn't mess up your application. Its not clear how long the 2.x-branch will be maintained, so please consider upgrading to 3.x. + +![](doc/branches.png) + +Version `3.x` introduces new features and develops the backend further. Unfortunately, overall backward compatibility can not be maintained. One of the major changes is the transition from MyISAM to InnoDB table engine (RDF store). + +## Requirements + +#### PHP + +| 5.6 | 7.0 | 7.1 | 7.2 | +|:---------------------:|:------------------:|:------------------:|:------------------:| +| :heavy_check_mark:(1) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | + +(1) It is compatible with PHP 5.3+ but old versions are no longer tested. + +#### Database systems + +| | 5.5 | 5.6 | 5.7 | 8.0 | +|:---------:|:------------------:|:------------------:|:------------------:|:---------------:| +| **MySQL** | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :collision: (1) | + +| | 10.0 | 10.1 | 10.2 | 10.3 | +|:-----------:|:------------------:|:------------------:|:------------------:|:------------------:| +| **MariaDB** | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | + +(1) As long as ARC2 uses mysqli, a connection to MySQL Server 8.0 is not possible. For more information, please look [here](https://github.com/semsol/arc2/commit/0ad48d61753b15ae02ff19f615b14aa52b6557f1). But its planned to switch to PDO ([issue](https://github.com/semsol/arc2/issues/109)) + + +## RDF triple store + +### SPARQL support + +Please have a look into [SPARQL-support.md](doc/SPARQL-support.md) to see which SPARQL 1.0/1.1 features are currently supported. + +### Use cache + +The RDF store implementation provides a hash-based query cache. It works on two levels: SQL and SPARQL, which means, that it checks given SPARQL queries as well as internally generated SQL queries. + +To use it, just add the following to the database configuration: + +```php +$store = ARC2::getStore(array( + 'db_name' => 'testdb', + 'db_user' => 'root', + 'db_pwd' => '', + 'db_host' => '127.0.0.1', + // ... + 'cache_enabled' => true // <== activates cache +)); +``` + +Per default it uses a file based cache, which stores items in the default temp folder of the system (in Linux its usually `/tmp`). But you can use another cache solution, such as memcached. + +#### PSR-16 compatibility + +Our cache solution is [PSR-16](https://www.php-fig.org/psr/psr-16/) compatible, which means, that you can use your own cache instance. To do that, add the following to the database configuration: + +```php +$store = ARC2::getStore(array( + 'db_name' => 'testdb', + 'db_user' => 'root', + 'db_pwd' => '', + 'db_host' => '127.0.0.1', + // ... + 'cache_enabled' => true + 'cache_instance' => new ArrayCache() // <=== example Cache instance, managed by yourself +)); +``` + +ARC2 uses [Symfony Cache](https://symfony.com/doc/current/components/cache.html) , which provides many connectors out of the box ([Overview](https://github.com/symfony/cache/tree/master/Simple)). This allows you to attach ARC2 to a [Redis server](https://github.com/symfony/cache/blob/master/Simple/RedisCache.php), for instance. + +### Known problems/restrictions with database systems + +In this section you find known problems with MariaDB or MySQL, regarding certain features. E.g. MySQL 5.5 doesn't allow FULLTEXT indexes in InnoDB. We try to encapsulate any differences in the DB adapters, so that you don't have to care about them. In case you run into problems, this section might be of help. + +#### MySQL 8.0 and mysqli + +Using mysqli with MySQL 8.0 as backend throws the following exception: + +> mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] + +Based on this [source](https://mysqlserverteam.com/upgrading-to-mysql-8-0-default-authentication-plugin-considerations/), one has to change the my.cnf, adding the following entry: + +> [mysqld] +> default-authentication-plugin=mysql_native_password + +## Internal information for developers + +Please have a look [here](doc/developer.md) to find information about maintaining and extending ARC2. + +### Docker setup + +For ARC2 developers we recommend this following [Docker setup](https://github.com/k00ni/PHP-Apache-MySQL-Docker). It provides a pre-configured set of software (for PHP, DBS etc.) and allows quick switches between different software versions. + +For more information have a look [here](https://github.com/k00ni/PHP-Apache-MySQL-Docker). diff --git a/composer.json b/composer.json index 5ae6bd30..94bc04fb 100644 --- a/composer.json +++ b/composer.json @@ -1,18 +1,33 @@ { "name": "semsol/arc2", - "type": "library", - "description": "ARC2 RDF library for PHP", - "keywords": ["rdf","sparql"], - "homepage": "https://github.com/semsol/arc2", - "license": "W3C", + "type": "library", + "description": "ARC2 is a PHP 5.6+ library for working with RDF (It is compatible with PHP 5.3+ but old versions are no longer tested). It also provides a MySQL-based triplestore with SPARQL support.", + "keywords": ["rdf","sparql"], + "homepage": "https://github.com/semsol/arc2", + "license": [ + "GPL-2.0-or-later", + "W3C" + ], + "support": { + "issues": "https://github.com/semsol/arc2/issues" + }, "authors": [ { "name": "Benji Nowack", - "homepage": "http://bnowack.de/" + "homepage": "http://bnowack.de/", + "role": "Creator, Developer" + }, + { + "name": "Konrad Abicht", + "homepage": "https://pier-and-peer.com", + "email": "konrad.abicht@pier-and-peer.com", + "role": "Maintainer, Developer" } ], "require": { - "php": ">=5.3.0" + "php": ">=5.3.0", + "joshcam/mysqli-database-class": "2.*", + "symfony/cache": "3.4.*||4.*" }, "require-dev": { "doctrine/instantiator": "1.0.*", @@ -20,7 +35,7 @@ "phpunit/phpunit": "^5.0" }, "autoload": { - "classmap": ["parsers/", "serializers/"], + "classmap": ["parsers/", "serializers/", "store/"], "files": [ "./ARC2.php", "./ARC2_Class.php", @@ -29,7 +44,12 @@ "./ARC2_Graph.php", "./ARC2_Reader.php", "./ARC2_Resource.php" - ] + ], + "psr-4": { + "ARC2\\": [ + "src/ARC2/" + ] + } }, "autoload-dev": { "psr-4": { diff --git a/doc/SPARQL-support.md b/doc/SPARQL-support.md new file mode 100644 index 00000000..d38f713e --- /dev/null +++ b/doc/SPARQL-support.md @@ -0,0 +1,142 @@ +# SPARQL support + +**Source**: Based on the following ARC2 wiki page: https://github.com/semsol/arc2/wiki/SPARQL- + +## Introduction + +ARC supports all [SPARQL Query Language](http://www.w3.org/TR/rdf-sparql-query/) features ([to a certain extent](http://www.w3.org/2001/sw/DataAccess/tests/implementations)) and also a number of pragmatic extensions such as aggregates (AVG / COUNT / MAX / MIN / SUM) and write mechanisms. I tried to keep the changes to the SPARQL specification at a minimum, so that the existing grammar parser and store functionality can be re-used, and also to stick to ARC's flat learning curve. + +This page documents the core differences between SPARQL and what is called "SPARQL+" in ARC2. + +## SELECT + +### Aggregates +```sql +SELECT COUNT(?contact) AS ?contacts WHERE { + <#me> foaf:knows ?contact . +} +ORDER BY DESC(?contacts) +``` +Note that the alias (... AS ...) has to be specified. + + +If you have more than a single result variable, you also have to provide GROUP BY information: +```sql +SELECT ?who COUNT(?contact) AS ?contacts WHERE { + ?who foaf:knows ?contact . +} +GROUP BY ?who +``` + +ARC2 currently has a bug in the `SUM` ([100](https://github.com/semsol/arc2/issues/100)) and `AVG` ([99](https://github.com/semsol/arc2/issues/99)) function. + +#### Supported aggregate functions + +| | AVG | COUNT | MIN | MAX | SUM | +|:--------|:----------------------------------------------------------:|:-----:|:---:|:---:|:---:| +| Support | x (but [bugged](https://github.com/semsol/arc2/issues/99)) | x | x | x | x | + + +### Supported relational terms + +| | = | != | < | > | +|:--------|:-:|:--:|:-:|:-:| +| Support | x | x | x | x | + +### Supported FILTER functions + +| | bound | datatype | isBlank | isIri | isLiteral | isUri | lang | langMatches | regex | str | +|:--------|:-----:|:--------:|:-------:|:-----:|:---------:|:-----:|:----:|:-----------:|:-----:|:---:| +| Support | x | x | x | x | x | x | x | x | x | x | + +## LOAD +```sql +LOAD +``` +ARC can extract triples from a variety of formats such as RDF/XML, Turtle, and HTML (eRDF, RDFa, microformats, basic Dublin Core data, OpenID Hooks, Feed links). + + +It is possible to add data to an existing graph: +```sql +LOAD INTO +``` +(LOAD in SPARQL+ is syntactically compatible with SPARUL.) + + +## INSERT INTO +```sql +INSERT INTO { + <#foo> "baz" . +} +``` +In this INSERT form the triples have to be fully specified, variables are not allowed. + + +It is possible to dynamically generate the triples that should be inserted: +```sql +INSERT INTO CONSTRUCT { + ?s foaf:knows ?o . +} +WHERE { + ?s xfn:contact ?o . +} +``` +This is a simple extension to SPARQL's existing CONSTRUCT query type. It adds the triples generated in the construction step to the specified graph. **Note**: The CONSTRUCT keyword was made optional with the Jan 7th, 2008 revision, to increase the compatibility with SPARUL. + + +## DELETE + +```sql +DELETE { + <#foo> "baz" . + <#foo2> ?any . +} +``` +Each specified triple will be deleted from the RDF store. It is possible to specify variables as wildcards, but they can't be used to build connected patterns. Each triple is handled as a stand-alone pattern. + + +FROM can be used to restrict the delete operations to selected graphs. It's also possible to not specify any triples. The whole graph will then be deleted. +```sql +DELETE FROM +``` + +DELETE can (like INSERT) be combined with a CONSTRUCT query (the CONSTRUCT keyword was made optional with the Jan 7th, 2008 revision): + +```sql +DELETE FROM { + ?s rel:wouldLikeToKnow ?o . +} +WHERE { + ?s kiss:kissed ?o . +} +``` + +Instead of deleting triples only in one graph, you can in all graphs by using: + +```sql +DELETE { + ?s rel:wouldLikeToKnow ?o . +} +WHERE { + ?s kiss:kissed ?o . +} +``` + +## SPARQL Grammar Changes and Additions +```sql +Query ::= Prologue ( SelectQuery | ConstructQuery | DescribeQuery | AskQuery | LoadQuery | InsertQuery | DeleteQuery ) + +SelectQuery ::= 'SELECT' ( 'DISTINCT' | 'REDUCED' )? ( Aggregate+ | Var+ | '*' ) DatasetClause* WhereClause SolutionModifier + +Aggregate ::= ( 'AVG' | 'COUNT' | 'MAX' | 'MIN' | 'SUM' ) '(' Var | '*' ')' 'AS' Var + +LoadQuery ::= 'LOAD' IRIref ( 'INTO' IRIref )? + +InsertQuery ::= 'INSERT' 'INTO' IRIref 'CONSTRUCT'? ConstructTemplate DatasetClause* WhereClause? SolutionModifier + +DeleteQuery ::= 'DELETE' ( 'FROM' IRIref )* 'CONSTRUCT'? ConstructTemplate? DatasetClause* WhereClause? SolutionModifier + +SolutionModifier ::= GroupClause? OrderClause? LimitOffsetClauses? + +GroupClause ::= 'GROUP' 'BY' Var ( ',' Var )* +``` diff --git a/doc/branches.png b/doc/branches.png new file mode 100644 index 00000000..b177abbc Binary files /dev/null and b/doc/branches.png differ diff --git a/doc/developer.md b/doc/developer.md new file mode 100644 index 00000000..5eb3369e --- /dev/null +++ b/doc/developer.md @@ -0,0 +1,49 @@ +# Developer information + +This document contains information about ARC2 internals which are relevant for maintaining and extending ARC2. + +## Run test environment + +To run test environment execute: + +```bash +make test +``` + +Tests are split into different groups, currently: +* unit +* db_adapter_depended + +You can run the `unit` group directly, but you need to set some environment variables for `db_adapter_depended`. +For more information please have a look into our `Makefile`. + +#### config.php + +Currently, we use the following standard db credentials to connect with the database: + +```php +$dbConfig = array( + 'db_name' => 'testdb', + 'db_user' => 'root', + 'db_pwd' => '', + 'db_host' => '127.0.0.1', +); +``` + +The is used in the travis environment. If you have different credentials, copy the `tests/config.php.dist` to `tests/config.php` and set your credentials. + +## Editor + +Please make sure your editor uses our `.editorconfig` file. + +## Docker setup + +For ARC2 developers we recommend this following [Docker setup](https://github.com/k00ni/PHP-Apache-MySQL-Docker). It provides a pre-configured set of software (for PHP, DBS etc.) and allows quick switches between different software versions. + +No matter if one needs a MariaDB 10.3 with PHP 7.2 or a PHP 5.6 with MySQL 5.7.0. If there is a docker container, it runs. + +### Docker and Travis + +We use a very wide range of software-combinations to test ARC2 ([Travis](https://travis-ci.org/semsol/arc2)). Currently, all combinations of major versions of PHP and database systems (currently MySQL and MariaDB only) are checked. + +Using a Docker setup for local development allows to switch the backend very easily. So, if a test with a certain DBS/PHP version combination fails on travis, its very likely that you can reproduce it locally. Dont forget to run `composer update` after a switch to make sure appropriate software is used. diff --git a/parsers/ARC2_AtomParser.php b/parsers/ARC2_AtomParser.php index 4368bd1e..e6a61ab9 100644 --- a/parsers/ARC2_AtomParser.php +++ b/parsers/ARC2_AtomParser.php @@ -10,236 +10,252 @@ class: ARC2 Atom Parser ARC2::inc('LegacyXMLParser'); -class ARC2_AtomParser extends ARC2_LegacyXMLParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->triples = array(); - $this->target_encoding = ''; - $this->t_count = 0; - $this->added_triples = array(); - $this->skip_dupes = false; - $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); - $this->bnode_id = 0; - $this->cache = array(); - $this->allowCDataNodes = 0; - } - - /* */ - - function done() { - $this->extractRDF(); - } - - /* */ - - function setReader(&$reader) { - $this->reader = $reader; - } - - function createBnodeID(){ - $this->bnode_id++; - return '_:' . $this->bnode_prefix . $this->bnode_id; - } - - function addT($t) { - //if (!isset($t['o_datatype'])) - if ($this->skip_dupes) { - //$h = md5(print_r($t, 1)); - $h = md5(serialize($t)); - if (!isset($this->added_triples[$h])) { - $this->triples[$this->t_count] = $t; - $this->t_count++; - $this->added_triples[$h] = true; - } +class ARC2_AtomParser extends ARC2_LegacyXMLParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - else { - $this->triples[$this->t_count] = $t; - $this->t_count++; + + public function __init() + {/* reader */ + parent::__init(); + $this->triples = []; + $this->target_encoding = ''; + $this->t_count = 0; + $this->added_triples = []; + $this->skip_dupes = false; + $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); + $this->bnode_id = 0; + $this->cache = []; + $this->allowCDataNodes = 0; } - } - - function getTriples() { - return $this->v('triples', array()); - } - - function countTriples() { - return $this->t_count; - } - - function getSimpleIndex($flatten_objects = 1, $vals = '') { - return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals); - } - - /* */ - - function extractRDF() { - $index = $this->getNodeIndex(); - //print_r($index); - $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $this->atom = 'http://www.w3.org/2005/Atom'; - $this->rss = 'http://purl.org/rss/1.0/'; - $this->dc = 'http://purl.org/dc/elements/1.1/'; - $this->sioc = 'http://rdfs.org/sioc/ns#'; - $this->dct = 'http://purl.org/dc/terms/'; - $this->content = 'http://purl.org/rss/1.0/modules/content/'; - $this->enc = 'http://purl.oclc.org/net/rss_2.0/enc#'; - $this->mappings = array( - 'feed' => $this->rss . 'channel', - 'entry' => $this->rss . 'item', - 'title' => $this->rss . 'title', - 'link' => $this->rss . 'link', - 'summary' => $this->rss . 'description', - 'content' => $this->content . 'encoded', - 'id' => $this->dc . 'identifier', - 'author' => $this->dc . 'creator', - 'category' => $this->dc . 'subject', - 'updated' => $this->dc . 'date', - 'source' => $this->dc . 'source', - ); - $this->dt_props = array( - $this->dc . 'identifier', - $this->rss . 'link' - ); - foreach ($index as $p_id => $nodes) { - foreach ($nodes as $pos => $node) { - $tag = $this->v('tag', '', $node); - if ($tag == 'feed') { - $struct = $this->extractChannel($index[$node['id']]); - $triples = ARC2::getTriplesFromIndex($struct); - foreach ($triples as $t) { - $this->addT($t); - } - } - elseif ($tag == 'entry') { - $struct = $this->extractItem($index[$node['id']]); - $triples = ARC2::getTriplesFromIndex($struct); - foreach ($triples as $t) { - $this->addT($t); - } - } - } + + public function done() + { + $this->extractRDF(); } - } - - function extractChannel($els) { - list($props, $sub_index) = $this->extractProps($els, 'channel'); - $uri = $props[$this->rss . 'link'][0]['value']; - return ARC2::getMergedIndex(array($uri => $props), $sub_index); - } - - function extractItem($els) { - list($props, $sub_index) = $this->extractProps($els, 'item'); - $uri = $props[$this->rss . 'link'][0]['value']; - return ARC2::getMergedIndex(array($uri => $props), $sub_index); - } - - function extractProps($els, $container) { - $r = array($this->rdf . 'type' => array(array('value' => $this->rss . $container, 'type' => 'uri'))); - $sub_index = array(); - foreach ($els as $info) { - /* key */ - $tag = $info['tag']; - if (!preg_match('/^[a-z0-9]+\:/i', $tag)) { - $k = isset($this->mappings[$tag]) ? $this->mappings[$tag] : ''; - } - elseif (isset($this->mappings[$tag])) { - $k = $this->mappings[$tag]; - } - else {/* qname */ - $k = $this->expandPName($tag); - } - //echo $k . "\n"; - if (($container == 'channel') && ($k == $this->rss . 'item')) continue; - /* val */ - $v = trim($info['cdata']); - if (!$v) $v = $this->v('href uri', '', $info['a']); - /* prop */ - if ($k) { - /* content handling */ - if (in_array($k, array($this->rss . 'description', $this->content . 'encoded'))) { - $v = $this->getNodeContent($info); - } - /* source handling */ - elseif ($k == $this->dc . 'source') { - $sub_nodes = $this->node_index[$info['id']]; - foreach ($sub_nodes as $sub_pos => $sub_info) { - if ($sub_info['tag'] == 'id') { - $v = trim($sub_info['cdata']); - } - } - } - /* link handling */ - elseif ($k == $this->rss . 'link') { - if ($link_type = $this->v('type', '', $info['a'])) { - $k2 = $this->dc . 'format'; - if (!isset($sub_index[$v])) $sub_index[$v] = array(); - if (!isset($sub_index[$v][$k2])) $sub_index[$v][$k2] = array(); - $sub_index[$v][$k2][] = array('value' => $link_type, 'type' => 'literal'); - } - } - /* author handling */ - elseif ($k == $this->dc . 'creator') { - $sub_nodes = $this->node_index[$info['id']]; - foreach ($sub_nodes as $sub_pos => $sub_info) { - if ($sub_info['tag'] == 'name') { - $v = trim($sub_info['cdata']); - } - if ($sub_info['tag'] == 'uri') { - $k2 = $this->sioc . 'has_creator'; - $v2 = trim($sub_info['cdata']); - if (!isset($r[$k2])) $r[$k2] = array(); - $r[$k2][] = array('value' => $v2, 'type' => 'uri'); + + public function setReader(&$reader) + { + $this->reader = $reader; + } + + public function createBnodeID() + { + ++$this->bnode_id; + + return '_:'.$this->bnode_prefix.$this->bnode_id; + } + + public function addT($t) + { + //if (!isset($t['o_datatype'])) + if ($this->skip_dupes) { + //$h = md5(print_r($t, 1)); + $h = md5(serialize($t)); + if (!isset($this->added_triples[$h])) { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + $this->added_triples[$h] = true; } - } + } else { + $this->triples[$this->t_count] = $t; + ++$this->t_count; } - /* date handling */ - elseif (in_array($k, array($this->dc . 'date', $this->dct . 'modified'))) { - if (!preg_match('/^[0-9]{4}/', $v) && ($sub_v = strtotime($v)) && ($sub_v != -1)) { - $tz = date('Z', $sub_v); /* timezone offset */ - $sub_v -= $tz; /* utc */ - $v = date('Y-m-d\TH:i:s\Z', $sub_v); - } - } - /* tag handling */ - elseif ($k == $this->dc . 'subject') { - $v = $this->v('term', '', $info['a']); - } - /* other attributes in closed tags */ - elseif (!$v && ($info['state'] == 'closed') && $info['a']) { - foreach ($info['a'] as $sub_k => $sub_v) { - if (!preg_match('/(xmlns|\:|type)/', $sub_k)) { - $v = $sub_v; - break; + } + + public function getTriples() + { + return $this->v('triples', []); + } + + public function countTriples() + { + return $this->t_count; + } + + public function getSimpleIndex($flatten_objects = 1, $vals = '') + { + return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals); + } + + public function extractRDF() + { + $index = $this->getNodeIndex(); + //print_r($index); + $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $this->atom = 'http://www.w3.org/2005/Atom'; + $this->rss = 'http://purl.org/rss/1.0/'; + $this->dc = 'http://purl.org/dc/elements/1.1/'; + $this->sioc = 'http://rdfs.org/sioc/ns#'; + $this->dct = 'http://purl.org/dc/terms/'; + $this->content = 'http://purl.org/rss/1.0/modules/content/'; + $this->enc = 'http://purl.oclc.org/net/rss_2.0/enc#'; + $this->mappings = [ + 'feed' => $this->rss.'channel', + 'entry' => $this->rss.'item', + 'title' => $this->rss.'title', + 'link' => $this->rss.'link', + 'summary' => $this->rss.'description', + 'content' => $this->content.'encoded', + 'id' => $this->dc.'identifier', + 'author' => $this->dc.'creator', + 'category' => $this->dc.'subject', + 'updated' => $this->dc.'date', + 'source' => $this->dc.'source', + ]; + $this->dt_props = [ + $this->dc.'identifier', + $this->rss.'link', + ]; + foreach ($index as $p_id => $nodes) { + foreach ($nodes as $pos => $node) { + $tag = $this->v('tag', '', $node); + if ('feed' == $tag) { + $struct = $this->extractChannel($index[$node['id']]); + $triples = ARC2::getTriplesFromIndex($struct); + foreach ($triples as $t) { + $this->addT($t); + } + } elseif ('entry' == $tag) { + $struct = $this->extractItem($index[$node['id']]); + $triples = ARC2::getTriplesFromIndex($struct); + foreach ($triples as $t) { + $this->addT($t); + } + } } - } } - if (!isset($r[$k])) $r[$k] = array(); - $r[$k][] = array('value' => $v, 'type' => in_array($k, $this->dt_props) || !preg_match('/^[a-z0-9]+\:[^\s]+$/is', $v) ? 'literal' : 'uri'); - } } - return array($r, $sub_index); - } - - function initXMLParser() { - if (!isset($this->xml_parser)) { - $enc = preg_match('/^(utf\-8|iso\-8859\-1|us\-ascii)$/i', $this->getEncoding(), $m) ? $m[1] : 'UTF-8'; - $parser = xml_parser_create($enc); - xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); - xml_set_element_handler($parser, 'open', 'close'); - xml_set_character_data_handler($parser, 'cData'); - xml_set_start_namespace_decl_handler($parser, 'nsDecl'); - xml_set_object($parser, $this); - $this->xml_parser = $parser; + + public function extractChannel($els) + { + list($props, $sub_index) = $this->extractProps($els, 'channel'); + $uri = $props[$this->rss.'link'][0]['value']; + + return ARC2::getMergedIndex([$uri => $props], $sub_index); } - } - /* */ + public function extractItem($els) + { + list($props, $sub_index) = $this->extractProps($els, 'item'); + $uri = $props[$this->rss.'link'][0]['value']; + return ARC2::getMergedIndex([$uri => $props], $sub_index); + } + public function extractProps($els, $container) + { + $r = [$this->rdf.'type' => [['value' => $this->rss.$container, 'type' => 'uri']]]; + $sub_index = []; + foreach ($els as $info) { + /* key */ + $tag = $info['tag']; + if (!preg_match('/^[a-z0-9]+\:/i', $tag)) { + $k = isset($this->mappings[$tag]) ? $this->mappings[$tag] : ''; + } elseif (isset($this->mappings[$tag])) { + $k = $this->mappings[$tag]; + } else {/* qname */ + $k = $this->expandPName($tag); + } + //echo $k . "\n"; + if (('channel' == $container) && ($k == $this->rss.'item')) { + continue; + } + /* val */ + $v = trim($info['cdata']); + if (!$v) { + $v = $this->v('href uri', '', $info['a']); + } + /* prop */ + if ($k) { + /* content handling */ + if (in_array($k, [$this->rss.'description', $this->content.'encoded'])) { + $v = $this->getNodeContent($info); + } + /* source handling */ + elseif ($k == $this->dc.'source') { + $sub_nodes = $this->node_index[$info['id']]; + foreach ($sub_nodes as $sub_pos => $sub_info) { + if ('id' == $sub_info['tag']) { + $v = trim($sub_info['cdata']); + } + } + } + /* link handling */ + elseif ($k == $this->rss.'link') { + if ($link_type = $this->v('type', '', $info['a'])) { + $k2 = $this->dc.'format'; + if (!isset($sub_index[$v])) { + $sub_index[$v] = []; + } + if (!isset($sub_index[$v][$k2])) { + $sub_index[$v][$k2] = []; + } + $sub_index[$v][$k2][] = ['value' => $link_type, 'type' => 'literal']; + } + } + /* author handling */ + elseif ($k == $this->dc.'creator') { + $sub_nodes = $this->node_index[$info['id']]; + foreach ($sub_nodes as $sub_pos => $sub_info) { + if ('name' == $sub_info['tag']) { + $v = trim($sub_info['cdata']); + } + if ('uri' == $sub_info['tag']) { + $k2 = $this->sioc.'has_creator'; + $v2 = trim($sub_info['cdata']); + if (!isset($r[$k2])) { + $r[$k2] = []; + } + $r[$k2][] = ['value' => $v2, 'type' => 'uri']; + } + } + } + /* date handling */ + elseif (in_array($k, [$this->dc.'date', $this->dct.'modified'])) { + if (!preg_match('/^[0-9]{4}/', $v) && ($sub_v = strtotime($v)) && ($sub_v != -1)) { + $tz = date('Z', $sub_v); /* timezone offset */ + $sub_v -= $tz; /* utc */ + $v = date('Y-m-d\TH:i:s\Z', $sub_v); + } + } + /* tag handling */ + elseif ($k == $this->dc.'subject') { + $v = $this->v('term', '', $info['a']); + } + /* other attributes in closed tags */ + elseif (!$v && ('closed' == $info['state']) && $info['a']) { + foreach ($info['a'] as $sub_k => $sub_v) { + if (!preg_match('/(xmlns|\:|type)/', $sub_k)) { + $v = $sub_v; + break; + } + } + } + if (!isset($r[$k])) { + $r[$k] = []; + } + $r[$k][] = ['value' => $v, 'type' => in_array($k, $this->dt_props) || !preg_match('/^[a-z0-9]+\:[^\s]+$/is', $v) ? 'literal' : 'uri']; + } + } + + return [$r, $sub_index]; + } + + public function initXMLParser() + { + if (!isset($this->xml_parser)) { + $enc = preg_match('/^(utf\-8|iso\-8859\-1|us\-ascii)$/i', $this->getEncoding(), $m) ? $m[1] : 'UTF-8'; + $parser = xml_parser_create($enc); + xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); + xml_set_element_handler($parser, 'open', 'close'); + xml_set_character_data_handler($parser, 'cData'); + xml_set_start_namespace_decl_handler($parser, 'nsDecl'); + xml_set_object($parser, $this); + $this->xml_parser = $parser; + } + } } diff --git a/parsers/ARC2_CBJSONParser.php b/parsers/ARC2_CBJSONParser.php index 53250b31..d1cbf5a8 100755 --- a/parsers/ARC2_CBJSONParser.php +++ b/parsers/ARC2_CBJSONParser.php @@ -1,267 +1,343 @@ * @license W3C Software License and GPL * @homepage - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('JSONParser'); -class ARC2_CBJSONParser extends ARC2_JSONParser { +class ARC2_CBJSONParser extends ARC2_JSONParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->base = 'http://cb.semsol.org/'; - $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $this->default_ns = $this->base . 'ns#'; - $this->nsp = array($this->rdf => 'rdf'); - } - - /* */ + public function __init() + {/* reader */ + parent::__init(); + $this->base = 'http://cb.semsol.org/'; + $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $this->default_ns = $this->base.'ns#'; + $this->nsp = [$this->rdf => 'rdf']; + } - function done() { - $this->extractRDF(); - } + public function done() + { + $this->extractRDF(); + } - function extractRDF($formats = '') { - $struct = $this->struct; - if ($type = $this->getStructType($struct)) { - $s = $this->getResourceID($struct, $type); - /* rdf:type */ - $this->addT($s, $this->rdf . 'type', $this->default_ns . $this->camelCase($type), 'uri', 'uri'); - /* explicit triples */ - $this->extractResourceRDF($struct, $s); + public function extractRDF($formats = '') + { + $struct = $this->struct; + if ($type = $this->getStructType($struct)) { + $s = $this->getResourceID($struct, $type); + /* rdf:type */ + $this->addT($s, $this->rdf.'type', $this->default_ns.$this->camelCase($type), 'uri', 'uri'); + /* explicit triples */ + $this->extractResourceRDF($struct, $s); + } } - } - - function getStructType($struct, $rel = '') { - /* url-based */ - if ($url = $this->v('crunchbase_url', '', $struct)) { - return preg_replace('/^.*crunchbase\.com\/([^\/]+)\/.*$/', '\\1', $url); + + public function getStructType($struct, $rel = '') + { + /* url-based */ + if ($url = $this->v('crunchbase_url', '', $struct)) { + return preg_replace('/^.*crunchbase\.com\/([^\/]+)\/.*$/', '\\1', $url); + } + /* rel-based */ + if ('person' == $rel) { + return 'person'; + } + if ('company' == $rel) { + return 'company'; + } + if ('acquiring_company' == $rel) { + return 'company'; + } + if ('firm' == $rel) { + return 'company'; + } + if ('provider' == $rel) { + return 'service-provider'; + } + /* struct-based */ + if (isset($struct['_type'])) { + return $struct['_type']; + } + if (isset($struct['round_code'])) { + return 'funding_round'; + } + if (isset($struct['products'])) { + return 'company'; + } + if (isset($struct['first_name'])) { + return 'person'; + } + if (isset($struct['investments'])) { + return 'financial-organization'; + } + if (isset($struct['launched_year'])) { + return 'product'; + } + if (isset($struct['providerships']) && is_array($struct['providerships'])) { + return 'service-provider'; + } + + return ''; } - /* rel-based */ - if ($rel == 'person') return 'person'; - if ($rel == 'company') return 'company'; - if ($rel == 'acquiring_company') return 'company'; - if ($rel == 'firm') return 'company'; - if ($rel == 'provider') return 'service-provider'; - /* struct-based */ - if (isset($struct['_type'])) return $struct['_type']; - if (isset($struct['round_code'])) return 'funding_round'; - if (isset($struct['products'])) return 'company'; - if (isset($struct['first_name'])) return 'person'; - if (isset($struct['investments'])) return 'financial-organization'; - if (isset($struct['launched_year'])) return 'product'; - if (isset($struct['providerships']) && is_array($struct['providerships'])) return 'service-provider'; - return ''; - } - - function getResourceID($struct, $type) { - if ($type && isset($struct['permalink'])) { - return $this->base . $type . '/' . $struct['permalink'] . '#self'; + + public function getResourceID($struct, $type) + { + if ($type && isset($struct['permalink'])) { + return $this->base.$type.'/'.$struct['permalink'].'#self'; + } + + return $this->createBnodeID(); } - return $this->createBnodeID(); - } - - function getPropertyURI($name, $ns = '') { - if (!$ns) $ns = $this->default_ns; - if (preg_match('/^(product|funding_round|investment|acquisition|.+ship|office|milestone|.+embed|.+link|degree|fund)s/', $name, $m)) $name = $m[1]; - if ($name == 'tag_list') $name = 'tag'; - if ($name == 'competitions') $name = 'competitor'; - return $ns . $name; - } - function createSubURI($s, $k, $pos) { - $s = str_replace('#self', '/', $s); - if (preg_match('/(office|ship|investment|milestone|fund|embed|link)s$/', $k)) $k = substr($k, 0, -1); - return $s . $k . '-' . ($pos + 1) . '#self'; - } - - /* */ - - function extractResourceRDF($struct, $s, $pos = 0) { - $s_type = preg_match('/^\_\:/', $s) ? 'bnode' : 'uri'; - $date_prefixes = array(); - foreach ($struct as $k => $v) { - if ($k == 'acquisition') $k = 'exit'; - if (preg_match('/^(.*)\_(year|month|day)$/', $k, $m)) { - if (!in_array($m[1], $date_prefixes)) $date_prefixes[] = $m[1]; - } - $sub_m = 'extract' . $this->camelCase($k) . 'RDF'; - if (method_exists($this, $sub_m)) { - $this->$sub_m($s, $s_type, $v); - continue; - } - $p = $this->getPropertyURI($k); - if (!$v) continue; - /* simple, single v */ - if (!is_array($v)) { - $o_type = preg_match('/^[a-z]+\:[^\s]+$/is', $v) ? 'uri' : 'literal'; - $v = trim($v); - if (preg_match('/^https?\:\/\/[^\/]+$/', $v)) $v .= '/'; - $this->addT($s, $p, $v, $s_type, $o_type); - /* rdfs:label */ - if ($k == 'name') $this->addT($s, 'http://www.w3.org/2000/01/rdf-schema#label', $v, $s_type, $o_type); - /* dc:identifier */ - //if ($k == 'permalink') $this->addT($s, 'http://purl.org/dc/elements/1.1/identifier', $v, $s_type, $o_type); - } - /* structured, single v */ - elseif (!$this->isFlatArray($v)) { - if ($o_type = $this->getStructType($v, $k)) {/* known type */ - $o = $this->getResourceID($v, $o_type); - $this->addT($s, $p, $o, $s_type, 'uri'); - $this->addT($o, $this->rdf . 'type', $this->default_ns . $this->camelCase($o_type), 'uri', 'uri'); - } - else {/* unknown type */ - $o = $this->createSubURI($s, $k, $pos); - $this->addT($s, $p, $o, $s_type, 'uri'); - $this->extractResourceRDF($v, $o); - } - } - /* value list */ - else { - foreach ($v as $sub_pos => $sub_v) { - $this->extractResourceRDF(array($k => $sub_v), $s, $sub_pos); - } - } + public function getPropertyURI($name, $ns = '') + { + if (!$ns) { + $ns = $this->default_ns; + } + if (preg_match('/^(product|funding_round|investment|acquisition|.+ship|office|milestone|.+embed|.+link|degree|fund)s/', $name, $m)) { + $name = $m[1]; + } + if ('tag_list' == $name) { + $name = 'tag'; + } + if ('competitions' == $name) { + $name = 'competitor'; + } + + return $ns.$name; } - /* infer XSD triples */ - foreach ($date_prefixes as $prefix) { - $this->inferDate($prefix, $s, $struct); + + public function createSubURI($s, $k, $pos) + { + $s = str_replace('#self', '/', $s); + if (preg_match('/(office|ship|investment|milestone|fund|embed|link)s$/', $k)) { + $k = substr($k, 0, -1); + } + + return $s.$k.'-'.($pos + 1).'#self'; } - } - function isFlatArray($v) { - foreach ($v as $k => $sub_v) { - return is_numeric($k) ? 1 : 0; + public function extractResourceRDF($struct, $s, $pos = 0) + { + $s_type = preg_match('/^\_\:/', $s) ? 'bnode' : 'uri'; + $date_prefixes = []; + foreach ($struct as $k => $v) { + if ('acquisition' == $k) { + $k = 'exit'; + } + if (preg_match('/^(.*)\_(year|month|day)$/', $k, $m)) { + if (!in_array($m[1], $date_prefixes)) { + $date_prefixes[] = $m[1]; + } + } + $sub_m = 'extract'.$this->camelCase($k).'RDF'; + if (method_exists($this, $sub_m)) { + $this->$sub_m($s, $s_type, $v); + continue; + } + $p = $this->getPropertyURI($k); + if (!$v) { + continue; + } + /* simple, single v */ + if (!is_array($v)) { + $o_type = preg_match('/^[a-z]+\:[^\s]+$/is', $v) ? 'uri' : 'literal'; + $v = trim($v); + if (preg_match('/^https?\:\/\/[^\/]+$/', $v)) { + $v .= '/'; + } + $this->addT($s, $p, $v, $s_type, $o_type); + /* rdfs:label */ + if ('name' == $k) { + $this->addT($s, 'http://www.w3.org/2000/01/rdf-schema#label', $v, $s_type, $o_type); + } + /* dc:identifier */ + //if ($k == 'permalink') $this->addT($s, 'http://purl.org/dc/elements/1.1/identifier', $v, $s_type, $o_type); + } + /* structured, single v */ + elseif (!$this->isFlatArray($v)) { + if ($o_type = $this->getStructType($v, $k)) {/* known type */ + $o = $this->getResourceID($v, $o_type); + $this->addT($s, $p, $o, $s_type, 'uri'); + $this->addT($o, $this->rdf.'type', $this->default_ns.$this->camelCase($o_type), 'uri', 'uri'); + } else {/* unknown type */ + $o = $this->createSubURI($s, $k, $pos); + $this->addT($s, $p, $o, $s_type, 'uri'); + $this->extractResourceRDF($v, $o); + } + } + /* value list */ + else { + foreach ($v as $sub_pos => $sub_v) { + $this->extractResourceRDF([$k => $sub_v], $s, $sub_pos); + } + } + } + /* infer XSD triples */ + foreach ($date_prefixes as $prefix) { + $this->inferDate($prefix, $s, $struct); + } } - } - - /* */ - - function extractTagListRDF($s, $s_type, $v) { - if (!$v) return 0; - $tags = preg_split('/\, /', $v); - foreach ($tags as $tag) { - if (!trim($tag)) continue; - $this->addT($s, $this->getPropertyURI('tag'), $tag, $s_type, 'literal'); + + public function isFlatArray($v) + { + foreach ($v as $k => $sub_v) { + return is_numeric($k) ? 1 : 0; + } } - } - function extractImageRDF($s, $s_type, $v, $rel = 'image') { - if (!$v) return 1; - $sizes = $v['available_sizes']; - foreach ($sizes as $size) { - $w = $size[0][0]; - $h = $size[0][1]; - $img = 'http://www.crunchbase.com/' . $size[1]; - $this->addT($s, $this->getPropertyURI($rel), $img, $s_type, 'uri'); - $this->addT($img, $this->getPropertyURI('width'), $w, 'uri', 'literal'); - $this->addT($img, $this->getPropertyURI('height'), $h, 'uri', 'literal'); + public function extractTagListRDF($s, $s_type, $v) + { + if (!$v) { + return 0; + } + $tags = preg_split('/\, /', $v); + foreach ($tags as $tag) { + if (!trim($tag)) { + continue; + } + $this->addT($s, $this->getPropertyURI('tag'), $tag, $s_type, 'literal'); + } } - } - function extractScreenshotsRDF($s, $s_type, $v) { - if (!$v) return 1; - foreach ($v as $sub_v) { - $this->extractImageRDF($s, $s_type, $sub_v, 'screenshot'); + public function extractImageRDF($s, $s_type, $v, $rel = 'image') + { + if (!$v) { + return 1; + } + $sizes = $v['available_sizes']; + foreach ($sizes as $size) { + $w = $size[0][0]; + $h = $size[0][1]; + $img = 'http://www.crunchbase.com/'.$size[1]; + $this->addT($s, $this->getPropertyURI($rel), $img, $s_type, 'uri'); + $this->addT($img, $this->getPropertyURI('width'), $w, 'uri', 'literal'); + $this->addT($img, $this->getPropertyURI('height'), $h, 'uri', 'literal'); + } } - } - - function extractProductsRDF($s, $s_type, $v) { - foreach ($v as $sub_v) { - $o = $this->getResourceID($sub_v, 'product'); - $this->addT($s, $this->getPropertyURI('product'), $o, $s_type, 'uri'); + + public function extractScreenshotsRDF($s, $s_type, $v) + { + if (!$v) { + return 1; + } + foreach ($v as $sub_v) { + $this->extractImageRDF($s, $s_type, $sub_v, 'screenshot'); + } } - } - function extractCompetitionsRDF($s, $s_type, $v) { - foreach ($v as $sub_v) { - $o = $this->getResourceID($sub_v['competitor'], 'company'); - $this->addT($s, $this->getPropertyURI('competitor'), $o, $s_type, 'uri'); + public function extractProductsRDF($s, $s_type, $v) + { + foreach ($v as $sub_v) { + $o = $this->getResourceID($sub_v, 'product'); + $this->addT($s, $this->getPropertyURI('product'), $o, $s_type, 'uri'); + } } - } - function extractFundingRoundsRDF($s, $s_type, $v) { - foreach ($v as $pos => $sub_v) { - $o = $this->createSubURI($s, 'funding_round', $pos); - $this->addT($s, $this->getPropertyURI('funding_round'), $o, $s_type, 'uri'); - $this->extractResourceRDF($sub_v, $o, $pos); + public function extractCompetitionsRDF($s, $s_type, $v) + { + foreach ($v as $sub_v) { + $o = $this->getResourceID($sub_v['competitor'], 'company'); + $this->addT($s, $this->getPropertyURI('competitor'), $o, $s_type, 'uri'); + } } - } - function extractInvestmentsRDF($s, $s_type, $v) { - foreach ($v as $pos => $sub_v) { - /* incoming */ - foreach (array('person' => 'person', 'company' => 'company', 'financial_org' => 'financial-organization') as $k => $type) { - if (isset($sub_v[$k])) $this->addT($s, $this->getPropertyURI('investment'), $this->getResourceID($sub_v[$k], $type), $s_type, 'uri'); - } - /* outgoing */ - if (isset($sub_v['funding_round'])) { - $o = $this->createSubURI($s, 'investment', $pos); - $this->addT($s, $this->getPropertyURI('investment'), $o, $s_type, 'uri'); - $this->extractResourceRDF($sub_v['funding_round'], $o, $pos); - } + public function extractFundingRoundsRDF($s, $s_type, $v) + { + foreach ($v as $pos => $sub_v) { + $o = $this->createSubURI($s, 'funding_round', $pos); + $this->addT($s, $this->getPropertyURI('funding_round'), $o, $s_type, 'uri'); + $this->extractResourceRDF($sub_v, $o, $pos); + } } - } - - function extractExternalLinksRDF($s, $s_type, $v) { - foreach ($v as $sub_v) { - $href = $sub_v['external_url']; - if (preg_match('/^https?\:\/\/[^\/]+$/', $href)) $href .= '/'; - $this->addT($s, $this->getPropertyURI('external_link'), $href, $s_type, 'uri'); - $this->addT($href, $this->getPropertyURI('title'), $sub_v['title'], $s_type, 'literal'); + + public function extractInvestmentsRDF($s, $s_type, $v) + { + foreach ($v as $pos => $sub_v) { + /* incoming */ + foreach (['person' => 'person', 'company' => 'company', 'financial_org' => 'financial-organization'] as $k => $type) { + if (isset($sub_v[$k])) { + $this->addT($s, $this->getPropertyURI('investment'), $this->getResourceID($sub_v[$k], $type), $s_type, 'uri'); + } + } + /* outgoing */ + if (isset($sub_v['funding_round'])) { + $o = $this->createSubURI($s, 'investment', $pos); + $this->addT($s, $this->getPropertyURI('investment'), $o, $s_type, 'uri'); + $this->extractResourceRDF($sub_v['funding_round'], $o, $pos); + } + } } - } - function extractWebPresencesRDF($s, $s_type, $v) { - foreach ($v as $sub_v) { - $href = $sub_v['external_url']; - if (preg_match('/^https?\:\/\/[^\/]+$/', $href)) $href .= '/'; - $this->addT($s, $this->getPropertyURI('web_presence'), $href, $s_type, 'uri'); - $this->addT($href, $this->getPropertyURI('title'), $sub_v['title'], $s_type, 'literal'); + public function extractExternalLinksRDF($s, $s_type, $v) + { + foreach ($v as $sub_v) { + $href = $sub_v['external_url']; + if (preg_match('/^https?\:\/\/[^\/]+$/', $href)) { + $href .= '/'; + } + $this->addT($s, $this->getPropertyURI('external_link'), $href, $s_type, 'uri'); + $this->addT($href, $this->getPropertyURI('title'), $sub_v['title'], $s_type, 'literal'); + } } - } - function extractCreatedAtRDF($s, $s_type, $v) { - $v = $this->getAPIDateXSD($v); - $this->addT($s, $this->getPropertyURI('created_at'), $v, $s_type, 'literal'); - } + public function extractWebPresencesRDF($s, $s_type, $v) + { + foreach ($v as $sub_v) { + $href = $sub_v['external_url']; + if (preg_match('/^https?\:\/\/[^\/]+$/', $href)) { + $href .= '/'; + } + $this->addT($s, $this->getPropertyURI('web_presence'), $href, $s_type, 'uri'); + $this->addT($href, $this->getPropertyURI('title'), $sub_v['title'], $s_type, 'literal'); + } + } - function extractUpdatedAtRDF($s, $s_type, $v) { - $v = $this->getAPIDateXSD($v); - $this->addT($s, $this->getPropertyURI('updated_at'), $v, $s_type, 'literal'); - } + public function extractCreatedAtRDF($s, $s_type, $v) + { + $v = $this->getAPIDateXSD($v); + $this->addT($s, $this->getPropertyURI('created_at'), $v, $s_type, 'literal'); + } - function getAPIDateXSD($val) { - //Fri Jan 16 21:11:48 UTC 2009 - if (preg_match('/^[a-z]+ ([a-z]+) ([0-9]+) ([0-9]{2}\:[0-9]{2}\:[0-9]{2}) UTC ([0-9]{4})/i', $val, $m)) { - $months = array('Jan' => '01', 'Feb' => '02', 'Mar' =>'03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12'); - return $m[4] . '-' . $months[$m[1]] . '-' . $m[2] . 'T' . $m[3] . 'Z'; + public function extractUpdatedAtRDF($s, $s_type, $v) + { + $v = $this->getAPIDateXSD($v); + $this->addT($s, $this->getPropertyURI('updated_at'), $v, $s_type, 'literal'); } - return '2000-01-01'; - } - /* */ + public function getAPIDateXSD($val) + { + //Fri Jan 16 21:11:48 UTC 2009 + if (preg_match('/^[a-z]+ ([a-z]+) ([0-9]+) ([0-9]{2}\:[0-9]{2}\:[0-9]{2}) UTC ([0-9]{4})/i', $val, $m)) { + $months = ['Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12']; + + return $m[4].'-'.$months[$m[1]].'-'.$m[2].'T'.$m[3].'Z'; + } - function inferDate($prefix, $s, $struct) { - $s_type = preg_match('/^\_\:/', $s) ? 'bnode' : 'uri'; - $r = ''; - foreach (array('year', 'month', 'day') as $suffix) { - $val = $this->v1($prefix . '_' . $suffix, '00', $struct); - $r .= ($r ? '-' : '') . str_pad($val, 2, '0', STR_PAD_LEFT); + return '2000-01-01'; } - if ($r != '00-00-00') { - $this->addT($s, $this->getPropertyURI($prefix . '_date'), $r, $s_type, 'literal'); + + public function inferDate($prefix, $s, $struct) + { + $s_type = preg_match('/^\_\:/', $s) ? 'bnode' : 'uri'; + $r = ''; + foreach (['year', 'month', 'day'] as $suffix) { + $val = $this->v1($prefix.'_'.$suffix, '00', $struct); + $r .= ($r ? '-' : '').str_pad($val, 2, '0', STR_PAD_LEFT); + } + if ('00-00-00' != $r) { + $this->addT($s, $this->getPropertyURI($prefix.'_date'), $r, $s_type, 'literal'); + } } - } - } diff --git a/parsers/ARC2_JSONParser.php b/parsers/ARC2_JSONParser.php index e4c35857..28021475 100755 --- a/parsers/ARC2_JSONParser.php +++ b/parsers/ARC2_JSONParser.php @@ -1,167 +1,183 @@ * @license W3C Software License and GPL * @homepage - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('RDFParser'); -class ARC2_JSONParser extends ARC2_RDFParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - } - - /* */ - - function x($re, $v, $options = 'si') { - while (preg_match('/^\s*(\/\*.*\*\/)(.*)$/Usi', $v, $m)) {/* comment removal */ - $v = $m[2]; - } - $this->unparsed_code = (strlen($this->unparsed_code) > strlen($v)) ? $v : $this->unparsed_code; - return ARC2::x($re, $v, $options); - } - - function parse($path, $data = '') { - $this->state = 0; - /* reader */ - if (!$this->v('reader')) { - ARC2::inc('Reader'); - $this->reader = new ARC2_Reader($this->a, $this); +class ARC2_JSONParser extends ARC2_RDFParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - $this->reader->setAcceptHeader('Accept: application/json; q=0.9, */*; q=0.1'); - $this->reader->activate($path, $data); - $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; - /* parse */ - $doc = ''; - while ($d = $this->reader->readStream()) { - $doc .= $d; - } - $this->reader->closeStream(); - unset($this->reader); - $doc = preg_replace('/^[^\{]*(.*\})[^\}]*$/is', '\\1', $doc); - $this->unparsed_code = $doc; - list($this->struct, $rest) = $this->extractObject($doc); - return $this->done(); - } - - /* */ - - function extractObject($v) { - if (function_exists('json_decode')) return array(json_decode($v, 1), ''); - $r = array(); - /* sub-object */ - if ($sub_r = $this->x('\{', $v)) { - $v = $sub_r[1]; - while ((list($sub_r, $v) = $this->extractEntry($v)) && $sub_r) { - $r[$sub_r['key']] = $sub_r['value']; - } - if ($sub_r = $this->x('\}', $v)) $v = $sub_r[1]; - } - /* sub-list */ - elseif ($sub_r = $this->x('\[', $v)) { - $v = $sub_r[1]; - while ((list($sub_r, $v) = $this->extractObject($v)) && $sub_r) { - $r[] = $sub_r; - $v = ltrim($v, ','); - } - if ($sub_r = $this->x('\]', $v)) $v = $sub_r[1]; + + public function __init() + { + parent::__init(); } - /* sub-value */ - elseif ((list($sub_r, $v) = $this->extractValue($v)) && ($sub_r !== false)) { - $r = $sub_r; + + public function x($re, $v, $options = 'si') + { + while (preg_match('/^\s*(\/\*.*\*\/)(.*)$/Usi', $v, $m)) {/* comment removal */ + $v = $m[2]; + } + $this->unparsed_code = (strlen($this->unparsed_code) > strlen($v)) ? $v : $this->unparsed_code; + + return ARC2::x($re, $v, $options); } - return array($r, $v); - } - - function extractEntry($v) { - if ($r = $this->x('\,', $v)) $v = $r[1]; - /* k */ - if ($r = $this->x('\"([^\"]+)\"\s*\:', $v)) { - $k = $r[1]; - $sub_v = $r[2]; - if (list($sub_r, $sub_v) = $this->extractObject($sub_v)) { - return array( - array('key' => $k, 'value' => $sub_r), - $sub_v - ); - } + + public function parse($path, $data = '') + { + $this->state = 0; + /* reader */ + if (!$this->v('reader')) { + ARC2::inc('Reader'); + $this->reader = new ARC2_Reader($this->a, $this); + } + $this->reader->setAcceptHeader('Accept: application/json; q=0.9, */*; q=0.1'); + $this->reader->activate($path, $data); + $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; + /* parse */ + $doc = ''; + while ($d = $this->reader->readStream()) { + $doc .= $d; + } + $this->reader->closeStream(); + unset($this->reader); + $doc = preg_replace('/^[^\{]*(.*\})[^\}]*$/is', '\\1', $doc); + $this->unparsed_code = $doc; + list($this->struct, $rest) = $this->extractObject($doc); + + return $this->done(); } - return array(0, $v); - } - - function extractValue($v) { - if ($r = $this->x('\,', $v)) $v = $r[1]; - if ($sub_r = $this->x('null', $v)) { - return array(null, $sub_r[1]); + + public function extractObject($v) + { + if (function_exists('json_decode')) { + return [json_decode($v, 1), '']; + } + $r = []; + /* sub-object */ + if ($sub_r = $this->x('\{', $v)) { + $v = $sub_r[1]; + while ((list($sub_r, $v) = $this->extractEntry($v)) && $sub_r) { + $r[$sub_r['key']] = $sub_r['value']; + } + if ($sub_r = $this->x('\}', $v)) { + $v = $sub_r[1]; + } + } + /* sub-list */ + elseif ($sub_r = $this->x('\[', $v)) { + $v = $sub_r[1]; + while ((list($sub_r, $v) = $this->extractObject($v)) && $sub_r) { + $r[] = $sub_r; + $v = ltrim($v, ','); + } + if ($sub_r = $this->x('\]', $v)) { + $v = $sub_r[1]; + } + } + /* sub-value */ + elseif ((list($sub_r, $v) = $this->extractValue($v)) && (false !== $sub_r)) { + $r = $sub_r; + } + + return [$r, $v]; } - if ($sub_r = $this->x('(true|false)', $v)) { - return array($sub_r[1], $sub_r[2]); + + public function extractEntry($v) + { + if ($r = $this->x('\,', $v)) { + $v = $r[1]; + } + /* k */ + if ($r = $this->x('\"([^\"]+)\"\s*\:', $v)) { + $k = $r[1]; + $sub_v = $r[2]; + if (list($sub_r, $sub_v) = $this->extractObject($sub_v)) { + return [ + ['key' => $k, 'value' => $sub_r], + $sub_v, + ]; + } + } + + return [0, $v]; } - if ($sub_r = $this->x('([\-\+]?[0-9\.]+)', $v)) { - return array($sub_r[1], $sub_r[2]); + + public function extractValue($v) + { + if ($r = $this->x('\,', $v)) { + $v = $r[1]; + } + if ($sub_r = $this->x('null', $v)) { + return [null, $sub_r[1]]; + } + if ($sub_r = $this->x('(true|false)', $v)) { + return [$sub_r[1], $sub_r[2]]; + } + if ($sub_r = $this->x('([\-\+]?[0-9\.]+)', $v)) { + return [$sub_r[1], $sub_r[2]]; + } + if ($sub_r = $this->x('\"', $v)) { + $rest = $sub_r[1]; + if (preg_match('/^([^\x5c]*|.*[^\x5c]|.*\x5c{2})\"(.*)$/sU', $rest, $m)) { + $val = $m[1]; + /* unescape chars (single-byte) */ + $val = preg_replace_callback('/\\\u(.{4})/', function ($matches) { + return chr(hexdec($matches[1])); + }, $val); + //$val = preg_replace_callback('/\\\u00(.{2})', function($matches) { return rawurldecode("%" . $matches[1]); }, $val); + /* other escaped chars */ + $from = ['\\\\', '\r', '\t', '\n', '\"', '\b', '\f', '\/']; + $to = ['\\', "\r", "\t", "\n", '"', "\b", "\f", '/']; + $val = str_replace($from, $to, $val); + + return [$val, $m[2]]; + } + } + + return [false, $v]; } - if ($sub_r = $this->x('\"', $v)) { - $rest = $sub_r[1]; - if (preg_match('/^([^\x5c]*|.*[^\x5c]|.*\x5c{2})\"(.*)$/sU', $rest, $m)) { - $val = $m[1]; - /* unescape chars (single-byte) */ - $val = preg_replace_callback('/\\\u(.{4})/', function($matches) { - return chr(hexdec($matches[1])); - }, $val); - //$val = preg_replace_callback('/\\\u00(.{2})', function($matches) { return rawurldecode("%" . $matches[1]); }, $val); - /* other escaped chars */ - $from = array('\\\\', '\r', '\t', '\n', '\"', '\b', '\f', '\/'); - $to = array("\\", "\r", "\t", "\n", '"', "\b", "\f", "/"); - $val = str_replace($from, $to, $val); - return array($val, $m[2]); - } + + public function getObject() + { + return $this->v('struct', []); } - return array(false, $v); - } - - /* */ - - function getObject() { - return $this->v('struct', array()); - } - - function getTriples() { - return $this->v('triples', array()); - } - - function countTriples() { - return $this->t_count; - } - - function addT($s = '', $p = '', $o = '', $s_type = '', $o_type = '', $o_dt = '', $o_lang = '') { - $o = $this->toUTF8($o); - //echo str_replace($this->base, '', "-----\n adding $s / $p / $o\n-----\n"); - $t = array('s' => $s, 'p' => $p, 'o' => $o, 's_type' => $s_type, 'o_type' => $o_type, 'o_datatype' => $o_dt, 'o_lang' => $o_lang); - if ($this->skip_dupes) { - $h = md5(serialize($t)); - if (!isset($this->added_triples[$h])) { - $this->triples[$this->t_count] = $t; - $this->t_count++; - $this->added_triples[$h] = true; - } + + public function getTriples() + { + return $this->v('triples', []); } - else { - $this->triples[$this->t_count] = $t; - $this->t_count++; + + public function countTriples() + { + return $this->t_count; } - } - /* */ - + public function addT($s = '', $p = '', $o = '', $s_type = '', $o_type = '', $o_dt = '', $o_lang = '') + { + $o = $this->toUTF8($o); + //echo str_replace($this->base, '', "-----\n adding $s / $p / $o\n-----\n"); + $t = ['s' => $s, 'p' => $p, 'o' => $o, 's_type' => $s_type, 'o_type' => $o_type, 'o_datatype' => $o_dt, 'o_lang' => $o_lang]; + if ($this->skip_dupes) { + $h = md5(serialize($t)); + if (!isset($this->added_triples[$h])) { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + $this->added_triples[$h] = true; + } + } else { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + } + } } diff --git a/parsers/ARC2_LegacyXMLParser.php b/parsers/ARC2_LegacyXMLParser.php index 8c16c481..817e9795 100644 --- a/parsers/ARC2_LegacyXMLParser.php +++ b/parsers/ARC2_LegacyXMLParser.php @@ -10,302 +10,306 @@ class: ARC2 Legaxy XML Parser ARC2::inc('Class'); -class ARC2_LegacyXMLParser extends ARC2_Class { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->encoding = $this->v('encoding', false, $this->a); - $this->state = 0; - $this->x_base = $this->base; - $this->xml = 'http://www.w3.org/XML/1998/namespace'; - $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $this->nsp = array($this->xml => 'xml', $this->rdf => 'rdf'); - $this->allowCDataNodes = 1; - $this->target_encoding = ''; - $this->keep_cdata_ws = $this->v('keep_cdata_whitespace', 0, $this->a); - } - - /* */ +class ARC2_LegacyXMLParser extends ARC2_Class +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } - function setReader(&$reader) { - $this->reader = $reader; - } + public function __init() + {/* reader */ + parent::__init(); + $this->encoding = $this->v('encoding', false, $this->a); + $this->state = 0; + $this->x_base = $this->base; + $this->xml = 'http://www.w3.org/XML/1998/namespace'; + $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $this->nsp = [$this->xml => 'xml', $this->rdf => 'rdf']; + $this->allowCDataNodes = 1; + $this->target_encoding = ''; + $this->keep_cdata_ws = $this->v('keep_cdata_whitespace', 0, $this->a); + } - function parse($path, $data = '', $iso_fallback = false) { - $this->nodes = array(); - $this->node_count = 0; - $this->level = 0; - /* reader */ - if (!$this->v('reader')) { - ARC2::inc('Reader'); - $this->reader = new ARC2_Reader($this->a, $this); + public function setReader(&$reader) + { + $this->reader = $reader; } - $this->reader->setAcceptHeader('Accept: application/xml; q=0.9, */*; q=0.1'); - $this->reader->activate($path, $data); - $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; - $this->base = $this->x_base; - $this->doc_url = $this->reader->base; - /* xml parser */ - $this->initXMLParser(); - /* parse */ - $first = true; - while ($d = $this->reader->readStream(1)) { - if ($iso_fallback && $first) { - $d = '' . "\n" . preg_replace('/^\<\?xml [^\>]+\?\>\s*/s', '', $d); - } - if (!xml_parse($this->xml_parser, $d, false)) { - $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); - $line = xml_get_current_line_number($this->xml_parser); - if (!$iso_fallback && preg_match("/Invalid character/i", $error_str)) { - xml_parser_free($this->xml_parser); - unset($this->xml_parser); - $this->reader->closeStream(); - unset($this->reader); - $this->__init(); - $this->encoding = 'ISO-8859-1'; - $this->initXMLParser(); - return $this->parse($path, $data, true); + + public function parse($path, $data = '', $iso_fallback = false) + { + $this->nodes = []; + $this->node_count = 0; + $this->level = 0; + /* reader */ + if (!$this->v('reader')) { + ARC2::inc('Reader'); + $this->reader = new ARC2_Reader($this->a, $this); } - else { - return $this->addError('XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')'); + $this->reader->setAcceptHeader('Accept: application/xml; q=0.9, */*; q=0.1'); + $this->reader->activate($path, $data); + $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; + $this->base = $this->x_base; + $this->doc_url = $this->reader->base; + /* xml parser */ + $this->initXMLParser(); + /* parse */ + $first = true; + while ($d = $this->reader->readStream(1)) { + if ($iso_fallback && $first) { + $d = ''."\n".preg_replace('/^\<\?xml [^\>]+\?\>\s*/s', '', $d); + } + if (!xml_parse($this->xml_parser, $d, false)) { + $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); + $line = xml_get_current_line_number($this->xml_parser); + if (!$iso_fallback && preg_match('/Invalid character/i', $error_str)) { + xml_parser_free($this->xml_parser); + unset($this->xml_parser); + $this->reader->closeStream(); + unset($this->reader); + $this->__init(); + $this->encoding = 'ISO-8859-1'; + $this->initXMLParser(); + + return $this->parse($path, $data, true); + } else { + return $this->addError('XML error: "'.$error_str.'" at line '.$line.' (parsing as '.$this->getEncoding().')'); + } + } + $first = false; } - } - $first = false; + $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); + xml_parser_free($this->xml_parser); + $this->reader->closeStream(); + unset($this->reader); + + return $this->done(); } - $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); - xml_parser_free($this->xml_parser); - $this->reader->closeStream(); - unset($this->reader); - return $this->done(); - } - - /* */ - - function getEncoding($src = 'config') { - if ($src == 'parser') { - return $this->target_encoding; + + public function getEncoding($src = 'config') + { + if ('parser' == $src) { + return $this->target_encoding; + } elseif (('config' == $src) && $this->encoding) { + return $this->encoding; + } + + return $this->reader->getEncoding(); } - elseif (($src == 'config') && $this->encoding) { - return $this->encoding; + + public function done() + { } - return $this->reader->getEncoding(); - } - /* */ - - function done() { - - } - - /* */ - - function getStructure() { - return array('nodes' => $this->v('nodes', array())); - } - - /* */ + public function getStructure() + { + return ['nodes' => $this->v('nodes', [])]; + } - function getNodeIndex(){ - if (!isset($this->node_index)) { - /* index by parent */ - $index = array(); - for ($i = 0, $i_max = count($this->nodes); $i < $i_max; $i++) { - $node = $this->nodes[$i]; - $node['id'] = $i; - $node['doc_base'] = $this->base; - if (isset($this->doc_url)) $node['doc_url'] = $this->doc_url; - $this->updateNode($node); - $p_id = $node['p_id']; - if (!isset($index[$p_id])) { - $index[$p_id] = array(); + public function getNodeIndex() + { + if (!isset($this->node_index)) { + /* index by parent */ + $index = []; + for ($i = 0, $i_max = count($this->nodes); $i < $i_max; ++$i) { + $node = $this->nodes[$i]; + $node['id'] = $i; + $node['doc_base'] = $this->base; + if (isset($this->doc_url)) { + $node['doc_url'] = $this->doc_url; + } + $this->updateNode($node); + $p_id = $node['p_id']; + if (!isset($index[$p_id])) { + $index[$p_id] = []; + } + $index[$p_id][$node['pos']] = $node; + } + $this->node_index = $index; } - $index[$p_id][$node['pos']] = $node; - } - $this->node_index = $index; + + return $this->node_index; } - return $this->node_index; - } - function getNodes() { - return $this->nodes; - } - - function getSubNodes($n) { - return $this->v($n['id'], array(), $this->getNodeIndex()); - } - - function getNodeContent($n, $outer = 0, $trim = 1) { - //echo '
' . htmlspecialchars(print_r($n, 1)) . '
'; - if ($n['tag'] == 'cdata') { - $r = $n['a']['value']; + public function getNodes() + { + return $this->nodes; } - else { - $r = ''; - if ($outer) { - $r .= '<' . $n['tag']; - asort($n['a']); - if (isset($n['a']['xmlns']) && $n['a']['xmlns']['']) { - $r .= ' xmlns="' . $n['a']['xmlns'][''] . '"'; - } - foreach ($n['a'] as $a => $val) { - $r .= preg_match('/^[^\s]+$/', $a) && !is_array($val) ? ' ' . $a . '="' . addslashes($val) . '"' : ''; - } - $r .= $n['empty'] ? '/>' : '>'; - } - if (!$n['empty']) { - $r .= $this->v('cdata', '', $n); - $sub_nodes = $this->getSubNodes($n); - foreach ($sub_nodes as $sub_n) { - $r .= $this->getNodeContent($sub_n, 1, 0); - } - if ($outer) { - $r .= ''; - } - } + + public function getSubNodes($n) + { + return $this->v($n['id'], [], $this->getNodeIndex()); } - return ($trim && !$this->keep_cdata_ws) ? trim($r) : $r; - } - /* */ - - function pushNode($n) { - $n['id'] = $this->node_count; - $this->nodes[$this->node_count] = $n; - $this->node_count++; - } - - function getCurNode($t = '') { - $i = 1; - do { - $r = $this->node_count ? $this->nodes[$this->node_count - $i] : 0; - $found = (!$t || ($r['tag'] == $t)) ? 1 : 0; - $i++; - } while (!$found && isset($this->nodes[$this->node_count - $i])); - return $r; - } - - function updateNode($node) {/* php4-save */ - $this->nodes[$node['id']] = $node; - } + public function getNodeContent($n, $outer = 0, $trim = 1) + { + //echo '
' . htmlspecialchars(print_r($n, 1)) . '
'; + if ('cdata' == $n['tag']) { + $r = $n['a']['value']; + } else { + $r = ''; + if ($outer) { + $r .= '<'.$n['tag']; + asort($n['a']); + if (isset($n['a']['xmlns']) && $n['a']['xmlns']['']) { + $r .= ' xmlns="'.$n['a']['xmlns'][''].'"'; + } + foreach ($n['a'] as $a => $val) { + $r .= preg_match('/^[^\s]+$/', $a) && !is_array($val) ? ' '.$a.'="'.addslashes($val).'"' : ''; + } + $r .= $n['empty'] ? '/>' : '>'; + } + if (!$n['empty']) { + $r .= $this->v('cdata', '', $n); + $sub_nodes = $this->getSubNodes($n); + foreach ($sub_nodes as $sub_n) { + $r .= $this->getNodeContent($sub_n, 1, 0); + } + if ($outer) { + $r .= ''; + } + } + } - /* */ + return ($trim && !$this->keep_cdata_ws) ? trim($r) : $r; + } - function initXMLParser() { - if (!isset($this->xml_parser)) { - $enc = preg_match('/^(utf\-8|iso\-8859\-1|us\-ascii)$/i', $this->getEncoding(), $m) ? $m[1] : 'UTF-8'; - $parser = xml_parser_create_ns($enc, ''); - xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); - xml_set_element_handler($parser, 'open', 'close'); - xml_set_character_data_handler($parser, 'cData'); - xml_set_start_namespace_decl_handler($parser, 'nsDecl'); - xml_set_object($parser, $this); - $this->xml_parser = $parser; + public function pushNode($n) + { + $n['id'] = $this->node_count; + $this->nodes[$this->node_count] = $n; + ++$this->node_count; } - } - /* */ - - function open($p, $t, $a) { - $t_exact = $t; - //echo "
\n".'opening '.$t . ' ' . print_r($a, 1); flush(); - //echo "
\n".'opening '.$t; flush(); - $t = strpos($t, ':') ? $t : strtolower($t); - /* base check */ - $base = ''; - if (($t == 'base') && isset($a['href'])) { - $this->base = $a['href']; - $base = $a['href']; + public function getCurNode($t = '') + { + $i = 1; + do { + $r = $this->node_count ? $this->nodes[$this->node_count - $i] : 0; + $found = (!$t || ($r['tag'] == $t)) ? 1 : 0; + ++$i; + } while (!$found && isset($this->nodes[$this->node_count - $i])); + + return $r; } - /* URIs */ - foreach (array('href', 'src', 'id') as $uri_a) { - if (isset($a[$uri_a])) { - $a[$uri_a . ' uri'] = ($uri_a == 'id') ? $this->calcURI('#'.$a[$uri_a]) : $this->calcURI($a[$uri_a]); - } + + public function updateNode($node) + {/* php4-save */ + $this->nodes[$node['id']] = $node; } - /* ns */ - if ($a) { - foreach ($a as $k => $v) { - if (strpos($k, 'xmlns') === 0) { - $this->nsDecl($p, trim(substr($k, 5), ':'), $v); + + public function initXMLParser() + { + if (!isset($this->xml_parser)) { + $enc = preg_match('/^(utf\-8|iso\-8859\-1|us\-ascii)$/i', $this->getEncoding(), $m) ? $m[1] : 'UTF-8'; + $parser = xml_parser_create_ns($enc, ''); + xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); + xml_set_element_handler($parser, 'open', 'close'); + xml_set_character_data_handler($parser, 'cData'); + xml_set_start_namespace_decl_handler($parser, 'nsDecl'); + xml_set_object($parser, $this); + $this->xml_parser = $parser; } - } } - /* node */ - $node = array( - 'tag' => $t, - 'tag_exact' => $t_exact, - 'a' => $a, - 'level' => $this->level, - 'pos' => 0, - 'p_id' => $this->node_count-1, - 'state' => 'open', - 'empty' => 0, - 'cdata' =>'' - ); - if ($base) { - $node['base'] = $base; - } - /* parent/sibling */ - if ($this->node_count) { - $l = $this->level; - $prev_node = $this->getCurNode(); - if ($prev_node['level'] == $l) { - $node['p_id'] = $prev_node['p_id']; - $node['pos'] = $prev_node['pos']+1; - } - elseif($prev_node['level'] > $l) { - while($prev_node['level'] > $l) { - if (!isset($this->nodes[$prev_node['p_id']])) { - //$this->addError('nesting mismatch: tag is ' . $t . ', level is ' . $l . ', prev_level is ' . $prev_node['level'] . ', prev_node p_id is ' . $prev_node['p_id']); - break; - } - $prev_node = $this->nodes[$prev_node['p_id']]; + + public function open($p, $t, $a) + { + $t_exact = $t; + //echo "
\n".'opening '.$t . ' ' . print_r($a, 1); flush(); + //echo "
\n".'opening '.$t; flush(); + $t = strpos($t, ':') ? $t : strtolower($t); + /* base check */ + $base = ''; + if (('base' == $t) && isset($a['href'])) { + $this->base = $a['href']; + $base = $a['href']; + } + /* URIs */ + foreach (['href', 'src', 'id'] as $uri_a) { + if (isset($a[$uri_a])) { + $a[$uri_a.' uri'] = ('id' == $uri_a) ? $this->calcURI('#'.$a[$uri_a]) : $this->calcURI($a[$uri_a]); + } + } + /* ns */ + if ($a) { + foreach ($a as $k => $v) { + if (0 === strpos($k, 'xmlns')) { + $this->nsDecl($p, trim(substr($k, 5), ':'), $v); + } + } } - $node['p_id'] = $prev_node['p_id']; - $node['pos'] = $prev_node['pos']+1; - } + /* node */ + $node = [ + 'tag' => $t, + 'tag_exact' => $t_exact, + 'a' => $a, + 'level' => $this->level, + 'pos' => 0, + 'p_id' => $this->node_count - 1, + 'state' => 'open', + 'empty' => 0, + 'cdata' => '', + ]; + if ($base) { + $node['base'] = $base; + } + /* parent/sibling */ + if ($this->node_count) { + $l = $this->level; + $prev_node = $this->getCurNode(); + if ($prev_node['level'] == $l) { + $node['p_id'] = $prev_node['p_id']; + $node['pos'] = $prev_node['pos'] + 1; + } elseif ($prev_node['level'] > $l) { + while ($prev_node['level'] > $l) { + if (!isset($this->nodes[$prev_node['p_id']])) { + //$this->addError('nesting mismatch: tag is ' . $t . ', level is ' . $l . ', prev_level is ' . $prev_node['level'] . ', prev_node p_id is ' . $prev_node['p_id']); + break; + } + $prev_node = $this->nodes[$prev_node['p_id']]; + } + $node['p_id'] = $prev_node['p_id']; + $node['pos'] = $prev_node['pos'] + 1; + } + } + $this->pushNode($node); + ++$this->level; + /* cdata */ + $this->cur_cdata = ''; } - $this->pushNode($node); - $this->level++; - /* cdata */ - $this->cur_cdata=""; - } - function close($p, $t, $empty = 0) { - //echo "
\n".'closing '.$t; flush(); - $node = $this->getCurNode($t); - $node['state'] = 'closed'; - $node['empty'] = $empty; - $this->updateNode($node); - $this->level--; - } - - function cData($p, $d) { - //echo trim($d) ? "
\n".'cdata: ' . $d : ''; flush(); - $node = $this->getCurNode(); - if($node['state'] == 'open') { - $node['cdata'] .= $d; - $this->updateNode($node); + public function close($p, $t, $empty = 0) + { + //echo "
\n".'closing '.$t; flush(); + $node = $this->getCurNode($t); + $node['state'] = 'closed'; + $node['empty'] = $empty; + $this->updateNode($node); + --$this->level; } - else {/* cdata is sibling of node */ - if ($this->allowCDataNodes) { - $this->open($p, 'cdata', array('value' => $d)); - $this->close($p, 'cdata'); - } + + public function cData($p, $d) + { + //echo trim($d) ? "
\n".'cdata: ' . $d : ''; flush(); + $node = $this->getCurNode(); + if ('open' == $node['state']) { + $node['cdata'] .= $d; + $this->updateNode($node); + } else {/* cdata is sibling of node */ + if ($this->allowCDataNodes) { + $this->open($p, 'cdata', ['value' => $d]); + $this->close($p, 'cdata'); + } + } } - } - - function nsDecl($p, $prf, $uri) { - if (is_array($uri)) return 1; - $this->ns[$prf] = $uri; - $this->nsp[$uri] = isset($this->nsp[$uri]) ? $this->nsp[$uri] : $prf; - } - /* */ - -} \ No newline at end of file + public function nsDecl($p, $prf, $uri) + { + if (is_array($uri)) { + return 1; + } + $this->ns[$prf] = $uri; + $this->nsp[$uri] = isset($this->nsp[$uri]) ? $this->nsp[$uri] : $prf; + } +} diff --git a/parsers/ARC2_RDFParser.php b/parsers/ARC2_RDFParser.php index df27601e..75902f16 100755 --- a/parsers/ARC2_RDFParser.php +++ b/parsers/ARC2_RDFParser.php @@ -1,139 +1,144 @@ * @license W3C Software License and GPL * @homepage - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('Class'); -class ARC2_RDFParser extends ARC2_Class { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* proxy_host, proxy_port, proxy_skip, http_accept_header, http_user_agent_header, max_redirects, reader, skip_dupes */ - parent::__init(); - $this->a['format'] = $this->v('format', false, $this->a); - $this->keep_time_limit = $this->v('keep_time_limit', 0, $this->a); - $this->triples = array(); - $this->t_count = 0; - $this->added_triples = array(); - $this->skip_dupes = $this->v('skip_dupes', false, $this->a); - $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); - $this->bnode_id = 0; - $this->format = ''; - } - - /* */ - - function setReader(&$reader) { - $this->reader = $reader; - } - - function parse($path, $data = '') { - /* reader */ - if (!isset($this->reader)) { - ARC2::inc('Reader'); - $this->reader = new ARC2_Reader($this->a, $this); +class ARC2_RDFParser extends ARC2_Class +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } + + public function __init() + {/* proxy_host, proxy_port, proxy_skip, http_accept_header, http_user_agent_header, max_redirects, reader, skip_dupes */ + parent::__init(); + $this->a['format'] = $this->v('format', false, $this->a); + $this->keep_time_limit = $this->v('keep_time_limit', 0, $this->a); + $this->triples = []; + $this->t_count = 0; + $this->added_triples = []; + $this->skip_dupes = $this->v('skip_dupes', false, $this->a); + $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); + $this->bnode_id = 0; + $this->format = ''; + } + + public function setReader(&$reader) + { + $this->reader = $reader; + } + + public function parse($path, $data = '') + { + /* reader */ + if (!isset($this->reader)) { + ARC2::inc('Reader'); + $this->reader = new ARC2_Reader($this->a, $this); + } + $this->reader->activate($path, $data); + /* format detection */ + $mappings = [ + 'rdfxml' => 'RDFXML', + 'turtle' => 'Turtle', + 'sparqlxml' => 'SPOG', + 'ntriples' => 'Turtle', + 'html' => 'SemHTML', + 'rss' => 'RSS', + 'atom' => 'Atom', + 'sgajson' => 'SGAJSON', + 'cbjson' => 'CBJSON', + ]; + $format = $this->reader->getFormat(); + if (!$format || !isset($mappings[$format])) { + return $this->addError('No parser available for "'.$format.'".'); + } + $this->format = $format; + /* format parser */ + $suffix = $mappings[$format].'Parser'; + ARC2::inc($suffix); + $cls = 'ARC2_'.$suffix; + $this->parser = new $cls($this->a, $this); + $this->parser->setReader($this->reader); + + return $this->parser->parse($path, $data); + } + + public function parseData($data) + { + return $this->parse(ARC2::getScriptURI(), $data); } - $this->reader->activate($path, $data) ; - /* format detection */ - $mappings = array( - 'rdfxml' => 'RDFXML', - 'turtle' => 'Turtle', - 'sparqlxml' => 'SPOG', - 'ntriples' => 'Turtle', - 'html' => 'SemHTML', - 'rss' => 'RSS', - 'atom' => 'Atom', - 'sgajson' => 'SGAJSON', - 'cbjson' => 'CBJSON' - ); - $format = $this->reader->getFormat(); - if (!$format || !isset($mappings[$format])) { - return $this->addError('No parser available for "' . $format . '".'); + + public function done() + { } - $this->format = $format; - /* format parser */ - $suffix = $mappings[$format] . 'Parser'; - ARC2::inc($suffix); - $cls = 'ARC2_' . $suffix; - $this->parser = new $cls($this->a, $this); - $this->parser->setReader($this->reader); - return $this->parser->parse($path, $data); - } - - function parseData($data) { - return $this->parse(ARC2::getScriptURI(), $data); - } - - /* */ - - function done() { - } - - /* */ - - function createBnodeID(){ - $this->bnode_id++; - return '_:' . $this->bnode_prefix . $this->bnode_id; - } - - function getTriples() { - return $this->v('parser') ? $this->m('getTriples', false, array(), $this->v('parser')) : array(); - } - - function countTriples() { - return $this->v('parser') ? $this->m('countTriples', false, 0, $this->v('parser')) : 0; - } - - function getSimpleIndex($flatten_objects = 1, $vals = '') { - return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals); - } - - function reset() { - $this->__init(); - if (isset($this->reader)) unset($this->reader); - if (isset($this->parser)) { - $this->parser->__init(); - unset($this->parser); + + public function createBnodeID() + { + ++$this->bnode_id; + + return '_:'.$this->bnode_prefix.$this->bnode_id; } - } - - /* */ - - function extractRDF($formats = '') { - if (method_exists($this->parser, 'extractRDF')) { - return $this->parser->extractRDF($formats); + + public function getTriples() + { + return $this->v('parser') ? $this->m('getTriples', false, [], $this->v('parser')) : []; } - } - - /* */ - - function getEncoding($src = 'config') { - if (method_exists($this->parser, 'getEncoding')) { - return $this->parser->getEncoding($src); + + public function countTriples() + { + return $this->v('parser') ? $this->m('countTriples', false, 0, $this->v('parser')) : 0; } - } - /** - * returns the array of namespace prefixes encountered during parsing - * @return array (keys = namespace URI / values = prefix used) - */ + public function getSimpleIndex($flatten_objects = 1, $vals = '') + { + return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals); + } - function getParsedNamespacePrefixes() { - if (isset($this->parser)) { - return $this->v('nsp', array(), $this->parser); + public function reset() + { + $this->__init(); + if (isset($this->reader)) { + unset($this->reader); + } + if (isset($this->parser)) { + $this->parser->__init(); + unset($this->parser); + } } - return $this->v('nsp', array()); - } - /* */ + public function extractRDF($formats = '') + { + if (method_exists($this->parser, 'extractRDF')) { + return $this->parser->extractRDF($formats); + } + } + + public function getEncoding($src = 'config') + { + if (method_exists($this->parser, 'getEncoding')) { + return $this->parser->getEncoding($src); + } + } + /** + * returns the array of namespace prefixes encountered during parsing. + * + * @return array (keys = namespace URI / values = prefix used) + */ + public function getParsedNamespacePrefixes() + { + if (isset($this->parser)) { + return $this->v('nsp', [], $this->parser); + } + + return $this->v('nsp', []); + } } diff --git a/parsers/ARC2_RDFXMLParser.php b/parsers/ARC2_RDFXMLParser.php index 37d58a24..02e8d6c6 100644 --- a/parsers/ARC2_RDFXMLParser.php +++ b/parsers/ARC2_RDFXMLParser.php @@ -1,640 +1,622 @@ * @license W3C Software License and GPL * @homepage - * @package ARC2 -*/ - + */ ARC2::inc('RDFParser'); -class ARC2_RDFXMLParser extends ARC2_RDFParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->encoding = $this->v('encoding', false, $this->a); - $this->state = 0; - $this->x_lang = ''; - $this->x_base = $this->base; - $this->xml = 'http://www.w3.org/XML/1998/namespace'; - $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $this->nsp = array($this->xml => 'xml', $this->rdf => 'rdf'); - $this->s_stack = array(); - $this->s_count = 0; - $this->target_encoding = ''; - } - - /* */ - - function parse($path, $data = '', $iso_fallback = false) { - /* reader */ - if (!$this->v('reader')) { - ARC2::inc('Reader'); - $this->reader = new ARC2_Reader($this->a, $this); - } - $this->reader->setAcceptHeader('Accept: application/rdf+xml; q=0.9, */*; q=0.1'); - $this->reader->activate($path, $data); - $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; - /* xml parser */ - $this->initXMLParser(); - /* parse */ - $first = true; - while ($d = $this->reader->readStream()) { - if (!$this->keep_time_limit) @set_time_limit($this->v('time_limit', 60, $this->a)); - if ($iso_fallback && $first) { - $d = '' . "\n" . preg_replace('/^\<\?xml [^\>]+\?\>\s*/s', '', $d); - $first = false; - } - if (!xml_parse($this->xml_parser, $d, false)) { - $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); - $line = xml_get_current_line_number($this->xml_parser); - $this->tmp_error = 'XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')'; - if (!$iso_fallback && preg_match("/Invalid character/i", $error_str)) { - xml_parser_free($this->xml_parser); - unset($this->xml_parser); - $this->reader->closeStream(); - $this->__init(); - $this->encoding = 'ISO-8859-1'; - unset($this->xml_parser); - unset($this->reader); - return $this->parse($path, $data, true); +class ARC2_RDFXMLParser extends ARC2_RDFParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } + + public function __init() + {/* reader */ + parent::__init(); + $this->encoding = $this->v('encoding', false, $this->a); + $this->state = 0; + $this->x_lang = ''; + $this->x_base = $this->base; + $this->xml = 'http://www.w3.org/XML/1998/namespace'; + $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $this->nsp = [$this->xml => 'xml', $this->rdf => 'rdf']; + $this->s_stack = []; + $this->s_count = 0; + $this->target_encoding = ''; + } + + public function parse($path, $data = '', $iso_fallback = false) + { + /* reader */ + if (!$this->v('reader')) { + ARC2::inc('Reader'); + $this->reader = new ARC2_Reader($this->a, $this); } - else { - return $this->addError($this->tmp_error); - } - } - } - $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); - xml_parser_free($this->xml_parser); - $this->reader->closeStream(); - unset($this->reader); - return $this->done(); - } - - /* */ - - function initXMLParser() { - if (!isset($this->xml_parser)) { - $enc = preg_match('/^(utf\-8|iso\-8859\-1|us\-ascii)$/i', $this->getEncoding(), $m) ? $m[1] : 'UTF-8'; - $parser = xml_parser_create_ns($enc, ''); - xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); - xml_set_element_handler($parser, 'open', 'close'); - xml_set_character_data_handler($parser, 'cdata'); - xml_set_start_namespace_decl_handler($parser, 'nsDecl'); - xml_set_object($parser, $this); - $this->xml_parser = $parser; - } - } - - /* */ - - function getEncoding($src = 'config') { - if ($src == 'parser') { - return $this->target_encoding; - } - elseif (($src == 'config') && $this->encoding) { - return $this->encoding; - } - return $this->reader->getEncoding(); - } - - /* */ - - function getTriples() { - return $this->v('triples', array()); - } - - function countTriples() { - return $this->t_count; - } - - /* */ - - function pushS(&$s) { - $s['pos'] = $this->s_count; - $this->s_stack[$this->s_count] = $s; - $this->s_count++; - } - - function popS(){/* php 4.0.x-safe */ - $r = array(); - $this->s_count--; - for ($i = 0, $i_max = $this->s_count; $i < $i_max; $i++) { - $r[$i] = $this->s_stack[$i]; - } - $this->s_stack = $r; - } - - function updateS($s) { - $this->s_stack[$s['pos']] = $s; - } - - function getParentS() { - return ($this->s_count && isset($this->s_stack[$this->s_count - 1])) ? $this->s_stack[$this->s_count - 1] : false; - } - - function getParentXBase() { - if ($p = $this->getParentS()) { - return isset($p['p_x_base']) && $p['p_x_base'] ? $p['p_x_base'] : (isset($p['x_base']) ? $p['x_base'] : ''); - } - return $this->x_base; - } - - function getParentXLang() { - if ($p = $this->getParentS()) { - return isset($p['p_x_lang']) && $p['p_x_lang'] ? $p['p_x_lang'] : (isset($p['x_lang']) ? $p['x_lang'] : ''); - } - return $this->x_lang; - } - - /* */ - - function addT($s, $p, $o, $s_type, $o_type, $o_dt = '', $o_lang = '') { - //echo "-----\nadding $s / $p / $o\n-----\n"; - $t = array('s' => $s, 'p' => $p, 'o' => $o, 's_type' => $s_type, 'o_type' => $o_type, 'o_datatype' => $o_dt, 'o_lang' => $o_lang); - if ($this->skip_dupes) { - $h = md5(serialize($t)); - if (!isset($this->added_triples[$h])) { - $this->triples[$this->t_count] = $t; - $this->t_count++; - $this->added_triples[$h] = true; - } - } - else { - $this->triples[$this->t_count] = $t; - $this->t_count++; - } - } - - function reify($t, $s, $p, $o, $s_type, $o_type, $o_dt = '', $o_lang = '') { - $this->addT($t, $this->rdf.'type', $this->rdf.'Statement', 'uri', 'uri'); - $this->addT($t, $this->rdf.'subject', $s, 'uri', $s_type); - $this->addT($t, $this->rdf.'predicate', $p, 'uri', 'uri'); - $this->addT($t, $this->rdf.'object', $o, 'uri', $o_type, $o_dt, $o_lang); - } - - /* */ - - function open($p, $t, $a) { - //echo "state is $this->state\n"; - //echo "opening $t\n"; - switch($this->state) { + $this->reader->setAcceptHeader('Accept: application/rdf+xml; q=0.9, */*; q=0.1'); + $this->reader->activate($path, $data); + $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; + /* xml parser */ + $this->initXMLParser(); + /* parse */ + $first = true; + while ($d = $this->reader->readStream()) { + if (!$this->keep_time_limit) { + @set_time_limit($this->v('time_limit', 60, $this->a)); + } + if ($iso_fallback && $first) { + $d = ''."\n".preg_replace('/^\<\?xml [^\>]+\?\>\s*/s', '', $d); + $first = false; + } + if (!xml_parse($this->xml_parser, $d, false)) { + $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); + $line = xml_get_current_line_number($this->xml_parser); + $this->tmp_error = 'XML error: "'.$error_str.'" at line '.$line.' (parsing as '.$this->getEncoding().')'; + if (!$iso_fallback && preg_match('/Invalid character/i', $error_str)) { + xml_parser_free($this->xml_parser); + unset($this->xml_parser); + $this->reader->closeStream(); + $this->__init(); + $this->encoding = 'ISO-8859-1'; + unset($this->xml_parser); + unset($this->reader); + + return $this->parse($path, $data, true); + } else { + return $this->addError($this->tmp_error); + } + } + } + $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); + xml_parser_free($this->xml_parser); + $this->reader->closeStream(); + unset($this->reader); + + return $this->done(); + } + + public function initXMLParser() + { + if (!isset($this->xml_parser)) { + $enc = preg_match('/^(utf\-8|iso\-8859\-1|us\-ascii)$/i', $this->getEncoding(), $m) ? $m[1] : 'UTF-8'; + $parser = xml_parser_create_ns($enc, ''); + xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); + xml_set_element_handler($parser, 'open', 'close'); + xml_set_character_data_handler($parser, 'cdata'); + xml_set_start_namespace_decl_handler($parser, 'nsDecl'); + xml_set_object($parser, $this); + $this->xml_parser = $parser; + } + } + + public function getEncoding($src = 'config') + { + if ('parser' == $src) { + return $this->target_encoding; + } elseif (('config' == $src) && $this->encoding) { + return $this->encoding; + } + + return $this->reader->getEncoding(); + } + + public function getTriples() + { + return $this->v('triples', []); + } + + public function countTriples() + { + return $this->t_count; + } + + public function pushS(&$s) + { + $s['pos'] = $this->s_count; + $this->s_stack[$this->s_count] = $s; + ++$this->s_count; + } + + public function popS() + {/* php 4.0.x-safe */ + $r = []; + --$this->s_count; + for ($i = 0, $i_max = $this->s_count; $i < $i_max; ++$i) { + $r[$i] = $this->s_stack[$i]; + } + $this->s_stack = $r; + } + + public function updateS($s) + { + $this->s_stack[$s['pos']] = $s; + } + + public function getParentS() + { + return ($this->s_count && isset($this->s_stack[$this->s_count - 1])) ? $this->s_stack[$this->s_count - 1] : false; + } + + public function getParentXBase() + { + if ($p = $this->getParentS()) { + return isset($p['p_x_base']) && $p['p_x_base'] ? $p['p_x_base'] : (isset($p['x_base']) ? $p['x_base'] : ''); + } + + return $this->x_base; + } + + public function getParentXLang() + { + if ($p = $this->getParentS()) { + return isset($p['p_x_lang']) && $p['p_x_lang'] ? $p['p_x_lang'] : (isset($p['x_lang']) ? $p['x_lang'] : ''); + } + + return $this->x_lang; + } + + public function addT($s, $p, $o, $s_type, $o_type, $o_dt = '', $o_lang = '') + { + //echo "-----\nadding $s / $p / $o\n-----\n"; + $t = ['s' => $s, 'p' => $p, 'o' => $o, 's_type' => $s_type, 'o_type' => $o_type, 'o_datatype' => $o_dt, 'o_lang' => $o_lang]; + if ($this->skip_dupes) { + $h = md5(serialize($t)); + if (!isset($this->added_triples[$h])) { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + $this->added_triples[$h] = true; + } + } else { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + } + } + + public function reify($t, $s, $p, $o, $s_type, $o_type, $o_dt = '', $o_lang = '') + { + $this->addT($t, $this->rdf.'type', $this->rdf.'Statement', 'uri', 'uri'); + $this->addT($t, $this->rdf.'subject', $s, 'uri', $s_type); + $this->addT($t, $this->rdf.'predicate', $p, 'uri', 'uri'); + $this->addT($t, $this->rdf.'object', $o, 'uri', $o_type, $o_dt, $o_lang); + } + + public function open($p, $t, $a) + { + //echo "state is $this->state\n"; + //echo "opening $t\n"; + switch ($this->state) { case 0: return $this->h0Open($t, $a); case 1: return $this->h1Open($t, $a); case 2: return $this->h2Open($t, $a); case 4: return $this->h4Open($t, $a); case 5: return $this->h5Open($t, $a); case 6: return $this->h6Open($t, $a); - default: $this->addError('open() called at state ' . $this->state . ' in '.$t); + default: $this->addError('open() called at state '.$this->state.' in '.$t); + } } - } - function close($p, $t) { - //echo "state is $this->state\n"; - //echo "closing $t\n"; - switch($this->state){ + public function close($p, $t) + { + //echo "state is $this->state\n"; + //echo "closing $t\n"; + switch ($this->state) { case 1: return $this->h1Close($t); case 2: return $this->h2Close($t); case 3: return $this->h3Close($t); case 4: return $this->h4Close($t); case 5: return $this->h5Close($t); case 6: return $this->h6Close($t); - default: $this->addError('close() called at state ' . $this->state . ' in '.$t); + default: $this->addError('close() called at state '.$this->state.' in '.$t); + } } - } - function cdata($p, $d) { - //echo "state is $this->state\n"; - //echo "cdata\n"; - switch($this->state){ + public function cdata($p, $d) + { + //echo "state is $this->state\n"; + //echo "cdata\n"; + switch ($this->state) { case 4: return $this->h4Cdata($d); case 6: return $this->h6Cdata($d); default: return false; } - } - - function nsDecl($p, $prf, $uri) { - $this->nsp[$uri] = isset($this->nsp[$uri]) ? $this->nsp[$uri] : $prf; - } - - /* */ - - function h0Open($t, $a) { - $this->x_lang = $this->v($this->xml.'lang', $this->x_lang, $a); - $this->x_base = $this->calcURI($this->v($this->xml.'base', $this->x_base, $a)); - $this->state = 1; - if ($t !== $this->rdf.'RDF') { - $this->h1Open($t, $a); - } - } - - /* */ - - function h1Open($t, $a) { - $s = array( - 'x_base' => isset($a[$this->xml.'base']) ? $this->calcURI($a[$this->xml.'base']) : $this->getParentXBase(), - 'x_lang' => isset($a[$this->xml.'lang']) ? $a[$this->xml.'lang'] : $this->getParentXLang(), - 'li_count' => 0, - ); - /* ID */ - if (isset($a[$this->rdf.'ID'])) { - $s['type'] = 'uri'; - $s['value'] = $this->calcURI('#'.$a[$this->rdf.'ID'], $s['x_base']); - } - /* about */ - elseif (isset($a[$this->rdf.'about'])) { - $s['type'] = 'uri'; - $s['value'] = $this->calcURI($a[$this->rdf.'about'], $s['x_base']); - } - /* bnode */ - else { - $s['type'] = 'bnode'; - if (isset($a[$this->rdf.'nodeID'])) { - $s['value'] = '_:'.$a[$this->rdf.'nodeID']; - } - else { - $s['value'] = $this->createBnodeID(); - } - } - /* sub-node */ - if ($this->state === 4) { - $sup_s = $this->getParentS(); - /* new collection */ - if (isset($sup_s['o_is_coll']) && $sup_s['o_is_coll']) { - $coll = array('value' => $this->createBnodeID(), 'type' => 'bnode', 'is_coll' => true, 'x_base' => $s['x_base'], 'x_lang' => $s['x_lang']); - $this->addT($sup_s['value'], $sup_s['p'], $coll['value'], $sup_s['type'], $coll['type']); - $this->addT($coll['value'], $this->rdf . 'first', $s['value'], $coll['type'], $s['type']); - $this->pushS($coll); - } - /* new entry in existing coll */ - elseif (isset($sup_s['is_coll']) && $sup_s['is_coll']) { - $coll = array('value' => $this->createBnodeID(), 'type' => 'bnode', 'is_coll' => true, 'x_base' => $s['x_base'], 'x_lang' => $s['x_lang']); - $this->addT($sup_s['value'], $this->rdf . 'rest', $coll['value'], $sup_s['type'], $coll['type']); - $this->addT($coll['value'], $this->rdf . 'first', $s['value'], $coll['type'], $s['type']); - $this->pushS($coll); - } - /* normal sub-node */ - elseif(isset($sup_s['p']) && $sup_s['p']) { - $this->addT($sup_s['value'], $sup_s['p'], $s['value'], $sup_s['type'], $s['type']); - } - } - /* typed node */ - if ($t !== $this->rdf.'Description') { - $this->addT($s['value'], $this->rdf.'type', $t, $s['type'], 'uri'); - } - /* (additional) typing attr */ - if (isset($a[$this->rdf.'type'])) { - $this->addT($s['value'], $this->rdf.'type', $a[$this->rdf.'type'], $s['type'], 'uri'); - } - /* Seq|Bag|Alt */ - if (in_array($t, array($this->rdf.'Seq', $this->rdf.'Bag', $this->rdf.'Alt'))) { - $s['is_con'] = true; - } - /* any other attrs (skip rdf and xml, except rdf:_, rdf:value, rdf:Seq) */ - foreach($a as $k => $v) { - if (((strpos($k, $this->xml) === false) && (strpos($k, $this->rdf) === false)) || preg_match('/(\_[0-9]+|value|Seq|Bag|Alt|Statement|Property|List)$/', $k)) { - if (strpos($k, ':')) { - $this->addT($s['value'], $k, $v, $s['type'], 'literal', '', $s['x_lang']); - } - } - } - $this->pushS($s); - $this->state = 2; - } - - /* */ - - function h2Open($t, $a) { - $s = $this->getParentS(); - foreach (array('p_x_base', 'p_x_lang', 'p_id', 'o_is_coll') as $k) { - unset($s[$k]); - } - /* base */ - if (isset($a[$this->xml.'base'])) { - $s['p_x_base'] = $this->calcURI($a[$this->xml.'base'], $s['x_base']); - } - $b = isset($s['p_x_base']) && $s['p_x_base'] ? $s['p_x_base'] : $s['x_base']; - /* lang */ - if (isset($a[$this->xml.'lang'])) { - $s['p_x_lang'] = $a[$this->xml.'lang']; - } - $l = isset($s['p_x_lang']) && $s['p_x_lang'] ? $s['p_x_lang'] : $s['x_lang']; - /* adjust li */ - if ($t === $this->rdf.'li') { - $s['li_count']++; - $t = $this->rdf.'_'.$s['li_count']; - } - /* set p */ - $s['p'] = $t; - /* reification */ - if (isset($a[$this->rdf.'ID'])) { - $s['p_id'] = $a[$this->rdf.'ID']; - } - $o = array('value' => '', 'type' => '', 'x_base' => $b, 'x_lang' => $l); - /* resource/rdf:resource */ - if (isset($a['resource'])) { - $a[$this->rdf . 'resource'] = $a['resource']; - unset($a['resource']); - } - if (isset($a[$this->rdf.'resource'])) { - $o['value'] = $this->calcURI($a[$this->rdf.'resource'], $b); - $o['type'] = 'uri'; - $this->addT($s['value'], $s['p'], $o['value'], $s['type'], $o['type']); - /* type */ - if (isset($a[$this->rdf.'type'])) { - $this->addT($o['value'], $this->rdf.'type', $a[$this->rdf.'type'], 'uri', 'uri'); - } - /* reification */ - if (isset($s['p_id'])) { - $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type']); - unset($s['p_id']); - } - $this->state = 3; - } - /* named bnode */ - elseif (isset($a[$this->rdf.'nodeID'])) { - $o['value'] = '_:' . $a[$this->rdf.'nodeID']; - $o['type'] = 'bnode'; - $this->addT($s['value'], $s['p'], $o['value'], $s['type'], $o['type']); - $this->state = 3; - /* reification */ - if (isset($s['p_id'])) { - $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type']); - } - } - /* parseType */ - elseif (isset($a[$this->rdf.'parseType'])) { - if ($a[$this->rdf.'parseType'] === 'Literal') { - $s['o_xml_level'] = 0; - $s['o_xml_data'] = ''; - $s['p_xml_literal_level'] = 0; - $s['ns'] = array(); - $this->state = 6; - } - elseif ($a[$this->rdf.'parseType'] === 'Resource') { - $o['value'] = $this->createBnodeID(); - $o['type'] = 'bnode'; - $o['has_closing_tag'] = 0; - $this->addT($s['value'], $s['p'], $o['value'], $s['type'], $o['type']); - $this->pushS($o); - /* reification */ - if (isset($s['p_id'])) { - $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type']); - unset($s['p_id']); + } + + public function nsDecl($p, $prf, $uri) + { + $this->nsp[$uri] = isset($this->nsp[$uri]) ? $this->nsp[$uri] : $prf; + } + + public function h0Open($t, $a) + { + $this->x_lang = $this->v($this->xml.'lang', $this->x_lang, $a); + $this->x_base = $this->calcURI($this->v($this->xml.'base', $this->x_base, $a)); + $this->state = 1; + if ($t !== $this->rdf.'RDF') { + $this->h1Open($t, $a); + } + } + + public function h1Open($t, $a) + { + $s = [ + 'x_base' => isset($a[$this->xml.'base']) ? $this->calcURI($a[$this->xml.'base']) : $this->getParentXBase(), + 'x_lang' => isset($a[$this->xml.'lang']) ? $a[$this->xml.'lang'] : $this->getParentXLang(), + 'li_count' => 0, + ]; + /* ID */ + if (isset($a[$this->rdf.'ID'])) { + $s['type'] = 'uri'; + $s['value'] = $this->calcURI('#'.$a[$this->rdf.'ID'], $s['x_base']); + } + /* about */ + elseif (isset($a[$this->rdf.'about'])) { + $s['type'] = 'uri'; + $s['value'] = $this->calcURI($a[$this->rdf.'about'], $s['x_base']); + } + /* bnode */ + else { + $s['type'] = 'bnode'; + if (isset($a[$this->rdf.'nodeID'])) { + $s['value'] = '_:'.$a[$this->rdf.'nodeID']; + } else { + $s['value'] = $this->createBnodeID(); + } } + /* sub-node */ + if (4 === $this->state) { + $sup_s = $this->getParentS(); + /* new collection */ + if (isset($sup_s['o_is_coll']) && $sup_s['o_is_coll']) { + $coll = ['value' => $this->createBnodeID(), 'type' => 'bnode', 'is_coll' => true, 'x_base' => $s['x_base'], 'x_lang' => $s['x_lang']]; + $this->addT($sup_s['value'], $sup_s['p'], $coll['value'], $sup_s['type'], $coll['type']); + $this->addT($coll['value'], $this->rdf.'first', $s['value'], $coll['type'], $s['type']); + $this->pushS($coll); + } + /* new entry in existing coll */ + elseif (isset($sup_s['is_coll']) && $sup_s['is_coll']) { + $coll = ['value' => $this->createBnodeID(), 'type' => 'bnode', 'is_coll' => true, 'x_base' => $s['x_base'], 'x_lang' => $s['x_lang']]; + $this->addT($sup_s['value'], $this->rdf.'rest', $coll['value'], $sup_s['type'], $coll['type']); + $this->addT($coll['value'], $this->rdf.'first', $s['value'], $coll['type'], $s['type']); + $this->pushS($coll); + } + /* normal sub-node */ + elseif (isset($sup_s['p']) && $sup_s['p']) { + $this->addT($sup_s['value'], $sup_s['p'], $s['value'], $sup_s['type'], $s['type']); + } + } + /* typed node */ + if ($t !== $this->rdf.'Description') { + $this->addT($s['value'], $this->rdf.'type', $t, $s['type'], 'uri'); + } + /* (additional) typing attr */ + if (isset($a[$this->rdf.'type'])) { + $this->addT($s['value'], $this->rdf.'type', $a[$this->rdf.'type'], $s['type'], 'uri'); + } + /* Seq|Bag|Alt */ + if (in_array($t, [$this->rdf.'Seq', $this->rdf.'Bag', $this->rdf.'Alt'])) { + $s['is_con'] = true; + } + /* any other attrs (skip rdf and xml, except rdf:_, rdf:value, rdf:Seq) */ + foreach ($a as $k => $v) { + if (((false === strpos($k, $this->xml)) && (false === strpos($k, $this->rdf))) || preg_match('/(\_[0-9]+|value|Seq|Bag|Alt|Statement|Property|List)$/', $k)) { + if (strpos($k, ':')) { + $this->addT($s['value'], $k, $v, $s['type'], 'literal', '', $s['x_lang']); + } + } + } + $this->pushS($s); $this->state = 2; - } - elseif ($a[$this->rdf.'parseType'] === 'Collection') { - $s['o_is_coll'] = true; - $this->state = 4; - } - } - /* sub-node or literal */ - else { - $s['o_cdata'] = ''; - if (isset($a[$this->rdf.'datatype'])) { - $s['o_datatype'] = $a[$this->rdf.'datatype']; - } - $this->state = 4; - } - /* any other attrs (skip rdf and xml) */ - foreach($a as $k => $v) { - if (((strpos($k, $this->xml) === false) && (strpos($k, $this->rdf) === false)) || preg_match('/(\_[0-9]+|value)$/', $k)) { - if (strpos($k, ':')) { - if (!$o['value']) { - $o['value'] = $this->createBnodeID(); + } + + public function h2Open($t, $a) + { + $s = $this->getParentS(); + foreach (['p_x_base', 'p_x_lang', 'p_id', 'o_is_coll'] as $k) { + unset($s[$k]); + } + /* base */ + if (isset($a[$this->xml.'base'])) { + $s['p_x_base'] = $this->calcURI($a[$this->xml.'base'], $s['x_base']); + } + $b = isset($s['p_x_base']) && $s['p_x_base'] ? $s['p_x_base'] : $s['x_base']; + /* lang */ + if (isset($a[$this->xml.'lang'])) { + $s['p_x_lang'] = $a[$this->xml.'lang']; + } + $l = isset($s['p_x_lang']) && $s['p_x_lang'] ? $s['p_x_lang'] : $s['x_lang']; + /* adjust li */ + if ($t === $this->rdf.'li') { + ++$s['li_count']; + $t = $this->rdf.'_'.$s['li_count']; + } + /* set p */ + $s['p'] = $t; + /* reification */ + if (isset($a[$this->rdf.'ID'])) { + $s['p_id'] = $a[$this->rdf.'ID']; + } + $o = ['value' => '', 'type' => '', 'x_base' => $b, 'x_lang' => $l]; + /* resource/rdf:resource */ + if (isset($a['resource'])) { + $a[$this->rdf.'resource'] = $a['resource']; + unset($a['resource']); + } + if (isset($a[$this->rdf.'resource'])) { + $o['value'] = $this->calcURI($a[$this->rdf.'resource'], $b); + $o['type'] = 'uri'; + $this->addT($s['value'], $s['p'], $o['value'], $s['type'], $o['type']); + /* type */ + if (isset($a[$this->rdf.'type'])) { + $this->addT($o['value'], $this->rdf.'type', $a[$this->rdf.'type'], 'uri', 'uri'); + } + /* reification */ + if (isset($s['p_id'])) { + $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type']); + unset($s['p_id']); + } + $this->state = 3; + } + /* named bnode */ + elseif (isset($a[$this->rdf.'nodeID'])) { + $o['value'] = '_:'.$a[$this->rdf.'nodeID']; $o['type'] = 'bnode'; $this->addT($s['value'], $s['p'], $o['value'], $s['type'], $o['type']); - } - /* reification */ - if (isset($s['p_id'])) { - $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type']); - unset($s['p_id']); - } - $this->addT($o['value'], $k, $v, $o['type'], 'literal'); - $this->state = 3; - } - } - } - $this->updateS($s); - } - - /* */ - - function h4Open($t, $a) { - return $this->h1Open($t, $a); - } - - /* */ - - function h5Open($t, $a) { - $this->state = 4; - return $this->h4Open($t, $a); - } - - /* */ - - function h6Open($t, $a) { - $s = $this->getParentS(); - $data = isset($s['o_xml_data']) ? $s['o_xml_data'] : ''; - $ns = isset($s['ns']) ? $s['ns'] : array(); - $parts = $this->splitURI($t); - if ((count($parts) === 1) || empty($parts[1])) { - $data .= '<'.$t; - } - else { - $ns_uri = $parts[0]; - $name = $parts[1]; - if (!isset($this->nsp[$ns_uri])) { - foreach ($this->nsp as $tmp1 => $tmp2) { - if (strpos($t, $tmp1) === 0) { - $ns_uri = $tmp1; - $name = substr($t, strlen($tmp1)); - break; - } - } - } - $nsp = $this->nsp[$ns_uri]; - $data .= $nsp ? '<' . $nsp . ':' . $name : '<' . $name; - /* ns */ - if (!isset($ns[$nsp.'='.$ns_uri]) || !$ns[$nsp.'='.$ns_uri]) { - $data .= $nsp ? ' xmlns:'.$nsp.'="'.$ns_uri.'"' : ' xmlns="'.$ns_uri.'"'; - $ns[$nsp.'='.$ns_uri] = true; - $s['ns'] = $ns; - } - } - foreach ($a as $k => $v) { - $parts = $this->splitURI($k); - if (count($parts) === 1) { - $data .= ' '.$k.'="'.$v.'"'; - } - else { - $ns_uri = $parts[0]; - $name = $parts[1]; - $nsp = $this->v($ns_uri, '', $this->nsp); - $data .= $nsp ? ' '.$nsp.':'.$name.'="'.$v.'"' : ' '.$name.'="'.$v.'"' ; - } - } - $data .= '>'; - $s['o_xml_data'] = $data; - $s['o_xml_level'] = isset($s['o_xml_level']) ? $s['o_xml_level'] + 1 : 1; - if ($t == $s['p']) {/* xml container prop */ - $s['p_xml_literal_level'] = isset($s['p_xml_literal_level']) ? $s['p_xml_literal_level'] + 1 : 1; - } - $this->updateS($s); - } - - /* */ - - function h1Close($t) {/* end of doc */ - $this->state = 0; - } - - /* */ - - function h2Close($t) {/* expecting a prop, getting a close */ - if ($s = $this->getParentS()) { - $has_closing_tag = (isset($s['has_closing_tag']) && !$s['has_closing_tag']) ? 0 : 1; - $this->popS(); - $this->state = 5; - if ($s = $this->getParentS()) {/* new s */ - if (!isset($s['p']) || !$s['p']) {/* p close after collection|parseType=Resource|node close after p close */ - $this->state = $this->s_count ? 4 : 1; - if (!$has_closing_tag) { - $this->state = 2; - } - } - elseif (!$has_closing_tag) { - $this->state = 2; - } - } - } - } - - /* */ - - function h3Close($t) {/* p close */ - $this->state = 2; - } - - /* */ - - function h4Close($t) {/* empty p | pClose after cdata | pClose after collection */ - if ($s = $this->getParentS()) { - $b = isset($s['p_x_base']) && $s['p_x_base'] ? $s['p_x_base'] : (isset($s['x_base']) ? $s['x_base'] : ''); - if (isset($s['is_coll']) && $s['is_coll']) { - $this->addT($s['value'], $this->rdf . 'rest', $this->rdf . 'nil', $s['type'], 'uri'); - /* back to collection start */ - while ((!isset($s['p']) || ($s['p'] != $t))) { - $sub_s = $s; - $this->popS(); - $s = $this->getParentS(); + $this->state = 3; + /* reification */ + if (isset($s['p_id'])) { + $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type']); + } } - /* reification */ - if (isset($s['p_id']) && $s['p_id']) { - $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $sub_s['value'], $s['type'], $sub_s['type']); + /* parseType */ + elseif (isset($a[$this->rdf.'parseType'])) { + if ('Literal' === $a[$this->rdf.'parseType']) { + $s['o_xml_level'] = 0; + $s['o_xml_data'] = ''; + $s['p_xml_literal_level'] = 0; + $s['ns'] = []; + $this->state = 6; + } elseif ('Resource' === $a[$this->rdf.'parseType']) { + $o['value'] = $this->createBnodeID(); + $o['type'] = 'bnode'; + $o['has_closing_tag'] = 0; + $this->addT($s['value'], $s['p'], $o['value'], $s['type'], $o['type']); + $this->pushS($o); + /* reification */ + if (isset($s['p_id'])) { + $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type']); + unset($s['p_id']); + } + $this->state = 2; + } elseif ('Collection' === $a[$this->rdf.'parseType']) { + $s['o_is_coll'] = true; + $this->state = 4; + } } - unset($s['p']); - $this->updateS($s); - } - else { - $dt = isset($s['o_datatype']) ? $s['o_datatype'] : ''; - $l = isset($s['p_x_lang']) && $s['p_x_lang'] ? $s['p_x_lang'] : (isset($s['x_lang']) ? $s['x_lang'] : ''); - $o = array('type' => 'literal', 'value' => $s['o_cdata']); - $this->addT($s['value'], $s['p'], $o['value'], $s['type'], $o['type'], $dt, $l); - /* reification */ - if (isset($s['p_id']) && $s['p_id']) { - $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type'], $dt, $l); + /* sub-node or literal */ + else { + $s['o_cdata'] = ''; + if (isset($a[$this->rdf.'datatype'])) { + $s['o_datatype'] = $a[$this->rdf.'datatype']; + } + $this->state = 4; + } + /* any other attrs (skip rdf and xml) */ + foreach ($a as $k => $v) { + if (((false === strpos($k, $this->xml)) && (false === strpos($k, $this->rdf))) || preg_match('/(\_[0-9]+|value)$/', $k)) { + if (strpos($k, ':')) { + if (!$o['value']) { + $o['value'] = $this->createBnodeID(); + $o['type'] = 'bnode'; + $this->addT($s['value'], $s['p'], $o['value'], $s['type'], $o['type']); + } + /* reification */ + if (isset($s['p_id'])) { + $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type']); + unset($s['p_id']); + } + $this->addT($o['value'], $k, $v, $o['type'], 'literal'); + $this->state = 3; + } + } } - unset($s['o_cdata']); - unset($s['o_datatype']); - unset($s['p']); $this->updateS($s); - } - $this->state = 2; - } - } - - /* */ - - function h5Close($t) {/* p close */ - if ($s = $this->getParentS()) { - unset($s['p']); - $this->updateS($s); - $this->state = 2; - } - } - - /* */ - - function h6Close($t) { - if ($s = $this->getParentS()) { - $l = isset($s['p_x_lang']) && $s['p_x_lang'] ? $s['p_x_lang'] : (isset($s['x_lang']) ? $s['x_lang'] : ''); - $data = $s['o_xml_data']; - $level = $s['o_xml_level']; - if ($level === 0) {/* pClose */ - $this->addT($s['value'], $s['p'], trim($data, ' '), $s['type'], 'literal', $this->rdf.'XMLLiteral', $l); - unset($s['o_xml_data']); - $this->state = 2; - } - else { + } + + public function h4Open($t, $a) + { + return $this->h1Open($t, $a); + } + + public function h5Open($t, $a) + { + $this->state = 4; + + return $this->h4Open($t, $a); + } + + public function h6Open($t, $a) + { + $s = $this->getParentS(); + $data = isset($s['o_xml_data']) ? $s['o_xml_data'] : ''; + $ns = isset($s['ns']) ? $s['ns'] : []; $parts = $this->splitURI($t); - if ((count($parts) === 1) || empty($parts[1])) { - $data .= ''; + if ((1 === count($parts)) || empty($parts[1])) { + $data .= '<'.$t; + } else { + $ns_uri = $parts[0]; + $name = $parts[1]; + if (!isset($this->nsp[$ns_uri])) { + foreach ($this->nsp as $tmp1 => $tmp2) { + if (0 === strpos($t, $tmp1)) { + $ns_uri = $tmp1; + $name = substr($t, strlen($tmp1)); + break; + } + } + } + $nsp = $this->nsp[$ns_uri]; + $data .= $nsp ? '<'.$nsp.':'.$name : '<'.$name; + /* ns */ + if (!isset($ns[$nsp.'='.$ns_uri]) || !$ns[$nsp.'='.$ns_uri]) { + $data .= $nsp ? ' xmlns:'.$nsp.'="'.$ns_uri.'"' : ' xmlns="'.$ns_uri.'"'; + $ns[$nsp.'='.$ns_uri] = true; + $s['ns'] = $ns; + } } - else { - $ns_uri = $parts[0]; - $name = $parts[1]; - if (!isset($this->nsp[$ns_uri])) { - foreach ($this->nsp as $tmp1 => $tmp2) { - if (strpos($t, $tmp1) === 0) { - $ns_uri = $tmp1; - $name = substr($t, strlen($tmp1)); - break; - } + foreach ($a as $k => $v) { + $parts = $this->splitURI($k); + if (1 === count($parts)) { + $data .= ' '.$k.'="'.$v.'"'; + } else { + $ns_uri = $parts[0]; + $name = $parts[1]; + $nsp = $this->v($ns_uri, '', $this->nsp); + $data .= $nsp ? ' '.$nsp.':'.$name.'="'.$v.'"' : ' '.$name.'="'.$v.'"'; } - } - $nsp = $this->nsp[$ns_uri]; - $data .= $nsp ? '' : ''; } + $data .= '>'; $s['o_xml_data'] = $data; - $s['o_xml_level'] = $level - 1; + $s['o_xml_level'] = isset($s['o_xml_level']) ? $s['o_xml_level'] + 1 : 1; if ($t == $s['p']) {/* xml container prop */ - $s['p_xml_literal_level']--; - } - } - $this->updateS($s); - } - } - - /* */ - - function h4Cdata($d) { - if ($s = $this->getParentS()) { - $s['o_cdata'] = isset($s['o_cdata']) ? $s['o_cdata'] . $d : $d; - $this->updateS($s); - } - } - - /* */ - - function h6Cdata($d) { - if ($s = $this->getParentS()) { - if (isset($s['o_xml_data']) || preg_match("/[\n\r]/", $d) || trim($d)) { - $d = htmlspecialchars($d, ENT_NOQUOTES); - $s['o_xml_data'] = isset($s['o_xml_data']) ? $s['o_xml_data'] . $d : $d; - } - $this->updateS($s); - } - } - - /* */ - + $s['p_xml_literal_level'] = isset($s['p_xml_literal_level']) ? $s['p_xml_literal_level'] + 1 : 1; + } + $this->updateS($s); + } + + public function h1Close($t) + {/* end of doc */ + $this->state = 0; + } + + public function h2Close($t) + {/* expecting a prop, getting a close */ + if ($s = $this->getParentS()) { + $has_closing_tag = (isset($s['has_closing_tag']) && !$s['has_closing_tag']) ? 0 : 1; + $this->popS(); + $this->state = 5; + if ($s = $this->getParentS()) {/* new s */ + if (!isset($s['p']) || !$s['p']) {/* p close after collection|parseType=Resource|node close after p close */ + $this->state = $this->s_count ? 4 : 1; + if (!$has_closing_tag) { + $this->state = 2; + } + } elseif (!$has_closing_tag) { + $this->state = 2; + } + } + } + } + + public function h3Close($t) + {/* p close */ + $this->state = 2; + } + + public function h4Close($t) + {/* empty p | pClose after cdata | pClose after collection */ + if ($s = $this->getParentS()) { + $b = isset($s['p_x_base']) && $s['p_x_base'] ? $s['p_x_base'] : (isset($s['x_base']) ? $s['x_base'] : ''); + if (isset($s['is_coll']) && $s['is_coll']) { + $this->addT($s['value'], $this->rdf.'rest', $this->rdf.'nil', $s['type'], 'uri'); + /* back to collection start */ + while ((!isset($s['p']) || ($s['p'] != $t))) { + $sub_s = $s; + $this->popS(); + $s = $this->getParentS(); + } + /* reification */ + if (isset($s['p_id']) && $s['p_id']) { + $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $sub_s['value'], $s['type'], $sub_s['type']); + } + unset($s['p']); + $this->updateS($s); + } else { + $dt = isset($s['o_datatype']) ? $s['o_datatype'] : ''; + $l = isset($s['p_x_lang']) && $s['p_x_lang'] ? $s['p_x_lang'] : (isset($s['x_lang']) ? $s['x_lang'] : ''); + $o = ['type' => 'literal', 'value' => $s['o_cdata']]; + $this->addT($s['value'], $s['p'], $o['value'], $s['type'], $o['type'], $dt, $l); + /* reification */ + if (isset($s['p_id']) && $s['p_id']) { + $this->reify($this->calcURI('#'.$s['p_id'], $b), $s['value'], $s['p'], $o['value'], $s['type'], $o['type'], $dt, $l); + } + unset($s['o_cdata']); + unset($s['o_datatype']); + unset($s['p']); + $this->updateS($s); + } + $this->state = 2; + } + } + + public function h5Close($t) + {/* p close */ + if ($s = $this->getParentS()) { + unset($s['p']); + $this->updateS($s); + $this->state = 2; + } + } + + public function h6Close($t) + { + if ($s = $this->getParentS()) { + $l = isset($s['p_x_lang']) && $s['p_x_lang'] ? $s['p_x_lang'] : (isset($s['x_lang']) ? $s['x_lang'] : ''); + $data = $s['o_xml_data']; + $level = $s['o_xml_level']; + if (0 === $level) {/* pClose */ + $this->addT($s['value'], $s['p'], trim($data, ' '), $s['type'], 'literal', $this->rdf.'XMLLiteral', $l); + unset($s['o_xml_data']); + $this->state = 2; + } else { + $parts = $this->splitURI($t); + if ((1 === count($parts)) || empty($parts[1])) { + $data .= ''; + } else { + $ns_uri = $parts[0]; + $name = $parts[1]; + if (!isset($this->nsp[$ns_uri])) { + foreach ($this->nsp as $tmp1 => $tmp2) { + if (0 === strpos($t, $tmp1)) { + $ns_uri = $tmp1; + $name = substr($t, strlen($tmp1)); + break; + } + } + } + $nsp = $this->nsp[$ns_uri]; + $data .= $nsp ? '' : ''; + } + $s['o_xml_data'] = $data; + $s['o_xml_level'] = $level - 1; + if ($t == $s['p']) {/* xml container prop */ + --$s['p_xml_literal_level']; + } + } + $this->updateS($s); + } + } + + public function h4Cdata($d) + { + if ($s = $this->getParentS()) { + $s['o_cdata'] = isset($s['o_cdata']) ? $s['o_cdata'].$d : $d; + $this->updateS($s); + } + } + + public function h6Cdata($d) + { + if ($s = $this->getParentS()) { + if (isset($s['o_xml_data']) || preg_match("/[\n\r]/", $d) || trim($d)) { + $d = htmlspecialchars($d, ENT_NOQUOTES); + $s['o_xml_data'] = isset($s['o_xml_data']) ? $s['o_xml_data'].$d : $d; + } + $this->updateS($s); + } + } } diff --git a/parsers/ARC2_RSSParser.php b/parsers/ARC2_RSSParser.php index 47ddfc73..3af286d4 100644 --- a/parsers/ARC2_RSSParser.php +++ b/parsers/ARC2_RSSParser.php @@ -10,176 +10,191 @@ class: ARC2 RSS Parser ARC2::inc('LegacyXMLParser'); -class ARC2_RSSParser extends ARC2_LegacyXMLParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->triples = array(); - $this->target_encoding = ''; - $this->t_count = 0; - $this->added_triples = array(); - $this->skip_dupes = false; - $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); - $this->bnode_id = 0; - $this->cache = array(); - $this->allowCDataNodes = 0; - } - - /* */ - - function done() { - $this->extractRDF(); - } - - /* */ - - function setReader(&$reader) { - $this->reader = $reader; - } - - function createBnodeID(){ - $this->bnode_id++; - return '_:' . $this->bnode_prefix . $this->bnode_id; - } - - function addT($t) { - //if (!isset($t['o_datatype'])) - if ($this->skip_dupes) { - $h = md5(serialize($t)); - if (!isset($this->added_triples[$h])) { - $this->triples[$this->t_count] = $t; - $this->t_count++; - $this->added_triples[$h] = true; - } +class ARC2_RSSParser extends ARC2_LegacyXMLParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - else { - $this->triples[$this->t_count] = $t; - $this->t_count++; + + public function __init() + {/* reader */ + parent::__init(); + $this->triples = []; + $this->target_encoding = ''; + $this->t_count = 0; + $this->added_triples = []; + $this->skip_dupes = false; + $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); + $this->bnode_id = 0; + $this->cache = []; + $this->allowCDataNodes = 0; } - } - - function getTriples() { - return $this->v('triples', array()); - } - - function countTriples() { - return $this->t_count; - } - - function getSimpleIndex($flatten_objects = 1, $vals = '') { - return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals); - } - - /* */ - - function extractRDF() { - $index = $this->getNodeIndex(); - $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $this->rss = 'http://purl.org/rss/1.0/'; - $this->dc = 'http://purl.org/dc/elements/1.1/'; - $this->dct = 'http://purl.org/dc/terms/'; - $this->content = 'http://purl.org/rss/1.0/modules/content/'; - $this->enc = 'http://purl.oclc.org/net/rss_2.0/enc#'; - $this->mappings = array( - 'channel' => $this->rss . 'channel', - 'item' => $this->rss . 'item', - 'title' => $this->rss . 'title', - 'link' => $this->rss . 'link', - 'description' => $this->rss . 'description', - 'guid' => $this->dc . 'identifier', - 'author' => $this->dc . 'creator', - 'category' => $this->dc . 'subject', - 'pubDate' => $this->dc . 'date', - 'pubdate' => $this->dc . 'date', - 'source' => $this->dc . 'source', - 'enclosure' => $this->enc . 'enclosure', - ); - $this->dt_props = array( - $this->dc . 'identifier', - $this->rss . 'link' - ); - foreach ($index as $p_id => $nodes) { - foreach ($nodes as $pos => $node) { - $tag = $this->v('tag', '', $node); - if ($tag == 'channel') { - $struct = $this->extractChannel($index[$node['id']]); - $triples = ARC2::getTriplesFromIndex($struct); - foreach ($triples as $t) { - $this->addT($t); - } - } - elseif ($tag == 'item') { - $struct = $this->extractItem($index[$node['id']]); - $triples = ARC2::getTriplesFromIndex($struct); - foreach ($triples as $t) { - $this->addT($t); - } + + public function done() + { + $this->extractRDF(); + } + + public function setReader(&$reader) + { + $this->reader = $reader; + } + + public function createBnodeID() + { + ++$this->bnode_id; + + return '_:'.$this->bnode_prefix.$this->bnode_id; + } + + public function addT($t) + { + //if (!isset($t['o_datatype'])) + if ($this->skip_dupes) { + $h = md5(serialize($t)); + if (!isset($this->added_triples[$h])) { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + $this->added_triples[$h] = true; + } + } else { + $this->triples[$this->t_count] = $t; + ++$this->t_count; } - } } - } - - function extractChannel($els) { - $res = array($this->rdf . 'type' => array(array('value' => $this->rss . 'channel', 'type' => 'uri'))); - $res = array_merge($res, $this->extractProps($els, 'channel')); - return array($res[$this->rss . 'link'][0]['value'] => $res); - } - - function extractItem($els) { - $res = array($this->rdf . 'type' => array(array('value' => $this->rss . 'item', 'type' => 'uri'))); - $res = array_merge($res, $this->extractProps($els, 'item')); - if (isset($res[$this->rss . 'link'])) return array($res[$this->rss . 'link'][0]['value'] => $res); - if (isset($res[$this->dc . 'identifier'])) return array($res[$this->dc . 'identifier'][0]['value'] => $res); - } - - function extractProps($els, $container) { - $res = array(); - foreach ($els as $info) { - /* key */ - $tag = $info['tag']; - if (!preg_match('/^[a-z0-9]+\:/i', $tag)) { - $k = isset($this->mappings[$tag]) ? $this->mappings[$tag] : ''; - } - else { - $k = $tag; - } - if (($container == 'channel') && ($k == $this->rss . 'item')) continue; - /* val */ - $v = $info['cdata']; - if (!$v) $v = $this->v('url', '', $info['a']); - if (!$v) $v = $this->v('href', '', $info['a']); - /* prop */ - if ($k) { - /* enclosure handling */ - if ($k == $this->enc . 'enclosure') { - $sub_res = array(); - foreach (array('length', 'type') as $attr) { - if ($attr_v = $this->v($attr, 0, $info['a'])) { - $sub_res[$this->enc . $attr] = array(array('value' => $attr_v, 'type' => 'literal')); + + public function getTriples() + { + return $this->v('triples', []); + } + + public function countTriples() + { + return $this->t_count; + } + + public function getSimpleIndex($flatten_objects = 1, $vals = '') + { + return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals); + } + + public function extractRDF() + { + $index = $this->getNodeIndex(); + $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $this->rss = 'http://purl.org/rss/1.0/'; + $this->dc = 'http://purl.org/dc/elements/1.1/'; + $this->dct = 'http://purl.org/dc/terms/'; + $this->content = 'http://purl.org/rss/1.0/modules/content/'; + $this->enc = 'http://purl.oclc.org/net/rss_2.0/enc#'; + $this->mappings = [ + 'channel' => $this->rss.'channel', + 'item' => $this->rss.'item', + 'title' => $this->rss.'title', + 'link' => $this->rss.'link', + 'description' => $this->rss.'description', + 'guid' => $this->dc.'identifier', + 'author' => $this->dc.'creator', + 'category' => $this->dc.'subject', + 'pubDate' => $this->dc.'date', + 'pubdate' => $this->dc.'date', + 'source' => $this->dc.'source', + 'enclosure' => $this->enc.'enclosure', + ]; + $this->dt_props = [ + $this->dc.'identifier', + $this->rss.'link', + ]; + foreach ($index as $p_id => $nodes) { + foreach ($nodes as $pos => $node) { + $tag = $this->v('tag', '', $node); + if ('channel' == $tag) { + $struct = $this->extractChannel($index[$node['id']]); + $triples = ARC2::getTriplesFromIndex($struct); + foreach ($triples as $t) { + $this->addT($t); + } + } elseif ('item' == $tag) { + $struct = $this->extractItem($index[$node['id']]); + $triples = ARC2::getTriplesFromIndex($struct); + foreach ($triples as $t) { + $this->addT($t); + } + } } - } - $struct[$v] = $sub_res; } - /* date handling */ - if (in_array($k, array($this->dc . 'date', $this->dct . 'modified'))) { - if (!preg_match('/^[0-9]{4}/', $v) && ($sub_v = strtotime($v)) && ($sub_v != -1)) { - $tz = date('Z', $sub_v); /* timezone offset */ - $sub_v -= $tz; /* utc */ - $v = date('Y-m-d\TH:i:s\Z', $sub_v); - } + } + + public function extractChannel($els) + { + $res = [$this->rdf.'type' => [['value' => $this->rss.'channel', 'type' => 'uri']]]; + $res = array_merge($res, $this->extractProps($els, 'channel')); + + return [$res[$this->rss.'link'][0]['value'] => $res]; + } + + public function extractItem($els) + { + $res = [$this->rdf.'type' => [['value' => $this->rss.'item', 'type' => 'uri']]]; + $res = array_merge($res, $this->extractProps($els, 'item')); + if (isset($res[$this->rss.'link'])) { + return [$res[$this->rss.'link'][0]['value'] => $res]; + } + if (isset($res[$this->dc.'identifier'])) { + return [$res[$this->dc.'identifier'][0]['value'] => $res]; } - if (!isset($res[$k])) $res[$k] = array(); - $res[$k][] = array('value' => $v, 'type' => in_array($k, $this->dt_props) || !preg_match('/^[a-z0-9]+\:[^\s]+$/is', $v) ? 'literal' : 'uri'); - } } - return $res; - } - - /* */ - + public function extractProps($els, $container) + { + $res = []; + foreach ($els as $info) { + /* key */ + $tag = $info['tag']; + if (!preg_match('/^[a-z0-9]+\:/i', $tag)) { + $k = isset($this->mappings[$tag]) ? $this->mappings[$tag] : ''; + } else { + $k = $tag; + } + if (('channel' == $container) && ($k == $this->rss.'item')) { + continue; + } + /* val */ + $v = $info['cdata']; + if (!$v) { + $v = $this->v('url', '', $info['a']); + } + if (!$v) { + $v = $this->v('href', '', $info['a']); + } + /* prop */ + if ($k) { + /* enclosure handling */ + if ($k == $this->enc.'enclosure') { + $sub_res = []; + foreach (['length', 'type'] as $attr) { + if ($attr_v = $this->v($attr, 0, $info['a'])) { + $sub_res[$this->enc.$attr] = [['value' => $attr_v, 'type' => 'literal']]; + } + } + $struct[$v] = $sub_res; + } + /* date handling */ + if (in_array($k, [$this->dc.'date', $this->dct.'modified'])) { + if (!preg_match('/^[0-9]{4}/', $v) && ($sub_v = strtotime($v)) && ($sub_v != -1)) { + $tz = date('Z', $sub_v); /* timezone offset */ + $sub_v -= $tz; /* utc */ + $v = date('Y-m-d\TH:i:s\Z', $sub_v); + } + } + if (!isset($res[$k])) { + $res[$k] = []; + } + $res[$k][] = ['value' => $v, 'type' => in_array($k, $this->dt_props) || !preg_match('/^[a-z0-9]+\:[^\s]+$/is', $v) ? 'literal' : 'uri']; + } + } + + return $res; + } } diff --git a/parsers/ARC2_SGAJSONParser.php b/parsers/ARC2_SGAJSONParser.php index db9c2240..71357e08 100755 --- a/parsers/ARC2_SGAJSONParser.php +++ b/parsers/ARC2_SGAJSONParser.php @@ -10,54 +10,65 @@ class: ARC2 SG API JSON Parser ARC2::inc('JSONParser'); -class ARC2_SGAJSONParser extends ARC2_JSONParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $this->nsp = array($this->rdf => 'rdf'); - } - - /* */ - - function done() { - $this->extractRDF(); - } - - function extractRDF($formats = '') { - $s = $this->getContext(); - $os = $this->getURLs($this->struct); - foreach ($os as $o) { - if ($o != $s) $this->addT($s, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $o, 'uri', 'uri'); +class ARC2_SGAJSONParser extends ARC2_JSONParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - } - - function getContext() { - if (!isset($this->struct['canonical_mapping'])) return ''; - foreach ($this->struct['canonical_mapping'] as $k => $v) return $v; - } - - function getURLs($struct) { - $r =array(); - if (is_array($struct)) { - foreach ($struct as $k => $v) { - if (preg_match('/^http:\/\//', $k) && !in_array($k, $r)) $r[] = $k; - $sub_r = $this->getURLs($v); - foreach ($sub_r as $sub_v) { - if (!in_array($sub_v, $r)) $r[] = $sub_v; + + public function __init() + {/* reader */ + parent::__init(); + $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $this->nsp = [$this->rdf => 'rdf']; + } + + public function done() + { + $this->extractRDF(); + } + + public function extractRDF($formats = '') + { + $s = $this->getContext(); + $os = $this->getURLs($this->struct); + foreach ($os as $o) { + if ($o != $s) { + $this->addT($s, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $o, 'uri', 'uri'); + } } - } } - elseif (preg_match('/^http:\/\//', $struct) && !in_array($struct, $r)) { - $r[] = $struct; + + public function getContext() + { + if (!isset($this->struct['canonical_mapping'])) { + return ''; + } + foreach ($this->struct['canonical_mapping'] as $k => $v) { + return $v; + } } - return $r; - } - - /* */ + public function getURLs($struct) + { + $r = []; + if (is_array($struct)) { + foreach ($struct as $k => $v) { + if (preg_match('/^http:\/\//', $k) && !in_array($k, $r)) { + $r[] = $k; + } + $sub_r = $this->getURLs($v); + foreach ($sub_r as $sub_v) { + if (!in_array($sub_v, $r)) { + $r[] = $sub_v; + } + } + } + } elseif (preg_match('/^http:\/\//', $struct) && !in_array($struct, $r)) { + $r[] = $struct; + } + + return $r; + } } diff --git a/parsers/ARC2_SPARQLParser.php b/parsers/ARC2_SPARQLParser.php index 997986b4..f34a537a 100644 --- a/parsers/ARC2_SPARQLParser.php +++ b/parsers/ARC2_SPARQLParser.php @@ -1,777 +1,841 @@ - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('TurtleParser'); -class ARC2_SPARQLParser extends ARC2_TurtleParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); - $this->bnode_id = 0; - $this->bnode_pattern_index = array('patterns' => array(), 'bnodes' => array()); - } - - /* */ - - function parse($q, $src = '', $iso_fallback = 'ignore') { - $this->setDefaultPrefixes(); - $this->base = $src ? $this->calcBase($src) : ARC2::getRequestURI(); - $this->r = array( - 'base' => '', - 'vars' => array(), - 'prefixes' => array() - ); - $this->unparsed_code = $q; - list($r, $v) = $this->xQuery($q); - if ($r) { - $this->r['query'] = $r; - $this->unparsed_code = trim($v); - } - elseif (!$this->getErrors() && !$this->unparsed_code) { - $this->addError('Query not properly closed'); - } - $this->r['prefixes'] = $this->prefixes; - $this->r['base'] = $this->base; - /* remove trailing comments */ - while (preg_match('/^\s*(\#[^\xd\xa]*)(.*)$/si', $this->unparsed_code, $m)) $this->unparsed_code = $m[2]; - if ($this->unparsed_code && !$this->getErrors()) { - $rest = preg_replace('/[\x0a|\x0d]/i', ' ', substr($this->unparsed_code, 0, 30)); - $msg = trim($rest) ? 'Could not properly handle "' . $rest . '"' : 'Syntax error, probably an incomplete pattern'; - $this->addError($msg); - } - } - - function getQueryInfos() { - return $this->v('r', array()); - } - - /* 1 */ - - function xQuery($v) { - list($r, $v) = $this->xPrologue($v); - foreach (array('Select', 'Construct', 'Describe', 'Ask') as $type) { - $m = 'x' . $type . 'Query'; - if ((list($r, $v) = $this->$m($v)) && $r) { - return array($r, $v); - } - } - return array(0, $v); - } - - /* 2 */ - - function xPrologue($v) { - $r = 0; - if ((list($sub_r, $v) = $this->xBaseDecl($v)) && $sub_r) { - $this->base = $sub_r; - $r = 1; - } - while ((list($sub_r, $v) = $this->xPrefixDecl($v)) && $sub_r) { - $this->prefixes[$sub_r['prefix']] = $sub_r['uri']; - $r = 1; - } - return array($r, $v); - } - - /* 5.. */ - - function xSelectQuery($v) { - if ($sub_r = $this->x('SELECT\s+', $v)) { - $r = array( - 'type' => 'select', - 'result_vars' => array(), - 'dataset' => array(), - ); - $all_vars = 0; - $sub_v = $sub_r[1]; - /* distinct, reduced */ - if ($sub_r = $this->x('(DISTINCT|REDUCED)\s+', $sub_v)) { - $r[strtolower($sub_r[1])] = 1; - $sub_v = $sub_r[2]; - } - /* result vars */ - if ($sub_r = $this->x('\*\s+', $sub_v)) { - $all_vars = 1; - $sub_v = $sub_r[1]; - } - else { - while ((list($sub_r, $sub_v) = $this->xResultVar($sub_v)) && $sub_r) { - $r['result_vars'][] = $sub_r; - } - } - if (!$all_vars && !count($r['result_vars'])) { - $this->addError('No result bindings specified.'); - } - /* dataset */ - while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { - $r['dataset'][] = $sub_r; - } - /* where */ - if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { - $r['pattern'] = $sub_r; - } - else { - return array(0, $v); - } - /* solution modifier */ - if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { - $r = array_merge($r, $sub_r); - } - /* all vars */ - if ($all_vars) { - foreach ($this->r['vars'] as $var) { - $r['result_vars'][] = array('var' => $var, 'aggregate' => 0, 'alias' => ''); - } - if (!$r['result_vars']) { - $r['result_vars'][] = '*'; - } - } - return array($r, $sub_v); - } - return array(0, $v); - } - - function xResultVar($v) { - return $this->xVar($v); - } - - /* 6.. */ - - function xConstructQuery($v) { - if ($sub_r = $this->x('CONSTRUCT\s*', $v)) { - $r = array( - 'type' => 'construct', - 'dataset' => array(), - ); - $sub_v = $sub_r[1]; - /* construct template */ - if ((list($sub_r, $sub_v) = $this->xConstructTemplate($sub_v)) && is_array($sub_r)) { - $r['construct_triples'] = $sub_r; - } - else { - $this->addError('Construct Template not found'); - return array(0, $v); - } - /* dataset */ - while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { - $r['dataset'][] = $sub_r; - } - /* where */ - if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { - $r['pattern'] = $sub_r; - } - else { - return array(0, $v); - } - /* solution modifier */ - if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { - $r = array_merge($r, $sub_r); - } - return array($r, $sub_v); - } - return array(0, $v); - } - - /* 7.. */ - - function xDescribeQuery($v) { - if ($sub_r = $this->x('DESCRIBE\s+', $v)) { - $r = array( - 'type' => 'describe', - 'result_vars' => array(), - 'result_uris' => array(), - 'dataset' => array(), - ); - $sub_v = $sub_r[1]; - $all_vars = 0; - /* result vars/uris */ - if ($sub_r = $this->x('\*\s+', $sub_v)) { - $all_vars = 1; - $sub_v = $sub_r[1]; - } - else { +class ARC2_SPARQLParser extends ARC2_TurtleParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } + + public function __init() + { + parent::__init(); + $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); + $this->bnode_id = 0; + $this->bnode_pattern_index = ['patterns' => [], 'bnodes' => []]; + } + + public function parse($q, $src = '', $iso_fallback = 'ignore') + { + $this->setDefaultPrefixes(); + $this->base = $src ? $this->calcBase($src) : ARC2::getRequestURI(); + $this->r = [ + 'base' => '', + 'vars' => [], + 'prefixes' => [], + ]; + $this->unparsed_code = $q; + list($r, $v) = $this->xQuery($q); + if ($r) { + $this->r['query'] = $r; + $this->unparsed_code = trim($v); + } elseif (!$this->getErrors() && !$this->unparsed_code) { + $this->addError('Query not properly closed'); + } + $this->r['prefixes'] = $this->prefixes; + $this->r['base'] = $this->base; + /* remove trailing comments */ + while (preg_match('/^\s*(\#[^\xd\xa]*)(.*)$/si', $this->unparsed_code, $m)) { + $this->unparsed_code = $m[2]; + } + if ($this->unparsed_code && !$this->getErrors()) { + $rest = preg_replace('/[\x0a|\x0d]/i', ' ', substr($this->unparsed_code, 0, 30)); + $msg = trim($rest) ? 'Could not properly handle "'.$rest.'"' : 'Syntax error, probably an incomplete pattern'; + $this->addError($msg); + } + } + + public function getQueryInfos() + { + return $this->v('r', []); + } + + /* 1 */ + + public function xQuery($v) + { + list($r, $v) = $this->xPrologue($v); + foreach (['Select', 'Construct', 'Describe', 'Ask'] as $type) { + $m = 'x'.$type.'Query'; + if ((list($r, $v) = $this->$m($v)) && $r) { + return [$r, $v]; + } + } + + return [0, $v]; + } + + /* 2 */ + + public function xPrologue($v) + { + $r = 0; + if ((list($sub_r, $v) = $this->xBaseDecl($v)) && $sub_r) { + $this->base = $sub_r; + $r = 1; + } + while ((list($sub_r, $v) = $this->xPrefixDecl($v)) && $sub_r) { + $this->prefixes[$sub_r['prefix']] = $sub_r['uri']; + $r = 1; + } + + return [$r, $v]; + } + + /* 5.. */ + + public function xSelectQuery($v) + { + if ($sub_r = $this->x('SELECT\s+', $v)) { + $r = [ + 'type' => 'select', + 'result_vars' => [], + 'dataset' => [], + ]; + $all_vars = 0; + $sub_v = $sub_r[1]; + /* distinct, reduced */ + if ($sub_r = $this->x('(DISTINCT|REDUCED)\s+', $sub_v)) { + $r[strtolower($sub_r[1])] = 1; + $sub_v = $sub_r[2]; + } + /* result vars */ + if ($sub_r = $this->x('\*\s+', $sub_v)) { + $all_vars = 1; + $sub_v = $sub_r[1]; + } else { + while ((list($sub_r, $sub_v) = $this->xResultVar($sub_v)) && $sub_r) { + $r['result_vars'][] = $sub_r; + } + } + if (!$all_vars && !count($r['result_vars'])) { + $this->addError('No result bindings specified.'); + } + /* dataset */ + while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { + $r['dataset'][] = $sub_r; + } + /* where */ + if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { + $r['pattern'] = $sub_r; + } else { + return [0, $v]; + } + /* solution modifier */ + if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { + $r = array_merge($r, $sub_r); + } + /* all vars */ + if ($all_vars) { + foreach ($this->r['vars'] as $var) { + $r['result_vars'][] = ['var' => $var, 'aggregate' => 0, 'alias' => '']; + } + if (!$r['result_vars']) { + $r['result_vars'][] = '*'; + } + } + + return [$r, $sub_v]; + } + + return [0, $v]; + } + + public function xResultVar($v) + { + return $this->xVar($v); + } + + /* 6.. */ + + public function xConstructQuery($v) + { + if ($sub_r = $this->x('CONSTRUCT\s*', $v)) { + $r = [ + 'type' => 'construct', + 'dataset' => [], + ]; + $sub_v = $sub_r[1]; + /* construct template */ + if ((list($sub_r, $sub_v) = $this->xConstructTemplate($sub_v)) && is_array($sub_r)) { + $r['construct_triples'] = $sub_r; + } else { + $this->addError('Construct Template not found'); + + return [0, $v]; + } + /* dataset */ + while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { + $r['dataset'][] = $sub_r; + } + /* where */ + if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { + $r['pattern'] = $sub_r; + } else { + return [0, $v]; + } + /* solution modifier */ + if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { + $r = array_merge($r, $sub_r); + } + + return [$r, $sub_v]; + } + + return [0, $v]; + } + + /* 7.. */ + + public function xDescribeQuery($v) + { + if ($sub_r = $this->x('DESCRIBE\s+', $v)) { + $r = [ + 'type' => 'describe', + 'result_vars' => [], + 'result_uris' => [], + 'dataset' => [], + ]; + $sub_v = $sub_r[1]; + $all_vars = 0; + /* result vars/uris */ + if ($sub_r = $this->x('\*\s+', $sub_v)) { + $all_vars = 1; + $sub_v = $sub_r[1]; + } else { + do { + $proceed = 0; + if ((list($sub_r, $sub_v) = $this->xResultVar($sub_v)) && $sub_r) { + $r['result_vars'][] = $sub_r; + $proceed = 1; + } + if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { + $r['result_uris'][] = $sub_r; + $proceed = 1; + } + } while ($proceed); + } + if (!$all_vars && !count($r['result_vars']) && !count($r['result_uris'])) { + $this->addError('No result bindings specified.'); + } + /* dataset */ + while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { + $r['dataset'][] = $sub_r; + } + /* where */ + if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { + $r['pattern'] = $sub_r; + } + /* solution modifier */ + if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { + $r = array_merge($r, $sub_r); + } + /* all vars */ + if ($all_vars) { + foreach ($this->r['vars'] as $var) { + $r['result_vars'][] = ['var' => $var, 'aggregate' => 0, 'alias' => '']; + } + } + + return [$r, $sub_v]; + } + + return [0, $v]; + } + + /* 8.. */ + + public function xAskQuery($v) + { + if ($sub_r = $this->x('ASK\s+', $v)) { + $r = [ + 'type' => 'ask', + 'dataset' => [], + ]; + $sub_v = $sub_r[1]; + /* dataset */ + while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { + $r['dataset'][] = $sub_r; + } + /* where */ + if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { + $r['pattern'] = $sub_r; + + return [$r, $sub_v]; + } else { + $this->addError('Missing or invalid WHERE clause.'); + } + } + + return [0, $v]; + } + + /* 9, 10, 11, 12 */ + + public function xDatasetClause($v) + { + if ($r = $this->x('FROM(\s+NAMED)?\s+', $v)) { + $named = $r[1] ? 1 : 0; + if ((list($r, $sub_v) = $this->xIRIref($r[2])) && $r) { + return [['graph' => $r, 'named' => $named], $sub_v]; + } + } + + return [0, $v]; + } + + /* 13 */ + + public function xWhereClause($v) + { + if ($r = $this->x('(WHERE)?', $v)) { + $v = $r[2]; + } + if ((list($r, $v) = $this->xGroupGraphPattern($v)) && $r) { + return [$r, $v]; + } + + return [0, $v]; + } + + /* 14, 15 */ + + public function xSolutionModifier($v) + { + $r = []; + if ((list($sub_r, $sub_v) = $this->xOrderClause($v)) && $sub_r) { + $r['order_infos'] = $sub_r; + } + while ((list($sub_r, $sub_v) = $this->xLimitOrOffsetClause($sub_v)) && $sub_r) { + $r = array_merge($r, $sub_r); + } + + return ($v == $sub_v) ? [0, $v] : [$r, $sub_v]; + } + + /* 18, 19 */ + + public function xLimitOrOffsetClause($v) + { + if ($sub_r = $this->x('(LIMIT|OFFSET)', $v)) { + $key = strtolower($sub_r[1]); + $sub_v = $sub_r[2]; + if ((list($sub_r, $sub_v) = $this->xINTEGER($sub_v)) && (false !== $sub_r)) { + return [[$key => $sub_r], $sub_v]; + } + if ((list($sub_r, $sub_v) = $this->xPlaceholder($sub_v)) && (false !== $sub_r)) { + return [[$key => $sub_r], $sub_v]; + } + } + + return [0, $v]; + } + + /* 16 */ + + public function xOrderClause($v) + { + if ($sub_r = $this->x('ORDER BY\s+', $v)) { + $sub_v = $sub_r[1]; + $r = []; + while ((list($sub_r, $sub_v) = $this->xOrderCondition($sub_v)) && $sub_r) { + $r[] = $sub_r; + } + if (count($r)) { + return [$r, $sub_v]; + } else { + $this->addError('No order conditions specified.'); + } + } + + return [0, $v]; + } + + /* 17, 27 */ + + public function xOrderCondition($v) + { + if ($sub_r = $this->x('(ASC|DESC)', $v)) { + $dir = strtolower($sub_r[1]); + $sub_v = $sub_r[2]; + if ((list($sub_r, $sub_v) = $this->xBrackettedExpression($sub_v)) && $sub_r) { + $sub_r['direction'] = $dir; + + return [$sub_r, $sub_v]; + } + } elseif ((list($sub_r, $sub_v) = $this->xVar($v)) && $sub_r) { + $sub_r['direction'] = 'asc'; + + return [$sub_r, $sub_v]; + } elseif ((list($sub_r, $sub_v) = $this->xBrackettedExpression($v)) && $sub_r) { + return [$sub_r, $sub_v]; + } elseif ((list($sub_r, $sub_v) = $this->xBuiltInCall($v)) && $sub_r) { + $sub_r['direction'] = 'asc'; + + return [$sub_r, $sub_v]; + } elseif ((list($sub_r, $sub_v) = $this->xFunctionCall($v)) && $sub_r) { + $sub_r['direction'] = 'asc'; + + return [$sub_r, $sub_v]; + } + + return [0, $v]; + } + + /* 20 */ + + public function xGroupGraphPattern($v) + { + $pattern_id = substr(md5(uniqid(rand())), 0, 4); + if ($sub_r = $this->x('\{', $v)) { + $r = ['type' => 'group', 'patterns' => []]; + $sub_v = $sub_r[1]; + if ((list($sub_r, $sub_v) = $this->xTriplesBlock($sub_v)) && $sub_r) { + $this->indexBnodes($sub_r, $pattern_id); + $r['patterns'][] = ['type' => 'triples', 'patterns' => $sub_r]; + } + do { + $proceed = 0; + if ((list($sub_r, $sub_v) = $this->xGraphPatternNotTriples($sub_v)) && $sub_r) { + $r['patterns'][] = $sub_r; + $pattern_id = substr(md5(uniqid(rand())), 0, 4); + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xFilter($sub_v)) && $sub_r) { + $r['patterns'][] = ['type' => 'filter', 'constraint' => $sub_r]; + $proceed = 1; + } + if ($sub_r = $this->x('\.', $sub_v)) { + $sub_v = $sub_r[1]; + } + if ((list($sub_r, $sub_v) = $this->xTriplesBlock($sub_v)) && $sub_r) { + $this->indexBnodes($sub_r, $pattern_id); + $r['patterns'][] = ['type' => 'triples', 'patterns' => $sub_r]; + $proceed = 1; + } + if ((list($sub_r, $sub_v) = $this->xPlaceholder($sub_v)) && $sub_r) { + $r['patterns'][] = $sub_r; + $proceed = 1; + } + } while ($proceed); + if ($sub_r = $this->x('\}', $sub_v)) { + $sub_v = $sub_r[1]; + + return [$r, $sub_v]; + } + $rest = preg_replace('/[\x0a|\x0d]/i', ' ', substr($sub_v, 0, 30)); + $this->addError('Incomplete or invalid Group Graph pattern. Could not handle "'.$rest.'"'); + } + + return [0, $v]; + } + + public function indexBnodes($triples, $pattern_id) + { + $index_id = count($this->bnode_pattern_index['patterns']); + $index_id = $pattern_id; + $this->bnode_pattern_index['patterns'][] = $triples; + foreach ($triples as $t) { + foreach (['s', 'p', 'o'] as $term) { + if ('bnode' == $t[$term.'_type']) { + $val = $t[$term]; + if (isset($this->bnode_pattern_index['bnodes'][$val]) && ($this->bnode_pattern_index['bnodes'][$val] != $index_id)) { + $this->addError('Re-used bnode label "'.$val.'" across graph patterns'); + } else { + $this->bnode_pattern_index['bnodes'][$val] = $index_id; + } + } + } + } + } + + /* 22.., 25.. */ + + public function xGraphPatternNotTriples($v) + { + if ((list($sub_r, $sub_v) = $this->xOptionalGraphPattern($v)) && $sub_r) { + return [$sub_r, $sub_v]; + } + if ((list($sub_r, $sub_v) = $this->xGraphGraphPattern($v)) && $sub_r) { + return [$sub_r, $sub_v]; + } + $r = ['type' => 'union', 'patterns' => []]; + $sub_v = $v; do { - $proceed = 0; - if ((list($sub_r, $sub_v) = $this->xResultVar($sub_v)) && $sub_r) { - $r['result_vars'][] = $sub_r; - $proceed = 1; - } - if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { - $r['result_uris'][] = $sub_r; - $proceed =1; - } + $proceed = 0; + if ((list($sub_r, $sub_v) = $this->xGroupGraphPattern($sub_v)) && $sub_r) { + $r['patterns'][] = $sub_r; + if ($sub_r = $this->x('UNION', $sub_v)) { + $sub_v = $sub_r[1]; + $proceed = 1; + } + } } while ($proceed); - } - if (!$all_vars && !count($r['result_vars']) && !count($r['result_uris'])) { - $this->addError('No result bindings specified.'); - } - /* dataset */ - while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { - $r['dataset'][] = $sub_r; - } - /* where */ - if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { - $r['pattern'] = $sub_r; - } - /* solution modifier */ - if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { - $r = array_merge($r, $sub_r); - } - /* all vars */ - if ($all_vars) { - foreach ($this->r['vars'] as $var) { - $r['result_vars'][] = array('var' => $var, 'aggregate' => 0, 'alias' => ''); - } - } - return array($r, $sub_v); - } - return array(0, $v); - } - - /* 8.. */ - - function xAskQuery($v) { - if ($sub_r = $this->x('ASK\s+', $v)) { - $r = array( - 'type' => 'ask', - 'dataset' => array(), - ); - $sub_v = $sub_r[1]; - /* dataset */ - while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { - $r['dataset'][] = $sub_r; - } - /* where */ - if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { - $r['pattern'] = $sub_r; - return array($r, $sub_v); - } - else { - $this->addError('Missing or invalid WHERE clause.'); - } - } - return array(0, $v); - } - - /* 9, 10, 11, 12 */ - - function xDatasetClause($v) { - if ($r = $this->x('FROM(\s+NAMED)?\s+', $v)) { - $named = $r[1] ? 1 : 0; - if ((list($r, $sub_v) = $this->xIRIref($r[2])) && $r) { - return array(array('graph' => $r, 'named' => $named), $sub_v); - } - } - return array(0, $v); - } - - /* 13 */ - - function xWhereClause($v) { - if ($r = $this->x('(WHERE)?', $v)) { - $v = $r[2]; - } - if ((list($r, $v) = $this->xGroupGraphPattern($v)) && $r) { - return array($r, $v); - } - return array(0, $v); - } - - /* 14, 15 */ - - function xSolutionModifier($v) { - $r = array(); - if ((list($sub_r, $sub_v) = $this->xOrderClause($v)) && $sub_r) { - $r['order_infos'] = $sub_r; - } - while ((list($sub_r, $sub_v) = $this->xLimitOrOffsetClause($sub_v)) && $sub_r) { - $r = array_merge($r, $sub_r); - } - return ($v == $sub_v) ? array(0, $v) : array($r, $sub_v); - } - - /* 18, 19 */ - - function xLimitOrOffsetClause($v) { - if ($sub_r = $this->x('(LIMIT|OFFSET)', $v)) { - $key = strtolower($sub_r[1]); - $sub_v = $sub_r[2]; - if ((list($sub_r, $sub_v) = $this->xINTEGER($sub_v)) && ($sub_r !== false)) { - return array(array($key =>$sub_r), $sub_v); - } - if ((list($sub_r, $sub_v) = $this->xPlaceholder($sub_v)) && ($sub_r !== false)) { - return array(array($key =>$sub_r), $sub_v); - } - } - return array(0, $v); - } - - /* 16 */ - - function xOrderClause($v) { - if ($sub_r = $this->x('ORDER BY\s+', $v)) { - $sub_v = $sub_r[1]; - $r = array(); - while ((list($sub_r, $sub_v) = $this->xOrderCondition($sub_v)) && $sub_r) { - $r[] = $sub_r; - } - if (count($r)) { - return array($r, $sub_v); - } - else { - $this->addError('No order conditions specified.'); - } - } - return array(0, $v); - } - - /* 17, 27 */ - - function xOrderCondition($v) { - if ($sub_r = $this->x('(ASC|DESC)', $v)) { - $dir = strtolower($sub_r[1]); - $sub_v = $sub_r[2]; - if ((list($sub_r, $sub_v) = $this->xBrackettedExpression($sub_v)) && $sub_r) { - $sub_r['direction'] = $dir; - return array($sub_r, $sub_v); - } - } - elseif ((list($sub_r, $sub_v) = $this->xVar($v)) && $sub_r) { - $sub_r['direction'] = 'asc'; - return array($sub_r, $sub_v); - } - elseif ((list($sub_r, $sub_v) = $this->xBrackettedExpression($v)) && $sub_r) { - return array($sub_r, $sub_v); - } - elseif ((list($sub_r, $sub_v) = $this->xBuiltInCall($v)) && $sub_r) { - $sub_r['direction'] = 'asc'; - return array($sub_r, $sub_v); - } - elseif ((list($sub_r, $sub_v) = $this->xFunctionCall($v)) && $sub_r) { - $sub_r['direction'] = 'asc'; - return array($sub_r, $sub_v); - } - return array(0, $v); - } - - /* 20 */ - - function xGroupGraphPattern($v) { - $pattern_id = substr(md5(uniqid(rand())), 0, 4); - if ($sub_r = $this->x('\{', $v)) { - $r = array('type' => 'group', 'patterns' => array()); - $sub_v = $sub_r[1]; - if ((list($sub_r, $sub_v) = $this->xTriplesBlock($sub_v)) && $sub_r) { - $this->indexBnodes($sub_r, $pattern_id); - $r['patterns'][] = array('type' => 'triples', 'patterns' => $sub_r); - } - do { - $proceed = 0; - if ((list($sub_r, $sub_v) = $this->xGraphPatternNotTriples($sub_v)) && $sub_r) { - $r['patterns'][] = $sub_r; - $pattern_id = substr(md5(uniqid(rand())), 0, 4); - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xFilter($sub_v)) && $sub_r) { - $r['patterns'][] = array('type' => 'filter', 'constraint' => $sub_r); - $proceed = 1; - } - if ($sub_r = $this->x('\.', $sub_v)) { - $sub_v = $sub_r[1]; - } - if ((list($sub_r, $sub_v) = $this->xTriplesBlock($sub_v)) && $sub_r) { - $this->indexBnodes($sub_r, $pattern_id); - $r['patterns'][] = array('type' => 'triples', 'patterns' => $sub_r); - $proceed = 1; - } - if ((list($sub_r, $sub_v) = $this->xPlaceholder($sub_v)) && $sub_r) { - $r['patterns'][] = $sub_r; - $proceed = 1; - } - } while ($proceed); - if ($sub_r = $this->x('\}', $sub_v)) { - $sub_v = $sub_r[1]; - return array($r, $sub_v); - } - $rest = preg_replace('/[\x0a|\x0d]/i', ' ', substr($sub_v, 0, 30)); - $this->addError('Incomplete or invalid Group Graph pattern. Could not handle "' . $rest . '"'); - } - return array(0, $v); - } - - function indexBnodes($triples, $pattern_id) { - $index_id = count($this->bnode_pattern_index['patterns']); - $index_id = $pattern_id; - $this->bnode_pattern_index['patterns'][] = $triples; - foreach ($triples as $t) { - foreach (array('s', 'p', 'o') as $term) { - if ($t[$term . '_type'] == 'bnode') { - $val = $t[$term]; - if (isset($this->bnode_pattern_index['bnodes'][$val]) && ($this->bnode_pattern_index['bnodes'][$val] != $index_id)) { - $this->addError('Re-used bnode label "' .$val. '" across graph patterns'); - } - else { - $this->bnode_pattern_index['bnodes'][$val] = $index_id; - } - } - } - } - } - - /* 22.., 25.. */ - - function xGraphPatternNotTriples($v) { - if ((list($sub_r, $sub_v) = $this->xOptionalGraphPattern($v)) && $sub_r) { - return array($sub_r, $sub_v); - } - if ((list($sub_r, $sub_v) = $this->xGraphGraphPattern($v)) && $sub_r) { - return array($sub_r, $sub_v); - } - $r = array('type' => 'union', 'patterns' => array()); - $sub_v = $v; - do { - $proceed = 0; - if ((list($sub_r, $sub_v) = $this->xGroupGraphPattern($sub_v)) && $sub_r) { - $r['patterns'][] = $sub_r; - if ($sub_r = $this->x('UNION', $sub_v)) { - $sub_v = $sub_r[1]; - $proceed = 1; - } - } - } while ($proceed); - $pc = count($r['patterns']); - if ($pc == 1) { - return array($r['patterns'][0], $sub_v); - } - elseif ($pc > 1) { - return array($r, $sub_v); - } - return array(0, $v); - } - - /* 23 */ - - function xOptionalGraphPattern($v) { - if ($sub_r = $this->x('OPTIONAL', $v)) { - $sub_v = $sub_r[1]; - if ((list($sub_r, $sub_v) = $this->xGroupGraphPattern($sub_v)) && $sub_r) { - return array(array('type' => 'optional', 'patterns' => $sub_r['patterns']), $sub_v); - } - $this->addError('Missing or invalid Group Graph Pattern after OPTIONAL'); - } - return array(0, $v); - } - - /* 24.. */ - - function xGraphGraphPattern($v) { - if ($sub_r = $this->x('GRAPH', $v)) { - $sub_v = $sub_r[1]; - $r = array('type' => 'graph', 'var' => '', 'uri' => '', 'patterns' => array()); - if ((list($sub_r, $sub_v) = $this->xVar($sub_v)) && $sub_r) { - $r['var'] = $sub_r; - } - elseif ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { - $r['uri'] = $sub_r; - } - if ($r['var'] || $r['uri']) { - if ((list($sub_r, $sub_v) = $this->xGroupGraphPattern($sub_v)) && $sub_r) { - $r['patterns'][] = $sub_r; - return array($r, $sub_v); - } - $this->addError('Missing or invalid Graph Pattern'); - } - } - return array(0, $v); - } - - /* 26.., 27.. */ - - function xFilter($v) { - if ($r = $this->x('FILTER', $v)) { - $sub_v = $r[1]; - if ((list($r, $sub_v) = $this->xBrackettedExpression($sub_v)) && $r) { - return array($r, $sub_v); - } - if ((list($r, $sub_v) = $this->xBuiltInCall($sub_v)) && $r) { - return array($r, $sub_v); - } - if ((list($r, $sub_v) = $this->xFunctionCall($sub_v)) && $r) { - return array($r, $sub_v); - } - $this->addError('Incomplete FILTER'); - } - return array(0, $v); - } - - /* 28.. */ - - function xFunctionCall($v) { - if ((list($r, $sub_v) = $this->xIRIref($v)) && $r) { - if ((list($sub_r, $sub_v) = $this->xArgList($sub_v)) && $sub_r) { - return array(array('type' => 'function_call', 'uri' => $r, 'args' => $sub_r), $sub_v); - } - } - return array(0, $v); - } - - /* 29 */ - - function xArgList($v) { - $r = array(); - $sub_v = $v; - $closed = 0; - if ($sub_r = $this->x('\(', $sub_v)) { - $sub_v = $sub_r[1]; - do { - $proceed = 0; - if ((list($sub_r, $sub_v) = $this->xExpression($sub_v)) && $sub_r) { - $r[] = $sub_r; - if ($sub_r = $this->x('\,', $sub_v)) { + $pc = count($r['patterns']); + if (1 == $pc) { + return [$r['patterns'][0], $sub_v]; + } elseif ($pc > 1) { + return [$r, $sub_v]; + } + + return [0, $v]; + } + + /* 23 */ + + public function xOptionalGraphPattern($v) + { + if ($sub_r = $this->x('OPTIONAL', $v)) { $sub_v = $sub_r[1]; - $proceed = 1; - } - } - if ($sub_r = $this->x('\)', $sub_v)) { - $sub_v = $sub_r[1]; - $closed = 1; - $proceed = 0; - } - } while ($proceed); - } - return $closed ? array($r, $sub_v) : array(0, $v); - } - - /* 30, 31 */ - - function xConstructTemplate($v) { - if ($sub_r = $this->x('\{', $v)) { - $r = array(); - if ((list($sub_r, $sub_v) = $this->xTriplesBlock($sub_r[1])) && is_array($sub_r)) { - $r = $sub_r; - } - if ($sub_r = $this->x('\}', $sub_v)) { - return array($r, $sub_r[1]); - } - } - return array(0, $v); - } - - /* 46, 47 */ - - function xExpression($v) { - if ((list($sub_r, $sub_v) = $this->xConditionalAndExpression($v)) && $sub_r) { - $r = array('type' => 'expression', 'sub_type' => 'or', 'patterns' => array($sub_r)); - do { - $proceed = 0; - if ($sub_r = $this->x('\|\|', $sub_v)) { - $sub_v = $sub_r[1]; - if ((list($sub_r, $sub_v) = $this->xConditionalAndExpression($sub_v)) && $sub_r) { - $r['patterns'][] = $sub_r; - $proceed = 1; - } - } - } while ($proceed); - return count($r['patterns']) == 1 ? array($r['patterns'][0], $sub_v) : array($r, $sub_v); - } - return array(0, $v); - } - - /* 48.., 49.. */ - - function xConditionalAndExpression($v) { - if ((list($sub_r, $sub_v) = $this->xRelationalExpression($v)) && $sub_r) { - $r = array('type' => 'expression', 'sub_type' => 'and', 'patterns' => array($sub_r)); - do { - $proceed = 0; - if ($sub_r = $this->x('\&\&', $sub_v)) { - $sub_v = $sub_r[1]; - if ((list($sub_r, $sub_v) = $this->xRelationalExpression($sub_v)) && $sub_r) { - $r['patterns'][] = $sub_r; - $proceed = 1; - } - } - } while ($proceed); - return count($r['patterns']) == 1 ? array($r['patterns'][0], $sub_v) : array($r, $sub_v); - } - return array(0, $v); - } - - /* 50, 51 */ - - function xRelationalExpression($v) { - if ((list($sub_r, $sub_v) = $this->xAdditiveExpression($v)) && $sub_r) { - $r = array('type' => 'expression', 'sub_type' => 'relational', 'patterns' => array($sub_r)); - do { - $proceed = 0; - /* don't mistake '<' + uriref with '<'-operator ("longest token" rule) */ - if ((list($sub_r, $sub_v) = $this->xIRI_REF($sub_v)) && $sub_r) { - $this->addError('Expected operator, found IRIref: "'.$sub_r.'".'); - } - if ($sub_r = $this->x('(\!\=|\=\=|\=|\<\=|\>\=|\<|\>)', $sub_v)) { - $op = $sub_r[1]; - $sub_v = $sub_r[2]; - $r['operator'] = $op; - if ((list($sub_r, $sub_v) = $this->xAdditiveExpression($sub_v)) && $sub_r) { - //$sub_r['operator'] = $op; - $r['patterns'][] = $sub_r; - $proceed = 1; - } - } - } while ($proceed); - return count($r['patterns']) == 1 ? array($r['patterns'][0], $sub_v) : array($r, $sub_v); - } - return array(0, $v); - } - - /* 52 */ - - function xAdditiveExpression($v) { - if ((list($sub_r, $sub_v) = $this->xMultiplicativeExpression($v)) && $sub_r) { - $r = array('type' => 'expression', 'sub_type' => 'additive', 'patterns' => array($sub_r)); - do { - $proceed = 0; - if ($sub_r = $this->x('(\+|\-)', $sub_v)) { - $op = $sub_r[1]; - $sub_v = $sub_r[2]; - if ((list($sub_r, $sub_v) = $this->xMultiplicativeExpression($sub_v)) && $sub_r) { - $sub_r['operator'] = $op; - $r['patterns'][] = $sub_r; - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xNumericLiteral($sub_v)) && $sub_r) { - $r['patterns'][] = array('type' => 'numeric', 'operator' => $op, 'value' => $sub_r); - $proceed = 1; - } - } - } while ($proceed); - //return array($r, $sub_v); - return count($r['patterns']) == 1 ? array($r['patterns'][0], $sub_v) : array($r, $sub_v); - } - return array(0, $v); - } - - /* 53 */ - - function xMultiplicativeExpression($v) { - if ((list($sub_r, $sub_v) = $this->xUnaryExpression($v)) && $sub_r) { - $r = array('type' => 'expression', 'sub_type' => 'multiplicative', 'patterns' => array($sub_r)); - do { - $proceed = 0; - if ($sub_r = $this->x('(\*|\/)', $sub_v)) { - $op = $sub_r[1]; - $sub_v = $sub_r[2]; - if ((list($sub_r, $sub_v) = $this->xUnaryExpression($sub_v)) && $sub_r) { - $sub_r['operator'] = $op; - $r['patterns'][] = $sub_r; - $proceed = 1; - } + if ((list($sub_r, $sub_v) = $this->xGroupGraphPattern($sub_v)) && $sub_r) { + return [['type' => 'optional', 'patterns' => $sub_r['patterns']], $sub_v]; + } + $this->addError('Missing or invalid Group Graph Pattern after OPTIONAL'); } - } while ($proceed); - return count($r['patterns']) == 1 ? array($r['patterns'][0], $sub_v) : array($r, $sub_v); + + return [0, $v]; } - return array(0, $v); - } - /* 54 */ + /* 24.. */ + + public function xGraphGraphPattern($v) + { + if ($sub_r = $this->x('GRAPH', $v)) { + $sub_v = $sub_r[1]; + $r = ['type' => 'graph', 'var' => '', 'uri' => '', 'patterns' => []]; + if ((list($sub_r, $sub_v) = $this->xVar($sub_v)) && $sub_r) { + $r['var'] = $sub_r; + } elseif ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { + $r['uri'] = $sub_r; + } + if ($r['var'] || $r['uri']) { + if ((list($sub_r, $sub_v) = $this->xGroupGraphPattern($sub_v)) && $sub_r) { + $r['patterns'][] = $sub_r; + + return [$r, $sub_v]; + } + $this->addError('Missing or invalid Graph Pattern'); + } + } + + return [0, $v]; + } + + /* 26.., 27.. */ + + public function xFilter($v) + { + if ($r = $this->x('FILTER', $v)) { + $sub_v = $r[1]; + if ((list($r, $sub_v) = $this->xBrackettedExpression($sub_v)) && $r) { + return [$r, $sub_v]; + } + if ((list($r, $sub_v) = $this->xBuiltInCall($sub_v)) && $r) { + return [$r, $sub_v]; + } + if ((list($r, $sub_v) = $this->xFunctionCall($sub_v)) && $r) { + return [$r, $sub_v]; + } + $this->addError('Incomplete FILTER'); + } - function xUnaryExpression($v) { - $sub_v = $v; - $op = ''; - if ($sub_r = $this->x('(\!|\+|\-)', $sub_v)) { - $op = $sub_r[1]; - $sub_v = $sub_r[2]; + return [0, $v]; } - if ((list($sub_r, $sub_v) = $this->xPrimaryExpression($sub_v)) && $sub_r) { - if (!is_array($sub_r)) { - $sub_r = array('type' => 'unary', 'expression' => $sub_r); - } - elseif ($sub_op = $this->v1('operator', '', $sub_r)) { - $ops = array('!!' => '', '++' => '+', '--' => '+', '+-' => '-', '-+' => '-'); - $op = isset($ops[$op . $sub_op]) ? $ops[$op . $sub_op] : $op . $sub_op; - } - $sub_r['operator'] = $op; - return array($sub_r, $sub_v); + + /* 28.. */ + + public function xFunctionCall($v) + { + if ((list($r, $sub_v) = $this->xIRIref($v)) && $r) { + if ((list($sub_r, $sub_v) = $this->xArgList($sub_v)) && $sub_r) { + return [['type' => 'function_call', 'uri' => $r, 'args' => $sub_r], $sub_v]; + } + } + + return [0, $v]; } - return array(0, $v); - } - /* 55 */ + /* 29 */ + + public function xArgList($v) + { + $r = []; + $sub_v = $v; + $closed = 0; + if ($sub_r = $this->x('\(', $sub_v)) { + $sub_v = $sub_r[1]; + do { + $proceed = 0; + if ((list($sub_r, $sub_v) = $this->xExpression($sub_v)) && $sub_r) { + $r[] = $sub_r; + if ($sub_r = $this->x('\,', $sub_v)) { + $sub_v = $sub_r[1]; + $proceed = 1; + } + } + if ($sub_r = $this->x('\)', $sub_v)) { + $sub_v = $sub_r[1]; + $closed = 1; + $proceed = 0; + } + } while ($proceed); + } - function xPrimaryExpression($v) { - foreach (array('BrackettedExpression', 'BuiltInCall', 'IRIrefOrFunction', 'RDFLiteral', 'NumericLiteral', 'BooleanLiteral', 'Var', 'Placeholder') as $type) { - $m = 'x' . $type; - if ((list($sub_r, $sub_v) = $this->$m($v)) && $sub_r) { - return array($sub_r, $sub_v); - } + return $closed ? [$r, $sub_v] : [0, $v]; } - return array(0, $v); - } - /* 56 */ + /* 30, 31 */ - function xBrackettedExpression($v) { - if ($r = $this->x('\(', $v)) { - if ((list($r, $sub_v) = $this->xExpression($r[1])) && $r) { - if ($sub_r = $this->x('\)', $sub_v)) { - return array($r, $sub_r[1]); + public function xConstructTemplate($v) + { + if ($sub_r = $this->x('\{', $v)) { + $r = []; + if ((list($sub_r, $sub_v) = $this->xTriplesBlock($sub_r[1])) && is_array($sub_r)) { + $r = $sub_r; + } + if ($sub_r = $this->x('\}', $sub_v)) { + return [$r, $sub_r[1]]; + } } - } + + return [0, $v]; } - return array(0, $v); - } - /* 57.., 58.. */ + /* 46, 47 */ + + public function xExpression($v) + { + if ((list($sub_r, $sub_v) = $this->xConditionalAndExpression($v)) && $sub_r) { + $r = ['type' => 'expression', 'sub_type' => 'or', 'patterns' => [$sub_r]]; + do { + $proceed = 0; + if ($sub_r = $this->x('\|\|', $sub_v)) { + $sub_v = $sub_r[1]; + if ((list($sub_r, $sub_v) = $this->xConditionalAndExpression($sub_v)) && $sub_r) { + $r['patterns'][] = $sub_r; + $proceed = 1; + } + } + } while ($proceed); + + return 1 == count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v]; + } - function xBuiltInCall($v) { - if ($sub_r = $this->x('(str|lang|langmatches|datatype|bound|sameterm|isiri|isuri|isblank|isliteral|regex)\s*\(', $v)) { - $r = array('type' => 'built_in_call', 'call' => strtolower($sub_r[1])); - if ((list($sub_r, $sub_v) = $this->xArgList('(' . $sub_r[2])) && is_array($sub_r)) { - $r['args'] = $sub_r; - return array($r, $sub_v); - } + return [0, $v]; } - return array(0, $v); - } - /* 59.. */ + /* 48.., 49.. */ - function xIRIrefOrFunction($v) { - if ((list($r, $v) = $this->xIRIref($v)) && $r) { - if ((list($sub_r, $sub_v) = $this->xArgList($v)) && is_array($sub_r)) { - return array(array('type' => 'function', 'uri' => $r, 'args' => $sub_r), $sub_v); - } - return array(array('type' => 'uri', 'uri' => $r), $sub_v); + public function xConditionalAndExpression($v) + { + if ((list($sub_r, $sub_v) = $this->xRelationalExpression($v)) && $sub_r) { + $r = ['type' => 'expression', 'sub_type' => 'and', 'patterns' => [$sub_r]]; + do { + $proceed = 0; + if ($sub_r = $this->x('\&\&', $sub_v)) { + $sub_v = $sub_r[1]; + if ((list($sub_r, $sub_v) = $this->xRelationalExpression($sub_v)) && $sub_r) { + $r['patterns'][] = $sub_r; + $proceed = 1; + } + } + } while ($proceed); + + return 1 == count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v]; + } + + return [0, $v]; + } + + /* 50, 51 */ + + public function xRelationalExpression($v) + { + if ((list($sub_r, $sub_v) = $this->xAdditiveExpression($v)) && $sub_r) { + $r = ['type' => 'expression', 'sub_type' => 'relational', 'patterns' => [$sub_r]]; + do { + $proceed = 0; + /* don't mistake '<' + uriref with '<'-operator ("longest token" rule) */ + if ((list($sub_r, $sub_v) = $this->xIRI_REF($sub_v)) && $sub_r) { + $this->addError('Expected operator, found IRIref: "'.$sub_r.'".'); + } + if ($sub_r = $this->x('(\!\=|\=\=|\=|\<\=|\>\=|\<|\>)', $sub_v)) { + $op = $sub_r[1]; + $sub_v = $sub_r[2]; + $r['operator'] = $op; + if ((list($sub_r, $sub_v) = $this->xAdditiveExpression($sub_v)) && $sub_r) { + //$sub_r['operator'] = $op; + $r['patterns'][] = $sub_r; + $proceed = 1; + } + } + } while ($proceed); + + return 1 == count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v]; + } + + return [0, $v]; + } + + /* 52 */ + + public function xAdditiveExpression($v) + { + if ((list($sub_r, $sub_v) = $this->xMultiplicativeExpression($v)) && $sub_r) { + $r = ['type' => 'expression', 'sub_type' => 'additive', 'patterns' => [$sub_r]]; + do { + $proceed = 0; + if ($sub_r = $this->x('(\+|\-)', $sub_v)) { + $op = $sub_r[1]; + $sub_v = $sub_r[2]; + if ((list($sub_r, $sub_v) = $this->xMultiplicativeExpression($sub_v)) && $sub_r) { + $sub_r['operator'] = $op; + $r['patterns'][] = $sub_r; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xNumericLiteral($sub_v)) && $sub_r) { + $r['patterns'][] = ['type' => 'numeric', 'operator' => $op, 'value' => $sub_r]; + $proceed = 1; + } + } + } while ($proceed); + //return array($r, $sub_v); + return 1 == count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v]; + } + + return [0, $v]; + } + + /* 53 */ + + public function xMultiplicativeExpression($v) + { + if ((list($sub_r, $sub_v) = $this->xUnaryExpression($v)) && $sub_r) { + $r = ['type' => 'expression', 'sub_type' => 'multiplicative', 'patterns' => [$sub_r]]; + do { + $proceed = 0; + if ($sub_r = $this->x('(\*|\/)', $sub_v)) { + $op = $sub_r[1]; + $sub_v = $sub_r[2]; + if ((list($sub_r, $sub_v) = $this->xUnaryExpression($sub_v)) && $sub_r) { + $sub_r['operator'] = $op; + $r['patterns'][] = $sub_r; + $proceed = 1; + } + } + } while ($proceed); + + return 1 == count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v]; + } + + return [0, $v]; + } + + /* 54 */ + + public function xUnaryExpression($v) + { + $sub_v = $v; + $op = ''; + if ($sub_r = $this->x('(\!|\+|\-)', $sub_v)) { + $op = $sub_r[1]; + $sub_v = $sub_r[2]; + } + if ((list($sub_r, $sub_v) = $this->xPrimaryExpression($sub_v)) && $sub_r) { + if (!is_array($sub_r)) { + $sub_r = ['type' => 'unary', 'expression' => $sub_r]; + } elseif ($sub_op = $this->v1('operator', '', $sub_r)) { + $ops = ['!!' => '', '++' => '+', '--' => '+', '+-' => '-', '-+' => '-']; + $op = isset($ops[$op.$sub_op]) ? $ops[$op.$sub_op] : $op.$sub_op; + } + $sub_r['operator'] = $op; + + return [$sub_r, $sub_v]; + } + + return [0, $v]; + } + + /* 55 */ + + public function xPrimaryExpression($v) + { + foreach (['BrackettedExpression', 'BuiltInCall', 'IRIrefOrFunction', 'RDFLiteral', 'NumericLiteral', 'BooleanLiteral', 'Var', 'Placeholder'] as $type) { + $m = 'x'.$type; + if ((list($sub_r, $sub_v) = $this->$m($v)) && $sub_r) { + return [$sub_r, $sub_v]; + } + } + + return [0, $v]; } - } - /* 70.. @@sync with TurtleParser */ + /* 56 */ + + public function xBrackettedExpression($v) + { + if ($r = $this->x('\(', $v)) { + if ((list($r, $sub_v) = $this->xExpression($r[1])) && $r) { + if ($sub_r = $this->x('\)', $sub_v)) { + return [$r, $sub_r[1]]; + } + } + } - function xIRI_REF($v) { - if (($r = $this->x('\<(\$\{[^\>]*\})\>', $v)) && ($sub_r = $this->xPlaceholder($r[1]))) { - return array($r[1], $r[2]); + return [0, $v]; } - elseif ($r = $this->x('\<([^\<\>\s\"\|\^`]*)\>', $v)) { - return array($r[1] ? $r[1] : true, $r[2]); + + /* 57.., 58.. */ + + public function xBuiltInCall($v) + { + if ($sub_r = $this->x('(str|lang|langmatches|datatype|bound|sameterm|isiri|isuri|isblank|isliteral|regex)\s*\(', $v)) { + $r = ['type' => 'built_in_call', 'call' => strtolower($sub_r[1])]; + if ((list($sub_r, $sub_v) = $this->xArgList('('.$sub_r[2])) && is_array($sub_r)) { + $r['args'] = $sub_r; + + return [$r, $sub_v]; + } + } + + return [0, $v]; } - /* allow reserved chars in obvious IRIs */ - elseif ($r = $this->x('\<(https?\:[^\s][^\<\>]*)\>', $v)) { - return array($r[1] ? $r[1] : true, $r[2]); + + /* 59.. */ + + public function xIRIrefOrFunction($v) + { + if ((list($r, $v) = $this->xIRIref($v)) && $r) { + if ((list($sub_r, $sub_v) = $this->xArgList($v)) && is_array($sub_r)) { + return [['type' => 'function', 'uri' => $r, 'args' => $sub_r], $sub_v]; + } + + return [['type' => 'uri', 'uri' => $r], $sub_v]; + } } - return array(0, $v); - } + /* 70.. @@sync with TurtleParser */ + + public function xIRI_REF($v) + { + if (($r = $this->x('\<(\$\{[^\>]*\})\>', $v)) && ($sub_r = $this->xPlaceholder($r[1]))) { + return [$r[1], $r[2]]; + } elseif ($r = $this->x('\<([^\<\>\s\"\|\^`]*)\>', $v)) { + return [$r[1] ? $r[1] : true, $r[2]]; + } + /* allow reserved chars in obvious IRIs */ + elseif ($r = $this->x('\<(https?\:[^\s][^\<\>]*)\>', $v)) { + return [$r[1] ? $r[1] : true, $r[2]]; + } + + return [0, $v]; + } } diff --git a/parsers/ARC2_SPARQLPlusParser.php b/parsers/ARC2_SPARQLPlusParser.php index ce655ffe..c0571a91 100644 --- a/parsers/ARC2_SPARQLPlusParser.php +++ b/parsers/ARC2_SPARQLPlusParser.php @@ -10,201 +10,218 @@ class: ARC2 SPARQL+ Parser (SPARQL + Aggregates + LOAD + INSERT + DELETE) ARC2::inc('SPARQLParser'); -class ARC2_SPARQLPlusParser extends ARC2_SPARQLParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - } - - /* +1 */ - - function xQuery($v) { - list($r, $v) = $this->xPrologue($v); - foreach (array('Select', 'Construct', 'Describe', 'Ask', 'Insert', 'Delete', 'Load') as $type) { - $m = 'x' . $type . 'Query'; - if ((list($r, $v) = $this->$m($v)) && $r) { - return array($r, $v); - } +class ARC2_SPARQLPlusParser extends ARC2_SPARQLParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - return array(0, $v); - } - - /* +3 */ - - function xResultVar($v) { - $aggregate = ''; - /* aggregate */ - if ($sub_r = $this->x('\(?(AVG|COUNT|MAX|MIN|SUM)\s*\(\s*([^\)]+)\)\s+AS\s+([^\s\)]+)\)?', $v)) { - $aggregate = $sub_r[1]; - $result_var = $sub_r[3]; - $v = $sub_r[2] . $sub_r[4]; - } - if ($sub_r && (list($sub_r, $sub_v) = $this->xVar($result_var)) && $sub_r) { - $result_var = $sub_r['value']; + + public function __init() + { + parent::__init(); } - /* * or var */ - if ((list($sub_r, $sub_v) = $this->x('\*', $v)) && $sub_r) { - return array(array('var' => '*', 'aggregate' => $aggregate, 'alias' => $aggregate ? $result_var : ''), $sub_v); + + /* +1 */ + + public function xQuery($v) + { + list($r, $v) = $this->xPrologue($v); + foreach (['Select', 'Construct', 'Describe', 'Ask', 'Insert', 'Delete', 'Load'] as $type) { + $m = 'x'.$type.'Query'; + if ((list($r, $v) = $this->$m($v)) && $r) { + return [$r, $v]; + } + } + + return [0, $v]; } - if ((list($sub_r, $sub_v) = $this->xVar($v)) && $sub_r) { - return array(array('var' => $sub_r['value'], 'aggregate' => $aggregate, 'alias' => $aggregate ? $result_var : ''), $sub_v); + + /* +3 */ + + public function xResultVar($v) + { + $aggregate = ''; + /* aggregate */ + if ($sub_r = $this->x('\(?(AVG|COUNT|MAX|MIN|SUM)\s*\(\s*([^\)]+)\)\s+AS\s+([^\s\)]+)\)?', $v)) { + $aggregate = $sub_r[1]; + $result_var = $sub_r[3]; + $v = $sub_r[2].$sub_r[4]; + } + if ($sub_r && (list($sub_r, $sub_v) = $this->xVar($result_var)) && $sub_r) { + $result_var = $sub_r['value']; + } + /* * or var */ + if ((list($sub_r, $sub_v) = $this->x('\*', $v)) && $sub_r) { + return [['var' => '*', 'aggregate' => $aggregate, 'alias' => $aggregate ? $result_var : ''], $sub_v]; + } + if ((list($sub_r, $sub_v) = $this->xVar($v)) && $sub_r) { + return [['var' => $sub_r['value'], 'aggregate' => $aggregate, 'alias' => $aggregate ? $result_var : ''], $sub_v]; + } + + return [0, $v]; } - return array(0, $v); - } - - /* +4 */ - - function xLoadQuery($v) { - if ($sub_r = $this->x('LOAD\s+', $v)) { - $sub_v = $sub_r[1]; - if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { - $r = array('type' => 'load', 'url' => $sub_r, 'target_graph' => ''); - if ($sub_r = $this->x('INTO\s+', $sub_v)) { - $sub_v = $sub_r[1]; - if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { - $r['target_graph'] = $sub_r; - } + + /* +4 */ + + public function xLoadQuery($v) + { + if ($sub_r = $this->x('LOAD\s+', $v)) { + $sub_v = $sub_r[1]; + if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { + $r = ['type' => 'load', 'url' => $sub_r, 'target_graph' => '']; + if ($sub_r = $this->x('INTO\s+', $sub_v)) { + $sub_v = $sub_r[1]; + if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { + $r['target_graph'] = $sub_r; + } + } + + return [$r, $sub_v]; + } } - return array($r, $sub_v); - } + + return [0, $v]; } - return array(0, $v); - } - - /* +5 */ - - function xInsertQuery($v) { - if ($sub_r = $this->x('INSERT\s+', $v)) { - $r = array( - 'type' => 'insert', - 'dataset' => array(), - ); - $sub_v = $sub_r[1]; - /* target */ - if ($sub_r = $this->x('INTO\s+', $sub_v)) { - $sub_v = $sub_r[1]; - if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { - $r['target_graph'] = $sub_r; - /* CONSTRUCT keyword, optional */ - if ($sub_r = $this->x('CONSTRUCT\s+', $sub_v)) { + + /* +5 */ + + public function xInsertQuery($v) + { + if ($sub_r = $this->x('INSERT\s+', $v)) { + $r = [ + 'type' => 'insert', + 'dataset' => [], + ]; $sub_v = $sub_r[1]; - } - /* construct template */ - if ((list($sub_r, $sub_v) = $this->xConstructTemplate($sub_v)) && is_array($sub_r)) { - $r['construct_triples'] = $sub_r; - } - else { - $this->addError('Construct Template not found'); - return array(0, $v); - } - /* dataset */ - while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { - $r['dataset'][] = $sub_r; - } - /* where */ - if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { - $r['pattern'] = $sub_r; - } - /* solution modifier */ - if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { - $r = array_merge($r, $sub_r); - } - return array($r, $sub_v); + /* target */ + if ($sub_r = $this->x('INTO\s+', $sub_v)) { + $sub_v = $sub_r[1]; + if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { + $r['target_graph'] = $sub_r; + /* CONSTRUCT keyword, optional */ + if ($sub_r = $this->x('CONSTRUCT\s+', $sub_v)) { + $sub_v = $sub_r[1]; + } + /* construct template */ + if ((list($sub_r, $sub_v) = $this->xConstructTemplate($sub_v)) && is_array($sub_r)) { + $r['construct_triples'] = $sub_r; + } else { + $this->addError('Construct Template not found'); + + return [0, $v]; + } + /* dataset */ + while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { + $r['dataset'][] = $sub_r; + } + /* where */ + if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { + $r['pattern'] = $sub_r; + } + /* solution modifier */ + if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { + $r = array_merge($r, $sub_r); + } + + return [$r, $sub_v]; + } + } } - } + + return [0, $v]; } - return array(0, $v); - } - - /* +6 */ - - function xDeleteQuery($v) { - if ($sub_r = $this->x('DELETE\s+', $v)) { - $r = array( - 'type' => 'delete', - 'target_graphs' => array() - ); - $sub_v = $sub_r[1]; - /* target */ - do { - $proceed = false; - if ($sub_r = $this->x('FROM\s+', $sub_v)) { - $sub_v = $sub_r[1]; - if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { - $r['target_graphs'][] = $sub_r; - $proceed = 1; - } + + /* +6 */ + + public function xDeleteQuery($v) + { + if ($sub_r = $this->x('DELETE\s+', $v)) { + $r = [ + 'type' => 'delete', + 'target_graphs' => [], + ]; + $sub_v = $sub_r[1]; + /* target */ + do { + $proceed = false; + if ($sub_r = $this->x('FROM\s+', $sub_v)) { + $sub_v = $sub_r[1]; + if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) { + $r['target_graphs'][] = $sub_r; + $proceed = 1; + } + } + } while ($proceed); + /* CONSTRUCT keyword, optional */ + if ($sub_r = $this->x('CONSTRUCT\s+', $sub_v)) { + $sub_v = $sub_r[1]; + } + /* construct template */ + if ((list($sub_r, $sub_v) = $this->xConstructTemplate($sub_v)) && is_array($sub_r)) { + $r['construct_triples'] = $sub_r; + /* dataset */ + while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { + $r['dataset'][] = $sub_r; + } + /* where */ + if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { + $r['pattern'] = $sub_r; + } + /* solution modifier */ + if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { + $r = array_merge($r, $sub_r); + } + } + + return [$r, $sub_v]; } - } while ($proceed); - /* CONSTRUCT keyword, optional */ - if ($sub_r = $this->x('CONSTRUCT\s+', $sub_v)) { - $sub_v = $sub_r[1]; - } - /* construct template */ - if ((list($sub_r, $sub_v) = $this->xConstructTemplate($sub_v)) && is_array($sub_r)) { - $r['construct_triples'] = $sub_r; - /* dataset */ - while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) { - $r['dataset'][] = $sub_r; + + return [0, $v]; + } + + /* +7 */ + + public function xSolutionModifier($v) + { + $r = []; + if ((list($sub_r, $sub_v) = $this->xGroupClause($v)) && $sub_r) { + $r['group_infos'] = $sub_r; } - /* where */ - if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) { - $r['pattern'] = $sub_r; + if ((list($sub_r, $sub_v) = $this->xOrderClause($sub_v)) && $sub_r) { + $r['order_infos'] = $sub_r; } - /* solution modifier */ - if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) { - $r = array_merge($r, $sub_r); + while ((list($sub_r, $sub_v) = $this->xLimitOrOffsetClause($sub_v)) && $sub_r) { + $r = array_merge($r, $sub_r); } - } - return array($r, $sub_v); - } - return array(0, $v); - } - - /* +7 */ - - function xSolutionModifier($v) { - $r = array(); - if ((list($sub_r, $sub_v) = $this->xGroupClause($v)) && $sub_r) { - $r['group_infos'] = $sub_r; - } - if ((list($sub_r, $sub_v) = $this->xOrderClause($sub_v)) && $sub_r) { - $r['order_infos'] = $sub_r; - } - while ((list($sub_r, $sub_v) = $this->xLimitOrOffsetClause($sub_v)) && $sub_r) { - $r = array_merge($r, $sub_r); + + return ($v == $sub_v) ? [0, $v] : [$r, $sub_v]; } - return ($v == $sub_v) ? array(0, $v) : array($r, $sub_v); - } - - /* +8 */ - - function xGroupClause($v) { - if ($sub_r = $this->x('GROUP BY\s+', $v)) { - $sub_v = $sub_r[1]; - $r = array(); - do { - $proceed = 0; - if ((list($sub_r, $sub_v) = $this->xVar($sub_v)) && $sub_r) { - $r[] = $sub_r; - $proceed = 1; - if ($sub_r = $this->x('\,', $sub_v)) { + + /* +8 */ + + public function xGroupClause($v) + { + if ($sub_r = $this->x('GROUP BY\s+', $v)) { $sub_v = $sub_r[1]; - } + $r = []; + do { + $proceed = 0; + if ((list($sub_r, $sub_v) = $this->xVar($sub_v)) && $sub_r) { + $r[] = $sub_r; + $proceed = 1; + if ($sub_r = $this->x('\,', $sub_v)) { + $sub_v = $sub_r[1]; + } + } + } while ($proceed); + if (count($r)) { + return [$r, $sub_v]; + } else { + $this->addError('No columns specified in GROUP BY clause.'); + } } - } while ($proceed); - if (count($r)) { - return array($r, $sub_v); - } - else { - $this->addError('No columns specified in GROUP BY clause.'); - } - } - return array(0, $v); - } -} + return [0, $v]; + } +} diff --git a/parsers/ARC2_SPARQLXMLResultParser.php b/parsers/ARC2_SPARQLXMLResultParser.php index d4c96a99..480a146a 100644 --- a/parsers/ARC2_SPARQLXMLResultParser.php +++ b/parsers/ARC2_SPARQLXMLResultParser.php @@ -10,103 +10,102 @@ class: ARC2 SPARQL Result XML Parser ARC2::inc('LegacyXMLParser'); -class ARC2_SPARQLXMLResultParser extends ARC2_LegacyXMLParser { +class ARC2_SPARQLXMLResultParser extends ARC2_LegacyXMLParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->srx = 'http://www.w3.org/2005/sparql-results#'; - $this->nsp[$this->srx] = 'srx'; - $this->allowCDataNodes = 0; - } - - /* */ - - function done() { - } - - /* */ - - function getVariables() { - $r = array(); - foreach ($this->nodes as $node) { - if ($node['tag'] == $this->srx . 'variable') { - $r[] = $node['a']['name']; - } + public function __init() + {/* reader */ + parent::__init(); + $this->srx = 'http://www.w3.org/2005/sparql-results#'; + $this->nsp[$this->srx] = 'srx'; + $this->allowCDataNodes = 0; } - return $r; - } - - function getRows() { - $r = array(); - $index = $this->getNodeIndex(); - foreach ($this->nodes as $node) { - if ($node['tag'] == $this->srx . 'result') { - $row = array(); - $row_id = $node['id']; - $bindings = isset($index[$row_id])? $index[$row_id] : array(); - foreach ($bindings as $binding) { - $row = array_merge($row, $this->getBinding($binding)); - } - if ($row) { - $r[] = $row; - } - } + + public function done() + { } - return $r; - } - function getBinding($node) { - $r = array(); - $index = $this->getNodeIndex(); - $var = $node['a']['name']; - $term = $index[$node['id']][0]; - $r[$var . ' type'] = preg_replace('/^uri$/', 'uri', substr($term['tag'], strlen($this->srx))); - $r[$var] = ($r[$var . ' type'] == 'bnode') ? '_:' . $term['cdata'] : $term['cdata']; - if (isset($term['a']['datatype'])) { - $r[$var . ' datatype'] = $term['a']['datatype']; + public function getVariables() + { + $r = []; + foreach ($this->nodes as $node) { + if ($node['tag'] == $this->srx.'variable') { + $r[] = $node['a']['name']; + } + } + + return $r; } - elseif (isset($term['a'][$this->xml . 'lang'])) { - $r[$var . ' lang'] = $term['a'][$this->xml . 'lang']; + + public function getRows() + { + $r = []; + $index = $this->getNodeIndex(); + foreach ($this->nodes as $node) { + if ($node['tag'] == $this->srx.'result') { + $row = []; + $row_id = $node['id']; + $bindings = isset($index[$row_id]) ? $index[$row_id] : []; + foreach ($bindings as $binding) { + $row = array_merge($row, $this->getBinding($binding)); + } + if ($row) { + $r[] = $row; + } + } + } + + return $r; } - return $r; - } - function getBooleanInsertedDeleted() { - foreach ($this->nodes as $node) { - if ($node['tag'] == $this->srx . 'boolean') { - return ($node['cdata'] == 'true') ? array('boolean' => true) : array('boolean' => false); - } - elseif ($node['tag'] == $this->srx . 'inserted') { - return array('inserted' => $node['cdata']); - } - elseif ($node['tag'] == $this->srx . 'deleted') { - return array('deleted' => $node['cdata']); - } - elseif ($node['tag'] == $this->srx . 'results') { - return ''; - } + public function getBinding($node) + { + $r = []; + $index = $this->getNodeIndex(); + $var = $node['a']['name']; + $term = $index[$node['id']][0]; + $r[$var.' type'] = preg_replace('/^uri$/', 'uri', substr($term['tag'], strlen($this->srx))); + $r[$var] = ('bnode' == $r[$var.' type']) ? '_:'.$term['cdata'] : $term['cdata']; + if (isset($term['a']['datatype'])) { + $r[$var.' datatype'] = $term['a']['datatype']; + } elseif (isset($term['a'][$this->xml.'lang'])) { + $r[$var.' lang'] = $term['a'][$this->xml.'lang']; + } + + return $r; } - return ''; - } - /* */ - - function getStructure() { - $r = array('variables' => $this->getVariables(), 'rows' => $this->getRows()); - /* boolean|inserted|deleted */ - if ($sub_r = $this->getBooleanInsertedDeleted()) { - foreach ($sub_r as $k => $v) { - $r[$k] = $v; - } + public function getBooleanInsertedDeleted() + { + foreach ($this->nodes as $node) { + if ($node['tag'] == $this->srx.'boolean') { + return ('true' == $node['cdata']) ? ['boolean' => true] : ['boolean' => false]; + } elseif ($node['tag'] == $this->srx.'inserted') { + return ['inserted' => $node['cdata']]; + } elseif ($node['tag'] == $this->srx.'deleted') { + return ['deleted' => $node['cdata']]; + } elseif ($node['tag'] == $this->srx.'results') { + return ''; + } + } + + return ''; } - return $r; - } - /* */ + public function getStructure() + { + $r = ['variables' => $this->getVariables(), 'rows' => $this->getRows()]; + /* boolean|inserted|deleted */ + if ($sub_r = $this->getBooleanInsertedDeleted()) { + foreach ($sub_r as $k => $v) { + $r[$k] = $v; + } + } - + return $r; + } } diff --git a/parsers/ARC2_SPOGParser.php b/parsers/ARC2_SPOGParser.php index 9158254d..de6bed69 100755 --- a/parsers/ARC2_SPOGParser.php +++ b/parsers/ARC2_SPOGParser.php @@ -1,188 +1,185 @@ - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('RDFParser'); -class ARC2_SPOGParser extends ARC2_RDFParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->encoding = $this->v('encoding', false, $this->a); - $this->xml = 'http://www.w3.org/XML/1998/namespace'; - $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $this->nsp = array($this->xml => 'xml', $this->rdf => 'rdf'); - $this->target_encoding = ''; - } - - /* */ - - function parse($path, $data = '', $iso_fallback = false) { - $this->state = 0; - /* reader */ - if (!$this->v('reader')) { - ARC2::inc('Reader'); - $this->reader = new ARC2_Reader($this->a, $this); +class ARC2_SPOGParser extends ARC2_RDFParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } + + public function __init() + {/* reader */ + parent::__init(); + $this->encoding = $this->v('encoding', false, $this->a); + $this->xml = 'http://www.w3.org/XML/1998/namespace'; + $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $this->nsp = [$this->xml => 'xml', $this->rdf => 'rdf']; + $this->target_encoding = ''; } - $this->reader->setAcceptHeader('Accept: sparql-results+xml; q=0.9, */*; q=0.1'); - $this->reader->activate($path, $data); - $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; - /* xml parser */ - $this->initXMLParser(); - /* parse */ - $first = true; - while ($d = $this->reader->readStream()) { - if ($iso_fallback && $first) { - $d = '' . "\n" . preg_replace('/^\<\?xml [^\>]+\?\>\s*/s', '', $d); - $first = false; - } - if (!xml_parse($this->xml_parser, $d, false)) { - $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); - $line = xml_get_current_line_number($this->xml_parser); - $this->tmp_error = 'XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')'; - $this->tmp_error .= $d . urlencode($d); - if (0 && !$iso_fallback && preg_match("/Invalid character/i", $error_str)) { - xml_parser_free($this->xml_parser); - unset($this->xml_parser); - $this->reader->closeStream(); - $this->__init(); - $this->encoding = 'ISO-8859-1'; - unset($this->xml_parser); - unset($this->reader); - return $this->parse($path, $data, true); + + public function parse($path, $data = '', $iso_fallback = false) + { + $this->state = 0; + /* reader */ + if (!$this->v('reader')) { + ARC2::inc('Reader'); + $this->reader = new ARC2_Reader($this->a, $this); } - else { - return $this->addError($this->tmp_error); + $this->reader->setAcceptHeader('Accept: sparql-results+xml; q=0.9, */*; q=0.1'); + $this->reader->activate($path, $data); + $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; + /* xml parser */ + $this->initXMLParser(); + /* parse */ + $first = true; + while ($d = $this->reader->readStream()) { + if ($iso_fallback && $first) { + $d = ''."\n".preg_replace('/^\<\?xml [^\>]+\?\>\s*/s', '', $d); + $first = false; + } + if (!xml_parse($this->xml_parser, $d, false)) { + $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); + $line = xml_get_current_line_number($this->xml_parser); + $this->tmp_error = 'XML error: "'.$error_str.'" at line '.$line.' (parsing as '.$this->getEncoding().')'; + $this->tmp_error .= $d.urlencode($d); + if (0 && !$iso_fallback && preg_match('/Invalid character/i', $error_str)) { + xml_parser_free($this->xml_parser); + unset($this->xml_parser); + $this->reader->closeStream(); + $this->__init(); + $this->encoding = 'ISO-8859-1'; + unset($this->xml_parser); + unset($this->reader); + + return $this->parse($path, $data, true); + } else { + return $this->addError($this->tmp_error); + } + } } - } - } - $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); - xml_parser_free($this->xml_parser); - $this->reader->closeStream(); - unset($this->reader); - return $this->done(); - } - - /* */ - - function initXMLParser() { - if (!isset($this->xml_parser)) { - $enc = preg_match('/^(utf\-8|iso\-8859\-1|us\-ascii)$/i', $this->getEncoding(), $m) ? $m[1] : 'UTF-8'; - $parser = xml_parser_create($enc); - xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); - xml_set_element_handler($parser, 'open', 'close'); - xml_set_character_data_handler($parser, 'cdata'); - xml_set_start_namespace_decl_handler($parser, 'nsDecl'); - xml_set_object($parser, $this); - $this->xml_parser = $parser; - } - } + $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); + xml_parser_free($this->xml_parser); + $this->reader->closeStream(); + unset($this->reader); - /* */ - - function getEncoding($src = 'config') { - if ($src == 'parser') { - return $this->target_encoding; - } - elseif (($src == 'config') && $this->encoding) { - return $this->encoding; + return $this->done(); } - return $this->reader->getEncoding(); - return 'UTF-8'; - } - - /* */ - - function getTriples() { - return $this->v('triples', array()); - } - - function countTriples() { - return $this->t_count; - } - - function addT($s = '', $p = '', $o = '', $s_type = '', $o_type = '', $o_dt = '', $o_lang = '', $g = '') { - if (!($s && $p && $o)) return 0; - //echo "-----\nadding $s / $p / $o\n-----\n"; - $t = array('s' => $s, 'p' => $p, 'o' => $o, 's_type' => $s_type, 'o_type' => $o_type, 'o_datatype' => $o_dt, 'o_lang' => $o_lang, 'g' => $g); - if ($this->skip_dupes) { - $h = md5(serialize($t)); - if (!isset($this->added_triples[$h])) { - $this->triples[$this->t_count] = $t; - $this->t_count++; - $this->added_triples[$h] = true; - } - } - else { - $this->triples[$this->t_count] = $t; - $this->t_count++; + + public function initXMLParser() + { + if (!isset($this->xml_parser)) { + $enc = preg_match('/^(utf\-8|iso\-8859\-1|us\-ascii)$/i', $this->getEncoding(), $m) ? $m[1] : 'UTF-8'; + $parser = xml_parser_create($enc); + xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); + xml_set_element_handler($parser, 'open', 'close'); + xml_set_character_data_handler($parser, 'cdata'); + xml_set_start_namespace_decl_handler($parser, 'nsDecl'); + xml_set_object($parser, $this); + $this->xml_parser = $parser; + } } - } - - /* */ - - function open($p, $t, $a) { - $this->state = $t; - if ($t == 'result') { - $this->t = array(); + + public function getEncoding($src = 'config') + { + if ('parser' == $src) { + return $this->target_encoding; + } elseif (('config' == $src) && $this->encoding) { + return $this->encoding; + } + + return $this->reader->getEncoding(); + + return 'UTF-8'; } - elseif ($t == 'binding') { - $this->binding = $a['name']; - $this->t[$this->binding] = ''; + + public function getTriples() + { + return $this->v('triples', []); } - elseif ($t == 'literal') { - $this->t[$this->binding . '_dt'] = $this->v('datatype', '', $a); - $this->t[$this->binding . '_lang'] = $this->v('xml:lang', '', $a); - $this->t[$this->binding . '_type'] = 'literal'; + + public function countTriples() + { + return $this->t_count; } - elseif ($t == 'uri') { - $this->t[$this->binding . '_type'] = 'uri'; + + public function addT($s = '', $p = '', $o = '', $s_type = '', $o_type = '', $o_dt = '', $o_lang = '', $g = '') + { + if (!($s && $p && $o)) { + return 0; + } + //echo "-----\nadding $s / $p / $o\n-----\n"; + $t = ['s' => $s, 'p' => $p, 'o' => $o, 's_type' => $s_type, 'o_type' => $o_type, 'o_datatype' => $o_dt, 'o_lang' => $o_lang, 'g' => $g]; + if ($this->skip_dupes) { + $h = md5(serialize($t)); + if (!isset($this->added_triples[$h])) { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + $this->added_triples[$h] = true; + } + } else { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + } } - elseif ($t == 'bnode') { - $this->t[$this->binding . '_type'] = 'bnode'; - $this->t[$this->binding] = '_:'; + + public function open($p, $t, $a) + { + $this->state = $t; + if ('result' == $t) { + $this->t = []; + } elseif ('binding' == $t) { + $this->binding = $a['name']; + $this->t[$this->binding] = ''; + } elseif ('literal' == $t) { + $this->t[$this->binding.'_dt'] = $this->v('datatype', '', $a); + $this->t[$this->binding.'_lang'] = $this->v('xml:lang', '', $a); + $this->t[$this->binding.'_type'] = 'literal'; + } elseif ('uri' == $t) { + $this->t[$this->binding.'_type'] = 'uri'; + } elseif ('bnode' == $t) { + $this->t[$this->binding.'_type'] = 'bnode'; + $this->t[$this->binding] = '_:'; + } } - } - - function close($p, $t) { - $this->prev_state = $this->state; - $this->state = ''; - if ($t == 'result') { - $this->addT( - $this->v('s', '', $this->t), - $this->v('p', '', $this->t), - $this->v('o', '', $this->t), - $this->v('s_type', '', $this->t), - $this->v('o_type', '', $this->t), - $this->v('o_dt', '', $this->t), - $this->v('o_lang', '', $this->t), + + public function close($p, $t) + { + $this->prev_state = $this->state; + $this->state = ''; + if ('result' == $t) { + $this->addT( + $this->v('s', '', $this->t), + $this->v('p', '', $this->t), + $this->v('o', '', $this->t), + $this->v('s_type', '', $this->t), + $this->v('o_type', '', $this->t), + $this->v('o_dt', '', $this->t), + $this->v('o_lang', '', $this->t), $this->v('g', '', $this->t) ); + } + } + + public function cData($p, $d) + { + if (in_array($this->state, ['uri', 'bnode', 'literal'])) { + $this->t[$this->binding] .= $d; + } } - } - function cData($p, $d) { - if (in_array($this->state, array('uri', 'bnode', 'literal'))) { - $this->t[$this->binding] .= $d; + public function nsDecl($p, $prf, $uri) + { + $this->nsp[$uri] = isset($this->nsp[$uri]) ? $this->nsp[$uri] : $prf; } - } - - function nsDecl($p, $prf, $uri) { - $this->nsp[$uri] = isset($this->nsp[$uri]) ? $this->nsp[$uri] : $prf; - } - - /* */ - } diff --git a/parsers/ARC2_SemHTMLParser.php b/parsers/ARC2_SemHTMLParser.php index bc562066..dccc4476 100644 --- a/parsers/ARC2_SemHTMLParser.php +++ b/parsers/ARC2_SemHTMLParser.php @@ -10,330 +10,349 @@ class: ARC2 RDF/XML Parser ARC2::inc('LegacyXMLParser'); -class ARC2_SemHTMLParser extends ARC2_LegacyXMLParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->default_sem_html_formats = 'dc openid erdf rdfa posh-rdf microformats'; - $this->triples = array(); - $this->target_encoding = ''; - $this->t_count = 0; - $this->added_triples = array(); - $this->skip_dupes = false; - $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); - $this->bnode_id = 0; - $this->auto_extract = $this->v('auto_extract', 1, $this->a); - $this->extracted_formats = array(); - $this->cache = array(); - $this->detected_formats = array(); - $this->keep_cdata_ws = $this->v('keep_cdata_whitespace', 0, $this->a); - } - - /* */ - - function x($re, $v, $options = 'si', $keep_ws = 0) { - list($ws, $v) = preg_match('/^(\s*)(.*)$/s', $v, $m) ? array($m[1], $m[2]) : array('', $v); - if (preg_match("/^" . $re . "(.*)$/" . $options, $v, $m)) { - if ($keep_ws) $m[1] = $ws . $m[1]; - return $m; +class ARC2_SemHTMLParser extends ARC2_LegacyXMLParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - return false; - } - - /* */ - - function setReader(&$reader) { - $this->reader = $reader; - } - - function createBnodeID(){ - $this->bnode_id++; - return '_:' . $this->bnode_prefix . $this->bnode_id; - } - - function addT($t) { - if (function_exists('html_entity_decode')) { - $t['o'] = html_entity_decode($t['o']); + + public function __init() + {/* reader */ + parent::__init(); + $this->default_sem_html_formats = 'dc openid erdf rdfa posh-rdf microformats'; + $this->triples = []; + $this->target_encoding = ''; + $this->t_count = 0; + $this->added_triples = []; + $this->skip_dupes = false; + $this->bnode_prefix = $this->v('bnode_prefix', 'arc'.substr(md5(uniqid(rand())), 0, 4).'b', $this->a); + $this->bnode_id = 0; + $this->auto_extract = $this->v('auto_extract', 1, $this->a); + $this->extracted_formats = []; + $this->cache = []; + $this->detected_formats = []; + $this->keep_cdata_ws = $this->v('keep_cdata_whitespace', 0, $this->a); } - if ($this->skip_dupes) { - $h = md5(serialize($t)); - if (!isset($this->added_triples[$h])) { - $this->triples[$this->t_count] = $t; - $this->t_count++; - $this->added_triples[$h] = true; - } + + public function x($re, $v, $options = 'si', $keep_ws = 0) + { + list($ws, $v) = preg_match('/^(\s*)(.*)$/s', $v, $m) ? [$m[1], $m[2]] : ['', $v]; + if (preg_match('/^'.$re.'(.*)$/'.$options, $v, $m)) { + if ($keep_ws) { + $m[1] = $ws.$m[1]; + } + + return $m; + } + + return false; } - else { - $this->triples[$this->t_count] = $t; - $this->t_count++; + + public function setReader(&$reader) + { + $this->reader = $reader; } - } - - function getTriples() { - return $this->v('triples', array()); - } - - function countTriples() { - return $this->t_count; - } - - function getSimpleIndex($flatten_objects = 1, $vals = '') { - return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals); - } - - /* */ - - function parse($path, $data = '', $iso_fallback = 'ignore') { - $this->nodes = array(); - $this->node_count = 0; - $this->level = 0; - /* reader */ - if (!$this->v('reader')) { - ARC2::inc('Reader'); - $this->reader = new ARC2_Reader($this->a, $this); + + public function createBnodeID() + { + ++$this->bnode_id; + + return '_:'.$this->bnode_prefix.$this->bnode_id; } - $this->reader->setAcceptHeader('Accept: text/html, application/xhtml, */*; q=0.9'); - $this->reader->activate($path, $data); - $this->target_encoding = $this->reader->getEncoding(false); - $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; - $this->base = $this->x_base; - $this->doc_url = $this->reader->base; - /* parse */ - $rest = ''; - $this->cur_tag = ''; - while ($d = $this->reader->readStream(1)) { - $rest = $this->processData($rest . $d); + + public function addT($t) + { + if (function_exists('html_entity_decode')) { + $t['o'] = html_entity_decode($t['o']); + } + if ($this->skip_dupes) { + $h = md5(serialize($t)); + if (!isset($this->added_triples[$h])) { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + $this->added_triples[$h] = true; + } + } else { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + } } - $this->reader->closeStream(); - unset($this->reader); - return $this->done(); - } - - /* */ - - function getEncoding($src = 'ignore') { - return $this->target_encoding; - } - - /* */ - - function done() { - if ($this->auto_extract) { - $this->extractRDF(); + + public function getTriples() + { + return $this->v('triples', []); } - } - - /* */ - - function processData($v) { - $sub_v = $v; - do { - $proceed = 1; - if ((list($sub_r, $sub_v) = $this->xComment($sub_v)) && $sub_r) { - $this->open(0, 'comment', array('value' => $sub_r)); - $this->close(0, 'comment'); - continue; - } - if ((list($sub_r, $sub_v) = $this->xDoctype($sub_v)) && $sub_r) { - $this->open(0, 'doctype', array('value' => $sub_r)); - $this->close(0, 'doctype'); - /* RDFa detection */ - if (preg_match('/rdfa /i', $sub_r)) $this->detected_formats['rdfa'] = 1; - continue; - } - if ($this->level && ((list($sub_r, $sub_v) = $this->xWS($sub_v)) && $sub_r)) { - $this->cData(0, $sub_r); - } - elseif ((list($sub_r, $sub_v) = $this->xOpen($sub_v)) && $sub_r) { - $this->open(0, $sub_r['tag'], $sub_r['a']); - $this->cur_tag = $sub_r['tag']; - if ($sub_r['empty']) { - $this->close(0, $sub_r['tag'], 1); - $this->cur_tag = ''; + + public function countTriples() + { + return $this->t_count; + } + + public function getSimpleIndex($flatten_objects = 1, $vals = '') + { + return ARC2::getSimpleIndex($this->getTriples(), $flatten_objects, $vals); + } + + public function parse($path, $data = '', $iso_fallback = 'ignore') + { + $this->nodes = []; + $this->node_count = 0; + $this->level = 0; + /* reader */ + if (!$this->v('reader')) { + ARC2::inc('Reader'); + $this->reader = new ARC2_Reader($this->a, $this); } - /* eRDF detection */ - if (!isset($this->detected_formats['erdf']) && isset($sub_r['a']['profile m']) && in_array('http://purl.org/NET/erdf/profile', $sub_r['a']['profile m'])) $this->detected_formats['erdf'] = 1; - /* poshRDF detection */ - if (!isset($this->detected_formats['posh-rdf']) && isset($sub_r['a']['class m']) && in_array('rdf-p', $sub_r['a']['class m'])) $this->detected_formats['posh-rdf'] = 1; - /* RDFa detection */ - if (!isset($this->detected_formats['rdfa']) && ($this->cur_tag == 'html') && isset($sub_r['a']['version m']) && in_array('XHTML+RDFa', $sub_r['a']['version m'])) $this->detected_formats['rdfa'] = 1; - if (!isset($this->detected_formats['rdfa']) && isset($sub_r['a']['xmlns']) && $sub_r['a']['xmlns'] && $this->isRDFNSDecl($sub_r['a']['xmlns'])) $this->detected_formats['rdfa'] = 1; - if (!isset($this->detected_formats['rdfa']) && array_intersect(array('about', 'typeof', 'property'), array_keys($sub_r['a']))) $this->detected_formats['rdfa'] = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xClose($sub_v)) && $sub_r) { - if (preg_match('/^(area|base|br|col|frame|hr|input|img|link|xmeta|param)$/', $sub_r['tag'])) { - /* already implicitly closed */ + $this->reader->setAcceptHeader('Accept: text/html, application/xhtml, */*; q=0.9'); + $this->reader->activate($path, $data); + $this->target_encoding = $this->reader->getEncoding(false); + $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; + $this->base = $this->x_base; + $this->doc_url = $this->reader->base; + /* parse */ + $rest = ''; + $this->cur_tag = ''; + while ($d = $this->reader->readStream(1)) { + $rest = $this->processData($rest.$d); } - else { - $this->close(0, $sub_r['tag']); - $this->cur_tag = ''; + $this->reader->closeStream(); + unset($this->reader); + + return $this->done(); + } + + public function getEncoding($src = 'ignore') + { + return $this->target_encoding; + } + + public function done() + { + if ($this->auto_extract) { + $this->extractRDF(); } - } - elseif ((list($sub_r, $sub_v) = $this->xCData($sub_v)) && $sub_r) { - $this->cData(0, $sub_r); - } - else { - $proceed = 0; - } - } while ($proceed); - return $sub_v; - } - - /* */ - - function isRDFNSDecl($ns) { - foreach ($ns as $k => $v) { - if ($k) return 1; } - return 0; - } - /* */ + public function processData($v) + { + $sub_v = $v; + do { + $proceed = 1; + if ((list($sub_r, $sub_v) = $this->xComment($sub_v)) && $sub_r) { + $this->open(0, 'comment', ['value' => $sub_r]); + $this->close(0, 'comment'); + continue; + } + if ((list($sub_r, $sub_v) = $this->xDoctype($sub_v)) && $sub_r) { + $this->open(0, 'doctype', ['value' => $sub_r]); + $this->close(0, 'doctype'); + /* RDFa detection */ + if (preg_match('/rdfa /i', $sub_r)) { + $this->detected_formats['rdfa'] = 1; + } + continue; + } + if ($this->level && ((list($sub_r, $sub_v) = $this->xWS($sub_v)) && $sub_r)) { + $this->cData(0, $sub_r); + } elseif ((list($sub_r, $sub_v) = $this->xOpen($sub_v)) && $sub_r) { + $this->open(0, $sub_r['tag'], $sub_r['a']); + $this->cur_tag = $sub_r['tag']; + if ($sub_r['empty']) { + $this->close(0, $sub_r['tag'], 1); + $this->cur_tag = ''; + } + /* eRDF detection */ + if (!isset($this->detected_formats['erdf']) && isset($sub_r['a']['profile m']) && in_array('http://purl.org/NET/erdf/profile', $sub_r['a']['profile m'])) { + $this->detected_formats['erdf'] = 1; + } + /* poshRDF detection */ + if (!isset($this->detected_formats['posh-rdf']) && isset($sub_r['a']['class m']) && in_array('rdf-p', $sub_r['a']['class m'])) { + $this->detected_formats['posh-rdf'] = 1; + } + /* RDFa detection */ + if (!isset($this->detected_formats['rdfa']) && ('html' == $this->cur_tag) && isset($sub_r['a']['version m']) && in_array('XHTML+RDFa', $sub_r['a']['version m'])) { + $this->detected_formats['rdfa'] = 1; + } + if (!isset($this->detected_formats['rdfa']) && isset($sub_r['a']['xmlns']) && $sub_r['a']['xmlns'] && $this->isRDFNSDecl($sub_r['a']['xmlns'])) { + $this->detected_formats['rdfa'] = 1; + } + if (!isset($this->detected_formats['rdfa']) && array_intersect(['about', 'typeof', 'property'], array_keys($sub_r['a']))) { + $this->detected_formats['rdfa'] = 1; + } + } elseif ((list($sub_r, $sub_v) = $this->xClose($sub_v)) && $sub_r) { + if (preg_match('/^(area|base|br|col|frame|hr|input|img|link|xmeta|param)$/', $sub_r['tag'])) { + /* already implicitly closed */ + } else { + $this->close(0, $sub_r['tag']); + $this->cur_tag = ''; + } + } elseif ((list($sub_r, $sub_v) = $this->xCData($sub_v)) && $sub_r) { + $this->cData(0, $sub_r); + } else { + $proceed = 0; + } + } while ($proceed); - function xComment($v) { - if ($r = $this->x('\<\!\-\-', $v)) { - if ($sub_r = $this->x('(.*)\-\-\>', $r[1], 'Us')) { - return array($sub_r[1], $sub_r[2]); - } + return $sub_v; } - return array(0, $v); - } - - function xDoctype($v) { - if ($r = $this->x('\<\!DOCTYPE', $v)) { - if ($sub_r = $this->x('([^\>]+)\>', $r[1])) { - return array($sub_r[1], $sub_r[2]); - } + + public function isRDFNSDecl($ns) + { + foreach ($ns as $k => $v) { + if ($k) { + return 1; + } + } + + return 0; } - return array(0, $v); - } - - function xWS($v) { - if ($r = ARC2::x('(\s+)', $v)) { - return array($r[1], $r[2]); + + public function xComment($v) + { + if ($r = $this->x('\<\!\-\-', $v)) { + if ($sub_r = $this->x('(.*)\-\-\>', $r[1], 'Us')) { + return [$sub_r[1], $sub_r[2]]; + } + } + + return [0, $v]; } - return array(0, $v); - } - - /* */ - - function xOpen($v) { - if ($r = $this->x('\<([^\s\/\>]+)([^\>]*)\>', $v)) { - list($sub_r, $sub_v) = $this->xAttributes($r[2]); - return array(array('tag' => strtolower($r[1]), 'a' => $sub_r, 'empty' => $this->isEmpty($r[1], $r[2])), $r[3]); + + public function xDoctype($v) + { + if ($r = $this->x('\<\!DOCTYPE', $v)) { + if ($sub_r = $this->x('([^\>]+)\>', $r[1])) { + return [$sub_r[1], $sub_r[2]]; + } + } + + return [0, $v]; } - return array(0, $v); - } - - /* */ - - function xAttributes($v) { - $r = array(); - while ((list($sub_r, $v) = $this->xAttribute($v)) && $sub_r) { - if ($sub_sub_r = $this->x('xmlns\:?(.*)', $sub_r['k'])) { - $this->nsDecl(0, $sub_sub_r[1], $sub_r['value']); - $r['xmlns'][$sub_sub_r[1]] = $sub_r['value']; - } - else { - $r[$sub_r['k']] = $sub_r['value']; - $r[$sub_r['k'] . ' m'] = $sub_r['values']; - } + + public function xWS($v) + { + if ($r = ARC2::x('(\s+)', $v)) { + return [$r[1], $r[2]]; + } + + return [0, $v]; } - return array($r, $v); - } - /* */ + public function xOpen($v) + { + if ($r = $this->x('\<([^\s\/\>]+)([^\>]*)\>', $v)) { + list($sub_r, $sub_v) = $this->xAttributes($r[2]); - function xAttribute($v) { - if ($r = $this->x('([^\s\=]+)\s*(\=)?\s*([\'\"]?)', $v)) { - if (!$r[2]) {/* no '=' */ - if ($r[1] == '/') { - return array(0, $r[4]); - } - return array(array('k' => $r[1], 'value' => 1, 'values' => array(1)), $r[4]); - } - if (!$r[3]) {/* no quots */ - if ($sub_r = $this->x('([^\s]+)', $r[4])) { - return array(array('k' => $r[1], 'value' => $sub_r[1], 'values' => array($sub_r[1])), $sub_r[2]); + return [['tag' => strtolower($r[1]), 'a' => $sub_r, 'empty' => $this->isEmpty($r[1], $r[2])], $r[3]]; } - return array(array('k' => $r[1], 'value' => '', 'values' => array()), $r[4]); - } - $val = ''; - $multi = 0; - $sub_v = $r[4]; - while ($sub_v && (!$sub_r = $this->x('(\x5c\\' .$r[3]. '|\\' .$r[3]. ')', $sub_v))) { - $val .= substr($sub_v, 0, 1); - $sub_v = substr($sub_v, 1); - } - $sub_v = $sub_v ? $sub_r[2] : $sub_v; - $vals = preg_split('/ /', $val); - return array(array('k' => $r[1], 'value' => $val, 'values' => $vals), $sub_v); + + return [0, $v]; } - return array(0, $v); - } - - /* */ - - function isEmpty($t, $v) { - if (preg_match('/^(area|base|br|col|frame|hr|input|img|link|xmeta|param)$/', $t)) { - return 1; + + public function xAttributes($v) + { + $r = []; + while ((list($sub_r, $v) = $this->xAttribute($v)) && $sub_r) { + if ($sub_sub_r = $this->x('xmlns\:?(.*)', $sub_r['k'])) { + $this->nsDecl(0, $sub_sub_r[1], $sub_r['value']); + $r['xmlns'][$sub_sub_r[1]] = $sub_r['value']; + } else { + $r[$sub_r['k']] = $sub_r['value']; + $r[$sub_r['k'].' m'] = $sub_r['values']; + } + } + + return [$r, $v]; } - if (preg_match('/\/$/', $v)) { - return 1; + + public function xAttribute($v) + { + if ($r = $this->x('([^\s\=]+)\s*(\=)?\s*([\'\"]?)', $v)) { + if (!$r[2]) {/* no '=' */ + if ('/' == $r[1]) { + return [0, $r[4]]; + } + + return [['k' => $r[1], 'value' => 1, 'values' => [1]], $r[4]]; + } + if (!$r[3]) {/* no quots */ + if ($sub_r = $this->x('([^\s]+)', $r[4])) { + return [['k' => $r[1], 'value' => $sub_r[1], 'values' => [$sub_r[1]]], $sub_r[2]]; + } + + return [['k' => $r[1], 'value' => '', 'values' => []], $r[4]]; + } + $val = ''; + $multi = 0; + $sub_v = $r[4]; + while ($sub_v && (!$sub_r = $this->x('(\x5c\\'.$r[3].'|\\'.$r[3].')', $sub_v))) { + $val .= substr($sub_v, 0, 1); + $sub_v = substr($sub_v, 1); + } + $sub_v = $sub_v ? $sub_r[2] : $sub_v; + $vals = preg_split('/ /', $val); + + return [['k' => $r[1], 'value' => $val, 'values' => $vals], $sub_v]; + } + + return [0, $v]; } - return 0; - } - - /* */ - - function xClose($v) { - if ($r = $this->x('\<\/([^\s\>]+)\>', $v)) { - return array(array('tag' => strtolower($r[1])), $r[2]); + + public function isEmpty($t, $v) + { + if (preg_match('/^(area|base|br|col|frame|hr|input|img|link|xmeta|param)$/', $t)) { + return 1; + } + if (preg_match('/\/$/', $v)) { + return 1; + } + + return 0; } - return array(0, $v); - } - - /* */ - - function xCData($v) { - if (preg_match('/(script|style)/i', $this->cur_tag)) { - if ($r = $this->x('(.+)(\<\/' . $this->cur_tag . '\>)', $v, 'Uis')) { - return array($r[1], $r[2] . $r[3]); - } + + public function xClose($v) + { + if ($r = $this->x('\<\/([^\s\>]+)\>', $v)) { + return [['tag' => strtolower($r[1])], $r[2]]; + } + + return [0, $v]; } - elseif ($r = $this->x('([^\<]+)', $v, 'si', $this->keep_cdata_ws)) { - return array($r[1], $r[2]); + + public function xCData($v) + { + if (preg_match('/(script|style)/i', $this->cur_tag)) { + if ($r = $this->x('(.+)(\<\/'.$this->cur_tag.'\>)', $v, 'Uis')) { + return [$r[1], $r[2].$r[3]]; + } + } elseif ($r = $this->x('([^\<]+)', $v, 'si', $this->keep_cdata_ws)) { + return [$r[1], $r[2]]; + } + + return [0, $v]; } - return array(0, $v); - } - - /* */ - - function extractRDF($formats = '') { - $this->node_index = $this->getNodeIndex(); - $formats = !$formats ? $this->v('sem_html_formats', $this->default_sem_html_formats, $this->a) : $formats; - $formats = preg_split('/ /', $formats); - foreach ($formats as $format) { - if (!in_array($format, $this->extracted_formats)) { - $comp = $this->camelCase($format) . 'Extractor'; - if (ARC2::inc($comp)) { - $cls = 'ARC2_' . $comp; - $e = new $cls($this->a, $this); - $e->extractRDF(); + + public function extractRDF($formats = '') + { + $this->node_index = $this->getNodeIndex(); + $formats = !$formats ? $this->v('sem_html_formats', $this->default_sem_html_formats, $this->a) : $formats; + $formats = preg_split('/ /', $formats); + foreach ($formats as $format) { + if (!in_array($format, $this->extracted_formats)) { + $comp = $this->camelCase($format).'Extractor'; + if (ARC2::inc($comp)) { + $cls = 'ARC2_'.$comp; + $e = new $cls($this->a, $this); + $e->extractRDF(); + } + $this->extracted_formats[] = $format; + } } - $this->extracted_formats[] = $format; - } } - } - - function getNode($id) { - return isset($this->nodes[$id]) ? $this->nodes[$id] : 0; - } - - /* */ - -} \ No newline at end of file + + public function getNode($id) + { + return isset($this->nodes[$id]) ? $this->nodes[$id] : 0; + } +} diff --git a/parsers/ARC2_TurtleParser.php b/parsers/ARC2_TurtleParser.php index 55a45415..9e6038fe 100644 --- a/parsers/ARC2_TurtleParser.php +++ b/parsers/ARC2_TurtleParser.php @@ -1,886 +1,947 @@ - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('RDFParser'); -class ARC2_TurtleParser extends ARC2_RDFParser { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() {/* reader */ - parent::__init(); - $this->state = 0; - $this->xml = 'http://www.w3.org/XML/1998/namespace'; - $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $this->xsd = 'http://www.w3.org/2001/XMLSchema#'; - $this->nsp = array($this->xml => 'xml', $this->rdf => 'rdf', $this->xsd => 'xsd'); - $this->unparsed_code = ''; - $this->max_parsing_loops = $this->v('turtle_max_parsing_loops', 500, $this->a); - } - - /* */ - - function x($re, $v, $options = 'si') { - $v = preg_replace('/^[\xA0\xC2]+/', ' ', $v); - while (preg_match('/^\s*(\#[^\xd\xa]*)(.*)$/si', $v, $m)) {/* comment removal */ - $v = $m[2]; - } - return ARC2::x($re, $v, $options); - //$this->unparsed_code = ($sub_r && count($sub_r)) ? $sub_r[count($sub_r) - 1] : ''; - } - - function createBnodeID(){ - $this->bnode_id++; - return '_:' . $this->bnode_prefix . $this->bnode_id; - } - - /* */ - - function addT($t) { - if ($this->skip_dupes) { - $h = md5(serialize($t)); - if (!isset($this->added_triples[$h])) { - $this->triples[$this->t_count] = $t; - $this->t_count++; - $this->added_triples[$h] = true; - } - } - else { - $this->triples[$this->t_count] = $t; - $this->t_count++; - } - } - - /* */ - - function getTriples() { - return $this->v('triples', array()); - } - - function countTriples() { - return $this->t_count; - } - - /* */ - - function getUnparsedCode() { - return $this->v('unparsed_code', ''); - } - - /* */ - - function setDefaultPrefixes() { - $this->prefixes = array( - 'rdf:' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', - 'rdfs:' => 'http://www.w3.org/2000/01/rdf-schema#', - 'owl:' => 'http://www.w3.org/2002/07/owl#', - 'xsd:' => 'http://www.w3.org/2001/XMLSchema#', - ); - if ($ns = $this->v('ns', array(), $this->a)) { - foreach ($ns as $p => $u) $this->prefixes[$p . ':'] = $u; - } - } - - - function parse($path, $data = '', $iso_fallback = false) { - $this->setDefaultPrefixes(); - /* reader */ - if (!$this->v('reader')) { - ARC2::inc('Reader'); - $this->reader = new ARC2_Reader($this->a, $this); - } - $this->reader->setAcceptHeader('Accept: application/x-turtle; q=0.9, */*; q=0.1'); - $this->reader->activate($path, $data); - $this->base = $this->v1('base', $this->reader->base, $this->a); - $this->r = array('vars' => array()); - /* parse */ - $buffer = ''; - $more_triples = array(); - $sub_v = ''; - $sub_v2 = ''; - $loops = 0; - $prologue_done = 0; - while ($d = $this->reader->readStream(0, 8192)) { - $buffer .= $d; - $sub_v = $buffer; - do { - $proceed = 0; - if (!$prologue_done) { - $proceed = 1; - if ((list($sub_r, $sub_v) = $this->xPrologue($sub_v)) && $sub_r) { - $loops = 0; - $sub_v .= $this->reader->readStream(0, 128); - /* we might have missed the final DOT in the previous prologue loop */ - if ($sub_r = $this->x('\.', $sub_v)) $sub_v = $sub_r[1]; - if ($this->x("\@?(base|prefix)", $sub_v)) {/* more prologue to come, use outer loop */ - $proceed = 0; +class ARC2_TurtleParser extends ARC2_RDFParser +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } + + public function __init() + {/* reader */ + parent::__init(); + $this->state = 0; + $this->xml = 'http://www.w3.org/XML/1998/namespace'; + $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $this->xsd = 'http://www.w3.org/2001/XMLSchema#'; + $this->nsp = [$this->xml => 'xml', $this->rdf => 'rdf', $this->xsd => 'xsd']; + $this->unparsed_code = ''; + $this->max_parsing_loops = $this->v('turtle_max_parsing_loops', 500, $this->a); + } + + public function x($re, $v, $options = 'si') + { + $v = preg_replace('/^[\xA0\xC2]+/', ' ', $v); + while (preg_match('/^\s*(\#[^\xd\xa]*)(.*)$/si', $v, $m)) {/* comment removal */ + $v = $m[2]; + } + + return ARC2::x($re, $v, $options); + //$this->unparsed_code = ($sub_r && count($sub_r)) ? $sub_r[count($sub_r) - 1] : ''; + } + + public function createBnodeID() + { + ++$this->bnode_id; + + return '_:'.$this->bnode_prefix.$this->bnode_id; + } + + public function addT($t) + { + if ($this->skip_dupes) { + $h = md5(serialize($t)); + if (!isset($this->added_triples[$h])) { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + $this->added_triples[$h] = true; } - } - else { - $prologue_done = 1; - } - } - if ($prologue_done && (list($sub_r, $sub_v, $more_triples, $sub_v2) = $this->xTriplesBlock($sub_v)) && is_array($sub_r)) { - $proceed = 1; - $loops = 0; - foreach ($sub_r as $t) { + } else { + $this->triples[$this->t_count] = $t; + ++$this->t_count; + } + } + + public function getTriples() + { + return $this->v('triples', []); + } + + public function countTriples() + { + return $this->t_count; + } + + public function getUnparsedCode() + { + return $this->v('unparsed_code', ''); + } + + public function setDefaultPrefixes() + { + $this->prefixes = [ + 'rdf:' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', + 'rdfs:' => 'http://www.w3.org/2000/01/rdf-schema#', + 'owl:' => 'http://www.w3.org/2002/07/owl#', + 'xsd:' => 'http://www.w3.org/2001/XMLSchema#', + ]; + if ($ns = $this->v('ns', [], $this->a)) { + foreach ($ns as $p => $u) { + $this->prefixes[$p.':'] = $u; + } + } + } + + public function parse($path, $data = '', $iso_fallback = false) + { + $this->setDefaultPrefixes(); + /* reader */ + if (!$this->v('reader')) { + ARC2::inc('Reader'); + $this->reader = new ARC2_Reader($this->a, $this); + } + $this->reader->setAcceptHeader('Accept: application/x-turtle; q=0.9, */*; q=0.1'); + $this->reader->activate($path, $data); + $this->base = $this->v1('base', $this->reader->base, $this->a); + $this->r = ['vars' => []]; + /* parse */ + $buffer = ''; + $more_triples = []; + $sub_v = ''; + $sub_v2 = ''; + $loops = 0; + $prologue_done = 0; + while ($d = $this->reader->readStream(0, 8192)) { + $buffer .= $d; + $sub_v = $buffer; + do { + $proceed = 0; + if (!$prologue_done) { + $proceed = 1; + if ((list($sub_r, $sub_v) = $this->xPrologue($sub_v)) && $sub_r) { + $loops = 0; + $sub_v .= $this->reader->readStream(0, 128); + /* we might have missed the final DOT in the previous prologue loop */ + if ($sub_r = $this->x('\.', $sub_v)) { + $sub_v = $sub_r[1]; + } + if ($this->x("\@?(base|prefix)", $sub_v)) {/* more prologue to come, use outer loop */ + $proceed = 0; + } + } else { + $prologue_done = 1; + } + } + if ($prologue_done && (list($sub_r, $sub_v, $more_triples, $sub_v2) = $this->xTriplesBlock($sub_v)) && is_array($sub_r)) { + $proceed = 1; + $loops = 0; + foreach ($sub_r as $t) { + $this->addT($t); + } + } + } while ($proceed); + ++$loops; + $buffer = $sub_v; + if ($loops > $this->max_parsing_loops) {/* most probably a parser or code bug, might also be a huge object value, though */ + $this->addError('too many loops: '.$loops.'. Could not parse "'.substr($buffer, 0, 200).'..."'); + break; + } + } + foreach ($more_triples as $t) { $this->addT($t); - } - } - } while ($proceed); - $loops++; - $buffer = $sub_v; - if ($loops > $this->max_parsing_loops) {/* most probably a parser or code bug, might also be a huge object value, though */ - $this->addError('too many loops: ' . $loops . '. Could not parse "' . substr($buffer, 0, 200) . '..."'); - break; - } - } - foreach ($more_triples as $t) { - $this->addT($t); - } - $sub_v = count($more_triples) ? $sub_v2 : $sub_v; - $buffer = $sub_v; - $this->unparsed_code = $buffer; - $this->reader->closeStream(); - unset($this->reader); - /* remove trailing comments */ - while (preg_match('/^\s*(\#[^\xd\xa]*)(.*)$/si', $this->unparsed_code, $m)) $this->unparsed_code = $m[2]; - if ($this->unparsed_code && !$this->getErrors()) { - $rest = preg_replace('/[\x0a|\x0d]/i', ' ', substr($this->unparsed_code, 0, 30)); - if (trim($rest)) $this->addError('Could not parse "' . $rest . '"'); - } - return $this->done(); - } - - function xPrologue($v) { - $r = 0; - if (!$this->t_count) { - if ((list($sub_r, $v) = $this->xBaseDecl($v)) && $sub_r) { - $this->base = $sub_r; - $r = 1; - } - while ((list($sub_r, $v) = $this->xPrefixDecl($v)) && $sub_r) { - $this->prefixes[$sub_r['prefix']] = $sub_r['uri']; - $r = 1; - } - } - return array($r, $v); - } - - /* 3 */ - - function xBaseDecl($v) { - if ($r = $this->x("\@?base\s+", $v)) { - if ((list($r, $sub_v) = $this->xIRI_REF($r[1])) && $r) { - if ($sub_r = $this->x('\.', $sub_v)) { - $sub_v = $sub_r[1]; - } - return array($r, $sub_v); - } - } - return array(0, $v); - } - - /* 4 */ - - function xPrefixDecl($v) { - if ($r = $this->x("\@?prefix\s+", $v)) { - if ((list($r, $sub_v) = $this->xPNAME_NS($r[1])) && $r) { - $prefix = $r; - if((list($r, $sub_v) = $this->xIRI_REF($sub_v)) && $r) { - $uri = $this->calcURI($r, $this->base); - if ($sub_r = $this->x('\.', $sub_v)) { - $sub_v = $sub_r[1]; - } - return array(array('prefix' => $prefix, 'uri_ref' => $r, 'uri' => $uri), $sub_v); - } - } - } - return array(0, $v); - } - - /* 21.., 32.. */ - - function xTriplesBlock($v) { - $pre_r = array(); - $r = array(); - $state = 1; - $sub_v = $v; - $buffer = $sub_v; - do { - $proceed = 0; - if ($state == 1) {/* expecting subject */ - $t = array('type' => 'triple', 's' => '', 'p' => '', 'o' => '', 's_type' => '', 'p_type' => '', 'o_type' => '', 'o_datatype' => '', 'o_lang' => ''); - if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { - $t['s'] = $sub_r['value']; - $t['s_type'] = $sub_r['type']; - $state = 2; - $proceed = 1; - if ($sub_r = $this->x('(\}|\.)', $sub_v)) { - if ($t['s_type'] == 'placeholder') { - $state = 4; + } + $sub_v = count($more_triples) ? $sub_v2 : $sub_v; + $buffer = $sub_v; + $this->unparsed_code = $buffer; + $this->reader->closeStream(); + unset($this->reader); + /* remove trailing comments */ + while (preg_match('/^\s*(\#[^\xd\xa]*)(.*)$/si', $this->unparsed_code, $m)) { + $this->unparsed_code = $m[2]; + } + if ($this->unparsed_code && !$this->getErrors()) { + $rest = preg_replace('/[\x0a|\x0d]/i', ' ', substr($this->unparsed_code, 0, 30)); + if (trim($rest)) { + $this->addError('Could not parse "'.$rest.'"'); + } + } + + return $this->done(); + } + + public function xPrologue($v) + { + $r = 0; + if (!$this->t_count) { + if ((list($sub_r, $v) = $this->xBaseDecl($v)) && $sub_r) { + $this->base = $sub_r; + $r = 1; } - else { - $this->addError('"' . $sub_r[1]. '" after subject found.'); + while ((list($sub_r, $v) = $this->xPrefixDecl($v)) && $sub_r) { + $this->prefixes[$sub_r['prefix']] = $sub_r['uri']; + $r = 1; } - } - } - elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) { - $t['s'] = $sub_r['id']; - $t['s_type'] = $sub_r['type']; - $pre_r = array_merge($pre_r, $sub_r['triples']); - $state = 2; - $proceed = 1; - if ($sub_r = $this->x('\.', $sub_v)) { - $this->addError('DOT after subject found.'); - } - } - elseif ((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) { - $t['s'] = $sub_r['id']; - $t['s_type'] = $sub_r['type']; - $pre_r = array_merge($pre_r, $sub_r['triples']); - $state = 2; - $proceed = 1; - } - elseif ($sub_r = $this->x('\.', $sub_v)) { - $this->addError('Subject expected, DOT found.' . $sub_v); - } - } - if ($state == 2) {/* expecting predicate */ - if ($sub_r = $this->x('a\s+', $sub_v)) { - $sub_v = $sub_r[1]; - $t['p'] = $this->rdf . 'type'; - $t['p_type'] = 'uri'; - $state = 3; - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { - if ($sub_r['type'] == 'bnode') { - $this->addError('Blank node used as triple predicate'); - } - $t['p'] = $sub_r['value']; - $t['p_type'] = $sub_r['type']; - $state = 3; - $proceed = 1; - } - elseif ($sub_r = $this->x('\.', $sub_v)) { - $state = 4; - } - elseif ($sub_r = $this->x('\}', $sub_v)) { - $buffer = $sub_v; - $r = array_merge($r, $pre_r); - $pre_r = array(); - $proceed = 0; - } - } - if ($state == 3) {/* expecting object */ - if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { - $t['o'] = $sub_r['value']; - $t['o_type'] = $sub_r['type']; - $t['o_lang'] = $this->v('lang', '', $sub_r); - $t['o_datatype'] = $this->v('datatype', '', $sub_r); - $pre_r[] = $t; - $state = 4; - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) { - $t['o'] = $sub_r['id']; - $t['o_type'] = $sub_r['type']; - $t['o_datatype'] = ''; - $pre_r = array_merge($pre_r, array($t), $sub_r['triples']); - $state = 4; - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) { - $t['o'] = $sub_r['id']; - $t['o_type'] = $sub_r['type']; - $t['o_datatype'] = ''; - $pre_r = array_merge($pre_r, array($t), $sub_r['triples']); - $state = 4; - $proceed = 1; - } - } - if ($state == 4) {/* expecting . or ; or , or } */ - if ($sub_r = $this->x('\.', $sub_v)) { - $sub_v = $sub_r[1]; - $buffer = $sub_v; - $r = array_merge($r, $pre_r); - $pre_r = array(); - $state = 1; - $proceed = 1; - } - elseif ($sub_r = $this->x('\;', $sub_v)) { - $sub_v = $sub_r[1]; - $state = 2; - $proceed = 1; - } - elseif ($sub_r = $this->x('\,', $sub_v)) { - $sub_v = $sub_r[1]; - $state = 3; - $proceed = 1; - if ($sub_r = $this->x('\}', $sub_v)) { - $this->addError('Object expected, } found.'); - } - } - if ($sub_r = $this->x('(\}|\{|OPTIONAL|FILTER|GRAPH)', $sub_v)) { - $buffer = $sub_v; - $r = array_merge($r, $pre_r); - $pre_r = array(); - $proceed = 0; - } - } - } while ($proceed); - return count($r) ? array($r, $buffer, $pre_r, $sub_v) : array(0, $buffer, $pre_r, $sub_v); - } - - /* 39.. */ - - function xBlankNodePropertyList($v) { - if ($sub_r = $this->x('\[', $v)) { - $sub_v = $sub_r[1]; - $s = $this->createBnodeID(); - $r = array('id' => $s, 'type' => 'bnode', 'triples' => array()); - $t = array('type' => 'triple', 's' => $s, 'p' => '', 'o' => '', 's_type' => 'bnode', 'p_type' => '', 'o_type' => '', 'o_datatype' => '', 'o_lang' => ''); - $state = 2; - $closed = 0; - do { - $proceed = 0; - if ($state == 2) {/* expecting predicate */ - if ($sub_r = $this->x('a\s+', $sub_v)) { - $sub_v = $sub_r[1]; - $t['p'] = $this->rdf . 'type'; - $t['p_type'] = 'uri'; - $state = 3; - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { - $t['p'] = $sub_r['value']; - $t['p_type'] = $sub_r['type']; - $state = 3; - $proceed = 1; - } - } - if ($state == 3) {/* expecting object */ - if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { - $t['o'] = $sub_r['value']; - $t['o_type'] = $sub_r['type']; - $t['o_lang'] = $this->v('lang', '', $sub_r); - $t['o_datatype'] = $this->v('datatype', '', $sub_r); - $r['triples'][] = $t; - $state = 4; - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) { - $t['o'] = $sub_r['id']; - $t['o_type'] = $sub_r['type']; - $t['o_datatype'] = ''; - $r['triples'] = array_merge($r['triples'], array($t), $sub_r['triples']); - $state = 4; - $proceed = 1; - } - elseif((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) { - $t['o'] = $sub_r['id']; - $t['o_type'] = $sub_r['type']; - $t['o_datatype'] = ''; - $r['triples'] = array_merge($r['triples'], array($t), $sub_r['triples']); - $state = 4; - $proceed = 1; - } - } - if ($state == 4) {/* expecting . or ; or , or ] */ - if ($sub_r = $this->x('\.', $sub_v)) { - $sub_v = $sub_r[1]; - $state = 1; - $proceed = 1; - } - if ($sub_r = $this->x('\;', $sub_v)) { + } + + return [$r, $v]; + } + + /* 3 */ + + public function xBaseDecl($v) + { + if ($r = $this->x("\@?base\s+", $v)) { + if ((list($r, $sub_v) = $this->xIRI_REF($r[1])) && $r) { + if ($sub_r = $this->x('\.', $sub_v)) { + $sub_v = $sub_r[1]; + } + + return [$r, $sub_v]; + } + } + + return [0, $v]; + } + + /* 4 */ + + public function xPrefixDecl($v) + { + if ($r = $this->x("\@?prefix\s+", $v)) { + if ((list($r, $sub_v) = $this->xPNAME_NS($r[1])) && $r) { + $prefix = $r; + if ((list($r, $sub_v) = $this->xIRI_REF($sub_v)) && $r) { + $uri = $this->calcURI($r, $this->base); + if ($sub_r = $this->x('\.', $sub_v)) { + $sub_v = $sub_r[1]; + } + + return [['prefix' => $prefix, 'uri_ref' => $r, 'uri' => $uri], $sub_v]; + } + } + } + + return [0, $v]; + } + + /* 21.., 32.. */ + + public function xTriplesBlock($v) + { + $pre_r = []; + $r = []; + $state = 1; + $sub_v = $v; + $buffer = $sub_v; + do { + $proceed = 0; + if (1 == $state) {/* expecting subject */ + $t = ['type' => 'triple', 's' => '', 'p' => '', 'o' => '', 's_type' => '', 'p_type' => '', 'o_type' => '', 'o_datatype' => '', 'o_lang' => '']; + if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { + $t['s'] = $sub_r['value']; + $t['s_type'] = $sub_r['type']; + $state = 2; + $proceed = 1; + if ($sub_r = $this->x('(\}|\.)', $sub_v)) { + if ('placeholder' == $t['s_type']) { + $state = 4; + } else { + $this->addError('"'.$sub_r[1].'" after subject found.'); + } + } + } elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) { + $t['s'] = $sub_r['id']; + $t['s_type'] = $sub_r['type']; + $pre_r = array_merge($pre_r, $sub_r['triples']); + $state = 2; + $proceed = 1; + if ($sub_r = $this->x('\.', $sub_v)) { + $this->addError('DOT after subject found.'); + } + } elseif ((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) { + $t['s'] = $sub_r['id']; + $t['s_type'] = $sub_r['type']; + $pre_r = array_merge($pre_r, $sub_r['triples']); + $state = 2; + $proceed = 1; + } elseif ($sub_r = $this->x('\.', $sub_v)) { + $this->addError('Subject expected, DOT found.'.$sub_v); + } + } + if (2 == $state) {/* expecting predicate */ + if ($sub_r = $this->x('a\s+', $sub_v)) { + $sub_v = $sub_r[1]; + $t['p'] = $this->rdf.'type'; + $t['p_type'] = 'uri'; + $state = 3; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { + if ('bnode' == $sub_r['type']) { + $this->addError('Blank node used as triple predicate'); + } + $t['p'] = $sub_r['value']; + $t['p_type'] = $sub_r['type']; + $state = 3; + $proceed = 1; + } elseif ($sub_r = $this->x('\.', $sub_v)) { + $state = 4; + } elseif ($sub_r = $this->x('\}', $sub_v)) { + $buffer = $sub_v; + $r = array_merge($r, $pre_r); + $pre_r = []; + $proceed = 0; + } + } + if (3 == $state) {/* expecting object */ + if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { + $t['o'] = $sub_r['value']; + $t['o_type'] = $sub_r['type']; + $t['o_lang'] = $this->v('lang', '', $sub_r); + $t['o_datatype'] = $this->v('datatype', '', $sub_r); + $pre_r[] = $t; + $state = 4; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) { + $t['o'] = $sub_r['id']; + $t['o_type'] = $sub_r['type']; + $t['o_datatype'] = ''; + $pre_r = array_merge($pre_r, [$t], $sub_r['triples']); + $state = 4; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) { + $t['o'] = $sub_r['id']; + $t['o_type'] = $sub_r['type']; + $t['o_datatype'] = ''; + $pre_r = array_merge($pre_r, [$t], $sub_r['triples']); + $state = 4; + $proceed = 1; + } + } + if (4 == $state) {/* expecting . or ; or , or } */ + if ($sub_r = $this->x('\.', $sub_v)) { + $sub_v = $sub_r[1]; + $buffer = $sub_v; + $r = array_merge($r, $pre_r); + $pre_r = []; + $state = 1; + $proceed = 1; + } elseif ($sub_r = $this->x('\;', $sub_v)) { + $sub_v = $sub_r[1]; + $state = 2; + $proceed = 1; + } elseif ($sub_r = $this->x('\,', $sub_v)) { + $sub_v = $sub_r[1]; + $state = 3; + $proceed = 1; + if ($sub_r = $this->x('\}', $sub_v)) { + $this->addError('Object expected, } found.'); + } + } + if ($sub_r = $this->x('(\}|\{|OPTIONAL|FILTER|GRAPH)', $sub_v)) { + $buffer = $sub_v; + $r = array_merge($r, $pre_r); + $pre_r = []; + $proceed = 0; + } + } + } while ($proceed); + + return count($r) ? [$r, $buffer, $pre_r, $sub_v] : [0, $buffer, $pre_r, $sub_v]; + } + + /* 39.. */ + + public function xBlankNodePropertyList($v) + { + if ($sub_r = $this->x('\[', $v)) { $sub_v = $sub_r[1]; + $s = $this->createBnodeID(); + $r = ['id' => $s, 'type' => 'bnode', 'triples' => []]; + $t = ['type' => 'triple', 's' => $s, 'p' => '', 'o' => '', 's_type' => 'bnode', 'p_type' => '', 'o_type' => '', 'o_datatype' => '', 'o_lang' => '']; $state = 2; - $proceed = 1; - } - if ($sub_r = $this->x('\,', $sub_v)) { - $sub_v = $sub_r[1]; - $state = 3; - $proceed = 1; - } - if ($sub_r = $this->x('\]', $sub_v)) { + $closed = 0; + do { + $proceed = 0; + if (2 == $state) {/* expecting predicate */ + if ($sub_r = $this->x('a\s+', $sub_v)) { + $sub_v = $sub_r[1]; + $t['p'] = $this->rdf.'type'; + $t['p_type'] = 'uri'; + $state = 3; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { + $t['p'] = $sub_r['value']; + $t['p_type'] = $sub_r['type']; + $state = 3; + $proceed = 1; + } + } + if (3 == $state) {/* expecting object */ + if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { + $t['o'] = $sub_r['value']; + $t['o_type'] = $sub_r['type']; + $t['o_lang'] = $this->v('lang', '', $sub_r); + $t['o_datatype'] = $this->v('datatype', '', $sub_r); + $r['triples'][] = $t; + $state = 4; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) { + $t['o'] = $sub_r['id']; + $t['o_type'] = $sub_r['type']; + $t['o_datatype'] = ''; + $r['triples'] = array_merge($r['triples'], [$t], $sub_r['triples']); + $state = 4; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) { + $t['o'] = $sub_r['id']; + $t['o_type'] = $sub_r['type']; + $t['o_datatype'] = ''; + $r['triples'] = array_merge($r['triples'], [$t], $sub_r['triples']); + $state = 4; + $proceed = 1; + } + } + if (4 == $state) {/* expecting . or ; or , or ] */ + if ($sub_r = $this->x('\.', $sub_v)) { + $sub_v = $sub_r[1]; + $state = 1; + $proceed = 1; + } + if ($sub_r = $this->x('\;', $sub_v)) { + $sub_v = $sub_r[1]; + $state = 2; + $proceed = 1; + } + if ($sub_r = $this->x('\,', $sub_v)) { + $sub_v = $sub_r[1]; + $state = 3; + $proceed = 1; + } + if ($sub_r = $this->x('\]', $sub_v)) { + $sub_v = $sub_r[1]; + $proceed = 0; + $closed = 1; + } + } + } while ($proceed); + if ($closed) { + return [$r, $sub_v]; + } + + return [0, $v]; + } + + return [0, $v]; + } + + /* 40.. */ + + public function xCollection($v) + { + if ($sub_r = $this->x('\(', $v)) { $sub_v = $sub_r[1]; + $s = $this->createBnodeID(); + $r = ['id' => $s, 'type' => 'bnode', 'triples' => []]; + $closed = 0; + do { + $proceed = 0; + if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { + $r['triples'][] = ['type' => 'triple', 's' => $s, 'p' => $this->rdf.'first', 'o' => $sub_r['value'], 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => $sub_r['type'], 'o_lang' => $this->v('lang', '', $sub_r), 'o_datatype' => $this->v('datatype', '', $sub_r)]; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) { + $r['triples'][] = ['type' => 'triple', 's' => $s, 'p' => $this->rdf.'first', 'o' => $sub_r['id'], 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => $sub_r['type'], 'o_lang' => '', 'o_datatype' => '']; + $r['triples'] = array_merge($r['triples'], $sub_r['triples']); + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) { + $r['triples'][] = ['type' => 'triple', 's' => $s, 'p' => $this->rdf.'first', 'o' => $sub_r['id'], 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => $sub_r['type'], 'o_lang' => '', 'o_datatype' => '']; + $r['triples'] = array_merge($r['triples'], $sub_r['triples']); + $proceed = 1; + } + if ($proceed) { + if ($sub_r = $this->x('\)', $sub_v)) { + $sub_v = $sub_r[1]; + $r['triples'][] = ['type' => 'triple', 's' => $s, 'p' => $this->rdf.'rest', 'o' => $this->rdf.'nil', 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => 'uri', 'o_lang' => '', 'o_datatype' => '']; + $closed = 1; + $proceed = 0; + } else { + $next_s = $this->createBnodeID(); + $r['triples'][] = ['type' => 'triple', 's' => $s, 'p' => $this->rdf.'rest', 'o' => $next_s, 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => 'bnode', 'o_lang' => '', 'o_datatype' => '']; + $s = $next_s; + } + } + } while ($proceed); + if ($closed) { + return [$r, $sub_v]; + } + } + + return [0, $v]; + } + + /* 42 */ + + public function xVarOrTerm($v) + { + if ((list($sub_r, $sub_v) = $this->xVar($v)) && $sub_r) { + return [$sub_r, $sub_v]; + } elseif ((list($sub_r, $sub_v) = $this->xGraphTerm($v)) && $sub_r) { + return [$sub_r, $sub_v]; + } + + return [0, $v]; + } + + /* 44, 74.., 75.. */ + + public function xVar($v) + { + if ($r = $this->x('(\?|\$)([^\s]+)', $v)) { + if ((list($sub_r, $sub_v) = $this->xVARNAME($r[2])) && $sub_r) { + if (!in_array($sub_r, $this->r['vars'])) { + $this->r['vars'][] = $sub_r; + } + + return [['value' => $sub_r, 'type' => 'var'], $sub_v.$r[3]]; + } + } + + return [0, $v]; + } + + /* 45 */ + + public function xGraphTerm($v) + { + foreach ([ + 'IRIref' => 'uri', + 'RDFLiteral' => 'literal', + 'NumericLiteral' => 'literal', + 'BooleanLiteral' => 'literal', + 'BlankNode' => 'bnode', + 'NIL' => 'uri', + 'Placeholder' => 'placeholder', + ] as $term => $type) { + $m = 'x'.$term; + if ((list($sub_r, $sub_v) = $this->$m($v)) && $sub_r) { + if (!is_array($sub_r)) { + $sub_r = ['value' => $sub_r]; + } + $sub_r['type'] = $this->v1('type', $type, $sub_r); + + return [$sub_r, $sub_v]; + } + } + + return [0, $v]; + } + + /* 60 */ + + public function xRDFLiteral($v) + { + if ((list($sub_r, $sub_v) = $this->xString($v)) && $sub_r) { + $sub_r['value'] = $this->unescapeNtripleUTF($sub_r['value']); + $r = $sub_r; + if ((list($sub_r, $sub_v) = $this->xLANGTAG($sub_v)) && $sub_r) { + $r['lang'] = $sub_r; + } elseif (!$this->x('\s', $sub_v) && ($sub_r = $this->x('\^\^', $sub_v)) && (list($sub_r, $sub_v) = $this->xIRIref($sub_r[1])) && $sub_r[1]) { + $r['datatype'] = $sub_r; + } + + return [$r, $sub_v]; + } + + return [0, $v]; + } + + /* 61.., 62.., 63.., 64.. */ + + public function xNumericLiteral($v) + { + $sub_r = $this->x('(\-|\+)?', $v); + $prefix = $sub_r[1]; + $sub_v = $sub_r[2]; + foreach (['DOUBLE' => 'double', 'DECIMAL' => 'decimal', 'INTEGER' => 'integer'] as $type => $xsd) { + $m = 'x'.$type; + if ((list($sub_r, $sub_v) = $this->$m($sub_v)) && (false !== $sub_r)) { + $r = ['value' => $prefix.$sub_r, 'type' => 'literal', 'datatype' => $this->xsd.$xsd]; + + return [$r, $sub_v]; + } + } + + return [0, $v]; + } + + /* 65.. */ + + public function xBooleanLiteral($v) + { + if ($r = $this->x('(true|false)', $v)) { + return [$r[1], $r[2]]; + } + + return [0, $v]; + } + + /* 66.., 87.., 88.., 89.., 90.., 91.. */ + + public function xString($v) + {/* largely simplified, may need some tweaks in following revisions */ + $sub_v = $v; + if (!preg_match('/^\s*([\']{3}|\'|[\"]{3}|\")(.*)$/s', $sub_v, $m)) { + return [0, $v]; + } + $delim = $m[1]; + $rest = $m[2]; + $sub_types = ["'''" => 'literal_long1', '"""' => 'literal_long2', "'" => 'literal1', '"' => 'literal2']; + $sub_type = $sub_types[$delim]; + $pos = 0; + $r = false; + do { $proceed = 0; - $closed = 1; - } - } - } while ($proceed); - if ($closed) { - return array($r, $sub_v); - } - return array(0, $v); - } - return array(0, $v); - } - - /* 40.. */ - - function xCollection($v) { - if ($sub_r = $this->x('\(', $v)) { - $sub_v = $sub_r[1]; - $s = $this->createBnodeID(); - $r = array('id' => $s, 'type' => 'bnode', 'triples' => array()); - $closed = 0; - do { - $proceed = 0; - if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) { - $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'first', 'o' => $sub_r['value'], 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => $sub_r['type'], 'o_lang' => $this->v('lang', '', $sub_r), 'o_datatype' => $this->v('datatype', '', $sub_r)); - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) { - $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'first', 'o' => $sub_r['id'], 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => $sub_r['type'], 'o_lang' => '', 'o_datatype' => ''); - $r['triples'] = array_merge($r['triples'], $sub_r['triples']); - $proceed = 1; - } - elseif((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) { - $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'first', 'o' => $sub_r['id'], 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => $sub_r['type'], 'o_lang' => '', 'o_datatype' => ''); - $r['triples'] = array_merge($r['triples'], $sub_r['triples']); - $proceed = 1; - } - if ($proceed) { - if ($sub_r = $this->x('\)', $sub_v)) { - $sub_v = $sub_r[1]; - $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'rest', 'o' => $this->rdf . 'nil', 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => 'uri', 'o_lang' => '', 'o_datatype' => ''); - $closed = 1; + $delim_pos = strpos($rest, $delim, $pos); + if (false === $delim_pos) { + break; + } + $new_rest = substr($rest, $delim_pos + strlen($delim)); + $r = substr($rest, 0, $delim_pos); + if (!preg_match('/([\x5c]+)$/s', $r, $m) || !(strlen($m[1]) % 2)) { + $rest = $new_rest; + } else { + $r = false; + $pos = $delim_pos + 1; + $proceed = 1; + } + } while ($proceed); + if (false !== $r) { + return [['value' => $this->toUTF8($r), 'type' => 'literal', 'sub_type' => $sub_type], $rest]; + } + + return [0, $v]; + } + + /* 67 */ + + public function xIRIref($v) + { + if ((list($r, $v) = $this->xIRI_REF($v)) && $r) { + return [$this->calcURI($r, $this->base), $v]; + } elseif ((list($r, $v) = $this->xPrefixedName($v)) && $r) { + return [$r, $v]; + } + + return [0, $v]; + } + + /* 68 */ + + public function xPrefixedName($v) + { + if ((list($r, $v) = $this->xPNAME_LN($v)) && $r) { + return [$r, $v]; + } elseif ((list($r, $sub_v) = $this->xPNAME_NS($v)) && $r) { + return isset($this->prefixes[$r]) ? [$this->prefixes[$r], $sub_v] : [0, $v]; + } + + return [0, $v]; + } + + /* 69.., 73.., 93, 94.. */ + + public function xBlankNode($v) + { + if (($r = $this->x('\_\:', $v)) && (list($r, $sub_v) = $this->xPN_LOCAL($r[1])) && $r) { + return [['type' => 'bnode', 'value' => '_:'.$r], $sub_v]; + } + if ($r = $this->x('\[[\x20\x9\xd\xa]*\]', $v)) { + return [['type' => 'bnode', 'value' => $this->createBnodeID()], $r[1]]; + } + + return [0, $v]; + } + + /* 70.. @@sync with SPARQLParser */ + + public function xIRI_REF($v) + { + //if ($r = $this->x('\<([^\<\>\"\{\}\|\^\'[:space:]]*)\>', $v)) { + if (($r = $this->x('\<(\$\{[^\>]*\})\>', $v)) && ($sub_r = $this->xPlaceholder($r[1]))) { + return [$r[1], $r[2]]; + } elseif ($r = $this->x('\<\>', $v)) { + return [true, $r[1]]; + } elseif ($r = $this->x('\<([^\s][^\<\>]*)\>', $v)) { + return [$r[1] ? $r[1] : true, $r[2]]; + } + + return [0, $v]; + } + + /* 71 */ + + public function xPNAME_NS($v) + { + list($r, $sub_v) = $this->xPN_PREFIX($v); + $prefix = $r ? $r : ''; + + return ($r = $this->x("\:", $sub_v)) ? [$prefix.':', $r[1]] : [0, $v]; + } + + /* 72 */ + + public function xPNAME_LN($v) + { + if ((list($r, $sub_v) = $this->xPNAME_NS($v)) && $r) { + if (!$this->x('\s', $sub_v) && (list($sub_r, $sub_v) = $this->xPN_LOCAL($sub_v)) && $sub_r) { + if (!isset($this->prefixes[$r])) { + return [0, $v]; + } + + return [$this->prefixes[$r].$sub_r, $sub_v]; + } + } + + return [0, $v]; + } + + /* 76 */ + + public function xLANGTAG($v) + { + if (!$this->x('\s', $v) && ($r = $this->x('\@([a-z]+(\-[a-z0-9]+)*)', $v))) { + return [$r[1], $r[3]]; + } + + return [0, $v]; + } + + /* 77.. */ + + public function xINTEGER($v) + { + if ($r = $this->x('([0-9]+)', $v)) { + return [$r[1], $r[2]]; + } + + return [false, $v]; + } + + /* 78.. */ + + public function xDECIMAL($v) + { + if ($r = $this->x('([0-9]+\.[0-9]*)', $v)) { + return [$r[1], $r[2]]; + } + if ($r = $this->x('(\.[0-9]+)', $v)) { + return [$r[1], $r[2]]; + } + + return [false, $v]; + } + + /* 79.., 86.. */ + + public function xDOUBLE($v) + { + if ($r = $this->x('([0-9]+\.[0-9]*E[\+\-]?[0-9]+)', $v)) { + return [$r[1], $r[2]]; + } + if ($r = $this->x('(\.[0-9]+E[\+\-]?[0-9]+)', $v)) { + return [$r[1], $r[2]]; + } + if ($r = $this->x('([0-9]+E[\+\-]?[0-9]+)', $v)) { + return [$r[1], $r[2]]; + } + + return [false, $v]; + } + + /* 92 */ + + public function xNIL($v) + { + if ($r = $this->x('\([\x20\x9\xd\xa]*\)', $v)) { + return [['type' => 'uri', 'value' => $this->rdf.'nil'], $r[1]]; + } + + return [0, $v]; + } + + /* 95.. */ + + public function xPN_CHARS_BASE($v) + { + if ($r = $this->x("([a-z]+|\\\u[0-9a-f]{1,4})", $v)) { + return [$r[1], $r[2]]; + } + + return [0, $v]; + } + + /* 96 */ + + public function xPN_CHARS_U($v) + { + if ((list($r, $sub_v) = $this->xPN_CHARS_BASE($v)) && $r) { + return [$r, $sub_v]; + } elseif ($r = $this->x('(_)', $v)) { + return [$r[1], $r[2]]; + } + + return [0, $v]; + } + + /* 97.. */ + + public function xVARNAME($v) + { + $r = ''; + do { $proceed = 0; - } - else { - $next_s = $this->createBnodeID(); - $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'rest', 'o' => $next_s, 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => 'bnode', 'o_lang' => '', 'o_datatype' => ''); - $s = $next_s; - } - } - } while ($proceed); - if ($closed) { - return array($r, $sub_v); - } - } - return array (0, $v); - } - - /* 42 */ - - function xVarOrTerm($v) { - if ((list($sub_r, $sub_v) = $this->xVar($v)) && $sub_r) { - return array($sub_r, $sub_v); - } - elseif ((list($sub_r, $sub_v) = $this->xGraphTerm($v)) && $sub_r) { - return array($sub_r, $sub_v); - } - return array(0, $v); - } - - /* 44, 74.., 75.. */ - - function xVar($v) { - if ($r = $this->x('(\?|\$)([^\s]+)', $v)) { - if ((list($sub_r, $sub_v) = $this->xVARNAME($r[2])) && $sub_r) { - if (!in_array($sub_r, $this->r['vars'])) { - $this->r['vars'][] = $sub_r; - } - return array(array('value' => $sub_r, 'type' => 'var'), $sub_v . $r[3]); - } - } - return array(0, $v); - } - - /* 45 */ - - function xGraphTerm($v) { - foreach (array( - 'IRIref' => 'uri', - 'RDFLiteral' => 'literal', - 'NumericLiteral' => 'literal', - 'BooleanLiteral' => 'literal', - 'BlankNode' => 'bnode', - 'NIL' => 'uri', - 'Placeholder' => 'placeholder' - ) as $term => $type) { - $m = 'x' . $term; - if ((list($sub_r, $sub_v) = $this->$m($v)) && $sub_r) { - if (!is_array($sub_r)) { - $sub_r = array('value' => $sub_r); - } - $sub_r['type'] = $this->v1('type', $type, $sub_r); - return array($sub_r, $sub_v); - } - } - return array(0, $v); - } - - /* 60 */ - - function xRDFLiteral($v) { - if ((list($sub_r, $sub_v) = $this->xString($v)) && $sub_r) { - $sub_r['value'] = $this->unescapeNtripleUTF($sub_r['value']); - $r = $sub_r; - if ((list($sub_r, $sub_v) = $this->xLANGTAG($sub_v)) && $sub_r) { - $r['lang'] = $sub_r; - } - elseif (!$this->x('\s', $sub_v) && ($sub_r = $this->x('\^\^', $sub_v)) && (list($sub_r, $sub_v) = $this->xIRIref($sub_r[1])) && $sub_r[1]) { - $r['datatype'] = $sub_r; - } - return array($r, $sub_v); - } - return array(0, $v); - } - - /* 61.., 62.., 63.., 64.. */ - - function xNumericLiteral($v) { - $sub_r = $this->x('(\-|\+)?', $v); - $prefix = $sub_r[1]; - $sub_v = $sub_r[2]; - foreach (array('DOUBLE' => 'double', 'DECIMAL' => 'decimal', 'INTEGER' => 'integer') as $type => $xsd) { - $m = 'x' . $type; - if ((list($sub_r, $sub_v) = $this->$m($sub_v)) && ($sub_r !== false)) { - $r = array('value' => $prefix . $sub_r, 'type' => 'literal', 'datatype' => $this->xsd . $xsd); - return array($r, $sub_v); - } - } - return array(0, $v); - } - - /* 65.. */ - - function xBooleanLiteral($v) { - if ($r = $this->x('(true|false)', $v)) { - return array($r[1], $r[2]); - } - return array(0, $v); - } - - /* 66.., 87.., 88.., 89.., 90.., 91.. */ - - function xString($v) {/* largely simplified, may need some tweaks in following revisions */ - $sub_v = $v; - if (!preg_match('/^\s*([\']{3}|\'|[\"]{3}|\")(.*)$/s', $sub_v, $m)) return array(0, $v); - $delim = $m[1]; - $rest = $m[2]; - $sub_types = array("'''" => 'literal_long1', '"""' => 'literal_long2', "'" => 'literal1', '"' => 'literal2'); - $sub_type = $sub_types[$delim]; - $pos = 0; - $r = false; - do { - $proceed = 0; - $delim_pos = strpos($rest, $delim, $pos); - if ($delim_pos === false) break; - $new_rest = substr($rest, $delim_pos + strlen($delim)); - $r = substr($rest, 0, $delim_pos); - if (!preg_match('/([\x5c]+)$/s', $r, $m) || !(strlen($m[1]) % 2)) { - $rest = $new_rest; - } - else { - $r = false; - $pos = $delim_pos + 1; - $proceed = 1; - } - } while ($proceed); - if ($r !== false) { - return array(array('value' => $this->toUTF8($r) , 'type' => 'literal', 'sub_type' => $sub_type), $rest); - } - return array(0, $v); - } - - /* 67 */ - - function xIRIref($v) { - if ((list($r, $v) = $this->xIRI_REF($v)) && $r) { - return array($this->calcURI($r, $this->base), $v); - } - elseif ((list($r, $v) = $this->xPrefixedName($v)) && $r) { - return array($r, $v); - } - return array(0, $v); - } - - /* 68 */ - - function xPrefixedName($v) { - if ((list($r, $v) = $this->xPNAME_LN($v)) && $r) { - return array($r, $v); - } - elseif ((list($r, $sub_v) = $this->xPNAME_NS($v)) && $r) { - return isset($this->prefixes[$r]) ? array($this->prefixes[$r], $sub_v) : array(0, $v); - } - return array(0, $v); - } - - /* 69.., 73.., 93, 94.. */ - - function xBlankNode($v) { - if (($r = $this->x('\_\:', $v)) && (list($r, $sub_v) = $this->xPN_LOCAL($r[1])) && $r) { - return array(array('type' => 'bnode', 'value' => '_:' . $r), $sub_v); - } - if ($r = $this->x('\[[\x20\x9\xd\xa]*\]', $v)) { - return array(array('type' => 'bnode', 'value' => $this->createBnodeID()), $r[1]); - } - return array(0, $v); - } - - /* 70.. @@sync with SPARQLParser */ - - function xIRI_REF($v) { - //if ($r = $this->x('\<([^\<\>\"\{\}\|\^\'[:space:]]*)\>', $v)) { - if (($r = $this->x('\<(\$\{[^\>]*\})\>', $v)) && ($sub_r = $this->xPlaceholder($r[1]))) { - return array($r[1], $r[2]); - } - elseif ($r = $this->x('\<\>', $v)) { - return array(true, $r[1]); - } - elseif ($r = $this->x('\<([^\s][^\<\>]*)\>', $v)) { - return array($r[1] ? $r[1] : true, $r[2]); - } - return array(0, $v); - } - - /* 71 */ - - function xPNAME_NS($v) { - list($r, $sub_v) = $this->xPN_PREFIX($v); - $prefix = $r ? $r : ''; - return ($r = $this->x("\:", $sub_v)) ? array($prefix . ':', $r[1]) : array(0, $v); - } - - /* 72 */ - - function xPNAME_LN($v) { - if ((list($r, $sub_v) = $this->xPNAME_NS($v)) && $r) { - if (!$this->x('\s', $sub_v) && (list($sub_r, $sub_v) = $this->xPN_LOCAL($sub_v)) && $sub_r) { - if (!isset($this->prefixes[$r])) { - return array(0, $v); - } - return array($this->prefixes[$r] . $sub_r, $sub_v); - } - } - return array(0, $v); - } - - /* 76 */ - - function xLANGTAG($v) { - if (!$this->x('\s', $v) && ($r = $this->x('\@([a-z]+(\-[a-z0-9]+)*)', $v))) { - return array($r[1], $r[3]); - } - return array(0, $v); - } - - /* 77.. */ - - function xINTEGER($v) { - if ($r = $this->x('([0-9]+)', $v)) { - return array($r[1], $r[2]); - } - return array(false, $v); - } - - /* 78.. */ - - function xDECIMAL($v) { - if ($r = $this->x('([0-9]+\.[0-9]*)', $v)) { - return array($r[1], $r[2]); - } - if ($r = $this->x('(\.[0-9]+)', $v)) { - return array($r[1], $r[2]); - } - return array(false, $v); - } - - /* 79.., 86.. */ - - function xDOUBLE($v) { - if ($r = $this->x('([0-9]+\.[0-9]*E[\+\-]?[0-9]+)', $v)) { - return array($r[1], $r[2]); - } - if ($r = $this->x('(\.[0-9]+E[\+\-]?[0-9]+)', $v)) { - return array($r[1], $r[2]); - } - if ($r = $this->x('([0-9]+E[\+\-]?[0-9]+)', $v)) { - return array($r[1], $r[2]); - } - return array(false, $v); - } - - /* 92 */ - - function xNIL($v) { - if ($r = $this->x('\([\x20\x9\xd\xa]*\)', $v)) { - return array(array('type' => 'uri', 'value' => $this->rdf . 'nil'), $r[1]); - } - return array(0, $v); - } - - /* 95.. */ - - function xPN_CHARS_BASE($v) { - if ($r = $this->x("([a-z]+|\\\u[0-9a-f]{1,4})", $v)) { - return array($r[1], $r[2]); - } - return array(0, $v); - } - - /* 96 */ - - function xPN_CHARS_U($v) { - if ((list($r, $sub_v) = $this->xPN_CHARS_BASE($v)) && $r) { - return array($r, $sub_v); - } - elseif ($r = $this->x("(_)", $v)) { - return array($r[1], $r[2]); - } - return array(0, $v); - } - - /* 97.. */ - - function xVARNAME($v) { - $r = ''; - do { - $proceed = 0; - if ($sub_r = $this->x('([0-9]+)', $v)) { - $r .= $sub_r[1]; - $v = $sub_r[2]; - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xPN_CHARS_U($v)) && $sub_r) { - $r .= $sub_r; - $v = $sub_v; - $proceed = 1; - } - elseif ($r && ($sub_r = $this->x('([\xb7\x300-\x36f]+)', $v))) { - $r .= $sub_r[1]; - $v = $sub_r[2]; - $proceed = 1; - } - } while ($proceed); - return array($r, $v); - } - - /* 98.. */ - - function xPN_CHARS($v) { - if ((list($r, $sub_v) = $this->xPN_CHARS_U($v)) && $r) { - return array($r, $sub_v); - } - elseif ($r = $this->x('([\-0-9\xb7\x300-\x36f])', $v)) { - return array($r[1], $r[2]); - } - return array(false, $v); - } - - /* 99 */ - - function xPN_PREFIX($v) { - if ($sub_r = $this->x("([^\s\:\(\)\{\}\;\,]+)", $v, 's')) {/* accelerator */ - return array($sub_r[1], $sub_r[2]);/* @@testing */ - } - if ((list($r, $sub_v) = $this->xPN_CHARS_BASE($v)) && $r) { - do { - $proceed = 0; - list($sub_r, $sub_v) = $this->xPN_CHARS($sub_v); - if ($sub_r !== false) { - $r .= $sub_r; - $proceed = 1; - } - elseif ($sub_r = $this->x("\.", $sub_v)) { - $r .= '.'; - $sub_v = $sub_r[1]; - $proceed = 1; - } - } while ($proceed); - list($sub_r, $sub_v) = $this->xPN_CHARS($sub_v); - $r .= $sub_r ? $sub_r : ''; - } - return array($r, $sub_v); - } - - /* 100 */ - - function xPN_LOCAL($v) { - if (($sub_r = $this->x("([^\s\(\)\{\}\[\]\;\,\.]+)", $v, 's')) && !preg_match('/^\./', $sub_r[2])) {/* accelerator */ - return array($sub_r[1], $sub_r[2]);/* @@testing */ - } - $r = ''; - $sub_v = $v; - do { - $proceed = 0; - if ($this->x('\s', $sub_v)) { - return array($r, $sub_v); - } - if ($sub_r = $this->x('([0-9])', $sub_v)) { - $r .= $sub_r[1]; - $sub_v = $sub_r[2]; - $proceed = 1; - } - elseif ((list($sub_r, $sub_v) = $this->xPN_CHARS_U($sub_v)) && $sub_r) { - $r .= $sub_r; - $proceed = 1; - } - elseif ($r) { - if (($sub_r = $this->x('(\.)', $sub_v)) && !preg_match('/^[\s\}]/s', $sub_r[2])) { - $r .= $sub_r[1]; - $sub_v = $sub_r[2]; - } - if ((list($sub_r, $sub_v) = $this->xPN_CHARS($sub_v)) && $sub_r) { - $r .= $sub_r; - $proceed = 1; - } - } - } while ($proceed); - return array($r, $sub_v); - } - - /* */ - - function unescapeNtripleUTF($v) { - if (strpos($v, '\\') === false) return $v; - $mappings = array('t' => "\t", 'n' => "\n", 'r' => "\r", '\"' => '"', '\'' => "'"); - foreach ($mappings as $in => $out) { - $v = preg_replace('/\x5c([' . $in . '])/', $out, $v); - } - if (strpos(strtolower($v), '\u') === false) return $v; - while (preg_match('/\\\(U)([0-9A-F]{8})/', $v, $m) || preg_match('/\\\(u)([0-9A-F]{4})/', $v, $m)) { - $no = hexdec($m[2]); - if ($no < 128) $char = chr($no); - else if ($no < 2048) $char = chr(($no >> 6) + 192) . chr(($no & 63) + 128); - else if ($no < 65536) $char = chr(($no >> 12) + 224) . chr((($no >> 6) & 63) + 128) . chr(($no & 63) + 128); - else if ($no < 2097152) $char = chr(($no >> 18) + 240) . chr((($no >> 12) & 63) + 128) . chr((($no >> 6) & 63) + 128) . chr(($no & 63) + 128); - else $char= ''; - $v = str_replace('\\' . $m[1] . $m[2], $char, $v); - } - return $v; - } - - /* */ - - function xPlaceholder($v) { - //if ($r = $this->x('(\?|\$)\{([^\}]+)\}', $v)) { - if ($r = $this->x('(\?|\$)', $v)) { - if (preg_match('/(\{(?:[^{}]+|(?R))*\})/', $r[2], $m) && strpos(trim($r[2]), $m[1]) === 0) { - $ph = substr($m[1], 1, -1); - $rest = substr(trim($r[2]), strlen($m[1])); - if (!isset($this->r['placeholders'])) $this->r['placeholders'] = array(); - if (!in_array($ph, $this->r['placeholders'])) $this->r['placeholders'][] = $ph; - return array(array('value' => $ph, 'type' => 'placeholder'), $rest); - } - } - return array(0, $v); - } - - /* */ + if ($sub_r = $this->x('([0-9]+)', $v)) { + $r .= $sub_r[1]; + $v = $sub_r[2]; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xPN_CHARS_U($v)) && $sub_r) { + $r .= $sub_r; + $v = $sub_v; + $proceed = 1; + } elseif ($r && ($sub_r = $this->x('([\xb7\x300-\x36f]+)', $v))) { + $r .= $sub_r[1]; + $v = $sub_r[2]; + $proceed = 1; + } + } while ($proceed); + + return [$r, $v]; + } + + /* 98.. */ + + public function xPN_CHARS($v) + { + if ((list($r, $sub_v) = $this->xPN_CHARS_U($v)) && $r) { + return [$r, $sub_v]; + } elseif ($r = $this->x('([\-0-9\xb7\x300-\x36f])', $v)) { + return [$r[1], $r[2]]; + } + + return [false, $v]; + } + + /* 99 */ + + public function xPN_PREFIX($v) + { + if ($sub_r = $this->x("([^\s\:\(\)\{\}\;\,]+)", $v, 's')) {/* accelerator */ + return [$sub_r[1], $sub_r[2]]; /* @@testing */ + } + if ((list($r, $sub_v) = $this->xPN_CHARS_BASE($v)) && $r) { + do { + $proceed = 0; + list($sub_r, $sub_v) = $this->xPN_CHARS($sub_v); + if (false !== $sub_r) { + $r .= $sub_r; + $proceed = 1; + } elseif ($sub_r = $this->x("\.", $sub_v)) { + $r .= '.'; + $sub_v = $sub_r[1]; + $proceed = 1; + } + } while ($proceed); + list($sub_r, $sub_v) = $this->xPN_CHARS($sub_v); + $r .= $sub_r ? $sub_r : ''; + } + + return [$r, $sub_v]; + } + + /* 100 */ + + public function xPN_LOCAL($v) + { + if (($sub_r = $this->x("([^\s\(\)\{\}\[\]\;\,\.]+)", $v, 's')) && !preg_match('/^\./', $sub_r[2])) {/* accelerator */ + return [$sub_r[1], $sub_r[2]]; /* @@testing */ + } + $r = ''; + $sub_v = $v; + do { + $proceed = 0; + if ($this->x('\s', $sub_v)) { + return [$r, $sub_v]; + } + if ($sub_r = $this->x('([0-9])', $sub_v)) { + $r .= $sub_r[1]; + $sub_v = $sub_r[2]; + $proceed = 1; + } elseif ((list($sub_r, $sub_v) = $this->xPN_CHARS_U($sub_v)) && $sub_r) { + $r .= $sub_r; + $proceed = 1; + } elseif ($r) { + if (($sub_r = $this->x('(\.)', $sub_v)) && !preg_match('/^[\s\}]/s', $sub_r[2])) { + $r .= $sub_r[1]; + $sub_v = $sub_r[2]; + } + if ((list($sub_r, $sub_v) = $this->xPN_CHARS($sub_v)) && $sub_r) { + $r .= $sub_r; + $proceed = 1; + } + } + } while ($proceed); + + return [$r, $sub_v]; + } + + public function unescapeNtripleUTF($v) + { + if (false === strpos($v, '\\')) { + return $v; + } + $mappings = ['t' => "\t", 'n' => "\n", 'r' => "\r", '\"' => '"', '\'' => "'"]; + foreach ($mappings as $in => $out) { + $v = preg_replace('/\x5c(['.$in.'])/', $out, $v); + } + if (false === strpos(strtolower($v), '\u')) { + return $v; + } + while (preg_match('/\\\(U)([0-9A-F]{8})/', $v, $m) || preg_match('/\\\(u)([0-9A-F]{4})/', $v, $m)) { + $no = hexdec($m[2]); + if ($no < 128) { + $char = chr($no); + } elseif ($no < 2048) { + $char = chr(($no >> 6) + 192).chr(($no & 63) + 128); + } elseif ($no < 65536) { + $char = chr(($no >> 12) + 224).chr((($no >> 6) & 63) + 128).chr(($no & 63) + 128); + } elseif ($no < 2097152) { + $char = chr(($no >> 18) + 240).chr((($no >> 12) & 63) + 128).chr((($no >> 6) & 63) + 128).chr(($no & 63) + 128); + } else { + $char = ''; + } + $v = str_replace('\\'.$m[1].$m[2], $char, $v); + } + + return $v; + } + + public function xPlaceholder($v) + { + //if ($r = $this->x('(\?|\$)\{([^\}]+)\}', $v)) { + if ($r = $this->x('(\?|\$)', $v)) { + if (preg_match('/(\{(?:[^{}]+|(?R))*\})/', $r[2], $m) && 0 === strpos(trim($r[2]), $m[1])) { + $ph = substr($m[1], 1, -1); + $rest = substr(trim($r[2]), strlen($m[1])); + if (!isset($this->r['placeholders'])) { + $this->r['placeholders'] = []; + } + if (!in_array($ph, $this->r['placeholders'])) { + $this->r['placeholders'][] = $ph; + } + + return [['value' => $ph, 'type' => 'placeholder'], $rest]; + } + } + + return [0, $v]; + } } diff --git a/phpunit.xml b/phpunit.xml index 3a9083f5..99fb579e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,11 +19,8 @@ - - ./tests/integration - - - ./tests/sparql11 + + ./tests/db_adapter_depended ./tests/unit @@ -34,6 +31,11 @@ + ./extractors + ./parsers + ./serializers + ./sparqlscript + ./src ./store diff --git a/serializers/ARC2_JSONLDSerializer.php b/serializers/ARC2_JSONLDSerializer.php index 9ff590dd..4ecde6e0 100644 --- a/serializers/ARC2_JSONLDSerializer.php +++ b/serializers/ARC2_JSONLDSerializer.php @@ -1,85 +1,88 @@ * @license W3C Software License and GPL * @homepage - * @package ARC2 -*/ - + */ ARC2::inc('RDFSerializer'); -class ARC2_JSONLDSerializer extends ARC2_RDFSerializer { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'application/ld+json'; - } - - /* */ - - function getTerm($v, $term = 's') { - if (!is_array($v)) { - if (preg_match('/^\_\:/', $v)) { - return ($term == 'o') ? $this->getTerm(array('value' => $v, 'type' => 'bnode'), 'o') : '"' . $v . '"'; - } - return ($term == 'o') ? $this->getTerm(array('value' => $v, 'type' => 'uri'), 'o') : '"' . $v . '"'; - } - if (!isset($v['type']) || ($v['type'] != 'literal')) { - if ($term != 'o') { - return $this->getTerm($v['value'], $term); - } - return '{ "@id" : "' . $this->jsonEscape($v['value']) . '" }'; +class ARC2_JSONLDSerializer extends ARC2_RDFSerializer +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - /* literal */ - $r = '{ "@value" : "' . $this->jsonEscape($v['value']) . '"'; - $suffix = isset($v['datatype']) ? ', "@type" : "' . $v['datatype'] . '"' : ''; - $suffix = isset($v['lang']) ? ', "@language" : "' . $v['lang'] . '"' : $suffix; - $r .= $suffix . ' }'; - return $r; - } - function jsonEscape($v) { - if (function_exists('json_encode')) { - return preg_replace('/^"(.*)"$/', '\\1', str_replace("\/","/",json_encode($v))); + public function __init() + { + parent::__init(); + $this->content_header = 'application/ld+json'; } - $from = array("\\", "\r", "\t", "\n", '"', "\b", "\f"); - $to = array('\\\\', '\r', '\t', '\n', '\"', '\b', '\f'); - return str_replace($from, $to, $v); - } - - function getSerializedIndex($index, $raw = 0) { - $r = ''; - $nl = "\n"; - foreach ($index as $s => $ps) { - $r .= $r ? ',' . $nl . $nl : ''; - $r .= ' { '. $nl . ' "@id" : ' . $this->getTerm($s); - //$first_p = 1; - foreach ($ps as $p => $os) { - $r .= ',' . $nl; - $r .= ' ' . $this->getTerm($p). ' : ['; - $first_o = 1; - if (!is_array($os)) {/* single literal o */ - $os = array(array('value' => $os, 'type' => 'literal')); + + public function getTerm($v, $term = 's') + { + if (!is_array($v)) { + if (preg_match('/^\_\:/', $v)) { + return ('o' == $term) ? $this->getTerm(['value' => $v, 'type' => 'bnode'], 'o') : '"'.$v.'"'; + } + + return ('o' == $term) ? $this->getTerm(['value' => $v, 'type' => 'uri'], 'o') : '"'.$v.'"'; } - foreach ($os as $o) { - $r .= $first_o ? $nl : ',' . $nl; - $r .= ' ' . $this->getTerm($o, 'o'); - $first_o = 0; + if (!isset($v['type']) || ('literal' != $v['type'])) { + if ('o' != $term) { + return $this->getTerm($v['value'], $term); + } + + return '{ "@id" : "'.$this->jsonEscape($v['value']).'" }'; } - $r .= $nl . ' ]'; - } - $r .= $nl . ' }'; + /* literal */ + $r = '{ "@value" : "'.$this->jsonEscape($v['value']).'"'; + $suffix = isset($v['datatype']) ? ', "@type" : "'.$v['datatype'].'"' : ''; + $suffix = isset($v['lang']) ? ', "@language" : "'.$v['lang'].'"' : $suffix; + $r .= $suffix.' }'; + + return $r; } - $r .= $r ? ' ' : ''; - return '[' . $nl . $r . $nl . ']'; - } - - /* */ + public function jsonEscape($v) + { + if (function_exists('json_encode')) { + return preg_replace('/^"(.*)"$/', '\\1', str_replace("\/", '/', json_encode($v))); + } + $from = ['\\', "\r", "\t", "\n", '"', "\b", "\f"]; + $to = ['\\\\', '\r', '\t', '\n', '\"', '\b', '\f']; + + return str_replace($from, $to, $v); + } + + public function getSerializedIndex($index, $raw = 0) + { + $r = ''; + $nl = "\n"; + foreach ($index as $s => $ps) { + $r .= $r ? ','.$nl.$nl : ''; + $r .= ' { '.$nl.' "@id" : '.$this->getTerm($s); + //$first_p = 1; + foreach ($ps as $p => $os) { + $r .= ','.$nl; + $r .= ' '.$this->getTerm($p).' : ['; + $first_o = 1; + if (!is_array($os)) {/* single literal o */ + $os = [['value' => $os, 'type' => 'literal']]; + } + foreach ($os as $o) { + $r .= $first_o ? $nl : ','.$nl; + $r .= ' '.$this->getTerm($o, 'o'); + $first_o = 0; + } + $r .= $nl.' ]'; + } + $r .= $nl.' }'; + } + $r .= $r ? ' ' : ''; + + return '['.$nl.$r.$nl.']'; + } } diff --git a/serializers/ARC2_LegacyHTMLSerializer.php b/serializers/ARC2_LegacyHTMLSerializer.php index 0adf7392..ce2ae2cd 100755 --- a/serializers/ARC2_LegacyHTMLSerializer.php +++ b/serializers/ARC2_LegacyHTMLSerializer.php @@ -10,102 +10,104 @@ class: ARC2 Legacy XML Serializer ARC2::inc('Class'); -class ARC2_LegacyHTMLSerializer extends ARC2_Class { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'text/html'; - } - - /* */ - - function getSerializedArray($struct, $root = 1, $ind = ' ') { - $n = "\n"; - $r = ''; - $is_flat = $this->isAssociativeArray($struct) ? 0 : 1; - foreach ($struct as $k => $v) { - if (!$is_flat) $r .= $n . $ind . $ind . '
' . $k . '
'; - $r .= $n . $ind . $ind . '
' . (is_array($v) ? $this->getSerializedArray($v, 0, $ind . $ind . $ind) . $n . $ind . $ind : htmlspecialchars($v)) . '
'; +class ARC2_LegacyHTMLSerializer extends ARC2_Class +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - return $n . $ind . '
' . $r . $n . $ind . '
'; - } - - /* */ - function isAssociativeArray($v) { - foreach (array_keys($v) as $k => $val) { - if ($k !== $val) return 1; + public function __init() + { + parent::__init(); + $this->content_header = 'text/html'; } - return 0; - } - - /* */ - function getSerializedNode($index, $node, $level = 0, $raw = 0) { - $r = ''; - $tag = $this->v('tag', '', $node); - if (preg_match('/^(comment|script)$/', $tag)) { - } - elseif ($tag == 'cdata') { - $r .= $this->v('cdata', '', $node); - $r .= $this->v('value', '', $node['a']); - } - else { - /* open tag */ - if (preg_match('/^(div|form|p|section)$/', $tag)) { - $r .= str_pad("\n", $level + 1, " "); - } - $r .= '<' . $tag; - $attrs = $this->v('a', array(), $node); - foreach ($attrs as $k => $v) { - /* use uri, if detected */ - if ($k != 'id') { - $v = $this->v($k . ' uri', $v, $attrs); + public function getSerializedArray($struct, $root = 1, $ind = ' ') + { + $n = "\n"; + $r = ''; + $is_flat = $this->isAssociativeArray($struct) ? 0 : 1; + foreach ($struct as $k => $v) { + if (!$is_flat) { + $r .= $n.$ind.$ind.'
'.$k.'
'; + } + $r .= $n.$ind.$ind.'
'.(is_array($v) ? $this->getSerializedArray($v, 0, $ind.$ind.$ind).$n.$ind.$ind : htmlspecialchars($v)).'
'; } - /* skip arrays and other derived attrs */ - if (preg_match('/\s/s', $k)) continue; - $r .= ' ' . $k . '="' . $v . '"'; - } - if ($node['empty']) { - $r .= '/>'; - } - else { - $r .= '>'; - /* cdata */ - $r .= $this->v('cdata', '', $node); - /* sub-nodes */ - $sub_nodes = $this->v($node['id'], array(), $index); - foreach ($sub_nodes as $sub_node) { - $r .= $this->getSerializedNode($index, $sub_node, $level + 1, 1); - } - /* close tag */ - //$r .= str_pad("\n", $level + 1, " ") . ''; - $r .= ''; - if (preg_match('/^(div|form|p|section)$/', $tag)) { - $r .= str_pad("\n", $level + 1, " "); + + return $n.$ind.'
'.$r.$n.$ind.'
'; + } + + public function isAssociativeArray($v) + { + foreach (array_keys($v) as $k => $val) { + if ($k !== $val) { + return 1; + } } - } + + return 0; } - /* doc envelope, in case of sub-structure serializing */ - if (!$raw && ($level == 0) && ($node['level'] > 1)) { - $r = ' + + public function getSerializedNode($index, $node, $level = 0, $raw = 0) + { + $r = ''; + $tag = $this->v('tag', '', $node); + if (preg_match('/^(comment|script)$/', $tag)) { + } elseif ('cdata' == $tag) { + $r .= $this->v('cdata', '', $node); + $r .= $this->v('value', '', $node['a']); + } else { + /* open tag */ + if (preg_match('/^(div|form|p|section)$/', $tag)) { + $r .= str_pad("\n", $level + 1, ' '); + } + $r .= '<'.$tag; + $attrs = $this->v('a', [], $node); + foreach ($attrs as $k => $v) { + /* use uri, if detected */ + if ('id' != $k) { + $v = $this->v($k.' uri', $v, $attrs); + } + /* skip arrays and other derived attrs */ + if (preg_match('/\s/s', $k)) { + continue; + } + $r .= ' '.$k.'="'.$v.'"'; + } + if ($node['empty']) { + $r .= '/>'; + } else { + $r .= '>'; + /* cdata */ + $r .= $this->v('cdata', '', $node); + /* sub-nodes */ + $sub_nodes = $this->v($node['id'], [], $index); + foreach ($sub_nodes as $sub_node) { + $r .= $this->getSerializedNode($index, $sub_node, $level + 1, 1); + } + /* close tag */ + //$r .= str_pad("\n", $level + 1, " ") . ''; + $r .= ''; + if (preg_match('/^(div|form|p|section)$/', $tag)) { + $r .= str_pad("\n", $level + 1, ' '); + } + } + } + /* doc envelope, in case of sub-structure serializing */ + if (!$raw && (0 == $level) && ($node['level'] > 1)) { + $r = ' - ' . $r . ' + '.$r.' '; - } - return $r; - } + } - /* */ + return $r; + } } - diff --git a/serializers/ARC2_LegacyJSONSerializer.php b/serializers/ARC2_LegacyJSONSerializer.php index 4166873b..6d0a04f3 100755 --- a/serializers/ARC2_LegacyJSONSerializer.php +++ b/serializers/ARC2_LegacyJSONSerializer.php @@ -10,44 +10,46 @@ class: ARC2 Legacy JSON Serializer ARC2::inc('Class'); -class ARC2_LegacyJSONSerializer extends ARC2_Class { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'application/json'; - } - - /* */ - - function getSerializedArray($struct, $ind = '') { - $n = "\n"; - if (function_exists('json_encode')) return str_replace('","', '",' . $n . '"', str_replace("\/","/",json_encode($struct))); - $r = ''; - $from = array("\\", "\r", "\t", "\n", '"', "\b", "\f"); - $to = array('\\\\', '\r', '\t', '\n', '\"', '\b', '\f'); - $is_flat = $this->isAssociativeArray($struct) ? 0 : 1; - foreach ($struct as $k => $v) { - $r .= $r ? ',' . $n . $ind . $ind : $ind . $ind; - $r .= $is_flat ? '' : '"' . $k . '": '; - $r .= is_array($v) ? $this->getSerializedArray($v, $ind . ' ') : '"' . str_replace($from, $to, $v) . '"'; +class ARC2_LegacyJSONSerializer extends ARC2_Class +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - return $is_flat ? $ind . '[' . $n . $r . $n . $ind . ']' : $ind . '{' . $n . $r . $n . $ind . '}'; - } - - /* */ - - function isAssociativeArray($v) { - foreach (array_keys($v) as $k => $val) { - if ($k !== $val) return 1; + + public function __init() + { + parent::__init(); + $this->content_header = 'application/json'; } - return 0; - } - - /* */ -} + public function getSerializedArray($struct, $ind = '') + { + $n = "\n"; + if (function_exists('json_encode')) { + return str_replace('","', '",'.$n.'"', str_replace("\/", '/', json_encode($struct))); + } + $r = ''; + $from = ['\\', "\r", "\t", "\n", '"', "\b", "\f"]; + $to = ['\\\\', '\r', '\t', '\n', '\"', '\b', '\f']; + $is_flat = $this->isAssociativeArray($struct) ? 0 : 1; + foreach ($struct as $k => $v) { + $r .= $r ? ','.$n.$ind.$ind : $ind.$ind; + $r .= $is_flat ? '' : '"'.$k.'": '; + $r .= is_array($v) ? $this->getSerializedArray($v, $ind.' ') : '"'.str_replace($from, $to, $v).'"'; + } + + return $is_flat ? $ind.'['.$n.$r.$n.$ind.']' : $ind.'{'.$n.$r.$n.$ind.'}'; + } + public function isAssociativeArray($v) + { + foreach (array_keys($v) as $k => $val) { + if ($k !== $val) { + return 1; + } + } + + return 0; + } +} diff --git a/serializers/ARC2_LegacyXMLSerializer.php b/serializers/ARC2_LegacyXMLSerializer.php index 27d939c7..ee07ad63 100755 --- a/serializers/ARC2_LegacyXMLSerializer.php +++ b/serializers/ARC2_LegacyXMLSerializer.php @@ -10,57 +10,61 @@ class: ARC2 Legacy XML Serializer ARC2::inc('Class'); -class ARC2_LegacyXMLSerializer extends ARC2_Class { +class ARC2_LegacyXMLSerializer extends ARC2_Class +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } + + public function __init() + { + parent::__init(); + $this->content_header = 'text/xml'; + } - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'text/xml'; - } + public function getSerializedArray($struct, $root = 1, $ind = ' ') + { + $n = "\n"; + $r = ''; + $is_flat = $this->isAssociativeArray($struct) ? 0 : 1; + foreach ($struct as $k => $v) { + $tag = $is_flat ? 'item' : preg_replace('/[\s]/s', '_', $k); + $tag = preg_replace('/^.*([a-z0-9\-\_]+)$/Uis', '\\1', $tag); + $r .= $n.$ind.'<'.$tag.'>'.(is_array($v) ? $this->getSerializedArray($v, 0, $ind.' ').$n.$ind : htmlspecialchars($v)).''; + } + if ($root) { + $r = $this->getHead().$r.$this->getFooter(); + } - /* */ - - function getSerializedArray($struct, $root = 1, $ind = ' ') { - $n = "\n"; - $r = ''; - $is_flat = $this->isAssociativeArray($struct) ? 0 : 1; - foreach ($struct as $k => $v) { - $tag = $is_flat ? 'item' : preg_replace('/[\s]/s', '_', $k); - $tag = preg_replace('/^.*([a-z0-9\-\_]+)$/Uis', '\\1', $tag); - $r .= $n . $ind . '<' . $tag . '>' . (is_array($v) ? $this->getSerializedArray($v, 0, $ind . ' ') . $n . $ind : htmlspecialchars($v)) . ''; + return $r; } - if ($root) $r = $this->getHead() . $r . $this->getFooter(); - return $r; - } - - /* */ - function getHead() { - $n = "\n"; - $r = ''; - $r .= $n . ''; - return $r; - } - - function getFooter() { - $n = "\n"; - $r = $n . ''; - return $r; - } - - /* */ + public function getHead() + { + $n = "\n"; + $r = ''; + $r .= $n.''; - function isAssociativeArray($v) { - foreach (array_keys($v) as $k => $val) { - if ($k !== $val) return 1; + return $r; } - return 0; - } - - /* */ -} + public function getFooter() + { + $n = "\n"; + $r = $n.''; + return $r; + } + + public function isAssociativeArray($v) + { + foreach (array_keys($v) as $k => $val) { + if ($k !== $val) { + return 1; + } + } + + return 0; + } +} diff --git a/serializers/ARC2_MicroRDFSerializer.php b/serializers/ARC2_MicroRDFSerializer.php index 47c9c0bc..d8afc0ce 100755 --- a/serializers/ARC2_MicroRDFSerializer.php +++ b/serializers/ARC2_MicroRDFSerializer.php @@ -1,142 +1,163 @@ - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('RDFSerializer'); -class ARC2_MicroRDFSerializer extends ARC2_RDFSerializer { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'text/html'; - $this->label_store = $this->v('label_store', '', $this->a); - } - - /* */ - - function getLabel($res, $ps = '') { - if (!$ps) $ps = array(); - foreach ($ps as $p => $os) { - if (preg_match('/[\/\#](name|label|summary|title|fn)$/i', $p)) { - return $os[0]['value']; - } +class ARC2_MicroRDFSerializer extends ARC2_RDFSerializer +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } + + public function __init() + { + parent::__init(); + $this->content_header = 'text/html'; + $this->label_store = $this->v('label_store', '', $this->a); + } + + public function getLabel($res, $ps = '') + { + if (!$ps) { + $ps = []; + } + foreach ($ps as $p => $os) { + if (preg_match('/[\/\#](name|label|summary|title|fn)$/i', $p)) { + return $os[0]['value']; + } + } + if (preg_match('/^\_\:/', $res)) { + return 'An unnamed resource'; + } + + return $this->extractTermLabel($res); + + return preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('_', ' ', $res)); } - if (preg_match('/^\_\:/', $res)) return "An unnamed resource"; - return $this->extractTermLabel($res); - return preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('_', ' ', $res)); - } - - function getSerializedIndex($index, $res = '') { - $r = ''; - $n = "\n"; - if ($res) $index = array($res => $index[$res]); - //return Trice::dump($index); - $types = $this->v($this->expandPName('rdf:type'), array(), $index); - $main_type = $types ? $types[0]['value'] : ''; - foreach ($index as $s => $ps) { - /* node */ - $r .= ' -
mdAttrs($s, $main_type) . '> -

' . ucfirst($this->getLabel($s, $ps)) . '

+ + public function getSerializedIndex($index, $res = '') + { + $r = ''; + $n = "\n"; + if ($res) { + $index = [$res => $index[$res]]; + } + //return Trice::dump($index); + $types = $this->v($this->expandPName('rdf:type'), [], $index); + $main_type = $types ? $types[0]['value'] : ''; + foreach ($index as $s => $ps) { + /* node */ + $r .= ' +
mdAttrs($s, $main_type).'> +

'.ucfirst($this->getLabel($s, $ps)).'

'; - /* arcs */ - foreach ($ps as $p => $os) { - $p_cls = strtolower($this->getPName($p)); - $p_cls = str_replace(':', '-', $p_cls); - $r .= ' -
- ' . ucfirst($this->getLabel($p)) . ': + /* arcs */ + foreach ($ps as $p => $os) { + $p_cls = strtolower($this->getPName($p)); + $p_cls = str_replace(':', '-', $p_cls); + $r .= ' +
+ '.ucfirst($this->getLabel($p)).':
    '; - $oc = count($os); - foreach ($os as $i => $o) { - $val = $this->getObjectValue($o, $p); - $cls = ''; - if ($i == 0) $cls .= ($cls ? ' ' : '') . 'first'; - if ($i == $oc - 1) $cls .= ($cls ? ' ' : '') . 'last'; - $r .= $n . '' . $val . ''; - } - $r .= ' + $oc = count($os); + foreach ($os as $i => $o) { + $val = $this->getObjectValue($o, $p); + $cls = ''; + if (0 == $i) { + $cls .= ($cls ? ' ' : '').'first'; + } + if ($i == $oc - 1) { + $cls .= ($cls ? ' ' : '').'last'; + } + $r .= $n.''.$val.''; + } + $r .= '
'; - } - /* /node */ - $r .= ' + } + /* /node */ + $r .= '
'; + } + + return $r; } - return $r; - } - - function getObjectValue($o, $p) { - if ($o['type'] == 'uri') { - if (preg_match('/(jpe?g|gif|png)$/i', $o['value'])) { - return $this->getImageObjectValue($o, $p); - } - return $this->getURIObjectValue($o, $p); - } - if ($o['type'] == "bnode") { - return $this->getBNodeObjectValue($o, $p); + + public function getObjectValue($o, $p) + { + if ('uri' == $o['type']) { + if (preg_match('/(jpe?g|gif|png)$/i', $o['value'])) { + return $this->getImageObjectValue($o, $p); + } + + return $this->getURIObjectValue($o, $p); + } + if ('bnode' == $o['type']) { + return $this->getBNodeObjectValue($o, $p); + } + + return $this->getLiteralObjectValue($o, $p); } - return $this->getLiteralObjectValue($o, $p); - } - - function getImageObjectValue($o, $p) { - return 'img'; - } - - function getURIObjectValue($o, $p) { - $id = htmlspecialchars($o['value']); - $label = $this->getObjectLabel($o['value']); - /* differing href */ - $href = htmlspecialchars($this->v('href', $o['value'], $o)); - if ($id != $href) { - return '' . $label . ''; + + public function getImageObjectValue($o, $p) + { + return 'img'; } - return '' . $label . ''; - //$label = $o['value']; + + public function getURIObjectValue($o, $p) + { + $id = htmlspecialchars($o['value']); + $label = $this->getObjectLabel($o['value']); + /* differing href */ + $href = htmlspecialchars($this->v('href', $o['value'], $o)); + if ($id != $href) { + return ''.$label.''; + } + + return ''.$label.''; + //$label = $o['value']; //$label = preg_replace('/^https?\:\/\/(www\.)?/', '', $label); - } + } - function getBNodeObjectValue($o, $p) { - return '
' . $o['value'] . '
'; - return '
An unnamed resource
'; - } + public function getBNodeObjectValue($o, $p) + { + return '
'.$o['value'].'
'; - function getLiteralObjectValue($o, $p) { - return '
' . $o['value'] . '
'; - } + return '
An unnamed resource
'; + } - /* */ + public function getLiteralObjectValue($o, $p) + { + return '
'.$o['value'].'
'; + } - function getObjectLabel($id) { - $r = $this->extractTermLabel($id); - if (!$this->label_store) return $r; - $q = ' + public function getObjectLabel($id) + { + $r = $this->extractTermLabel($id); + if (!$this->label_store) { + return $r; + } + $q = ' SELECT ?val WHERE { - <' . $id . '> ?p ?val . + <'.$id.'> ?p ?val . FILTER(REGEX(str(?p), "(label|title|name|summary)$")) } LIMIT 1 '; - $row = $this->label_store->query($q, 'row'); - return $row ? $row['val'] : $r; - } + $row = $this->label_store->query($q, 'row'); - /* */ - + return $row ? $row['val'] : $r; + } } - diff --git a/serializers/ARC2_NTriplesSerializer.php b/serializers/ARC2_NTriplesSerializer.php index 7c80d31a..61e8b6ec 100644 --- a/serializers/ARC2_NTriplesSerializer.php +++ b/serializers/ARC2_NTriplesSerializer.php @@ -1,163 +1,172 @@ - * @package ARC2 -*/ - + */ ARC2::inc('RDFSerializer'); -class ARC2_NTriplesSerializer extends ARC2_RDFSerializer { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->esc_chars = array(); - $this->raw = 0; - } - - /* */ - - function getTerm($v, $term = '') { - // type detection - if (!is_array($v) || empty($v['type'])) { - // bnode - if (preg_match('/^\_\:/', $v)) { - return $this->getTerm(array('value' => $v, 'type' => 'bnode')); - } - // uri - if (preg_match('/^[a-z0-9]+\:[^\s\"]*$/is' . ($this->has_pcre_unicode ? 'u' : ''), $v)) { - return $this->getTerm(array('value' => $v, 'type' => 'uri')); - } - // fallback for non-unicode environments: subjects and predicates can't be literals. - if (in_array($term, array('s', 'p'))) { - return $this->getTerm(array('value' => $v, 'type' => 'uri')); - } - // assume literal - return $this->getTerm(array('type' => 'literal', 'value' => $v)); +class ARC2_NTriplesSerializer extends ARC2_RDFSerializer +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - if ($v['type'] == 'bnode') { - return $v['value']; - } - elseif ($v['type'] == 'uri') { - return '<' . $this->escape($v['value']) . '>'; + + public function __init() + { + parent::__init(); + $this->esc_chars = []; + $this->raw = 0; } - // something went wrong - elseif ($v['type'] != 'literal') { - return $this->getTerm($v['value']); + + public function getTerm($v, $term = '') + { + // type detection + if (!is_array($v) || empty($v['type'])) { + // bnode + if (preg_match('/^\_\:/', $v)) { + return $this->getTerm(['value' => $v, 'type' => 'bnode']); + } + // uri + if (preg_match('/^[a-z0-9]+\:[^\s\"]*$/is'.($this->has_pcre_unicode ? 'u' : ''), $v)) { + return $this->getTerm(['value' => $v, 'type' => 'uri']); + } + // fallback for non-unicode environments: subjects and predicates can't be literals. + if (in_array($term, ['s', 'p'])) { + return $this->getTerm(['value' => $v, 'type' => 'uri']); + } + // assume literal + return $this->getTerm(['type' => 'literal', 'value' => $v]); + } + if ('bnode' == $v['type']) { + return $v['value']; + } elseif ('uri' == $v['type']) { + return '<'.$this->escape($v['value']).'>'; + } + // something went wrong + elseif ('literal' != $v['type']) { + return $this->getTerm($v['value']); + } + /* literal */ + $quot = '"'; + if ($this->raw && preg_match('/\"/', $v['value'])) { + $quot = "'"; + if (preg_match('/\'/', $v['value'])) { + $quot = '"""'; + if (preg_match('/\"\"\"/', $v['value']) || preg_match('/\"$/', $v['value']) || preg_match('/^\"/', $v['value'])) { + $quot = "'''"; + $v['value'] = preg_replace("/'$/", "' ", $v['value']); + $v['value'] = preg_replace("/^'/", " '", $v['value']); + $v['value'] = str_replace("'''", '\\\'\\\'\\\'', $v['value']); + } + } + } + if ($this->raw && (1 == strlen($quot)) && preg_match('/[\x0d\x0a]/', $v['value'])) { + $quot = $quot.$quot.$quot; + } + $suffix = isset($v['lang']) && $v['lang'] ? '@'.$v['lang'] : ''; + $suffix = isset($v['datatype']) && $v['datatype'] ? '^^'.$this->getTerm($v['datatype']) : $suffix; + //return $quot . "object" . utf8_encode($v['value']) . $quot . $suffix; + return $quot.$this->escape($v['value']).$quot.$suffix; } - /* literal */ - $quot = '"'; - if ($this->raw && preg_match('/\"/', $v['value'])) { - $quot = "'"; - if (preg_match('/\'/', $v['value'])) { - $quot = '"""'; - if (preg_match('/\"\"\"/', $v['value']) || preg_match('/\"$/', $v['value']) || preg_match('/^\"/', $v['value'])) { - $quot = "'''"; - $v['value'] = preg_replace("/'$/", "' ", $v['value']); - $v['value'] = preg_replace("/^'/", " '", $v['value']); - $v['value'] = str_replace("'''", '\\\'\\\'\\\'', $v['value']); + + public function getSerializedIndex($index, $raw = 0) + { + $this->raw = $raw; + $r = ''; + $nl = "\n"; + foreach ($index as $s => $ps) { + $s = $this->getTerm($s, 's'); + foreach ($ps as $p => $os) { + $p = $this->getTerm($p, 'p'); + if (!is_array($os)) {/* single literal o */ + $os = [['value' => $os, 'type' => 'literal']]; + } + foreach ($os as $o) { + $o = $this->getTerm($o, 'o‚'); + $r .= $r ? $nl : ''; + $r .= $s.' '.$p.' '.$o.' .'; + } + } } - } + + return $r.$nl; } - if ($this->raw && (strlen($quot) == 1) && preg_match('/[\x0d\x0a]/', $v['value'])) { - $quot = $quot . $quot . $quot; + + public function escape($v) + { + $r = ''; + // decode, if possible + $v = (false === strpos(utf8_decode(str_replace('?', '', $v)), '?')) ? utf8_decode($v) : $v; + if ($this->raw) { + return $v; + } // no further escaping wanted + // escape tabs and linefeeds + $v = str_replace(["\t", "\r", "\n"], ['\t', '\r', '\n'], $v); + // escape non-ascii-chars + $v = preg_replace_callback('/([^a-zA-Z0-9 \!\#\$\%\&\(\)\*\+\,\-\.\/\:\;\=\?\@\^\_\{\|\}]+)/', [$this, 'escapeChars'], $v); + + return $v; } - $suffix = isset($v['lang']) && $v['lang'] ? '@' . $v['lang'] : ''; - $suffix = isset($v['datatype']) && $v['datatype'] ? '^^' . $this->getTerm($v['datatype']) : $suffix; - //return $quot . "object" . utf8_encode($v['value']) . $quot . $suffix; - return $quot . $this->escape($v['value']) . $quot . $suffix; - } - - function getSerializedIndex($index, $raw = 0) { - $this->raw = $raw; - $r = ''; - $nl = "\n"; - foreach ($index as $s => $ps) { - $s = $this->getTerm($s, 's'); - foreach ($ps as $p => $os) { - $p = $this->getTerm($p, 'p'); - if (!is_array($os)) {/* single literal o */ - $os = array(array('value' => $os, 'type' => 'literal')); + + public function escapeChars($matches) + { + $v = $matches[1]; + $r = ''; + // loop through mb chars + if (function_exists('mb_strlen')) { + for ($i = 0, $i_max = mb_strlen($v, 'UTF-8'); $i < $i_max; ++$i) { + $c = mb_substr($v, $i, 1, 'UTF-8'); + if (!isset($this->esc_chars[$c])) { + $this->esc_chars[$c] = $this->getEscapedChar($c, $this->getCharNo($c, 1)); + } + $r .= $this->esc_chars[$c]; + } } - foreach ($os as $o) { - $o = $this->getTerm($o, 'o‚'); - $r .= $r ? $nl : ''; - $r .= $s . ' ' . $p . ' ' . $o . ' .'; + // fall back to built-in JSON functionality, if available + elseif (function_exists('json_encode')) { + $r = json_encode($v); + if ('null' == $r) { + $r = json_encode(utf8_encode($v)); + } + // remove boundary quotes + if ('"' == substr($r, 0, 1)) { + $r = substr($r, 1); + } + if ('"' == substr($r, -1)) { + $r = substr($r, 0, -1); + } + // uppercase hex chars + $r = preg_replace_callback('/(\\\u)([0-9a-f]{4})', function ($matches) { + return $matches[1].strtoupper($matches[2]); + }, $r); + $r = preg_replace_callback('/(\\\U)([0-9a-f]{8})', function ($matches) { + return $matches[1].strtoupper($matches[2]); + }, $r); } - } + // escape byte-wise (may be wrong for mb chars and newer php versions) + else { + for ($i = 0, $i_max = strlen($v); $i < $i_max; ++$i) { + $c = $v[$i]; + if (!isset($this->esc_chars[$c])) { + $this->esc_chars[$c] = $this->getEscapedChar($c, $this->getCharNo($c)); + } + $r .= $this->esc_chars[$c]; + } + } + + return $r; } - return $r . $nl; - } - - /* */ - function escape($v) { - $r = ''; - // decode, if possible - $v = (strpos(utf8_decode(str_replace('?', '', $v)), '?') === false) ? utf8_decode($v) : $v; - if ($this->raw) return $v;// no further escaping wanted - // escape tabs and linefeeds - $v = str_replace(array("\t", "\r", "\n"), array('\t', '\r', '\n'), $v); - // escape non-ascii-chars - $v = preg_replace_callback('/([^a-zA-Z0-9 \!\#\$\%\&\(\)\*\+\,\-\.\/\:\;\=\?\@\^\_\{\|\}]+)/', array($this, 'escapeChars'), $v); - return $v; - } - - function escapeChars($matches) { - $v = $matches[1]; - $r = ''; - // loop through mb chars - if (function_exists('mb_strlen')) { - for ($i = 0, $i_max = mb_strlen($v, 'UTF-8'); $i < $i_max; $i++) { - $c = mb_substr($v, $i, 1, 'UTF-8'); - if (!isset($this->esc_chars[$c])) { - $this->esc_chars[$c] = $this->getEscapedChar($c, $this->getCharNo($c, 1)); - } - $r .= $this->esc_chars[$c]; - } - } - // fall back to built-in JSON functionality, if available - else if (function_exists('json_encode')) { - $r = json_encode($v); - if ($r == 'null') $r = json_encode (utf8_encode($v)); - // remove boundary quotes - if (substr($r, 0, 1) == '"') $r = substr($r, 1); - if (substr($r, -1) == '"') $r = substr($r, 0, -1); - // uppercase hex chars - $r = preg_replace_callback('/(\\\u)([0-9a-f]{4})', function($matches) { - return $matches[1] . strtoupper($matches[2]); - }, $r); - $r = preg_replace_callback('/(\\\U)([0-9a-f]{8})', function($matches) { - return $matches[1] . strtoupper($matches[2]); - }, $r); - } - // escape byte-wise (may be wrong for mb chars and newer php versions) - else { - for ($i = 0, $i_max = strlen($v); $i < $i_max; $i++) { - $c = $v[$i]; - if (!isset($this->esc_chars[$c])) { - $this->esc_chars[$c] = $this->getEscapedChar($c, $this->getCharNo($c)); - } - $r .= $this->esc_chars[$c]; - } - } - return $r; - } - - /* */ - - function getCharNo($c, $is_encoded = false) { - $c_utf = $is_encoded ? $c : utf8_encode($c); - $bl = strlen($c_utf);/* binary length */ - $r = 0; - switch ($bl) { + public function getCharNo($c, $is_encoded = false) + { + $c_utf = $is_encoded ? $c : utf8_encode($c); + $bl = strlen($c_utf); /* binary length */ + $r = 0; + switch ($bl) { case 1:/* 0####### (0-127) */ $r = ord($c_utf); break; @@ -171,26 +180,51 @@ function getCharNo($c, $is_encoded = false) { $r = ((ord($c_utf[0]) - 240) * 262144) + ((ord($c_utf[1]) - 128) * 4096) + ((ord($c_utf[2]) - 128) * 64) + (ord($c_utf[3]) - 128); break; } - return $r; - } - function getEscapedChar($c, $no) {/*see http://www.w3.org/TR/rdf-testcases/#ntrip_strings */ - if ($no < 9) return "\\u" . sprintf('%04X', $no); /* #x0-#x8 (0-8) */ - if ($no == 9) return '\t'; /* #x9 (9) */ - if ($no == 10) return '\n'; /* #xA (10) */ - if ($no < 13) return "\\u" . sprintf('%04X', $no); /* #xB-#xC (11-12) */ - if ($no == 13) return '\r'; /* #xD (13) */ - if ($no < 32) return "\\u" . sprintf('%04X', $no); /* #xE-#x1F (14-31) */ - if ($no < 34) return $c; /* #x20-#x21 (32-33) */ - if ($no == 34) return '\"'; /* #x22 (34) */ - if ($no < 92) return $c; /* #x23-#x5B (35-91) */ - if ($no == 92) return '\\'; /* #x5C (92) */ - if ($no < 127) return $c; /* #x5D-#x7E (93-126) */ - if ($no < 65536) return "\\u" . sprintf('%04X', $no); /* #x7F-#xFFFF (128-65535) */ - if ($no < 1114112) return "\\U" . sprintf('%08X', $no); /* #x10000-#x10FFFF (65536-1114111) */ - return ''; /* not defined => ignore */ - } - - /* */ - + return $r; + } + + public function getEscapedChar($c, $no) + {/*see http://www.w3.org/TR/rdf-testcases/#ntrip_strings */ + if ($no < 9) { + return '\\u'.sprintf('%04X', $no); + } /* #x0-#x8 (0-8) */ + if (9 == $no) { + return '\t'; + } /* #x9 (9) */ + if (10 == $no) { + return '\n'; + } /* #xA (10) */ + if ($no < 13) { + return '\\u'.sprintf('%04X', $no); + } /* #xB-#xC (11-12) */ + if (13 == $no) { + return '\r'; + } /* #xD (13) */ + if ($no < 32) { + return '\\u'.sprintf('%04X', $no); + } /* #xE-#x1F (14-31) */ + if ($no < 34) { + return $c; + } /* #x20-#x21 (32-33) */ + if (34 == $no) { + return '\"'; + } /* #x22 (34) */ + if ($no < 92) { + return $c; + } /* #x23-#x5B (35-91) */ + if (92 == $no) { + return '\\'; + } /* #x5C (92) */ + if ($no < 127) { + return $c; + } /* #x5D-#x7E (93-126) */ + if ($no < 65536) { + return '\\u'.sprintf('%04X', $no); + } /* #x7F-#xFFFF (128-65535) */ + if ($no < 1114112) { + return '\\U'.sprintf('%08X', $no); + } /* #x10000-#x10FFFF (65536-1114111) */ + return ''; /* not defined => ignore */ + } } diff --git a/serializers/ARC2_POSHRDFSerializer.php b/serializers/ARC2_POSHRDFSerializer.php index 5d4bcf7f..d33347a6 100755 --- a/serializers/ARC2_POSHRDFSerializer.php +++ b/serializers/ARC2_POSHRDFSerializer.php @@ -10,96 +10,110 @@ class: ARC2 POSH RDF Serializer ARC2::inc('RDFSerializer'); -class ARC2_POSHRDFSerializer extends ARC2_RDFSerializer { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'text/html'; - } - - /* */ - - function getLabel($res, $ps = '') { - if (!$ps) $ps = array(); - foreach ($ps as $p => $os) { - if (preg_match('/[\/\#](name|label|summary|title|fn)$/i', $p)) { - return $os[0]['value']; - } +class ARC2_POSHRDFSerializer extends ARC2_RDFSerializer +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - if (preg_match('/^\_\:/', $res)) return "An unnamed resource"; - return preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('_', ' ', $res)); - } - - function getSerializedIndex($index, $res = '') { - $r = ''; - $n = "\n"; - if ($res) $index = array($res => $index[$res]); - //return Trice::dump($index); - foreach ($index as $s => $ps) { - /* node */ - $r .= ' + + public function __init() + { + parent::__init(); + $this->content_header = 'text/html'; + } + + public function getLabel($res, $ps = '') + { + if (!$ps) { + $ps = []; + } + foreach ($ps as $p => $os) { + if (preg_match('/[\/\#](name|label|summary|title|fn)$/i', $p)) { + return $os[0]['value']; + } + } + if (preg_match('/^\_\:/', $res)) { + return 'An unnamed resource'; + } + + return preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('_', ' ', $res)); + } + + public function getSerializedIndex($index, $res = '') + { + $r = ''; + $n = "\n"; + if ($res) { + $index = [$res => $index[$res]]; + } + //return Trice::dump($index); + foreach ($index as $s => $ps) { + /* node */ + $r .= '
-

' . $this->getLabel($s, $ps) . '

+

'.$this->getLabel($s, $ps).'

'; - /* arcs */ - foreach ($ps as $p => $os) { - $r .= ' + /* arcs */ + foreach ($ps as $p => $os) { + $r .= '
- ' . ucfirst($this->getLabel($p)) . ' + '.ucfirst($this->getLabel($p)).' '; - foreach ($os as $o) { - $r .= $n . $this->getObjectValue($o); - } - $r .= ' + foreach ($os as $o) { + $r .= $n.$this->getObjectValue($o); + } + $r .= '
'; - } - /* node */ - $r .= ' + } + /* node */ + $r .= '
'; + } + + return $r; } - return $r; - } - - function getObjectValue($o) { - if ($o['type'] == 'uri') { - if (preg_match('/(jpe?g|gif|png)$/i', $o['value'])) { - return $this->getImageObjectValue($o); - } - return $this->getURIObjectValue($o); + + public function getObjectValue($o) + { + if ('uri' == $o['type']) { + if (preg_match('/(jpe?g|gif|png)$/i', $o['value'])) { + return $this->getImageObjectValue($o); + } + + return $this->getURIObjectValue($o); + } + if ('bnode' == $o['type']) { + return $this->getBNodeObjectValue($o); + } + + return $this->getLiteralObjectValue($o); } - if ($o['type'] == "bnode") { - return $this->getBNodeObjectValue($o); + + public function getImageObjectValue($o) + { + return 'img'; } - return $this->getLiteralObjectValue($o); - } - - function getImageObjectValue($o) { - return 'img'; - } - - function getURIObjectValue($o) { - $href = htmlspecialchars($o['value']); - $label = $o['value']; - $label = preg_replace('/^https?\:\/\/(www\.)?/', '', $label); - return '' . $label . ''; - } - - function getBNodeObjectValue($o) { - return '
An unnamed resource
'; - } - - function getLiteralObjectValue($o) { - return '
' . $o['value'] . '
'; - } - - /* */ -} + public function getURIObjectValue($o) + { + $href = htmlspecialchars($o['value']); + $label = $o['value']; + $label = preg_replace('/^https?\:\/\/(www\.)?/', '', $label); + return ''.$label.''; + } + + public function getBNodeObjectValue($o) + { + return '
An unnamed resource
'; + } + + public function getLiteralObjectValue($o) + { + return '
'.$o['value'].'
'; + } +} diff --git a/serializers/ARC2_RDFJSONSerializer.php b/serializers/ARC2_RDFJSONSerializer.php index 31f439ab..3997a55e 100644 --- a/serializers/ARC2_RDFJSONSerializer.php +++ b/serializers/ARC2_RDFJSONSerializer.php @@ -1,89 +1,92 @@ * @license W3C Software License and GPL * @homepage - * @package ARC2 -*/ - + */ ARC2::inc('RDFSerializer'); -class ARC2_RDFJSONSerializer extends ARC2_RDFSerializer { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'application/json'; - } - - /* */ - - function getTerm($v, $term = 's') { - if (!is_array($v)) { - if (preg_match('/^\_\:/', $v)) { - return ($term == 'o') ? $this->getTerm(array('value' => $v, 'type' => 'bnode'), 'o') : '"' . $v . '"'; - } - return ($term == 'o') ? $this->getTerm(array('value' => $v, 'type' => 'uri'), 'o') : '"' . $v . '"'; - } - if (!isset($v['type']) || ($v['type'] != 'literal')) { - if ($term != 'o') { - return $this->getTerm($v['value'], $term); - } - if (preg_match('/^\_\:/', $v['value'])) { - return '{ "value" : "' . $this->jsonEscape($v['value']) . '", "type" : "bnode" }'; - } - return '{ "value" : "' . $this->jsonEscape($v['value']) . '", "type" : "uri" }'; +class ARC2_RDFJSONSerializer extends ARC2_RDFSerializer +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - /* literal */ - $r = '{ "value" : "' . $this->jsonEscape($v['value']) . '", "type" : "literal"'; - $suffix = isset($v['datatype']) ? ', "datatype" : "' . $v['datatype'] . '"' : ''; - $suffix = isset($v['lang']) ? ', "lang" : "' . $v['lang'] . '"' : $suffix; - $r .= $suffix . ' }'; - return $r; - } - function jsonEscape($v) { - if (function_exists('json_encode')) { - return preg_replace('/^"(.*)"$/', '\\1', str_replace("\/","/",json_encode($v))); + public function __init() + { + parent::__init(); + $this->content_header = 'application/json'; } - $from = array("\\", "\r", "\t", "\n", '"', "\b", "\f"); - $to = array('\\\\', '\r', '\t', '\n', '\"', '\b', '\f'); - return str_replace($from, $to, $v); - } - - function getSerializedIndex($index, $raw = 0) { - $r = ''; - $nl = "\n"; - foreach ($index as $s => $ps) { - $r .= $r ? ',' . $nl . $nl : ''; - $r .= ' ' . $this->getTerm($s). ' : {'; - $first_p = 1; - foreach ($ps as $p => $os) { - $r .= $first_p ? $nl : ',' . $nl; - $r .= ' ' . $this->getTerm($p). ' : ['; - $first_o = 1; - if (!is_array($os)) {/* single literal o */ - $os = array(array('value' => $os, 'type' => 'literal')); + + public function getTerm($v, $term = 's') + { + if (!is_array($v)) { + if (preg_match('/^\_\:/', $v)) { + return ('o' == $term) ? $this->getTerm(['value' => $v, 'type' => 'bnode'], 'o') : '"'.$v.'"'; + } + + return ('o' == $term) ? $this->getTerm(['value' => $v, 'type' => 'uri'], 'o') : '"'.$v.'"'; } - foreach ($os as $o) { - $r .= $first_o ? $nl : ',' . $nl; - $r .= ' ' . $this->getTerm($o, 'o'); - $first_o = 0; + if (!isset($v['type']) || ('literal' != $v['type'])) { + if ('o' != $term) { + return $this->getTerm($v['value'], $term); + } + if (preg_match('/^\_\:/', $v['value'])) { + return '{ "value" : "'.$this->jsonEscape($v['value']).'", "type" : "bnode" }'; + } + + return '{ "value" : "'.$this->jsonEscape($v['value']).'", "type" : "uri" }'; } - $first_p = 0; - $r .= $nl . ' ]'; - } - $r .= $nl . ' }'; + /* literal */ + $r = '{ "value" : "'.$this->jsonEscape($v['value']).'", "type" : "literal"'; + $suffix = isset($v['datatype']) ? ', "datatype" : "'.$v['datatype'].'"' : ''; + $suffix = isset($v['lang']) ? ', "lang" : "'.$v['lang'].'"' : $suffix; + $r .= $suffix.' }'; + + return $r; } - $r .= $r ? ' ' : ''; - return '{' . $nl . $r . $nl . '}'; - } - - /* */ + public function jsonEscape($v) + { + if (function_exists('json_encode')) { + return preg_replace('/^"(.*)"$/', '\\1', str_replace("\/", '/', json_encode($v))); + } + $from = ['\\', "\r", "\t", "\n", '"', "\b", "\f"]; + $to = ['\\\\', '\r', '\t', '\n', '\"', '\b', '\f']; + + return str_replace($from, $to, $v); + } + + public function getSerializedIndex($index, $raw = 0) + { + $r = ''; + $nl = "\n"; + foreach ($index as $s => $ps) { + $r .= $r ? ','.$nl.$nl : ''; + $r .= ' '.$this->getTerm($s).' : {'; + $first_p = 1; + foreach ($ps as $p => $os) { + $r .= $first_p ? $nl : ','.$nl; + $r .= ' '.$this->getTerm($p).' : ['; + $first_o = 1; + if (!is_array($os)) {/* single literal o */ + $os = [['value' => $os, 'type' => 'literal']]; + } + foreach ($os as $o) { + $r .= $first_o ? $nl : ','.$nl; + $r .= ' '.$this->getTerm($o, 'o'); + $first_o = 0; + } + $first_p = 0; + $r .= $nl.' ]'; + } + $r .= $nl.' }'; + } + $r .= $r ? ' ' : ''; + + return '{'.$nl.$r.$nl.'}'; + } } diff --git a/serializers/ARC2_RDFSerializer.php b/serializers/ARC2_RDFSerializer.php index b13a2c01..21aea982 100755 --- a/serializers/ARC2_RDFSerializer.php +++ b/serializers/ARC2_RDFSerializer.php @@ -1,53 +1,53 @@ - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('Class'); -class ARC2_RDFSerializer extends ARC2_Class { +class ARC2_RDFSerializer extends ARC2_Class +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } + + public function __init() + { + parent::__init(); + foreach ($this->ns as $k => $v) { + $this->nsp[$v] = $k; + } + } + + public function xgetPName($v) + {/* moved to merged getPName in ARC2_CLass */ + if (preg_match('/^([a-z0-9\_\-]+)\:([a-z\_][a-z0-9\_\-]*)$/i', $v, $m) && isset($this->ns[$m[1]])) { + $this->used_ns = !in_array($this->ns[$m[1]], $this->used_ns) ? array_merge($this->used_ns, [$this->ns[$m[1]]]) : $this->used_ns; + + return $v; + } + if (preg_match('/^(.*[\/\#])([a-z\_][a-z0-9\-\_]*)$/i', $v, $m)) { + return $this->getPrefix($m[1]).':'.$m[2]; + } - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - foreach ($this->ns as $k => $v) { - $this->nsp[$v] = $k; + return 0; } - } - - /* */ - - function xgetPName($v) {/* moved to merged getPName in ARC2_CLass */ - if (preg_match('/^([a-z0-9\_\-]+)\:([a-z\_][a-z0-9\_\-]*)$/i', $v, $m) && isset($this->ns[$m[1]])) { - $this->used_ns = !in_array($this->ns[$m[1]], $this->used_ns) ? array_merge($this->used_ns, array($this->ns[$m[1]])) : $this->used_ns; - return $v; + + public function getSerializedTriples($triples, $raw = 0) + { + $index = ARC2::getSimpleIndex($triples, 0); + + return $this->getSerializedIndex($index, $raw); } - if (preg_match('/^(.*[\/\#])([a-z\_][a-z0-9\-\_]*)$/i', $v, $m)) { - return $this->getPrefix($m[1]) . ':' . $m[2]; + + public function getSerializedIndex($index, $raw = 0) + { + return ''; } - return 0; - } - - /* */ - - function getSerializedTriples($triples, $raw = 0) { - $index = ARC2::getSimpleIndex($triples, 0); - return $this->getSerializedIndex($index, $raw); - } - - function getSerializedIndex($index, $raw = 0) { - return ''; - } - - /* */ - } diff --git a/serializers/ARC2_RDFXMLSerializer.php b/serializers/ARC2_RDFXMLSerializer.php index 8f5e4c7e..f7ebcabb 100644 --- a/serializers/ARC2_RDFXMLSerializer.php +++ b/serializers/ARC2_RDFXMLSerializer.php @@ -1,198 +1,233 @@ - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('RDFSerializer'); -class ARC2_RDFXMLSerializer extends ARC2_RDFSerializer { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'application/rdf+xml'; - $this->pp_containers = $this->v('serializer_prettyprint_containers', 0, $this->a); - $this->default_ns = $this->v('serializer_default_ns', '', $this->a); - $this->type_nodes = $this->v('serializer_type_nodes', 0, $this->a); - } - - /* */ - - function getTerm($v, $type) { - if (!is_array($v)) {/* uri or bnode */ - if (preg_match('/^\_\:(.*)$/', $v, $m)) { - return ' rdf:nodeID="' . $m[1] . '"'; - } - if ($type == 's') { - return ' rdf:about="' . htmlspecialchars($v) . '"'; - } - if ($type == 'p') { - $pn = $this->getPName($v); - return $pn ? $pn : 0; - } - if ($type == 'o') { - $v = $this->expandPName($v); - if (!preg_match('/^[a-z0-9]{2,}\:[^\s]+$/is', $v)) return $this->getTerm(array('value' => $v, 'type' => 'literal'), $type); - return ' rdf:resource="' . htmlspecialchars($v) . '"'; - } - if ($type == 'datatype') { - $v = $this->expandPName($v); - return ' rdf:datatype="' . htmlspecialchars($v) . '"'; - } - if ($type == 'lang') { - return ' xml:lang="' . htmlspecialchars($v) . '"'; - } - } - if ($this->v('type', '', $v) != 'literal') { - return $this->getTerm($v['value'], 'o'); +class ARC2_RDFXMLSerializer extends ARC2_RDFSerializer +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); } - /* literal */ - $dt = isset($v['datatype']) ? $v['datatype'] : ''; - $lang = isset($v['lang']) ? $v['lang'] : ''; - if ($dt == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral') { - return ' rdf:parseType="Literal">' . $v['value']; - } - elseif ($dt) { - return $this->getTerm($dt, 'datatype') . '>' . htmlspecialchars($v['value']); + + public function __init() + { + parent::__init(); + $this->content_header = 'application/rdf+xml'; + $this->pp_containers = $this->v('serializer_prettyprint_containers', 0, $this->a); + $this->default_ns = $this->v('serializer_default_ns', '', $this->a); + $this->type_nodes = $this->v('serializer_type_nodes', 0, $this->a); } - elseif ($lang) { - return $this->getTerm($lang, 'lang') . '>' . htmlspecialchars($v['value']); + + public function getTerm($v, $type) + { + if (!is_array($v)) {/* uri or bnode */ + if (preg_match('/^\_\:(.*)$/', $v, $m)) { + return ' rdf:nodeID="'.$m[1].'"'; + } + if ('s' == $type) { + return ' rdf:about="'.htmlspecialchars($v).'"'; + } + if ('p' == $type) { + $pn = $this->getPName($v); + + return $pn ? $pn : 0; + } + if ('o' == $type) { + $v = $this->expandPName($v); + if (!preg_match('/^[a-z0-9]{2,}\:[^\s]+$/is', $v)) { + return $this->getTerm(['value' => $v, 'type' => 'literal'], $type); + } + + return ' rdf:resource="'.htmlspecialchars($v).'"'; + } + if ('datatype' == $type) { + $v = $this->expandPName($v); + + return ' rdf:datatype="'.htmlspecialchars($v).'"'; + } + if ('lang' == $type) { + return ' xml:lang="'.htmlspecialchars($v).'"'; + } + } + if ('literal' != $this->v('type', '', $v)) { + return $this->getTerm($v['value'], 'o'); + } + /* literal */ + $dt = isset($v['datatype']) ? $v['datatype'] : ''; + $lang = isset($v['lang']) ? $v['lang'] : ''; + if ('http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' == $dt) { + return ' rdf:parseType="Literal">'.$v['value']; + } elseif ($dt) { + return $this->getTerm($dt, 'datatype').'>'.htmlspecialchars($v['value']); + } elseif ($lang) { + return $this->getTerm($lang, 'lang').'>'.htmlspecialchars($v['value']); + } + + return '>'.htmlspecialchars($this->v('value', '', $v)); } - return '>' . htmlspecialchars($this->v('value', '', $v)); - } - function getPName($v, $connector = ':') { - if ($this->default_ns && (strpos($v, $this->default_ns) === 0)) { - $pname = substr($v, strlen($this->default_ns)); - if (!preg_match('/\//', $pname)) return $pname; + public function getPName($v, $connector = ':') + { + if ($this->default_ns && (0 === strpos($v, $this->default_ns))) { + $pname = substr($v, strlen($this->default_ns)); + if (!preg_match('/\//', $pname)) { + return $pname; + } + } + + return parent::getPName($v, $connector); } - return parent::getPName($v, $connector); - } - - function getHead() { - $r = ''; - $nl = "\n"; - $r .= ''; - $r .= $nl . 'used_ns as $v) { - $r .= $first_ns ? ' ' : $nl . ' '; - foreach ($this->ns as $prefix => $ns) { - if ($ns != $v) continue; - $r .= 'xmlns:' . $prefix . '="' .$v. '"'; - break; - } - $first_ns = 0; + + public function getHead() + { + $r = ''; + $nl = "\n"; + $r .= ''; + $r .= $nl.'used_ns as $v) { + $r .= $first_ns ? ' ' : $nl.' '; + foreach ($this->ns as $prefix => $ns) { + if ($ns != $v) { + continue; + } + $r .= 'xmlns:'.$prefix.'="'.$v.'"'; + break; + } + $first_ns = 0; + } + if ($this->default_ns) { + $r .= $first_ns ? ' ' : $nl.' '; + $r .= 'xmlns="'.$this->default_ns.'"'; + } + $r .= '>'; + + return $r; } - if ($this->default_ns) { - $r .= $first_ns ? ' ' : $nl . ' '; - $r .= 'xmlns="' . $this->default_ns . '"'; + + public function getFooter() + { + $r = ''; + $nl = "\n"; + $r .= $nl.$nl.''; + + return $r; } - $r .= '>'; - return $r; - } - - function getFooter() { - $r = ''; - $nl = "\n"; - $r .= $nl . $nl . ''; - return $r; - } - - function getSerializedIndex($index, $raw = 0) { - $r = ''; - $nl = "\n"; - foreach ($index as $raw_s => $ps) { - $r .= $r ? $nl . $nl : ''; - $s = $this->getTerm($raw_s, 's'); - $tag = 'rdf:Description'; - list($tag, $ps) = $this->getNodeTag($ps); - $sub_ps = 0; - /* pretty containers */ - if ($this->pp_containers && ($ctag = $this->getContainerTag($ps))) { - $tag = 'rdf:' . $ctag; - list($ps, $sub_ps) = $this->splitContainerEntries($ps); - } - $r .= ' <' . $tag . '' .$s . '>'; - $first_p = 1; - foreach ($ps as $p => $os) { - if (!$os) continue; - $p = $this->getTerm($p, 'p'); - if ($p) { - $r .= $nl . str_pad('', 4); - $first_o = 1; - if (!is_array($os)) {/* single literal o */ - $os = array(array('value' => $os, 'type' => 'literal')); - } - foreach ($os as $o) { - $o = $this->getTerm($o, 'o'); - $r .= $first_o ? '' : $nl . ' '; - $r .= '<' . $p; - $r .= $o; - $r .= preg_match('/\>/', $o) ? '' : '/>'; - $first_o = 0; - } - $first_p = 0; + + public function getSerializedIndex($index, $raw = 0) + { + $r = ''; + $nl = "\n"; + foreach ($index as $raw_s => $ps) { + $r .= $r ? $nl.$nl : ''; + $s = $this->getTerm($raw_s, 's'); + $tag = 'rdf:Description'; + list($tag, $ps) = $this->getNodeTag($ps); + $sub_ps = 0; + /* pretty containers */ + if ($this->pp_containers && ($ctag = $this->getContainerTag($ps))) { + $tag = 'rdf:'.$ctag; + list($ps, $sub_ps) = $this->splitContainerEntries($ps); + } + $r .= ' <'.$tag.''.$s.'>'; + $first_p = 1; + foreach ($ps as $p => $os) { + if (!$os) { + continue; + } + $p = $this->getTerm($p, 'p'); + if ($p) { + $r .= $nl.str_pad('', 4); + $first_o = 1; + if (!is_array($os)) {/* single literal o */ + $os = [['value' => $os, 'type' => 'literal']]; + } + foreach ($os as $o) { + $o = $this->getTerm($o, 'o'); + $r .= $first_o ? '' : $nl.' '; + $r .= '<'.$p; + $r .= $o; + $r .= preg_match('/\>/', $o) ? '' : '/>'; + $first_o = 0; + } + $first_p = 0; + } + } + $r .= $r ? $nl.' ' : ''; + if ($sub_ps) { + $r .= $nl.$nl.$this->getSerializedIndex([$raw_s => $sub_ps], 1); + } } - } - $r .= $r ? $nl . ' ' : ''; - if ($sub_ps) $r .= $nl . $nl . $this->getSerializedIndex(array($raw_s => $sub_ps), 1); - } - if ($raw) { - return $r; + if ($raw) { + return $r; + } + + return $this->getHead().$nl.$nl.$r.$this->getFooter(); } - return $this->getHead() . $nl . $nl . $r . $this->getFooter(); - } - - function getNodeTag($ps) { - if (!$this->type_nodes) return array('rdf:Description', $ps); - $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $types = $this->v($rdf . 'type', array(), $ps); - if (!$types) return array('rdf:Description', $ps); - $type = array_shift($types); - $ps[$rdf . 'type'] = $types; - if (!is_array($type)) $type = array('value' => $type); - return array($this->getPName($type['value']), $ps); - } - - /* */ - - function getContainerTag($ps) { - $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - if (!isset($ps[$rdf . 'type'])) return ''; - $types = $ps[$rdf . 'type']; - foreach ($types as $type) { - if (!in_array($type['value'], array($rdf . 'Bag', $rdf . 'Seq', $rdf . 'Alt'))) return ''; - return str_replace($rdf, '', $type['value']); + + public function getNodeTag($ps) + { + if (!$this->type_nodes) { + return ['rdf:Description', $ps]; + } + $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $types = $this->v($rdf.'type', [], $ps); + if (!$types) { + return ['rdf:Description', $ps]; + } + $type = array_shift($types); + $ps[$rdf.'type'] = $types; + if (!is_array($type)) { + $type = ['value' => $type]; + } + + return [$this->getPName($type['value']), $ps]; } - } - - function splitContainerEntries($ps) { - $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; - $items = array(); - $rest = array(); - foreach ($ps as $p => $os) { - $p_short = str_replace($rdf, '', $p); - if ($p_short === 'type') continue; - if (preg_match('/^\_([0-9]+)$/', $p_short, $m)) { - $items = array_merge($items, $os); - } - else { - $rest[$p] = $os; - } + + public function getContainerTag($ps) + { + $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + if (!isset($ps[$rdf.'type'])) { + return ''; + } + $types = $ps[$rdf.'type']; + foreach ($types as $type) { + if (!in_array($type['value'], [$rdf.'Bag', $rdf.'Seq', $rdf.'Alt'])) { + return ''; + } + + return str_replace($rdf, '', $type['value']); + } } - if ($items) return array(array($rdf . 'li' => $items), $rest); - return array($rest, 0); - } - /* */ + public function splitContainerEntries($ps) + { + $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + $items = []; + $rest = []; + foreach ($ps as $p => $os) { + $p_short = str_replace($rdf, '', $p); + if ('type' === $p_short) { + continue; + } + if (preg_match('/^\_([0-9]+)$/', $p_short, $m)) { + $items = array_merge($items, $os); + } else { + $rest[$p] = $os; + } + } + if ($items) { + return [[$rdf.'li' => $items], $rest]; + } + + return [$rest, 0]; + } } diff --git a/serializers/ARC2_RSS10Serializer.php b/serializers/ARC2_RSS10Serializer.php index 51a5cb03..7a4c61cf 100755 --- a/serializers/ARC2_RSS10Serializer.php +++ b/serializers/ARC2_RSS10Serializer.php @@ -1,30 +1,28 @@ - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('RDFXMLSerializer'); -class ARC2_RSS10Serializer extends ARC2_RDFXMLSerializer { - - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'application/rss+xml'; - $this->default_ns = 'http://purl.org/rss/1.0/'; - $this->type_nodes = true; - } +class ARC2_RSS10Serializer extends ARC2_RDFXMLSerializer +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } - /* */ - + public function __init() + { + parent::__init(); + $this->content_header = 'application/rss+xml'; + $this->default_ns = 'http://purl.org/rss/1.0/'; + $this->type_nodes = true; + } } diff --git a/serializers/ARC2_TurtleSerializer.php b/serializers/ARC2_TurtleSerializer.php index c0d39908..84e42321 100644 --- a/serializers/ARC2_TurtleSerializer.php +++ b/serializers/ARC2_TurtleSerializer.php @@ -1,121 +1,128 @@ - * @package ARC2 + * * @version 2010-11-16 -*/ - + */ ARC2::inc('RDFSerializer'); -class ARC2_TurtleSerializer extends ARC2_RDFSerializer { +class ARC2_TurtleSerializer extends ARC2_RDFSerializer +{ + public function __construct($a, &$caller) + { + parent::__construct($a, $caller); + } - function __construct($a, &$caller) { - parent::__construct($a, $caller); - } - - function __init() { - parent::__init(); - $this->content_header = 'application/x-turtle'; - } + public function __init() + { + parent::__init(); + $this->content_header = 'application/x-turtle'; + } - /* */ - - function getTerm($v, $term = '', $qualifier = '') { - if (!is_array($v)) { - if (preg_match('/^\_\:/', $v)) { - return $v; - } - if (($term === 'p') && ($pn = $this->getPName($v))) { - return $pn; - } - if ( - ($term === 'o') && - in_array($qualifier, array('rdf:type', 'rdfs:domain', 'rdfs:range', 'rdfs:subClassOf')) && + public function getTerm($v, $term = '', $qualifier = '') + { + if (!is_array($v)) { + if (preg_match('/^\_\:/', $v)) { + return $v; + } + if (('p' === $term) && ($pn = $this->getPName($v))) { + return $pn; + } + if ( + ('o' === $term) && + in_array($qualifier, ['rdf:type', 'rdfs:domain', 'rdfs:range', 'rdfs:subClassOf']) && ($pn = $this->getPName($v)) ) { - return $pn; - } - if (preg_match('/^[a-z0-9]+\:[^\s]*$/is' . ($this->has_pcre_unicode ? 'u' : ''), $v)) { - return '<' .$v. '>'; - } - return $this->getTerm(array('type' => 'literal', 'value' => $v), $term, $qualifier); - } - if (!isset($v['type']) || ($v['type'] != 'literal')) { - return $this->getTerm($v['value'], $term, $qualifier); - } - /* literal */ - $quot = '"'; - if (preg_match('/\"/', $v['value'])) { - $quot = "'"; - if (preg_match('/\'/', $v['value']) || preg_match('/[\x0d\x0a]/', $v['value'])) { - $quot = '"""'; - if (preg_match('/\"\"\"/', $v['value']) || preg_match('/\"$/', $v['value']) || preg_match('/^\"/', $v['value'])) { - $quot = "'''"; - $v['value'] = preg_replace("/'$/", "' ", $v['value']); - $v['value'] = preg_replace("/^'/", " '", $v['value']); - $v['value'] = str_replace("'''", '\\\'\\\'\\\'', $v['value']); + return $pn; + } + if (preg_match('/^[a-z0-9]+\:[^\s]*$/is'.($this->has_pcre_unicode ? 'u' : ''), $v)) { + return '<'.$v.'>'; + } + + return $this->getTerm(['type' => 'literal', 'value' => $v], $term, $qualifier); } - } - } - if ((strlen($quot) == 1) && preg_match('/[\x0d\x0a]/', $v['value'])) { - $quot = $quot . $quot . $quot; - } - $suffix = isset($v['lang']) && $v['lang'] ? '@' . $v['lang'] : ''; - $suffix = isset($v['datatype']) && $v['datatype'] ? '^^' . $this->getTerm($v['datatype'], 'dt') : $suffix; - return $quot . $v['value'] . $quot . $suffix; - } - - function getHead() { - $r = ''; - $nl = "\n"; - foreach ($this->used_ns as $v) { - $r .= $r ? $nl : ''; - foreach ($this->ns as $prefix => $ns) { - if ($ns != $v) continue; - $r .= '@prefix ' . $prefix . ': <' .$v. '> .'; - break; - } - } - return $r; - } - - function getSerializedIndex($index, $raw = 0) { - $r = ''; - $nl = "\n"; - foreach ($index as $s => $ps) { - $r .= $r ? ' .' . $nl . $nl : ''; - $s = $this->getTerm($s, 's'); - $r .= $s; - $first_p = 1; - foreach ($ps as $p => $os) { - if (!$os) continue; - $p = $this->getTerm($p, 'p'); - $r .= $first_p ? ' ' : ' ;' . $nl . str_pad('', strlen($s) + 1); - $r .= $p; - $first_o = 1; - if (!is_array($os)) {/* single literal o */ - $os = array(array('value' => $os, 'type' => 'literal')); + if (!isset($v['type']) || ('literal' != $v['type'])) { + return $this->getTerm($v['value'], $term, $qualifier); } - foreach ($os as $o) { - $r .= $first_o ? ' ' : ' ,' . $nl . str_pad('', strlen($s) + strlen($p) + 2); - $o = $this->getTerm($o, 'o', $p); - $r .= $o; - $first_o = 0; + /* literal */ + $quot = '"'; + if (preg_match('/\"/', $v['value'])) { + $quot = "'"; + if (preg_match('/\'/', $v['value']) || preg_match('/[\x0d\x0a]/', $v['value'])) { + $quot = '"""'; + if (preg_match('/\"\"\"/', $v['value']) || preg_match('/\"$/', $v['value']) || preg_match('/^\"/', $v['value'])) { + $quot = "'''"; + $v['value'] = preg_replace("/'$/", "' ", $v['value']); + $v['value'] = preg_replace("/^'/", " '", $v['value']); + $v['value'] = str_replace("'''", '\\\'\\\'\\\'', $v['value']); + } + } } - $first_p = 0; - } + if ((1 == strlen($quot)) && preg_match('/[\x0d\x0a]/', $v['value'])) { + $quot = $quot.$quot.$quot; + } + $suffix = isset($v['lang']) && $v['lang'] ? '@'.$v['lang'] : ''; + $suffix = isset($v['datatype']) && $v['datatype'] ? '^^'.$this->getTerm($v['datatype'], 'dt') : $suffix; + + return $quot.$v['value'].$quot.$suffix; } - $r .= $r ? ' .' : ''; - if ($raw) { - return $r; + + public function getHead() + { + $r = ''; + $nl = "\n"; + foreach ($this->used_ns as $v) { + $r .= $r ? $nl : ''; + foreach ($this->ns as $prefix => $ns) { + if ($ns != $v) { + continue; + } + $r .= '@prefix '.$prefix.': <'.$v.'> .'; + break; + } + } + + return $r; } - return $r ? $this->getHead() . $nl . $nl . $r : ''; - } - - /* */ + public function getSerializedIndex($index, $raw = 0) + { + $r = ''; + $nl = "\n"; + foreach ($index as $s => $ps) { + $r .= $r ? ' .'.$nl.$nl : ''; + $s = $this->getTerm($s, 's'); + $r .= $s; + $first_p = 1; + foreach ($ps as $p => $os) { + if (!$os) { + continue; + } + $p = $this->getTerm($p, 'p'); + $r .= $first_p ? ' ' : ' ;'.$nl.str_pad('', strlen($s) + 1); + $r .= $p; + $first_o = 1; + if (!is_array($os)) {/* single literal o */ + $os = [['value' => $os, 'type' => 'literal']]; + } + foreach ($os as $o) { + $r .= $first_o ? ' ' : ' ,'.$nl.str_pad('', strlen($s) + strlen($p) + 2); + $o = $this->getTerm($o, 'o', $p); + $r .= $o; + $first_o = 0; + } + $first_p = 0; + } + } + $r .= $r ? ' .' : ''; + if ($raw) { + return $r; + } + + return $r ? $this->getHead().$nl.$nl.$r : ''; + } } diff --git a/src/ARC2/Store/Adapter/AbstractAdapter.php b/src/ARC2/Store/Adapter/AbstractAdapter.php new file mode 100644 index 00000000..ea9afd55 --- /dev/null +++ b/src/ARC2/Store/Adapter/AbstractAdapter.php @@ -0,0 +1,74 @@ + + * @author Konrad Abicht + * @license W3C Software License and GPL + * @homepage + */ + +namespace ARC2\Store\Adapter; + +abstract class AbstractAdapter +{ + protected $configuration; + protected $db; + + /** + * Stores errors of failed queries. + * + * @var array + */ + protected $errors = array(); + + /** + * Sent queries. + * + * @var array + */ + protected $queries = array(); + + /** + * @param array $configuration Default is array(). Only use, if you have your own mysqli connection. + */ + public function __construct(array $configuration = array()) + { + $this->configuration = $configuration; + + $this->checkRequirements(); + } + + abstract public function checkRequirements(); + + abstract public function connect($existingConnection = null); + + abstract public function disconnect(); + + abstract public function escape($value); + + abstract public function exec($sql); + + abstract public function fetchList($sql); + + abstract public function fetchRow($sql); + + abstract public function getAdapterName(); + + abstract public function getCollation(); + + abstract public function getDBSName(); + + abstract public function getLastInsertId(); + + abstract public function getServerInfo(); + + abstract public function getErrorMessage(); + + abstract public function getNumberOfRows($sql); + + abstract public function getStoreName(); + + abstract public function getTablePrefix(); + + abstract public function simpleQuery($sql); +} diff --git a/src/ARC2/Store/Adapter/AdapterFactory.php b/src/ARC2/Store/Adapter/AdapterFactory.php new file mode 100644 index 00000000..9ace7c39 --- /dev/null +++ b/src/ARC2/Store/Adapter/AdapterFactory.php @@ -0,0 +1,65 @@ + + * @author Konrad Abicht + * @license W3C Software License and GPL + * @homepage + */ + +namespace ARC2\Store\Adapter; + +/** + * It provides an adapter instance for requested adapter name. + */ +class AdapterFactory +{ + /** + * @param string $adapterName + * @param array $configuration Default is array() + */ + public function getInstanceFor($adapterName, $configuration = array()) + { + if (in_array($adapterName, $this->getSupportedAdapters())) { + /* + * mysqli + */ + if ('mysqli' == $adapterName) { + if (false == class_exists('\\ARC2\\Store\\Adapter\\mysqliAdapter')) { + require_once 'mysqliAdapter.php'; + } + return new mysqliAdapter($configuration); + /* + * PDO + */ + } elseif ('pdo' == $adapterName) { + // use cache? + if (isset($configuration['cache_enabled']) && true === $configuration['cache_enabled']) { + if (false == class_exists('\\ARC2\\Store\\Adapter\\CachedPDOAdapter')) { + require_once 'CachedPDOAdapter.php'; + } + return new CachedPDOAdapter($configuration); + // no cache + } else { + if (false == class_exists('\\ARC2\\Store\\Adapter\\PDOAdapter')) { + require_once 'PDOAdapter.php'; + } + return new PDOAdapter($configuration); + } + } + } + + throw new \Exception( + 'Unknown adapter name given. Currently supported are: ' + .implode(', ', $this->getSupportedAdapters()) + ); + } + + /** + * @return array + */ + public function getSupportedAdapters() + { + return array('mysqli', 'pdo'); + } +} diff --git a/src/ARC2/Store/Adapter/CachedPDOAdapter.php b/src/ARC2/Store/Adapter/CachedPDOAdapter.php new file mode 100644 index 00000000..f4c805e5 --- /dev/null +++ b/src/ARC2/Store/Adapter/CachedPDOAdapter.php @@ -0,0 +1,145 @@ + + * @author Konrad Abicht + * @license W3C Software License and GPL + * @homepage + */ + +namespace ARC2\Store\Adapter; + +use Psr\SimpleCache\CacheInterface; +use Symfony\Component\Cache\Simple\FilesystemCache; + +/** + * PDO Adapter - Handles database operations using PDO. + */ +class CachedPDOAdapter extends PDOAdapter +{ + protected $cacheEnabled; + protected $cache; + + public function __construct(array $configuration = array()) + { + parent::__construct($configuration); + + $this->initCache($configuration); + } + + protected function initCache(array $configuration) + { + $this->cacheEnabled = isset($configuration['cache_enabled']) + && true === $configuration['cache_enabled']; + + if ($this->cacheEnabled) { + // reuse existing cache instance, if it implements Psr\SimpleCache\CacheInterface + if (isset($configuration['cache_instance']) + && $configuration['cache_instance'] instanceof CacheInterface) { + $this->cache = $configuration['cache_instance']; + + // create new cache instance + } else { + // FYI: https://symfony.com/doc/current/components/cache/adapters/filesystem_adapter.html + $this->cache = new FilesystemCache('arc2', 0, null); + } + } else { + throw new \Exception('Cache not enabled, therefore CachedPDOAdapter can not be used.'); + } + } + + public function clearCache() + { + $this->cache->clear(); + } + + /** + * @param string $sql + * + * @return array + */ + public function fetchList($sql) + { + $key = hash('sha1', $sql); + + // sql query is known + if ($this->cache->has($key)) { + return $this->cache->get($key); + + } else { + $result = parent::fetchList($sql); + $this->cache->set($key, $result); + return $result; + } + } + + /** + * @param string $sql + * + * @return array + */ + public function fetchRow($sql) + { + $key = hash('sha1', $sql); + + // sql query is known + if ($this->cache->has($key)) { + return $this->cache->get($key); + + } else { + $result = parent::fetchRow($sql); + $this->cache->set($key, $result); + return $result; + } + } + + public function getCacheInstance() + { + return $this->cache; + } + + public function getNumberOfRows($sql) + { + $key = hash('sha1', $sql); + + // sql query is known + if ($this->cache->has($key)) { + return $this->cache->get($key); + + } else { + $result = parent::getNumberOfRows($sql); + $this->cache->set($key, $result); + return $result; + } + } + + /** + * catches the first part of the query + * we need that to determine if its an query which changes the DB in any way + */ + protected function queryChangesDb($sql) + { + $sqlPart = substr(trim($sql), 0, 4); + return true === in_array($sqlPart, ['CREA', 'DROP', 'DELE', 'INSE', 'RENA', 'UPDA',]); + } + + public function simpleQuery($sql) + { + if ($this->queryChangesDb($sql)) { + $this->cache->clear(); + } + + return parent::simpleQuery($sql); + } + + public function exec($sql) + { + if ($this->queryChangesDb($sql)) { + $this->cache->clear(); + } + + return parent::exec($sql); + } +} diff --git a/src/ARC2/Store/Adapter/MysqliDbExtended.php b/src/ARC2/Store/Adapter/MysqliDbExtended.php new file mode 100644 index 00000000..5a74cd51 --- /dev/null +++ b/src/ARC2/Store/Adapter/MysqliDbExtended.php @@ -0,0 +1,111 @@ +mysqli()->affected_rows; + } + + /** + * If you ran a query using MysqliDbExtended::simpleQuery and an error occoured, you can + * get the error code with this function. + * + * @return int Error code, if available. + */ + public function getErrorCode() + { + return $this->mysqli()->errno; + } + + /** + * If you ran a query using MysqliDbExtended::simpleQuery and an error occoured, you can + * get the error message with this function. + * + * @return string Non-empty string, if an error occoured, empty string otherwise. + */ + public function getErrorMessage() + { + return $this->mysqli()->error; + } + + /** + * @return int + */ + public function getLastInsertId() + { + if (is_object($this->last_result)) { + return $this->last_result->insert_id; + } + + return null; + } + + /** + * Executes a SQL statement and returns the number of rows. This function will return 0, + * regardless of errors in the query. + * + * @param string $sql Query to execute. + * + * @return int Number of rows, if available, 0 otherwise. + */ + public function getNumberOfRows($sql = null) + { + if (null != $sql) { + $result = $this->mysqli()->query($sql); + return is_object($result) ? $result->num_rows : 0; + + } elseif (is_object($this->last_result)) { + return $this->last_result->num_rows; + } + + return 0; + } + + /** + * Returns the server version. + * + * @return string + */ + public function getServerInfo() + { + return $this->mysqli()->server_info; + } + + /** + * For compatibility reasons. Executes a query using mysqli and returns the result. Dont use + * this function directly. It is only used once to make sure, ARC2 keeps its backward compatibility + * while in the 2.x branch. + * + * @param string $sql Query to execute. + * + * @return mysqli result|false + */ + public function mysqliQuery($sql) + { + return $this->mysqli()->query($sql); + } + + /** + * @param string $sql Query to execute. + * + * @return bool True if query runs without problems, false otherwise. + */ + public function simpleQuery($sql, $num_rows = null) + { + $this->last_result = $this->mysqli()->query($sql, $num_rows); + return $this->last_result ? true : false; + } +} diff --git a/src/ARC2/Store/Adapter/PDOAdapter.php b/src/ARC2/Store/Adapter/PDOAdapter.php new file mode 100644 index 00000000..7b245e23 --- /dev/null +++ b/src/ARC2/Store/Adapter/PDOAdapter.php @@ -0,0 +1,320 @@ + + * @author Konrad Abicht + * @license W3C Software License and GPL + * @homepage + */ + +namespace ARC2\Store\Adapter; + +/** + * PDO Adapter - Handles database operations using PDO. + */ +class PDOAdapter extends AbstractAdapter +{ + public function checkRequirements() + { + if (false == \extension_loaded('pdo_mysql')) { + throw new \Exception('Extension pdo_mysql is not loaded.'); + } + } + + public function getAdapterName() + { + return 'pdo'; + } + + /** + * Connect to server or storing a given connection. + * + * @param EasyDB $existingConnection Default is null. + */ + public function connect($existingConnection = null) + { + // reuse a given existing connection. + // it assumes that $existingConnection is a PDO connection object + if (null !== $existingConnection) { + $this->db = $existingConnection; + + // create your own connection + } elseif (false === $this->db instanceof \PDO) { + /* + * build connection string + * + * - db_pdo_protocol: Protocol to determine server, e.g. mysql + */ + if (false == isset($this->configuration['db_pdo_protocol'])) { + throw new \Exception( + 'When using PDO the protocol has to be given (e.g. mysql). Please set db_pdo_protocol in database configuration.' + ); + } + $dsn = $this->configuration['db_pdo_protocol'].':host='. $this->configuration['db_host']; + if (isset($this->configuration['db_name'])) { + $dsn .= ';dbname='.$this->configuration['db_name']; + } + + // set charset + $dsn .= ';charset=utf8mb4'; + + $this->db = new \PDO( + $dsn, + $this->configuration['db_user'], + $this->configuration['db_pwd'] + ); + + $this->db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); + + // errors DONT lead to exceptions + // set to false for compatibility reasons with mysqli. ARC2 using mysqli does not throw any + // exceptions, instead collects errors in a hidden array. + $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + + // default fetch mode is associative + $this->db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); + + // from source: http://php.net/manual/de/ref.pdo-mysql.php + // If this attribute is set to TRUE on a PDOStatement, the MySQL driver will use + // the buffered versions of the MySQL API. But we wont rely on that, setting it false. + $this->db->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); + + // This is RDF, we may need many JOINs... + // TODO find an equivalent in other DBS + $stmt = $this->db->prepare('SET SESSION SQL_BIG_SELECTS=1'); + $stmt->execute(); + $stmt->closeCursor(); + + // with MySQL 5.6 we ran into exceptions like: + // PDOException: SQLSTATE[42000]: Syntax error or access violation: + // 1140 In aggregated query without GROUP BY, expression #1 of SELECT list contains + // nonaggregated column 'testdb.T_0_0_0.p'; this is incompatible with sql_mode=only_full_group_by + // + // the following query makes this right. + // FYI: https://stackoverflow.com/questions/23921117/disable-only-full-group-by + $stmt = $this->db->prepare("SET sql_mode = ''"); + $stmt->execute(); + $stmt->closeCursor(); + } + + return $this->db; + } + + /** + * @return void + */ + public function disconnect() + { + // FYI: https://stackoverflow.com/questions/18277233/pdo-closing-connection + $this->db = null; + } + + public function escape($value) + { + // quote surronds the string with ', but using trim aligns the result + return \trim($this->db->quote($value), "'"); + } + + /** + * @param string $sql + * + * @return array + */ + public function fetchList($sql) + { + // save query + $this->queries[] = [ + 'query' => $sql, + 'by_function' => 'fetchList' + ]; + + if (null == $this->db) { + $this->connect(); + } + + $stmt = $this->db->prepare($sql); + $stmt->execute(); + $rows = $stmt->fetchAll(); + $stmt->closeCursor(); + + return $rows; + } + + public function fetchRow($sql) + { + // save query + $this->queries[] = [ + 'query' => $sql, + 'by_function' => 'fetchRow' + ]; + + if (null == $this->db) { + $this->connect(); + } + + $row = false; + $stmt = $this->db->prepare($sql); + $stmt->execute(); + $rows = $stmt->fetchAll(); + if (0 < \count($rows)) { + $row = \array_values($rows)[0]; + } + $stmt->closeCursor(); + + return $row; + } + + public function getCollation() + { + $row = $this->fetchRow('SHOW TABLE STATUS LIKE "'.$this->getTablePrefix().'setting"'); + + if (isset($row['Collation'])) { + return $row['Collation']; + } else { + return ''; + } + } + + public function getConnection() + { + return $this->db; + } + + public function getConnectionId() + { + return $this->db->query('SELECT CONNECTION_ID()')->fetch(\PDO::FETCH_ASSOC); + } + + public function getDBSName() + { + if (null == $this->db) { + return; + } + + $clientVersion = \strtolower($this->db->getAttribute(\PDO::ATTR_CLIENT_VERSION)); + $serverVersion = \strtolower($this->db->getAttribute(\PDO::ATTR_SERVER_VERSION)); + if (false !== \strpos($clientVersion, 'mariadb') || false !== \strpos($serverVersion, 'mariadb')) { + $return = 'mariadb'; + } elseif (false !== \strpos($clientVersion, 'mysql') || false !== \strpos($serverVersion, 'mysql')) { + $return = 'mysql'; + } else { + $return = null; + } + + return $return; + } + + public function getServerInfo() + { + return $this->db->getAttribute(\constant('PDO::ATTR_CLIENT_VERSION')); + } + + /** + * Returns the version of the database server like 05-00-12 + */ + public function getServerVersion() + { + $res = \preg_match( + "/([0-9]+)\.([0-9]+)\.([0-9]+)/", + $this->getServerInfo(), + $matches + ); + + return 1 == $res + ? \sprintf('%02d-%02d-%02d', $matches[1], $matches[2], $matches[3]) + : '00-00-00'; + } + + public function getErrorCode() + { + return $this->db->errorCode(); + } + + public function getErrorMessage() + { + return $this->db->errorInfo()[2]; + } + + public function getLastInsertId() + { + return $this->db->lastInsertId(); + } + + public function getNumberOfRows($sql) + { + // save query + $this->queries[] = [ + 'query' => $sql, + 'by_function' => 'getNumberOfRows' + ]; + + $stmt = $this->db->prepare($sql); + $stmt->execute(); + $rowCount = \count($stmt->fetchAll()); + $stmt->closeCursor(); + return $rowCount; + } + + public function getStoreName() + { + if (isset($this->configuration['store_name'])) { + return $this->configuration['store_name']; + } + + return 'arc'; + } + + public function getTablePrefix() + { + $prefix = ''; + if (isset($this->configuration['db_table_prefix'])) { + $prefix = $this->configuration['db_table_prefix'].'_'; + } + + $prefix .= $this->getStoreName().'_'; + return $prefix; + } + + /** + * @param string $sql Query + * + * @return bool True if query ran fine, false otherwise. + */ + public function simpleQuery($sql) + { + // save query + $this->queries[] = [ + 'query' => $sql, + 'by_function' => 'simpleQuery' + ]; + + if (false === $this->db instanceof \PDO) { + $this->connect(); + } + + $stmt = $this->db->prepare($sql); + $stmt->execute(); + $stmt->closeCursor(); + return true; + } + + /** + * Encapsulates internal PDO::exec call. This allows us to extend it, e.g. with caching functionality. + * + * @param string $sql + * + * @return int Number of affected rows. + */ + public function exec($sql) + { + // save query + $this->queries[] = [ + 'query' => $sql, + 'by_function' => 'exec' + ]; + + return $this->db->exec($sql); + } +} diff --git a/src/ARC2/Store/Adapter/mysqliAdapter.php b/src/ARC2/Store/Adapter/mysqliAdapter.php new file mode 100644 index 00000000..adaf0135 --- /dev/null +++ b/src/ARC2/Store/Adapter/mysqliAdapter.php @@ -0,0 +1,255 @@ + + * @author Konrad Abicht + * @license W3C Software License and GPL + * @homepage + */ + +namespace ARC2\Store\Adapter; + +/** + * mysqli Adapter - Handles database operations using mysqli. + */ +class mysqliAdapter extends AbstractAdapter +{ + protected $last_result; + + public function checkRequirements() + { + if (false == \extension_loaded('mysqli') || false == \function_exists('mysqli_connect')) { + throw new \Exception('Extension mysqli is not loaded or function mysqli_connect is not available.'); + } + } + + public function getAdapterName() + { + return 'mysqli'; + } + + /** + * Connect to server or storing a given connection. + * + * @return string|MysqliDbExtended String if an error occoured, instance of MysqliDbExtended otherwise. + */ + public function connect($existingConnection = null) + { + // reuse a given existing connection. + // it assumes that $existingConnection is a mysqli connection object + if (null !== $existingConnection) { + $this->db = new MysqliDbExtended($existingConnection); + + // create your own connection + } elseif (null == $this->db) { + // connect + try { + $this->db = new MysqliDbExtended( + $this->configuration['db_host'], + $this->configuration['db_user'], + $this->configuration['db_pwd'] + ); + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + if (isset($this->configuration['db_name']) + && true !== $this->db->simpleQuery('USE `'.$this->configuration['db_name'].'`')) { + $fixed = 0; + /* try to create it */ + if ($this->configuration['db_name']) { + $this->db->simpleQuery(' + CREATE DATABASE IF NOT EXISTS `'.$this->configuration['db_name'].'` + DEFAULT CHARACTER SET utf8 + DEFAULT COLLATE utf8_general_ci + ' + ); + if ($this->db->simpleQuery('USE `'.$this->configuration['db_name'].'`')) { + $this->db->simpleQuery("SET NAMES 'utf8'"); + $fixed = 1; + } + } + if (!$fixed) { + return $this->addError($this->db->getErrorMessage()); + } else { + if (preg_match('/^utf8/', $this->getCollation())) { + $this->db->simpleQuery("SET NAMES 'utf8'"); + } + // This is RDF, we may need many JOINs... + $this->db->simpleQuery('SET SESSION SQL_BIG_SELECTS=1'); + } + } + + return $this->db; + } + + public function disconnect() + { + return $this->db->disconnect(); + } + + public function escape($value) + { + return $this->db->escape($value); + } + + public function fetchList($sql) + { + return $this->db->rawQuery($sql); + } + + public function fetchRow($sql) + { + $row = $this->db->rawQueryOne($sql); + + return null != $row ? $row : false; + } + + public function getCollation() + { + $row = $this->fetchRow('SHOW TABLE STATUS LIKE "'.$this->getTablePrefix().'setting"'); + + if (isset($row['Collation'])) { + return $row['Collation']; + } else { + return ''; + } + } + + public function getConnectionId() + { + if (null != $this->db) { + return $this->db->mysqli()->thread_id; + } + } + + /** + * For backward compatibility reasons. Get mysqli connection object. + * + * @return mysqli + */ + public function getConnection() + { + return $this->db->mysqli(); + } + + public function getDBSName() + { + if (null == $this->db) { + return null; + } + + return false !== strpos($this->getServerInfo(), 'MariaDB') + ? 'mariadb' + : 'mysql'; + } + + public function getLastInsertId() + { + if (null != $this->db) { + return $this->db->getLastInsertId(); + } + + return 'No database connection (mysqliAdapter).'; + } + + public function getServerInfo() + { + $this->connect(); + + return $this->db->mysqli()->server_info; + } + + /** + * Returns the version of the database server like 05-00-12 + */ + public function getServerVersion() + { + $res = preg_match( + "/([0-9]+)\.([0-9]+)\.([0-9]+)/", + $this->getServerInfo(), + $matches + ); + + return 1 == $res + ? sprintf('%02d-%02d-%02d', $matches[1], $matches[2], $matches[3]) + : '00-00-00'; + } + + public function getErrorMessage() + { + return $this->db->getErrorMessage(); + } + + public function getErrorCode() + { + return $this->db->getErrorCode(); + } + + public function getNumberOfRows($sql) + { + return $this->db->getNumberOfRows($sql); + } + + public function getStoreName() + { + if (isset($this->configuration['store_name'])) { + return $this->configuration['store_name']; + } + + return 'arc'; + } + + public function getTablePrefix() + { + $prefix = ''; + if (isset($this->configuration['db_table_prefix'])) { + $prefix = $this->configuration['db_table_prefix'].'_'; + } + + $prefix .= $this->getStoreName().'_'; + return $prefix; + } + + /** + * For compatibility reasons. Executes a query using mysqli and returns the result. Dont use + * this function directly. It is only used once to make sure, ARC2 keeps its backward compatibility + * while in the 2.x branch. + * + * @param string $sql Query to execute. + * + * @return mysqli result|false + */ + public function mysqliQuery($sql) + { + return $this->db->mysqliQuery($sql); + } + + /** + * @param string $sql Query + * + * @return bool True if query ran fine, false otherwise. + */ + public function simpleQuery($sql) + { + if (null == $this->db) { + $this->connect(); + } + + return $this->db->simpleQuery($sql); + } + + /** + * @param string $sql Query with return of affected rows + * + * @return bool True if query ran fine, false otherwise. + */ + public function exec($sql) + { + $this->db->simpleQuery($sql); + return $this->db->getAffectedRows(); + } +} diff --git a/store/ARC2_Store.php b/store/ARC2_Store.php index 52aea8b2..3e40d5fd 100644 --- a/store/ARC2_Store.php +++ b/store/ARC2_Store.php @@ -10,13 +10,16 @@ class ARC2_Store extends ARC2_Class { + protected $cache; + protected $db; + public function __construct($a, &$caller) { parent::__construct($a, $caller); } public function __init() - {/* db_con */ + { parent::__init(); $this->table_lock = 0; $this->triggers = $this->v('store_triggers', [], $this->a); @@ -24,6 +27,27 @@ public function __init() $this->is_win = ('win' == strtolower(substr(PHP_OS, 0, 3))) ? true : false; $this->max_split_tables = $this->v('store_max_split_tables', 10, $this->a); $this->split_predicates = $this->v('store_split_predicates', [], $this->a); + + /* + * setup cache instance, if required by the user. + */ + if (isset($this->a['cache_enabled']) && true === $this->a['cache_enabled']) { + // reuse existing cache instance, if it implements Psr\SimpleCache\CacheInterface + if (isset($this->a['cache_instance']) + && $this->a['cache_instance'] instanceof \Psr\SimpleCache\CacheInterface) { + $this->cache = $this->a['cache_instance']; + + // create new cache instance + } else { + // FYI: https://symfony.com/doc/current/components/cache/adapters/filesystem_adapter.html + $this->cache = new \Symfony\Component\Cache\Simple\FilesystemCache('arc2', 0, null); + } + } + } + + public function cacheEnabled() + { + return isset($this->a['cache_enabled']) && true === $this->a['cache_enabled']; } public function getName() @@ -45,53 +69,65 @@ public function getTablePrefix() public function createDBCon() { + // build connection credential array foreach (['db_host' => 'localhost', 'db_user' => '', 'db_pwd' => '', 'db_name' => ''] as $k => $v) { $this->a[$k] = $this->v($k, $v, $this->a); } - if (!$db_con = mysqli_connect($this->a['db_host'], $this->a['db_user'], $this->a['db_pwd'])) { - return $this->addError(mysqli_error($db_con)); - } - $this->a['db_con'] = $db_con; - if (!mysqli_query($db_con, 'USE `'.$this->a['db_name'].'`')) { - $fixed = 0; - /* try to create it */ - if ($this->a['db_name']) { - $this->queryDB(' - CREATE DATABASE IF NOT EXISTS `'.$this->a['db_name'].'` - DEFAULT CHARACTER SET utf8 - DEFAULT COLLATE utf8_general_ci - ', $db_con, 1 - ); - if (mysqli_query($db_con, 'USE `'.$this->a['db_name'].'`')) { - $this->queryDB("SET NAMES 'utf8'", $db_con); - $fixed = 1; - } + + // connect + try { + if (false === class_exists('\\ARC2\\Store\\Adapter\\AdapterFactory')) { + require __DIR__.'/../src/ARC2/Store/Adapter/AdapterFactory.php'; } - if (!$fixed) { - return $this->addError(mysqli_error($db_con)); + if (false == isset($this->a['db_adapter'])) { + $this->a['db_adapter'] = 'mysqli'; } + $factory = new \ARC2\Store\Adapter\AdapterFactory(); + $this->db = $factory->getInstanceFor($this->a['db_adapter'], $this->a); + $err = $this->db->connect(); + // stop here, if an error occoured + if (is_string($err) && false !== empty($err)) { + throw new \Exception($err); + } + } catch (\Exception $e) { + return $this->addError($e->getMessage()); } - if (preg_match('/^utf8/', $this->getCollation())) { - $this->queryDB("SET NAMES 'utf8'", $db_con); + + if ('mysqli' == $this->db->getAdapterName()) { + $this->a['db_con'] = $this->db->getConnection(); } - // This is RDF, we may need many JOINs... - $this->queryDB('SET SESSION SQL_BIG_SELECTS=1', $db_con); + + $this->a['db_object'] = $this->db; return true; } + public function getDBObject() + { + return $this->db; + } + + /** + * @param int $force 1 if you want to force a connection. + * + * @return mysqli mysqli-connection, only if mysqli adapter was selected. null otherwise, + * because direct access to DB connection is not recommended. + */ public function getDBCon($force = 0) { - if ($force || !isset($this->a['db_con'])) { + if ($force || !isset($this->a['db_object'])) { if (!$this->createDBCon()) { return false; } } - if (!$force && !mysqli_thread_id($this->a['db_con'])) { - return $this->getDBCon(1); - } - return $this->a['db_con']; + if ('mysqli' == $this->a['db_adapter']) { + // for backward compatibility reasons only. + // TODO remove that in 3.x + return $this->a['db_con']; + } else { + return true; + } } /** @@ -99,34 +135,51 @@ public function getDBCon($force = 0) */ public function closeDBCon() { - if ($this->v('db_con', false, $this->a)) { - mysqli_close($this->a['db_con']); + if (isset($this->a['db_object'])) { + $this->db->disconnect(); } unset($this->a['db_con']); + unset($this->a['db_object']); } public function getDBVersion() { if (!$this->v('db_version')) { - $this->db_version = preg_match("/^([0-9]+)\.([0-9]+)\.([0-9]+)/", mysqli_get_server_info($this->getDBCon()), $m) ? sprintf('%02d-%02d-%02d', $m[1], $m[2], $m[3]) : '00-00-00'; + // connect, if no connection available + if (null == $this->db) { + $this->createDBCon(); + } + + $this->db_version = $this->db->getServerVersion(); } return $this->db_version; } - public function getCollation() + /** + * @return string Returns DBS name. Possible values: mysql, mariadb + */ + public function getDBSName() { - $rs = $this->queryDB('SHOW TABLE STATUS LIKE "'.$this->getTablePrefix().'setting"', $this->getDBCon()); + return $this->db->getDBSName(); + } - return ($rs && ($row = mysqli_fetch_array($rs)) && isset($row['Collation'])) ? $row['Collation'] : ''; + public function getCollation() + { + $row = $this->db->fetchRow('SHOW TABLE STATUS LIKE "'.$this->getTablePrefix().'setting"'); + return isset($row['Collation']) ? $row['Collation'] : ''; } public function getColumnType() { if (!$this->v('column_type')) { $tbl = $this->getTablePrefix().'g2t'; - $rs = $this->queryDB('SHOW COLUMNS FROM '.$tbl.' LIKE "t"', $this->getDBCon()); - $row = $rs ? mysqli_fetch_array($rs) : ['Type' => 'mediumint']; + + $row = $this->db->fetchRow('SHOW COLUMNS FROM '.$tbl.' LIKE "t"'); + if (null == $row) { + $row = ['Type' => 'mediumint']; + } + $this->column_type = preg_match('/mediumint/', $row['Type']) ? 'mediumint' : 'int'; } @@ -138,8 +191,9 @@ public function hasHashColumn($tbl) $var_name = 'has_hash_column_'.$tbl; if (!isset($this->$var_name)) { $tbl = $this->getTablePrefix().$tbl; - $rs = $this->queryDB('SHOW COLUMNS FROM '.$tbl.' LIKE "val_hash"', $this->getDBCon()); - $this->$var_name = ($rs && mysqli_fetch_array($rs)); + + $row = $this->db->fetchRow('SHOW COLUMNS FROM '.$tbl.' LIKE "val_hash"'); + $this->$var_name = null !== $row; } return $this->$var_name; @@ -150,8 +204,9 @@ public function hasFulltextIndex() if (!isset($this->has_fulltext_index)) { $this->has_fulltext_index = 0; $tbl = $this->getTablePrefix().'o2val'; - $rs = $this->queryDB('SHOW INDEX FROM '.$tbl, $this->getDBCon()); - while ($row = mysqli_fetch_array($rs)) { + + $rows = $this->db->fetchList('SHOW INDEX FROM '.$tbl); + foreach($rows as $row) { if ('val' != $row['Column_name']) { continue; } @@ -172,7 +227,7 @@ public function enableFulltextSearch() return 1; } $tbl = $this->getTablePrefix().'o2val'; - $this->queryDB('CREATE FULLTEXT INDEX vft ON '.$tbl.'(val(128))', $this->getDBCon(), 1); + $this->db->simpleQuery('CREATE FULLTEXT INDEX vft ON '.$tbl.'(val(128))'); } public function disableFulltextSearch() @@ -181,24 +236,29 @@ public function disableFulltextSearch() return 1; } $tbl = $this->getTablePrefix().'o2val'; - $this->queryDB('DROP INDEX vft ON '.$tbl, $this->getDBCon()); + $this->db->simpleQuery('DROP INDEX vft ON '.$tbl); } public function countDBProcesses() { - return ($rs = $this->queryDB('SHOW PROCESSLIST', $this->getDBCon())) ? mysqli_num_rows($rs) : 0; + return $this->db->getNumberOfRows('SHOW PROCESSLIST'); } + /** + * Manipulating database processes using ARC2 is discouraged. + * + * @deprecated + */ public function killDBProcesses($needle = '', $runtime = 30) { - $dbcon = $this->getDBCon(); /* make sure needle is sql */ if (preg_match('/\?.+ WHERE/i', $needle, $m)) { $needle = $this->query($needle, 'sql'); } - $rs = $this->queryDB('SHOW FULL PROCESSLIST', $dbcon); $ref_tbl = $this->getTablePrefix().'triple'; - while ($row = mysqli_fetch_array($rs)) { + + $rows = $this->db->fetchList('SHOW FULL PROCESSLIST'); + foreach ($rows as $row) { if ($row['Time'] < $runtime) { continue; } @@ -218,7 +278,7 @@ public function killDBProcesses($needle = '', $runtime = 30) if (!$kill) { continue; } - $this->queryDB('KILL '.$row['Id'], $dbcon); + $this->db->simpleQuery('KILL '.$row['Id']); } } @@ -229,20 +289,25 @@ public function getTables() public function isSetUp() { - if (($con = $this->getDBCon())) { + if (null !== $this->db) { $tbl = $this->getTablePrefix().'setting'; - return $this->queryDB('SELECT 1 FROM '.$tbl.' LIMIT 0', $con) ? 1 : 0; + try { + // mysqli way + return $this->db->simpleQuery('SELECT 1 FROM '.$tbl.' LIMIT 0') ? 1 : 0; + } catch (\Exception $e) { + // when using PDO, an exception gets thrown if $tbl does not exist. + $this->errors[] = $e->getMessage(); + return 0; + } } + + return 0; } public function setUp($force = 0) { - if (($force || !$this->isSetUp()) && ($con = $this->getDBCon())) { - if ($this->getDBVersion() < '04-00-04') { - /* UPDATE + JOINs */ - return $this->addError('MySQL version not supported. ARC requires version 4.0.4 or higher.'); - } + if (($force || !$this->isSetUp()) && false !== $this->getDBCon()) { ARC2::inc('StoreTableManager'); $mgr = new ARC2_StoreTableManager($this->a, $this); $mgr->createTables(); @@ -266,19 +331,26 @@ public function splitTables() public function hasSetting($k) { + if (null == $this->db) { + $this->createDBCon(); + } + $tbl = $this->getTablePrefix().'setting'; - $sql = 'SELECT val FROM '.$tbl." WHERE k = '".md5($k)."'"; - $rs = $this->queryDB($sql, $this->getDBCon()); - return ($rs && ($row = mysqli_fetch_array($rs))) ? 1 : 0; + return $this->db->fetchRow('SELECT val FROM '.$tbl." WHERE k = '".md5($k)."'") + ? 1 + : 0; } public function getSetting($k, $default = 0) { + if (null == $this->db) { + $this->createDBCon(); + } + $tbl = $this->getTablePrefix().'setting'; - $sql = 'SELECT val FROM '.$tbl." WHERE k = '".md5($k)."'"; - $rs = $this->queryDB($sql, $this->getDBCon()); - if ($rs && ($row = mysqli_fetch_array($rs))) { + $row = $this->db->fetchRow('SELECT val FROM '.$tbl." WHERE k = '".md5($k)."'"); + if (isset($row['val'])) { return unserialize($row['val']); } @@ -287,22 +359,21 @@ public function getSetting($k, $default = 0) public function setSetting($k, $v) { - $con = $this->getDBCon(); $tbl = $this->getTablePrefix().'setting'; if ($this->hasSetting($k)) { - $sql = 'UPDATE '.$tbl." SET val = '".mysqli_real_escape_string($con, serialize($v))."' WHERE k = '".md5($k)."'"; + $sql = 'UPDATE '.$tbl." SET val = '".$this->db->escape(serialize($v))."' WHERE k = '".md5($k)."'"; } else { - $sql = 'INSERT INTO '.$tbl." (k, val) VALUES ('".md5($k)."', '".mysqli_real_escape_string($con, serialize($v))."')"; + $sql = 'INSERT INTO '.$tbl." (k, val) VALUES ('".md5($k)."', '".$this->db->escape(serialize($v))."')"; } - return $this->queryDB($sql, $con); + return $this->db->simpleQuery($sql); } public function removeSetting($k) { $tbl = $this->getTablePrefix().'setting'; - return $this->queryDB('DELETE FROM '.$tbl." WHERE k = '".md5($k)."'", $this->getDBCon()); + return $this->db->simpleQuery('DELETE FROM '.$tbl." WHERE k = '".md5($k)."'"); } public function getQueueTicket() @@ -311,14 +382,13 @@ public function getQueueTicket() return 1; } $t = 'ticket_'.substr(md5(uniqid(rand())), 0, 10); - $con = $this->getDBCon(); /* lock */ - $rs = $this->queryDB('LOCK TABLES '.$this->getTablePrefix().'setting WRITE', $con); + $this->db->simpleQuery('LOCK TABLES '.$this->getTablePrefix().'setting WRITE'); /* queue */ $queue = $this->getSetting('query_queue', []); $queue[] = $t; $this->setSetting('query_queue', $queue); - $this->queryDB('UNLOCK TABLES', $con); + $this->db->simpleQuery('UNLOCK TABLES'); /* loop */ $lc = 0; $queue = $this->getSetting('query_queue', []); @@ -341,27 +411,25 @@ public function removeQueueTicket($t) if (!$this->queue_queries) { return 1; } - $con = $this->getDBCon(); /* lock */ - $this->queryDB('LOCK TABLES '.$this->getTablePrefix().'setting WRITE', $con); + $this->db->simpleQuery('LOCK TABLES '.$this->getTablePrefix().'setting WRITE'); /* queue */ $vals = $this->getSetting('query_queue', []); $pos = array_search($t, $vals); $queue = ($pos < (count($vals) - 1)) ? array_slice($vals, $pos + 1) : []; $this->setSetting('query_queue', $queue); - $this->queryDB('UNLOCK TABLES', $con); + $this->db->simpleQuery('UNLOCK TABLES'); } public function reset($keep_settings = 0) { - $con = $this->getDBCon(); $tbls = $this->getTables(); $prefix = $this->getTablePrefix(); /* remove split tables */ $ps = $this->getSetting('split_predicates', []); foreach ($ps as $p) { $tbl = 'triple_'.abs(crc32($p)); - $this->queryDB('DROP TABLE '.$prefix.$tbl, $con); + $this->db->simpleQuery('DROP TABLE '.$prefix.$tbl); } $this->removeSetting('split_predicates'); /* truncate tables */ @@ -369,17 +437,20 @@ public function reset($keep_settings = 0) if ($keep_settings && ('setting' == $tbl)) { continue; } - $this->queryDB('TRUNCATE '.$prefix.$tbl, $con); + $this->db->simpleQuery('TRUNCATE '.$prefix.$tbl); } } public function drop() { - $con = $this->getDBCon(); - $tbls = $this->getTables(); + if (null == $this->db) { + $this->createDBCon(); + } + $prefix = $this->getTablePrefix(); + $tbls = $this->getTables(); foreach ($tbls as $tbl) { - $this->queryDB('DROP TABLE '.$prefix.$tbl, $con); + $this->db->simpleQuery('DROP TABLE IF EXISTS '.$prefix.$tbl); } } @@ -429,17 +500,15 @@ public function createBackup($path, $q = '') public function renameTo($name) { - $con = $this->getDBCon(); $tbls = $this->getTables(); $old_prefix = $this->getTablePrefix(); $new_prefix = $this->v('db_table_prefix', '', $this->a); $new_prefix .= $new_prefix ? '_' : ''; $new_prefix .= $name.'_'; foreach ($tbls as $tbl) { - $rs = $this->queryDB('RENAME TABLE '.$old_prefix.$tbl.' TO '.$new_prefix.$tbl, $con); - $err = mysqli_error($con); - if (!empty($err)) { - return $this->addError($err); + $this->db->simpleQuery('RENAME TABLE '.$old_prefix.$tbl.' TO '.$new_prefix.$tbl); + if (!empty($this->db->getErrorMessage())) { + return $this->addError($this->db->getErrorMessage()); } } $this->a['store_name'] = $name; @@ -452,48 +521,89 @@ public function replicateTo($name) $new_store = ARC2::getStore($conf); $new_store->setUp(); $new_store->reset(); - $con = $this->getDBCon(); $tbls = $this->getTables(); $old_prefix = $this->getTablePrefix(); $new_prefix = $new_store->getTablePrefix(); foreach ($tbls as $tbl) { - $rs = $this->queryDB('INSERT IGNORE INTO '.$new_prefix.$tbl.' SELECT * FROM '.$old_prefix.$tbl, $con); - $err = mysqli_error($con); - if (!empty($err)) { - return $this->addError($err); + $this->db->simpleQuery('INSERT IGNORE INTO '.$new_prefix.$tbl.' SELECT * FROM '.$old_prefix.$tbl); + if (!empty($this->db->getErrorMessage())) { + return $this->addError($this->db->getErrorMessage()); } } return $new_store->query('SELECT COUNT(*) AS t_count WHERE { ?s ?p ?o}', 'row'); } + /** + * Executes a SPARQL query. + * + * @param string $q SPARQL query + * @param string $result_format Possible values: infos, raw, rows, row + * @param string $src + * @param int $keep_bnode_ids Keep blank node IDs? Default is 0 + * @param int $log_query Log executed queries? Default is 0 + * + * @return array|int Array if query returned a result, 0 otherwise. + */ public function query($q, $result_format = '', $src = '', $keep_bnode_ids = 0, $log_query = 0) { if ($log_query) { $this->logQuery($q); } - $con = $this->getDBCon(); if (preg_match('/^dump/i', $q)) { $infos = ['query' => ['type' => 'dump']]; } else { - ARC2::inc('SPARQLPlusParser'); - $p = new ARC2_SPARQLPlusParser($this->a, $this); - $p->parse($q, $src); - $infos = $p->getQueryInfos(); + // check cache + $key = \hash('sha1', $q); + if ($this->cacheEnabled() && $this->cache->has($key.'_infos')) { + $infos = $this->cache->get($key.'_infos'); + $errors = $this->cache->get($key.'_errors'); + // no entry found + } else { + ARC2::inc('SPARQLPlusParser'); + $p = new ARC2_SPARQLPlusParser($this->a, $this); + $p->parse($q, $src); + $infos = $p->getQueryInfos(); + $errors = $p->getErrors(); + + // store result in cache + if ($this->cacheEnabled()) { + $this->cache->set($key.'_infos', $infos); + $this->cache->set($key.'_errors', $errors); + } + } } + if ('infos' == $result_format) { return $infos; } + $infos['result_format'] = $result_format; - if (!isset($p) || !$p->getErrors()) { + + if (!isset($p) || 0 == count($errors)) { $qt = $infos['query']['type']; if (!in_array($qt, ['select', 'ask', 'describe', 'construct', 'load', 'insert', 'delete', 'dump'])) { return $this->addError('Unsupported query type "'.$qt.'"'); } $t1 = ARC2::mtime(); - $r = ['query_type' => $qt, 'result' => $this->runQuery($infos, $qt, $keep_bnode_ids, $q)]; - $t2 = ARC2::mtime(); - $r['query_time'] = $t2 - $t1; + + // if cache is enabled, get/store result + $key = \hash('sha1', $q); + if ($this->cacheEnabled() && $this->cache->has($key)) { + $result = $this->cache->get($key); + + } else { + $result = $this->runQuery($infos, $qt, $keep_bnode_ids, $q); + + // store in cache, if enabled + if ($this->cacheEnabled()) { + $this->cache->set($key, $result); + } + } + + $r = ['query_type' => $qt, 'result' => $result]; + $r['query_time'] = ARC2::mtime() - $t1; + /* query result */ if ('raw' == $result_format) { return $r['result']; @@ -511,8 +621,16 @@ public function query($q, $result_format = '', $src = '', $keep_bnode_ids = 0, $ return 0; } + /** + * Runs a SPARQL query. Dont use this function directly, use query instead. + */ public function runQuery($infos, $type, $keep_bnode_ids = 0, $q = '') { + // invalidate cache, if enabled and a query is executed, which changes the store + if ($this->cacheEnabled() && in_array($type, ['load', 'insert', 'delete'])) { + $this->cache->clear(); + } + ARC2::inc('Store'.ucfirst($type).'QueryHandler'); $cls = 'ARC2_Store'.ucfirst($type).'QueryHandler'; $h = new $cls($this->a, $this); @@ -588,18 +706,15 @@ public function getTermID($val, $term = '') if ((strlen($val) < 100) && isset($this->term_id_cache[$term][$val])) { return $this->term_id_cache[$term][$val]; } - $con = $this->getDBCon(); $r = 0; /* via hash */ if (preg_match('/^(s2val|o2val)$/', $tbl) && $this->hasHashColumn($tbl)) { - $sql = 'SELECT id, val FROM '.$this->getTablePrefix().$tbl." WHERE val_hash = '".$this->getValueHash($val)."' ORDER BY id"; - $rs = $this->queryDB($sql, $con); - if (!$rs || !mysqli_num_rows($rs)) {// try 32 bit version - $sql = 'SELECT id, val FROM '.$this->getTablePrefix().$tbl." WHERE val_hash = '".$this->getValueHash($val, true)."' ORDER BY id"; - $rs = $this->queryDB($sql, $con); - } - if (($rs = $this->queryDB($sql, $con)) && mysqli_num_rows($rs)) { - while ($row = mysqli_fetch_array($rs)) { + + $rows = $this->db->fetchList( + 'SELECT id, val FROM '.$this->getTablePrefix().$tbl." WHERE val_hash = '".$this->getValueHash($val)."' ORDER BY id" + ); + if (is_array($rows) && 0 < count($rows)) { + foreach($rows as $row) { if ($row['val'] == $val) { $r = $row['id']; break; @@ -609,8 +724,10 @@ public function getTermID($val, $term = '') } /* exact match */ else { - $sql = 'SELECT id FROM '.$this->getTablePrefix().$tbl." WHERE val = BINARY '".mysqli_real_escape_string($con, $val)."' LIMIT 1"; - if (($rs = $this->queryDB($sql, $con)) && mysqli_num_rows($rs) && ($row = mysqli_fetch_array($rs))) { + $sql = 'SELECT id FROM '.$this->getTablePrefix().$tbl." WHERE val = BINARY '".$this->db->escape($val)."' LIMIT 1"; + $row = $this->db->fetchRow($sql); + + if (null !== $row && isset($row['id'])) { $r = $row['id']; } } @@ -624,9 +741,10 @@ public function getTermID($val, $term = '') public function getIDValue($id, $term = '') { $tbl = preg_match('/^(s|o)$/', $term) ? $term.'2val' : 'id2val'; - $con = $this->getDBCon(); - $sql = 'SELECT val FROM '.$this->getTablePrefix().$tbl.' WHERE id = '.mysqli_real_escape_string($con, $id).' LIMIT 1'; - if (($rs = $this->queryDB($sql, $con)) && mysqli_num_rows($rs) && ($row = mysqli_fetch_array($rs))) { + $row = $this->db->fetchRow( + 'SELECT val FROM '.$this->getTablePrefix().$tbl.' WHERE id = '.$this->db->escape($id).' LIMIT 1' + ); + if (isset($row['val'])) { return $row['val']; } @@ -638,11 +756,11 @@ public function getLock($t_out = 10, $t_out_init = '') if (!$t_out_init) { $t_out_init = $t_out; } - $con = $this->getDBCon(); + $l_name = $this->a['db_name'].'.'.$this->getTablePrefix().'.write_lock'; - $rs = $this->queryDB('SELECT IS_FREE_LOCK("'.$l_name.'") AS success', $con); - if ($rs) { - $row = mysqli_fetch_array($rs); + $row = $this->db->fetchRow('SELECT IS_FREE_LOCK("'.$l_name.'") AS success'); + + if (is_array($row)) { if (!$row['success']) { if ($t_out) { sleep(1); @@ -650,10 +768,8 @@ public function getLock($t_out = 10, $t_out_init = '') return $this->getLock($t_out - 1, $t_out_init); } } else { - $rs = $this->queryDB('SELECT GET_LOCK("'.$l_name.'", '.$t_out_init.') AS success', $con); - if ($rs) { - $row = mysqli_fetch_array($rs); - + $row = $this->db->fetchRow('SELECT GET_LOCK("'.$l_name.'", '.$t_out_init.') AS success'); + if (isset($row['success'])) { return $row['success']; } } @@ -664,14 +780,17 @@ public function getLock($t_out = 10, $t_out_init = '') public function releaseLock() { - $con = $this->getDBCon(); - - return $this->queryDB('DO RELEASE_LOCK("'.$this->a['db_name'].'.'.$this->getTablePrefix().'.write_lock")', $con); + return $this->db->simpleQuery('DO RELEASE_LOCK("'.$this->a['db_name'].'.'.$this->getTablePrefix().'.write_lock")'); } public function processTables($level = 2, $operation = 'optimize') - {/* 1: triple + g2t, 2: triple + *2val, 3: all tables */ - $con = $this->getDBCon(); + { + /* + * level: + * 1. triple + g2t + * 2. triple + *2val + * 3. all tables + */ $pre = $this->getTablePrefix(); $tbls = $this->getTables(); $sql = ''; @@ -685,10 +804,9 @@ public function processTables($level = 2, $operation = 'optimize') $sql .= $sql ? ', ' : strtoupper($operation).' TABLE '; $sql .= $pre.$tbl; } - $this->queryDB($sql, $con); - $err = mysqli_error($con); - if (!empty($err)) { - $this->addError($err.' in '.$sql); + $this->db->simpleQuery($sql); + if (false == empty($this->db->getErrorMessage())) { + $this->addError($this->db->getErrorMessage().' in '.$sql); } } @@ -719,39 +837,46 @@ public function changeNamespaceURI($old_uri, $new_uri) return $c->changeNamespaceURI($old_uri, $new_uri); } + /** + * @param string $res URI + * @param string $unnamed_label How to label a resource without a name? + * + * @return string + */ public function getResourceLabel($res, $unnamed_label = 'An unnamed resource') { + // init local label cache, if not set if (!isset($this->resource_labels)) { $this->resource_labels = []; } + // if we already know the label for the given resource if (isset($this->resource_labels[$res])) { return $this->resource_labels[$res]; } + // if no URI was given, assume its a literal and return it if (!preg_match('/^[a-z0-9\_]+\:[^\s]+$/si', $res)) { return $res; - } /* literal */ + } + $ps = $this->getLabelProps(); if ($this->getSetting('store_label_properties', '-') != md5(serialize($ps))) { $this->inferLabelProps($ps); } - //$sub_q .= $sub_q ? ' || ' : ''; - //$sub_q .= 'REGEX(str(?p), "(last_name|name|fn|title|label)$", "i")'; - $q = 'SELECT ?label WHERE { <'.$res.'> ?p ?label . ?p a } LIMIT 3'; - $r = ''; - $rows = $this->query($q, 'rows'); - foreach ($rows as $row) { - $r = strlen($row['label']) > strlen($r) ? $row['label'] : $r; - } - if (!$r && preg_match('/^\_\:/', $res)) { - return $unnamed_label; + + foreach ($ps as $labelProperty) { + // send a query for each label property + $result = $this->query('SELECT ?label WHERE { <'.$res.'> <'.$labelProperty.'> ?label }'); + if (isset($result['result']['rows'][0])) { + $this->resource_labels[$res] = $result['result']['rows'][0]['label']; + return $result['result']['rows'][0]['label']; + } } - $r = $r ? $r : preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('#self', '', $res)); + + $r = preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('#self', '', $res)); $r = str_replace('_', ' ', $r); $r = preg_replace_callback('/([a-z])([A-Z])/', function ($matches) { return $matches[1].' '.strtolower($matches[2]); }, $r); - $this->resource_labels[$res] = $r; - return $r; } diff --git a/store/ARC2_StoreAskQueryHandler.php b/store/ARC2_StoreAskQueryHandler.php index ff402570..9458c8d3 100755 --- a/store/ARC2_StoreAskQueryHandler.php +++ b/store/ARC2_StoreAskQueryHandler.php @@ -18,7 +18,7 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->store = $this->caller; } @@ -39,10 +39,8 @@ public function buildResultVars() public function getFinalQueryResult($q_sql, $tmp_tbl) { - $con = $this->store->getDBCon(); - $rs = mysqli_query($con, 'SELECT success FROM '.$tmp_tbl); - $r = ($row = mysqli_fetch_array($rs)) ? $row['success'] : 0; - + $row = $this->store->a['db_object']->fetchRow('SELECT success FROM '.$tmp_tbl); + $r = isset($row['success']) ? $row['success'] : 0; return $r ? true : false; } } diff --git a/store/ARC2_StoreConstructQueryHandler.php b/store/ARC2_StoreConstructQueryHandler.php index 54c292d4..8c7ca92f 100755 --- a/store/ARC2_StoreConstructQueryHandler.php +++ b/store/ARC2_StoreConstructQueryHandler.php @@ -18,7 +18,7 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->store = $this->caller; } diff --git a/store/ARC2_StoreDeleteQueryHandler.php b/store/ARC2_StoreDeleteQueryHandler.php index d7bcb04c..b8889809 100644 --- a/store/ARC2_StoreDeleteQueryHandler.php +++ b/store/ARC2_StoreDeleteQueryHandler.php @@ -16,7 +16,7 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->store = $this->caller; $this->handler_type = 'delete'; @@ -25,7 +25,6 @@ public function __init() public function runQuery($infos) { $this->infos = $infos; - $con = $this->store->getDBCon(); $t1 = ARC2::mtime(); /* delete */ $this->refs_deleted = false; @@ -46,9 +45,13 @@ public function runQuery($infos) if ($tc && ($this->refs_deleted || (1 == rand(1, 100)))) { $this->cleanTableReferences(); } + // TODO What does this rand() call here? remove it and think about a cleaner way + // when to trigger optimizeTables if ($tc && (1 == rand(1, 100))) { $this->store->optimizeTables(); } + // TODO What does this rand() call here? remove it and think about a cleaner way + // when to trigger cleanValueTables if ($tc && (1 == rand(1, 500))) { $this->cleanValueTables(); } @@ -67,11 +70,9 @@ public 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); + $r += $this->store->a['db_object']->exec('DELETE FROM '.$tbl_prefix.'g2t WHERE g = '.$g_id); } } $this->refs_deleted = $r ? 1 : 0; @@ -84,7 +85,6 @@ public 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 = ''; @@ -120,7 +120,7 @@ public function deleteTriples() if ($gq) { $sql = ($dbv < '04-01') ? 'DELETE '.$tbl_prefix.'g2t' : 'DELETE G'; $sql .= ' - FROM '.$tbl_prefix.'g2t G + FROM '.$tbl_prefix.'g2t G JOIN '.$this->getTripleTable().' T ON (T.t = G.t'.$gq.') WHERE '.$q.' '; @@ -129,13 +129,10 @@ public function deleteTriples() $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 += $this->store->a['db_object']->exec($sql); + if (!empty($this->store->a['db_object']->getErrorMessage())) { + $this->addError($this->store->a['db_object']->getErrorMessage().' in '.$sql); } - $r += mysqli_affected_rows($con); } return $r; @@ -159,7 +156,6 @@ public function cleanTableReferences() 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 */ @@ -167,32 +163,33 @@ public function cleanTableReferences() 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)) { + $numRows = $this->store->a['db_object']->getNumberOfRows($sql); + if (0 < $numRows) { /* delete unconnected triples */ $sql = ($dbv < '04-01') ? 'DELETE '.$tbl_prefix.'triple' : 'DELETE T'; $sql .= ' - FROM '.$tbl_prefix.'triple T + 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); + $this->store->a['db_object']->simpleQuery($sql); } /* check for unconnected graph refs */ if ((1 == rand(1, 10))) { $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)) { + 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 (0 < $this->store->a['db_object']->getNumberOfRows($sql)) { /* 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); - } + FROM '.$tbl_prefix.'g2t G + LEFT JOIN '.$tbl_prefix.'triple T ON (T.t = G.t) + WHERE T.t IS NULL + '; + $this->store->a['db_object']->simpleQuery($sql); + } } /* release lock */ $this->store->releaseLock(); @@ -204,35 +201,39 @@ public function cleanValueTables() 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 + 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); + $this->store->a['db_object']->simpleQuery($sql); + /* s2val */ $sql = ($dbv < '04-01') ? 'DELETE '.$tbl_prefix.'s2val' : 'DELETE V'; $sql .= ' - FROM '.$tbl_prefix.'s2val V + 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); + $this->store->a['db_object']->simpleQuery($sql); + /* id2val */ $sql = ($dbv < '04-01') ? 'DELETE '.$tbl_prefix.'id2val' : 'DELETE V'; $sql .= ' - FROM '.$tbl_prefix.'id2val V + 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); + // TODO was commented out before. could this be a problem? + $this->store->a['db_object']->simpleQuery($sql); + /* release lock */ $this->store->releaseLock(); } diff --git a/store/ARC2_StoreDescribeQueryHandler.php b/store/ARC2_StoreDescribeQueryHandler.php index a23e72a9..00dcb5ce 100644 --- a/store/ARC2_StoreDescribeQueryHandler.php +++ b/store/ARC2_StoreDescribeQueryHandler.php @@ -18,7 +18,7 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->store = $this->caller; $this->detect_labels = $this->v('detect_describe_query_labels', 0, $this->a); diff --git a/store/ARC2_StoreDumpQueryHandler.php b/store/ARC2_StoreDumpQueryHandler.php index 1be2d409..f00025f0 100755 --- a/store/ARC2_StoreDumpQueryHandler.php +++ b/store/ARC2_StoreDumpQueryHandler.php @@ -18,7 +18,7 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->store = $this->caller; } @@ -26,7 +26,6 @@ public function __init() public function runQuery($infos, $keep_bnode_ids = 0) { $this->infos = $infos; - $con = $this->store->getDBCon(); ARC2::inc('StoreDumper'); $d = new ARC2_StoreDumper($this->a, $this->store); $d->dumpSPOG(); diff --git a/store/ARC2_StoreDumper.php b/store/ARC2_StoreDumper.php index 26595a53..471e174d 100755 --- a/store/ARC2_StoreDumper.php +++ b/store/ARC2_StoreDumper.php @@ -43,11 +43,11 @@ public function dumpSPOG() $offset = 0; do { $proceed = 0; - $rs = $this->getRecordset($offset); - if (!$rs) { + $rows = $this->getRecordset($offset); + if (false == is_array($rows)) { break; } - while ($row = mysqli_fetch_array($rs)) { + foreach($rows as $row) { echo $this->getEntry($row); $proceed = 1; } @@ -68,11 +68,11 @@ public function saveSPOG($path, $q = '') $offset = 0; do { $proceed = 0; - $rs = $this->getRecordset($offset); - if (!$rs) { + $rows = $this->getRecordset($offset); + if (false == is_array($rows)) { break; } - while ($row = mysqli_fetch_array($rs)) { + foreach($rows as $row) { fwrite($fp, $this->getEntry($row)); $proceed = 1; } @@ -101,7 +101,6 @@ public function saveCustomSPOG($path, $q) public function getRecordset($offset) { $prefix = $this->store->getTablePrefix(); - $con = $this->store->getDBCon(); $sql = ' SELECT VS.val AS s, @@ -128,13 +127,13 @@ public function getRecordset($offset) if ($offset) { $sql .= ' OFFSET '.$offset; } - $rs = mysqli_query($con, $sql, MYSQLI_USE_RESULT); - $err = mysqli_error($con); - if (!empty($err)) { - return $this->addError($err); + + $rows = $this->store->a['db_object']->fetchList($sql); + if (false == empty($this->store->a['db_object']->getErrorMessage())) { + return $this->addError($this->store->a['db_object']->getErrorMessage()); } - return $rs; + return $rows; } public function getHeader() diff --git a/store/ARC2_StoreEndpoint.php b/store/ARC2_StoreEndpoint.php index 584fc826..864f80af 100755 --- a/store/ARC2_StoreEndpoint.php +++ b/store/ARC2_StoreEndpoint.php @@ -397,7 +397,6 @@ public function getSPARQLXMLSelectResultDoc($r) public function getSPARQLJSONSelectResultDoc($r) { - $con = $this->getDBCon(); $this->setHeader('content-type', 'Content-Type: application/sparql-results+json'); $vars = $r['result']['variables']; $rows = $r['result']['rows']; @@ -430,13 +429,13 @@ public function getSPARQLJSONSelectResultDoc($r) $r .= ' "'.$var.'": {'; if ('uri' == $row[$var.' type']) { $r .= $nl.' "type": "uri",'; - $r .= $nl.' "value": "'.mysqli_real_escape_string($con, $row[$var]).'"'; + $r .= $nl.' "value": "'.$this->store->a['db_object']->escape($row[$var]).'"'; } elseif ('bnode' == $row[$var.' type']) { $r .= $nl.' "type": "bnode",'; $r .= $nl.' "value": "'.substr($row[$var], 2).'"'; } else { - $dt = isset($row[$var.' datatype']) ? ','.$nl.' "datatype": "'.mysqli_real_escape_string($con, $row[$var.' datatype']).'"' : ''; - $lang = isset($row[$var.' lang']) ? ','.$nl.' "xml:lang": "'.mysqli_real_escape_string($con, $row[$var.' lang']).'"' : ''; + $dt = isset($row[$var.' datatype']) ? ','.$nl.' "datatype": "'.$this->store->a['db_object']->escape($row[$var.' datatype']).'"' : ''; + $lang = isset($row[$var.' lang']) ? ','.$nl.' "xml:lang": "'.$this->store->a['db_object']->escape($row[$var.' lang']).'"' : ''; $type = $dt ? 'typed-literal' : 'literal'; $r .= $nl.' "type": "'.$type.'",'; $r .= $nl.' "value": "'.$this->jsonEscape($row[$var]).'"'; diff --git a/store/ARC2_StoreHelper.php b/store/ARC2_StoreHelper.php index ca2e3242..60b6de02 100755 --- a/store/ARC2_StoreHelper.php +++ b/store/ARC2_StoreHelper.php @@ -18,7 +18,7 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->store = $this->caller; } @@ -29,20 +29,20 @@ public function changeNamespaceURI($old_uri, $new_uri) $t_changes = 0; /* table lock */ if ($this->store->getLock()) { - $con = $this->store->getDBCon(); foreach (['id', 's', 'o'] as $id_col) { $tbl = $this->store->getTablePrefix().$id_col.'2val'; - $sql = 'SELECT id, val FROM '.$tbl.' WHERE val LIKE "'.mysqli_real_escape_string($con, $old_uri).'%"'; - $rs = mysqli_query($con, $sql); - if (!$rs) { + $sql = 'SELECT id, val FROM '.$tbl.' WHERE val LIKE "'.$this->store->a['db_object']->escape($old_uri).'%"'; + $rows = $this->store->a['db_object']->fetchList($sql); + + if (false == is_array($rows)) { continue; } - while ($row = mysqli_fetch_array($rs)) { + foreach($rows as $row) { $new_val = str_replace($old_uri, $new_uri, $row['val']); $new_id = $this->store->getTermID($new_val, $id_col); if (!$new_id) {/* unknown ns uri, overwrite current id value */ - $sub_sql = 'UPDATE '.$tbl." SET val = '".mysqli_real_escape_string($con, $new_val)."' WHERE id = ".$row['id']; - $sub_r = mysqli_query($con, $sub_sql); + $sub_sql = 'UPDATE '.$tbl." SET val = '".$this->store->a['db_object']->escape($new_val)."' WHERE id = ".$row['id']; + $sub_r = $this->store->a['db_object']->simpleQuery($sub_sql); ++$id_changes; } else {/* replace ids */ $t_tbls = $this->store->getTables(); @@ -50,8 +50,8 @@ public function changeNamespaceURI($old_uri, $new_uri) if (preg_match('/^triple/', $t_tbl)) { foreach (['s', 'p', 'o', 'o_lang_dt'] as $t_col) { $sub_sql = 'UPDATE '.$this->store->getTablePrefix().$t_tbl.' SET '.$t_col.' = '.$new_id.' WHERE '.$t_col.' = '.$row['id']; - $sub_r = mysqli_query($con, $sub_sql); - $t_changes += mysqli_affected_rows($con); + $sub_r = $this->store->a['db_object']->simpleQuery($sub_sql); + $t_changes += $this->store->a['db_object']->getAffectedRows(); } } } diff --git a/store/ARC2_StoreInsertQueryHandler.php b/store/ARC2_StoreInsertQueryHandler.php index dbfbe115..8f0bd729 100644 --- a/store/ARC2_StoreInsertQueryHandler.php +++ b/store/ARC2_StoreInsertQueryHandler.php @@ -16,7 +16,7 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->store = $this->caller; } @@ -24,7 +24,6 @@ public function __init() public function runQuery($infos, $keep_bnode_ids = 0) { $this->infos = $infos; - $con = $this->store->getDBCon(); /* insert */ if (!$this->v('pattern', [], $this->infos['query'])) { $triples = $this->infos['query']['construct_triples']; diff --git a/store/ARC2_StoreLoadQueryHandler.php b/store/ARC2_StoreLoadQueryHandler.php index c69a7d30..54e2d550 100644 --- a/store/ARC2_StoreLoadQueryHandler.php +++ b/store/ARC2_StoreLoadQueryHandler.php @@ -155,15 +155,17 @@ public function addT($s, $p, $o, $s_type, $o_type, $o_dt = '', $o_lang = '') public function getMaxTermID() { - $con = $this->store->getDBCon(); $sql = ''; foreach (['id2val', 's2val', 'o2val'] as $tbl) { $sql .= $sql ? ' UNION ' : ''; $sql .= '(SELECT MAX(id) as `id` FROM '.$this->store->getTablePrefix().$tbl.')'; } $r = 0; - if (($rs = $this->queryDB($sql, $con)) && mysqli_num_rows($rs)) { - while ($row = mysqli_fetch_array($rs)) { + + $rows = $this->store->a['db_object']->fetchList($sql); + + if (is_array($rows)) { + foreach($rows as $row) { $r = ($r < $row['id']) ? $row['id'] : $r; } } @@ -171,12 +173,18 @@ public function getMaxTermID() return $r + 1; } + /** + * @todo change DB schema and avoid using this function because it does not protect against race conditions + * + * @return int + */ public function getMaxTripleID() { - $con = $this->store->getDBCon(); $sql = 'SELECT MAX(t) AS `id` FROM '.$this->store->getTablePrefix().'triple'; - if (($rs = $this->queryDB($sql, $con)) && mysqli_num_rows($rs) && ($row = mysqli_fetch_array($rs))) { - return $row['id'] + 1; + + $row = $this->store->a['db_object']->fetchRow($sql); + if (isset($row['id'])) { + return $row['id']+1; } return 1; @@ -184,7 +192,6 @@ public function getMaxTripleID() public function getStoredTermID($val, $type_id, $tbl) { - $con = $this->store->getDBCon(); /* buffered */ if (isset($this->term_ids[$val])) { if (!isset($this->term_ids[$val][$tbl])) { @@ -204,12 +211,12 @@ public function getStoredTermID($val, $type_id, $tbl) $sub_tbls = ('id' == $tbl) ? ['id2val', 's2val', 'o2val'] : ('s' == $tbl ? ['s2val', 'id2val', 'o2val'] : ['o2val', 'id2val', 's2val']); foreach ($sub_tbls as $sub_tbl) { $id = 0; - //$sql = "SELECT id AS `id`, '" . $sub_tbl . "' AS `tbl` FROM " . $tbl_prefix . $sub_tbl . " WHERE val = BINARY '" . mysql_real_escape_string($val, $con) . "'"; /* via hash */ if (preg_match('/^(s2val|o2val)$/', $sub_tbl) && $this->hasHashColumn($sub_tbl)) { $sql = 'SELECT id AS `id`, val AS `val` FROM '.$tbl_prefix.$sub_tbl." WHERE val_hash = BINARY '".$this->getValueHash($val)."'"; - if (($rs = $this->queryDB($sql, $con)) && mysqli_num_rows($rs)) { - while ($row = mysqli_fetch_array($rs)) { + $rows = $this->store->a['db_object']->fetchList($sql); + if (is_array($rows)) { + foreach($rows as $row) { if ($row['val'] == $val) { $id = $row['id']; break; @@ -217,10 +224,13 @@ public function getStoredTermID($val, $type_id, $tbl) } } } else { - $sql = 'SELECT id AS `id` FROM '.$tbl_prefix.$sub_tbl." WHERE val = BINARY '".mysqli_real_escape_string($con, $val)."'"; - if (($rs = $this->queryDB($sql.' LIMIT 1', $con)) && mysqli_num_rows($rs)) { - $row = mysqli_fetch_array($rs); - $id = $row['id']; + $binaryValue = $this->store->a['db_object']->escape($val); + if (false !== empty($binaryValue)) { + $sql = 'SELECT id AS `id` FROM '.$tbl_prefix.$sub_tbl." WHERE val = BINARY '".$binaryValue."'"; + $row = $this->store->a['db_object']->fetchRow($sql); + if (is_array($row) && isset($row['id'])) { + $id = $row['id']; + } } } if ($id) { @@ -248,23 +258,25 @@ public function getStoredTermID($val, $type_id, $tbl) public function getTripleID($t) { - $con = $this->store->getDBCon(); $val = serialize($t); /* buffered */ if (isset($this->triple_ids[$val])) { return [$this->triple_ids[$val]]; /* hack for "don't insert this triple" */ } /* db */ - $sql = 'SELECT t FROM '.$this->store->getTablePrefix().'triple WHERE - s = '.$t['s'].' AND p = '.$t['p'].' AND o = '.$t['o'].' AND o_lang_dt = '.$t['o_lang_dt'].' AND s_type = '.$t['s_type'].' AND o_type = '.$t['o_type'].' - LIMIT 1 - '; - if (($rs = $this->queryDB($sql, $con)) && mysqli_num_rows($rs) && ($row = mysqli_fetch_array($rs))) { + $sql = 'SELECT t + FROM '.$this->store->getTablePrefix().'triple + WHERE s = '.$t['s'].' AND p = '.$t['p'].' AND o = '.$t['o'].' + AND o_lang_dt = '.$t['o_lang_dt'].' AND s_type = '.$t['s_type'].' + AND o_type = '.$t['o_type'].' + LIMIT 1'; + $row = $this->store->a['db_object']->fetchRow($sql); + if (isset($row['t'])) { $this->triple_ids[$val] = $row['t']; /* hack for "don't insert this triple" */ return [$row['t']]; /* hack for "don't insert this triple" */ - } + /* new */ - else { + } else { $this->triple_ids[$val] = $this->max_triple_id; ++$this->max_triple_id; /* split tables ? */ @@ -300,7 +312,9 @@ public function getOComp($val) if (isset($m[3]) && ('Z' != $m[3])) { $uts = strtotime(str_replace('T', ' ', $val)); if (preg_match('/([\+\-])([0-9]{2})\:?([0-9]{2})$/', $m[3], $sub_m)) { - $diff_mins = (3600 * ltrim($sub_m[2], '0')) + ltrim($sub_m[3], '0'); + // without the explicit (int) casting, you will get the following error with PHP 7.1+ + // A non-numeric value encountered + $diff_mins = (3600 * (int)ltrim($sub_m[2], '0')) + (int)ltrim($sub_m[3], '0'); $uts = ('-' == $sub_m[1]) ? $uts + $diff_mins : $uts - $diff_mins; $val = date('Y-m-d\TH:i:s\Z', $uts); } @@ -345,14 +359,13 @@ public function getOComp($val) public function bufferTripleSQL($t) { - $con = $this->store->getDBCon(); $tbl = 'triple'; $sql = ', '; if (!isset($this->sql_buffers[$tbl])) { $this->sql_buffers[$tbl] = 'INSERT IGNORE INTO '.$this->store->getTablePrefix().$tbl.' (t, s, p, o, o_lang_dt, o_comp, s_type, o_type) VALUES'; $sql = ' '; } - $this->sql_buffers[$tbl] .= $sql.'('.$t['t'].', '.$t['s'].', '.$t['p'].', '.$t['o'].', '.$t['o_lang_dt'].", '".mysqli_real_escape_string($con, $t['o_comp'])."', ".$t['s_type'].', '.$t['o_type'].')'; + $this->sql_buffers[$tbl] .= $sql.'('.$t['t'].', '.$t['s'].', '.$t['p'].', '.$t['o'].', '.$t['o_lang_dt'].", '".$this->store->a['db_object']->escape($t['o_comp'])."', ".$t['s_type'].', '.$t['o_type'].')'; } public function bufferGraphSQL($g2t) @@ -368,17 +381,16 @@ public function bufferGraphSQL($g2t) public function bufferIDSQL($tbl, $id, $val, $val_type) { - $con = $this->store->getDBCon(); $tbl = $tbl.'2val'; if ('id2val' == $tbl) { $cols = 'id, val, val_type'; - $vals = '('.$id.", '".mysqli_real_escape_string($con, $val)."', ".$val_type.')'; + $vals = '('.$id.", '".$this->store->a['db_object']->escape($val)."', ".$val_type.')'; } elseif (preg_match('/^(s2val|o2val)$/', $tbl) && $this->hasHashColumn($tbl)) { $cols = 'id, val_hash, val'; - $vals = '('.$id.", '".$this->getValueHash($val)."', '".mysqli_real_escape_string($con, $val)."')"; + $vals = '('.$id.", '".$this->getValueHash($val)."', '".$this->store->a['db_object']->escape($val)."')"; } else { $cols = 'id, val'; - $vals = '('.$id.", '".mysqli_real_escape_string($con, $val)."')"; + $vals = '('.$id.", '".$this->store->a['db_object']->escape($val)."')"; } if (!isset($this->sql_buffers[$tbl])) { $this->sql_buffers[$tbl] = ''; @@ -392,7 +404,6 @@ public function bufferIDSQL($tbl, $id, $val, $val_type) public function checkSQLBuffers($force_write = 0, $reset_id_buffers = 0, $refresh_lock = 0, $split_tables = 0) { - $con = $this->store->getDBCon(); if (!$this->keep_time_limit) { set_time_limit($this->v('time_limit', 60, $this->a)); } @@ -400,19 +411,30 @@ public function checkSQLBuffers($force_write = 0, $reset_id_buffers = 0, $refres $buffer_size = isset($this->sql_buffers[$tbl]) ? 1 : 0; if ($buffer_size && $force_write) { $t1 = ARC2::mtime(); - $this->queryDB($this->sql_buffers[$tbl], $con); + $this->store->a['db_object']->simpleQuery($this->sql_buffers[$tbl]); /* table error */ - $er = mysqli_error($con); - if (!empty($er)) { - $this->autoRepairTable($er, $con, $this->sql_buffers[$tbl]); + $error = $this->store->a['db_object']->getErrorMessage(); + if (!empty($error)) { + $this->autoRepairTable($error, $this->sql_buffers[$tbl]); } unset($this->sql_buffers[$tbl]); if ($this->log_inserts) { $t2 = ARC2::mtime(); - $this->inserts[$tbl] = $this->v($tbl, 0, $this->inserts) + max(0, mysqli_affected_rows($con)); + $this->inserts[$tbl] = $this->v( + $tbl, + 0, + $this->inserts + ) + max(0, $this->store->a['db_object']->getAffectedRows()); + $dur = round($t2 - $t1, 4); - $this->insert_times[$tbl] = isset($this->insert_times[$tbl]) ? $this->insert_times[$tbl] : ['min' => $dur, 'max' => $dur, 'sum' => $dur]; - $this->insert_times[$tbl] = ['min' => min($dur, $this->insert_times[$tbl]['min']), 'max' => max($dur, $this->insert_times[$tbl]['max']), 'sum' => $dur + $this->insert_times[$tbl]['sum']]; + $this->insert_times[$tbl] = isset($this->insert_times[$tbl]) + ? $this->insert_times[$tbl] + : ['min' => $dur, 'max' => $dur, 'sum' => $dur]; + $this->insert_times[$tbl] = [ + 'min' => min($dur, $this->insert_times[$tbl]['min']), + 'max' => max($dur, $this->insert_times[$tbl]['max']), + 'sum' => $dur + $this->insert_times[$tbl]['sum'] + ]; } /* reset term id buffers */ if ($reset_id_buffers) { @@ -435,12 +457,13 @@ public function checkSQLBuffers($force_write = 0, $reset_id_buffers = 0, $refres return 1; } - public function autoRepairTable($er, $con, $sql = '') + public function autoRepairTable($er, $sql = '') { $this->addError('MySQL error: '.$er.' ('.$sql.')'); if (preg_match('/Table \'[^\']+\/([a-z0-9\_\-]+)\' .*(crashed|repair)/i', $er, $m)) { - $rs = $this->queryDB('REPAIR TABLE '.rawurlencode($m[1]), $con); - $msg = $rs ? mysqli_fetch_array($rs) : []; + $row = $this->store->a['db_object']->fetchRow('REPAIR TABLE '.rawurlencode($m[1])); + $msg = is_array($row) ? $row : []; + if ('error' == $this->v('Msg_type', 'error', $msg)) { /* auto-reset */ if ($this->v('store_reset_on_table_crash', 0, $this->a)) { @@ -450,7 +473,6 @@ public function autoRepairTable($er, $con, $sql = '') $er = $this->v('Msg_text', 'unknown error', $msg); $this->addError('Auto-repair failed on '.rawurlencode($m[1]).': '.$er); } - //die("Fatal errors: \n" . print_r($this->getErrors(), 1)); } } } diff --git a/store/ARC2_StoreQueryHandler.php b/store/ARC2_StoreQueryHandler.php index 59c516e1..98f93d32 100755 --- a/store/ARC2_StoreQueryHandler.php +++ b/store/ARC2_StoreQueryHandler.php @@ -18,7 +18,7 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->xsd = 'http://www.w3.org/2001/XMLSchema#'; $this->allow_extension_functions = $this->v('store_allow_extension_functions', 1, $this->a); @@ -55,8 +55,7 @@ public function createMergeTable() return 1; } $this->mrg_table_id = 'MRG_'.$this->store->getTablePrefix().crc32(uniqid(rand())); - $con = $this->store->getDBCon(); - $this->queryDB('FLUSH TABLES', $con); + $this->getDBObject()->query('FLUSH TABLES'); $indexes = $this->v('store_indexes', ['sp (s,p)', 'os (o,s)', 'po (p,o)'], $this->a); $index_code = $indexes ? 'KEY '.implode(', KEY ', $indexes).', ' : ''; $prefix = $this->store->getTablePrefix(); @@ -72,7 +71,7 @@ public function createMergeTable() o_type tinyint(1) NOT NULL default 0, /* uri/bnode/literal => 0/1/2 */ misc tinyint(1) NOT NULL default 0, /* temporary flags */ UNIQUE KEY (t), '.$index_code.' KEY (misc) - ) + ) '; $v = $this->store->getDBVersion(); $sql .= (($v < '04-01-00') && ($v >= '04-00-18')) ? 'ENGINE' : (($v >= '04-01-02') ? 'ENGINE' : 'TYPE'); @@ -81,17 +80,19 @@ public function createMergeTable() $sql .= ','.$prefix.'triple_'.abs(crc32($p)); } $sql .= ')'; + // TODO whats about that? //$sql .= ($v >= '04-00-00') ? " CHARACTER SET utf8" : ""; //$sql .= ($v >= '04-01-00') ? " COLLATE utf8_unicode_ci" : ""; //echo $sql; - return $this->queryDB($sql, $con); + return $this->getDBObject()->query($sql); } public function dropMergeTable() { return 1; + // TODO triple_all table seems not used anymore, therefore this function can be removed? $sql = 'DROP TABLE IF EXISTS '.$this->store->getTablePrefix().'triple_all'; //echo $sql; - return $this->queryDB($sql, $this->store->getDBCon()); + //return $this->queryDB($sql, $this->store->getDBCon()); } } diff --git a/store/ARC2_StoreSelectQueryHandler.php b/store/ARC2_StoreSelectQueryHandler.php index f820050e..88388c7f 100644 --- a/store/ARC2_StoreSelectQueryHandler.php +++ b/store/ARC2_StoreSelectQueryHandler.php @@ -18,10 +18,9 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->store = $this->caller; - $con = $this->store->getDBCon(); $this->handler_type = 'select'; $this->engine_type = $this->v('store_engine_type', 'MyISAM', $this->a); $this->cache_results = $this->v('store_cache_results', 0, $this->a); @@ -29,7 +28,6 @@ public function __init() public function runQuery($infos) { - $con = $this->store->getDBCon(); $rf = $this->v('result_format', '', $infos); $this->infos = $infos; $this->infos['null_vars'] = []; @@ -53,7 +51,7 @@ public function runQuery($infos) $r = $this->getFinalQueryResult($q_sql, $tmp_tbl); /* remove intermediate results */ if (!$this->cache_results) { - $this->queryDB('DROP TABLE IF EXISTS '.$tmp_tbl, $con); + $this->getDBObjectFromARC2Class()->simpleQuery('DROP TABLE IF EXISTS '.$tmp_tbl); } return $r; @@ -108,6 +106,8 @@ public function buildInitialIndexes() { $this->dependency_log = []; $this->index = $this->getEmptyIndex(); + // if no pattern is in the query, the index "pattern" is undefined, which leads to an error. + // TODO throw an exception/raise an error and avoid "Undefined index: pattern" notification $this->buildIndex($this->infos['query']['pattern'], 0); $tmp = $this->index; $this->analyzeIndex($this->getPattern('0')); @@ -119,7 +119,6 @@ public function buildInitialIndexes() public function createTempTable($q_sql) { - $con = $this->store->getDBCon(); $v = $this->store->getDBVersion(); if ($this->cache_results) { $tbl = $this->store->getTablePrefix().'Q'.md5($q_sql); @@ -132,13 +131,12 @@ public function createTempTable($q_sql) $tmp_sql = 'CREATE TEMPORARY TABLE '.$tbl.' ( '.$this->getTempTableDef($tbl, $q_sql).') '; $tmp_sql .= (($v < '04-01-00') && ($v >= '04-00-18')) ? 'ENGINE' : (($v >= '04-01-02') ? 'ENGINE' : 'TYPE'); $tmp_sql .= '='.$this->engine_type; /* HEAP doesn't support AUTO_INCREMENT, and MySQL breaks on MEMORY sometimes */ - if (!$this->queryDB($tmp_sql, $con) && !$this->queryDB(str_replace('CREATE TEMPORARY', 'CREATE', $tmp_sql), $con)) { - return $this->addError(mysqli_error($con)); + if (!$this->store->a['db_object']->simpleQuery($tmp_sql) + && !$this->store->a['db_object']->simpleQuery(str_replace('CREATE TEMPORARY', 'CREATE', $tmp_sql))) { + return $this->addError($this->store->a['db_object']->getErrorMessage()); } - mysqli_query($con, 'INSERT INTO '.$tbl.' '."\n".$q_sql, MYSQLI_USE_RESULT); - $er = mysqli_error($con); - if (!empty($er)) { - $this->addError($er); + if (false == $this->store->a['db_object']->simpleQuery('INSERT INTO '.$tbl.' '."\n".$q_sql)) { + $this->addError($this->store->a['db_object']->getErrorMessage()); } return $tbl; @@ -203,24 +201,33 @@ public function getFinalQueryResult($q_sql, $tmp_tbl) /* result */ $r = ['variables' => $vars]; $v_sql = $this->getValueSQL($tmp_tbl, $q_sql); - //echo "\n\n" . $v_sql; + $t1 = ARC2::mtime(); - $con = $this->store->getDBCon(); - $rs = mysqli_query($con, $v_sql, MYSQLI_USE_RESULT); - $er = mysqli_error($con); - if (!empty($er)) { - $this->addError($er); + + try { + $entries = []; // in case an exception gets thrown + + $entries = $this->store->a['db_object']->fetchList($v_sql); + } catch (\Exception $e) { + $this->addError($e->getMessage()); } + $t2 = ARC2::mtime(); $rows = []; $types = [0 => 'uri', 1 => 'bnode', 2 => 'literal']; - if ($rs) { - while ($pre_row = mysqli_fetch_array($rs)) { + if (0 < count($entries)) { + foreach($entries as $pre_row) { $row = []; foreach ($vars as $var) { if (isset($pre_row[$var])) { $row[$var] = $pre_row[$var]; - $row[$var.' type'] = isset($pre_row[$var.' type']) ? $types[$pre_row[$var.' type']] : (in_array($var, $aggregate_vars) ? 'literal' : 'uri'); + $row[$var.' type'] = isset($pre_row[$var.' type']) + ? $types[$pre_row[$var.' type']] + : ( + in_array($var, $aggregate_vars) + ? 'literal' + : 'uri' + ); if (isset($pre_row[$var.' lang_dt']) && ($lang_dt = $pre_row[$var.' lang_dt'])) { if (preg_match('/^([a-z]+(\-[a-z0-9]+)*)$/i', $lang_dt)) { $row[$var.' lang'] = $lang_dt; @@ -467,17 +474,19 @@ public function getQuerySQL() $nl = "\n"; $where_sql = $this->getWHERESQL(); /* pre-fills $index['sub_joins'] $index['constraints'] */ $order_sql = $this->getORDERSQL(); /* pre-fills $index['sub_joins'] $index['constraints'] */ - return ''. - ($this->is_union_query ? 'SELECT' : 'SELECT'.$this->getDistinctSQL()).$nl. - $this->getResultVarsSQL().$nl. /* fills $index['sub_joins'] */ - $this->getFROMSQL(). - $this->getAllJoinsSQL(). - $this->getWHERESQL(). - $this->getGROUPSQL(). - $this->getORDERSQL(). - ($this->is_union_query ? '' : $this->getLIMITSQL()). - $nl. - ''; + return ''. ( + $this->is_union_query + ? 'SELECT' + : 'SELECT'.$this->getDistinctSQL()).$nl. + $this->getResultVarsSQL().$nl. /* fills $index['sub_joins'] */ + $this->getFROMSQL(). + $this->getAllJoinsSQL(). + $this->getWHERESQL(). + $this->getGROUPSQL(). + $this->getORDERSQL(). + ($this->is_union_query + ? '' + : $this->getLIMITSQL()).$nl.''; } public function getDistinctSQL() @@ -841,7 +850,8 @@ public function getDependentJoins($id) } public function getRequiredSubJoinSQL($id, $prefix = '') - {/* id is a triple pattern id. Optional FILTERS and GRAPHs are getting added to the join directly */ + { + /* id is a triple pattern id. Optional FILTERS and GRAPHs are getting added to the join directly */ $nl = "\n"; $r = ''; foreach ($this->index['sub_joins'] as $alias) { @@ -861,7 +871,6 @@ public function getRequiredSubJoinSQL($id, $prefix = '') $sub_sub_r = 'T_'.$id.'.o_type = 2'; $sub_r .= $nl.' AND ('.$sub_sub_r.')'; } - //$cur_prefix = $prefix ? $prefix . ' ' : 'STRAIGHT_'; $cur_prefix = $prefix ? $prefix.' ' : ''; if ('g' == $col) { $r .= trim($cur_prefix.'JOIN '.$this->getValueTable($col).' V_'.$id.'_'.$col.' ON ('.$nl.' (G_'.$id.'.'.$col.' = V_'.$id.'_'.$col.'.id) '.$sub_r.$nl.')'); @@ -883,7 +892,7 @@ public function getRequiredSubJoinSQL($id, $prefix = '') $added_gts[] = $set['graph']; } } - $sub_r .= ('' !== $sub_sub_r) ? $nl.' AND (G_'.$id.'.g IN ('.$sub_sub_r.'))' : ''; // /* ' . str_replace('#' , '::', $set['graph']) . ' */'; + $sub_r .= ('' !== $sub_sub_r) ? $nl.' AND (G_'.$id.'.g IN ('.$sub_sub_r.'))' : ''; /* other graph join conditions */ foreach ($this->index['graph_vars'] as $var => $occurs) { $occur_tbls = []; @@ -899,7 +908,6 @@ public function getRequiredSubJoinSQL($id, $prefix = '') } } } - //$cur_prefix = $prefix ? $prefix . ' ' : 'STRAIGHT_'; $cur_prefix = $prefix ? $prefix.' ' : ''; $r .= trim($cur_prefix.'JOIN '.$this->getGraphTable().' G_'.$id.' ON ('.$nl.' (T_'.$id.'.t = G_'.$id.'.t)'.$sub_r.$nl.')'); } @@ -926,14 +934,14 @@ public function getWHERESQL() $d_joins = $this->getDependentJoins($id); $added = []; $d_aliases = []; - //echo $id . ' =>' . print_r($d_joins, 1); $id_alias = 'T_'.$id.'.s'; foreach ($d_joins as $alias) { if (preg_match('/^(T|V|G)_([0-9\_]+)(_[spo])?\.([a-z\_]+)/', $alias, $m)) { $tbl_type = $m[1]; $tbl_pattern_id = $m[2]; $suffix = $m[3]; - if (($tbl_pattern_id >= $id) && $this->sameOptional($tbl_pattern_id, $id)) {/* get rid of dependency permutations and nested optionals */ + /* get rid of dependency permutations and nested optionals */ + if (($tbl_pattern_id >= $id) && $this->sameOptional($tbl_pattern_id, $id)) { if (!in_array($tbl_type.'_'.$tbl_pattern_id.$suffix, $added)) { $sub_r .= $sub_r ? ' AND ' : ''; $sub_r .= $alias.' IS NULL'; @@ -944,7 +952,8 @@ public function getWHERESQL() } } } - if (count($d_aliases) > 2) {/* @@todo fix this! */ + /* TODO fix this! */ + if (count($d_aliases) > 2) { $sub_r1 = ' /* '.$id_alias.' dependencies */'; $sub_r2 = '(('.$id_alias.' IS NULL) OR (CONCAT('.implode(', ', $d_aliases).') IS NOT NULL))'; $r .= $r ? $nl.$sub_r1.$nl.' AND '.$sub_r2 : $sub_r1.$nl.$sub_r2; @@ -1316,6 +1325,9 @@ public function detectExpressionValueType($pattern_ids) return ''; } + /** + * @todo not in use, so remove? + */ public function getRelationalExpressionSQL($pattern, $context, $val_type = '', $parent_type = '') { $r = ''; @@ -1334,6 +1346,9 @@ public function getRelationalExpressionSQL($pattern, $context, $val_type = '', $ return $r ? '('.$r.')' : $r; } + /** + * @todo not in use, so remove? + */ public function getAdditiveExpressionSQL($pattern, $context, $val_type = '', $parent_type = '') { $r = ''; @@ -1350,6 +1365,9 @@ public function getAdditiveExpressionSQL($pattern, $context, $val_type = '', $pa return $r; } + /** + * @todo not in use, so remove? + */ public function getMultiplicativeExpressionSQL($pattern, $context, $val_type = '', $parent_type = '') { $r = ''; @@ -1430,7 +1448,7 @@ public function getUriExpressionSQL($pattern, $context, $val_type = '') { $val = $pattern['uri']; $r = $pattern['operator']; - $r .= is_numeric($val) ? ' '.$val : ' "'.mysqli_real_escape_string($this->store->getDBCon(), $val).'"'; + $r .= is_numeric($val) ? ' '.$val : ' "'.$this->store->a['db_object']->escape($val).'"'; return $r; } @@ -1444,10 +1462,10 @@ public function getLiteralExpressionSQL($pattern, $context, $val_type = '', $par } elseif (preg_match('/^(true|false)$/i', $val) && ('http://www.w3.org/2001/XMLSchema#boolean' == $this->v1('datatype', '', $pattern))) { $r .= ' '.strtoupper($val); } elseif ('regex' == $parent_type) { - $sub_r = mysqli_real_escape_string($this->store->getDBCon(), $val); + $sub_r = $this->store->a['db_object']->escape($val); $r .= ' "'.preg_replace('/\x5c\x5c/', '\\', $sub_r).'"'; } else { - $r .= ' "'.mysqli_real_escape_string($this->store->getDBCon(), $val).'"'; + $r .= ' "'.$this->store->a['db_object']->escape($val).'"'; } if (($lang_dt = $this->v1('lang', '', $pattern)) || ($lang_dt = $this->v1('datatype', '', $pattern))) { /* try table/alias via var in siblings */ @@ -1467,8 +1485,10 @@ public function getLiteralExpressionSQL($pattern, $context, $val_type = '', $par } else { $context_pattern_id = $pattern['id']; } - if ($tbl == $context_pattern_id) {/* @todo better dependency check */ - if ($term_id || ('http://www.w3.org/2001/XMLSchema#integer' != $lang_dt)) {/* skip if simple int, but no id */ + // TODO better dependency check + if ($tbl == $context_pattern_id) { + if ($term_id || ('http://www.w3.org/2001/XMLSchema#integer' != $lang_dt)) { + /* skip, if simple int, but no id */ $this->addConstraintSQLEntry($context_pattern_id, 'T_'.$tbl.'.o_lang_dt = '.$term_id.' /* '.preg_replace('/[\#\*\>]/', '::', $lang_dt).' */'); } } @@ -1681,6 +1701,9 @@ public function getLangmatchesCallSQL($pattern, $context) return ''; } + /** + * @todo not in use, so remove? + */ public function getSametermCallSQL($pattern, $context) { if (2 == count($pattern['args'])) { @@ -1790,10 +1813,11 @@ public function getLIMITSQL() $limit = $this->v('limit', -1, $this->infos['query']); $offset = $this->v('offset', -1, $this->infos['query']); if ($limit != -1) { - $offset = ($offset == -1) ? 0 : mysqli_real_escape_string($this->store->getDBCon(), $offset); + $offset = ($offset == -1) ? 0 : $this->store->a['db_object']->escape($offset); $r = 'LIMIT '.$offset.','.$limit; } elseif ($offset != -1) { - $r = 'LIMIT '.mysqli_real_escape_string($this->store->getDBCon(), $offset).',999999999999'; /* mysql doesn't support stand-alone offsets .. */ + // mysql doesn't support stand-alone offsets + $r = 'LIMIT '.$this->store->a['db_object']->escape($offset).',999999999999'; } return $r ? $nl.$r : ''; diff --git a/store/ARC2_StoreTableManager.php b/store/ARC2_StoreTableManager.php index 58f402d6..38d9830e 100755 --- a/store/ARC2_StoreTableManager.php +++ b/store/ARC2_StoreTableManager.php @@ -17,19 +17,16 @@ public function __construct($a, &$caller) } public function __init() - {/* db_con */ + { parent::__init(); $this->engine_type = $this->v('store_engine_type', 'MyISAM', $this->a); } public function getTableOptionsCode() { - $v = $this->getDBVersion(); - $r = ''; - $r .= (($v < '04-01-00') && ($v >= '04-00-18')) ? 'ENGINE' : (($v >= '04-01-02') ? 'ENGINE' : 'TYPE'); - $r .= '='.$this->engine_type; - $r .= ($v >= '04-00-00') ? ' CHARACTER SET utf8' : ''; - $r .= ($v >= '04-01-00') ? ' COLLATE utf8_unicode_ci' : ''; + $r = 'ENGINE='.$this->engine_type; + $r .= ' CHARACTER SET utf8'; + $r .= ' COLLATE utf8_unicode_ci'; $r .= ' DELAY_KEY_WRITE = 1'; return $r; @@ -37,24 +34,23 @@ public function getTableOptionsCode() public function createTables() { - $con = $this->getDBCon(); if (!$this->createTripleTable()) { - return $this->addError('Could not create "triple" table ('.mysqli_error($con).').'); + return $this->addError('Could not create "triple" table ('.$this->a['db_object']->getErrorMessage().').'); } if (!$this->createG2TTable()) { - return $this->addError('Could not create "g2t" table ('.mysqli_error($con).').'); + return $this->addError('Could not create "g2t" table ('.$this->a['db_object']->getErrorMessage().').'); } if (!$this->createID2ValTable()) { - return $this->addError('Could not create "id2val" table ('.mysqli_error($con).').'); + return $this->addError('Could not create "id2val" table ('.$this->a['db_object']->getErrorMessage().').'); } if (!$this->createS2ValTable()) { - return $this->addError('Could not create "s2val" table ('.mysqli_error($con).').'); + return $this->addError('Could not create "s2val" table ('.$this->a['db_object']->getErrorMessage().').'); } if (!$this->createO2ValTable()) { - return $this->addError('Could not create "o2val" table ('.mysqli_error($con).').'); + return $this->addError('Could not create "o2val" table ('.$this->a['db_object']->getErrorMessage().').'); } if (!$this->createSettingTable()) { - return $this->addError('Could not create "setting" table ('.mysqli_error($con).').'); + return $this->addError('Could not create "setting" table ('.$this->a['db_object']->getErrorMessage().').'); } return 1; @@ -79,8 +75,7 @@ public function createTripleTable($suffix = 'triple') UNIQUE KEY (t), '.$index_code.' KEY (misc) ) '.$this->getTableOptionsCode().' '; - - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function extendTripleTableColumns($suffix = 'triple') @@ -94,7 +89,7 @@ public function extendTripleTableColumns($suffix = 'triple') MODIFY o_lang_dt int(10) UNSIGNED NOT NULL '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function createG2TTable() @@ -107,7 +102,7 @@ public function createG2TTable() ) '.$this->getTableOptionsCode().' '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function extendG2tTableColumns($suffix = 'g2t') @@ -118,7 +113,7 @@ public function extendG2tTableColumns($suffix = 'g2t') MODIFY t int(10) UNSIGNED NOT NULL '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function createID2ValTable() @@ -129,13 +124,13 @@ public function createID2ValTable() misc tinyint(1) NOT NULL default 0, val text NOT NULL, val_type tinyint(1) NOT NULL default 0, /* uri/bnode/literal => 0/1/2 */ - PRIMARY KEY (`id`), - UNIQUE KEY (id,val_type), + PRIMARY KEY (`id`), + UNIQUE KEY (id,val_type), KEY v (val(64)) ) '.$this->getTableOptionsCode().' '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function extendId2valTableColumns($suffix = 'id2val') @@ -145,7 +140,7 @@ public function extendId2valTableColumns($suffix = 'id2val') MODIFY id int(10) UNSIGNED NOT NULL '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function createS2ValTable() @@ -162,7 +157,7 @@ public function createS2ValTable() ) '.$this->getTableOptionsCode().' '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function extendS2valTableColumns($suffix = 's2val') @@ -172,7 +167,7 @@ public function extendS2valTableColumns($suffix = 's2val') MODIFY id int(10) UNSIGNED NOT NULL '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function createO2ValTable() @@ -192,7 +187,7 @@ public function createO2ValTable() ) '.$this->getTableOptionsCode().' '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function extendO2valTableColumns($suffix = 'o2val') @@ -202,7 +197,7 @@ public function extendO2valTableColumns($suffix = 'o2val') MODIFY id int(10) UNSIGNED NOT NULL '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function createSettingTable() @@ -215,12 +210,11 @@ public function createSettingTable() ) '.$this->getTableOptionsCode().' '; - return mysqli_query($this->getDBCon(), $sql); + return $this->a['db_object']->simpleQuery($sql); } public function extendColumns() { - $con = $this->getDBCon(); $tbl_prefix = $this->getTablePrefix(); $tbls = $this->getTables(); foreach ($tbls as $suffix) { @@ -257,13 +251,12 @@ public function unsplitPredicate($p) $old_tbl = $this->getTablePrefix().$suffix; $new_tbl = $this->getTablePrefix().'triple'; $p_id = $this->getTermID($p, 'p'); - $con = $this->getDBCon(); $sql = ' INSERT IGNORE INTO '.$new_tbl.' SELECT * FROM '.$old_tbl.' WHERE '.$old_tbl.'.p = '.$p_id.' '; - if ($rs = mysqli_query($con, $sql)) { - mysqli_query($con, 'DROP TABLE '.$old_tbl); + if ($this->a['db_object']->simpleQuery($sql)) { + $this->a['db_object']->simpleQuery('DROP TABLE '.$old_tbl); return 1; } else { @@ -278,17 +271,16 @@ public function splitPredicate($p) $old_tbl = $this->getTablePrefix().'triple'; $new_tbl = $this->getTablePrefix().$suffix; $p_id = $this->getTermID($p, 'p'); - $con = $this->getDBCon(); $sql = ' INSERT IGNORE INTO '.$new_tbl.' SELECT * FROM '.$old_tbl.' WHERE '.$old_tbl.'.p = '.$p_id.' '; - if ($rs = mysqli_query($con, $sql)) { - mysqli_query($con, 'DELETE FROM '.$old_tbl.' WHERE '.$old_tbl.'.p = '.$p_id); + if ($this->a['db_object']->simpleQuery($sql)) { + $this->a['db_object']->simpleQuery('DELETE FROM '.$old_tbl.' WHERE '.$old_tbl.'.p = '.$p_id); return 1; } else { - mysqli_query($con, 'DROP TABLE '.$new_tbl); + $this->a['db_object']->simpleQuery('DROP TABLE '.$new_tbl); return 0; } diff --git a/tests/ARC2_TestCase.php b/tests/ARC2_TestCase.php index 613a2aa8..6336fc86 100644 --- a/tests/ARC2_TestCase.php +++ b/tests/ARC2_TestCase.php @@ -2,6 +2,8 @@ namespace Tests; +use Psr\SimpleCache\CacheInterface; + class ARC2_TestCase extends \PHPUnit\Framework\TestCase { /** @@ -23,5 +25,20 @@ public function setUp() global $dbConfig; $this->dbConfig = $dbConfig; + + // in case we run with a cache, clear it + if (isset($this->dbConfig['cache_instance']) && $this->dbConfig['cache_instance'] instanceof CacheInterface) { + $this->dbConfig['cache_instance']->clear(); + } + } + + public function tearDown() + { + // in case we run with a cache, clear it + if (isset($this->dbConfig['cache_instance']) && $this->dbConfig['cache_instance'] instanceof CacheInterface) { + $this->dbConfig['cache_instance']->clear(); + } + + parent::tearDown(); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e78011ca..ea2e476e 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -18,6 +18,37 @@ 'db_name' => 'testdb', 'db_user' => 'root', 'db_pwd' => '', - 'db_host' => 'localhost', + 'db_host' => '127.0.0.1', ); } + +// set defaults for dbConfig entries +if (false == isset($dbConfig['store_name'])) { + $dbConfig['store_name'] = 'arc'; +} + +if (false == isset($dbConfig['db_table_prefix'])) { + $dbConfig['db_table_prefix'] = null; +} + +// set DB adapter (see related phpunit-xx.xml file), possible values: mysqli, pdo +if ('pdo' == getenv('DB_ADAPTER')) { + if (!empty(getenv('DB_PDO_PROTOCOL'))) { + $dbConfig['db_adapter'] = 'pdo'; + $dbConfig['db_pdo_protocol'] = getenv('DB_PDO_PROTOCOL'); + } else { + throw new \Exception('Environment variable DB_PDO_PROTOCOL not set. Possible values are: mysql'); + } +} else { + $dbConfig['db_adapter'] = 'mysqli'; +} + +/* + * set cache enable + * + * if enabled, we use an instance of ArrayCache which is very fast + */ +if (true ===\getenv('CACHE_ENABLED') || 'true' == \getenv('CACHE_ENABLED')) { + $dbConfig['cache_enabled'] = true; + $dbConfig['cache_instance'] = new Symfony\Component\Cache\Simple\ArrayCache(); +} diff --git a/tests/data/nt/saft-arc2-addition-regression1.nt b/tests/data/nt/saft-arc2-addition-regression1.nt new file mode 100644 index 00000000..3d6ca2e5 --- /dev/null +++ b/tests/data/nt/saft-arc2-addition-regression1.nt @@ -0,0 +1,442 @@ + "Cumberland"@en . + "Sydney"@en . + "151.2111111111111"^^ . + "4399722"^^ . + "33.859972222222225 151.2111111111111"@en . + "33.859972222222225"^^ . + . + "1911"^^ . + "0.4"^^ . + "1.0"^^ . + "3.53533377060864E9"^^ . + "3.534E9"^^ . + "1.4E7"^^ . + . + "Adams County"@en . + . + "0.9652553963561148"^^ . + "2000"^^ . + . + "1.294994055168E7"^^ . + . + "3476"^^ . + "3.548E9"^^ . + "3.54828371116032E9"^^ . + . + "Charles Douglas Cecil"@en . + "26"^^ . + . + . + "Chuck Cecil"@en . + "Free safety"@en . + "1.8288"^^ . + . + "1964-11-08"^^ . + "83462.4"^^ . + "Active (coach)"@en . + . + . + . + "0.0"^^ . + "221.0"^^ . + . + . + . + . + "Daugava"@en . + "1020000.0"^^ . + "8.79E10"^^ . + . + . + "678.0"^^ . + . + . + "-82.4"^^ . + . + . + . + "244620.288"^^ . + "331524.864"^^ . + . + . + "44.8"^^ . + "Lake Huron"@en . + "228.6"^^ . + "1.934721118420992E11"^^ . + . + "175.8696"^^ . + . + . + . + . + "59.436"^^ . + . + "6155740.8"^^ . + "44.8 -82.4"@en . + . + "3.5387863697990693E12"^^ . + "5.959562641883136E10"^^ . + . + "-118.15444444444445"^^ . + "36.72833333333333"^^ . + "36.72833333333333 -118.15444444444445"@en . + "2008"^^ . + "1976-07-30"^^ . + "78468"^^ . + "3293903.278336"^^ . + "Manzanar War Relocation Center"@en . + . + . + "76000484"@en . + "1942"^^ . + "Writer"@en . + "42.45333333333333"^^ . + "-74.60722222222222"^^ . + "42.45333333333333 -74.60722222222222"@en . + "2002-09-11"^^ . + "1.8542"^^ . + . + "1973"^^ . + "102"@en . + "1956"^^ . + "9"@en . + "87998.4"^^ . + . + "1955"^^ . + "1933-05-07"^^ . + "19"^^ . + . + . + "17024"^^ . + . + . + "Men/Women of Troy"@en . + "33389"^^ . + . + "14300"^^ . + "Let whoever earns the palm bear it"@en . + . + "4597"^^ . + . + . + . + "Trojans"@en . + . + . + "University of Southern California"@en . + "2.5E9"^^ . + "Traveler"@en . + "Palmam qui meruit ferat"@en . + "USC Cardinal and USC Gold"@en . + "16384"^^ . + . + . + . + "45.37351388888889"^^ . + "1866"^^ . + . + . + "-121.69591944444444"^^ . + "45.37351388888889 -121.69591944444444"@en . + . + "2348.7888"^^ . + . + . + "1857"^^ . + "3428.6952"^^ . + . + "USGS Mount Hood South 45121-C6"@en . + . + "Mount Hood"@en . + "Wah Yan College, Hong Kong"@en . + . + "22.27408"^^ . + . + "22.27408 114.17615"@en . + . + . + "20000.0"^^ . + "1919"^^ . + . + . + . + . + "WYHK"@en . + . + "281 Queen's Road East"@en . + . + "Open"@en . + . + . + "Red, green, blue, white"@en . + . + . + "In Hoc Signo Vinces"@en . + . + . + "114.17615"^^ . + . + "26"@en . + . + "(\"In this sign you shall conquer\")"@en . + . + . + "944"^^ . + "President"@en . + . + . + "Headmaster"@en . + . + "Honour All Men, Love the Brotherhood, Fear God, Honour the King."@en . + "1552"^^ . + "Christ's Hospital"@en . + . + . + "18"^^ . + . + . + . + "Blue & Yellow"@en . + "831"^^ . + "11"^^ . + . + . + . + "Today's Best Country Hits"@en . + . + . + "4.0"^^ . + "WBIE-FM (1968-1981)"@en . + "R&R"@en . + "33.80722222222222 -84.33944444444444"@en . + "33.80722222222222"^^ . + . + "Radio License Holding II, LLC, Debtor in possession"@en . + "WKHX (1981-1987)"@en . + . + "1.015E8"^^ . + . + "C0"@en . + "73161"^^ . + "WKHX-FM"@en . + "-84.33944444444444"^^ . + . + "Kicks 101.5"@en . + . + . + "1.8288"^^ . + "Left"@en . + "1990"^^ . + . + . + "1991"^^ . + . + "Centre"@en . + "6th overall"@en . + "1973-07-20"^^ . + . + . + . + . + . + . + "95256.0"^^ . + . + "Sunk by Surface Craft, 09 February 1944"@en . + . + "U-238"@en . + "1943-01-07"^^ . + "1943-02-20"^^ . + . + "1942-04-21"^^ . + "1971-11-08"^^ . + "4"^^ . + . + . + . + . + . + . + . + "Stairway to Heaven"@en . + . + . + "475.0"^^ . + "5800000"^^ . + "Frankfurt am Main"@en . + "069, 06109, 06101"@en . + "60001-60599, 65901-65936"@en . + "2008-03-31"^^ . + "651899"^^ . + "50.110277777777775 8.682222222222222"@en . + . + "0001"^^ . + "8.682222222222222"^^ . + . + "2.4831E8"^^ . + . + "50.110277777777775"^^ . + . + . + . + . + . + . + . + . + . + "2"^^ . + . + "3000.0"^^ . + . + . + . + "1965-09-30"^^ . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + . + "32"^^ . + . + . + . + . + . + "Thunderbirds"@en . + . + "3.6908352E9"^^ . + "57.0"^^ . + "6.833E9"^^ . + "2.748938461E12"^^ . + "1270.0"^^ . + "2.6610418080000005E9"^^ . + "2.748860873947125E12"^^ . + "49.0"^^ . + "1781-03-13"^^ . + "0.51"^^ . + "2.661031504799999E9"^^ . + "J2000"@en . + "Uranus"@en . + "63.086"^^ . + "76680.0"^^ . + "5.9"^^ . + "3.004374037087353E12"^^ . + "76.0"^^ . + "53.0"^^ . + "3.004419704E12"^^ . + "24516.0"^^ . + "0.3"^^ . + . + . + "0230"@en . + . + "Winterthur"@en . + . + . + "687.0"^^ . + . + "8400"@en . + . + . + . + . + "100000"^^ . + . + "6.793E7"^^ . + . + . + . + . + "8.75"^^ . + . + "439.0"^^ . + . + . + . + . + . + . + "2008"^^ . + "393.0"^^ . + . + "47.5 8.75"@en . + "47.5"^^ . + . + "327,452 active personnel"@en . + . + . + . + . + "Ultramarine Blue & Air Force Yellow"@en . + . + . + "United States Air Force"@en . + . + . + . + . + . + . + . + "1947"^^ . + . + . + . + "450 ICBMs"@en . + . + . + . + . + . + . + . + . + . + . + . + "50px"@en . + . + . + . + "5,573 aircraft, of which 2,132 are fighters"@en . + . + . + . + . + . + . + . + . + . + "\"To fly, fight and win ... in air, space and cyberspace.\""@en . + . + . + "32 satellites"@en . + "\"Above All\" (as of 19 Feb. 2008)"@en . + . + . + . + . + . + . + . + . diff --git a/tests/db_adapter_depended/ARC2_ClassTest.php b/tests/db_adapter_depended/ARC2_ClassTest.php new file mode 100644 index 00000000..9b4cee75 --- /dev/null +++ b/tests/db_adapter_depended/ARC2_ClassTest.php @@ -0,0 +1,67 @@ +store = \ARC2::getStore($this->dbConfig); + $this->store->createDBCon(); + $this->store->setup(); + $this->dbConnection = $this->store->getDBCon(); + + $this->fixture = new \ARC2_Class($this->dbConfig, $this); + + if ('mysqli' !== $this->dbConfig['db_adapter']) { + $this->markTestSkipped('Db adapter is not mysqli, therefore skip tests with queryDB.'); + } + } + + /* + * Tests for queryDB + */ + + public function testQueryDB() + { + $this->store->createDBCon(); + $this->store->setup(); + + $result = $this->fixture->queryDB('SHOW TABLES', $this->dbConnection); + $this->assertEquals(1, $result->field_count); + $this->assertTrue(0 < $result->num_rows); + } + + public function testQueryDBInvalidQuery() + { + $result = $this->fixture->queryDB('invalid-query', $this->dbConnection); + $this->assertFalse($result); + } + + public function testQueryDBInvalidQueryWithLog() + { + $result = $this->fixture->queryDB('invalid-query', $this->dbConnection, true); + $this->assertFalse($result); + + if ('mysql' == $this->store->getDBSName()) { + $dbsName = 'MySQL'; + } else { + $dbsName = 'MariaDB'; + } + + $this->assertEquals( + [ + 'You have an error in your SQL syntax; check the manual that corresponds to your ' + .$dbsName.' server version for the right syntax to use near \'invalid-query\' at line 1' + ], + $this->fixture->errors + ); + } +} diff --git a/tests/db_adapter_depended/sparql_1_1_tests/AggregatesTest.php b/tests/db_adapter_depended/sparql_1_1_tests/AggregatesTest.php new file mode 100644 index 00000000..845d7dc3 --- /dev/null +++ b/tests/db_adapter_depended/sparql_1_1_tests/AggregatesTest.php @@ -0,0 +1,113 @@ +w3cTestsFolderPath = __DIR__.'/w3c-tests/aggregates'; + $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/aggregates/manifest#'; + } + + /* + * tests + */ + + public function test_agg_avg_01() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + + $testname = 'agg-avg-01'; + + // get test data + $data = $this->getTestData($this->testPref . $testname); + + // load test data into graph + $this->store->insert($data, $this->dataGraphUri); + + // get query to test + $testQuery = $this->getTestQuery($this->testPref . $testname); + + // get expected result + $expectedResult = $this->getExpectedResult($this->testPref . $testname); + + // get actual result for given test query + $actualResult = $this->store->query($testQuery); + $actualResultAsXml = $this->getXmlVersionOfResult($actualResult); + + $this->assertEquals( + '2', + (string) $actualResultAsXml->results->result->binding->literal[0] + ); + + // remember current behavior, but skip test anyway to show developer here is still a problem. + $this->markTestSkipped( + 'Rounding bug in AVG function. See https://github.com/semsol/arc2/issues/99' + ); + } + + public function test_agg_empty_group() + { + $this->runTestFor('agg-empty-group'); + } + + public function test_agg_min_01() + { + $this->runTestFor('agg-min-01'); + } + + public function test_agg01() + { + $this->runTestFor('agg01'); + } + + public function test_agg02() + { + $this->runTestFor('agg02'); + } + + public function test_agg04() + { + $this->runTestFor('agg04'); + } + + public function test_agg05() + { + $this->runTestFor('agg05'); + } + + public function test_agg08() + { + $this->runTestFor('agg08'); + } + + public function test_agg09() + { + $this->runTestFor('agg09'); + } + + public function test_agg10() + { + $this->runTestFor('agg10'); + } + + public function test_agg11() + { + $this->runTestFor('agg11'); + } + + public function test_agg12() + { + $this->runTestFor('agg12'); + } +} diff --git a/tests/sparql11/ComplianceTest.php b/tests/db_adapter_depended/sparql_1_1_tests/ComplianceTest.php similarity index 99% rename from tests/sparql11/ComplianceTest.php rename to tests/db_adapter_depended/sparql_1_1_tests/ComplianceTest.php index 213c85f5..b42f2b30 100644 --- a/tests/sparql11/ComplianceTest.php +++ b/tests/db_adapter_depended/sparql_1_1_tests/ComplianceTest.php @@ -1,6 +1,6 @@ store->queryDB( + $row = $this->store->getDBObject()->fetchRow( 'SELECT COUNT(*) as count FROM '. $tableName, $this->store->getDBCon() ); - $row = $result->fetch_assoc(); return $row['count']; } diff --git a/tests/sparql11/ConstructTest.php b/tests/db_adapter_depended/sparql_1_1_tests/ConstructTest.php similarity index 89% rename from tests/sparql11/ConstructTest.php rename to tests/db_adapter_depended/sparql_1_1_tests/ConstructTest.php index 6077bf06..ea834379 100644 --- a/tests/sparql11/ConstructTest.php +++ b/tests/db_adapter_depended/sparql_1_1_tests/ConstructTest.php @@ -1,6 +1,6 @@ loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'constructwhere01'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - public function test_constructwhere02() { $this->runTestFor('constructwhere02'); diff --git a/tests/db_adapter_depended/sparql_1_1_tests/DropTest.php b/tests/db_adapter_depended/sparql_1_1_tests/DropTest.php new file mode 100644 index 00000000..f07c1f5b --- /dev/null +++ b/tests/db_adapter_depended/sparql_1_1_tests/DropTest.php @@ -0,0 +1,74 @@ +w3cTestsFolderPath = __DIR__.'/w3c-tests/drop'; + $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/drop/manifest#'; + } + + /** + * Helper function to get test query for a given test. + * + * @param string $testUri + * @return string Query to test. + */ + protected function getTestQuery($testUri) + { + /* + example: + + :group1 mf:action [ + qt:query + ] + */ + $query = $this->store->query(' + PREFIX mf: . + PREFIX ut: . + SELECT * FROM <'. $this->manifestGraphUri .'> WHERE { + <'. $testUri .'> mf:action [ ut:request ?queryFile ] . + } + '); + + return $query['result']['rows'][0]['queryFile']; + } + + /* + * tests + */ + + // this test is not part of the W3C test collection + // it tests DELETE FROM <...> command which is the ARC2 equivalent to DROP GRAPH <...> + public function test_delete_graph() + { + $graphUri = 'http://example.org/g1'; + + $this->store->query('INSERT INTO <'.$graphUri.'> { + "G1" ; + "Graph 1" . + }'); + + // check if graph really contains data + $res = $this->store->query('SELECT * WHERE {?s ?p ?o.}'); + $this->assertTrue(0 < count($res['result']['rows']), 'No test data in graph found.'); + + // run test query + $res = $this->store->query('DELETE FROM <'. $graphUri .'>'); + + // check if test data are still available + $res = $this->store->query('SELECT * FROM <'. $graphUri .'> WHERE {?s ?p ?o.}'); + $this->assertTrue(0 == count($res['result']['rows'])); + } +} diff --git a/tests/db_adapter_depended/sparql_1_1_tests/SyntaxUpdate1Test.php b/tests/db_adapter_depended/sparql_1_1_tests/SyntaxUpdate1Test.php new file mode 100644 index 00000000..e7646bfa --- /dev/null +++ b/tests/db_adapter_depended/sparql_1_1_tests/SyntaxUpdate1Test.php @@ -0,0 +1,239 @@ +w3cTestsFolderPath = __DIR__.'/w3c-tests/syntax-update-1'; + $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/syntax-update-1/manifest#'; + } + + /** + * Helper function to get test query for a given test. + * + * @param string $testUri + * @return string Query to test. + */ + protected function getTestQuery($testUri) + { + /* + example: + + :test_1 rdf:type mf:PositiveUpdateSyntaxTest11 ; + dawgt:approval dawgt:Approved ; + dawgt:approvedBy ; + mf:name "syntax-update-01.ru" ; + mf:action ;. + */ + $query = $this->store->query(' + PREFIX mf: . + SELECT * FROM <'. $this->manifestGraphUri .'> WHERE { + <'. $testUri .'> mf:action ?queryFile . + } + '); + + return file_get_contents($query['result']['rows'][0]['queryFile']); + } + + /* + * tests + */ + + public function test_test_1() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_1'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertTrue(is_array($result) && isset($result['query_type'])); + } + + public function test_test_2() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_2'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertTrue(is_array($result) && isset($result['query_type'])); + } + + public function test_test_41() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_41'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_42() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_42'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_43() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_43'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_44() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_44'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_45() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_45'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_46() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_46'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_47() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_47'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_48() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_48'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_49() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_49'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_50() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_50'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_51() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_51'); + + // fire query + $result = $this->store->query($query); + + // check current reaction of ARC2, for compatible reasons + $this->assertTrue(is_array($result)); + + // check result + $this->markTestSkipped( + 'Query has to fail, but ARC2 returns an array as if query is considered valid. Query: ' + .PHP_EOL + .$this->makeQueryA1Liner($query) + ); + } + + public function test_test_52() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_52'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } + + public function test_test_54() + { + $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); + $query = $this->getTestQuery($this->testPref . 'test_54'); + + // fire query + $result = $this->store->query($query); + + // check result + $this->assertEquals(0, $result); + } +} diff --git a/tests/sparql11/w3c-tests/aggregates/agg-avg-01.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-avg-01.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-avg-01.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-avg-01.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-avg-01.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-avg-01.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-avg-01.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-avg-01.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-avg-02.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-avg-02.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-avg-02.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-avg-02.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-avg-02.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-avg-02.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-avg-02.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-avg-02.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-empty-group.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-empty-group.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-empty-group.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-empty-group.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-empty-group.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-empty-group.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-empty-group.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-empty-group.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-err-01.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-01.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-err-01.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-01.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-err-01.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-01.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-err-01.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-01.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-err-01.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-01.ttl similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-err-01.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-01.ttl diff --git a/tests/sparql11/w3c-tests/aggregates/agg-err-02.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-02.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-err-02.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-02.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-err-02.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-02.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-err-02.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-02.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-err-02.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-02.ttl similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-err-02.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-err-02.ttl diff --git a/tests/sparql11/w3c-tests/aggregates/agg-groupconcat-1.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-1.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-groupconcat-1.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-1.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-groupconcat-1.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-1.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-groupconcat-1.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-1.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-groupconcat-1.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-1.ttl similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-groupconcat-1.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-1.ttl diff --git a/tests/sparql11/w3c-tests/aggregates/agg-groupconcat-2.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-2.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-groupconcat-2.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-2.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-groupconcat-2.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-2.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-groupconcat-2.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-2.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-groupconcat-3.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-3.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-groupconcat-3.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-3.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-groupconcat-3.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-3.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-groupconcat-3.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-groupconcat-3.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-max-01.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-max-01.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-max-01.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-max-01.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-max-01.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-max-01.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-max-01.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-max-01.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-max-02.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-max-02.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-max-02.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-max-02.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-max-02.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-max-02.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-max-02.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-max-02.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-min-01.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-min-01.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-min-01.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-min-01.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-min-01.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-min-01.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-min-01.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-min-01.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-min-02.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-min-02.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-min-02.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-min-02.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-min-02.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-min-02.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-min-02.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-min-02.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-numeric.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-numeric.ttl similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-numeric.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-numeric.ttl diff --git a/tests/sparql11/w3c-tests/aggregates/agg-numeric2.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-numeric2.ttl similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-numeric2.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-numeric2.ttl diff --git a/tests/sparql11/w3c-tests/aggregates/agg-sample-01.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sample-01.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-sample-01.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sample-01.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-sample-01.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sample-01.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-sample-01.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sample-01.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-sum-01.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sum-01.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-sum-01.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sum-01.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-sum-01.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sum-01.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-sum-01.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sum-01.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg-sum-02.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sum-02.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-sum-02.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sum-02.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg-sum-02.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sum-02.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg-sum-02.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg-sum-02.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg01.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg01.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg01.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg01.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg01.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg01.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg01.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg01.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg01.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg01.ttl similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg01.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg01.ttl diff --git a/tests/sparql11/w3c-tests/aggregates/agg02.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg02.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg02.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg02.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg02.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg02.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg02.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg02.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg03.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg03.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg03.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg03.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg03.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg03.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg03.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg03.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg04.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg04.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg04.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg04.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg04.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg04.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg04.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg04.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg05.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg05.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg05.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg05.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg05.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg05.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg05.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg05.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg06.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg06.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg06.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg06.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg06.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg06.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg06.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg06.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg07.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg07.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg07.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg07.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg07.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg07.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg07.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg07.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg08.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg08.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg08.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg08.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg08.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg08.ttl similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg08.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg08.ttl diff --git a/tests/sparql11/w3c-tests/aggregates/agg08b.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg08b.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg08b.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg08b.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg08b.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg08b.srx similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg08b.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg08b.srx diff --git a/tests/sparql11/w3c-tests/aggregates/agg09.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg09.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg09.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg09.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg10.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg10.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg10.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg10.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg11.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg11.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg11.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg11.rq diff --git a/tests/sparql11/w3c-tests/aggregates/agg12.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg12.rq similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/agg12.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/agg12.rq diff --git a/tests/sparql11/w3c-tests/aggregates/empty.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/empty.ttl similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/empty.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/empty.ttl diff --git a/tests/sparql11/w3c-tests/aggregates/manifest.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/manifest.ttl similarity index 100% rename from tests/sparql11/w3c-tests/aggregates/manifest.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/aggregates/manifest.ttl diff --git a/tests/sparql11/w3c-tests/construct/constructwhere01.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere01.rq similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere01.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere01.rq diff --git a/tests/sparql11/w3c-tests/construct/constructwhere01result.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere01result.ttl similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere01result.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere01result.ttl diff --git a/tests/sparql11/w3c-tests/construct/constructwhere02.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere02.rq similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere02.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere02.rq diff --git a/tests/sparql11/w3c-tests/construct/constructwhere02result.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere02result.ttl similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere02result.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere02result.ttl diff --git a/tests/sparql11/w3c-tests/construct/constructwhere03.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere03.rq similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere03.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere03.rq diff --git a/tests/sparql11/w3c-tests/construct/constructwhere03result.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere03result.ttl similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere03result.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere03result.ttl diff --git a/tests/sparql11/w3c-tests/construct/constructwhere04.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere04.rq similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere04.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere04.rq diff --git a/tests/sparql11/w3c-tests/construct/constructwhere04result.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere04result.ttl similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere04result.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere04result.ttl diff --git a/tests/sparql11/w3c-tests/construct/constructwhere05.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere05.rq similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere05.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere05.rq diff --git a/tests/sparql11/w3c-tests/construct/constructwhere06.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere06.rq similarity index 100% rename from tests/sparql11/w3c-tests/construct/constructwhere06.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/constructwhere06.rq diff --git a/tests/sparql11/w3c-tests/construct/data.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/data.ttl similarity index 100% rename from tests/sparql11/w3c-tests/construct/data.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/data.ttl diff --git a/tests/sparql11/w3c-tests/construct/manifest.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/manifest.ttl similarity index 100% rename from tests/sparql11/w3c-tests/construct/manifest.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/construct/manifest.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-01.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-02.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-02.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-02.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-02.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-03.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-03.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-03.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-03.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-04.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-04.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-04.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-04.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-05.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-05.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-05.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-05.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-06.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-06.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-06.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-06.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-07.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-07.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-07.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-07.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-post-01f.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-01f.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-post-01f.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-01f.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-post-01s.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-01s.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-post-01s.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-01s.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-post-01s2.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-01s2.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-post-01s2.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-01s2.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-post-02f.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-02f.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-post-02f.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-02f.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-post-02s.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-02s.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-post-02s.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-02s.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-post-03f.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-03f.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-post-03f.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-post-03f.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-pre-01.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-pre-01.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-pre-01.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-pre-01.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-pre-02.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-pre-02.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-pre-02.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-pre-02.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-pre-03.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-pre-03.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-pre-03.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-pre-03.ttl diff --git a/tests/sparql11/w3c-tests/delete/delete-using-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-using-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-01.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-using-02.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-02.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-using-02.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-02.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-using-03.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-03.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-using-03.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-03.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-using-04.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-04.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-using-04.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-04.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-using-05.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-05.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-using-05.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-05.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-using-06.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-06.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-using-06.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-using-06.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-with-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-with-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-01.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-with-02.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-02.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-with-02.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-02.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-with-03.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-03.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-with-03.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-03.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-with-04.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-04.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-with-04.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-04.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-with-05.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-05.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-with-05.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-05.ru diff --git a/tests/sparql11/w3c-tests/delete/delete-with-06.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-06.ru similarity index 100% rename from tests/sparql11/w3c-tests/delete/delete-with-06.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/delete-with-06.ru diff --git a/tests/sparql11/w3c-tests/delete/manifest.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/manifest.ttl similarity index 100% rename from tests/sparql11/w3c-tests/delete/manifest.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/delete/manifest.ttl diff --git a/tests/sparql11/w3c-tests/drop/drop-all-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-all-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/drop/drop-all-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-all-01.ru diff --git a/tests/sparql11/w3c-tests/drop/drop-default-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-default-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/drop/drop-default-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-default-01.ru diff --git a/tests/sparql11/w3c-tests/drop/drop-default.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-default.ttl similarity index 100% rename from tests/sparql11/w3c-tests/drop/drop-default.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-default.ttl diff --git a/tests/sparql11/w3c-tests/drop/drop-g1.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-g1.ttl similarity index 100% rename from tests/sparql11/w3c-tests/drop/drop-g1.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-g1.ttl diff --git a/tests/sparql11/w3c-tests/drop/drop-g2.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-g2.ttl similarity index 100% rename from tests/sparql11/w3c-tests/drop/drop-g2.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-g2.ttl diff --git a/tests/sparql11/w3c-tests/drop/drop-graph-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-graph-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/drop/drop-graph-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-graph-01.ru diff --git a/tests/sparql11/w3c-tests/drop/drop-named-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-named-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/drop/drop-named-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/drop-named-01.ru diff --git a/tests/sparql11/w3c-tests/drop/manifest.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/manifest.ttl similarity index 100% rename from tests/sparql11/w3c-tests/drop/manifest.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/drop/manifest.ttl diff --git a/tests/sparql11/w3c-tests/exists/exists01.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists01.rq similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists01.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists01.rq diff --git a/tests/sparql11/w3c-tests/exists/exists01.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists01.srx similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists01.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists01.srx diff --git a/tests/sparql11/w3c-tests/exists/exists01.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists01.ttl similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists01.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists01.ttl diff --git a/tests/sparql11/w3c-tests/exists/exists02.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists02.rq similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists02.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists02.rq diff --git a/tests/sparql11/w3c-tests/exists/exists02.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists02.srx similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists02.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists02.srx diff --git a/tests/sparql11/w3c-tests/exists/exists02.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists02.ttl similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists02.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists02.ttl diff --git a/tests/sparql11/w3c-tests/exists/exists03.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists03.rq similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists03.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists03.rq diff --git a/tests/sparql11/w3c-tests/exists/exists03.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists03.srx similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists03.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists03.srx diff --git a/tests/sparql11/w3c-tests/exists/exists04.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists04.rq similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists04.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists04.rq diff --git a/tests/sparql11/w3c-tests/exists/exists04.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists04.srx similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists04.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists04.srx diff --git a/tests/sparql11/w3c-tests/exists/exists05.rq b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists05.rq similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists05.rq rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists05.rq diff --git a/tests/sparql11/w3c-tests/exists/exists05.srx b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists05.srx similarity index 100% rename from tests/sparql11/w3c-tests/exists/exists05.srx rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/exists05.srx diff --git a/tests/sparql11/w3c-tests/exists/manifest.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/manifest.ttl similarity index 100% rename from tests/sparql11/w3c-tests/exists/manifest.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/exists/manifest.ttl diff --git a/tests/sparql11/w3c-tests/move/manifest.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/manifest.ttl similarity index 100% rename from tests/sparql11/w3c-tests/move/manifest.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/manifest.ttl diff --git a/tests/sparql11/w3c-tests/move/move-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/move/move-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-01.ru diff --git a/tests/sparql11/w3c-tests/move/move-01.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-01.ttl similarity index 100% rename from tests/sparql11/w3c-tests/move/move-01.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-01.ttl diff --git a/tests/sparql11/w3c-tests/move/move-02.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-02.ttl similarity index 100% rename from tests/sparql11/w3c-tests/move/move-02.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-02.ttl diff --git a/tests/sparql11/w3c-tests/move/move-03.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-03.ru similarity index 100% rename from tests/sparql11/w3c-tests/move/move-03.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-03.ru diff --git a/tests/sparql11/w3c-tests/move/move-06.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-06.ru similarity index 100% rename from tests/sparql11/w3c-tests/move/move-06.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-06.ru diff --git a/tests/sparql11/w3c-tests/move/move-07.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-07.ru similarity index 100% rename from tests/sparql11/w3c-tests/move/move-07.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-07.ru diff --git a/tests/sparql11/w3c-tests/move/move-default.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-default.ttl similarity index 100% rename from tests/sparql11/w3c-tests/move/move-default.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/move/move-default.ttl diff --git a/tests/sparql11/w3c-tests/syntax-update-1/manifest.ttl b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/manifest.ttl similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/manifest.ttl rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/manifest.ttl diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-01.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-02.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-02.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-02.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-02.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-03.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-03.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-03.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-03.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-04.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-04.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-04.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-04.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-05.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-05.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-05.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-05.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-06.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-06.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-06.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-06.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-07.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-07.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-07.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-07.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-08.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-08.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-08.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-08.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-09.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-09.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-09.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-09.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-10.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-10.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-10.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-10.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-11.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-11.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-11.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-11.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-12.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-12.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-12.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-12.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-13.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-13.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-13.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-13.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-14.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-14.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-14.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-14.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-15.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-15.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-15.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-15.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-16.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-16.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-16.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-16.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-17.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-17.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-17.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-17.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-18.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-18.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-18.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-18.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-19.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-19.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-19.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-19.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-20.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-20.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-20.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-20.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-21.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-21.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-21.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-21.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-22.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-22.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-22.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-22.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-23.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-23.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-23.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-23.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-24.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-24.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-24.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-24.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-25.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-25.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-25.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-25.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-26.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-26.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-26.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-26.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-27.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-27.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-27.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-27.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-28.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-28.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-28.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-28.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-29.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-29.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-29.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-29.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-30.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-30.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-30.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-30.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-31.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-31.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-31.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-31.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-32.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-32.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-32.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-32.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-33.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-33.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-33.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-33.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-34.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-34.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-34.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-34.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-35.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-35.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-35.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-35.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-36.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-36.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-36.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-36.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-37.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-37.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-37.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-37.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-38.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-38.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-38.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-38.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-39.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-39.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-39.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-39.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-40.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-40.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-40.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-40.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-53.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-53.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-53.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-53.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-54.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-54.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-54.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-54.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-01.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-01.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-01.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-01.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-02.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-02.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-02.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-02.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-03.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-03.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-03.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-03.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-04.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-04.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-04.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-04.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-05.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-05.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-05.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-05.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-06.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-06.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-06.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-06.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-07.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-07.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-07.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-07.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-08.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-08.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-08.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-08.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-09.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-09.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-09.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-09.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-10.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-10.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-10.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-10.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-11.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-11.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-11.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-11.ru diff --git a/tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-12.ru b/tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-12.ru similarity index 100% rename from tests/sparql11/w3c-tests/syntax-update-1/syntax-update-bad-12.ru rename to tests/db_adapter_depended/sparql_1_1_tests/w3c-tests/syntax-update-1/syntax-update-bad-12.ru diff --git a/tests/db_adapter_depended/src/ARC2/Store/Adapter/CachedPDOAdapterTest.php b/tests/db_adapter_depended/src/ARC2/Store/Adapter/CachedPDOAdapterTest.php new file mode 100644 index 00000000..0b05e95a --- /dev/null +++ b/tests/db_adapter_depended/src/ARC2/Store/Adapter/CachedPDOAdapterTest.php @@ -0,0 +1,96 @@ +markTestSkipped('Test skipped, because extension pdo_mysql is not installed.'); + } + + // stop, if pdo_db_protocol is not set in dbConfig + if (false == isset($this->dbConfig['db_pdo_protocol'])) { + $this->markTestSkipped( + 'Test skipped, because db_pdo_protocol is not set. Its ok, if this happens in unit test environment.' + ); + } + + parent::setUp(); + + $this->dbConfig['cache_enabled'] = true; + + $this->fixture = new CachedPDOAdapter($this->dbConfig); + + // remove all tables + $tables = $this->fixture->fetchList('SHOW TABLES'); + foreach($tables as $table) { + $this->fixture->simpleQuery('DROP TABLE '. $table['Tables_in_'.$this->dbConfig['db_name']]); + } + } + + public function testFetchRow() + { + // create test data + $this->fixture->simpleQuery('CREATE TABLE users (id INT(6), name VARCHAR(30) NOT NULL)'); + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (1, "foobar");'); + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (2, "foobar2");'); + + $selectQuery = 'SELECT * FROM users WHERE id = 2'; + + // check that cache doesnt know $selectQuery yet. + $this->assertFalse($this->fixture->getCacheInstance()->has(hash('sha1', $selectQuery))); + + $user = $this->fixture->fetchRow($selectQuery); + + // check cache, that expected entry is available + $this->assertTrue($this->fixture->getCacheInstance()->has(hash('sha1', $selectQuery))); + + // check if $users is equal to the cached version + $this->assertEquals($user, $this->fixture->fetchRow($selectQuery)); + } + + public function testFetchList() + { + // create test data + $this->fixture->simpleQuery('CREATE TABLE users (id INT(6), name VARCHAR(30) NOT NULL)'); + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (1, "foobar");'); + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (2, "foobar2");'); + + $selectQuery = 'SELECT * FROM users'; + + // check that cache doesnt know $selectQuery yet. + $this->assertFalse($this->fixture->getCacheInstance()->has(hash('sha1', $selectQuery))); + + $users = $this->fixture->fetchList($selectQuery); + + // check cache, that expected entry is available + $this->assertTrue($this->fixture->getCacheInstance()->has(hash('sha1', $selectQuery))); + + // check if $users is equal to the cached version + $this->assertEquals($users, $this->fixture->fetchList($selectQuery)); + } + + public function testCacheInvalidationIfDBChanges() + { + // create test data + $this->fixture->simpleQuery('CREATE TABLE users (id INT(6), name VARCHAR(30) NOT NULL)'); + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (1, "foobar");'); + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (2, "foobar2");'); + + $selectQuery = 'SELECT * FROM users'; + + $users = $this->fixture->fetchList($selectQuery); + $this->assertTrue($this->fixture->getCacheInstance()->has(hash('sha1', $selectQuery))); + + // change table and therefore the DB => invalidation of the cache + $this->fixture->exec('DELETE FROM users WHERE id = 1'); + + $this->assertFalse($this->fixture->getCacheInstance()->has(hash('sha1', $selectQuery))); + } +} diff --git a/tests/integration/store/ARC2_StoreAskQueryHandlerTest.php b/tests/db_adapter_depended/store/ARC2_StoreAskQueryHandlerTest.php similarity index 85% rename from tests/integration/store/ARC2_StoreAskQueryHandlerTest.php rename to tests/db_adapter_depended/store/ARC2_StoreAskQueryHandlerTest.php index eeafd4d9..d4e6beee 100644 --- a/tests/integration/store/ARC2_StoreAskQueryHandlerTest.php +++ b/tests/db_adapter_depended/store/ARC2_StoreAskQueryHandlerTest.php @@ -1,6 +1,6 @@ fixture = new \ARC2_StoreAskQueryHandler($this->store->a, $this->store); } + public function tearDown() + { + $this->store->closeDBCon(); + } + /* * Tests for __init */ diff --git a/tests/integration/store/ARC2_StoreInsertQueryHandlerTest.php b/tests/db_adapter_depended/store/ARC2_StoreInsertQueryHandlerTest.php similarity index 85% rename from tests/integration/store/ARC2_StoreInsertQueryHandlerTest.php rename to tests/db_adapter_depended/store/ARC2_StoreInsertQueryHandlerTest.php index c1bb21bf..5f550651 100644 --- a/tests/integration/store/ARC2_StoreInsertQueryHandlerTest.php +++ b/tests/db_adapter_depended/store/ARC2_StoreInsertQueryHandlerTest.php @@ -1,6 +1,6 @@ fixture = new \ARC2_StoreInsertQueryHandler($this->store->a, $this->store); } + public function tearDown() + { + $this->store->closeDBCon(); + } + /* * Tests for __init */ diff --git a/tests/db_adapter_depended/store/ARC2_StoreTest.php b/tests/db_adapter_depended/store/ARC2_StoreTest.php new file mode 100644 index 00000000..f171d92f --- /dev/null +++ b/tests/db_adapter_depended/store/ARC2_StoreTest.php @@ -0,0 +1,778 @@ +fixture = \ARC2::getStore($this->dbConfig); + $this->fixture->createDBCon(); + + // remove all tables + $tables = $this->fixture->getDBObject()->fetchList('SHOW TABLES'); + foreach($tables as $table) { + $this->fixture->getDBObject()->simpleQuery('DROP TABLE '. $table['Tables_in_'.$this->dbConfig['db_name']]); + } + + // fresh setup of ARC2 + $this->fixture->setup(); + } + + public function tearDown() + { + $this->fixture->closeDBCon(); + } + + /** + * Returns a list of all available graph URIs of the store. It can also respect access control, + * to only returned available graphs in the current context. But that depends on the implementation + * and can differ. + * + * @return array simple array of key-value-pairs, which consists of graph URIs as key and NamedNode + * instance as value + */ + protected function getGraphs() + { + // g2t table + if (isset($this->dbConfig['db_table_prefix'])) { + $g2t = $this->dbConfig['db_table_prefix'] . '_'; + } else { + $g2t = ''; + } + if (isset($this->dbConfig['store_name'])) { + $g2t .= $this->dbConfig['store_name'] . '_'; + } + $g2t .= 'g2t'; + + // id2val table + if (isset($this->dbConfig['db_table_prefix'])) { + $id2val = $this->dbConfig['db_table_prefix'] . '_'; + } else { + $id2val = ''; + } + if (isset($this->dbConfig['store_name'])) { + $id2val .= $this->dbConfig['store_name'] . '_'; + } + $id2val .= 'id2val'; + + // collects all values which have an ID (column g) in the g2t table. + $query = 'SELECT id2val.val AS graphUri + FROM '.$g2t.' g2t + LEFT JOIN '.$id2val.' id2val ON g2t.g = id2val.id + GROUP BY g'; + + // send SQL query + $list = $this->fixture->getDBObject()->fetchList($query); + $graphs = []; + + // collect graph URI's + foreach($list as $row) { + $graphs[] = $row['graphUri']; + } + + return $graphs; + } + + public function testSetup() + { + $this->fixture->reset(); + + $this->fixture->setup(); + } + + /* + * Tests for caching behavior + */ + + public function testCaching() + { + if (false == $this->fixture->cacheEnabled()) { + $this->markTestSkipped('Skip tests of ARC2_Store caching, because cache is not enabled.'); + } + + // add test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $selectQuery = 'SELECT * FROM {?s ?p ?o.}'; + + // check that query is not known in cache + $this->assertFalse($this->dbConfig['cache_instance']->has(\hash('sha1', $selectQuery))); + + $result = $this->fixture->query($selectQuery); + unset($result['query_time']); + $this->assertEquals(1, \count($result['result']['rows'])); + + $this->assertTrue($this->dbConfig['cache_instance']->has(\hash('sha1', $selectQuery))); + + // compare cached and raw result + $cachedResult = $this->fixture->query($selectQuery); + unset($cachedResult['query_time']); + $this->assertEquals($result, $cachedResult); + } + + /* + * Tests for changeNamespaceURI + */ + + public function testChangeNamespaceURIEmptyStore() + { + $res = $this->fixture->changeNamespaceURI( + 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', + 'urn:rdf' + ); + + $this->assertEquals( + [ + 'id_replacements' => 0, + 'triple_updates' => 0, + ], + $res + ); + } + + public function testChangeNamespaceURIFilledStore() + { + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $res = $this->fixture->changeNamespaceURI( + 'http://pref/', + 'urn:rdf' + ); + + $this->assertEquals( + [ + 'id_replacements' => 2, + 'triple_updates' => 0, + ], + $res + ); + } + + /* + * Tests for countDBProcesses + */ + + public function testCountDBProcesses() + { + $this->assertTrue(is_integer($this->fixture->countDBProcesses())); + } + + /* + * Tests for createBackup + */ + + public function testCreateBackup() + { + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $this->fixture->createBackup('/tmp/backup.txt'); + + $expectedXML = << + + + + + + + + + + + http://s + + + http://p1 + + + baz + + + http://example.com/ + + + + + +XML; + $this->assertEquals(file_get_contents('/tmp/backup.txt'), $expectedXML); + } + + /* + * Tests for closeDBCon + */ + + public function testCloseDBCon() + { + $this->assertTrue(isset($this->fixture->a['db_object'])); + + $this->fixture->closeDBCon(); + + $this->assertFalse(isset($this->fixture->a['db_object'])); + } + + /* + * Tests for delete + */ + + public function testDelete() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + "label1" . + }'); + + $res = $this->fixture->query('SELECT * WHERE {?s ?p ?o.}'); + $this->assertEquals(2, \count($res['result']['rows'])); + + // remove graph + $this->fixture->delete(false, 'http://example.com/'); + + $res = $this->fixture->query('SELECT * WHERE {?s ?p ?o.}'); + $this->assertEquals(0, \count($res['result']['rows'])); + } + + /* + * Tests for drop + */ + + public function testDrop() + { + // make sure all tables were created + $this->fixture->setup(); + $tables = $this->fixture->getDBObject()->fetchList('SHOW TABLES'); + $this->assertEquals(6, \count($tables)); + + // remove all tables + $this->fixture->drop(); + + // check that all tables were removed + $tables = $this->fixture->getDBObject()->fetchList('SHOW TABLES'); + $this->assertEquals(0, \count($tables)); + } + + /* + * Tests for dump + */ + + public function testDump() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + // fixed dump call using error_reporting to avoid + // Cannot modify header information - headers already sent by (output started at + // ./vendor/phpunit/phpunit/src/Util/Printer.php:110) + // thanks to https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-364024753 + error_reporting(0); + ob_start(); + $this->fixture->dump(); + $dumpContent = ob_get_clean(); + error_reporting(E_ALL); + + $expectedXML = << + + + + + + + + + + + http://s + + + http://p1 + + + baz + + + http://example.com/ + + + + + +XML; + $this->assertEquals($expectedXML, $dumpContent); + } + + /* + * Tests for enableFulltextSearch + */ + + public function testEnableFulltextSearch() + { + $res1 = $this->fixture->enableFulltextSearch(); + $res2 = $this->fixture->disableFulltextSearch(); + + $this->assertNull($res1); + $this->assertEquals(1, $res2); + + $this->assertEquals(0, $this->fixture->a['db_object']->getErrorCode()); + $this->assertEquals('', $this->fixture->a['db_object']->getErrorMessage()); + } + + /* + * Tests for getDBVersion + */ + + // just check pattern + public function testGetDBVersion() + { + $result = preg_match('/[0-9]{2}-[0-9]{2}-[0-9]{2}/', $this->fixture->getDBVersion(), $match); + $this->assertEquals(1, $result); + } + + /* + * Tests for getDBCon + */ + + public function testGetDBCon() + { + // TODO use a different check, if mariadb or mysql is used + $this->assertTrue(false !== $this->fixture->getDBCon()); + } + + /* + * Tests for getSetting and setSetting + */ + + public function testGetAndSetSetting() + { + $this->assertEquals(0, $this->fixture->getSetting('foo')); + + $this->fixture->setSetting('foo', 'bar'); + + $this->assertEquals('bar', $this->fixture->getSetting('foo')); + } + + public function testGetAndSetSettingUseDefault() + { + $this->assertEquals('no-entry', $this->fixture->getSetting('not-available-'.time(), 'no-entry')); + } + + public function testGetAndSetSettingExistingSetting() + { + $this->assertEquals(0, $this->fixture->getSetting('foo')); + + $this->fixture->setSetting('foo', 'bar'); + $this->fixture->setSetting('foo', 'bar2'); // overrides existing setting + + $this->assertEquals('bar2', $this->fixture->getSetting('foo')); + } + + /* + * Tests for getLabelProps + */ + + public function testGetLabelProps() + { + $this->assertEquals( + [ + 'http://www.w3.org/2000/01/rdf-schema#label', + 'http://xmlns.com/foaf/0.1/name', + 'http://purl.org/dc/elements/1.1/title', + 'http://purl.org/rss/1.0/title', + 'http://www.w3.org/2004/02/skos/core#prefLabel', + 'http://xmlns.com/foaf/0.1/nick', + ], + $this->fixture->getLabelProps() + ); + } + + /* + * Tests for getResourceLabel + */ + + public function testGetResourceLabel() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + "label1" . + }'); + + $res = $this->fixture->getResourceLabel('http://s'); + + $this->assertEquals('label1', $res); + } + + public function testGetResourceLabelNoData() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $res = $this->fixture->getResourceLabel('http://s'); + + $this->assertEquals('s', $res); + } + + /* + * Tests for getResourcePredicates + */ + + public function testGetResourcePredicates() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + "bar" . + }'); + + $res = $this->fixture->getResourcePredicates('http://s'); + + $this->assertEquals( + [ + 'http://p1' => [], + 'http://p2' => [], + ], + $res + ); + } + + public function testGetResourcePredicatesMultipleGraphs() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + "bar" . + }'); + + $this->fixture->query('INSERT INTO { + "baz" . + "bar" . + }'); + + $res = $this->fixture->getResourcePredicates('http://s'); + + $this->assertEquals( + [ + 'http://p1' => [], + 'http://p2' => [], + 'http://p3' => [], + 'http://p4' => [], + ], + $res + ); + } + + /* + * Tests for getPredicateRange + */ + + public function testGetPredicateRange() + { + // test data + $this->fixture->query('INSERT INTO { + . + }'); + + $res = $this->fixture->getPredicateRange('http://p1'); + + $this->assertEquals('http://foobar', $res); + } + + public function testGetPredicateRangeNotFound() + { + $res = $this->fixture->getPredicateRange('http://not-available'); + + $this->assertEquals('', $res); + } + + /* + * Tests for getIDValue + */ + + public function testGetIDValue() + { + $this->fixture->query('INSERT INTO { + . + }'); + + $res = $this->fixture->getIDValue(1); + + $this->assertEquals('http://example.com/', $res); + } + + public function testGetIDValueNoData() + { + $res = $this->fixture->getIDValue(1); + + $this->assertEquals(0, $res); + } + + /** + * Saft frameworks ARC2 addition fails to run with ARC2 2.4. + * + * https://github.com/SaftIng/Saft/tree/master/src/Saft/Addition/ARC2 + */ + public function testInsertSaftRegressionTest1() + { + $res = $this->fixture->query('SELECT * FROM WHERE { ?s ?p ?o. } '); + $this->assertEquals(0, count($res['result']['rows'])); + + $this->fixture->insert( + file_get_contents(__DIR__.'/../../data/nt/saft-arc2-addition-regression1.nt'), + 'http://example.com/' + ); + + $res1 = $this->fixture->query('SELECT * FROM WHERE { ?s ?p ?o. } '); + $this->assertEquals(442, count($res1['result']['rows'])); + + $res2 = $this->fixture->query('SELECT * WHERE { ?s ?p ?o. } '); + $this->assertEquals(442, count($res2['result']['rows'])); + } + + /** + * Saft frameworks ARC2 addition fails to run with ARC2 2.4. + * + * https://github.com/SaftIng/Saft/tree/master/src/Saft/Addition/ARC2 + * + * This tests checks gathering of freshly created resources. + */ + public function testInsertSaftRegressionTest2() + { + $res = $this->fixture->query('INSERT INTO { . }'); + + $res1 = $this->fixture->query('SELECT * FROM WHERE {?s ?p ?o.}'); + $this->assertEquals(1, count($res1['result']['rows'])); + + $res2 = $this->fixture->query('SELECT * WHERE {?s ?p ?o.}'); + $this->assertEquals(1, count($res2['result']['rows'])); + + $res2 = $this->fixture->query('SELECT ?s ?p ?o WHERE {?s ?p ?o.}'); + $this->assertEquals(1, count($res2['result']['rows'])); + } + + /** + * Saft frameworks ARC2 addition fails to run with ARC2 2.4. + * + * This test checks side effects of update operations on different graphs. + * + * We add 1 triple to 1 and another to another graph. Afterwards removing the first graph. + * In the end should the second graph still containg his triple. + */ + public function testInsertSaftRegressionTest3() + { + $this->fixture->query( + 'INSERT INTO { . }' + ); + $this->fixture->query( + 'INSERT INTO { . }' + ); + $this->fixture->query( + 'DELETE FROM ' + ); + + $res = $this->fixture->query('SELECT * FROM WHERE {?s ?p ?o.}'); + $this->assertEquals(1, count($res['result']['rows'])); + } + + public function testMultipleInsertQuerysInDifferentGraphs() + { + $this->fixture->query('INSERT INTO { . }'); + $this->fixture->query('INSERT INTO { . }'); + $this->fixture->query('INSERT INTO { . }'); + + $res = $this->fixture->query('SELECT * FROM WHERE {?s ?p ?o.}'); + $this->assertEquals(1, count($res['result']['rows'])); + + $res = $this->fixture->query('SELECT * FROM WHERE {?s ?p ?o.}'); + $this->assertEquals(1, count($res['result']['rows'])); + + $res = $this->fixture->query('SELECT * WHERE {?s ?p ?o.}'); + $this->assertEquals(3, count($res['result']['rows'])); + + $this->markTestSkipped( + 'Adding the same triple into two graphs does not work.' + . PHP_EOL . 'Bug report: https://github.com/semsol/arc2/issues/114' + ); + } + + /* + * Tests for logQuery + */ + + public function testLogQuery() + { + $logFile = 'arc_query_log.txt'; + + $this->assertFalse(file_exists($logFile)); + + $this->fixture->logQuery('query1'); + + $this->assertTrue(file_exists($logFile)); + unlink($logFile); + } + + /* + * Tests for renameTo + */ + + public function testRenameTo() + { + /* + * remove all tables + */ + $tables = $this->fixture->getDBObject()->fetchList('SHOW TABLES'); + foreach($tables as $table) { + $this->fixture->getDBObject()->simpleQuery('DROP TABLE '. $table['Tables_in_'.$this->fixture->a['db_name']]); + } + + /* + * create fresh store and check tables + */ + $this->fixture->setup(); + + $tables = $this->fixture->getDBObject()->fetchList('SHOW TABLES'); + foreach($tables as $table) { + $this->assertTrue( + false !== strpos($table['Tables_in_'.$this->fixture->a['db_name']], $this->dbConfig['db_table_prefix'].'_') + ); + } + + /* + * rename store + */ + $prefix = 'new_store'; + $this->fixture->renameTo($prefix); + + /* + * check for new prefixes + */ + $tables = $this->fixture->getDBObject()->fetchList('SHOW TABLES'); + foreach($tables as $table) { + $this->assertTrue( + false !== strpos($table['Tables_in_'.$this->fixture->a['db_name']], $prefix) + ); + } + } + + /* + * Tests for replace + */ + + public function testReplace() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + "label1" . + }'); + + $res = $this->fixture->query('SELECT * WHERE {?s ?p ?o.}'); + $this->assertEquals(2, \count($res['result']['rows'])); + + $this->assertEquals( + [ + 'http://original/' + ], + $this->getGraphs() + ); + + // replace graph + $returnVal = $this->fixture->replace(false, 'http://original/', 'http://replacement/'); + + // check triples + $res = $this->fixture->query('SELECT * FROM WHERE {?s ?p ?o.}'); + $this->assertEquals(0, \count($res['result']['rows'])); + + // get available graphs + $this->assertEquals(0, \count($this->getGraphs())); + + $res = $this->fixture->query('SELECT * FROM WHERE {?s ?p ?o.}'); + // TODO this does not makes sense, why are there no triples? + $this->assertEquals(0, \count($res['result']['rows'])); + + $res = $this->fixture->query('SELECT * WHERE {?s ?p ?o.}'); + // TODO this does not makes sense, why are there no triples? + $this->assertEquals(0, \count($res['result']['rows'])); + + // check return value + $this->assertEquals( + [ + [ + 't_count' => 2, + 'delete_time' => $returnVal[0]['delete_time'], + 'index_update_time' => $returnVal[0]['index_update_time'] + ], + false + ], + $returnVal + ); + } + + /* + * Tests for replicateTo + */ + + public function testReplicateTo() + { + if ('05-06' == substr($this->fixture->getDBVersion(), 0, 5)) { + $this->markTestSkipped('With MySQL 5.6 ARC2_Store::replicateTo does not work. Tables keep their names.'); + } + + // test data + $this->fixture->query('INSERT INTO { + "2009-05-28T18:03:38+09:00" . + "2009-05-28T18:03:38+09:00GMT" . + "21 August 2007" . + }'); + + // replicate + $this->fixture->replicateTo('replicate'); + + /* + * check for new prefixes + */ + $tables = $this->fixture->getDBObject()->fetchList('SHOW TABLES'); + $foundArcPrefix = $foundReplicatePrefix = false; + foreach($tables as $table) { + // check for original table + if (false !== strpos($table['Tables_in_'.$this->dbConfig['db_name']], $this->dbConfig['store_name'].'_')) { + $foundArcPrefix = true; + // check for replicated table + } elseif (false !== strpos($table['Tables_in_'.$this->dbConfig['db_name']], 'replicate_')) { + $foundReplicatePrefix = true; + } + } + + $this->assertTrue($foundArcPrefix); + $this->assertTrue($foundReplicatePrefix); + } + + /* + * Tests for reset + */ + + public function testResetKeepSettings() + { + $this->fixture->setSetting('foo', 'bar'); + $this->assertEquals(1, $this->fixture->hasSetting('foo')); + + $this->fixture->reset(1); + + $this->assertEquals(1, $this->fixture->hasSetting('foo')); + } +} diff --git a/tests/integration/store/query/AskQueryTest.php b/tests/db_adapter_depended/store/query/AskQueryTest.php similarity index 90% rename from tests/integration/store/query/AskQueryTest.php rename to tests/db_adapter_depended/store/query/AskQueryTest.php index dfe00ea9..3a00a804 100644 --- a/tests/integration/store/query/AskQueryTest.php +++ b/tests/db_adapter_depended/store/query/AskQueryTest.php @@ -1,6 +1,6 @@ fixture->setup(); } + public function tearDown() + { + $this->fixture->closeDBCon(); + } + public function testAskDefaultGraph() { // test data diff --git a/tests/integration/store/query/DeleteQueryTest.php b/tests/db_adapter_depended/store/query/DeleteQueryTest.php similarity index 91% rename from tests/integration/store/query/DeleteQueryTest.php rename to tests/db_adapter_depended/store/query/DeleteQueryTest.php index 9b86122c..cc1622e4 100644 --- a/tests/integration/store/query/DeleteQueryTest.php +++ b/tests/db_adapter_depended/store/query/DeleteQueryTest.php @@ -1,6 +1,6 @@ fixture->setup(); } - protected function runSPOQuery(string $g = null) + public function tearDown() + { + $this->fixture->closeDBCon(); + } + + protected function runSPOQuery($g = null) { return null == $g ? $this->fixture->query('SELECT * WHERE {?s ?p ?o.}') @@ -124,7 +129,7 @@ public function testDeleteFromWhere() rdf:type . }'); - $this->assertEquals(5, \count($this->runSPOQuery()['result']['rows'])); + $this->assertEquals(5, \count($this->runSPOQuery('http://example.com/1')['result']['rows'])); $this->fixture->query('DELETE FROM { 1, 2 . @@ -132,6 +137,6 @@ public function testDeleteFromWhere() 1, 2 . }'); - $this->assertEquals(3, \count($this->runSPOQuery()['result']['rows'])); + $this->assertEquals(3, \count($this->runSPOQuery('http://example.com/1')['result']['rows'])); } } diff --git a/tests/db_adapter_depended/store/query/DescribeQueryTest.php b/tests/db_adapter_depended/store/query/DescribeQueryTest.php new file mode 100644 index 00000000..7c82c1af --- /dev/null +++ b/tests/db_adapter_depended/store/query/DescribeQueryTest.php @@ -0,0 +1,106 @@ +fixture = \ARC2::getStore($this->dbConfig); + $this->fixture->drop(); + $this->fixture->setup(); + } + + public function tearDown() + { + $this->fixture->closeDBCon(); + } + + public function testDescribeDefaultGraph() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $res = $this->fixture->query('DESCRIBE '); + $this->assertEquals( + [ + 'query_type' => 'describe', + 'result' => [ + 'http://s' => [ + 'http://p1' => [ + [ + 'value' => 'baz', + 'type' => 'literal' + ] + ] + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + public function testDescribeWhereDefaultGraph() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $res = $this->fixture->query('DESCRIBE ?s WHERE {?s ?p "baz".}'); + $this->assertEquals( + [ + 'query_type' => 'describe', + 'result' => [ + 'http://s' => [ + 'http://p1' => [ + [ + 'value' => 'baz', + 'type' => 'literal' + ] + ] + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + public function testDescribeWhereDefaultGraph2() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $res = $this->fixture->query('DESCRIBE * WHERE {?s ?p "baz".}'); + $this->assertEquals( + [ + 'query_type' => 'describe', + 'result' => [ + 'http://s' => [ + 'http://p1' => [ + [ + 'value' => 'baz', + 'type' => 'literal' + ] + ] + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } +} diff --git a/tests/db_adapter_depended/store/query/ErrorHandlingInQueriesTest.php b/tests/db_adapter_depended/store/query/ErrorHandlingInQueriesTest.php new file mode 100644 index 00000000..a2e2817a --- /dev/null +++ b/tests/db_adapter_depended/store/query/ErrorHandlingInQueriesTest.php @@ -0,0 +1,54 @@ +fixture = \ARC2::getStore($this->dbConfig); + $this->fixture->drop(); + $this->fixture->setup(); + } + + public function tearDown() + { + $this->fixture->closeDBCon(); + } + + /** + * What if a result variable is not used in query. + */ + public function testResultVariableNotUsedInQuery() + { + $res = $this->fixture->query(' + SELECT ?not_used_in_query ?s WHERE { + ?s ?p ?o . + } + '); + + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'not_used_in_query', 's' + ], + 'rows' => [ + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + + $this->assertTrue(2 <= count($this->fixture->errors)); + } +} diff --git a/tests/integration/store/query/InsertIntoQueryTest.php b/tests/db_adapter_depended/store/query/InsertIntoQueryTest.php similarity index 67% rename from tests/integration/store/query/InsertIntoQueryTest.php rename to tests/db_adapter_depended/store/query/InsertIntoQueryTest.php index 364fd09f..8cdc7348 100644 --- a/tests/integration/store/query/InsertIntoQueryTest.php +++ b/tests/db_adapter_depended/store/query/InsertIntoQueryTest.php @@ -1,6 +1,6 @@ fixture->setup(); } + public function tearDown() + { + $this->fixture->closeDBCon(); + } + public function testInsertInto() { // test data @@ -40,7 +45,8 @@ public function testInsertIntoAllKindsOfTriples() 2.0 . "3" . "4"^^xsd:integer . - _:foo "5" . + "5"@en . + _:foo "6" . }'); $res = $this->fixture->query('SELECT * FROM {?s ?p ?o.}'); @@ -48,7 +54,7 @@ public function testInsertIntoAllKindsOfTriples() // using <#foo> in query makes ARC2 using the phpunit path as prefix // e.g. file:///var/www/html/pier-and-peer/ARC2/vendor/phpunit/phpunit/phpunit# // therefore we build this prefix manually to check later - $filePrefix = 'file://'.str_replace('tests/integration/store/query', '', __DIR__); + $filePrefix = 'file://'.str_replace('tests/db_adapter_depended/store/query', '', __DIR__); $filePrefix .= 'vendor/phpunit/phpunit/phpunit#'; $this->assertEquals( @@ -113,12 +119,21 @@ public function testInsertIntoAllKindsOfTriples() 'o datatype' => 'http://www.w3.org/2001/XMLSchema#integer', ], [ - 's' => $res['result']['rows'][7]['s'], - 's type' => 'bnode', + 's' => 'http://s2', + 's type' => 'uri', 'p' => 'http://foo', 'p type' => 'uri', 'o' => '5', 'o type' => 'literal', + 'o lang' => 'en', + ], + [ + 's' => $res['result']['rows'][8]['s'], + 's type' => 'bnode', + 'p' => 'http://foo', + 'p type' => 'uri', + 'o' => '6', + 'o type' => 'literal', ], ], $res['result']['rows'] @@ -163,6 +178,55 @@ public function testInsertIntoBlankNode() ); } + public function testInsertIntoDate() + { + // test data + $this->fixture->query('INSERT INTO { + "2009-05-28T18:03:38+09:00" . + "2009-05-28T18:03:38+09:00GMT" . + "21 August 2007" . + }'); + + $res = $this->fixture->query('SELECT * FROM {?s ?p ?o.}'); + + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => ['s', 'p', 'o'], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'p' => 'http://p1', + 'p type' => 'uri', + 'o' => '2009-05-28T18:03:38+09:00', + 'o type' => 'literal', + ], + [ + 's' => 'http://s', + 's type' => 'uri', + 'p' => 'http://p1', + 'p type' => 'uri', + 'o' => '2009-05-28T18:03:38+09:00GMT', + 'o type' => 'literal', + ], + [ + 's' => 'http://s', + 's type' => 'uri', + 'p' => 'http://p1', + 'p type' => 'uri', + 'o' => '21 August 2007', + 'o type' => 'literal', + ], + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + public function testInsertIntoList() { // test data @@ -206,6 +270,35 @@ public function testInsertIntoList() ); } + // show that ARC2 can't store long values + public function testInsertIntoLongValue() + { + // create long URI (ca. 250 chars) + $longURI = 'http://'.hash('sha512', 'long') + .hash('sha512', 'URI'); + + // test data + $this->fixture->query('INSERT INTO { + <'.$longURI.'/s> <'.$longURI.'/p> <'.$longURI.'/o> ; + <'.$longURI.'/p2> <'.$longURI.'/o2> . + '); + + $res = $this->fixture->query('SELECT * {?s ?p ?o.}'); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => ['s', 'p', 'o'], + 'rows' => [] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + + $this->markTestSkipped('ARC2 can not store long values, e.g. URIs with around 250 chars.'); + } + public function testInsertIntoListMoreComplex() { // test data @@ -268,4 +361,27 @@ public function testInsertIntoListMoreComplex() $res['result']['rows'] ); } + + public function testInsertIntoWhere() + { + // test data + $this->fixture->query('INSERT INTO CONSTRUCT { + "Leipzig" . + "Grimma" . + } WHERE { + ?s "Leipzig" . + }'); + + // we expect that 1 element gets added to the store, because of the WHERE clause. + // but ARC2 added none. + $res = $this->fixture->query('SELECT * FROM {?s ?p ?o.}'); + $this->assertEquals(0, \count($res['result']['rows'])); + + $this->markTestSkipped( + 'ARC2 does not check the WHERE clause when inserting data. No data added at all.' + .PHP_EOL + .PHP_EOL.'FYI: https://www.w3.org/Submission/SPARQL-Update/#sec_examples and ' + .PHP_EOL.'https://github.com/semsol/arc2/wiki/SPARQL-#insert-example' + ); + } } diff --git a/tests/db_adapter_depended/store/query/KnownNotWorkingSparqlQueriesTest.php b/tests/db_adapter_depended/store/query/KnownNotWorkingSparqlQueriesTest.php new file mode 100644 index 00000000..f65986a5 --- /dev/null +++ b/tests/db_adapter_depended/store/query/KnownNotWorkingSparqlQueriesTest.php @@ -0,0 +1,175 @@ +fixture = \ARC2::getStore($this->dbConfig); + $this->fixture->drop(); + $this->fixture->setup(); + } + + public function tearDown() + { + $this->fixture->closeDBCon(); + } + + /** + * Variable alias + */ + public function testSelectAlias() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $res = $this->fixture->query(' + SELECT (?s AS ?s_alias) ?o FROM WHERE {?s ?o.} + '); + + $this->assertEquals(0, $res); + } + + /** + * FILTER: langMatches with * + * + * Based on the specification (https://www.w3.org/TR/rdf-sparql-query/#func-langMatches) + * langMatches with * has to return all entries with no language set. + */ + public function testSelectFilterLangMatchesWithStar() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + "in de"@de . + "in en"@en . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER langMatches (lang(?o), "*") + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + /** + * sameTerm + */ + public function testSelectSameTerm() + { + // test data + $this->fixture->query('INSERT INTO { + "100" . + "100" . + }'); + + $res = $this->fixture->query('SELECT ?c1 ?c2 WHERE { + ?c1 ?weight ?w1. + + ?c2 ?weight ?w2. + + FILTER (sameTerm(?w1, ?w2)) + }'); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'c1', 'c2' + ], + 'rows' => [ + [ + 'c1' => 'http://container1', + 'c1 type' => 'uri', + 'c2' => 'http://container1', + 'c2 type' => 'uri', + ], + [ + 'c1' => 'http://container2', + 'c1 type' => 'uri', + 'c2' => 'http://container1', + 'c2 type' => 'uri', + ], + [ + 'c1' => 'http://container1', + 'c1 type' => 'uri', + 'c2' => 'http://container2', + 'c2 type' => 'uri', + ], + [ + 'c1' => 'http://container2', + 'c1 type' => 'uri', + 'c2' => 'http://container2', + 'c2 type' => 'uri', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res, + '', + 0, + 10, + true + ); + + $this->markTestSkipped( + 'ARC2: solving sameterm does not work properly. The result contains elements multiple times. ' + . PHP_EOL . 'Expected behavior is described here: https://www.w3.org/TR/rdf-sparql-query/#func-sameTerm' + ); + } + + /** + * Sub Select + */ + public function testSelectSubSelect() + { + // test data + $this->fixture->query('INSERT INTO { + "1" . + "3" . + "2" . + + . + . + . + }'); + + $res = $this->fixture->query(' + SELECT * WHERE { + { + SELECT ?p WHERE { + ?p "1" . + } + } + ?p ?who . + } + '); + + $this->assertEquals(0, $res); + } +} diff --git a/tests/db_adapter_depended/store/query/LoadQueryTest.php b/tests/db_adapter_depended/store/query/LoadQueryTest.php new file mode 100644 index 00000000..0ff2c0e7 --- /dev/null +++ b/tests/db_adapter_depended/store/query/LoadQueryTest.php @@ -0,0 +1,60 @@ +fixture = \ARC2::getStore($this->dbConfig); + $this->fixture->drop(); + $this->fixture->setup(); + } + + public function tearDown() + { + $this->fixture->closeDBCon(); + } + + public function testLoad() + { + // check that store is empty + $res = $this->fixture->query('SELECT * WHERE {?s ?p ?o.}'); + $this->assertEquals(0, count($res['result']['rows'])); + + $filepath = 'https://raw.githubusercontent.com/semsol/arc2/' + .'master/tests/sparql11/w3c-tests/move/manifest.ttl'; + $this->fixture->query('LOAD <'.$filepath.'>'); + + // check that triples were inserted + $res = $this->fixture->query(' + SELECT * + FROM + WHERE {?s ?p ?o.} + '); + $this->assertEquals(106, count($res['result']['rows'])); + } + + public function testLoadInto() + { + // check that store is empty + $res = $this->fixture->query('SELECT * FROM WHERE {?s ?p ?o.}'); + $this->assertEquals(0, count($res['result']['rows'])); + + $filepath = 'https://raw.githubusercontent.com/semsol/arc2/' + .'master/tests/sparql11/w3c-tests/move/manifest.ttl'; + $this->fixture->query('LOAD <'.$filepath.'> INTO '); + + // check that triples were inserted + $res = $this->fixture->query('SELECT * FROM WHERE {?s ?p ?o.}'); + $this->assertEquals(106, count($res['result']['rows'])); + } +} diff --git a/tests/db_adapter_depended/store/query/SelectQueryTest.php b/tests/db_adapter_depended/store/query/SelectQueryTest.php new file mode 100644 index 00000000..550b0b36 --- /dev/null +++ b/tests/db_adapter_depended/store/query/SelectQueryTest.php @@ -0,0 +1,1427 @@ +fixture = \ARC2::getStore($this->dbConfig); + $this->fixture->drop(); + $this->fixture->setup(); + } + + public function tearDown() + { + $this->fixture->closeDBCon(); + } + + public function testSelectDefaultGraph() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $res = $this->fixture->query('SELECT * WHERE { ?o.}'); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'o' + ], + 'rows' => [ + [ + 'o' => 'baz', + 'o type' => 'literal' + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + public function testSelectGraphSpecified() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $res = $this->fixture->query('SELECT * FROM WHERE { ?o.}'); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'o' + ], + 'rows' => [ + [ + 'o' => 'baz', + 'o type' => 'literal' + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // simulate a LEFT JOIN using OPTIONAL + public function testSelectLeftJoinUsingOptional() + { + // test data + $this->fixture->query('INSERT INTO { + . + . + + . + . + + . + }'); + + $res = $this->fixture->query(' + SELECT * WHERE { + ?s ?o . + OPTIONAL { + ?o ?o2 . + } + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o', 'o2' + ], + 'rows' => [ + [ + 's' => 'http://s3', + 's type' => 'uri', + 'o' => 'http://s1', + 'o type' => 'uri', + 'o2' => 'http://s2', + 'o2 type' => 'uri' + ], + [ + 's' => 'http://s3', + 's type' => 'uri', + 'o' => 'http://s1', + 'o type' => 'uri', + 'o2' => 'http://s3', + 'o2 type' => 'uri' + ], + [ + 's' => 'http://s1', + 's type' => 'uri', + 'o' => 'http://s2', + 'o type' => 'uri', + 'o2' => 'http://s3', + 'o2 type' => 'uri' + ], + [ + 's' => 'http://s1', + 's type' => 'uri', + 'o' => 'http://s2', + 'o type' => 'uri', + 'o2' => 'http://s4', + 'o2 type' => 'uri' + ], + [ + 's' => 'http://s1', + 's type' => 'uri', + 'o' => 'http://s3', + 'o type' => 'uri', + 'o2' => 'http://s1', + 'o2 type' => 'uri' + ], + [ + 's' => 'http://s2', + 's type' => 'uri', + 'o' => 'http://s3', + 'o type' => 'uri', + 'o2' => 'http://s1', + 'o2 type' => 'uri' + ], + [ + 's' => 'http://s2', + 's type' => 'uri', + 'o' => 'http://s4', + 'o type' => 'uri' + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res, + '', + 0, + 10, + true + ); + } + + // OPTIONAL, artifical query to extend coverage for store code. (ARC2_StoreSelectQueryHandler::sameOptional) + public function testSelectOptional() + { + // test data + $this->fixture->query('INSERT INTO { + . + }'); + + $res = $this->fixture->query(' + SELECT * WHERE { + ?s ?o . + OPTIONAL { + ?o ?o2 . + } + OPTIONAL { + ?o ?o2 . + } + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o', 'o2' + ], + 'rows' => [ + [ + 's' => 'http://s1', + 's type' => 'uri', + 'o' => 'http://s2', + 'o type' => 'uri', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + public function testSelectNoWhereClause() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + }'); + + $res = $this->fixture->query('SELECT * FROM { ?o.}'); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'o' + ], + 'rows' => [ + [ + 'o' => 'baz', + 'o type' => 'literal' + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + /* + * FILTER + */ + + // bound: is variable set? + public function testSelectFilterBoundNotBounding() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (bound(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // bound: is variable set? + public function testSelectFilterBoundVariableBounded() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (bound(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => 'foo', + 'o type' => 'literal', + ] + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // datatype + public function testSelectFilterDatatype() + { + // test data + $this->fixture->query('INSERT INTO { + 3 . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (datatype(?o) = xsd:integer) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => '3', + 'o type' => 'literal', + 'o datatype' => 'http://www.w3.org/2001/XMLSchema#integer', + ] + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // isBlank + public function testSelectFilterIsBlankFound() + { + // test data + $this->fixture->query('INSERT INTO { + _:foo . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (isBlank(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => $res['result']['rows'][0]['o'], + 'o type' => 'bnode', + ] + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // isBlank + public function testSelectFilterIsBlankNotFound() + { + // test data + $this->fixture->query('INSERT INTO { + . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (isBlank(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // isIri + public function testSelectFilterIsIriFound() + { + // test data + $this->fixture->query('INSERT INTO { + . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (isIri(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => 'urn:id', + 'o type' => 'uri', + ] + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // isIri + public function testSelectFilterIsIriNotFound() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (isIri(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // isLiteral + public function testSelectFilterIsLiteralFound() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (isLiteral(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => 'foo', + 'o type' => 'literal', + ] + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // isLiteral + public function testSelectFilterIsLiteralNotFound() + { + // test data + $this->fixture->query('INSERT INTO { + . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (isLiteral(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // isUri + public function testSelectFilterIsUriFound() + { + // test data + $this->fixture->query('INSERT INTO { + . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (isUri(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => 'urn:id', + 'o type' => 'uri', + ] + ] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // isUri + public function testSelectFilterIsUriNotFound() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (isUri(?o)) + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [] + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // lang: test behavior when using a language + public function testSelectFilterLang() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + "in de"@de . + "in en"@en . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (lang(?o) = "en") + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => 'in en', + 'o type' => 'literal', + 'o lang' => 'en' + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // langMatches + public function testSelectFilterLangMatches() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + "in de"@de . + "in en"@en . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER langMatches (lang(?o), "en") + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => 'in en', + 'o type' => 'literal', + 'o lang' => 'en' + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // regex + public function testSelectFilterRegex() + { + // test data + $this->fixture->query('INSERT INTO { + "Alice". + "Bob" . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER regex (?o, "^Ali") + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => 'Alice', + 'o type' => 'literal' + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // regex + public function testSelectFilterRegexWithModifier() + { + // test data + $this->fixture->query('INSERT INTO { + "Alice". + "Bob" . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER regex (?o, "^ali", "i") + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => 'Alice', + 'o type' => 'literal' + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // str + public function testSelectFilterStr() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + "in de"@de . + "in en"@en . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (str(?o) = "in en") + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [ + [ + 's' => 'http://s', + 's type' => 'uri', + 'o' => 'in en', + 'o type' => 'literal', + 'o lang' => 'en' + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // str + public function testSelectFilterStrNotFound() + { + // test data + $this->fixture->query('INSERT INTO { + "foo" . + "in de"@de . + "in en"@en . + }'); + + $res = $this->fixture->query(' + SELECT ?s ?o WHERE { + ?s ?o . + FILTER (str(?o) = "in it") + } + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'o' + ], + 'rows' => [], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // > + public function testSelectFilterRelationalGreatThan() + { + // test data + $this->fixture->query('INSERT INTO { + "150" . + "50" . + }'); + + $res = $this->fixture->query('SELECT ?c WHERE { + ?c ?w . + + FILTER (?w > 100) + }'); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'c' + ], + 'rows' => [ + [ + 'c' => 'http://container1', + 'c type' => 'uri', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // < + public function testSelectFilterRelationalSmallerThan() + { + // test data + $this->fixture->query('INSERT INTO { + "150" . + "50" . + }'); + + $res = $this->fixture->query('SELECT ?c WHERE { + ?c ?w . + + FILTER (?w < 100) + }'); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'c' + ], + 'rows' => [ + [ + 'c' => 'http://container2', + 'c type' => 'uri', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // = + public function testSelectFilterRelationalEqual() + { + // test data + $this->fixture->query('INSERT INTO { + "150" . + "50" . + }'); + + $res = $this->fixture->query('SELECT ?c WHERE { + ?c ?w . + + FILTER (?w = 150) + }'); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'c' + ], + 'rows' => [ + [ + 'c' => 'http://container1', + 'c type' => 'uri', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + // != + public function testSelectFilterRelationalNotEqual() + { + // test data + $this->fixture->query('INSERT INTO { + "150" . + "50" . + }'); + + $res = $this->fixture->query('SELECT ?c WHERE { + ?c ?w . + + FILTER (?w != 150) + }'); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'c' + ], + 'rows' => [ + [ + 'c' => 'http://container2', + 'c type' => 'uri', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + /* + * SELECT COUNT + */ + + public function testSelectCount() + { + // test data + $this->fixture->query('INSERT INTO { + "baz" . + "baz" . + "baz" . + }'); + + $res = $this->fixture->query(' + SELECT COUNT(?s) AS ?count WHERE { + ?s "baz" . + } + ORDER BY DESC(?count) + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'count' + ], + 'rows' => [ + [ + 'count' => '3', + 'count type' => 'literal', + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + /* + * GROUP BY + */ + + public function testSelectGroupBy() + { + $query = 'SELECT ?who COUNT(?person) as ?persons WHERE { + ?who ?person . + } + GROUP BY ?who + '; + + // mark skipped, if we have a certain MySQL version running + // TODO make that more flexible, currently its tight to MySQL + $serverVersion = $this->fixture->a['db_object']->getServerVersion(); + if ('05-07' == substr($serverVersion, 0, 5)) { + $this->markTestSkipped( + '[mysql 5.7] Result set is empty for query: ' + .$query + ); + } + + // test data + $this->fixture->query('INSERT INTO { + , . + . + }'); + + $res = $this->fixture->query($query); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'who', + 'persons' + ], + 'rows' => [ + [ + 'who' => 'http://person1', + 'who type' => 'uri', + 'persons' => '2', + 'persons type' => 'literal', + ], + [ + 'who' => 'http://person2', + 'who type' => 'uri', + 'persons' => '1', + 'persons type' => 'literal', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + /* + * OFFSET and LIMIT + */ + + public function testSelectOffset() + { + // test data + $this->fixture->query('INSERT INTO { + "1" . + "3" . + "2" . + }'); + + $res = $this->fixture->query(' + SELECT * WHERE { ?s ?p ?o . } + OFFSET 1 + '); + + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'p', 'o' + ], + 'rows' => [ + [ + 's' => 'http://person3', + 's type' => 'uri', + 'p' => 'http://id', + 'p type' => 'uri', + 'o' => '3', + 'o type' => 'literal', + ], + [ + 's' => 'http://person2', + 's type' => 'uri', + 'p' => 'http://id', + 'p type' => 'uri', + 'o' => '2', + 'o type' => 'literal', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + public function testSelectOffsetLimit() + { + // test data + $this->fixture->query('INSERT INTO { + "1" . + "3" . + "2" . + }'); + + $res = $this->fixture->query(' + SELECT * WHERE { ?s ?p ?o . } + OFFSET 1 LIMIT 2 + '); + + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'p', 'o' + ], + 'rows' => [ + [ + 's' => 'http://person3', + 's type' => 'uri', + 'p' => 'http://id', + 'p type' => 'uri', + 'o' => '3', + 'o type' => 'literal', + ], + [ + 's' => 'http://person2', + 's type' => 'uri', + 'p' => 'http://id', + 'p type' => 'uri', + 'o' => '2', + 'o type' => 'literal', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + public function testSelectLimit() + { + // test data + $this->fixture->query('INSERT INTO { + "1" . + "3" . + "2" . + }'); + + $res = $this->fixture->query(' + SELECT * WHERE { ?s ?p ?o . } + LIMIT 2 + '); + + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', 'p', 'o' + ], + 'rows' => [ + [ + 's' => 'http://person1', + 's type' => 'uri', + 'p' => 'http://id', + 'p type' => 'uri', + 'o' => '1', + 'o type' => 'literal', + ], + [ + 's' => 'http://person3', + 's type' => 'uri', + 'p' => 'http://id', + 'p type' => 'uri', + 'o' => '3', + 'o type' => 'literal', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + /* + * ORDER BY + */ + + public function testSelectOrderByAsc() + { + // test data + $this->fixture->query('INSERT INTO { + "1" . + "3" . + "2" . + }'); + + $res = $this->fixture->query(' + SELECT * WHERE { + ?s ?id . + } + ORDER BY ASC(?id) + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', + 'id' + ], + 'rows' => [ + [ + 's' => 'http://person1', + 's type' => 'uri', + 'id' => '1', + 'id type' => 'literal', + ], + [ + 's' => 'http://person2', + 's type' => 'uri', + 'id' => '2', + 'id type' => 'literal', + ], + [ + 's' => 'http://person3', + 's type' => 'uri', + 'id' => '3', + 'id type' => 'literal', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + public function testSelectOrderByDesc() + { + // test data + $this->fixture->query('INSERT INTO { + "1" . + "3" . + "2" . + }'); + + $res = $this->fixture->query(' + SELECT * WHERE { + ?s ?id . + } + ORDER BY DESC(?id) + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', + 'id' + ], + 'rows' => [ + [ + 's' => 'http://person3', + 's type' => 'uri', + 'id' => '3', + 'id type' => 'literal', + ], + [ + 's' => 'http://person2', + 's type' => 'uri', + 'id' => '2', + 'id type' => 'literal', + ], + [ + 's' => 'http://person1', + 's type' => 'uri', + 'id' => '1', + 'id type' => 'literal', + ] + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + public function testSelectOrderByWithoutContent() + { + $res = $this->fixture->query(' + SELECT * WHERE { + ?s ?id . + } + ORDER BY + '); + + // query false, therefore 0 as result + $this->assertEquals(0, $res); + } + + /* + * UNION + */ + + public function testSelectUnion() + { + // test data + $this->fixture->query('INSERT INTO { + "1" . + "3" . + "2" . + }'); + + $res = $this->fixture->query(' + SELECT * WHERE { + { + ?p "1" . + } UNION { + ?p "3" . + } + } + '); + + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 'p' + ], + 'rows' => [ + [ + 'p' => 'http://person1', + 'p type' => 'uri', + ], + [ + 'p' => 'http://person3', + 'p type' => 'uri', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } + + /* + * Tests using certain queries with SELECT FROM WHERE and not just SELECT WHERE + */ + + public function testSelectOrderByAscWithFromClause() + { + // test data + $this->fixture->query('INSERT INTO { + "1" . + "3" . + "2" . + }'); + + $res = $this->fixture->query(' + SELECT * FROM WHERE { + ?s ?id . + } + ORDER BY ASC(?id) + '); + $this->assertEquals( + [ + 'query_type' => 'select', + 'result' => [ + 'variables' => [ + 's', + 'id' + ], + 'rows' => [ + [ + 's' => 'http://person1', + 's type' => 'uri', + 'id' => '1', + 'id type' => 'literal', + ], + [ + 's' => 'http://person2', + 's type' => 'uri', + 'id' => '2', + 'id type' => 'literal', + ], + [ + 's' => 'http://person3', + 's type' => 'uri', + 'id' => '3', + 'id type' => 'literal', + ], + ], + ], + 'query_time' => $res['query_time'] + ], + $res + ); + } +} diff --git a/tests/integration/store/ARC2_StoreTest.php b/tests/integration/store/ARC2_StoreTest.php deleted file mode 100644 index 89101657..00000000 --- a/tests/integration/store/ARC2_StoreTest.php +++ /dev/null @@ -1,313 +0,0 @@ -fixture = \ARC2::getStore($this->dbConfig); - $this->fixture->drop(); - $this->fixture->setup(); - } - - public function testSetup() - { - $this->fixture->reset(); - - $this->fixture->setup(); - } - - /* - * Tests for createDBCon - */ - - public function testCreateDBConDatabaseNotAvailable() - { - $this->fixture->queryDB('DROP DATABASE '.$this->fixture->a['db_name'], $this->fixture->getDBCon()); - $databases = $this->fixture->queryDB('SHOW DATABASES', $this->fixture->getDBCon()); - while($row = mysqli_fetch_array($databases)) { - $this->assertFalse($this->fixture->a['db_name'] == $row[0]); - } - - // create connection, which also creates the DB - $this->fixture->createDBCon(); - - $databases = $this->fixture->queryDB('SHOW DATABASES', $this->fixture->getDBCon()); - $foundDb = false; - while($row = mysqli_fetch_array($databases)) { - if ($this->fixture->a['db_name'] == $row[0]) { - $foundDb = true; - } - } - $this->assertTrue($foundDb); - } - - /* - * Tests for closeDBCon - */ - - public function testCloseDBCon() - { - $this->assertTrue(isset($this->fixture->a['db_con'])); - - $this->fixture->closeDBCon(); - - $this->assertFalse(isset($this->fixture->a['db_con'])); - } - - /* - * Tests for delete - */ - - public function testDelete() - { - // test data - $this->fixture->query('INSERT INTO { - "baz" . - "label1" . - }'); - - $res = $this->fixture->query('SELECT * WHERE {?s ?p ?o.}'); - $this->assertEquals(2, \count($res['result']['rows'])); - - // remove graph - $this->fixture->delete(false, 'http://example.com/'); - - $res = $this->fixture->query('SELECT * WHERE {?s ?p ?o.}'); - $this->assertEquals(0, \count($res['result']['rows'])); - } - - /* - * Tests for drop - */ - - public function testDrop() - { - // make sure all tables were created - $this->fixture->setup(); - $tables = $this->fixture->getTables(); - $res = $this->fixture->queryDB('SHOW TABLES;', $this->fixture->getDBCon()); - $this->assertEquals(6, $res->num_rows); - - // remove all tables - $this->fixture->drop(); - - // check that all tables were removed - $res = $this->fixture->queryDB('SHOW TABLES;', $this->fixture->getDBCon()); - $this->assertEquals(0, $res->num_rows); - } - - /* - * Tests for dump - */ - - public function testDump() - { - $this->markTestSkipped( - 'Can not test dump-method, because it causes the following error:' - .PHP_EOL - .'Cannot modify header information - headers already sent by (output started at ./vendor/phpunit/phpunit/src/Util/Printer.php:110)' - ); - } - - /* - * Tests for getDBVersion - */ - - // just check pattern - public function testGetDBVersion() - { - $result = preg_match('/[0-9]{2}-[0-9]{2}-[0-9]{2}/', $this->fixture->getDBVersion(), $match); - $this->assertEquals(1, $result); - } - - /* - * Tests for getSetting and setSetting - */ - - public function testGetAndSetSetting() - { - $this->assertEquals(0, $this->fixture->getSetting('foo')); - - $this->fixture->setSetting('foo', 'bar'); - - $this->assertEquals('bar', $this->fixture->getSetting('foo')); - } - - /* - * Tests for getLabelProps - */ - - public function testGetLabelProps() - { - $this->assertEquals( - [ - 'http://www.w3.org/2000/01/rdf-schema#label', - 'http://xmlns.com/foaf/0.1/name', - 'http://purl.org/dc/elements/1.1/title', - 'http://purl.org/rss/1.0/title', - 'http://www.w3.org/2004/02/skos/core#prefLabel', - 'http://xmlns.com/foaf/0.1/nick', - ], - $this->fixture->getLabelProps() - ); - } - - /* - * Tests for getResourceLabel - */ - - public function testGetResourceLabel() - { - // test data - $this->fixture->query('INSERT INTO { - "baz" . - "label1" . - }'); - - $res = $this->fixture->getResourceLabel('http://s'); - - $this->assertEquals('label1', $res); - } - - public function testGetResourceLabelNoData() - { - // test data - $this->fixture->query('INSERT INTO { - "baz" . - }'); - - $res = $this->fixture->getResourceLabel('http://s'); - - $this->assertEquals('s', $res); - } - - /* - * Tests for getResourcePredicates - */ - - public function testGetResourcePredicates() - { - // test data - $this->fixture->query('INSERT INTO { - "baz" . - "bar" . - }'); - - $res = $this->fixture->getResourcePredicates('http://s'); - - $this->assertEquals( - [ - 'http://p1' => [], - 'http://p2' => [], - ], - $res - ); - } - - public function testGetResourcePredicatesMultipleGraphs() - { - // test data - $this->fixture->query('INSERT INTO { - "baz" . - "bar" . - }'); - - $this->fixture->query('INSERT INTO { - "baz" . - "bar" . - }'); - - $res = $this->fixture->getResourcePredicates('http://s'); - - $this->assertEquals( - [ - 'http://p1' => [], - 'http://p2' => [], - 'http://p3' => [], - 'http://p4' => [], - ], - $res - ); - } - - /* - * Tests for getPredicateRange - */ - - public function testGetPredicateRange() - { - // test data - $this->fixture->query('INSERT INTO { - . - }'); - - $res = $this->fixture->getPredicateRange('http://p1'); - - $this->assertEquals('http://foobar', $res); - } - - public function testGetPredicateRangeNotFound() - { - $res = $this->fixture->getPredicateRange('http://not-available'); - - $this->assertEquals('', $res); - } - - /* - * Tests for getIDValue - */ - - public function testGetIDValue() - { - $this->fixture->query('INSERT INTO { - . - }'); - - $res = $this->fixture->getIDValue(1); - - $this->assertEquals('http://example.com/', $res); - } - - public function testGetIDValueNoData() - { - $res = $this->fixture->getIDValue(1); - - $this->assertEquals(0, $res); - } - - /* - * Tests for logQuery - */ - - public function testLogQuery() - { - $logFile = 'arc_query_log.txt'; - - $this->assertFalse(file_exists($logFile)); - - $this->fixture->logQuery('query1'); - - $this->assertTrue(file_exists($logFile)); - unlink($logFile); - } - - /* - * Tests for reset - */ - - public function testResetKeepSettings() - { - $this->fixture->setSetting('foo', 'bar'); - $this->assertEquals(1, $this->fixture->hasSetting('foo')); - - $this->fixture->reset(1); - - $this->assertEquals(1, $this->fixture->hasSetting('foo')); - } -} diff --git a/tests/integration/store/query/SelectQueryTest.php b/tests/integration/store/query/SelectQueryTest.php deleted file mode 100644 index cd8e09ff..00000000 --- a/tests/integration/store/query/SelectQueryTest.php +++ /dev/null @@ -1,266 +0,0 @@ -fixture = \ARC2::getStore($this->dbConfig); - $this->fixture->drop(); - $this->fixture->setup(); - } - - public function testSelectDefaultGraph() - { - // test data - $this->fixture->query('INSERT INTO { - "baz" . - }'); - - $res = $this->fixture->query('SELECT * WHERE { ?o.}'); - $this->assertEquals( - [ - 'query_type' => 'select', - 'result' => [ - 'variables' => [ - 'o' - ], - 'rows' => [ - [ - 'o' => 'baz', - 'o type' => 'literal' - ] - ], - ], - 'query_time' => $res['query_time'] - ], - $res - ); - } - - public function testSelectGraphSpecified() - { - // test data - $this->fixture->query('INSERT INTO { - "baz" . - }'); - - $res = $this->fixture->query('SELECT * FROM { ?o.}'); - $this->assertEquals( - [ - 'query_type' => 'select', - 'result' => [ - 'variables' => [ - 'o' - ], - 'rows' => [ - [ - 'o' => 'baz', - 'o type' => 'literal' - ] - ], - ], - 'query_time' => $res['query_time'] - ], - $res - ); - } - - /* - * SELECT COUNT - */ - - public function testSelectCount() - { - // test data - $this->fixture->query('INSERT INTO { - "baz" . - "baz" . - "baz" . - }'); - - $res = $this->fixture->query(' - SELECT COUNT(?s) AS ?count WHERE { - ?s "baz" . - } - ORDER BY DESC(?count) - '); - $this->assertEquals( - [ - 'query_type' => 'select', - 'result' => [ - 'variables' => [ - 'count' - ], - 'rows' => [ - [ - 'count' => '3', - 'count type' => 'literal', - ] - ], - ], - 'query_time' => $res['query_time'] - ], - $res - ); - } - - /* - * GROUP BY - */ - - public function testSelectGroupBy() - { - // test data - $this->fixture->query('INSERT INTO { - , . - . - }'); - - $res = $this->fixture->query(' - SELECT ?who COUNT(?person) as ?persons WHERE { - ?who ?person . - } - GROUP BY ?who - '); - $this->assertEquals( - [ - 'query_type' => 'select', - 'result' => [ - 'variables' => [ - 'who', - 'persons' - ], - 'rows' => [ - [ - 'who' => 'http://person1', - 'who type' => 'uri', - 'persons' => '2', - 'persons type' => 'literal', - ], - [ - 'who' => 'http://person2', - 'who type' => 'uri', - 'persons' => '1', - 'persons type' => 'literal', - ], - ], - ], - 'query_time' => $res['query_time'] - ], - $res - ); - } - - /* - * ORDER BY - */ - - public function testSelectOrderByAsc() - { - // test data - $this->fixture->query('INSERT INTO { - "1" . - "3" . - "2" . - }'); - - $res = $this->fixture->query(' - SELECT * WHERE { - ?s ?id . - } - ORDER BY ASC(?id) - '); - $this->assertEquals( - [ - 'query_type' => 'select', - 'result' => [ - 'variables' => [ - 's', - 'id' - ], - 'rows' => [ - [ - 's' => 'http://person1', - 's type' => 'uri', - 'id' => '1', - 'id type' => 'literal', - ], - [ - 's' => 'http://person2', - 's type' => 'uri', - 'id' => '2', - 'id type' => 'literal', - ], - [ - 's' => 'http://person3', - 's type' => 'uri', - 'id' => '3', - 'id type' => 'literal', - ], - ], - ], - 'query_time' => $res['query_time'] - ], - $res - ); - } - - public function testSelectOrderByDesc() - { - // test data - $this->fixture->query('INSERT INTO { - "1" . - "3" . - "2" . - }'); - - $res = $this->fixture->query(' - SELECT * WHERE { - ?s ?id . - } - ORDER BY DESC(?id) - '); - $this->assertEquals( - [ - 'query_type' => 'select', - 'result' => [ - 'variables' => [ - 's', - 'id' - ], - 'rows' => [ - [ - 's' => 'http://person3', - 's type' => 'uri', - 'id' => '3', - 'id type' => 'literal', - ], - [ - 's' => 'http://person2', - 's type' => 'uri', - 'id' => '2', - 'id type' => 'literal', - ], - [ - 's' => 'http://person1', - 's type' => 'uri', - 'id' => '1', - 'id type' => 'literal', - ] - ], - ], - 'query_time' => $res['query_time'] - ], - $res - ); - } -} diff --git a/tests/sparql11/AggregatesTest.php b/tests/sparql11/AggregatesTest.php deleted file mode 100644 index 852b1970..00000000 --- a/tests/sparql11/AggregatesTest.php +++ /dev/null @@ -1,275 +0,0 @@ -w3cTestsFolderPath = __DIR__.'/w3c-tests/aggregates'; - $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/aggregates/manifest#'; - } - - /* - * tests - */ - - public function test_agg_avg_01() - { - $this->markTestSkipped( - 'Test skipped, because of rounding bug in AVG function. See https://github.com/semsol/arc2/issues/99' - ); - } - - public function test_agg_avg_02() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-avg-02'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_empty_group() - { - $this->runTestFor('agg-empty-group'); - } - - public function test_agg_err_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-err-01'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_err_02() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-err-02'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_groupconcat_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-groupconcat-01'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_groupconcat_02() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-groupconcat-02'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_groupconcat_03() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-groupconcat-03'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_max_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-max-01'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'Result is empty and misses entry:' - . PHP_EOL - . '' - . '3.0E4' - . '' - . PHP_EOL - . 'for query: '. - $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_max_02() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-max-01'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'Result is missing multiple entries.' - . PHP_EOL - . 'for query: '. - $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_min_01() - { - $this->runTestFor('agg-min-01'); - } - - public function test_agg_min_02() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-min-02'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'Result is missing multiple entries.' - . PHP_EOL - . 'for query: '. - $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_sample_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg-sample-01'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg_sum_01() - { - $this->markTestSkipped( - 'Test skipped, because of rounding bug in SUM function. See https://github.com/semsol/arc2/issues/100' - ); - } - - public function test_agg01() - { - $this->runTestFor('agg01'); - } - - public function test_agg02() - { - $this->runTestFor('agg02'); - } - - public function test_agg03() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg03'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg04() - { - $this->runTestFor('agg04'); - } - - public function test_agg05() - { - $this->runTestFor('agg05'); - } - - public function test_agg06() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg06'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg07() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg07'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg08() - { - $this->runTestFor('agg08'); - } - - public function test_agg08b() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'agg08b'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_agg09() - { - $this->runTestFor('agg09'); - } - - public function test_agg10() - { - $this->runTestFor('agg10'); - } - - public function test_agg11() - { - $this->runTestFor('agg11'); - } - - public function test_agg12() - { - $this->runTestFor('agg12'); - } -} diff --git a/tests/sparql11/DeleteTest.php b/tests/sparql11/DeleteTest.php deleted file mode 100644 index 06289aa4..00000000 --- a/tests/sparql11/DeleteTest.php +++ /dev/null @@ -1,302 +0,0 @@ -w3cTestsFolderPath = __DIR__.'/w3c-tests/delete'; - $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/delete/manifest#'; - } - - /** - * Helper function to get test query for a given test. - * - * @param string $testUri - * @return string Query to test. - */ - protected function getTestQuery($testUri) - { - /* - example: - - :dawg-delete-01 a mf:UpdateEvaluationTest ; - [...] - mf:action [ ut:request ; - ut:data - ] ; - mf:result [ ut:data - ] . - */ - $query = $this->store->query(' - PREFIX mf: . - PREFIX ut: . - SELECT * FROM <'. $this->manifestGraphUri .'> WHERE { - <'. $testUri .'> mf:action [ ut:request ?queryFile ] . - } - '); - - return file_get_contents($query['result']['rows'][0]['queryFile']); - } - - /* - * tests - */ - - public function test_dawg_delete_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-01'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_02() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-02'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_03() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-03'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_04() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-04'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_05() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-05'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_06() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-06'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_07() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-07'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_with_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-with-01'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_with_02() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-with-02'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_with_03() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-with-03'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_with_04() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-with-04'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_with_05() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-with-05'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_with_06() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-with-06'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_using_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-using-01'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_using_02a() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-using-02a'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_using_03() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-using-03'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_using_04() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-using-04'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_using_05() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-using-05'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_delete_using_06a() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-delete-using-06a'); - - $this->markTestSkipped( - 'This kind of delete query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#delete-example' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } -} diff --git a/tests/sparql11/DropTest.php b/tests/sparql11/DropTest.php deleted file mode 100644 index 1fad989e..00000000 --- a/tests/sparql11/DropTest.php +++ /dev/null @@ -1,156 +0,0 @@ -w3cTestsFolderPath = __DIR__.'/w3c-tests/drop'; - $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/drop/manifest#'; - } - - /** - * @param string $graphUri - * @todo Check that $graphUri is a valid URI - * @todo port this functionality to ARC2_Store->query - */ - protected function createGraph($graphUri) - { - // table names - $g2t = 'arc_g2t'; - $id2val = 'arc_id2val'; - - /* - * for id2val table - */ - $query = 'INSERT INTO '. $id2val .' (val) VALUES("'. $graphUri .'")'; - $this->store->queryDB($query, $this->store->getDBCon()); - $usedId = $this->store->getDBCon()->insert_id; - - /* - * for g2t table - */ - $newIdg2t = 1 + $this->getRowCount($g2t); - $query = 'INSERT INTO '. $g2t .' (t, g) VALUES('. $newIdg2t .', '. $usedId .')'; - $this->store->queryDB($query, $this->store->getDBCon()); - $usedId = $this->store->getDBCon()->insert_id; - } - - /** - * Helper function to get test query for a given test. - * - * @param string $testUri - * @return string Query to test. - */ - protected function getTestQuery($testUri) - { - /* - example: - - :group1 mf:action [ - qt:query - ] - */ - $query = $this->store->query(' - PREFIX mf: . - PREFIX ut: . - SELECT * FROM <'. $this->manifestGraphUri .'> WHERE { - <'. $testUri .'> mf:action [ ut:request ?queryFile ] . - } - '); - - return $query['result']['rows'][0]['queryFile']; - } - - /* - * tests - */ - - public function test_dawg_drop_all_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-drop-all-01'); - - $this->markTestSkipped( - 'This kind of drop query is currently not supported. ' - . 'See https://github.com/semsol/arc2/wiki/SPARQL-' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_drop_default_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-drop-default-01'); - - $this->markTestSkipped( - 'This kind of drop query is currently not supported. ' - . 'See https://github.com/semsol/arc2/wiki/SPARQL-' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_drop_graph_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-drop-graph-01'); - - $this->markTestSkipped( - 'This kind of drop query is currently not supported. ' - . 'See https://github.com/semsol/arc2/wiki/SPARQL-' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - public function test_dawg_drop_named_01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'dawg-drop-named-01'); - - $this->markTestSkipped( - 'This kind of drop query is currently not supported. ' - . 'See https://github.com/semsol/arc2/wiki/SPARQL-' - . PHP_EOL . 'Query: ' . $this->makeQueryA1Liner($query) - ); - } - - // this test is not part of the W3C test collection - // it tests DELETE FROM <...> command which is the ARC2 equivalent to DROP GRAPH <...> - public function test_delete_graph() - { - $graphUri = 'http://example.org/g1'; - - // create graph - $this->createGraph($graphUri); - - // load test data into graph - $parser = \ARC2::getTurtleParser(); - $parser->parse($this->w3cTestsFolderPath .'/drop-g1.ttl'); - $this->store->insert($parser->getSimpleIndex(), $graphUri); - - // check if graph really contains data - $res = $this->store->query('SELECT * FROM <'. $graphUri .'> WHERE {?s ?p ?o.}'); - $this->assertTrue(0 < count($res['result']['rows']), 'No test data in graph found.'); - - // run test query - $res = $this->store->query('DELETE FROM <'. $graphUri .'>'); - - // check if test data are still available - $res = $this->store->query('SELECT * FROM <'. $graphUri .'> WHERE {?s ?p ?o.}'); - $this->assertTrue(0 == count($res['result']['rows'])); - } -} diff --git a/tests/sparql11/ExistsTest.php b/tests/sparql11/ExistsTest.php deleted file mode 100644 index 7140fbd0..00000000 --- a/tests/sparql11/ExistsTest.php +++ /dev/null @@ -1,85 +0,0 @@ -w3cTestsFolderPath = __DIR__.'/w3c-tests/exists'; - $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/exists/manifest#'; - } - - /* - * tests - */ - - public function test_exists01() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'exists01'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_exists02() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'exists02'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_exists03() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'exists03'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_exists04() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'exists04'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_exists05() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'exists05'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'ARC2_Store::query returns 0 for query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } -} diff --git a/tests/sparql11/GroupingTest.php b/tests/sparql11/GroupingTest.php deleted file mode 100644 index b6666f32..00000000 --- a/tests/sparql11/GroupingTest.php +++ /dev/null @@ -1,74 +0,0 @@ -w3cTestsFolderPath = __DIR__.'/w3c-tests/grouping'; - $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/grouping/manifest#'; - } - - /* - * tests - */ - - public function test_group01() - { - $this->runTestFor('group01'); - } - - public function test_group02() - { - $this->runTestFor('group02'); - } - - public function test_group03() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'group03'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'Query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_group04() - { - // get failing query - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'group04'); - - $this->markTestSkipped( - 'This kind of query is currently not supported. ' - . 'Query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_group05() - { - $this->runTestFor('group05'); - } - - public function test_group06() - { - $this->runTestFor('group06'); - } - - public function test_group07() - { - $this->runTestFor('group07'); - } -} diff --git a/tests/sparql11/MoveTest.php b/tests/sparql11/MoveTest.php deleted file mode 100644 index ae95e51a..00000000 --- a/tests/sparql11/MoveTest.php +++ /dev/null @@ -1,81 +0,0 @@ -w3cTestsFolderPath = __DIR__.'/w3c-tests/move'; - $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/move/manifest#'; - } - - /* - * tests - */ - - public function test_move01() - { - $this->markTestSkipped( - 'MOVE query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#sparql-grammar-changes-and-additions' - ); - } - - public function test_move02() - { - $this->markTestSkipped( - 'MOVE query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#sparql-grammar-changes-and-additions' - ); - } - - public function test_move03() - { - $this->markTestSkipped( - 'MOVE query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#sparql-grammar-changes-and-additions' - ); - } - - public function test_move04() - { - $this->markTestSkipped( - 'MOVE query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#sparql-grammar-changes-and-additions' - ); - } - - public function test_move05() - { - $this->markTestSkipped( - 'MOVE query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#sparql-grammar-changes-and-additions' - ); - } - - public function test_move06() - { - $this->markTestSkipped( - 'MOVE query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#sparql-grammar-changes-and-additions' - ); - } - - public function test_move07() - { - $this->markTestSkipped( - 'MOVE query is currently not supported. ' - . 'https://github.com/semsol/arc2/wiki/SPARQL-#sparql-grammar-changes-and-additions' - ); - } -} diff --git a/tests/sparql11/SyntaxUpdate1Test.php b/tests/sparql11/SyntaxUpdate1Test.php deleted file mode 100644 index a974b4a5..00000000 --- a/tests/sparql11/SyntaxUpdate1Test.php +++ /dev/null @@ -1,702 +0,0 @@ -w3cTestsFolderPath = __DIR__.'/w3c-tests/syntax-update-1'; - $this->testPref = 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/syntax-update-1/manifest#'; - } - - /** - * Helper function to get test query for a given test. - * - * @param string $testUri - * @return string Query to test. - */ - protected function getTestQuery($testUri) - { - /* - example: - - :test_1 rdf:type mf:PositiveUpdateSyntaxTest11 ; - dawgt:approval dawgt:Approved ; - dawgt:approvedBy ; - mf:name "syntax-update-01.ru" ; - mf:action ;. - */ - $query = $this->store->query(' - PREFIX mf: . - SELECT * FROM <'. $this->manifestGraphUri .'> WHERE { - <'. $testUri .'> mf:action ?queryFile . - } - '); - - return file_get_contents($query['result']['rows'][0]['queryFile']); - } - - /* - * tests - */ - - public function test_test_1() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_1'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertTrue(is_array($result) && isset($result['query_type'])); - } - - public function test_test_2() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_2'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertTrue(is_array($result) && isset($result['query_type'])); - } - - public function test_test_3() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_3'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_4() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_4'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_5() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_5'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_6() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_6'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_7() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_7'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_8() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_8'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_9() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_9'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_10() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_10'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_11() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_11'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_12() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_12'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_13() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_13'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_14() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_14'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_15() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_15'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_16() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_16'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_17() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_17'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_18() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_18'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_19() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_19'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_20() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_20'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_21() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_21'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_22() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_22'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_23() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_23'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_24() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_24'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_25() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_25'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_26() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_26'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_27() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_27'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_28() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_28'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_29() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_29'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_30() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_30'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_31() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_31'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_32() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_32'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_33() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_33'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_34() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_34'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_35() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_35'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_36() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_36'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_37() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_37'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_38() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_38'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_39() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_39'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_40() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_40'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_41() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_41'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_42() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_42'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_43() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_43'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_44() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_44'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_45() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_45'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_46() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_46'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_47() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_47'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_48() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_48'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_49() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_49'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_50() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_50'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_51() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_51'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped( - 'Query has to fail, but ARC2 returns an array as if query is considered valid. Query: '. PHP_EOL . $this->makeQueryA1Liner($query) - ); - } - - public function test_test_52() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_52'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } - - public function test_test_53() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_53'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->markTestSkipped('This kind of query is currently not supported. Query: '. PHP_EOL . $this->makeQueryA1Liner($query)); - } - - public function test_test_54() - { - $this->loadManifestFileIntoStore($this->w3cTestsFolderPath); - $query = $this->getTestQuery($this->testPref . 'test_54'); - - // fire query - $result = $this->store->query($query); - - // check result - $this->assertEquals(0, $result); - } -} diff --git a/tests/sparql11/w3c-tests/grouping/group-data-1.ttl b/tests/sparql11/w3c-tests/grouping/group-data-1.ttl deleted file mode 100644 index b00ea616..00000000 --- a/tests/sparql11/w3c-tests/grouping/group-data-1.ttl +++ /dev/null @@ -1,5 +0,0 @@ -@prefix : . - -:s1 :p 1 . -:s1 :q 9 . -:s2 :p 2 . diff --git a/tests/sparql11/w3c-tests/grouping/group-data-2.ttl b/tests/sparql11/w3c-tests/grouping/group-data-2.ttl deleted file mode 100644 index 8f3c8763..00000000 --- a/tests/sparql11/w3c-tests/grouping/group-data-2.ttl +++ /dev/null @@ -1,6 +0,0 @@ -@prefix : . - -:s1 :p 1 . -:s3 :p 1 . -:s1 :q 9 . -:s2 :p 2 . diff --git a/tests/sparql11/w3c-tests/grouping/group01.rq b/tests/sparql11/w3c-tests/grouping/group01.rq deleted file mode 100644 index 98ffaccb..00000000 --- a/tests/sparql11/w3c-tests/grouping/group01.rq +++ /dev/null @@ -1,7 +0,0 @@ -PREFIX : - -SELECT ?s -{ - ?s :p ?v . -} -GROUP BY ?s diff --git a/tests/sparql11/w3c-tests/grouping/group01.srx b/tests/sparql11/w3c-tests/grouping/group01.srx deleted file mode 100644 index adcc478a..00000000 --- a/tests/sparql11/w3c-tests/grouping/group01.srx +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - http://example/s1 - - - - - http://example/s2 - - - - diff --git a/tests/sparql11/w3c-tests/grouping/group02.rq b/tests/sparql11/w3c-tests/grouping/group02.rq deleted file mode 100644 index 98ffaccb..00000000 --- a/tests/sparql11/w3c-tests/grouping/group02.rq +++ /dev/null @@ -1,7 +0,0 @@ -PREFIX : - -SELECT ?s -{ - ?s :p ?v . -} -GROUP BY ?s diff --git a/tests/sparql11/w3c-tests/grouping/group02.srx b/tests/sparql11/w3c-tests/grouping/group02.srx deleted file mode 100644 index adcc478a..00000000 --- a/tests/sparql11/w3c-tests/grouping/group02.srx +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - http://example/s1 - - - - - http://example/s2 - - - - diff --git a/tests/sparql11/w3c-tests/grouping/group03.rq b/tests/sparql11/w3c-tests/grouping/group03.rq deleted file mode 100644 index 7621f945..00000000 --- a/tests/sparql11/w3c-tests/grouping/group03.rq +++ /dev/null @@ -1,8 +0,0 @@ -PREFIX : - -SELECT ?w (SAMPLE(?v) AS ?S) -{ - ?s :p ?v . - OPTIONAL { ?s :q ?w } -} -GROUP BY ?w diff --git a/tests/sparql11/w3c-tests/grouping/group03.srx b/tests/sparql11/w3c-tests/grouping/group03.srx deleted file mode 100644 index f4215d5c..00000000 --- a/tests/sparql11/w3c-tests/grouping/group03.srx +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - 9 - - - 1 - - - - - 2 - - - - diff --git a/tests/sparql11/w3c-tests/grouping/group04.rq b/tests/sparql11/w3c-tests/grouping/group04.rq deleted file mode 100644 index e2f4ed28..00000000 --- a/tests/sparql11/w3c-tests/grouping/group04.rq +++ /dev/null @@ -1,9 +0,0 @@ -PREFIX : -PREFIX xsd: - -SELECT ?X (SAMPLE(?v) AS ?S) -{ - ?s :p ?v . - OPTIONAL { ?s :q ?w } -} -GROUP BY (COALESCE(?w, "1605-11-05"^^xsd:date) AS ?X) diff --git a/tests/sparql11/w3c-tests/grouping/group04.srx b/tests/sparql11/w3c-tests/grouping/group04.srx deleted file mode 100644 index bf4c48ff..00000000 --- a/tests/sparql11/w3c-tests/grouping/group04.srx +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - 1605-11-05 - - - 2 - - - - - 9 - - - 1 - - - - diff --git a/tests/sparql11/w3c-tests/grouping/group05.rq b/tests/sparql11/w3c-tests/grouping/group05.rq deleted file mode 100644 index a56cf7c1..00000000 --- a/tests/sparql11/w3c-tests/grouping/group05.rq +++ /dev/null @@ -1,8 +0,0 @@ -PREFIX : - -SELECT ?s ?w -{ - ?s :p ?v . - OPTIONAL { ?s :q ?w } -} -GROUP BY ?s ?w diff --git a/tests/sparql11/w3c-tests/grouping/group05.srx b/tests/sparql11/w3c-tests/grouping/group05.srx deleted file mode 100644 index 959bddcd..00000000 --- a/tests/sparql11/w3c-tests/grouping/group05.srx +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - http://example/s1 - - - 9 - - - - - http://example/s3 - - - - - http://example/s2 - - - - diff --git a/tests/sparql11/w3c-tests/grouping/group06.rq b/tests/sparql11/w3c-tests/grouping/group06.rq deleted file mode 100644 index c5cad42d..00000000 --- a/tests/sparql11/w3c-tests/grouping/group06.rq +++ /dev/null @@ -1,7 +0,0 @@ -PREFIX : - -SELECT ?s ?v -{ - ?s :p ?v . -} -GROUP BY ?s diff --git a/tests/sparql11/w3c-tests/grouping/group07.rq b/tests/sparql11/w3c-tests/grouping/group07.rq deleted file mode 100644 index 43a3945d..00000000 --- a/tests/sparql11/w3c-tests/grouping/group07.rq +++ /dev/null @@ -1,17 +0,0 @@ -prefix lode: -prefix dc: -prefix rdfs: - -select ?event ?eventName ?venue ?photo -where { - ?photo lode:illustrate ?event . - { - select ?event ?eventName ?venue - where { - ?event dc:title ?eventName . - ?event lode:atPlace ?venue . - ?venue rdfs:label "Live Music Hall" . - } - } -} -GROUP BY ?event diff --git a/tests/sparql11/w3c-tests/grouping/manifest.ttl b/tests/sparql11/w3c-tests/grouping/manifest.ttl deleted file mode 100644 index cb61e508..00000000 --- a/tests/sparql11/w3c-tests/grouping/manifest.ttl +++ /dev/null @@ -1,89 +0,0 @@ -@prefix rdf: . -@prefix : . -@prefix rdfs: . -@prefix mf: . -@prefix qt: . -@prefix dawgt: . - -<> rdf:type mf:Manifest ; - rdfs:label "Grouping" ; - mf:entries - ( - :group01 -## :group02 # Now the same as group01 - :group03 - :group04 - :group05 - :group06 - :group07 - ) . - - -:group01 rdf:type mf:QueryEvaluationTest ; - mf:name "Group-1"; - rdfs:comment "Simple grouping" ; - dawgt:approval dawgt:Approved ; - dawgt:approvedBy ; - mf:action - [ qt:query ; - qt:data ] ; - mf:result - . - -:group02 rdf:type mf:QueryEvaluationTest ; - mf:name "Group-2"; - rdfs:comment "Grouping, simple, using *" ; - dawgt:approval dawgt:Approved ; - dawgt:approvedBy ; - mf:action - [ qt:query ; - qt:data ] ; - mf:result - . - -:group03 rdf:type mf:QueryEvaluationTest ; - mf:name "Group-3"; - rdfs:comment "Grouping with an unbound" ; - dawgt:approval dawgt:Approved ; - dawgt:approvedBy ; - mf:action - [ qt:query ; - qt:data ] ; - mf:result - . - -:group04 rdf:type mf:QueryEvaluationTest ; - mf:name "Group-4"; - rdfs:comment "Grouping with expression" ; - dawgt:approval dawgt:Approved ; - dawgt:approvedBy ; - mf:action - [ qt:query ; - qt:data ] ; - mf:result - . - -:group05 rdf:type mf:QueryEvaluationTest ; - mf:name "Group-5"; - rdfs:comment "Grouping with unbound " ; - dawgt:approval dawgt:Approved ; - dawgt:approvedBy ; - mf:action - [ qt:query ; - qt:data ] ; - mf:result - . - -:group06 rdf:type mf:NegativeSyntaxTest11; - mf:name "Group-6" ; - rdfs:comment "projection of ungrouped variable"; - dawgt:approval dawgt:Approved ; - dawgt:approvedBy ; - mf:action . - -:group07 rdf:type mf:NegativeSyntaxTest11; - mf:name "Group-7" ; - rdfs:comment "projection of ungrouped variable, more complex example than Group-6"; - dawgt:approval dawgt:Approved ; - dawgt:approvedBy ; - mf:action . diff --git a/tests/unit/phpunit.xml b/tests/unit/phpunit.xml deleted file mode 100644 index d00dbcc5..00000000 --- a/tests/unit/phpunit.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - ../.. - - ../../tests - - - - - diff --git a/tests/unit/src/ARC2/Store/Adapter/AbstractAdapterTest.php b/tests/unit/src/ARC2/Store/Adapter/AbstractAdapterTest.php new file mode 100644 index 00000000..f3ca4a2e --- /dev/null +++ b/tests/unit/src/ARC2/Store/Adapter/AbstractAdapterTest.php @@ -0,0 +1,264 @@ +checkAdapterRequirements(); + + $this->fixture = $this->getAdapterInstance($this->dbConfig); + $result = $this->fixture->connect(); + + // remove all tables + $tables = $this->fixture->fetchList('SHOW TABLES'); + foreach($tables as $table) { + $this->fixture->simpleQuery('DROP TABLE '. $table['Tables_in_'.$this->dbConfig['db_name']]); + } + } + + public function tearDown() + { + if (null !== $this->fixture) { + $this->fixture->disconnect(); + } + } + + protected function dropAllTables() + { + // remove all tables + $tables = $this->fixture->fetchList('SHOW TABLES'); + foreach($tables as $table) { + $this->fixture->simpleQuery('DROP TABLE '. $table['Tables_in_'.$this->dbConfig['db_name']]); + } + } + + /* + * Tests for connect + */ + + public function testConnectCreateNewConnection() + { + $this->fixture->disconnect(); + + // do explicit reconnect + $this->fixture = $this->getAdapterInstance($this->dbConfig); + $this->fixture->connect(); + + $tables = $this->fixture->fetchList('SHOW TABLES'); + $this->assertTrue(is_array($tables)); + } + + /* + * Tests for exec + */ + + public function testExec() + { + $this->fixture->simpleQuery('CREATE TABLE users (id INT(6), name VARCHAR(30) NOT NULL)'); + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (1, "foobar");'); + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (2, "foobar2");'); + + $this->assertEquals(2, $this->fixture->exec('DELETE FROM users;')); + } + + /* + * Tests for fetchRow + */ + + public function testFetchRow() + { + // valid query + $sql = 'CREATE TABLE users ( + id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(30) NOT NULL + )'; + $this->fixture->simpleQuery($sql); + $this->assertEquals(null, $this->fixture->fetchRow('SELECT * FROM users')); + + // add data + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (1, "foobar");'); + $this->assertEquals( + [ + 'id' => 1, + 'name' => 'foobar', + ], + $this->fixture->fetchRow('SELECT * FROM users WHERE id = 1;') + ); + } + + /* + * Tests for fetchList + */ + + public function testFetchList() + { + // valid query + $sql = 'CREATE TABLE users ( + id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(30) NOT NULL + )'; + $this->fixture->simpleQuery($sql); + $this->assertEquals([], $this->fixture->fetchList('SELECT * FROM users')); + + // add data + $this->fixture->simpleQuery('INSERT INTO users (id, name) VALUE (1, "foobar");'); + $this->assertEquals( + [ + [ + 'id' => 1, + 'name' => 'foobar', + ] + ], + $this->fixture->fetchList('SELECT * FROM users') + ); + } + + /* + * Tests for getCollation + */ + + public function testGetCollation() + { + // g2t table + if (isset($this->dbConfig['db_table_prefix'])) { + $table = $this->dbConfig['db_table_prefix'] . '_'; + } else { + $table = ''; + } + if (isset($this->dbConfig['store_name'])) { + $table .= $this->dbConfig['store_name'] . '_'; + } + $table .= 'setting'; + + // create setting table which is used to determine collation + $sql = 'CREATE TABLE '.$table.' ( + k char(32) NOT NULL, + val text NOT NULL, + UNIQUE KEY (k) + ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci DELAY_KEY_WRITE = 1'; + $this->fixture->simpleQuery($sql); + + $this->assertEquals('utf8_unicode_ci', $this->fixture->getCollation()); + } + + // setting table not there + public function testGetCollationNoReferenceTable() + { + $this->assertEquals('', $this->fixture->getCollation()); + } + + /* + * Tests for getDBSName + */ + + public function testGetDBSName() + { + // connect and check + $this->fixture->connect(); + $this->assertTrue(in_array($this->fixture->getDBSName(), array('mariadb', 'mysql'))); + } + + public function testGetDBSNameNoConnection() + { + // disconnect current connection + $this->fixture->disconnect(); + + // create new instance, but dont connect + $db = $this->getAdapterInstance($this->dbConfig); + + $this->assertNull($db->getDBSName()); + } + + /* + * Tests for getNumberOfRows + */ + + public function testGetNumberOfRows() + { + // create test table + $this->fixture->simpleQuery(' + CREATE TABLE pet (name VARCHAR(20)); + '); + + $this->assertEquals(1, $this->fixture->getNumberOfRows('SHOW TABLES')); + } + + /* + * Tests for query + */ + + public function testQuery() + { + // valid query + $sql = 'CREATE TABLE MyGuests (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY)'; + $this->fixture->simpleQuery($sql); + + $foundTable = false; + foreach ($this->fixture->fetchList('SHOW TABLES') as $entry) { + if ('MyGuests' == $entry['Tables_in_'.$this->dbConfig['db_name']]) { + $foundTable = true; + break; + } + } + $this->assertTrue($foundTable, 'Expected table not found.'); + } + + /* + * Tests for getServerVersion + */ + + public function testGetServerVersion() + { + // check that server version looks like 05-00-05 + $this->assertEquals(1, preg_match('/\d\d-\d\d-\d\d/', $this->fixture->getServerVersion())); + } + + /* + * Tests for getStoreName + */ + + public function testGetStoreName() + { + $this->assertEquals($this->dbConfig['store_name'], $this->fixture->getStoreName()); + } + + public function testGetStoreNameNotDefined() + { + $this->fixture->disconnect(); + + $copyOfDbConfig = $this->dbConfig; + unset($copyOfDbConfig['store_name']); + + $db = $this->getAdapterInstance($copyOfDbConfig); + + $this->assertEquals('arc', $db->getStoreName()); + } + + /* + * Tests for simpleQuery + */ + + public function testSimpleQueryNoConnection() + { + // test, that it creates a connection itself, when calling simpleQuery + $this->fixture->disconnect(); + + $db = $this->getAdapterInstance($this->dbConfig); + $sql = 'CREATE TABLE MyGuests (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY)'; + $this->assertTrue($db->simpleQuery($sql)); + } +} diff --git a/tests/unit/src/ARC2/Store/Adapter/AdapterFactoryTest.php b/tests/unit/src/ARC2/Store/Adapter/AdapterFactoryTest.php new file mode 100644 index 00000000..cb23bdf7 --- /dev/null +++ b/tests/unit/src/ARC2/Store/Adapter/AdapterFactoryTest.php @@ -0,0 +1,44 @@ +fixture = new AdapterFactory(); + } + + /* + * Tests for getInstanceFor + */ + + public function testGetInstanceFor() + { + $this->assertTrue($this->fixture->getInstanceFor('mysqli') instanceof AbstractAdapter); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Unknown adapter name given. Currently supported are: mysqli, pdo + */ + public function testGetInstanceForInvalidAdapterName() + { + $this->fixture->getInstanceFor('invalid'); + } + + /* + * Tests for getSupportedAdapters + */ + + public function testGetSupportedAdapters() + { + $this->assertEquals(array('mysqli', 'pdo'), $this->fixture->getSupportedAdapters()); + } +} diff --git a/tests/unit/src/ARC2/Store/Adapter/CachedPDOAdapterTest.php b/tests/unit/src/ARC2/Store/Adapter/CachedPDOAdapterTest.php new file mode 100644 index 00000000..ccd023e1 --- /dev/null +++ b/tests/unit/src/ARC2/Store/Adapter/CachedPDOAdapterTest.php @@ -0,0 +1,40 @@ +markTestSkipped('Cache not enabled, therefore skipped.'); + } + + parent::setUp(); + } + + protected function getAdapterInstance($configuration) + { + return new CachedPDOAdapter($configuration); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Cache not enabled, therefore CachedPDOAdapter can not be used. + */ + public function testExceptionIfCacheIsNotEnabled() + { + $cfg = $this->dbConfig; + unset($cfg['cache_enabled']); + unset($cfg['cache_instance']); + + new CachedPDOAdapter($cfg); + } +} diff --git a/tests/unit/src/ARC2/Store/Adapter/PDOAdapterTest.php b/tests/unit/src/ARC2/Store/Adapter/PDOAdapterTest.php new file mode 100644 index 00000000..610238b0 --- /dev/null +++ b/tests/unit/src/ARC2/Store/Adapter/PDOAdapterTest.php @@ -0,0 +1,110 @@ +markTestSkipped('Test skipped, because extension pdo_mysql is not installed.'); + } + + // stop, if pdo_db_protocol is not set in dbConfig + if (false == isset($this->dbConfig['db_pdo_protocol'])) { + $this->markTestSkipped( + 'Test skipped, because db_pdo_protocol is not set. Its ok, if this happens in unit test environment.' + ); + } + } + + protected function getAdapterInstance($configuration) + { + return new PDOAdapter($configuration); + } + + public function testConnectUseGivenConnection() + { + $this->fixture->disconnect(); + + // create connection outside of the instance + $dsn = $this->dbConfig['db_pdo_protocol'].':host='. $this->dbConfig['db_host']; + $dsn .= ';dbname='.$this->dbConfig['db_name']; + $dsn .= ';charset=utf8mb4'; + $connection = new \PDO( + $dsn, + $this->dbConfig['db_user'], + $this->dbConfig['db_pwd'] + ); + + $connection->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); + $connection->setAttribute(\PDO::ERRMODE_EXCEPTION, true); + $connection->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_BOTH); + $connection->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); + + $this->fixture = $this->getAdapterInstance($this->dbConfig); + + // use existing connection + $this->fixture->connect($connection); + + // if not the same origin, the connection ID differs + $connectionId = $connection->query('SELECT CONNECTION_ID()')->fetch(\PDO::FETCH_ASSOC); + $this->assertEquals($this->fixture->getConnectionId(), $connectionId); + + /* + * simple test query to check that its working + */ + $sql = 'CREATE TABLE MyGuests (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY)'; + $this->fixture->simpleQuery($sql); + + $tables = $this->fixture->fetchList('SHOW TABLES'); + $this->assertTrue(is_array($tables) && 0 < count($tables)); + } + + public function testEscape() + { + $this->assertEquals('\"hallo\"', $this->fixture->escape('"hallo"')); + } + + public function testGetAdapterName() + { + $this->assertEquals('pdo', $this->fixture->getAdapterName()); + } + + public function testGetConnection() + { + $this->assertTrue($this->fixture->getConnection() instanceof \PDO); + } + + /** + * @expectedException \Exception + */ + public function testGetNumberOfRowsInvalidQuery() + { + $dbs = 'mysql' == $this->fixture->getDBSName() ? 'MySQL' : 'MariaDB'; + + $this->expectExceptionMessage( + "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your $dbs server version for the right syntax to use near 'of x' at line 1" + ); + + $this->fixture->getNumberOfRows('SHOW TABLES of x'); + } + + /** + * @expectedException \Exception + */ + public function testQueryInvalid() + { + $dbs = 'mysql' == $this->fixture->getDBSName() ? 'MySQL' : 'MariaDB'; + + $this->expectExceptionMessage( + "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your $dbs server version for the right syntax to use near 'invalid query' at line 1" + ); + + // invalid query + $this->assertFalse($this->fixture->simpleQuery('invalid query')); + } +} diff --git a/tests/unit/src/ARC2/Store/Adapter/mysqliAdapterTest.php b/tests/unit/src/ARC2/Store/Adapter/mysqliAdapterTest.php new file mode 100644 index 00000000..52684721 --- /dev/null +++ b/tests/unit/src/ARC2/Store/Adapter/mysqliAdapterTest.php @@ -0,0 +1,102 @@ +markTestSkipped('Test skipped, because extension mysqli is not installed.'); + } + } + + protected function getAdapterInstance($configuration) + { + return new mysqliAdapter($configuration); + } + + public function testConnectUseGivenConnection() + { + $this->fixture->disconnect(); + + // create connection outside of the instance + $connection = mysqli_connect( + $this->dbConfig['db_host'], + $this->dbConfig['db_user'], + $this->dbConfig['db_pwd'], + $this->dbConfig['db_name'] + ); + + $this->fixture = new mysqliAdapter(); + + // use existing connection + $this->fixture->connect($connection); + + // if not the same origin, the connection ID differs + $this->assertEquals($this->fixture->getConnectionId(), mysqli_thread_id($connection)); + + // simple test query to check that its working + $result = $this->fixture->simpleQuery('SHOW TABLES'); + $this->assertTrue($result); + } + + public function testEscape() + { + $this->assertEquals('\"hallo\"', $this->fixture->escape('"hallo"')); + } + + public function testGetAdapterName() + { + $this->assertEquals('mysqli', $this->fixture->getAdapterName()); + } + + public function testGetConnection() + { + $this->assertTrue($this->fixture->getConnection() instanceof \mysqli); + } + + /* + * Tests for getErrorMessage and getErrorCode + */ + + public function testGetErrorMessageAndGetErrorCode() + { + // invalid query + $result = $this->fixture->simpleQuery('SHOW TABLES of x'); + $this->assertFalse($result); + $errorMsg = $this->fixture->getErrorMessage(); + $errorCode = $this->fixture->getErrorCode(); + + $dbs = 'mariadb' == $this->fixture->getDBSName() ? 'MariaDB' : 'MySQL'; + + $this->assertEquals( + "You have an error in your SQL syntax; check the manual that corresponds to your $dbs server " + ."version for the right syntax to use near 'of x' at line 1", + $errorMsg + ); + + $this->assertTrue(0 < $errorCode); + } + + public function testGetNumberOfRowsInvalidQuery() + { + // run with invalid query + $this->assertEquals(0, $this->fixture->getNumberOfRows('SHOW TABLES of x')); + } + + public function testMysqliQuery() + { + $res = $this->fixture->mysqliQuery('SHOW TABLES'); + $this->assertTrue($res instanceof \mysqli_result); + } + + public function testQueryInvalid() + { + // invalid query + $this->assertFalse($this->fixture->simpleQuery('invalid query')); + } +} diff --git a/tests/unit/store/ARC2_StoreTest.php b/tests/unit/store/ARC2_StoreTest.php new file mode 100644 index 00000000..a572314e --- /dev/null +++ b/tests/unit/store/ARC2_StoreTest.php @@ -0,0 +1,36 @@ +fixture = \ARC2::getStore($this->dbConfig); + $this->fixture->createDBCon(); + + // remove all tables + $tables = $this->fixture->getDBObject()->fetchList('SHOW TABLES'); + foreach($tables as $table) { + $this->fixture->getDBObject()->simpleQuery('DROP TABLE '. $table['Tables_in_'.$this->dbConfig['db_name']]); + } + + // fresh setup of ARC2 + $this->fixture->setup(); + } + + public function tearDown() + { + $this->fixture->closeDBCon(); + } + + public function testCacheEnabled() + { + $cacheEnabled = isset($this->dbConfig['cache_enabled']) && $this->dbConfig['cache_enabled']; + $this->assertEquals($cacheEnabled, $this->fixture->cacheEnabled()); + } +}