1
0
cms11/app/Services/BundleUpdaters/Updaters/CriticUpdater.php

103 lines
3.4 KiB
PHP

<?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);
$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);
$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);
}
}
$this->bundle->save();
}
/**
* 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;
}
}