diff --git a/VERSIONS_WIP/64852 b/VERSIONS_WIP/64852 new file mode 100644 index 0000000000000000000000000000000000000000..cb8d057705714720f26c0440024496fec21087b4 --- /dev/null +++ b/VERSIONS_WIP/64852 @@ -0,0 +1 @@ + - ticket #64852 : Ajout d'un script d'import des avis depuis le POD d'Archimed \ No newline at end of file diff --git a/scripts/import_pod_ratings.php b/scripts/import_pod_ratings.php new file mode 100644 index 0000000000000000000000000000000000000000..5a5ce5c4b528130ced7bd55fbe606c41e83e40a7 --- /dev/null +++ b/scripts/import_pod_ratings.php @@ -0,0 +1,227 @@ +<?php +"Import record comments from POD. Require a CSV export. + Usage: php scripts/import_pod_ratings.php ~/avis.out" +require(__DIR__.'/../console.php'); + +function exitWithError($message) { + echo $message; + echo "\n\n"; + exit(1); +} + + +if (!isset($argv[1])) + exitWithError('File to import not provided'); + +$filename = $argv[1]; +if (false === ($handle = fopen($filename, "r"))) + exitWithError('Could not open ' . $filename); + + +class PODImportLog { + protected + $_stats = ['import_count' => 0, + 'invalid' => 0, + 'record_not_found' => 0, + 'user_not_found' => 0]; + + function printError($comment_id, $message) { + echo "\n" . '[ERR] ' . $comment_id . ': '. $message . "\n"; + } + + + function saveError($comment) { + echo "\n" . '[ERR] Comment: ' . $comment->getEntete() . ' Error: ' . implode('. ', $comment->getErrors()) . "\n"; + } + + + function commentInvalid($comment_id, $message) { + $this->_stats['invalid'] = $this->_stats['invalid'] + 1; + $this->printError($comment_id, 'Comment is invalid: ' . $message); + } + + + function recordNotFound($comment_id, $isbn, $barcode) { + $this->_stats['record_not_found'] = $this->_stats['record_not_found'] + 1; + $this->printError($comment_id, 'record not found. ISBN: ' . $isbn . ' CB: ' . $barcode); + } + + function userNotFound($comment_id, $user_card) { + $this->_stats['user_not_found'] = $this->_stats['user_not_found'] + 1; + $this->printError($comment_id, 'user not found: ' . $user_card); + } + + + function success() { + $this->_stats['import_count'] = $this->_stats['import_count'] + 1; + echo '.'; + } + + + function printStats() { + foreach($this->_stats as $label => $count) + echo "$label : $count\n"; + } +} + + + +class PODCommentImport { + protected + $_log, + $_unimarc, + $_comments = []; + + function __construct() { + $this->_unimarc = new Class_NoticeUnimarc_Writer(); + $this->_log = new PODImportLog(); + } + + + function findRecord($isbn, $barcode) { + if ($item = Class_Exemplaire::findFirstBy(['code_barres' => $barcode])) + return $item->getNotice(); + + return Class_Notice::findFirstBy(['isbn' => $isbn]); + } + + + function findUser($user_card) { + return Class_Users::findFirstBy(['idabon' => $user_card]); + } + + + function createComment($record, $user, $comment_title, $comment_content, $rating, $creation_date) { + $head = trim($comment_title) + ? iconv('CP850', 'UTF-8', $comment_title) + : $record->getTitrePrincipal(); + + return (new Class_AvisNotice()) + ->setClefOeuvre($record->getClefOeuvre()) + ->setDateAvis(implode('-', array_reverse(explode('/', $creation_date)))) + ->setEntete($head) + ->setAvis(iconv('CP850', 'UTF-8', $comment_content)) + ->setNote($rating) + ->setUser($user); + } + + + function importComment($data) { + $comment_id = $data[0]; + $isbn = $data[2]; + $barcode = $data[6]; + $user_card = $data[8]; + $comment_title = $data[10]; + $comment_content = $data[11]; + $creation_date = $data[12]; + $rating = $data[13]; + + if (!$record = $this->findRecord($isbn, $barcode)) + return $this->_log->recordNotFound($comment_id, $isbn, $barcode); + + if (!$user = $this->findUser($user_card)) + return $this->_log->userNotFound($comment_id, $user_card); + + $comment = $this->createComment($record, $user, $comment_title, $comment_content, $rating, $creation_date); + + if (!$comment->isValid()) + return $this->_log->commentInvalid($comment_id, implode(', ', $comment->getErrors())); + + $this->_comments[] = $comment; + $this->_log->success(); + } + + + function save() { + array_map([$this, 'saveComment'], $this->_comments); + } + + + function saveComment($comment) { + if ($comment->save()) + return $this->_log->success(); + + $this->_log->saveError($comment); + $this->dumpComment($comment); + } + + function dumpComment($comment) { + echo "\n\n"; + foreach($comment->getRawAttributes() as $attribute => $value) + echo $attribute . ': ' . $value . "\n"; + } + + + function printStats() { + $this->_log->printStats(); + } +} + + +function check($expected, $actual, $message) { + if ($expected === $actual) { + echo "\n[OK] " . $message .': ' . $actual . "\n"; + return; + } + + echo "\n[FAIL] " . $message . ': expected: ' . $expected . ' actual: ' . $actual . "\n"; +} + + +function runTests() { + $comment = Class_AvisNotice::findFirstBy(['clef_oeuvre' => 'ANKA--GUERAUDG-']); + + check('2012-11-03', $comment->getDateAvis(), 'Anka date'); + + check('Anka', $comment->getEntete(), 'Anka title'); + + check(5, (int)$comment->getNote(), 'Anka rating'); + + check(24694, (int)$comment->getUser()->getIdabon(), 'Anka user card'); + + check('Anka', $comment->getFirstNotice()->getTitrePrincipal(), 'Anka record title'); + + + $comment = Class_AvisNotice::findFirstBy(['clef_oeuvre' => 'AVENUEDESMYSTERES--IRVINGJ-']); + + check('Avenue des Mystères', $comment->getEntete(), 'Avenue des Mystères comment title'); +} + + + +$pod_import = new PODCommentImport(); + +try { + fgetcsv($handle, 0, "\t"); // ignore first line + while (false !== ($data = fgetcsv($handle, 0, "\t"))) { + $pod_import->importComment($data); + } +} finally { + fclose($handle); +} + +echo "\n\n"; +$pod_import->printStats(); + + +echo "\n\nApply import [A], Test import [T], Exit [default]: "; +$handle = fopen ("php://stdin","r"); +$line = fgets($handle); +fclose($handle); + + +if(trim($line) == 'T'){ + echo "\nTest import\n"; + + Class_AvisNotice::getLoader()->defaultToVolatile(); + $pod_import->save(); + runTests(); +} + +if(trim($line) == 'A'){ + echo "\nApply\n"; + $pod_import->save(); +} + +echo "\nBye bye\n"; +?> \ No newline at end of file