diff --git a/composer.json b/composer.json index 0866c6d..efa2075 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "require": { "php": "^8.1", "psr/log": "*", - "acdh-oeaw/arche-diss-cache": "^0.7", + "acdh-oeaw/arche-diss-cache": "^0.8", "zozlak/logging": "^1.0" }, "autoload": { diff --git a/src/acdhOeaw/arche/iiifManifest/Resource.php b/src/acdhOeaw/arche/iiifManifest/Resource.php index 9e91be5..2fa64c0 100644 --- a/src/acdhOeaw/arche/iiifManifest/Resource.php +++ b/src/acdhOeaw/arche/iiifManifest/Resource.php @@ -139,9 +139,9 @@ private function findFirstResource(): array { $this->log?->info("resolved: $resolvedRes collection: $collectionRes first: $firstRes"); // for better caching - //$node = $this->meta->getNode(); - //$graph->add(DF::quad($node, $this->schema->id, $firstRes)); - //$graph->add(DF::quad($node, $this->schema->id, $collectionRes)); + $node = $this->meta->getNode(); + $graph->add(DF::quad($node, $this->schema->id, $firstRes)); + $graph->add(DF::quad($node, $this->schema->id, $collectionRes)); return [$firstRes, $collectionRes]; } @@ -179,6 +179,7 @@ private function getManifest(TermInterface $firstRes, $graph = $this->meta->getDataset(); // first check if a collection doesn't have a custom manifest + /** @phpstan-ignore property.notFound */ $customManifest = $graph->getObjectValue(new QT($collectionRes, $this->schema->iiifManifest)); if (!empty($customManifest) && $customManifest !== $this->config->defaultIiifManifestUri) { $data = @file_get_contents($customManifest); @@ -271,7 +272,7 @@ private function getNextSbj(DatasetInterface $data, PT $collectionTmpl): TermInt if ($graph->any($collectionTmpl->withSubject($i))) { $sbj = $i; // for better caching - //$graph->add(DF::quad($node, $this->schema->id, $i)); + $graph->add(DF::quad($node, $this->schema->id, $i)); } } return $sbj; diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index 38f5d9f..3a1046c 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -28,8 +28,14 @@ use quickRdf\DatasetNode; use quickRdf\DataFactory as DF; +use termTemplates\PredicateTemplate as PT; use quickRdfIo\Util as RdfIoUtil; +use acdhOeaw\arche\lib\dissCache\CachePdo; +use acdhOeaw\arche\lib\dissCache\CacheItem; +use acdhOeaw\arche\lib\dissCache\ResponseCache; +use acdhOeaw\arche\lib\dissCache\RepoWrapperInterface; use acdhOeaw\arche\lib\Schema; +use acdhOeaw\arche\lib\SearchConfig; use acdhOeaw\arche\lib\RepoResourceInterface; use acdhOeaw\arche\lib\dissCache\ResponseCacheItem; use acdhOeaw\arche\iiifManifest\Resource as IiifResource; @@ -52,6 +58,14 @@ static public function setUpBeforeClass(): void { self::$schema = new Schema(self::$cfg->iiifManifest->schema); } + public function setUp(): void { + parent::setUp(); + + foreach (glob('/tmp/cachePdo*') as $i) { + unlink($i); + } + } + public function testModeImage(): void { $headers = ['Location' => 'https://loris.acdh.oeaw.ac.at/13/info.json']; $expected = new ResponseCacheItem("Redirect to https://loris.acdh.oeaw.ac.at/13/info.json", 302, $headers, false); @@ -97,12 +111,41 @@ public function testModeManifest(): void { public function testHasIiifManifest(): void { // not a iiif-manifest file but we currenlty have none in ARCHE - $expected = json_decode(file_get_contents(__DIR__.'/data_hasIiifManifest.json'), true); + $expected = json_decode(file_get_contents(__DIR__ . '/data_hasIiifManifest.json'), true); $this->checkOutput($expected, $this->getOutput(self::COLLECTION_URL, IiifResource::MODE_MANIFEST, __DIR__ . '/meta_hasIiifManifest.ttl')); } - private function checkOutput(array $expected, - ResponseCacheItem $actual): void { + public function testCacheWholeCollection(): void { + $cfg = self::$cfg->dissCacheService; + $sc = new SearchConfig(); + $sc->metadataMode = $cfg->metadataMode; + $sc->metadataParentProperty = $cfg->parentProperty; + $sc->resourceProperties = $cfg->resourceProperties; + $sc->relativesProperties = $cfg->relativesProperties; + + $graph = new DatasetNode(DF::namedNode(self::RESOURCE_URL)); + $graph->add(RdfIoUtil::parse(__DIR__ . '/meta.ttl', new DF(), 'text/turtle')); + $repoRes = $this->createStub(RepoResourceInterface::class); + $repoRes->method('getGraph')->willReturn($graph); + $repoRes->method('getIds')->willReturnCallback(fn() => $graph->listObjects(new PT(self::$schema->id))->getValues()); + $repo = $this->createStub(RepoWrapperInterface::class); + $repo->method('getResourceById')->willReturn($repoRes); + $repo->method('getModificationTimestamp')->willReturn(PHP_INT_MAX); + + $db = new CachePdo('sqlite:/tmp/cachePdo_db.sqlite', 'iiifManifest'); + $clbck = fn($res, $param) => IiifResource::cacheHandler($res, $param, self::$cfg->iiifManifest); + $cache = new ResponseCache($db, $clbck, 0, 0, [$repo], $sc); + + $cache->getResponse(['images'], self::RESOURCE_URL); + $resDbItem = $db->get(self::RESOURCE_URL); + $collDbItem = $db->get(self::COLLECTION_URL); + $res2DbItem = $db->get('https://arche.acdh.oeaw.ac.at/api/11'); + $this->assertInstanceOf(CacheItem::class, $resDbItem); + $this->assertEquals($collDbItem, $collDbItem); + $this->assertEquals($collDbItem, $res2DbItem); + } + + private function checkOutput(array $expected, ResponseCacheItem $actual): void { $this->assertEquals(200, $actual->responseCode); $this->assertEquals(['Content-Type' => 'application/json'], $actual->headers); $this->assertFalse($actual->hit);