Commit 1e065005 authored by Patrick Barroca's avatar Patrick Barroca 😁
Browse files

Merge branch...

Merge branch 'dev#64852_contractuel_migration_des_avis_pod_bokeh_caluire_et_cuire_et_seyssinet_pariset' into 'master'

Dev#64852 contractuel migration des avis pod bokeh caluire et cuire et seyssinet pariset

See merge request !2426
parents 265b14b9 8c247e7e
- ticket #64852 : Ajout d'un script d'import des avis depuis le POD d'Archimed
\ No newline at end of file
<?php
/**
* Import record comments from POD. Require a CSV export.
* Usage: php scripts/import_pod_ratings.php ~/avis.out someone
*/
require(__DIR__.'/../console.php');
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 $user_card
? Class_Users::findFirstBy(['idabon' => $user_card])
: null;
}
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)
->setStatut(1);
}
function importComment($data, $default_user) {
$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)) {
$this->_log->userNotFound($comment_id, $user_card);
$user = $default_user;
}
$comment = $this->createComment($record, $user, trim($comment_title), trim($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(1, $comment->getStatut(), 'Anka status');
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');
}
function exitWithError($message) {
echo $message;
echo "\n\n";
exit(1);
}
if (3 > count($argv)) {
echo <<<USAGE
Usage:
php scripts/import_pod_ratings [PATH TO EXPORT] [ACCOUNT LOGIN FOR ORPHANED RATINGS]
Example:
php scripts/import_pod_ratings ~/avis.out admin
USAGE;
exit(1);
}
$filename = $argv[1];
if (false === ($handle = fopen($filename, "r")))
exitWithError('Could not open ' . $filename);
$login = $argv[2];
if (!$default_user = Class_Users::findFirstBy(['login' => $login]))
exitWithError('Could not find user: '. $login);
$pod_import = new PODCommentImport();
try {
fgetcsv($handle, 0, "\t"); // ignore first line
while (false !== ($data = fgetcsv($handle, 0, "\t"))) {
$pod_import->importComment($data, $default_user);
}
} 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
Markdown is supported
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