Commit 8c247e7e authored by Laurent's avatar Laurent Committed by Patrick Barroca
Browse files

dev #64852 POD import: tune script

parent 63d9da75
<?php
"Import record comments from POD. Require a CSV export.
Usage: php scripts/import_pod_ratings.php ~/avis.out"
/**
* Import record comments from POD. Require a CSV export.
* Usage: php scripts/import_pod_ratings.php ~/avis.out someone
*/
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 {
......@@ -87,7 +78,9 @@ class PODCommentImport {
function findUser($user_card) {
return Class_Users::findFirstBy(['idabon' => $user_card]);
return $user_card
? Class_Users::findFirstBy(['idabon' => $user_card])
: null;
}
......@@ -102,11 +95,12 @@ class PODCommentImport {
->setEntete($head)
->setAvis(iconv('CP850', 'UTF-8', $comment_content))
->setNote($rating)
->setUser($user);
->setUser($user)
->setStatut(1);
}
function importComment($data) {
function importComment($data, $default_user) {
$comment_id = $data[0];
$isbn = $data[2];
$barcode = $data[6];
......@@ -119,10 +113,12 @@ class PODCommentImport {
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);
if (!$user = $this->findUser($user_card)) {
$this->_log->userNotFound($comment_id, $user_card);
$user = $default_user;
}
$comment = $this->createComment($record, $user, $comment_title, $comment_content, $rating, $creation_date);
$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()));
......@@ -175,6 +171,8 @@ function runTests() {
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');
......@@ -189,12 +187,43 @@ function runTests() {
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);
$pod_import->importComment($data, $default_user);
}
} finally {
fclose($handle);
......
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