Skip to content

Commit

Permalink
Handle redirected Wikidata items
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Aug 30, 2021
1 parent 4ae5c14 commit c58a69a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ we will endevour to fix up the manual to make it clearer about that topic.

install
config
tags
36 changes: 36 additions & 0 deletions docs/tags.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.. _tags:

Tags
====

Tags are the primary way of grouping Posts together by topic; each Post can have any number of Tags.
Each Tag has its own page (with a URL of the form ``/Txx``, where ``xx`` is the Tag's ID)
and on that page it has
a title (which must be unique),
and optional description (which uses the same syntax as Post bodies).

Wikidata
--------

Tags can also be linked to Wikidata_ items.
This is the way to show that a Tag's topic is *the same as* the concept represented by the Wikidata item.
For example, a Tag with a title of "Douglas Adams" would have the Wikidata item ID of "Q42_".
Only one Tag can be linked to each Wikidata item.
Adding a Wikidata link to a Tag adds a metadata table to the Tag's page,
showing all the relevant metadata from the Wikidata item.
If a row in the metadata table refers to another Tag that is also linked to Wikidata,
then the value displayed will be a link to that Tag.

Wikidata items can be merged_ (when multiple are accidentally created for the same concept),
but this doesn't affect the linkage from Twyne.
The only change will be that the new ID for the item will be displayed in the metadata table;
no data in the database is changed, because the old ID will remain on Wikidata as a permanent alias for the new one.

The metadata from Wikidata is fetched up every time someone views a Tag's page, and is cached for two weeks.
The cache can be cleared (forcing it to fetch the latest data) by running the following CLI command::

./bin/console cache:pool:clear cache.app

.. _Wikidata: https://www.wikidata.org/
.. _Q42: https://www.wikidata.org/wiki/Q42
.. _merged: https://www.wikidata.org/wiki/Help:Merge
19 changes: 12 additions & 7 deletions src/Repository/WikidataRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,23 @@ public function search(string $q)
}

/**
* @param $id
* @return array With keys 'label', 'description', 'properties' and maybe 'authorities'.
* @param string $id
* @return array With keys 'id', 'label', 'description', 'properties' and maybe 'authorities'.
* The ID will differ from $id if the entity has been redirected.
*/
public function getData($id): array
public function getData(string $id): array
{
$entity = $this->getEntities([$id])[$id];
if (!isset($entity['labels'])) {
return [
'id' => '',
'label' => '',
'description' => '',
'properties' => [],
];
}
$out = [
'id' => $entity['id'],
'label' => $entity['labels']['en']['value'] ?? '(No label)',
'description' => $entity['descriptions']['en']['value'] ?? '(No description)',
'properties' => [],
Expand Down Expand Up @@ -177,11 +180,13 @@ public function getEntities(array $ids)
throw new Exception('Entities not found in response.');
}
foreach ($responseData['entities'] as $entity) {
// Handle redirected IDs, storing them under the original ID rather than the actual one.
$entityId = $entity['redirects']['from'] ?? $entity['id'];
// Cache each entity for two weeks.
$cacheItems[$entity['id']]->set($entity);
$cacheItems[$entity['id']]->expiresAfter(new DateInterval('P14D'));
$this->cache->save($cacheItems[$entity['id']]);
$out[$entity['id']] = $entity;
$cacheItems[$entityId]->set($entity);
$cacheItems[$entityId]->expiresAfter(new DateInterval('P14D'));
$this->cache->save($cacheItems[$entityId]);
$out[$entityId] = $entity;
}
}
$this->cache->commit();
Expand Down
2 changes: 1 addition & 1 deletion templates/tag/view.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<caption>
<img src="{{ asset('build/images/wikidata-logo.png') }}" alt="Wikidata logo." />
Wikidata item
<a href="https://www.wikidata.org/wiki/{{ tag.wikidata }}">{{ tag.wikidata }}</a>:
<a href="https://www.wikidata.org/wiki/{{ entity.id }}">{{ entity.id }}</a>:
<strong>{{ entity.label }}</strong>
{% if entity.description %}
&mdash; <dfn>{{ entity.description }}</dfn>
Expand Down
19 changes: 19 additions & 0 deletions tests/Repository/WikidataRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Test\Repository;

use App\Repository\WikidataRepository;
use App\Tests\Repository\RepositoryTestBase;

class WikidataRepositoryTest extends RepositoryTestBase
{

public function testGetData()
{
/** @var WikidataRepository $wikidataRepo */
$wikidataRepo = self::$container->get(WikidataRepository::class);
// Redirected item.
$data = $wikidataRepo->getData('Q73007800');
$this->assertSame('Q5501480', $data['id']);
}
}

0 comments on commit c58a69a

Please sign in to comment.