Skip to content

Commit

Permalink
PHRAS-3931_phraseanet_local_id_in_api (#4400)
Browse files Browse the repository at this point in the history
* add "resource_id" to record / story ; formula defined into InstanceIdAware.php

* fix tests

* fix tests

* switch to main/instance_id
  • Loading branch information
jygaulier authored Oct 30, 2023
1 parent ebe9153 commit 179431d
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 19 deletions.
31 changes: 31 additions & 0 deletions lib/Alchemy/Phrasea/Controller/Api/InstanceIdAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Alchemy\Phrasea\Controller\Api;

use Alchemy\Phrasea\Core\Configuration\PropertyAccess;

trait InstanceIdAware
{
private $instanceId;

/**
* @param PropertyAccess $conf
* @return InstanceIdAware
*/
public function setInstanceId(PropertyAccess $conf)
{
$this->instanceId = $conf->get(
['main', 'instance_id'],
md5($conf->get(['main', 'key'], ''))
);

return $this;
}

public function getResourceIdResolver()
{
return function(\record_adapter $record): string {
return $this->instanceId . '_' . $record->getDataboxId() . '_' . $record->getRecordId();
};
}
}
29 changes: 16 additions & 13 deletions lib/Alchemy/Phrasea/Controller/Api/V1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class V1Controller extends Controller
use DispatcherAware;
use FilesystemAware;
use JsonBodyAware;
use InstanceIdAware;

const OBJECT_TYPE_USER = 'http://api.phraseanet.com/api/objects/user';
const OBJECT_TYPE_STORY = 'http://api.phraseanet.com/api/objects/story';
Expand Down Expand Up @@ -1216,7 +1217,7 @@ public function searchAction(Request $request)
{
$subdefTransformer = new SubdefTransformer($this->app['acl'], $this->getAuthenticatedUser(), new PermalinkTransformer());
$technicalDataTransformer = new TechnicalDataTransformer();
$recordTransformer = new RecordTransformer($subdefTransformer, $technicalDataTransformer);
$recordTransformer = new RecordTransformer($subdefTransformer, $technicalDataTransformer, $this->getResourceIdResolver());
$storyTransformer = new StoryTransformer($subdefTransformer, $recordTransformer);
$compositeTransformer = new V1SearchCompositeResultTransformer($recordTransformer, $storyTransformer);
$searchTransformer = new V1SearchResultTransformer($compositeTransformer);
Expand Down Expand Up @@ -1275,7 +1276,7 @@ public function searchRecordsAction(Request $request)
{
$subdefTransformer = new SubdefTransformer($this->app['acl'], $this->getAuthenticatedUser(), new PermalinkTransformer());
$technicalDataTransformer = new TechnicalDataTransformer();
$recordTransformer = new RecordTransformer($subdefTransformer, $technicalDataTransformer);
$recordTransformer = new RecordTransformer($subdefTransformer, $technicalDataTransformer, $this->getResourceIdResolver());
$searchTransformer = new V1SearchRecordsResultTransformer($recordTransformer);

$transformerResolver = new SearchResultTransformerResolver([
Expand Down Expand Up @@ -1667,6 +1668,7 @@ private function listRecord(Request $request, record_adapter $record)
$data = [
'databox_id' => $record->getDataboxId(),
'record_id' => $record->getRecordId(),
'resource_id' => ($this->getResourceIdResolver())($record),
'mime_type' => $record->getMimeType(),
'title' => $record->get_title(['encode'=> record_adapter::ENCODE_NONE]),
'original_name' => $record->get_original_name(),
Expand Down Expand Up @@ -1721,17 +1723,18 @@ private function listStory(Request $request, record_adapter $story)
};

return [
'@entity@' => self::OBJECT_TYPE_STORY,
'databox_id' => $story->getDataboxId(),
'story_id' => $story->getRecordId(),
'@entity@' => self::OBJECT_TYPE_STORY,
'databox_id' => $story->getDataboxId(),
'story_id' => $story->getRecordId(),
'resource_id' => ($this->getResourceIdResolver())($story),
'cover_record_id' => $story->getCoverRecordId(),
'updated_on' => $story->getUpdated()->format(DATE_ATOM),
'created_on' => $story->getCreated()->format(DATE_ATOM),
'collection_id' => $story->getCollectionId(),
'base_id' => $story->getBaseId(),
'thumbnail' => $this->listEmbeddableMedia($request, $story, $story->get_thumbnail()),
'uuid' => $story->getUuid(),
'metadatas' => [
'updated_on' => $story->getUpdated()->format(DATE_ATOM),
'created_on' => $story->getCreated()->format(DATE_ATOM),
'collection_id' => $story->getCollectionId(),
'base_id' => $story->getBaseId(),
'thumbnail' => $this->listEmbeddableMedia($request, $story, $story->get_thumbnail()),
'uuid' => $story->getUuid(),
'metadatas' => [
'@entity@' => self::OBJECT_TYPE_STORY_METADATA_BAG,
'dc:contributor' => $format($caption, \databox_Field_DCESAbstract::Contributor),
'dc:coverage' => $format($caption, \databox_Field_DCESAbstract::Coverage),
Expand All @@ -1749,7 +1752,7 @@ private function listStory(Request $request, record_adapter $story)
'dc:title' => $format($caption, \databox_Field_DCESAbstract::Title),
'dc:type' => $format($caption, \databox_Field_DCESAbstract::Type),
],
'records' => $this->listRecords($request, array_values($story->getChildren()->get_elements())),
'records' => $this->listRecords($request, array_values($story->getChildren()->get_elements())),
];
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Alchemy/Phrasea/Controller/Api/V3/V3ResultHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use ACL;
use Alchemy\Phrasea\Authentication\Authenticator;
use Alchemy\Phrasea\Controller\Api\InstanceIdAware;
use Alchemy\Phrasea\Core\Configuration\PropertyAccess;
use Alchemy\Phrasea\Media\MediaSubDefinitionUrlGenerator;
use databox_status;
Expand All @@ -17,6 +18,8 @@

class V3ResultHelpers
{
use InstanceIdAware;

/** @var PropertyAccess */
private $conf;

Expand Down Expand Up @@ -151,6 +154,7 @@ public function listRecord(Request $request, record_adapter $record, ACL $aclfor
$data = [
'databox_id' => $record->getDataboxId(),
'record_id' => $record->getRecordId(),
'resource_id' => ($this->getResourceIdResolver())($record),
'mime_type' => $record->getMimeType(),
'title' => $record->get_title(['encode'=> record_adapter::ENCODE_NONE]),
'original_name' => $record->get_original_name(),
Expand Down
4 changes: 3 additions & 1 deletion lib/Alchemy/Phrasea/Controller/Api/V3/V3SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Alchemy\Phrasea\Application\Helper\DispatcherAware;
use Alchemy\Phrasea\Application\Helper\JsonBodyAware;
use Alchemy\Phrasea\Collection\Reference\CollectionReference;
use Alchemy\Phrasea\Controller\Api\InstanceIdAware;
use Alchemy\Phrasea\Controller\Api\Result;
use Alchemy\Phrasea\Controller\Controller;
use Alchemy\Phrasea\Databox\DataboxGroupable;
Expand Down Expand Up @@ -51,6 +52,7 @@ class V3SearchController extends Controller
{
use JsonBodyAware;
use DispatcherAware;
use InstanceIdAware;

/**
* Search for results
Expand All @@ -65,7 +67,7 @@ public function searchAction(Request $request)

$subdefTransformer = new SubdefTransformer($this->app['acl'], $this->getAuthenticatedUser(), new PermalinkTransformer());
$technicalDataTransformer = new TechnicalDataTransformer();
$recordTransformer = new RecordTransformer($subdefTransformer, $technicalDataTransformer);
$recordTransformer = new RecordTransformer($subdefTransformer, $technicalDataTransformer, $this->getResourceIdResolver());
$storyTransformer = new V3StoryTransformer($recordTransformer);
$compositeTransformer = new V3SearchCompositeResultTransformer($recordTransformer, $storyTransformer);
$searchTransformer = new V3SearchResultTransformer($compositeTransformer);
Expand Down
6 changes: 5 additions & 1 deletion lib/Alchemy/Phrasea/ControllerProvider/Api/V1.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function register(Application $app)
->setDataboxLoggerLocator($app['phraseanet.logger'])
->setDispatcher($app['dispatcher'])
->setFileSystemLocator(new LazyLocator($app, 'filesystem'))
->setJsonBodyHelper(new LazyLocator($app, 'json.body_helper'));
->setJsonBodyHelper(new LazyLocator($app, 'json.body_helper'))
->setInstanceId($app['conf'])
;
});
}

Expand Down Expand Up @@ -186,6 +188,7 @@ public function connect(Application $app)
->assert('databox_id', '\d+')
->assert('record_id', '\d+');

/** @uses \Alchemy\Phrasea\Controller\Api\V1Controller::getRecordAction */
$controllers->get('/records/{databox_id}/{record_id}/', 'controller.api.v1:getRecordAction')
->before('controller.api.v1:ensureCanAccessToRecord')
->assert('databox_id', '\d+')
Expand Down Expand Up @@ -238,6 +241,7 @@ public function connect(Application $app)
->assert('record_id', '\d+');
$controllers->get('/stories/{any_id}/{anyother_id}/embed/', 'controller.api.v1:getBadRequestAction');

/** @uses \Alchemy\Phrasea\Controller\Api\V1Controller::getStoryAction */
$controllers->get('/stories/{databox_id}/{record_id}/', 'controller.api.v1:getStoryAction')
->before('controller.api.v1:ensureCanAccessToRecord')
->assert('databox_id', '\d+')
Expand Down
8 changes: 5 additions & 3 deletions lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function register(Application $app)
$app['conf'],
$app['media_accessor.subdef_url_generator'],
$app['authentication'],
$app['url_generator']
));
$app['url_generator']))
->setInstanceId($app['conf']);
});
$app['controller.api.v3.subdefs_service'] = $app->share(function (PhraseaApplication $app) {
return (new V3SubdefsServiceController($app))
Expand All @@ -46,7 +46,9 @@ public function register(Application $app)
;
});
$app['controller.api.v3.search'] = $app->share(function (PhraseaApplication $app) {
return (new V3SearchController($app));
return (new V3SearchController($app))
->setInstanceId($app['conf'])
;
});
$app['controller.api.v3.searchraw'] = $app->share(function (PhraseaApplication $app) {
return (new V3SearchRawController($app));
Expand Down
17 changes: 16 additions & 1 deletion lib/Alchemy/Phrasea/Search/RecordTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ class RecordTransformer extends TransformerAbstract
*/
private $technicalDataTransformer;

public function __construct(SubdefTransformer $subdefTransformer, TechnicalDataTransformer $technicalDataTransformer)
/**
* @var callable
*/
private $resourceIdResolver;

public function __construct(SubdefTransformer $subdefTransformer, TechnicalDataTransformer $technicalDataTransformer, callable $resourceIdResolver)
{
$this->subdefTransformer = $subdefTransformer;
$this->technicalDataTransformer = $technicalDataTransformer;
$this->resourceIdResolver = $resourceIdResolver;
}

public function transform($recordView)
Expand All @@ -55,6 +61,7 @@ public function transform($recordView)
return [
'databox_id' => $record->getDataboxId(),
'record_id' => $record->getRecordId(),
'resource_id' => ($this->resourceIdResolver)($record),
'mime_type' => $record->getMimeType(),
'title' => $record->get_title(['encode'=> record_adapter::ENCODE_NONE]),
'original_name' => $record->get_original_name(),
Expand Down Expand Up @@ -160,4 +167,12 @@ public function includeCaption(RecordView $recordView)
];
});
}

/**
* @return callable
*/
public function getResourceIdResolver(): callable
{
return $this->resourceIdResolver;
}
}
1 change: 1 addition & 0 deletions lib/Alchemy/Phrasea/Search/StoryTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function transform(StoryView $storyView)
'@entity@' => 'http://api.phraseanet.com/api/objects/story',
'databox_id' => $story->getDataboxId(),
'story_id' => $story->getRecordId(),
'resource_id' => ($this->recordTransformer->getResourceIdResolver())($story),
'cover_record_id' => $story->getCoverRecordId(),
'updated_on' => NullableDateTime::format($story->getUpdated()),
'created_on' => NullableDateTime::format($story->getUpdated()),
Expand Down

0 comments on commit 179431d

Please sign in to comment.