save files information in Solr upon retrieving them

master
Gilles Crettenand 11 years ago
parent b611de131e
commit 866ec8bacb

@ -30,6 +30,7 @@ class Configuration {
'port' => '8983',
'username' => '',
'password' => '',
'path' => 'solr/bsr1',
'result_count' => 10,
),
'log' => array(

@ -18,7 +18,7 @@ class BookSearch
'port' => Configuration::get('solr.port'),
'login' => Configuration::get('solr.username'),
'password' => Configuration::get('solr.password'),
'path' => 'solr/bsr1',
'path' => Configuration::get('solr.path'),
);
$this->client = new SolrClient($options);

@ -247,6 +247,34 @@ class NetBiblio extends WebService
return json_decode($json, true);
}
private function SetFiles($files) {
$json = json_encode(array_values(array_map(function($f) {
return array(
'id' => $f['id'],
'samples' => array('set' => isset($f['samples']) ? $f['samples'] : array()),
// for now, we don't know how to save the zip without the hash
'zip' => '', // array('set' => isset($f['zip']['uri']) ? $f['zip']['uri'] : ''),
);
}, $files)));
$uri = sprintf('%s:%s/%s/update?commitWithin=500',
Configuration::get('solr.server'),
Configuration::get('solr.port'),
Configuration::get('solr.path')
);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
));
curl_exec($ch);
curl_close($ch);
}
private function AddBookData(array $books)
{
if(isset($books['code'])) {
@ -258,21 +286,14 @@ class NetBiblio extends WebService
$books = array_map(function($b) {
// add files if we already have them
$files = array();
$samples = array();
foreach(array('mp3', 'ogg') as $ext) {
$k = 'sample_'.$ext;
if(isset($b[$k]) && strlen($b[$k]) > 0) {
$samples[] = $b[$k];
}
unset($b[$k]);
}
if(! empty($samples)) {
$files['samples'] = $samples;
if(isset($b['samples']) && count($b['zip']) > 0) {
$files['samples'] = $b['samples'];
}
unset($b['samples']);
if(isset($b['zip']) && strlen($b['zip']) > 0) {
$files['zip'] = $b['zip'];
unset($b['zip']);
}
unset($b['zip']);
if(! empty($files)) {
$b['files'] = $files;
@ -286,22 +307,35 @@ class NetBiblio extends WebService
return $b;
}, $books);
// retrieve files information for the book that don't have any at this time
// retrieve files information for the book that don't have all of them at this time
$booksWithoutFiles = array_filter($books, function($b) {
return ! isset($b['files']);
return ! (
isset($b['files']) &&
isset($b['files']['samples']) &&
count($b['files']['samples']) == 2 && // we want two samples (mp3 and ogg)
(strlen($this->login) == 0 || isset($b['files']['zip'])) // we want a zip file only for logged in people
);
});
if(count($booksWithoutFiles) > 0) {
$ids = array_map(function($b) { return $b['code']; }, $booksWithoutFiles);
$files = $this->GetFiles($ids);
if(count($files) > 0) {
foreach($booksWithoutFiles as $k => $b) {
if(isset($files[$b['code']])) {
$books[$k]['files'] = $files[$b['code']];
$files[$b['code']]['id'] = $b['id'];
} else {
// we need to have an empty array for mobile apps compatibility.
$books[$k]['files'] = array();
}
}
}
$this->SetFiles($files);
}
return $books;
}

Loading…
Cancel
Save