Skip to content

Commit

Permalink
integrate plugin metadataupdater to phraseanet
Browse files Browse the repository at this point in the history
  • Loading branch information
aynsix committed Nov 22, 2023
1 parent ab26d49 commit 2486446
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -189,6 +190,8 @@ $cli->command(new NetworkProxiesTestCommand('network-proxies:test'));

$cli->command(new BuildPermalinks());

$cli->command(new SetmetaCustomCommand());

$cli->loadPlugins();

$cli->run();
58 changes: 58 additions & 0 deletions lib/Alchemy/Phrasea/Command/MetadataUpdater/MetaManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Alchemy\Phrasea\Command\MetadataUpdater;

use Monolog\Logger;
use PHPExiftool\Reader;

class MetaManager
{

public function get_meta($file,$tag )
{
$logger = new Logger('exif-tool');
$reader = Reader::create($logger);

$metadatas = $reader->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);
}

}
141 changes: 141 additions & 0 deletions lib/Alchemy/Phrasea/Command/MetadataUpdater/SetmetaCustomCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace Alchemy\Phrasea\Command\MetadataUpdater;

use \appbox;
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

class SetmetaCustomCommand extends Command
{
/** @var InputInterface $input */
private $input;
/** @var OutputInterface $output */
private $output;
/** @var appbox $appbox */
private $appbox;
/** @var array $databoxes */
private $databoxes;


public function configure()
{
$this->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)));
}
}
}
}
}

0 comments on commit 2486446

Please sign in to comment.