From a9396ab9875fb020765620cb6f2e69ed799a1226 Mon Sep 17 00:00:00 2001 From: Pieter Colpaert Date: Thu, 20 Apr 2017 10:50:44 +0200 Subject: [PATCH 1/4] Fixed perf test --- perf/parser-streaming-perf.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/perf/parser-streaming-perf.php b/perf/parser-streaming-perf.php index 803713e..a0a813c 100644 --- a/perf/parser-streaming-perf.php +++ b/perf/parser-streaming-perf.php @@ -13,8 +13,7 @@ $TEST = microtime(true); $count = 0; -$parser = new TriGParser([ "documentIRI" => $base ]); -$callback = function ($error, $triple) use (&$count, $TEST, $filename) { +$parser = new TriGParser([ "documentIRI" => $base ], function ($error, $triple) use (&$count, $TEST, $filename) { if ($triple) { $count++; } @@ -23,14 +22,14 @@ echo '* Triples parsed: ' . $count . "\n"; echo '* Memory usage: ' . (memory_get_usage() / 1024 / 1024) . "MB\n"; } -}; +}); $handle = fopen($filename, "r"); if ($handle) { - while (($line = fgets($handle)) !== false) { - $parser->parseChunk($line, $callback); + while (($line = fgets($handle, 4096)) !== false) { + $parser->parseChunk($line); } - $parser->end($callback); + $parser->end(); fclose($handle); } else { // error opening the file. From b7486f9d3a2027c38df19c51a44f8bdcb162c2d2 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Tue, 14 Nov 2017 12:21:35 +0100 Subject: [PATCH 2/4] Util.php: fixed string concatenation (+ => .) if you use `+` in PHP it actually tries to sum up things, not combine them. Use `.` instead. --- src/Util.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Util.php b/src/Util.php index a43f88a..585f45e 100644 --- a/src/Util.php +++ b/src/Util.php @@ -49,7 +49,7 @@ public static function getLiteralValue ($literal) { preg_match("/^\"(.*)\"/", $literal, $match); //TODO: somehow the copied regex did not work. To be checked. Contained [^] if (empty($match)) { - throw new \Exception($literal + ' is not a literal'); + throw new \Exception($literal . ' is not a literal'); } return $match[1]; } @@ -58,7 +58,7 @@ public static function getLiteralType ($literal) { preg_match('/^".*"(?:\^\^([^"]+)|(@)[^@"]+)?$/',$literal,$match);//TODO: somehow the copied regex did not work. To be checked. Contained [^] instead of the . if (empty($match)) - throw new \Exception($literal + ' is not a literal'); + throw new \Exception($literal . ' is not a literal'); if (!empty($match[1])) { return $match[1]; } else { @@ -72,7 +72,7 @@ public static function getLiteralLanguage ($literal) { preg_match('/^".*"(?:@([^@"]+)|\^\^[^"]+)?$/', $literal, $match); if (empty($match)) - throw new \Exception($literal + ' is not a literal'); + throw new \Exception($literal . ' is not a literal'); return isset($match[1]) ? strtolower($match[1]) : ''; } From 029be51c269c89f057a0cba71f171dc21a62837b Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Tue, 14 Nov 2017 12:37:46 +0100 Subject: [PATCH 3/4] UtilTest.php: two new tests to check reaction of getLiteralLanguage and getLiteralValue if non-literal was given --- test/UtilTest.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/test/UtilTest.php b/test/UtilTest.php index 01d6b77..ff83e09 100644 --- a/test/UtilTest.php +++ b/test/UtilTest.php @@ -119,6 +119,14 @@ public function testGetLiteralValue () { //it('does not work with null', function () { //TODO: Util::getLiteralValue.bind(null, null).should.throw('null is not a literal'); } + + // tests reaction if no literal was given + public function testGetLiteralValueNoLiteralGiven() + { + $this->expectException('\Exception'); + + Util::getLiteralValue('invalid'); + } public function testGetLiteralType () { //it('gets the type of a literal', function () { @@ -190,7 +198,14 @@ public function testGetLiteralLanguage () { //it('does not work with null', function () { //TODO: Util::getLiteralLanguage.bind(null, null).should.throw('null is not a literal'); } - + + // tests reaction if no language was given + public function testGetLiteralLanguageNoLiteralGiven() + { + $this->expectException('\Exception'); + + Util::getLiteralLanguage('invalid'); + } public function testIsPrefixedName () { //it('matches a prefixed name', function () { @@ -353,4 +368,4 @@ public function testthe function () { } */ } - \ No newline at end of file + From ded2e85697b779a8ab47248bb4931d243c34680a Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Tue, 14 Nov 2017 13:06:21 +0100 Subject: [PATCH 4/4] Util.php: added s to regex in getLiteralType and getLiteralLanguage Before that fix, certain multi line strings could not be processed. Example string: "This document is published by the Provenance Working Group (http://www.w3.org/2011/prov/wiki/Main_Page). If you wish to make comments regarding this document, please send them to public-prov-comments@w3.org (subscribe public-prov-comments-request@w3.org, archives http://lists.w3.org/Archives/Public/public-prov-comments/). All feedback is welcome."^^ --- src/Util.php | 4 ++-- test/UtilTest.php | 28 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Util.php b/src/Util.php index a43f88a..f37104e 100644 --- a/src/Util.php +++ b/src/Util.php @@ -56,7 +56,7 @@ public static function getLiteralValue ($literal) // Gets the type of a literal in the N3 library public static function getLiteralType ($literal) { - preg_match('/^".*"(?:\^\^([^"]+)|(@)[^@"]+)?$/',$literal,$match);//TODO: somehow the copied regex did not work. To be checked. Contained [^] instead of the . + preg_match('/^".*"(?:\^\^([^"]+)|(@)[^@"]+)?$/s',$literal,$match);//TODO: somehow the copied regex did not work. To be checked. Contained [^] instead of the . if (empty($match)) throw new \Exception($literal + ' is not a literal'); if (!empty($match[1])) { @@ -70,7 +70,7 @@ public static function getLiteralType ($literal) // Gets the language of a literal in the N3 library public static function getLiteralLanguage ($literal) { - preg_match('/^".*"(?:@([^@"]+)|\^\^[^"]+)?$/', $literal, $match); + preg_match('/^".*"(?:@([^@"]+)|\^\^[^"]+)?$/s', $literal, $match); if (empty($match)) throw new \Exception($literal + ' is not a literal'); return isset($match[1]) ? strtolower($match[1]) : ''; diff --git a/test/UtilTest.php b/test/UtilTest.php index 01d6b77..28e5a7f 100644 --- a/test/UtilTest.php +++ b/test/UtilTest.php @@ -151,8 +151,18 @@ public function testGetLiteralType () { //it('does not work with null', function () { //TODO: Util::getLiteralType.bind(null, null).should.throw('null is not a literal'); - } - + } + + // tests getLiteralType if multi line string was given (check for adaption of Util.php, + // adding an s to the regex) + public function testGetLiteralTypeMultilineString() + { + $literal = '"This document is published by the Provenance Working Group (http://www.w3.org/2011/prov/wiki/Main_Page). + +If you wish to make comments regarding this document, please send them to public-prov-comments@w3.org (subscribe public-prov-comments-request@w3.org, archives http://lists.w3.org/Archives/Public/public-prov-comments/). All feedback is welcome."^^'; + + $this->assertEquals('', Util::getLiteralType($literal)); + } public function testGetLiteralLanguage () { //it('gets the language of a literal', function () { @@ -188,9 +198,19 @@ public function testGetLiteralLanguage () { //it('does not work with null', function () { - //TODO: Util::getLiteralLanguage.bind(null, null).should.throw('null is not a literal'); + //TODO: Util::getLiteralLanguage.bind(null, null).should.throw('null is not a literal'); + } + + // tests getLiteralLanguage if multi line string was given (check for adaption of Util.php, + // adding an s to the regex) + public function testGetLiteralLanguageMultilineString() + { + $literal = '"This document is published by the Provenance Working Group (http://www.w3.org/2011/prov/wiki/Main_Page). + +If you wish to make comments regarding this document, please send them to public-prov-comments@w3.org (subscribe public-prov-comments-request@w3.org, archives http://lists.w3.org/Archives/Public/public-prov-comments/). All feedback is welcome."@en'; + + $this->assertEquals('en', Util::getLiteralLanguage($literal)); } - public function testIsPrefixedName () { //it('matches a prefixed name', function () {