Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redo reference system with map and plugin architecture #3774

Draft
wants to merge 31 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2ac200f
Initial work
dafeder Mar 26, 2022
56afa7c
Whole bunch of work
dafeder Mar 27, 2022
7cdedd5
Some documentation cleanup
dafeder Mar 27, 2022
47ec1b0
Initial services refactor
dafeder Apr 5, 2022
3091c42
Update distribution in Lifecycle
dafeder Apr 5, 2022
b804d9e
More fixes
dafeder Apr 7, 2022
920626e
Merge remote-tracking branch 'origin/2.x' into dict-refs
dafeder Apr 7, 2022
ff41258
Clean up LifeCycle
dafeder Apr 9, 2022
9ec672b
Fix newRevision for distirbution/resources
dafeder Apr 9, 2022
c15ef2c
Merge branch '2.x' into dict-refs
dafeder Mar 15, 2023
0695e06
Some code cleanup
dafeder Mar 16, 2023
10b114a
ResourceMapper::newRevision no longer static
dafeder Mar 17, 2023
5863a40
Resource mapper improvement
dafeder Mar 17, 2023
f4e59e7
Finish ItemReference w/tests
dafeder Mar 17, 2023
94126d0
Merge remote-tracking branch 'origin/2.x' into dict-refs
dafeder Mar 17, 2023
c3ba57c
Make dispatchEvent protected for mocking
dafeder Mar 21, 2023
c2c4948
Change dataResource to use Drupal datetime
dafeder Mar 24, 2023
929b6d9
Resource reference tests
dafeder Mar 24, 2023
bf279c3
TSV format test
dafeder Mar 24, 2023
0a0262e
Tweak
dafeder Mar 24, 2023
a88f4e4
Finish resource reference tests
dafeder Mar 25, 2023
2ecdedf
Referencer/Dereferener tests
dafeder Mar 29, 2023
74660b5
Fix DkanUnitTest suite
dafeder Mar 29, 2023
40baada
Fix mockstorage
dafeder Mar 29, 2023
a0daaec
Fix dereference logic
dafeder Mar 29, 2023
d477ec0
Return null if dereference finds no value
dafeder Mar 30, 2023
cb542a0
Fix referencer test
dafeder Mar 30, 2023
90af16e
Merge remote-tracking branch 'origin/2.x' into dict-refs
dafeder Apr 11, 2023
29dab09
Change text
dafeder Apr 14, 2023
650cd29
200
dafeder Apr 14, 2023
d612ef9
Remove use
dafeder Apr 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions modules/common/src/DataResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function __construct($file_path, $mimeType, $perspective = self::DEFAULT_
$this->mimeType = $mimeType;
$this->perspective = $perspective;
// @todo Create a timestamp property and generate uuid for version.
$this->version = time();
$this->version = $this->getCurrentTime();
$this->checksum = NULL;
}

Expand All @@ -102,14 +102,32 @@ public function __construct($file_path, $mimeType, $perspective = self::DEFAULT_
* system to create new versions of resources when they deem it necessary.
*/
public function createNewVersion() {
$newVersion = time();
$newVersion = $this->getCurrentTime();
if ($newVersion == $this->version) {
$newVersion++;
}

return $this->createCommon('version', $newVersion);
}

/**
* Use Drupal datetime service if container available,to make this mockable.
*
* @return int
* Current timestamp.
*
* @todo Remove try/catch?
*/
protected function getCurrentTime(): int {
try {
return (int) \Drupal::service('datetime.time')->getCurrentTime();
}
catch (\Throwable $e) {
// Fall back to php time().
return time();
}
}

/**
* Create a new perspective.
*
Expand Down
2 changes: 1 addition & 1 deletion modules/common/src/EventDispatcherTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ trait EventDispatcherTrait {
* @throws \Exception
* If any of the subscribers registered and Exception it is thrown.
*/
private function dispatchEvent($eventName, $data, $validator = NULL) {
protected function dispatchEvent($eventName, $data, $validator = NULL) {
if ($this->useLegacyDispatcher()) {
$data = $this->legacyDispatchEvent($eventName, $data, $validator);
return $data;
Expand Down
2 changes: 1 addition & 1 deletion modules/common/src/Plugin/DkanApiDocs/CommonApiDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @DkanApiDocs(
* id = "common_dkan_api_docs",
* description = "Base API docs plugin."
* description = @Translation("Base API docs plugin.")
* )
*/
class CommonApiDocs extends DkanApiDocsBase {
Expand Down
3 changes: 0 additions & 3 deletions modules/common/src/Plugin/DkanApiDocsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

/**
* Base class for API Docs plugins.
*
* @see \Drupal\plugin_type_example\Annotation\Sandwich
* @see \Drupal\plugin_type_example\SandwichInterface
*/
abstract class DkanApiDocsBase extends PluginBase implements DkanApiDocsInterface {

Expand Down
4 changes: 2 additions & 2 deletions modules/datastore/src/Service/ResourceLocalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use Contracts\FactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\metastore\Exception\AlreadyRegistered;
use Drupal\metastore\Reference\Referencer;
use Drupal\metastore\ResourceMapper;
use FileFetcher\FileFetcher;
use Procrastinator\Result;
use Drupal\common\EventDispatcherTrait;
use Drupal\metastore\Plugin\MetastoreReferenceType\ResourceReference;

/**
* Resource localizer.
Expand Down Expand Up @@ -120,7 +120,7 @@ private function registerNewPerspectives(DataResource $resource, FileFetcher $fi
$dir = "file://" . $this->drupalFiles->getPublicFilesDirectory();
$localFileDrupalUri = str_replace($dir, "public://", $localFilePath);
$localUrl = $this->drupalFiles->fileCreateUrl($localFileDrupalUri);
$localUrl = Referencer::hostify($localUrl);
$localUrl = ResourceReference::hostify($localUrl);

$new = $resource->createNewPerspective(self::LOCAL_FILE_PERSPECTIVE, $localFilePath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function retrieveContains(string $string, bool $caseSensitive): array {
return [];
}

public function retrieveByHash($hash, $schemaId) {
public function retrieveByHash($hash) {
return [];
}

Expand Down
2 changes: 2 additions & 0 deletions modules/metastore/metastore.module
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ function resource_mapper_display() {

/**
* Helper method to retrieve the static value for a resource's revisioning.
*
* We use a static variable because it's set in an event subscriber.
*/
function resource_mapper_new_revision() {
return drupal_static('metastore_resource_mapper_new_revision', 0);
Expand Down
21 changes: 21 additions & 0 deletions modules/metastore/metastore.schemas.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
catalog:
schema_path: schema/catalog.json
references:
- { property: dataset, schema: dataset }

dataset:
schema_path: schema/dataset.json
ui_schema_path: schema/dataset.ui.json
identifier: { property: identifier, type: uuid }
references:
- { property: distribution, schema: distribution }
- { property: publisher, schema: organization }
- { property: keyword, schema: keyword }
- { property: theme, schema: theme }

distribution:
schema_path: schema/distribution.json
references:
- { property: downloadURL, type: resource }
- { property: describedBy, schema: data-dictionary, type: id }
class: distribution
18 changes: 11 additions & 7 deletions modules/metastore/metastore.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ services:
dkan.metastore.referencer:
class: \Drupal\metastore\Reference\Referencer
arguments:
- '@config.factory'
- '@dkan.metastore.storage'
calls:
- [setLoggerFactory, ['@logger.factory']]
- '@dkan.metastore.reference_map'

dkan.metastore.dereferencer:
class: \Drupal\metastore\Reference\Dereferencer
arguments:
- '@dkan.metastore.reference_map'

dkan.metastore.reference_map:
class: \Drupal\metastore\Reference\ReferenceMap
arguments:
- '@plugin.manager.dkan_reference_type'
- '@config.factory'
- '@dkan.metastore.storage'
calls:
- [setLoggerFactory, ['@logger.factory']]

dkan.metastore.orphan_checker:
class: \Drupal\metastore\Reference\OrphanChecker
Expand Down Expand Up @@ -109,3 +109,7 @@ services:
class: \Drupal\metastore\DataDictionary\DataDictionaryDiscovery
arguments:
- '@config.factory'

plugin.manager.dkan_reference_type:
class: \Drupal\metastore\Reference\ReferenceTypeManager
parent: default_plugin_manager
33 changes: 33 additions & 0 deletions modules/metastore/src/Annotation/MetastoreReferenceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Drupal\metastore\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
* Defines a DKAN ReferenceDefinition annotation object.
*
* @see \Drupal\metastore\Plugin\ReferenceDefinitionManager
* @see plugin_api
*
* @Annotation
*/
class MetastoreReferenceType extends Plugin {

/**
* The plugin ID.
*
* @var string
*/
public $id;

/**
* A brief, human readable, description of the API docs.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $description;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\common\Events\Event;
use Drupal\common\DataResource;
use Drupal\Core\Logger\LoggerChannelFactory;
use Drupal\metastore\Plugin\MetastoreReferenceType\ResourceReference;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\metastore\Plugin\QueueWorker\OrphanReferenceProcessor;
use Drupal\metastore\Service;
Expand Down Expand Up @@ -113,7 +114,7 @@ private function resourceInUseElsewhere(string $dist_id, string $file_path): boo
// Iterate over the metadata for all dataset distributions.
foreach ($this->service->getAll('distribution') as $metadata) {
// Attempt to determine the filepath for this distribution's resource.
$dist_file_path = Referencer::hostify($metadata->{'$.data.downloadURL'} ?? '');
$dist_file_path = ResourceReference::hostify($metadata->{'$.data.downloadURL'} ?? '');
// If the current distribution does is not the excluded distribution, and
// it's resource file path matches the supplied file path...
if ($metadata->{'$.identifier'} !== $dist_id && !empty($dist_file_path) && $dist_file_path === $file_path) {
Expand Down
76 changes: 4 additions & 72 deletions modules/metastore/src/LifeCycle/LifeCycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Drupal\metastore\LifeCycle;

use Drupal\common\EventDispatcherTrait;
use Drupal\common\DataResource;
use Drupal\common\UrlHostTokenResolver;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Queue\QueueFactory;
use Drupal\metastore\MetastoreItemInterface;
Expand Down Expand Up @@ -132,7 +130,7 @@ protected function datasetLoad(MetastoreItemInterface $data) {
$metadata = $data->getMetaData();

// Dereference dataset properties.
$metadata = $this->dereferencer->dereference($metadata);
$metadata = $this->dereferencer->dereference($metadata, "dataset");
$metadata = $this->addDatasetModifiedDate($metadata, $data->getModifiedDate());

$data->setMetadata($metadata);
Expand All @@ -157,26 +155,7 @@ protected function distributionLoad(MetastoreItemInterface $data) {
return;
}

$downloadUrl = $metadata->data->downloadURL;

if (isset($downloadUrl) && !filter_var($downloadUrl, FILTER_VALIDATE_URL)) {
$resourceIdentifier = $downloadUrl;
$ref = NULL;
$original = NULL;
[$ref, $original] = $this->retrieveDownloadUrlFromResourceMapper($resourceIdentifier);

$downloadUrl = isset($original) ? $original : "";

$refProperty = "%Ref:downloadURL";
$metadata->data->{$refProperty} = count($ref) == 0 ? NULL : $ref;
}

if (is_string($downloadUrl)) {
$downloadUrl = UrlHostTokenResolver::resolve($downloadUrl);
}

$metadata->data->downloadURL = $downloadUrl;

$metadata->data = $this->dereferencer->dereference($metadata->data, "distribution");
$data->setMetadata($metadata);
}

Expand Down Expand Up @@ -204,54 +183,6 @@ protected function distributionPredelete(MetastoreItemInterface $data) {
}
}

/**
* Get a download URL.
*
* @param string $resourceIdentifier
* Identifier for resource.
*
* @return array
* Array of reference and original.
*/
private function retrieveDownloadUrlFromResourceMapper(string $resourceIdentifier) {
$reference = [];
$original = NULL;

$info = DataResource::parseUniqueIdentifier($resourceIdentifier);

// Load resource object.
$sourceResource = $this->resourceMapper->get($info['identifier'], DataResource::DEFAULT_SOURCE_PERSPECTIVE, $info['version']);

if (!$sourceResource) {
return [$reference, $original];
}

$reference[] = $this->createResourceReference($sourceResource);
$perspective = \resource_mapper_display();
$resource = $sourceResource;

if (
$perspective != DataResource::DEFAULT_SOURCE_PERSPECTIVE &&
$new = $this->resourceMapper->get($info['identifier'], $perspective, $info['version'])
) {
$resource = $new;
$reference[] = $this->createResourceReference($resource);
}
$original = $resource->getFilePath();

return [$reference, $original];
}

/**
* Private.
*/
private function createResourceReference(DataResource $resource): object {
return (object) [
"identifier" => $resource->getUniqueIdentifier(),
"data" => $resource,
];
}

/**
* Dataset pre-save life cycle method.
*
Expand Down Expand Up @@ -287,7 +218,7 @@ protected function referenceMetadata(MetastoreItemInterface $data): void {
return $data instanceof MetastoreItemInterface;
});

$metadata = $this->referencer->reference($metadata);
$metadata = $this->referencer->reference($metadata, "dataset");

$data->setMetadata($metadata);

Expand All @@ -313,6 +244,7 @@ protected function datadictionaryPresave(MetastoreItemInterface $data): void {
*/
protected function distributionPresave(MetastoreItemInterface $data) {
$metadata = $data->getMetaData();
$metadata->data = $this->referencer->reference($metadata->data, "distribution");
$data->setMetadata($metadata);
}

Expand Down
Loading