diff --git a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/IntegrationControllerTest.php b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/IntegrationControllerTest.php index ad48675d98bc7de4ff54c08c335dc632d7d7f8aa..69e264383bab559691cfc22c3ff285874f3e0eea 100644 --- a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/IntegrationControllerTest.php +++ b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/IntegrationControllerTest.php @@ -227,6 +227,13 @@ abstract class Cosmo_IntegrationControllerGenerateActionTestCase extends CosmoCo '6|Bacs imagiers', '7|Albums']) + ->whenCalled('file') + ->with('../ftp/transfert/library1/etalon/genres.txt') + ->answers(['BIB_GENRES|SUPPORT|CODE|LIBELLE|DOC', + '0|1|Album|f', + '0|2|Bande dessinée|f', + '0|3|Biographie|f']) + ->beStrict(); Class_Cosmogramme_LandingDirectory::setFileSystem($file_system); @@ -314,9 +321,9 @@ class Cosmo_IntegrationControllerGenerateActionNanookPostTest 'libelle' => 'Abonné Nanook', 'type_fichier' => 1]); - Class_Cosmogramme_Generator::setTimeSource($this->mock() - ->whenCalled('time')->answers(strtotime('2016-01-28')) - ->beStrict()); + $time_source = $this->mock()->whenCalled('time')->answers(strtotime('2016-01-28')); + Class_Cosmogramme_Generator::setTimeSource($time_source); + Class_Cosmogramme_Generator_AbstractTask::setTimeSource($time_source); $this->postDispatch('cosmo/integration/generate', ['path_ftp' => 'library1', @@ -329,6 +336,7 @@ class Cosmo_IntegrationControllerGenerateActionNanookPostTest public function tearDown() { Class_Cosmogramme_Generator::setTimeSource(null); + Class_Cosmogramme_Generator_AbstractTask::setTimeSource(null); parent::tearDown(); } @@ -513,4 +521,35 @@ class Cosmo_IntegrationControllerGenerateActionNanookPostTest $this->assertNotNull(Class_CodifEmplacement::findFirstBy(['libelle' => 'Coin des tout-petits', 'regles' => '995$6=4'])); } + + + /** @test */ + public function bdKindShouldBeCReated() { + $this->assertNotNull(Class_CodifGenre::findFirstBy(['libelle' => 'Biographie', + 'regles' => '995$7=3'])); + } + + + /** @test */ + public function shouldHaveCreated10Deweys() { + $this->assertEquals(10, Class_CodifDewey::count()); + } + + + /** @test */ + public function generaliteDeweyShouldHaveId0() { + $this->assertNotNull(Class_CodifDewey::findFirstBy(['id_dewey' => 0, 'libelle' => 'Généralités'])); + } + + + /** @test */ + public function histoireDeweyShouldHaveId9() { + $this->assertNotNull(Class_CodifDewey::findFirstBy(['id_dewey' => 9, 'libelle' => 'Histoire, géographie'])); + } + + + /** @test */ + public function shouldSayFinished() { + $this->assertXPathContentContains('//h2', 'Traitement terminé'); + } } \ No newline at end of file diff --git a/library/Class/CodifDewey.php b/library/Class/CodifDewey.php index 7c87cc69a31a7f47bac8bff19ff65b9ac9e7f922..c0e345541ff2d055ba8beba2442c398eda2fb14f 100644 --- a/library/Class/CodifDewey.php +++ b/library/Class/CodifDewey.php @@ -16,7 +16,7 @@ * * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE * along with BOKEH; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ class Class_CodifDeweyLoader extends Storm_Model_Loader { @@ -38,6 +38,7 @@ class Class_CodifDewey extends Storm_Model_Abstract { protected $_table_name = 'codif_dewey'; protected $_table_primary = 'id_dewey'; protected $_loader_class = 'Class_CodifDeweyLoader'; + protected $_fixed_id = true; // ---------------------------------------------------------------- // Rend une liste pour un champ suggestion @@ -46,24 +47,24 @@ class Class_CodifDewey extends Storm_Model_Abstract { // Lancer la recherche $new = ''; if($mode=="1") { - for($i=0; $i < strlen($recherche); $i++) + for($i=0; $i < strlen($recherche); $i++) if($recherche[$i] >="0" and $recherche[$i] <= "9") $new.=$recherche[$i]; $req="select id_dewey,libelle from codif_dewey where id_dewey like'".$new."%' order by id_dewey limit ".$limite_resultat; } if($mode=="2") $req="select id_dewey,libelle from codif_dewey where libelle like'".addslashes($recherche)."%' order by id_dewey limit ".$limite_resultat; if($mode=="3") $req="select id_dewey,libelle from codif_dewey where libelle like'%".addslashes($recherche)."%' order by id_dewey limit ".$limite_resultat; - + $resultat=fetchAll($req); - + // Mettre l'indice et le libelle if(!$resultat) return false; foreach($resultat as $enreg) { $liste[]=array($enreg["id_dewey"], $this->formatIndice($enreg["id_dewey"])." : ".$enreg["libelle"]); } - + return $liste; - } + } // ---------------------------------------------------------------- // Rend une liste d'indices par niveau @@ -71,7 +72,7 @@ class Class_CodifDewey extends Storm_Model_Abstract { static function getIndices($pere) { $sql = Zend_Registry::get('sql'); - if($pere == "root") + if($pere == "root") $liste=$sql->fetchAll("select * from codif_dewey where LENGTH(id_dewey)=1 order by id_dewey"); else { $long=strlen($pere)+1; @@ -80,7 +81,7 @@ class Class_CodifDewey extends Storm_Model_Abstract { } return $liste; } - + // ---------------------------------------------------------------- // Ponctue un indice dewey // ---------------------------------------------------------------- diff --git a/library/Class/Cosmogramme/Generator.php b/library/Class/Cosmogramme/Generator.php index 0847902932dc3762bf9000ed57b6bf4565f01984..d5a44705d7bfbff3e644fa5c4d8ae17e93d253f4 100644 --- a/library/Class/Cosmogramme/Generator.php +++ b/library/Class/Cosmogramme/Generator.php @@ -33,9 +33,15 @@ class Class_Cosmogramme_Generator { if (!$this->isValid()) return false; - return $this->_generateLibraries() + $result = $this->_generateLibraries() && $this->_generateSections() - && $this->_generateLocations(); + && $this->_generateLocations() + && $this->_generateKinds() + && $this->_generateDewey(); + + $this->log('<br><h2>'. $this->_('Traitement terminé.') .'</h2>'); + + return $result; } @@ -82,9 +88,11 @@ class Class_Cosmogramme_Generator { protected function _generateLibrariesEntities($libraries) { $this->logTitle($this->_('1 - Création des bibliothèques')); + $html = ''; foreach ($libraries as $data) $html .= $this->_generateLibrary($data); + $this->log('<div class="liste"><table class="blank">' . $html . '</table></div>'); } @@ -158,9 +166,11 @@ class Class_Cosmogramme_Generator { protected function _generatePlannedIntegrations($libraries) { $this->logTitle($this->_('3 - Programmation des intégrations')); + $html = ''; foreach ($libraries as $data) $html .= $this->_generatePlannedIntegration($data); + $this->log('<div class="liste"><table class="blank">' . $html . '</table></div>'); } @@ -183,122 +193,73 @@ class Class_Cosmogramme_Generator { } - protected function logTitle($title) { - $this->log('<h3>' . $title . '</h3>'); + protected function _generateSections() { + $sections = $this->_landing_directory->getSectionsOf($this->_params['path_ftp']); + return (new Class_Cosmogramme_Generator_SectionsTask($this))->run($sections); } - protected function _extractLibraryData($data) { - $data = utf8_encode($data); - $elem = explode('|', $data); + protected function _generateLocations() { + $locations = $this->_landing_directory->getLocationsOf($this->_params['path_ftp']); + return (new Class_Cosmogramme_Generator_LocationsTask($this))->run($locations); + } - return ($elem[0] == 'BIB_SPS_UTT') ? [] : $elem; + + protected function _generateKinds() { + $kinds = $this->_landing_directory->getKindsOf($this->_params['path_ftp']); + return (new Class_Cosmogramme_Generator_KindsTask($this))->run($kinds); } - protected function _generateSections() { - if (!$sections = $this->_landing_directory->getSectionsOf($this->_params['path_ftp'])) { - $this->log($this->_('Étalon des sections vide')); - return false; - } + protected function _generateDewey() { + $this->logTitle($this->_('7 - Création des classes Dewey')); - $this->logTitle($this->_('4 - Création des sections')); - $date = $this->getCurrentDate(); + Class_CodifDewey::deleteBy(['id_dewey' => range(0, 9)]); + + $map = ['Généralités', + 'Philosophie et disciplines connexes', + 'Religion', + 'Sciences sociales', + 'Langues', + 'Sciences de la nature et mathématiques', + 'Technique (sciences appliquées)', + 'Arts', + 'Littérature (Belles-lettres)', + 'Histoire, géographie']; $html = ''; - foreach($sections as $data) - $html .= $this->_generateSection($data, $date); + foreach($map as $k => $v) { + Class_CodifDewey::newInstance(['id_dewey' => $k, 'libelle' => $v]) + ->save(); + $html .= '<tr><td class="blank">'. $k .'</td><td class="blank">'. $v . '</td></tr>'; + } $this->log('<div class="liste"><table class="blank">' . $html . '</table></div>'); - $this - ->_directQuery('delete from codif_section where date_maj != "' . $date. '"') - ->_directQuery('update codif_section set date_maj=""'); - return true; } - protected function _generateSection($data, $date) { - $data = utf8_encode($data); - $elems = explode('|', $data); - if ('BIB_C_SECTION' == $elems[0] || !trim($elems[0])) - return ''; - - $label = trim($elems[1]); - if (!$section = Class_CodifSection::findFirstBy(['libelle' => $label])) - $section = Class_CodifSection::newInstance(['libelle' => $label, - 'date_maj' => $date, - 'regles' => $this->_getSectionRuleFor($elems[0])]); - - if (!$section->isNew() && $section->getDateMaj() == $date) - $section->setRegles($section->getRegles() . ';' . strtolower($elems[0])); - - $section->save(); - - return '<tr><td class="blank">' . $elems[0] . '</td><td class="blank">' . $elems[1] . '</td></tr>'; + public function isPergame() { + return Class_IntBib::SIGB_PERGAME == $this->_params['type_sigb']; } - protected function _getSectionRuleFor($code) { - $field = null; - if (Class_IntBib::SIGB_NANOOK == $this->_params['type_sigb']) - $field = '9'; - if (Class_IntBib::SIGB_PERGAME == $this->_params['type_sigb']) - $field = 'q'; - - if ($field) - return '995$' . $field . '=' . strtolower($code); + public function isNanook() { + return Class_IntBib::SIGB_NANOOK == $this->_params['type_sigb']; } - protected function _generateLocations() { - if (!$locations = $this->_landing_directory->getLocationsOf($this->_params['path_ftp'])) { - $this->log($this->_('Étalon des emplacements vide')); - return false; - } - - $this->logTitle($this->_('5 - Création des emplacements')); - $date = $this->getCurrentDate(); - - $html = ''; - foreach($locations as $data) - $html .= $this->_generateLocation($data, $date); - - $this->log('<div class="liste"><table class="blank">' . $html . '</table></div>'); - - $this - ->_directQuery('delete from codif_emplacement where date_maj !="' . $date . '"') - ->_directQuery('update codif_emplacement set date_maj =""'); - - return true; + protected function logTitle($title) { + $this->log('<h3>' . $title . '</h3>'); } - protected function _generateLocation($data, $date) { + protected function _extractLibraryData($data) { $data = utf8_encode($data); - $elems = explode('|', $data); - if ('BIB_' == substr($elems[0], 0, 4) || !trim($elems[0])) - return ''; - - $label = trim($elems[1]); - if (!$location = Class_CodifEmplacement::findFirstBy(['libelle' => $label])) - $location = Class_CodifEmplacement::newInstance(['libelle' => $label, - 'date_maj' => $date, - 'regles' => '995$6=' . strtolower($elems[0])]); - - if (!$location->isNew() && $location->getDateMaj() == $date) - $location->setRegles($location->getRegles() . ';' . strtolower($elems[0])); - - $location->save(); - - return '<tr><td class="blank">' . $elems[0] . '</td><td class="blank">' . $elems[1] . '</td></tr>'; - } - + $elem = explode('|', $data); - protected function _directQuery($sql) { - Zend_Db_Table::getDefaultAdapter()->query($sql); - return $this; + return ($elem[0] == 'BIB_SPS_UTT') ? [] : $elem; } } diff --git a/library/Class/Cosmogramme/Generator/AbstractTask.php b/library/Class/Cosmogramme/Generator/AbstractTask.php new file mode 100644 index 0000000000000000000000000000000000000000..f3b2730a43a0f586a927d88fbbf54c9057ed001d --- /dev/null +++ b/library/Class/Cosmogramme/Generator/AbstractTask.php @@ -0,0 +1,126 @@ +<?php +/** + * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +abstract class Class_Cosmogramme_Generator_AbstractTask { + use Trait_Translator, Trait_Loggable, Trait_TimeSource; + + protected $_generator; + + public function __construct($generator) { + $this->_generator = $generator; + } + + + public function run($datas) { + if (!$datas) { + $this->log($this->_('Étalon des %s vide', $this->_name)); + return false; + } + + $this->logTitle($this->_('%d - Création des %s', $this->_id, $this->_name)); + $date = $this->getCurrentDate(); + + $datas = $this->_prepare($datas); + $html = ''; + foreach($datas as $data) + $html .= $this->_runOne($data, $date); + + $this->log('<div class="liste"><table class="blank">' . $html . '</table></div>'); + + $this + ->_directQuery('delete from ' . $this->_table . ' where date_maj != "' . $date. '"') + ->_directQuery('update ' . $this->_table . ' set date_maj=""'); + + return true; + } + + + protected function _runOne($data, $date) { + $data = utf8_encode($data); + $elems = explode('|', $data); + + if ($this->_isExcluded($elems)) + return ''; + + $label = trim($elems[1]); + $code = strtolower($elems[0]); + + $model_class = $this->_model_class; + if (!$model = $model_class::findFirstBy(['libelle' => $label])) + $model = $model_class::newInstance(['libelle' => $label]); + + $model + ->setRegles($this->_createOrConcatRules($code, $model, $date)) + ->setDateMaj($date) + ->save(); + + return $this->renderOne($code, $label); + } + + + protected function _isExcluded($elems) { + return false; + } + + + protected function _directQuery($sql) { + Zend_Db_Table::getDefaultAdapter()->query($sql); + return $this; + } + + + protected function logTitle($title) { + $this->log('<h3>' . $title . '</h3>'); + } + + + protected function renderOne($code, $label) { + return '<tr><td class="blank">' . $code . '</td><td class="blank">' . $label . '</td></tr>'; + } + + + protected function _createOrConcatRules($code, $model, $date) { + return !$model->isNew() && $model->getDateMaj() == $date + ? $model->getRegles() . ';' . $code + : $this->_getRuleFor($code); + } + + + protected function _getRuleFor($code) { + return ''; + } + + + protected function _prepare($datas) { + return $datas; + } + + + public function isPergame() { + return $this->_generator->isPergame(); + } + + + public function isNanook() { + return $this->_generator->isNanook(); + } +} \ No newline at end of file diff --git a/library/Class/Cosmogramme/Generator/KindsTask.php b/library/Class/Cosmogramme/Generator/KindsTask.php new file mode 100644 index 0000000000000000000000000000000000000000..355a96b11f0ec808c608d938957bebd5cc55c254 --- /dev/null +++ b/library/Class/Cosmogramme/Generator/KindsTask.php @@ -0,0 +1,83 @@ +<?php +/** + * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +class Class_Cosmogramme_Generator_KindsTask extends Class_Cosmogramme_Generator_AbstractTask { + protected + $_index = 6, + $_name = 'genres', + $_table = 'codif_genre'; + + + protected function _prepare($datas) { + $this->_kinds = []; + foreach($datas as $data) + $this->_prepareOne($data); + + return $this->_kinds; + } + + + protected function _prepareOne($data) { + $data = utf8_encode($data); + $elems = explode('|', $data); + if ('BIB_' == substr($elems[0], 0, 4) || !trim($elems[1])) + return ''; + + $key = Class_Indexation::getInstance()->alphaMaj($elems[2]); + $code = trim($elems[1]); + $label = trim($elems[2]); + + if (array_key_exists($key, $this->_kinds)) { + $this->_kinds[$key]['codes'][] = $code; + return; + } + + $this->_kinds[$key] = ['codes' => [$code], 'label' => $label]; + } + + + protected function _runOne($data, $date) { + $label = $data['label']; + $codes = implode(';', $data['codes']); + + if (!$model = Class_CodifGenre::findFirstBy(['libelle' => $label])) + $model = Class_CodifGenre::newInstance(['libelle' => $label]); + + $model + ->setRegles($this->_getRuleFor($codes)) + ->setDateMaj($date) + ->save(); + + return $this->renderOne($codes, $label); + } + + + protected function _getRuleFor($codes) { + $field = null; + if ($this->isNanook()) + $field = '995$7'; + if ($this->isPergame()) + $field = '930$4'; + + if ($field) + return $field . '=' . $codes; + } +} diff --git a/library/Class/Cosmogramme/Generator/LocationsTask.php b/library/Class/Cosmogramme/Generator/LocationsTask.php new file mode 100644 index 0000000000000000000000000000000000000000..90512c8173dfdbc1298331405bf07a7ec7b4c1af --- /dev/null +++ b/library/Class/Cosmogramme/Generator/LocationsTask.php @@ -0,0 +1,38 @@ +<?php +/** + * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +class Class_Cosmogramme_Generator_LocationsTask extends Class_Cosmogramme_Generator_AbstractTask { + protected + $_index = 5, + $_name = 'emplacements', + $_table = 'codif_emplacement', + $_model_class = 'Class_CodifEmplacement'; + + + protected function _isExcluded($elems) { + return 'BIB_' == substr($elems[0], 0, 4) || !trim($elems[0]); + } + + + protected function _getRuleFor($code) { + return '995$6=' . $code; + } +} diff --git a/library/Class/Cosmogramme/Generator/SectionsTask.php b/library/Class/Cosmogramme/Generator/SectionsTask.php new file mode 100644 index 0000000000000000000000000000000000000000..97b53c0ef2e8fbc356abd7b2189fec96f5f8beeb --- /dev/null +++ b/library/Class/Cosmogramme/Generator/SectionsTask.php @@ -0,0 +1,46 @@ +<?php +/** + * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class Class_Cosmogramme_Generator_SectionsTask extends Class_Cosmogramme_Generator_AbstractTask { + protected + $_index = 4, + $_name = 'sections', + $_table = 'codif_section', + $_model_class = 'Class_CodifSection'; + + + protected function _isExcluded($elems) { + return 'BIB_C_SECTION' == $elems[0] || !trim($elems[0]); + } + + + protected function _getRuleFor($code) { + $field = null; + if ($this->isNanook()) + $field = '9'; + if ($this->isPergame()) + $field = 'q'; + + if ($field) + return '995$' . $field . '=' . strtolower($code); + } +} \ No newline at end of file diff --git a/library/Class/Cosmogramme/LandingDirectory.php b/library/Class/Cosmogramme/LandingDirectory.php index 4b508f2b8edf838e9865168746481264d322e116..798c55efee2631ffe36012c8579f7a808ba8785d 100644 --- a/library/Class/Cosmogramme/LandingDirectory.php +++ b/library/Class/Cosmogramme/LandingDirectory.php @@ -95,19 +95,27 @@ class Class_Cosmogramme_LandingDirectory { public function getLibrariesOf($subdir) { - $file = $this->_getStandardFilePath($subdir, $this->_required_files['libraries']); - return $this->getFileSystem()->file($file); + return $this->getFileNamed('libraries', $subdir); } public function getSectionsOf($subdir) { - $file = $this->_getStandardFilePath($subdir, $this->_required_files['sections']); - return $this->getFileSystem()->file($file); + return $this->getFileNamed('sections', $subdir); } public function getLocationsOf($subdir) { - $file = $this->_getStandardFilePath($subdir, $this->_required_files['locations']); + return $this->getFileNamed('locations', $subdir); + } + + + public function getKindsOf($subdir) { + return $this->getFileNamed('kinds', $subdir); + } + + + protected function getFileNamed($name, $subdir) { + $file = $this->_getStandardFilePath($subdir, $this->_required_files[$name]); return $this->getFileSystem()->file($file); }