diff --git a/bin/console b/bin/console index 0b89ddadfe..38c70b0122 100755 --- a/bin/console +++ b/bin/console @@ -27,6 +27,7 @@ use Alchemy\Phrasea\Command\Databox\ListDataboxCommand; use Alchemy\Phrasea\Command\Databox\MountDataboxCommand; use Alchemy\Phrasea\Command\Databox\UnMountDataboxCommand; use Alchemy\Phrasea\Command\MailTest; +use Alchemy\Phrasea\Command\MetadataUpdater\SetmetaCustomCommand; use Alchemy\Phrasea\Command\NetworkProxiesTestCommand; use Alchemy\Phrasea\Command\Plugin\AddPlugin; use Alchemy\Phrasea\Command\Plugin\ListPlugin; @@ -189,6 +190,8 @@ $cli->command(new NetworkProxiesTestCommand('network-proxies:test')); $cli->command(new BuildPermalinks()); +$cli->command(new SetmetaCustomCommand()); + $cli->loadPlugins(); $cli->run(); diff --git a/lib/Alchemy/Phrasea/Command/MetadataUpdater/MetaManager.php b/lib/Alchemy/Phrasea/Command/MetadataUpdater/MetaManager.php new file mode 100644 index 0000000000..fa9009141f --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/MetadataUpdater/MetaManager.php @@ -0,0 +1,58 @@ +files($file)->first(); + $value = NULL; + + $metadata = null; + + foreach ($metadatas as $metadata) { + if ($metadata->getTag() == $tag) { + $value = explode(";", $metadata->getValue()); + break; + } + } + + return $value; + } + + public function setMetaByAPI($host,$base,$record,$token,$values,$metastructid) + { + $url = $host."/api/v1/records/".$base."/".$record."/setmetadatas/?oauth_token=".$token; + + $value=""; + $opt = []; + foreach ($values as $key => $value) { + $opt[] = "metadatas[".$key."][meta_struct_id]=".$metastructid."&metadatas[".$key."][meta_id]=&metadatas[".$key."][value]=".trim($value); + } + + $opts = implode("&", $opt); + + echo $url."\n"; + echo $opts."\n"; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $opts); + + $ce = curl_exec($ch); + curl_close($ch); + + print_r($ce); + } + +} diff --git a/lib/Alchemy/Phrasea/Command/MetadataUpdater/SetmetaCustomCommand.php b/lib/Alchemy/Phrasea/Command/MetadataUpdater/SetmetaCustomCommand.php new file mode 100644 index 0000000000..5435f47342 --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/MetadataUpdater/SetmetaCustomCommand.php @@ -0,0 +1,141 @@ +setName("metadatas:set") + ->setDescription('Read metadata from file and add it to field') + ->addOption('dry', null, InputOption::VALUE_NONE, "Dry run (list alert only and list record and meta values).") + ->addOption('max-rid', null, InputOption::VALUE_REQUIRED, "highest record_id value") + ->addOption('min-rid', null, InputOption::VALUE_REQUIRED, "lowest record_id value") + ->addOption('metadata-tag', null, InputOption::VALUE_REQUIRED, "tag to search exemple IPTC:KEYWORD") + ->addOption('meta-struct-id', null, InputOption::VALUE_REQUIRED, "ID of the field de fill") + ->addOption('sbas-id', null, InputOption::VALUE_REQUIRED, "Id of a database to work on") + ->addOption('subdef', null, InputOption::VALUE_REQUIRED, "name of the definition to read") + // ->setHelp('') + ; + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function doExecute(InputInterface $input, OutputInterface $output) + { + $this->input = $input; + // add cool styles + $style = new OutputFormatterStyle('black', 'yellow'); //array('bold')); + $output->getFormatter()->setStyle('warning', $style); + $this->output = $output; + + $this->appbox = $this->container['phraseanet.appbox']; + $this->databox = null; + $databoxes = NULL; + + if (empty($input->getOption('metadata-tag'))) { + $output->writeln("metadata-tag is mandatory"); + + return 0; + } + + + foreach ($this->appbox->get_databoxes() as $databox) { + $this->databox = $databox; + + if ($input->getOption("sbas-id") != NULL) { + $databox = $this->appbox->get_databox($input->getOption("sbas-id")); + } + + $sbas_id = $databox->get_sbas_id(); + $sbas_name = $databox->get_dbname(); + echo "Working ON ".$sbas_name." with Id ".$sbas_id; + $this->connection = $databox->get_connection(); + $meta = new MetaManager(); + $sql = "SELECT * FROM record"; + $record_max = $input->getOption("max-rid"); + $record_min = $input->getOption("min-rid"); + + if($record_min != NULL) { + $sql .=" WHERE record_id>=".$record_min; + } + + if($record_max != NULL) { + $sql .= " AND record_id<=".$record_max; + } + + $exec = $databox->get_connection()->executeQuery($sql); + + while ($row=$exec->fetch(\PDO::FETCH_ASSOC)) { + + $record = $this->databox->get_record($row['record_id']); + + /** @var media_subdef $subdef */ + + if($input->getOption("meta-struct-id") != NULL) { + $meta_struct_id = $input->getOption("meta-struct-id"); + } else { + echo "meta_struct_id is mandatory"; + break; + } + + foreach ($record->get_subdefs() as $subdef) { + $name = $subdef->get_name(); + $path = $subdef->get_path($name); + $filepath = $subdef->get_file($name); + $file = $path.$filepath; + $this->metadatatag = $input->getOption('metadata-tag'); + $subdef = ($input->getOption('subdef')==NULL ? "document" : $input->getOption('subdef')); + + if ($name==$subdef) { + echo "Searching for data into: ".$this->metadatatag." into ".$name."\n"; + $results = $meta->get_meta($file, $this->metadatatag); + // var_dump($results); + } + } + + if ($results != NULL) { + + foreach ($results as $result) { + $metadatas[] = [ + 'action' => 'set', + 'meta_struct_id' => $meta_struct_id, + 'meta_id' => null, + 'value' => $result + ]; + } + + $actions['metadatas'] = $metadatas; + + $record->setMetadatasByActions(json_decode(json_encode($actions))); + } + } + } + } +}