From 9b406eb94794df81b04868034ccc148c32ea6256 Mon Sep 17 00:00:00 2001 From: Gabriel Felipe Soares Date: Tue, 2 Nov 2021 15:53:00 +0100 Subject: [PATCH] feat: remove system properties from indexation --- README.md | 12 +++- manifest.php | 4 ++ .../Normalizer/MetadataNormalizer.php | 30 +++++--- .../MetadataServiceProvider.php | 59 ++++++++++++++++ .../PropertyAllowedSpecification.php | 52 ++++++++++++++ .../Normalizer/MetadataNormalizerTest.php | 14 ++++ .../PropertyAllowedSpecificationTest.php | 69 +++++++++++++++++++ 7 files changed, 229 insertions(+), 11 deletions(-) create mode 100644 model/Metadata/ServiceProvider/MetadataServiceProvider.php create mode 100644 model/Metadata/Specification/PropertyAllowedSpecification.php create mode 100644 tests/Unit/Metadata/Specification/PropertyAllowedSpecificationTest.php diff --git a/README.md b/README.md index ee9b8c8c..787109a9 100644 --- a/README.md +++ b/README.md @@ -157,4 +157,14 @@ Resources not indexed for class http://www.tao.lu/Ontologies/TAOItem.rdf#Item Summary Missing resources: 1 Missing resources indexed: 1 -``` \ No newline at end of file +``` + +## Env variables + +### Avoid indexing metadata + +To avoid indexing metadata that is used in the criteria filter: + +```shell +ADVANCED_SEARCH_METADATA_BLACK_LIST=URI1,URI2,URI3 +``` diff --git a/manifest.php b/manifest.php index abaa0cba..603cc20d 100755 --- a/manifest.php +++ b/manifest.php @@ -18,6 +18,7 @@ * Copyright (c) 2021 (original work) Open Assessment Technologies SA; */ +use oat\taoAdvancedSearch\model\Metadata\ServiceProvider\MetadataServiceProvider; use oat\taoAdvancedSearch\scripts\install\RegisterEvents; use oat\taoAdvancedSearch\scripts\install\RegisterServices; use oat\taoAdvancedSearch\scripts\install\RegisterTaskQueueServices; @@ -52,5 +53,8 @@ ], 'constants' => [ 'BASE_URL' => ROOT_URL . 'taoAdvancedSearch/', + ], + 'containerServiceProviders' => [ + MetadataServiceProvider::class, ] ]; diff --git a/model/Metadata/Normalizer/MetadataNormalizer.php b/model/Metadata/Normalizer/MetadataNormalizer.php index 86765b82..e8baa895 100644 --- a/model/Metadata/Normalizer/MetadataNormalizer.php +++ b/model/Metadata/Normalizer/MetadataNormalizer.php @@ -31,6 +31,7 @@ use oat\taoAdvancedSearch\model\Index\IndexResource; use oat\taoAdvancedSearch\model\Index\Normalizer\NormalizerInterface; use oat\taoAdvancedSearch\model\Metadata\Factory\ClassPathFactory; +use oat\taoAdvancedSearch\model\Metadata\Specification\PropertyAllowedSpecification; use oat\taoAdvancedSearch\model\Resource\Repository\IndexableClassCachedRepository; use oat\taoAdvancedSearch\model\Resource\Repository\IndexableClassRepositoryInterface; @@ -77,15 +78,19 @@ private function getPropertiesFromClass(core_kernel_classes_Class $class): array ? $this->getGetClassMetadataValuesService()->getByClassRecursive($class, 0) : $this->getGetClassMetadataValuesService()->getByClassExplicitly($class, 0); + $specification = $this->getPropertyAllowedSpecification(); + /** @var Metadata $property */ foreach ($properties as $property) { - $propertyCollection[] = [ - 'propertyUri' => $property->getPropertyUri(), - 'propertyLabel' => $property->getLabel(), - 'propertyAlias' => $property->getAlias(), - 'propertyType' => $property->getType(), - 'propertyValues' => null, - ]; + if ($specification->isSatisfiedBy($property->getPropertyUri())) { + $propertyCollection[] = [ + 'propertyUri' => $property->getPropertyUri(), + 'propertyLabel' => $property->getLabel(), + 'propertyAlias' => $property->getAlias(), + 'propertyType' => $property->getType(), + 'propertyValues' => null, + ]; + } } return $propertyCollection; @@ -93,12 +98,12 @@ private function getPropertiesFromClass(core_kernel_classes_Class $class): array private function getGetClassMetadataValuesService(): GetClassMetadataValuesService { - return $this->getServiceLocator()->get(GetClassMetadataValuesService::class); + return $this->getServiceManager()->getContainer()->get(GetClassMetadataValuesService::class); } private function getClassPathFactory(): ClassPathFactory { - return $this->getServiceLocator()->get(ClassPathFactory::class); + return $this->getServiceManager()->getContainer()->get(ClassPathFactory::class); } private function isRootClass(core_kernel_classes_Class $class) @@ -108,6 +113,11 @@ private function isRootClass(core_kernel_classes_Class $class) private function getIndexableClassRepository(): IndexableClassRepositoryInterface { - return $this->getServiceLocator()->get(IndexableClassCachedRepository::class); + return $this->getServiceManager()->getContainer()->get(IndexableClassCachedRepository::class); + } + + private function getPropertyAllowedSpecification(): PropertyAllowedSpecification + { + return $this->getServiceManager()->getContainer()->get(PropertyAllowedSpecification::class); } } diff --git a/model/Metadata/ServiceProvider/MetadataServiceProvider.php b/model/Metadata/ServiceProvider/MetadataServiceProvider.php new file mode 100644 index 00000000..0da0037e --- /dev/null +++ b/model/Metadata/ServiceProvider/MetadataServiceProvider.php @@ -0,0 +1,59 @@ +setParameters($configurator); + + $services = $configurator->services(); + + $services->set(PropertyAllowedSpecification::class, PropertyAllowedSpecification::class) + ->args( + [ + param(PropertyAllowedSpecification::CONFIG_BLACK_LIST) + ] + )->public(); + } + + private function setParameters(ContainerConfigurator $configurator): void + { + $parameters = $configurator->parameters(); + $parameters->set( + PropertyAllowedSpecification::CONFIG_BLACK_LIST, + array_filter( + explode( + ',', + (string)getenv(PropertyAllowedSpecification::CONFIG_BLACK_LIST) + ) + ) + ); + } +} diff --git a/model/Metadata/Specification/PropertyAllowedSpecification.php b/model/Metadata/Specification/PropertyAllowedSpecification.php new file mode 100644 index 00000000..fa4e46f4 --- /dev/null +++ b/model/Metadata/Specification/PropertyAllowedSpecification.php @@ -0,0 +1,52 @@ +blackListUris = array_merge($blackListUris, self::SYSTEM_PROPERTIES); + } + + public function isSatisfiedBy(string $propertyUri): bool + { + return !in_array($propertyUri, $this->blackListUris, true); + } +} diff --git a/tests/Unit/Metadata/Normalizer/MetadataNormalizerTest.php b/tests/Unit/Metadata/Normalizer/MetadataNormalizerTest.php index 197e64ad..2587cbc1 100644 --- a/tests/Unit/Metadata/Normalizer/MetadataNormalizerTest.php +++ b/tests/Unit/Metadata/Normalizer/MetadataNormalizerTest.php @@ -32,6 +32,7 @@ use oat\tao\model\TaoOntology; use oat\taoAdvancedSearch\model\Metadata\Factory\ClassPathFactory; use oat\taoAdvancedSearch\model\Metadata\Normalizer\MetadataNormalizer; +use oat\taoAdvancedSearch\model\Metadata\Specification\PropertyAllowedSpecification; use oat\taoAdvancedSearch\model\Resource\Repository\IndexableClassCachedRepository; use PHPUnit\Framework\MockObject\MockObject; @@ -58,6 +59,9 @@ class MetadataNormalizerTest extends TestCase /** @var IndexableClassCachedRepository|MockObject */ private $indexableClassRepository; + /** @var PropertyAllowedSpecification|MockObject */ + private $propertyAllowedSpecification; + public function setUp(): void { $this->subject = new MetadataNormalizer(); @@ -67,6 +71,7 @@ public function setUp(): void $this->metadataMock = $this->createMock(Metadata::class); $this->ontology = $this->createMock(Ontology::class); $this->classPathFactory = $this->createMock(ClassPathFactory::class); + $this->propertyAllowedSpecification = $this->createMock(PropertyAllowedSpecification::class); $this->classPathFactory ->method('create') @@ -79,6 +84,7 @@ public function setUp(): void Ontology::SERVICE_ID => $this->ontology, ClassPathFactory::class => $this->classPathFactory, IndexableClassCachedRepository::class => $this->indexableClassRepository, + PropertyAllowedSpecification::class => $this->propertyAllowedSpecification, ] ) ); @@ -90,6 +96,10 @@ public function testNormalizeTakesOnlyClass(): void ->method('getClass') ->willReturn($this->classMock); + $this->propertyAllowedSpecification + ->method('isSatisfiedBy') + ->willReturn(true); + $this->classMock ->method('isClass') ->willReturn(false); @@ -116,6 +126,10 @@ public function testNormalize( ] ); + $this->propertyAllowedSpecification + ->method('isSatisfiedBy') + ->willReturn(true); + $this->ontology ->method('getClass') ->willReturn($this->classMock); diff --git a/tests/Unit/Metadata/Specification/PropertyAllowedSpecificationTest.php b/tests/Unit/Metadata/Specification/PropertyAllowedSpecificationTest.php new file mode 100644 index 00000000..66f8fac6 --- /dev/null +++ b/tests/Unit/Metadata/Specification/PropertyAllowedSpecificationTest.php @@ -0,0 +1,69 @@ +subject = new PropertyAllowedSpecification( + [ + self::PROPERTY_FORBIDDEN, + ] + ); + } + + /** + * @dataProvider getDataProvider + */ + public function testIsSatisfiedBy(bool $expected, string $property): void + { + $this->assertSame($expected, $this->subject->isSatisfiedBy($property)); + } + + public function getDataProvider(): array + { + return [ + 'with allowed properties' => [ + true, + 'anyUriHere' + ], + 'with not allowed properties' => [ + false, + self::PROPERTY_FORBIDDEN + ], + 'with not allowed system properties' => [ + false, + 'http://www.tao.lu/Ontologies/TAOItem.rdf#ItemModel' + ], + ]; + } +}