Skip to content

Commit

Permalink
PLT-71 Fix PHP type issues (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrliptontea authored Dec 1, 2023
1 parent 2bf1389 commit e7c7b58
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 41 deletions.
11 changes: 8 additions & 3 deletions src/classes/ExtendedGraph.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ExtendedGraph
var $_labeller;

/**
* @param string|\Tripod\ExtendedGraph $graph
* @param string|array $graph
*/
public function __construct($graph=null){
$this->_labeller = new Labeller();
Expand Down Expand Up @@ -549,7 +549,10 @@ public function from_rdfxml($rdfxml, $base='') {
public function from_json($json) {
if ($json) {
$this->remove_all_triples();
$this->_index = json_decode($json, true);
$index = json_decode($json, true);
if (is_array($index)) {
$this->_index = $index;
}
}
}

Expand All @@ -562,7 +565,9 @@ public function from_json($json) {
public function add_json($json) {
if ($json) {
$json_index = json_decode($json, true);
$this->_index = $this->merge($this->_index, $json_index);
if (is_array($json_index)) {
$this->_index = $this->merge($this->_index, $json_index);
}
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/mongo/Driver.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,13 @@ public function select($query,$fields,$sortBy=null,$limit=null,$offset=0,$contex
{
$row[$key] = $value;
}
else
elseif (is_array($value))
{
if (array_key_exists(VALUE_LITERAL,$value))
if (isset($value[VALUE_LITERAL]))
{
$row[$key] = $value[VALUE_LITERAL];
}
else if (array_key_exists(VALUE_URI,$value))
elseif (isset($value[VALUE_URI]))
{
$row[$key] = $value[VALUE_URI];
}
Expand All @@ -502,7 +502,14 @@ public function select($query,$fields,$sortBy=null,$limit=null,$offset=0,$contex
// possible array of values
foreach ($value as $v)
{
$row[$key][] = array_key_exists(VALUE_LITERAL,$v) ? $v[VALUE_LITERAL] : $v[VALUE_URI];
if (isset($v[VALUE_LITERAL]))
{
$row[$key][] = $v[VALUE_LITERAL];
}
else if (isset($v[VALUE_URI]))
{
$row[$key][] = $v[VALUE_URI];
}
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/mongo/delegates/SearchDocuments.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,16 @@ protected function addFields(Array $source, Array $fieldsOrIndices, Array &$targ
$values = array();

if(isset($source[$p])){
if(isset($source[$p]['u'])){
$values[] = ($isIndex) ? mb_strtolower(trim($source[$p]['u']), 'UTF-8') : trim($source[$p]['u']);
} else if (isset($source[$p]['l'])){
$values[] = ($isIndex) ? mb_strtolower(trim($source[$p]['l']), 'UTF-8') : trim($source[$p]['l']);
} else {
if(isset($source[$p][VALUE_URI])){
$values[] = ($isIndex) ? mb_strtolower(trim($source[$p][VALUE_URI]), 'UTF-8') : trim($source[$p][VALUE_URI]);
} elseif (isset($source[$p][VALUE_LITERAL])){
$values[] = ($isIndex) ? mb_strtolower(trim($source[$p][VALUE_LITERAL]), 'UTF-8') : trim($source[$p][VALUE_LITERAL]);
} elseif (is_array($source[$p])) {
foreach($source[$p] as $v){
if(isset($v['u'])){
$values[] = ($isIndex) ? mb_strtolower(trim($v['u']), 'UTF-8') : trim($v['u']);
} else {
$values[] = ($isIndex) ? mb_strtolower(trim($v['l']), 'UTF-8') : trim($v['l']);
if(isset($v[VALUE_URI])){
$values[] = ($isIndex) ? mb_strtolower(trim($v[VALUE_URI]), 'UTF-8') : trim($v[VALUE_URI]);
} elseif(isset($v[VALUE_LITERAL])) {
$values[] = ($isIndex) ? mb_strtolower(trim($v[VALUE_LITERAL]), 'UTF-8') : trim($v[VALUE_LITERAL]);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/mongo/delegates/SearchIndexer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ public function generateAndIndexSearchDocuments($resourceUri, $context, $podName
]
);
foreach ($resourceAndType as $rt) {
if (array_key_exists("rdf:type", $rt)) {
if (isset($rt["rdf:type"])) {
$rdfTypes = [];

if (array_key_exists('u', $rt["rdf:type"])) {
$rdfTypes[] = $rt["rdf:type"]['u'];
if (isset($rt["rdf:type"][VALUE_URI])) {
$rdfTypes[] = $rt["rdf:type"][VALUE_URI];
} else {
// an array of types
foreach ($rt["rdf:type"] as $type) {
$rdfTypes[] = $type['u'];
if (isset($type[VALUE_URI])) {
$rdfTypes[] = $type[VALUE_URI];
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/mongo/delegates/Tables.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,22 +433,22 @@ protected function generateTableRowsForResource($resource, $context=null, $specT
foreach ($resourceAndType as $rt)
{
$id = $rt["_id"];
if (array_key_exists("rdf:type",$rt))
if (isset($rt["rdf:type"]))
{
if (array_key_exists('u',$rt["rdf:type"]))
if (isset($rt["rdf:type"][VALUE_URI]))
{
// single type, not an array of values
$this->generateTableRowsForType($rt["rdf:type"]['u'],$id[_ID_RESOURCE],$id[_ID_CONTEXT], $specTypes);
$this->generateTableRowsForType($rt["rdf:type"][VALUE_URI],$id[_ID_RESOURCE],$id[_ID_CONTEXT], $specTypes);
}
else
{
// an array of types
foreach ($rt["rdf:type"] as $type)
{
// Defensive check in case there is bad data for rdf:type
if(array_key_exists('u', $type))
if (isset($type[VALUE_URI]))
{
$this->generateTableRowsForType($type['u'],$id[_ID_RESOURCE],$id[_ID_CONTEXT], $specTypes);
$this->generateTableRowsForType($type[VALUE_URI],$id[_ID_RESOURCE],$id[_ID_CONTEXT], $specTypes);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/mongo/delegates/Updates.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ protected function applyChangeSet(\Tripod\ChangeSet $cs, $originalCBDs, $context
$valueIndex = array_search($removal, $valueObject);
if($valueIndex === false)
{
$v = array_pop(array_values($removal));
$values = array_values($removal);
$v = array_pop($values);
$this->errorLog("Removal value {$subjectOfChange} {$predicate} {$v} does not appear in target document to be updated",array("doc"=>$doc));
throw new \Exception("Removal value {$subjectOfChange} {$predicate} {$v} does not appear in target document to be updated");
}
Expand Down
10 changes: 6 additions & 4 deletions src/mongo/delegates/Views.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,19 +329,21 @@ public function generateViews($resources,$context=null)
foreach ($resourceAndType as $rt)
{
$id = $rt["_id"];
if (array_key_exists("rdf:type",$rt))
if (isset($rt["rdf:type"]))
{
if (array_key_exists('u',$rt["rdf:type"]))
if (isset($rt["rdf:type"][VALUE_URI]))
{
// single type, not an array of values
$this->generateViewsForResourcesOfType($rt["rdf:type"]['u'],$id[_ID_RESOURCE],$id[_ID_CONTEXT]);
$this->generateViewsForResourcesOfType($rt["rdf:type"][VALUE_URI],$id[_ID_RESOURCE],$id[_ID_CONTEXT]);
}
else
{
// an array of types
foreach ($rt["rdf:type"] as $type)
{
$this->generateViewsForResourcesOfType($type['u'],$id[_ID_RESOURCE],$id[_ID_CONTEXT]);
if (isset($type[VALUE_URI])) {
$this->generateViewsForResourcesOfType($type[VALUE_URI],$id[_ID_RESOURCE],$id[_ID_CONTEXT]);
}
}
}
}
Expand Down
90 changes: 89 additions & 1 deletion test/unit/ExtendedGraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,5 +700,93 @@ public function testReplaceLiteralTriples()
$this->assertFalse($graph->has_literal_triple("http://test/1", 'http://www.w3.org/2000/01/rdf-schema#label', "value1"));
$this->assertFalse($graph->has_literal_triple("http://test/1", 'http://www.w3.org/2000/01/rdf-schema#label', "value2"));
}

public function testFromJson()
{
$graph = new ExtendedGraph();
$graph->from_json('{
"http://subject/1": {
"http://predicate": [
{ "type": "uri", "value": "http://value/1" }
]
},
"http://subject/2": {
"http://predicate": [
{ "type": "uri", "value": "http://value/2" }
]
},
"http://subject/3": {
"http://predicate": [
{ "type": "uri", "value": "http://value/3" }
]
}
}');

$this->assertEquals(3, count($graph->get_subjects()));
$this->assertEquals(['http://subject/1', 'http://subject/2', 'http://subject/3'], $graph->get_subjects());
$this->assertEquals(['http://value/1', 'http://value/2', 'http://value/3'], $graph->get_resource_properties('http://predicate'));
}

public function testFromInvalidJson()
{
$graph = new ExtendedGraph();
$index = $graph->get_index();

$graph->from_json('not a valid json');

// Should not have changed
$this->assertEquals($index, $graph->get_index());
}

public function testAddJson()
{
$graph = new ExtendedGraph();
$graph->add_json('{
"http://subject/1": {
"http://predicate": [
{ "type": "uri", "value": "http://value/1" }
]
}
}');

$this->assertEquals(1, count($graph->get_subjects()));
$this->assertEquals(['http://subject/1'], $graph->get_subjects());
$this->assertEquals(['http://value/1'], $graph->get_resource_properties('http://predicate'));

$graph->add_json('{
"http://subject/2": {
"http://predicate": [
{ "type": "uri", "value": "http://value/2" }
]
}
}');

$this->assertEquals(2, count($graph->get_subjects()));
$this->assertEquals(['http://subject/1', 'http://subject/2'], $graph->get_subjects());
$this->assertEquals(['http://value/1', 'http://value/2'], $graph->get_resource_properties('http://predicate'));
}

public function testAddInvalidJson()
{
$graph = new ExtendedGraph();
$graph->add_json('{
"http://subject/1": {
"http://predicate": [
{ "type": "uri", "value": "http://value/1" }
]
}
}');

$this->assertEquals(1, $graph->get_triple_count());
$this->assertEquals(1, count($graph->get_subjects()));
$this->assertEquals(['http://subject/1'], $graph->get_subjects());
$this->assertEquals(['http://value/1'], $graph->get_resource_properties('http://predicate'));
$index = $graph->get_index();

$graph->add_json('not a valid json');

// Should not have changed
$this->assertEquals($index, $graph->get_index());
$this->assertEquals(1, $graph->get_triple_count());
}
}
?>
6 changes: 4 additions & 2 deletions test/unit/mongo/MongoSearchProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ protected function setUp()
$t = array();
if(isset($result['rdf:type']['u'])){
$t[] = $result['rdf:type']['u'];
} else {
} elseif (is_array($result['rdf:type'])) {
foreach($result['rdf:type'] as $_t){
$t[] = $_t['u'];
if (isset($_t['u'])) {
$t[] = $_t['u'];
}
}
}
$this->indexer->generateAndIndexSearchDocuments($result['_id']['r'], $result['_id']['c'], $this->tripod->getPodName());
Expand Down
4 changes: 2 additions & 2 deletions test/unit/mongo/MongoTripodComputedFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public function testReplaceComputedField()
$this->assertArrayNotHasKey('rdfType', $tableDoc['value']);

$tableDoc = $collection->findOne(array('_id.type'=>'t_replace_type', '_id.r'=>'http://talisaspire.com/resources/3SplCtWGPqEyXcDiyhHQpA'));
$this->assertEquals('Book Resource', $tableDoc['value']['resourceType']);
$this->assertEquals('Book Resource Testing', $tableDoc['value']['resourceType']);
$this->assertArrayNotHasKey('rdfType', $tableDoc['value']);


Expand Down Expand Up @@ -499,4 +499,4 @@ public function testNestConditionalInArithmeticFunction()
\Tripod\Config::getInstance();
$collection->drop();
}
}
}
Loading

0 comments on commit e7c7b58

Please sign in to comment.