public function replaceMetadata($metadataCollection, \record_adapter $record) { $metadatas = []; $tagnameToFieldnameMapping = []; $arrayStructure = iterator_to_array($record->get_databox()->get_meta_structure()); array_walk($arrayStructure, function ($databoxField) use(&$tagnameToFieldnameMapping) { $tagname = $databoxField->get_tag()->getTagname(); $tagnameToFieldnameMapping[$tagname][] = $databoxField->get_name(); }); array_walk($metadataCollection, function (Metadata $metadata) use(&$metadatas, $tagnameToFieldnameMapping) { $tagname = $metadata->getTag()->getTagname(); if (!isset($tagnameToFieldnameMapping[$tagname])) { return; } foreach ($tagnameToFieldnameMapping[$tagname] as $fieldname) { if (!isset($metadatas[$fieldname])) { $metadatas[$fieldname] = []; } $metadatas[$fieldname] = array_merge($metadatas[$fieldname], $metadata->getValue()->asArray()); } }); $metas = []; array_walk($arrayStructure, function (\databox_field $field) use(&$metas, $metadatas, $record) { $fieldname = $field->get_name(); if (!isset($metadatas[$fieldname])) { return; } $values = $metadatas[$fieldname]; if ($record->get_caption()->has_field($fieldname)) { foreach ($record->get_caption()->get_field($fieldname)->get_values() as $value) { $value->delete(); } } if ($field->is_multi()) { $tmpValues = []; foreach ($values as $value) { $tmpValues = array_merge($tmpValues, \caption_field::get_multi_values($value, $field->get_separator())); } $values = array_unique($tmpValues); foreach ($values as $value) { if (trim($value) === '') { continue; } $metas[] = ['meta_struct_id' => $field->get_id(), 'meta_id' => null, 'value' => $value]; } } else { $value = array_pop($values); if (trim($value) === '') { return; } $metas[] = ['meta_struct_id' => $field->get_id(), 'meta_id' => null, 'value' => $value]; } }); if (count($metas) > 0) { $record->set_metadatas($metas, true); } }
/** * {@inheritdoc} */ public function toMetadataArray(\databox_descriptionStructure $metadatasStructure) { $metas = []; $unicode = new \unicode(); foreach ($metadatasStructure as $databox_field) { if ('' === $databox_field->get_tag()->getTagname()) { // skipping fields without sources continue; } if ($this->containsKey($databox_field->get_tag()->getTagname())) { if ($databox_field->is_multi()) { $values = $this->get($databox_field->get_tag()->getTagname())->getValue()->asArray(); $tmp = []; foreach ($values as $value) { foreach (\caption_field::get_multi_values($value, $databox_field->get_separator()) as $v) { $tmp[] = $v; } } $values = array_unique($tmp); foreach ($values as $value) { $value = $unicode->substituteCtrlCharacters($value, ' '); $value = $unicode->toUTF8($value); if ($databox_field->get_type() == 'date') { $value = $unicode->parseDate($value); } $metas[] = ['meta_struct_id' => $databox_field->get_id(), 'value' => $value, 'meta_id' => null]; } } else { $value = $this->get($databox_field->get_tag()->getTagname())->getValue()->asString(); $value = $unicode->substituteCtrlCharacters($value, ' '); $value = $unicode->toUTF8($value); if ($databox_field->get_type() == 'date') { $value = $unicode->parseDate($value); } $metas[] = ['meta_struct_id' => $databox_field->get_id(), 'value' => $value, 'meta_id' => null]; } } } unset($unicode); return $metas; }
protected function readXMLForDatabox(Application $app, \databox_descriptionStructure $metadatasStructure, $pathfile) { if (false === $app['filesystem']->exists($pathfile)) { throw new \InvalidArgumentException(sprintf('file %s does not exists', $pathfile)); } if (false === ($sxcaption = @simplexml_load_file($pathfile))) { throw new \InvalidArgumentException(sprintf('Invalid XML file %s', $pathfile)); } $metadataBag = new MetaFieldsBag(); foreach ($sxcaption->description->children() as $tagname => $field) { $field = trim($field); $meta = $metadatasStructure->get_element_by_name(trim($tagname)); if (!$meta) { continue; } if ($meta->is_multi()) { $fields = caption_field::get_multi_values($field, $meta->get_separator()); if (!$metadataBag->containsKey($meta->get_name())) { $values = $fields; } else { $values = array_merge($metadataBag->get($meta->get_name())->getValue(), $fields); } $metadataBag->set($meta->get_name(), new BorderAttribute\MetaField($meta, $values)); } else { $metadataBag->set($meta->get_name(), new BorderAttribute\MetaField($meta, [$field])); } } return $metadataBag; }
public static function purge() { self::$localCache = []; }
/** * {@inheritdoc} */ public function apply(base $databox, Application $app) { /** * Fail if upgrade has previously failed, no problem */ try { $sql = "ALTER TABLE `metadatas`\n ADD `updated` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '1',\n ADD INDEX ( `updated` )"; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $stmt->closeCursor(); $sql = 'UPDATE metadatas SET updated = "0" WHERE meta_struct_id IN ( SELECT id FROM metadatas_structure WHERE multi = "1" )'; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $stmt->closeCursor(); } catch (DBALException $e) { } try { $sql = 'ALTER TABLE `metadatas` DROP INDEX `unique`'; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $stmt->closeCursor(); } catch (DBALException $e) { } $sql = 'SELECT m . * FROM metadatas_structure s, metadatas m WHERE m.meta_struct_id = s.id AND s.multi = "1" AND updated="0"'; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $rowCount = $stmt->rowCount(); $stmt->closeCursor(); $n = 0; $perPage = 1000; while ($n < $rowCount) { $sql = 'SELECT m . * FROM metadatas_structure s, metadatas m WHERE m.meta_struct_id = s.id AND s.multi = "1" LIMIT ' . $n . ', ' . $perPage; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); $databox->get_connection()->beginTransaction(); $sql = 'INSERT INTO metadatas(id, record_id, meta_struct_id, value) VALUES (null, :record_id, :meta_struct_id, :value)'; $stmt = $databox->get_connection()->prepare($sql); $databox_fields = []; foreach ($rs as $row) { $meta_struct_id = $row['meta_struct_id']; if (!isset($databox_fields[$meta_struct_id])) { $databox_fields[$meta_struct_id] = \databox_field::get_instance($app, $databox, $meta_struct_id); } $values = \caption_field::get_multi_values($row['value'], $databox_fields[$meta_struct_id]->get_separator()); foreach ($values as $value) { $params = [':record_id' => $row['record_id'], ':meta_struct_id' => $row['meta_struct_id'], ':value' => $value]; $stmt->execute($params); } } $stmt->closeCursor(); $sql = 'DELETE FROM metadatas WHERE id = :id'; $stmt = $databox->get_connection()->prepare($sql); foreach ($rs as $row) { $params = [':id' => $row['id']]; $stmt->execute($params); } $stmt->closeCursor(); $databox->get_connection()->commit(); $n += $perPage; } /** * Remove the extra column */ try { $sql = "ALTER TABLE `metadatas` DROP `updated`"; $stmt = $databox->get_connection()->prepare($sql); $stmt->execute(); $stmt->closeCursor(); } catch (\Exception $e) { } return true; }
/** * @covers caption_field::get_multi_values * @dataProvider getMultiValues */ public function testGet_multi_values($separator, $serialized, $values) { $this->assertEquals($values, caption_field::get_multi_values($serialized, $separator)); }
/** * * @todo move this function to caption_record * @param Array $params An array containing three keys : meta_struct_id (int) , meta_id (int or null) and value (Array) * @return record_adapter */ protected function set_metadata(array $params, databox $databox) { $mandatoryParams = ['meta_struct_id', 'meta_id', 'value']; foreach ($mandatoryParams as $param) { if (!array_key_exists($param, $params)) { throw new Exception_InvalidArgument(sprintf('Invalid metadata, missing key %s', $param)); } } if (!is_scalar($params['value'])) { throw new Exception('Metadata value should be scalar'); } $databox_field = databox_field::get_instance($this->app, $databox, $params['meta_struct_id']); $caption_field = new caption_field($this->app, $databox_field, $this); $vocab = $vocab_id = null; if (isset($params['vocabularyId']) && $databox_field->getVocabularyControl()) { try { $vocab = $databox_field->getVocabularyControl(); $vocab_id = $params['vocabularyId']; $vocab->validate($vocab_id); } catch (\Exception $e) { $vocab = $vocab_id = null; } } if (trim($params['meta_id']) !== '') { $tmp_val = trim($params['value']); $caption_field_value = $caption_field->get_value($params['meta_id']); if ($tmp_val === '') { $caption_field_value->delete(); unset($caption_field_value); } else { $caption_field_value->set_value($params['value']); if ($vocab && $vocab_id) { $caption_field_value->setVocab($vocab, $vocab_id); } } } else { $caption_field_value = caption_Field_Value::create($this->app, $databox_field, $this, $params['value'], $vocab, $vocab_id); } $this->caption_record = null; return $this; }
public function tearDown() { ACLProvider::purge(); \collection::purge(); \databox::purge(); \caption_field::purge(); \caption_Field_Value::purge(); \databox_field::purge(); \databox_status::purge(); \thesaurus_xpath::purge(); /** * Kris Wallsmith pro-tip * @see http://kriswallsmith.net/post/18029585104/faster-phpunit */ $refl = new ReflectionObject($this); foreach ($refl->getProperties() as $prop) { if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_') && 0 !== strpos($prop->getDeclaringClass()->getName(), 'Phraseanet')) { $prop->setAccessible(true); $prop->setValue($this, null); } } $refl = null; parent::tearDown(); //In case some executed script modify 'max_execution_time' ini var //Initialize set_time_limit(0) which is the default value for PHP CLI set_time_limit(0); }
/** * Update the metadatas of a record * * @param \record_adapter $record * @param string $xml */ protected function updateMetadatas(\record_adapter $record, $xml) { $metas = $record->get_databox()->get_meta_structure(); $datas = $metadatas = []; if (false !== ($sxe = simplexml_load_string($xml))) { $fields = $sxe->xpath('/record/description'); if ($fields && is_array($fields)) { foreach ($fields[0] as $fieldname => $value) { $fieldname = trim($fieldname); $value = trim($value); if (null === ($databox_field = $metas->get_element_by_name($fieldname))) { continue; } if ($databox_field->is_multi()) { $new_value = \caption_field::get_multi_values($value, $databox_field->get_separator()); if (isset($datas[$databox_field->get_id()])) { $value = array_unique(array_merge($datas[$databox_field->get_id()], $new_value)); } else { $value = $new_value; } } else { $new_value = $value; if (isset($datas[$databox_field->get_id()])) { $value = $datas[$databox_field->get_id()] . ' ' . $new_value; } else { $value = $new_value; } } $datas[$databox_field->get_id()] = $value; } } } foreach ($datas as $meta_struct_id => $values) { if (is_array($values)) { foreach ($values as $value) { $metadatas[] = ['meta_struct_id' => $meta_struct_id, 'meta_id' => null, 'value' => $value]; } } else { $metadatas[] = ['meta_struct_id' => $meta_struct_id, 'meta_id' => null, 'value' => $values]; } } $record->set_metadatas($metadatas, true); }
/** * * @return databox_field */ public function save() { $connbas = $this->get_connection(); $sql = 'UPDATE metadatas_structure SET `name` = :name, `src` = :source, `indexable` = :indexable, `readonly` = :readonly, `required` = :required, `separator` = :separator, `multi` = :multi, `business` = :business, `aggregable` = :aggregable, `report` = :report, `type` = :type, `tbranch` = :tbranch, `sorter` = :position, `thumbtitle` = :thumbtitle, `VocabularyControlType` = :VocabularyControlType, `RestrictToVocabularyControl` = :RestrictVocab, `label_en` = :label_en, `label_fr` = :label_fr, `label_de` = :label_de, `label_nl` = :label_nl WHERE id = :id'; $params = [':name' => $this->name, ':source' => $this->tag->getTagname(), ':indexable' => $this->indexable ? '1' : '0', ':readonly' => $this->readonly ? '1' : '0', ':required' => $this->required ? '1' : '0', ':separator' => $this->separator, ':multi' => $this->multi ? '1' : '0', ':business' => $this->Business ? '1' : '0', ':aggregable' => $this->aggregable ? '1' : '0', ':report' => $this->report ? '1' : '0', ':type' => $this->type, ':tbranch' => $this->tbranch, ':position' => $this->position, ':thumbtitle' => $this->thumbtitle, ':VocabularyControlType' => $this->Vocabulary ? $this->Vocabulary->getType() : null, ':RestrictVocab' => $this->Vocabulary ? $this->VocabularyRestriction ? '1' : '0' : '0', ':id' => $this->id, ':label_en' => isset($this->labels['en']) ? $this->labels['en'] : null, ':label_fr' => isset($this->labels['fr']) ? $this->labels['fr'] : null, ':label_de' => isset($this->labels['de']) ? $this->labels['de'] : null, ':label_nl' => isset($this->labels['nl']) ? $this->labels['nl'] : null]; $stmt = $connbas->prepare($sql); $stmt->execute($params); $stmt->closeCursor(); if ($this->renamed) { caption_field::rename_all_metadatas($this); $this->renamed = false; } $dom_struct = $this->databox->get_dom_structure(); $xp_struct = $this->databox->get_xpath_structure(); $nodes = $xp_struct->query('/record/description/*[@meta_id=' . $this->id . ']'); if ($nodes->length == 0) { $meta = $dom_struct->createElement($this->name); $nodes_parent = $xp_struct->query('/record/description'); $nodes_parent->item(0)->appendChild($meta); } else { $meta = $nodes->item(0); if ($this->name != $meta->nodeName) { $old_meta = $meta; $meta = $dom_struct->createElement($this->name); $nodes_parent = $xp_struct->query('/record/description'); $nodes_parent->item(0)->replaceChild($meta, $old_meta); } } $meta->setAttribute('src', $this->tag->getTagname()); $meta->setAttribute('index', $this->indexable ? '1' : '0'); $meta->setAttribute('readonly', $this->readonly ? '1' : '0'); $meta->setAttribute('required', $this->required ? '1' : '0'); $meta->setAttribute('multi', $this->multi ? '1' : '0'); $meta->setAttribute('report', $this->report ? '1' : '0'); $meta->setAttribute('business', $this->Business ? '1' : '0'); $meta->setAttribute('aggregable', $this->aggregable ? '1' : '0'); $meta->setAttribute('type', $this->type); $meta->setAttribute('tbranch', $this->tbranch); if ($this->multi) { $meta->setAttribute('separator', $this->separator); } $meta->setAttribute('thumbtitle', $this->thumbtitle); $meta->setAttribute('meta_id', $this->id); $meta->setAttribute('sorter', $this->position); $this->delete_data_from_cache(); $this->databox->saveStructure($dom_struct); return $this; }
/** * Retrieve information about a caption field * * @param caption_field $field * * @return array */ private function list_record_caption_field(\caption_Field_Value $value, \caption_field $field) { return ['meta_id' => $value->getId(), 'meta_structure_id' => $field->get_meta_struct_id(), 'name' => $field->get_name(), 'labels' => ['fr' => $field->get_databox_field()->get_label('fr'), 'en' => $field->get_databox_field()->get_label('en'), 'de' => $field->get_databox_field()->get_label('de'), 'nl' => $field->get_databox_field()->get_label('nl')], 'value' => $value->getValue()]; }
/** * Adds a record to Phraseanet * * @param File $file The package file * @return \record_adater */ protected function createRecord(File $file) { $element = \record_adapter::createFromFile($file, $this->app); $date = new \DateTime(); $file->addAttribute(new MetadataAttr(new Metadata(new TfArchivedate(), new MonoValue($date->format('Y/m/d H:i:s'))))); $file->addAttribute(new MetadataAttr(new Metadata(new TfRecordid(), new MonoValue($element->get_record_id())))); $metadatas = []; /** * @todo $key is not tagname but fieldname */ $fieldToKeyMap = []; if (!$fieldToKeyMap) { foreach ($file->getCollection()->get_databox()->get_meta_structure() as $databox_field) { $tagname = $databox_field->get_tag()->getTagname(); if (!isset($fieldToKeyMap[$tagname])) { $fieldToKeyMap[$tagname] = []; } $fieldToKeyMap[$tagname][] = $databox_field->get_name(); } } foreach ($file->getMedia()->getMetadatas() as $metadata) { $key = $metadata->getTag()->getTagname(); if (!isset($fieldToKeyMap[$key])) { continue; } foreach ($fieldToKeyMap[$key] as $k) { if (!isset($metadatas[$k])) { $metadatas[$k] = []; } $metadatas[$k] = array_merge($metadatas[$k], $metadata->getValue()->asArray()); } } foreach ($file->getAttributes() as $attribute) { switch ($attribute->getName()) { /** * @todo implement METATAG aka metadata by fieldname (where as * current metadata is metadata by source. */ case AttributeInterface::NAME_METAFIELD: $key = $attribute->getField()->get_name(); if (!isset($metadatas[$key])) { $metadatas[$key] = []; } $metadatas[$key] = array_merge($metadatas[$key], $attribute->getValue()); break; case AttributeInterface::NAME_METADATA: $key = $attribute->getValue()->getTag()->getTagname(); if (!isset($fieldToKeyMap[$key])) { continue; } foreach ($fieldToKeyMap[$key] as $k) { if (!isset($metadatas[$k])) { $metadatas[$k] = []; } $metadatas[$k] = array_merge($metadatas[$k], $attribute->getValue()->getValue()->asArray()); } break; case AttributeInterface::NAME_STATUS: $element->set_binary_status(decbin(bindec($element->get_status()) | bindec($attribute->getValue()))); break; case AttributeInterface::NAME_STORY: $story = $attribute->getValue(); if (!$story->hasChild($element)) { $story->appendChild($element); } break; } } $databox = $element->get_databox(); $metas = []; foreach ($metadatas as $fieldname => $values) { foreach ($databox->get_meta_structure()->get_elements() as $databox_field) { if ($databox_field->get_name() == $fieldname) { if ($databox_field->is_multi()) { $tmpValues = []; foreach ($values as $value) { $tmpValues = array_merge($tmpValues, \caption_field::get_multi_values($value, $databox_field->get_separator())); } $values = array_unique($tmpValues); foreach ($values as $value) { if (!trim($value)) { continue; } $metas[] = ['meta_struct_id' => $databox_field->get_id(), 'meta_id' => null, 'value' => $value]; } } else { $value = array_pop($values); if (!trim($value)) { continue; } $metas[] = ['meta_struct_id' => $databox_field->get_id(), 'meta_id' => null, 'value' => $value]; } } } } if ($metas) { $element->set_metadatas($metas, true); } $element->rebuild_subdefs(); $element->reindex(); return $element; }