2024-04-24 20:53:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\BundleUpdaters\Updaters;
|
|
|
|
|
|
|
|
use App\Classes\Bundle;
|
|
|
|
use App\Exceptions\UnableToFindWikidataEntityId;
|
|
|
|
use App\Services\Wikidata\WikidataClient;
|
|
|
|
use App\Services\Wikidata\WikidataExtractor;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
use function Laravel\Prompts\select;
|
|
|
|
use function Laravel\Prompts\text;
|
|
|
|
|
|
|
|
class CriticUpdater extends BaseUpdater
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Return a boolean value indicating if the updater can actually make the
|
|
|
|
* update using known data.
|
|
|
|
*/
|
|
|
|
public function canUpdateBundle(): bool
|
|
|
|
{
|
|
|
|
return !empty($this->bundle->metadata()->get('title'))
|
|
|
|
&& !empty($this->bundle->metadata()->get('wikidataEntityId'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array describing what kind of data the updater needs in
|
|
|
|
* addition to the one it already has
|
|
|
|
*/
|
|
|
|
public function formSpecs(): ?array
|
|
|
|
{
|
|
|
|
$specs = [];
|
|
|
|
|
|
|
|
if (empty($this->bundle->metadata()->get('title'))) {
|
|
|
|
$specs['title'] = fn () => text('Work title', '', '', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->bundle->metadata()->get('title')) && empty($this->bundle->metadata()->get('wikidataEntityId'))) {
|
|
|
|
$options = app()->make(WikidataClient::class)->searchEntityId($this->bundle->metadata()->get('title'));
|
|
|
|
|
|
|
|
if (!empty($options)) {
|
|
|
|
$specs['wikidataEntityId'] = fn () => select('Confirm searched work', $options);
|
|
|
|
} else {
|
|
|
|
throw new UnableToFindWikidataEntityId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $specs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates bundle's extended metadata
|
|
|
|
*/
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
$lastUpdate = Carbon::parse($this->bundle->metadata('wikidata/entity')->get('modified', '1970-01-01'));
|
|
|
|
$entityId = $this->bundle->metadata()->get('wikidataEntityId');
|
|
|
|
$wikidata = app()->make(WikidataClient::class);
|
|
|
|
$extractor = app()->make(WikidataExtractor::class);
|
|
|
|
|
|
|
|
if ($lastUpdate->lt(now()->subMonth())) {
|
|
|
|
$completeEntity = $wikidata->getEntityData($entityId, true)['entities'][$entityId];
|
|
|
|
} else {
|
|
|
|
$completeEntity = $this->bundle->metadata('wikidata/entity')->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
$extractor->extract($completeEntity, $entityId);
|
|
|
|
|
2024-04-24 23:28:55 +02:00
|
|
|
$this->bundle->metadata('wikidata/included')->setMany($extractor->included(), true);
|
|
|
|
$this->bundle->metadata('wikidata/excluded')->setMany($extractor->excluded(), true);
|
|
|
|
$this->bundle->metadata('wikidata/unused')->setMany($extractor->unused(), true);
|
|
|
|
$this->bundle->metadata('wikidata/entity')->setMany($extractor->everythingElse(), true);
|
2024-04-24 20:53:37 +02:00
|
|
|
|
|
|
|
$frenchTitle = $this->bundle->metadata('wikidata/entity')->get('labels.fr.value', null);
|
|
|
|
$originalTitle = $this->bundle->metadata('wikidata/entity')->get('labels.en.value', null);
|
|
|
|
|
|
|
|
if (!empty($originalTitle)) {
|
|
|
|
$this->bundle->metadata()->set('title', $originalTitle);
|
|
|
|
|
|
|
|
if (!empty($frenchTitle) && $frenchTitle !== $originalTitle) {
|
|
|
|
$this->bundle->metadata()->set('subTitle', $frenchTitle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-25 17:11:12 +02:00
|
|
|
$this->bundle->metadata()->set('virtualMetadata', ['wikidata/included']);
|
|
|
|
|
2024-04-24 20:53:37 +02:00
|
|
|
$this->bundle->save();
|
2024-04-25 17:11:12 +02:00
|
|
|
$this->bundle->repair();
|
2024-04-24 20:53:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a boolean value indicating if this updater in particular can
|
|
|
|
* update specified bundle
|
|
|
|
*/
|
|
|
|
public static function handles(Bundle $bundle): bool
|
|
|
|
{
|
|
|
|
$parts = preg_split('#/#', $bundle->getPath(), -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
|
|
|
|
if ($parts[0] !== 'critiques' || count($parts) !== 3) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|