Skip to content
Snippets Groups Projects
Commit 4baf3e78 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

hotline #61844 : wip

parent 05d13d95
Branches
Tags
4 merge requests!2334Master,!2254Master,!2253Hotline master,!2251Hotline#61844 recuperation des enrichissements
Pipeline #2100 passed with stage
in 19 minutes and 43 seconds
......@@ -9,40 +9,74 @@ possible command and their options are:
attach path/to/old/metadatas/file.php\n\n";
if (!$argv[1]
if (!isset($argv[1])
|| !in_array($argv[1], ['extract', 'attach'])) {
echo $usage;
exit(1);
}
class Artevod_Metadatas_Item extends Class_Entity {
public static function newFrom($data) {
$id = array_key_exists('id', $data) ? $data['id'] : null;
$tags = array_key_exists('tags', $data) ? $data['tags'] : null;
return (new static())
->setId($id)
->setTags($tags);
}
public function asArray() {
return ['id' => $this->getId(),
'tags' => $this->getTags(),
'merged' => $this->getMergedTags()];
}
}
class Artevod_Metadatas_Collection {
protected $_metas = [];
public function __construct($metas=[]) {
$this->_metas = $metas;
foreach($metas as $k => $v)
$this->_metas[$k] = Artevod_Metadatas_Item::newFrom($v);
}
public function mergeWith($other) {
$merge = new static();
array_walk($this->_metas,
function($value, $key) use($other, $merge)
$this
->eachDo(function($value, $key) use($other, $merge)
{
if (!$other->has($key))
return;
$mine = explode(';', $value);
$theirs = explode(';', $other->get($key, ''));
if ($missing = array_diff($theirs, $mine))
$merge->add($key, implode(';', $missing));
$mine = explode(';', $value->getTags());
$theirs = explode(';', $other->get($key)->getTags());
if ($missing = array_filter(array_diff($theirs, $mine))) {
$merged = array_unique(array_merge($mine, $theirs));
$item = (new Artevod_Metadatas_Item())
->setId($value->getId())
->setTags(implode(';', $missing))
->setMergedTags(implode(';', array_filter($merged)));
$merge->atPut($key, $item);
}
});
return $merge;
}
public function add($key, $value) {
public function eachDo($closure) {
array_walk($this->_metas, $closure);
}
public function atPut($key, $value) {
$this->_metas[$key] = $value;
}
......@@ -60,7 +94,11 @@ class Artevod_Metadatas_Collection {
public function asArray() {
return $this->_metas;
$result = [];
foreach($this->_metas as $k => $v)
$result[$k] = $v->asArray();
return $result;
}
......@@ -75,7 +113,8 @@ class Artevod_Metadatas_Collection {
public function firstKey() {
return reset(array_keys($this->_metas));
$keys = array_keys($this->_metas);
return reset($keys);
}
......@@ -85,7 +124,8 @@ class Artevod_Metadatas_Collection {
public function lastKey() {
return end(array_keys($this->_metas));
$keys = array_keys($this->_metas);
return end($keys);
}
}
......@@ -110,6 +150,11 @@ class Artevod_Metadatas {
echo "\n";
$path = __DIR__ . '/artevod_metas_' . $this->_db_name . '.php';
$this->_writeInFile($collection, $path);
}
protected function _writeInFile($collection, $path) {
file_put_contents($path,
'<?php return ' . var_export($collection->asArray(), true) . ';');
......@@ -141,7 +186,10 @@ class Artevod_Metadatas {
return;
}
$collection->add($key, $album['tags']);
$item = (new Artevod_Metadatas_Item())
->setId($album['id'])
->setTags($album['tags']);
$collection->atPut($key, $item);
echo '.';
}
......@@ -185,7 +233,35 @@ class Artevod_Metadatas {
$merge = $current_metas->mergeWith($old_metas);
echo "found " . $merge->count() . " albums with missing data\n";
echo "example : " . $merge->firstKey() . ' is missing ' . $merge->first() . "\n";
$this->_injectMetasInCurrentDb($merge);
echo "\nbackup what was merged...\n";
$path = __DIR__ . '/artevod_metas_merge_'. date('Ymd_His') .'.php';
$this->_writeInFile($merge, $path);
}
protected function _injectMetasInCurrentDb($collection) {
$statement = $this->_db->prepare('update album set tags=? where id=?');
$collection
->eachDo(function($item, $key) use($statement)
{
$this->_injectMetaInCurrentDb($item, $statement);
});
}
protected function _injectMetaInCurrentDb($item, $statement) {
$tags = $item->getMergedTags();
$id = $item->getId();
$statement->bind_param('si', $tags, $id);
if (!$statement->execute()) {
echo $this->_db->error . "\n";
return;
}
echo ".";
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment