/** * Checks for UUID in metadatas * * @todo Check if a file exists with the same checksum * @todo Check if an UUID is contained in the attributes, replace It if * necessary * * @param boolean $generate if true, if no uuid found, a valid one is generated * @param boolean $write if true, writes uuid in all available metadatas * @return File */ public function getUUID($generate = false, $write = false) { if ($this->uuid && !$write) { return $this->uuid; } $availableUUIDs = ['XMP-exif:ImageUniqueID', 'SigmaRaw:ImageUniqueID', 'IPTC:UniqueDocumentID', 'ExifIFD:ImageUniqueID', 'Canon:ImageUniqueID']; if (!$this->uuid) { $metadatas = $this->media->getMetadatas(); $uuid = null; foreach ($availableUUIDs as $meta) { if ($metadatas->containsKey($meta)) { $candidate = $metadatas->get($meta)->getValue()->asString(); if (Uuid::isValid($candidate)) { $uuid = $candidate; break; } } } if (!$uuid && $generate) { $uuid = Uuid::uuid4(); } $this->uuid = $uuid; } if ($write) { $value = new MonoValue($this->uuid); $metadatas = new ExiftoolMetadataBag(); foreach ($availableUUIDs as $tagname) { $metadatas->add(new Metadata(TagFactory::getFromRDFTagname($tagname), $value)); } /** * PHPExiftool throws exception on some files not supported */ try { $this->app['exiftool.writer']->reset(); $this->app['exiftool.writer']->write($this->getFile()->getRealPath(), $metadatas); } catch (PHPExiftoolException $e) { } } return $this->uuid; }
/** * {@inheritdoc} */ protected function doJob(JobData $data) { $app = $data->getApplication(); $settings = simplexml_load_string($data->getTask()->getSettings()); $clearDoc = (bool) (string) $settings->cleardoc; $MWG = (bool) (string) $settings->mwg; // move this in service provider configuration // $app['exiftool.writer']->setModule(Writer::MODULE_MWG, true); foreach ($app->getDataboxes() as $databox) { $conn = $databox->get_connection(); $metaSubdefs = []; foreach ($databox->get_subdef_structure() as $type => $definitions) { foreach ($definitions as $sub) { $name = $sub->get_name(); if ($sub->meta_writeable()) { $metaSubdefs[$name . '_' . $type] = true; } } } $sql = 'SELECT record_id, coll_id, jeton FROM record WHERE (jeton & ' . PhraseaTokens::WRITE_META . ' > 0)'; $stmt = $conn->prepare($sql); $stmt->execute(); $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC); $stmt->closeCursor(); foreach ($rs as $row) { $record_id = $row['record_id']; $token = $row['jeton']; $record = $databox->get_record($record_id); $type = $record->get_type(); $subdefs = []; foreach ($record->get_subdefs() as $name => $subdef) { $write_document = $token & PhraseaTokens::WRITE_META_DOC && $name == 'document'; $write_subdef = $token & PhraseaTokens::WRITE_META_SUBDEF && isset($metaSubdefs[$name . '_' . $type]); if (($write_document || $write_subdef) && $subdef->is_physically_present()) { $subdefs[$name] = $subdef->get_pathfile(); } } $metadata = new Metadata\MetadataBag(); if ($record->getUuid()) { $metadata->add(new Metadata\Metadata(new Tag\XMPExif\ImageUniqueID(), new Value\Mono($record->getUuid()))); $metadata->add(new Metadata\Metadata(new Tag\ExifIFD\ImageUniqueID(), new Value\Mono($record->getUuid()))); $metadata->add(new Metadata\Metadata(new Tag\IPTC\UniqueDocumentID(), new Value\Mono($record->getUuid()))); } $caption = $record->get_caption(); foreach ($databox->get_meta_structure() as $fieldStructure) { $tagName = $fieldStructure->get_tag()->getTagname(); $fieldName = $fieldStructure->get_name(); // skip fields with no src if ($tagName == '') { continue; } // check exiftool known tags to skip Phraseanet:tf-* try { TagFactory::getFromRDFTagname($tagName); } catch (TagUnknown $e) { continue; } try { $field = $caption->get_field($fieldName); $data = $field->get_values(); if ($fieldStructure->is_multi()) { $values = array(); foreach ($data as $value) { $values[] = $value->getValue(); } $value = new Value\Multi($values); } else { $data = array_pop($data); $value = $data->getValue(); $value = new Value\Mono($value); } } catch (\Exception $e) { // the field is not set in the record, erase it if ($fieldStructure->is_multi()) { $value = new Value\Multi(array('')); } else { $value = new Value\Mono(''); } } $metadata->add(new Metadata\Metadata($fieldStructure->get_tag(), $value)); } $app['exiftool.writer']->reset(); if ($MWG) { $app['exiftool.writer']->setModule(ExifWriter::MODULE_MWG, true); } foreach ($subdefs as $name => $file) { $app['exiftool.writer']->erase($name != 'document' || $clearDoc, true); try { $app['exiftool.writer']->write($file, $metadata); $this->log('info', sprintf('meta written for sbasid=%1$d - recordid=%2$d (%3$s)', $databox->get_sbas_id(), $record_id, $name)); } catch (PHPExiftoolException $e) { $this->log('error', sprintf('meta NOT written for sbasid=%1$d - recordid=%2$d (%3$s) because "%s"', $databox->get_sbas_id(), $record_id, $name, $e->getMessage())); } } $sql = 'UPDATE record SET jeton=jeton & ~' . PhraseaTokens::WRITE_META . ' WHERE record_id = :record_id'; $stmt = $conn->prepare($sql); $stmt->execute([':record_id' => $record_id]); $stmt->closeCursor(); } } }
/** * Get a PHPExiftool Tag from tagName * * @param string $tagName * @param bool $throwException * @return object Makes phpstorm hangs \PHPExiftool\Driver\TagInterface */ public static function loadClassFromTagName($tagName, $throwException = true) { $tagName = str_ireplace('/rdf:rdf/rdf:description/', '', $tagName); if ('' === trim($tagName)) { return new Nosource(); } try { return TagFactory::getFromTagname($tagName); } catch (TagUnknown $exception) { if ($throwException) { throw new NotFoundHttpException(sprintf("Tag %s not found", $tagName), $exception); } } return new Nosource(); }