Last active
May 18, 2022 20:08
-
-
Save seth-shaw-unlv/2b6dc74b4b1b988b4224b12ceac06953 to your computer and use it in GitHub Desktop.
Islandora script for triggering new derivatives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\system\Entity\Action; | |
use Drupal\Core\Session\UserSession; | |
use Drupal\user\Entity\User; | |
$userid = 1; | |
$account = User::load($userid); | |
$accountSwitcher = \Drupal::service('account_switcher'); | |
$userSession = new UserSession([ | |
'uid' => $account->id(), | |
'name' => $account->getDisplayName(), | |
'roles' => $account->getRoles(), | |
]); | |
$accountSwitcher->switchTo($userSession); | |
$original_tid = reset( | |
\Drupal::entityQuery('taxonomy_term') | |
->condition('field_external_uri', 'http://pcdm.org/use#OriginalFile') | |
->execute() | |
); | |
$intermediate_tid = reset( | |
\Drupal::entityQuery('taxonomy_term') | |
->condition('field_external_uri', 'http://pcdm.org/use#IntermediateFile') | |
->execute() | |
); | |
$service_tid = reset( | |
\Drupal::entityQuery('taxonomy_term') | |
->condition('field_external_uri', 'http://pcdm.org/use#ServiceFile') | |
->execute() | |
); | |
$originals_query = \Drupal::database()->select('media', 'm'); | |
$originals_query->join('media__field_media_use', 'u', 'm.mid = u.entity_id'); | |
$originals_query->join('media__field_media_of', 'o', 'm.mid = o.entity_id'); | |
$originals_query->join('media__field_mime_type', 'mime', 'm.mid = mime.entity_id'); | |
$originals_query->fields('o', ['field_media_of_target_id']) | |
->condition('u.field_media_use_target_id', $original_tid) | |
->condition('mime.field_mime_type_value', 'image/%%', 'LIKE'); | |
$nodes_w_originals = $originals_query->execute()->fetchCol(); | |
# We are likely to get more service files than originals because we aren't limiting | |
# to images. This is because we sometimes use PDF (or perhaps other) service files. | |
$service_query = \Drupal::database()->select('media', 'm'); | |
$service_query->join('media__field_media_use', 'u', 'm.mid = u.entity_id'); | |
$service_query->join('media__field_media_of', 'o', 'm.mid = o.entity_id'); | |
$service_query->fields('o', ['field_media_of_target_id']) | |
->condition('u.field_media_use_target_id', $service_tid); | |
$nodes_w_service = $service_query->execute()->fetchCol(); | |
# Similarly, we may have intermediates that aren't images. | |
$intermediate_query = \Drupal::database()->select('media', 'm'); | |
$intermediate_query->join('media__field_media_use', 'u', 'm.mid = u.entity_id'); | |
$intermediate_query->join('media__field_media_of', 'o', 'm.mid = o.entity_id'); | |
$intermediate_query->fields('o', ['field_media_of_target_id']) | |
->condition('u.field_media_use_target_id', $intermediate_tid); | |
$nodes_w_intermediates = $intermediate_query->execute()->fetchCol(); | |
$results = array_diff($nodes_w_originals, $nodes_w_service); | |
$original_action = Action::load('image_generate_a_service_file_from_an_original_file'); | |
$intermediate_action = Action::load('image_generate_a_service_file_from_an_intermediate_file'); | |
foreach ($results as $nid) { | |
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid); | |
$action = (in_array($nid, $nodes_w_intermediates)) ? $intermediate_action : $original_action; | |
printf("Performing '%s' on '%s' at %s\n", $action->id(), $node->toUrl()->toString(), date(DATE_ATOM)); | |
$action->execute([$node]); | |
} | |
$accountSwitcher->switchBack(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment