Skip to content

Commit

Permalink
Allow to refresh metadatas even if media is not host locally
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaudGLT committed Jan 27, 2025
1 parent c5ab406 commit eed1f99
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
55 changes: 32 additions & 23 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,8 @@ function (Event $event) {
return;
}
$valueOptions = ['clear' => 'Clear text']; // @translate;
$store = $this->getServiceLocator()->get('Omeka\File\Store');
if ($store instanceof Local) {
// Files must be stored locally to refresh extracted text.
$valueOptions['refresh'] = 'Refresh text'; // @translate
}
$valueOptions['refresh'] = 'Refresh text'; // @translate

$form->add([
'name' => 'extract_text_action',
'type' => Element\Select::class,
Expand Down Expand Up @@ -276,13 +273,10 @@ function (Event $event) {
'view.edit.form.after',
function (Event $event) {
$view = $event->getTarget();
$store = $this->getServiceLocator()->get('Omeka\File\Store');
$select = new Element\Select('extract_text_action');
$valueOptions = ['clear' => 'Clear text']; // @translate
if ($store instanceof Local) {
$valueOptions['refresh'] = 'Refresh text'; // @translate
$valueOptions['refresh_background'] = 'Refresh text (background)'; // @translate
}
$valueOptions['refresh'] = 'Refresh text'; // @translate
$valueOptions['refresh_background'] = 'Refresh text (background)'; // @translate
$select->setLabel('Extract text'); // @translate
$select->setValueOptions($valueOptions);
$select->setEmptyOption('[No action]'); // @translate
Expand Down Expand Up @@ -310,10 +304,10 @@ public function extractTextItem(Item $item, Property $textProperty, $action = 'd
{
$services = $this->getServiceLocator();
$store = $services->get('Omeka\File\Store');
$downloader = $services->get('Omeka\File\Downloader');
if ('refresh_background' === $action) {
// Note that we only dispatch the job when a) not already in the
// background and b) when files are stored locally.
if (('cli' !== PHP_SAPI) && ($store instanceof Local)) {
// Note that we only dispatch the job when not already in the background.
if ('cli' !== PHP_SAPI) {
$jobDispatcher = $services->get('Omeka\Job\Dispatcher');
$jobDispatcher->dispatch('ExtractText\Job\RefreshItemText', [
'item_id' => $item->getId(),
Expand All @@ -326,10 +320,17 @@ public function extractTextItem(Item $item, Property $textProperty, $action = 'd
// Order by position in case the position was changed on this request.
$criteria = Criteria::create()->orderBy(['position' => Criteria::ASC]);
foreach ($itemMedia->matching($criteria) as $media) {
// Files must be stored locally to refresh extracted text.
if (('refresh' === $action) && ($store instanceof Local)) {
$filePath = $store->getLocalPath(sprintf('original/%s', $media->getFilename()));
$this->setTextToMedia($filePath, $media, $textProperty);
if ('refresh' === $action) {
if ($store instanceof Local) {
$filePath = $store->getLocalPath(sprintf('original/%s', $media->getFilename()));
$this->setTextToMedia($filePath, $media, $textProperty);
} else {
$tempFile = $downloader->download($store->getUri(sprintf('original/%s', $media->getFilename())));
if ($tempFile) {
$this->setTextToMedia($tempFile->getTempPath(), $media, $textProperty);
$tempFile->delete();
}
}
}
$mediaValues = $media->getValues();
$criteria = Criteria::create()
Expand Down Expand Up @@ -365,20 +366,28 @@ public function extractTextMedia(Media $media, Property $textProperty, $action =
{
$services = $this->getServiceLocator();
$store = $services->get('Omeka\File\Store');
$downloader = $services->get('Omeka\File\Downloader');
if ('refresh_background' === $action) {
// Note that we only dispatch the job when a) not already in the
// background and b) when files are stored locally.
if (('cli' !== PHP_SAPI) && ($store instanceof Local)) {
// Note that we only dispatch the job when not already in the background.
if ('cli' !== PHP_SAPI) {
$jobDispatcher = $services->get('Omeka\Job\Dispatcher');
$jobDispatcher->dispatch('ExtractText\Job\RefreshMediaText', [
'media_id' => $media->getId(),
]);
}
return;
}
if (('refresh' === $action) && ($store instanceof Local)) {
$filePath = $store->getLocalPath(sprintf('original/%s', $media->getFilename()));
$this->setTextToMedia($filePath, $media, $textProperty);
if ('refresh' === $action) {
if ($store instanceof Local) {
$filePath = $store->getLocalPath(sprintf('original/%s', $media->getFilename()));
$this->setTextToMedia($filePath, $media, $textProperty);
} else {
$tempFile = $downloader->download($store->getUri(sprintf('original/%s', $media->getFilename())));
if ($tempFile) {
$this->setTextToMedia($tempFile->getTempPath(), $media, $textProperty);
$tempFile->delete();
}
}
}
if ('clear' === $action) {
$mediaValues = $media->getValues();
Expand Down
8 changes: 3 additions & 5 deletions config/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ version = "2.1.0"
omeka_version_constraint = "^4.0.0"
name = "Extract Text"
description = "Extract text from files to make them searchable."
author = "Omeka Team"
author_link = "https://omeka.org/"
author = "Omeka Team + BibLibre"
author_link = "https://github.com/biblibre"
configurable = true
module_link = "https://omeka.org/s/docs/user-manual/modules/extracttext/"
dev_link = "https://omeka.org/s/docs/developer/module_docs/ExtractText/"
support_link = "https://forum.omeka.org/c/omeka-s/modules"
module_link = "https://github.com/biblibre/omeka-s-module-ExtractText.git"

0 comments on commit eed1f99

Please sign in to comment.