Skip to content

feat (media-library): emscli media library update file links#1572

Open
YanisGroffier wants to merge 27 commits intoems-project:7.xfrom
YanisGroffier:feat/emscli-media-library-update-file-links
Open

feat (media-library): emscli media library update file links#1572
YanisGroffier wants to merge 27 commits intoems-project:7.xfrom
YanisGroffier:feat/emscli-media-library-update-file-links

Conversation

@YanisGroffier
Copy link

Q A
Bug fix? n
New feature? y
BC breaks? n
Deprecations? n
Fixed tickets? n
Documentation? n

Command to convert ems file links (in wysiwyg fields) into ems object links (if the file already exists in a media library)

@theus77 theus77 changed the title Feat/emscli media library update file links feat (media-library): emscli media library update file links Feb 6, 2026
Comment on lines +14 to +15
use RectorPrefix202601\Symfony\Component\Console\Input\InputArgument;
use RectorPrefix202601\Symfony\Component\Console\Input\InputOption;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the right classes

Comment on lines +59 to +76
$this->options = new MediaLibrarySyncOptions(
//TODO If I have to put all args in this method, do I have to declare them before (in the object attributes) or can I simply put a bunch of nulls and false?
folder: $this->getArgumentString(self::ARGUMENT_FOLDER),
contentType: $this->getOptionString(self::OPTION_CONTENT_TYPE),
folderField: $this->getOptionString(self::OPTION_FOLDER_FIELD),
pathField: null,
fileField: null,
metaDataFile: null,
locateRowExpression: null,
targetFolder: null,
dryRun: false,
onlyMissingFile: false,
onlyMetadataFile: false,
hashFolder: false,
hashMetaDataFile: false,
);

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you want to have a MediaLibrarySyncOptions? With all those options? I would recommand to first starts with simple class's members

foreach ($coreApi->search()->scroll($search) as $hit)
{

foreach($this->fields as $field) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not hesitate to create functions in order to avoid too much nested loops

Comment on lines +82 to +83
$this->io->section('Found Media Library files');
dump($this->logReports);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See in the colibri script how an excel is generated and uploaded in the admin

Comment on lines +113 to +115
if($match['content_type'] === 'media_file') {
$emsLink = EMSLink::fromMatch($match);
$this->logMediaLibraryLink($key, $emsLink, $value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to log already "good" links

], $tempFile->path);
$filename = \sprintf('UpdateFileLinks - Rapport %s.xlsx', \date('YmdHis'));
$hash = $this->coreApi->file()->uploadFile($tempFile->path, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $filename);
$this->io->success($this->buildUrl($hash, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $filename));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the mime type not already define somewhere ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No but it can be put in a const to avoid repetition

@YanisGroffier YanisGroffier marked this pull request as ready for review March 9, 2026 12:06
@YanisGroffier YanisGroffier requested a review from a team as a code owner March 9, 2026 12:06
@YanisGroffier YanisGroffier requested a review from theus77 March 9, 2026 15:46
$this->mediaLibraryContentType = $this->getOptionString(self::OPTION_MEDIA_LIBRARY_CONTENT_TYPE);
$this->fileField = $this->getOptionString(self::OPTION_FILE_FIELD);
$this->coreApi = $this->adminHelper->getCoreApi();
$this->mimeType = $this->getOptionString(self::OPTION_MIME_TYPE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really useful to me. The mimetype 'Type of spreadsheet document', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' should be defined in the class EMS\Helpers\Html\MimeTypes

$defaultAlias = $this->coreApi->meta()->getDefaultContentTypeEnvironmentAlias($this->contentTypeName);
$search = new Search([$defaultAlias]);
$search->setContentTypes([$this->contentTypeName]);
$search->setSources(['*']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use $this->fields instead of retriving everything

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about converting property accessor field to a elasticsearch field

$hash = $link->getOuuid();
$found = $this->findMediaFileByHash($link, $hash, $value);
if ($found && $this->force) {
$value = \str_replace($match[0], $found->jsonSerialize(), $value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no log?

if ($found && $this->force) {
$value = \str_replace($match[0], $found->jsonSerialize(), $value);
} else {
$this->logAssetLink($key, EMSLink::fromMatch($match), $value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you log? not found or not $this->force


private function findMediaFileByHash(EMSLink $link, string $hash, mixed $value): ?EMSLink
{
$alias = $this->coreApi->meta()->getDefaultContentTypeEnvironmentAlias($this->mediaLibraryContentType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done once

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants