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

hotline #61844 : wip

parent 31a572dc
Branches
Tags
4 merge requests!2334Master,!2254Master,!2253Hotline master,!2251Hotline#61844 recuperation des enrichissements
Pipeline #2089 passed with stage
in 20 minutes and 38 seconds
- ticket #61844 : ArteVOD, script de récupération des enrichissements
\ No newline at end of file
<?php
require 'console.php';
$usage = "usage : php artevod_metadatas.php command [options]
possible command and their options are:
extract [database user password host], if options are omitted extract from database in current Bokeh config
attach path/to/old/metadatas/file.php\n\n";
if (!$argv[1]
|| !in_array($argv[1], ['extract', 'attach'])) {
echo $usage;
exit(1);
}
class Artevod_Metadatas_Collection {
protected $_metas = [];
public function __construct($metas=[]) {
$this->_metas = $metas;
}
public function add($key, $value) {
$this->_metas[$key] = $value;
}
public function get($key, $default=null) {
return $this->has($key)
? $this->_metas[$key]
: $default;
}
public function has($key) {
return array_key_exists($key, $this->_metas);
}
public function asArray() {
return $this->_metas;
}
public function count() {
return count($this->_metas);
}
}
class Artevod_Metadatas {
use Trait_MemoryCleaner;
protected
$_db,
$_db_name;
public function extractFrom($db) {
$this->_db = $db;
$this->_initDbName();
echo "starting extraction from " . $this->_db_name . "\n";
$collection = $this->_extractFromCurrentDb();
echo "\n";
$path = __DIR__ . '/artevod_metas_' . $this->_db_name . '.php';
file_put_contents($path,
'<?php return ' . var_export($collection->asArray(), true) . ';');
echo "Wrote " . $collection->count() . " metas in " . $path . "\n";
}
protected function _extractFromCurrentDb() {
$collection = new Artevod_Metadatas_Collection();
$albums = $this->_query('select id, notes, tags from album where type_doc_id=104');
while ($album = $albums->fetch_assoc())
$this->_extractAlbumIn($album, $collection);
return $collection;
}
protected function _extractAlbumIn($album, $collection) {
$url = $this->_extractAlbumUrl($album);
$parts = explode('/', parse_url($url, PHP_URL_PATH));
if (!$key = end($parts)) {
echo "No key found for album " . $album['id'] . "\n";
return;
}
if ($collection->has($key)) {
echo "Duplicate key ". $key ." found for album " . $album['id'] . "\n";
return;
}
$collection->add($key, $album['tags']);
echo '.';
}
protected function _extractAlbumUrl($album) {
$marc = new Class_Album_Marc($album['notes']);
$url = '';
foreach($marc->getDatasOfField('856') as $field) {
if (array_key_exists('x', $field) && 'external_uri' == $field['x']) {
$url = $field['a'];
break;
}
}
return $url;
}
protected function _initDbName() {
$result = $this->_query('select database() as name');
$this->_db_name = $result->fetch_assoc()['name'];
}
protected function _query($sql) {
return $this->_db->query($sql);
}
public function attach($old_metas) {
$this->_db = Zend_Db_Table::getDefaultAdapter()->getConnection();
$this->_initDbName();
$old_metas = new Artevod_Metadatas_Collection($old_metas);
echo "attaching " . $old_metas->count() . " old metadatas into " . $this->_db_name . "\n";
$current_metas = $this->_extractFromCurrentDb();
echo "currently " . $current_metas->count() . " metadatas, starting comparison...\n";
}
}
$artevod_metas = new Artevod_Metadatas();
if ('extract' == $argv[1]) {
$db = connect($argc, $argv);
$artevod_metas->extractFrom($db);
exit(0);
}
if ('attach' == $argv[1]) {
$old_metas = loadMetasFile($argc, $argv);
$artevod_metas->attach($old_metas);
exit(0);
}
function loadMetasFile($argc, $argv) {
if (3 > $argc) {
echo "attach command needs path to extracted metadatas php file\n";
exit(1);
}
if (!is_readable($argv[2])) {
echo "cannot read from file " . $argv[2] . "\n";
exit(1);
}
return require $argv[2];
}
function connect($argc, $argv) {
if (2 == $argc) {
echo "using current Bokeh database\n";
return Zend_Db_Table::getDefaultAdapter()->getConnection();
}
if (5 > $argc) {
echo "extract command takes no options to extract from current database or at least database, user and password\n";
exit(1);
}
$host = isset($argv[5]) ? $argv[5] : 'localhost';
$db = new mysqli($host, $argv[3], $argv[4], $argv[2]);
if ($db->connect_errno) {
echo 'Failed to connect SQL to ' . $host . ' : ' . $db->connect_error . "\n";
exit(1);
}
return $db;
}
\ No newline at end of file
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