From a635710117bda5602ce51b3aa26f1ae675433608 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Mon, 20 Jan 2025 13:45:33 +0100 Subject: [PATCH] fix: Metadata field search Signed-off-by: Louis Chemineau --- lib/private/Files/Cache/SearchBuilder.php | 17 +++++++++++------ tests/lib/Files/Cache/SearchBuilderTest.php | 9 +++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/private/Files/Cache/SearchBuilder.php b/lib/private/Files/Cache/SearchBuilder.php index e89b95b756ff8..6a0cba7f1f2e1 100644 --- a/lib/private/Files/Cache/SearchBuilder.php +++ b/lib/private/Files/Cache/SearchBuilder.php @@ -12,6 +12,7 @@ use OCP\Files\Search\ISearchComparison; use OCP\Files\Search\ISearchOperator; use OCP\Files\Search\ISearchOrder; +use OCP\FilesMetadata\IFilesMetadataManager; use OCP\FilesMetadata\IMetadataQuery; /** @@ -80,13 +81,10 @@ class SearchBuilder { public const TAG_FAVORITE = '_$!!$_'; - /** @var IMimeTypeLoader */ - private $mimetypeLoader; - public function __construct( - IMimeTypeLoader $mimetypeLoader, + private IMimeTypeLoader $mimetypeLoader, + private IFilesMetadataManager $filesMetadataManager, ) { - $this->mimetypeLoader = $mimetypeLoader; } /** @@ -285,11 +283,18 @@ private function validateComparison(ISearchComparison $operator) { private function getExtraOperatorField(ISearchComparison $operator, IMetadataQuery $metadataQuery): array { - $paramType = self::$fieldTypes[$operator->getField()]; $field = $operator->getField(); $value = $operator->getValue(); $type = $operator->getType(); + $knownMetadata = $this->filesMetadataManager->getKnownMetadata(); + $isIndex = $knownMetadata->isIndex($field); + $paramType = $knownMetadata->getType($field) === 'int' ? 'integer' : 'string'; + + if (!$isIndex) { + throw new \InvalidArgumentException('Cannot search non indexed metadata key'); + } + switch ($operator->getExtra()) { case IMetadataQuery::EXTRA: $metadataQuery->joinIndex($field); // join index table if not joined yet diff --git a/tests/lib/Files/Cache/SearchBuilderTest.php b/tests/lib/Files/Cache/SearchBuilderTest.php index acb2e109144e5..17285d4420921 100644 --- a/tests/lib/Files/Cache/SearchBuilderTest.php +++ b/tests/lib/Files/Cache/SearchBuilderTest.php @@ -15,6 +15,7 @@ use OCP\Files\Search\ISearchBinaryOperator; use OCP\Files\Search\ISearchComparison; use OCP\Files\Search\ISearchOperator; +use OCP\FilesMetadata\IFilesMetadataManager; use Test\TestCase; /** @@ -24,9 +25,12 @@ class SearchBuilderTest extends TestCase { /** @var IQueryBuilder */ private $builder; - /** @var IMimeTypeLoader|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IMimeTypeLoader&\PHPUnit\Framework\MockObject\MockObject */ private $mimetypeLoader; + /** @var IFilesMetadataManager&\PHPUnit\Framework\MockObject\MockObject */ + private $filesMetadataManager; + /** @var SearchBuilder */ private $searchBuilder; @@ -37,6 +41,7 @@ protected function setUp(): void { parent::setUp(); $this->builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $this->mimetypeLoader = $this->createMock(IMimeTypeLoader::class); + $this->filesMetadataManager = $this->createMock(IFilesMetadataManager::class); $this->mimetypeLoader->expects($this->any()) ->method('getId') @@ -60,7 +65,7 @@ protected function setUp(): void { [6, 'image'] ]); - $this->searchBuilder = new SearchBuilder($this->mimetypeLoader); + $this->searchBuilder = new SearchBuilder($this->mimetypeLoader, $this->filesMetadataManager); $this->numericStorageId = 10000; $this->builder->select(['fileid'])