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', 'port' => '8983',
'username' => '', 'username' => '',
'password' => '', 'password' => '',
'path' => 'solr/bsr1',
'result_count' => 10, 'result_count' => 10,
), ),
'log' => array( 'log' => array(

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

@ -247,6 +247,34 @@ class NetBiblio extends WebService
return json_decode($json, true); 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) private function AddBookData(array $books)
{ {
if(isset($books['code'])) { if(isset($books['code'])) {
@ -258,21 +286,14 @@ class NetBiblio extends WebService
$books = array_map(function($b) { $books = array_map(function($b) {
// add files if we already have them // add files if we already have them
$files = array(); $files = array();
$samples = array(); if(isset($b['samples']) && count($b['zip']) > 0) {
foreach(array('mp3', 'ogg') as $ext) { $files['samples'] = $b['samples'];
$k = 'sample_'.$ext;
if(isset($b[$k]) && strlen($b[$k]) > 0) {
$samples[] = $b[$k];
}
unset($b[$k]);
}
if(! empty($samples)) {
$files['samples'] = $samples;
} }
unset($b['samples']);
if(isset($b['zip']) && strlen($b['zip']) > 0) { if(isset($b['zip']) && strlen($b['zip']) > 0) {
$files['zip'] = $b['zip']; $files['zip'] = $b['zip'];
unset($b['zip']);
} }
unset($b['zip']);
if(! empty($files)) { if(! empty($files)) {
$b['files'] = $files; $b['files'] = $files;
@ -286,22 +307,35 @@ class NetBiblio extends WebService
return $b; return $b;
}, $books); }, $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) { $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); $ids = array_map(function($b) { return $b['code']; }, $booksWithoutFiles);
$files = $this->GetFiles($ids); $files = $this->GetFiles($ids);
if(count($files) > 0) {
foreach($booksWithoutFiles as $k => $b) { foreach($booksWithoutFiles as $k => $b) {
if(isset($files[$b['code']])) { if(isset($files[$b['code']])) {
$books[$k]['files'] = $files[$b['code']]; $books[$k]['files'] = $files[$b['code']];
$files[$b['code']]['id'] = $b['id'];
} else { } else {
// we need to have an empty array for mobile apps compatibility. // we need to have an empty array for mobile apps compatibility.
$books[$k]['files'] = array(); $books[$k]['files'] = array();
} }
} }
}
$this->SetFiles($files);
}
return $books; return $books;
} }

Loading…
Cancel
Save