Skip to content
Snippets Groups Projects
Commit 7f9d380b authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

dev #72435 : imcomplete authorities from relations are completed

parent 50700419
Branches
Tags
2 merge requests!2894Dev#72435 3 explo autorite etape 1 minsoc09 communaute importer un fichier autorite,!2874Dev#72435 3 explo autorite etape 1 minsoc09 communaute importer un fichier autorite
Pipeline #4615 failed with stage
in 44 minutes and 55 seconds
......@@ -21,6 +21,8 @@
class Class_Cosmogramme_Integration_Record_Authority {
use Trait_TimeSource;
const
RECORD_REJECT = 0,
RECORD_INSERT = 1;
......@@ -37,10 +39,6 @@ class Class_Cosmogramme_Integration_Record_Authority {
$this->_indexation = Class_Indexation::getInstance();
}
protected function _encoding() {
return $this->_integration->getProfilDonnees()->getAccents();
}
public function import($data) {
$this->_reader->setNotice($data, $this->_encoding());
......@@ -62,59 +60,79 @@ class Class_Cosmogramme_Integration_Record_Authority {
'type_doc' => $this->_getAuthorityType(),
'titres' => $this->_indexation->getFulltext($this->_addRejectsTo($title)),
'alpha_titre' => $this->_indexation->codeAlphaTitre($title),
'annee' => $this->_getCreationYear()];
$facets = [];
$relations = $this->_reader->get_subfield('550');
foreach($relations as $relation) {
$remote_id = $remote_type = $remote_label = null;
foreach($this->_reader->decoupe_bloc_champ($relation) as $pair) {
if ('3' == $pair['code'])
$remote_id = $pair['valeur'];
if ('5' == $pair['code'])
$remote_type = $pair['valeur'];
if ('a' == $pair['code'])
$remote_label = $pair['valeur'];
}
if (!$remote_id || !$remote_label)
continue;
$remote_type = $remote_type ? $remote_type : 'z';
$this->_insert($remote_id,
['type' => Class_Notice::TYPE_AUTHORITY,
'titres' => $remote_label,
'alpha_titre' => $this->_indexation->codeAlphaTitre($remote_label)]);
$facets[] = $remote_type.$remote_id;
}
'annee' => $this->_getCreationYear(),
'facettes' => implode(' ', $this->_getFacets())];
$record['facettes'] = implode(' ', $facets);
$this->_insert($id_origine, $record);
$this->_save($id_origine, $record);
$this->_statut = static::RECORD_INSERT;
}
protected function _insert($id_origine, $record) {
protected function _encoding() {
return $this->_integration->getProfilDonnees()->getAccents();
}
protected function _getFacets() {
return array_filter(array_map([$this, '_facetFromRelated'],
$this->_reader->get_subfield('550')));
}
protected function _facetFromRelated($relation) {
$relation = $this->_reader->decoupe_bloc_champ($relation);
$relation = new Class_Cosmogramme_Integration_Record_AuthorityRelation($relation);
if (!$relation->isValid())
return;
$this->_save($relation->id(),
['type' => Class_Notice::TYPE_AUTHORITY,
'titres' => $this->_indexation->getFulltext($relation->label()),
'alpha_titre' => $this->_indexation->codeAlphaTitre($relation->label())]);
return $relation->facet();
}
protected function _save($id_origine, $record) {
$attribs = ['type' => Class_Notice::TYPE_AUTHORITY,
'id_origine' => $id_origine,
'id_int_bib' => $this->_getIdBib()];
if ($item = Class_Exemplaire::findFirstBy($attribs)) {
$item->setToDelete(false)->save();
return;
}
($item = Class_Exemplaire::findFirstBy($attribs))
? $this->_update($item, $record)
: $this->_insert($attribs, $record);
}
protected function _insert($item, $record) {
$record = Class_Notice::newInstance($record);
$record->save();
Class_Exemplaire::newInstance($attribs)
$record
->setDateMaj($this->getTimeSource()->dateDayAndHours())
->save();
Class_Exemplaire::newInstance($item)
->setNotice($record)
->save();
}
protected function _update($item, $record) {
$item->setToDelete(false)->save();
if (!$existing_record = $item->getNotice())
return;
if (array_key_exists('unimarc', $record)
&& '' != $record['unimarc'])
$existing_record->updateAttributes($record);
$existing_record
->setDateMaj($this->getTimeSource()->dateDayAndHours());
$existing_record->save();
}
protected function _addRejectsTo($title) {
$parts = [$title];
......@@ -157,3 +175,51 @@ class Class_Cosmogramme_Integration_Record_Authority {
return $this->_integration->getIdBib();
}
}
class Class_Cosmogramme_Integration_Record_AuthorityRelation {
const
FIELD_ID = '3',
FIELD_TYPE = '5',
FIELD_LABEL = 'a';
protected
$_id,
$_type = 'z',
$_label;
public function __construct($datas) {
if (!$datas)
return;
$map = [static::FIELD_ID => '_id',
static::FIELD_TYPE => '_type',
static::FIELD_LABEL => '_label'];
foreach($datas as $pair)
if (array_key_exists($pair['code'], $map))
$this->{$map[$pair['code']]} = $pair['valeur'];
}
public function isValid() {
return $this->_id && $this->_label;
}
public function id() {
return $this->_id;
}
public function label() {
return $this->_label;
}
public function facet() {
return $this->_type . $this->_id;
}
}
\ No newline at end of file
......@@ -74,6 +74,14 @@ class PhaseAuthorityTest extends Class_Cosmogramme_Integration_PhaseTestCase {
}
protected function _recordWithIdOrigine($id) {
return ($item = Class_Exemplaire::findFirstBy(['type' => Class_Notice::TYPE_AUTHORITY,
'id_origine' => $id]))
? $item->getNotice()
: null;
}
/** @test */
public function newPhaseShouldBe0() {
$this->assertTrue($this->_phase->isId(0));
......@@ -88,26 +96,28 @@ class PhaseAuthorityTest extends Class_Cosmogramme_Integration_PhaseTestCase {
/** @test */
public function amenagementDuTerritoireShouldExists() {
$this->assertNotNull($item = Class_Exemplaire::findFirstBy(['type' => Class_Notice::TYPE_AUTHORITY,
'id_origine' => '185349']));
$record = $item->getNotice();
$this->assertNotNull($record = $this->_recordWithIdOrigine('185349'));
$this->assertEquals('185349', $record->getExemplaires()[0]->getIdOrigine());
$this->assertContains('g192156 ', $record->getFacettes());
}
/** @test */
public function cadreEtMilieuDeVieShouldExists() {
$this->assertNotNull(Class_Exemplaire::findFirstBy(['type' => Class_Notice::TYPE_AUTHORITY,
'id_origine' => '192156']));
$this->assertNotNull($this->_recordWithIdOrigine('192156'));
}
/** @test */
public function plieShouldIndexRejects() {
$this->assertContains('PLAN LOCAL',
Class_Notice::findFirstBy(['alpha_titre' => 'PLIE'])->getTitres());
$record = $this->_recordWithIdOrigine('192516');
$this->assertContains('PLAN LOCAL', $record->getTitres());
}
/** @test */
public function assuranceMaladieShouldHaveUnimarc() {
$record = $this->_recordWithIdOrigine('185351');
$this->assertContains('CMU', $record->get_subfield('550', 'a'));
}
}
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