Skip to content

Commit

Permalink
If metadata write mode is "merge", apply the "merge" rule for title o…
Browse files Browse the repository at this point in the history
…nly on the language level (resolves #19)
  • Loading branch information
zozlak committed Nov 25, 2021
1 parent d3bb96f commit 9485327
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/acdhOeaw/arche/doorkeeper/Doorkeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use acdhOeaw\UriNormalizer;
use acdhOeaw\epicHandle\HandleService;
use acdhOeaw\arche\core\Transaction;
use acdhOeaw\arche\core\Metadata;
use acdhOeaw\arche\core\Resource as Res;
use acdhOeaw\arche\core\RestController as RC;
use acdhOeaw\arche\lib\schema\Ontology;
Expand Down Expand Up @@ -620,6 +621,26 @@ static private function checkTitleProp(Resource $meta): void {
}
$langs[$lang] = (string) $i;
}

// preserve old titles when needed
$mode = RC::getRequestParameter('metadataWriteMode');
if ($mode === Metadata::SAVE_MERGE) {
$query = RC::$pdo->prepare("
SELECT value, lang
FROM metadata
WHERE property = ? AND id = ?
");
$id = preg_replace('|^.*/|', '', $meta->getUri());
$query->execute([$titleProp, $id]);
while ($i = $query->fetchObject()) {
if (!isset($langs[$i->lang])) {
$titles[] = '';
$meta->addLiteral($titleProp, $i->value, $i->lang);
}
}
}

// if everything's fine, just return
if (count($titles) > 0) {
return;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,38 @@ public function testTitleAuto(): void {
$this->assertContains('bar', $titles);
}

public function testTitlePreserveOtherLang(): void {
$titleProp = self::$config->schema->label;
self::$repo->begin();

$meta = self::createMetadata([$titleProp => new Literal('foo', 'en')]);
$res = self::$repo->createResource($meta);
$meta->delete($titleProp);
$meta->addLiteral($titleProp, 'bar', 'de');

$res->setMetadata($meta);
$res->updateMetadata(RepoResource::UPDATE_MERGE);
$tmp = $res->getGraph()->allLiterals($titleProp);
$titles = [];
foreach ($tmp as $i) {
$titles[$i->getLang()] = (string) $i;
}
$this->assertEquals(2, count($titles));
$this->assertArrayHasKey('en', $titles);
$this->assertArrayHasKey('de', $titles);
$this->assertEquals('foo', $titles['en']);
$this->assertEquals('bar', $titles['de']);

$res->setMetadata($meta);
$res->updateMetadata(RepoResource::UPDATE_OVERWRITE);
$titles = $res->getGraph()->allLiterals($titleProp);
$this->assertEquals(1, count($titles));
$this->assertEquals('bar', (string) $titles[0]);
$this->assertEquals('de', $titles[0]->getLang());

self::$repo->rollback();
}

/**
*
* @depends testTitleAuto
Expand Down

0 comments on commit 9485327

Please sign in to comment.