Skip to content

Commit

Permalink
Merge pull request #975 from oat-sa/release-15.16.0
Browse files Browse the repository at this point in the history
Release 15.16.0
  • Loading branch information
gabrielfs7 authored Feb 7, 2022
2 parents 86b6143 + 924ebdb commit d823948
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 35 deletions.
11 changes: 11 additions & 0 deletions core/kernel/classes/class.Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ public function isClass()
return (bool) $returnValue;
}

public function isWritable(): bool
{
$implementation = $this->getImplementation();

if ($implementation instanceof core_kernel_persistence_smoothsql_Resource) {
return $implementation->isWritable($this);
}

return true;
}

/**
* returns true if the resource is a valid property (using facts or
* rules)
Expand Down
59 changes: 31 additions & 28 deletions core/kernel/persistence/smoothsql/class.Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,35 @@ class core_kernel_persistence_smoothsql_Resource implements core_kernel_persiste
* @var core_kernel_persistence_smoothsql_SmoothModel
*/
private $model;

public function __construct(core_kernel_persistence_smoothsql_SmoothModel $model)
{
$this->model = $model;
}

protected function getModel()
{
return $this->model;
}

/**
* @return common_persistence_SqlPersistence
*/
protected function getPersistence()
{
return $this->model->getPersistence();
}

protected function getModelReadSqlCondition()
{
return 'modelid IN (' . implode(',', $this->model->getReadableModels()) . ')';
}

protected function getModelWriteSqlCondition()
{
return 'modelid IN (' . implode(',', $this->model->getWritableModels()) . ')';
}

protected function getNewTripleModelId()
{
return $this->model->getNewTripleModelId();
Expand All @@ -96,7 +96,6 @@ public function getTypes(core_kernel_classes_Resource $resource)
$uri = $this->getPersistence()->getPlatForm()->getPhpTextValue($row['object']);
$returnValue[$uri] = $this->getModel()->getClass($uri);
}


return (array) $returnValue;
}
Expand All @@ -121,7 +120,7 @@ public function getPropertyValues(core_kernel_classes_Resource $resource, core_k
throw new core_kernel_persistence_Exception('Option \'last\' no longer supported');
}
$platform = $this->getPersistence()->getPlatForm();

// Define language if required
$defaultLg = '';
if (isset($options['lg'])) {
Expand All @@ -139,7 +138,7 @@ public function getPropertyValues(core_kernel_classes_Resource $resource, core_k
AND predicate = ?
AND (l_language = ? OR l_language = ' . $this->getPersistence()->quote('') . $defaultLg . ')
AND ' . $this->getModelReadSqlCondition();

if ($one) {
// Select first
$query .= ' ORDER BY id DESC';
Expand All @@ -149,7 +148,7 @@ public function getPropertyValues(core_kernel_classes_Resource $resource, core_k
// Select All
$result = $this->getPersistence()->query($query, [$resource->getUri(), $property->getUri(), $lang]);
}

// Treat the query result
if ($result == true) {
if (isset($options['lg'])) {
Expand All @@ -162,7 +161,7 @@ public function getPropertyValues(core_kernel_classes_Resource $resource, core_k
$returnValue = core_kernel_persistence_smoothsql_Utils::filterByLanguage($this->getPersistence(), $result->fetchAll(), 'l_language', $lang, $default);
}
}

return (array) $returnValue;
}

Expand All @@ -180,12 +179,12 @@ public function getPropertyValues(core_kernel_classes_Resource $resource, core_k
public function getPropertyValuesByLg(core_kernel_classes_Resource $resource, core_kernel_classes_Property $property, $lg)
{
$options = ['lg' => $lg];

$returnValue = new core_kernel_classes_ContainerCollection($resource);
foreach ($this->getPropertyValues($resource, $property, $options) as $value) {
$returnValue->add(common_Utils::toResource($value));
}

return $returnValue;
}

Expand Down Expand Up @@ -316,14 +315,14 @@ public function removePropertyValues(core_kernel_classes_Resource $resource, cor
$conditions[] = "{$multiCondition} ) ";
}
}

foreach ($conditions as $i => $additionalCondition) {
$query .= " AND ( {$additionalCondition} ) ";
}

//be sure the property we try to remove is included in an updatable model
$query .= ' AND ' . $this->getModelWriteSqlCondition();

if ($property->isLgDependent()) {
$query .= ' AND (l_language = ? OR l_language = ?) ';
$returnValue = $this->getPersistence()->exec($query, [
Expand All @@ -338,11 +337,11 @@ public function removePropertyValues(core_kernel_classes_Resource $resource, cor
$property->getUri()
]);
}

if (!$returnValue) {
$returnValue = false;
}

return (bool) $returnValue;
}

Expand All @@ -362,13 +361,13 @@ public function removePropertyValueByLg(core_kernel_classes_Resource $resource,
$sqlQuery = 'DELETE FROM statements WHERE subject = ? and predicate = ? and l_language = ?';
//be sure the property we try to remove is included in an updatable model
$sqlQuery .= ' AND ' . $this->getModelWriteSqlCondition();

$returnValue = $this->getPersistence()->exec($sqlQuery, [
$resource->getUri(),
$property->getUri(),
($property->isLgDependent() ? $lg : ''),
]);

if (!$returnValue) {
$returnValue = false;
}
Expand All @@ -389,7 +388,7 @@ public function getRdfTriples(core_kernel_classes_Resource $resource)
// TODO: refactor this to use a triple store abstraction
$query = 'SELECT * FROM statements WHERE subject = ? AND ' . $this->getModelReadSqlCondition() . ' ORDER BY predicate';
$result = $this->getPersistence()->query($query, [$resource->getUri()]);

$returnValue = new core_kernel_classes_ContainerCollection(new common_Object(__METHOD__));
while ($statement = $result->fetch()) {
$triple = new core_kernel_classes_Triple();
Expand All @@ -407,6 +406,11 @@ public function getRdfTriples(core_kernel_classes_Resource $resource)
return $returnValue;
}

public function isWritable(core_kernel_classes_Resource $resource): bool
{
return $this->model->isWritable($resource);
}

/**
* Short description of method getUsedLanguages
*
Expand Down Expand Up @@ -495,12 +499,11 @@ public function delete(core_kernel_classes_Resource $resource, $deleteReference
} elseif ($deleteReference) {
$sqlQuery = 'DELETE FROM statements WHERE ' . $this->getPersistence()->getPlatForm()->getObjectTypeCondition() . ' = ? AND ' . $this->getModelWriteSqlCondition();
$return = $this->getPersistence()->exec($sqlQuery, [$resource->getUri()]);

if ($return !== false) {
$returnValue = true;
}
}


return (bool) $returnValue;
}
Expand All @@ -522,7 +525,7 @@ public function getPropertiesValues(core_kernel_classes_Resource $resource, $pro
if (count($properties) == 0) {
return [];
}

$predicatesQuery = '';
//build the predicate query
//$predicatesQuery = implode(',', $properties);
Expand All @@ -549,7 +552,7 @@ public function getPropertiesValues(core_kernel_classes_Resource $resource, $pro
' OR l_language = ' . $this->getPersistence()->quote($lang) . ')
AND ' . $this->getModelReadSqlCondition();
$result = $this->getPersistence()->query($query);

$rows = $result->fetchAll();
foreach ($rows as $row) {
$value = $platform->getPhpTextValue($row['object']);
Expand Down Expand Up @@ -588,16 +591,16 @@ public function removeType(core_kernel_classes_Resource $resource, core_kernel_c
{
$query = 'DELETE FROM statements
WHERE subject = ? AND predicate = ? AND ' . $this->getPersistence()->getPlatForm()->getObjectTypeCondition() . ' = ?';

//be sure the property we try to remove is included in an updatable model
$query .= ' AND ' . $this->getModelWriteSqlCondition();

$returnValue = $this->getPersistence()->exec($query, [
$resource->getUri(),
OntologyRdf::RDF_TYPE,
$class->getUri()
]);

$returnValue = true;

return $returnValue;
Expand Down
28 changes: 21 additions & 7 deletions core/kernel/persistence/smoothsql/class.SmoothModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class core_kernel_persistence_smoothsql_SmoothModel extends ConfigurableService
* @var common_persistence_SqlPersistence
*/
private $persistence;

function getResource($uri)
{
$resource = new \core_kernel_classes_Resource($uri);
Expand All @@ -73,6 +73,20 @@ function getProperty($uri)
return $property;
}

public function isWritable(core_kernel_classes_Resource $resource): bool
{
$writableModels = $this->getWritableModels();

/** @var core_kernel_classes_Triple $triple */
foreach ($resource->getRdfTriples() as $triple) {
if (!in_array((int)$triple->modelid, $writableModels, true)) {
return false;
}
}

return true;
}

/**
* @return common_persistence_SqlPersistence
*/
Expand All @@ -99,7 +113,7 @@ public function getRdfInterface()
{
return new core_kernel_persistence_smoothsql_SmoothRdf($this);
}

/**
* (non-PHPdoc)
* @see \oat\generis\model\data\Model::getRdfsInterface()
Expand All @@ -108,7 +122,7 @@ public function getRdfsInterface()
{
return new core_kernel_persistence_smoothsql_SmoothRdfs($this);
}

/**
* @return ComplexSearchService
*/
Expand All @@ -120,7 +134,7 @@ public function getSearchInterface()
}

// Manage the sudmodels of the smooth mode

/**
* Returns the id of the model to add to
*
Expand All @@ -130,7 +144,7 @@ public function getNewTripleModelId()
{
return $this->getOption(self::OPTION_NEW_TRIPLE_MODEL);
}

public function getReadableModels()
{
return $this->getOption(self::OPTION_READABLE_MODELS);
Expand All @@ -140,7 +154,7 @@ public function getWritableModels()
{
return $this->getOption(self::OPTION_WRITEABLE_MODELS);
}

//
// Deprecated functions
//
Expand Down Expand Up @@ -176,7 +190,7 @@ public static function getReadableModelIds()
}
return $model->getReadableModels();
}

/**
* Returns the submodel ids that are updatable
*
Expand Down
Loading

0 comments on commit d823948

Please sign in to comment.