From 15f11721d98792c5e7d72c5f4df18d0d19f31575 Mon Sep 17 00:00:00 2001 From: llaffont <llaffont@afi-sa.fr> Date: Mon, 1 Dec 2014 18:13:03 +0100 Subject: [PATCH] dev #17990 sito tops 5 --- library/Class/Import/Typo3.php | 83 ++++++++++++++++++--- tests/library/Class/Import/Typo3Fixture.php | 23 ++++-- tests/library/Class/Import/Typo3Test.php | 27 ++++--- 3 files changed, 107 insertions(+), 26 deletions(-) diff --git a/library/Class/Import/Typo3.php b/library/Class/Import/Typo3.php index 451f6df5128..c9c47402dd2 100644 --- a/library/Class/Import/Typo3.php +++ b/library/Class/Import/Typo3.php @@ -21,8 +21,8 @@ class Class_Import_Typo3 { const DEFAULT_CAT_ID = 30000; - protected $user_mapper; - + protected + $user_mapper; public function __construct() { $this->t3db = new Typo3DB(); @@ -52,6 +52,8 @@ class Class_Import_Typo3 { public function run($what = 'all') { + $_SERVER['HTTP_HOST']='localhost'; + if ($what == 'all' || $what == 'users') { echo "Importing users\n"; $this->import_user(); @@ -61,17 +63,17 @@ class Class_Import_Typo3 { echo "Importing categories\n"; $this->import_categories(); - echo "Importing articles\n"; - $this->importArticles(); - echo "Importing articles pages\n"; $this->importArticlesPages(); + echo "Importing articles\n"; + $this->importArticles(); + echo "Importing events\n"; $this->importCalendar(); echo "Importing sites\n"; - $this->import_sites(); + $this->importSites(); } Class_Import_Typo3_Logs::getInstance()->report(); @@ -114,7 +116,7 @@ class Class_Import_Typo3 { } - public function import_sites() { + public function importSites() { $t3news = $this->t3db->findAllSites(); foreach($t3news as $new) { @@ -132,12 +134,30 @@ class Class_Import_Typo3 { continue; } - $this->sites_categories_map->setDomainsFor($element, - $this->t3db->findAllForeignUidForNewsCat($new['uid'])); + $foreign_uids = $this->t3db->findAllForeignUidForNewsCat($new['uid']); + $this->sites_categories_map->setDomainsFor($element, $foreign_uids); + + if (in_array(43, array_map(function($row){return $row['uid_foreign'];}, $foreign_uids))) { + $this->putSiteInTop5($element, $new); + } } } + public function putSiteInTop5($element, $t3record) { + $parts = $element->getCategorie()->getPathParts(); + $parts[0] = 'NOS TOPS 5'; + $path = '/' . implode('/', $parts); + + if ($title = $this->t3db->findPageTitle($t3record['pid'])) { + $path .= ('/' . $title); + } + + $site_top5_cat = $this->sites_categories_map->findOrCreateByPath($path); + $element->setCategorie($site_top5_cat)->save(); + } + + public function manipulate_body($body) { $body = $this->wrapWithHtml($body); $body = $this->translateLinksToAnchor($body); @@ -190,7 +210,6 @@ class Class_Import_Typo3 { public function importArticles() { - $_SERVER['HTTP_HOST']='localhost'; $t3news = $this->t3db->findAllArticles(); foreach($t3news as $new) { $date_creation = date("Y-m-d H:i:s", $new['crdate']); @@ -314,6 +333,7 @@ class Class_Import_Typo3 { $this->errors[] = 'pages with uid: ' . $new['uid'] . ' (' . $new['title'] . ') - ' . implode(', ', $element->getErrors()); continue; } + $this->report['pages_created']++; } return $this; @@ -560,23 +580,64 @@ class SiteCategoriesMap extends CategoriesMap { protected function incrementSaved() { Class_Import_Typo3_Logs::getInstance()->incrementSitoCategoriesSaved(); } + + + + public function findOrCreateByPath($path) { + if ($path == Trait_TreeNode::$PATH_SEPARATOR) + return null; + + $parts = array_values(array_filter(explode(Trait_TreeNode::$PATH_SEPARATOR, $path))); + + $category = null; + foreach($parts as $part) { + if (!$sub_category = Class_SitothequeCategorie::findFirstBy(['libelle' => $part, + 'id_cat_mere' => $category ? $category->getId() : 0])) { + $sub_category = Class_SitothequeCategorie::newInstance(['libelle' => $part, + 'id_cat_mere' => $category ? $category->getId() : 0]); + $sub_category->save(); + } + $category = $sub_category; + } + + return $category; + } } + + class CalendarCategoriesMap extends ArticleCategoriesMap { public function getParentCatId($t3id) { return $this->find($t3id)->getId(); } } + + class Typo3DB { + protected + $t3db, + $pages_titles; + - protected $t3db; public function __construct() { $this->t3db = new Class_Systeme_Sql('localhost', 'root', 'root', 'miop_typo3'); } + + public function findPageTitle($uid) { + if (isset($this->pages_titles[$uid])) + return $this->pages_titles[$uid]; + + if (!$row = $this->t3db->fetchEnreg('select title from pages where uid='.$uid)) { + return $this->pages_titles[$uid] = ''; + } + + return $this->pages_titles[$uid] = $row['title']; + } + public function findAllUsers() { return $this->t3db->fetchAll('select * from be_users where uid > 1'); } diff --git a/tests/library/Class/Import/Typo3Fixture.php b/tests/library/Class/Import/Typo3Fixture.php index 5a9d04cf1f5..c0539f0c13a 100644 --- a/tests/library/Class/Import/Typo3Fixture.php +++ b/tests/library/Class/Import/Typo3Fixture.php @@ -78,6 +78,10 @@ class MockTypo3DB { ['title' => 'Art', 'uid' => 99, 'parent_category' => 0 + ], + ['title' => 'NOS TOPS 5', + 'uid' => 43, + 'parent_category' => 0 ] ]; } @@ -107,7 +111,8 @@ class MockTypo3DB { 'ext_url' => 'http://www.alesia.com/', 'tx_danpextendnews_tags' => 'Alésia, Jules César, Vercingétorix, Gaule, armée, bataille', 'short' => null, - 'uid' => 14478], + 'uid' => 14478, + 'pid' => 49], ['crdate' => '1412769359', 'category' => 1, @@ -115,9 +120,8 @@ class MockTypo3DB { 'ext_url' => 'http://koha.mediathequeouestprovence.fr/cgi-bin/koha/opac-detail.pl?biblionumber=268360', 'tx_danpextendnews_tags' => 'Canada', 'short' => null, - 'uid' => 14479] - - + 'uid' => 14479, + 'pid' => 0] ]; } @@ -266,7 +270,8 @@ La collection<span style="text-decoration: none"><span style="font-style: normal public function findAllForeignUidForNewsCat($uid) { return [ ['uid_foreign' => 81], - ['uid_foreign' => 82], + ['uid_foreign' => 43], + ['uid_foreign' => 82] ]; } @@ -333,10 +338,18 @@ La collection<span style="text-decoration: none"><span style="font-style: normal 'endtime' => 0, 'hidden' => 0, 'cruser_id' => 1, + 'title' => '', 'bodytext' => 'Initiées en 2008 par le pôle Langues et Littérature de Miramas, les lectures à voix haute ou Lectures impromptues ont évolué au fil du temps. En 2010, elles ont joué les variations, de lectures surprises solo en lectures thématiques à plusieurs voix en passant par le café-lecture où les bibliothécaires présentent <link http://koha.mediathequeouestprovence.fr/cgi-bin/koha/opac-shelves.pl?viewshelf=1556&sortfield=title%20%3Ccid:part1.08080603.08080008@ouestprovence.fr%3E _blank external-link-new-window>des textes</link> et en lisent parfois des extraits.<br /><br />Autant d\'autres façons de lire, de donner envie de lire, d\'échanger sur ses lectures...que nous poursuivons en 2011.<br /><br />Les bibliothécaires lisent pour vous, adhérents ou non adhérents, amateurs de littérature, jeunes et moins jeunes...Il n\'est pas nécessaire d\'avoir lu les textes, d\'avoir des connaissances particulières...juste le goût des histoires, d\'échanger, l\'envie de partager ses propres lectures et même, pourquoi pas, de lire soi-même...<br /><br />Les bibliothécaires lisent surtout des extraits de romans ou des nouvelles complètes, parfois un conte ou des poèmes, à une ou plusieurs voix. En fin de séance, chacun peut échanger et les textes peuvent être empruntés.<br />Les rendez-vous sont mensuels : un signet aux couleurs des lectures est distribué pour vousaider à mémoriser les dates et lieux de ces instants littéraires.<br /><br />Il n\'est pas besoin de s\'inscrire ; n\'hésitez pas à prendre un moment pour venir écouter, découvrir des auteurs, des histoires variées ; le montage vidéo qui accompagne ce billet est une modeste invitation à ce partage.', 'header' => 'A haute voix... le roman se fait entendre...' ] ]; } + + public function findPageTitle($uid) { + if ($uid == 49) + return '2014'; + return ''; + } + } diff --git a/tests/library/Class/Import/Typo3Test.php b/tests/library/Class/Import/Typo3Test.php index 2f134993585..8cef371fd37 100644 --- a/tests/library/Class/Import/Typo3Test.php +++ b/tests/library/Class/Import/Typo3Test.php @@ -212,7 +212,7 @@ class Import_Typo3ArticleTest extends Import_Typo3TestCase { /** @test */ public function articleABordDuBateauPirateShouldBeInDomainAutres() { - $this->assertEquals(['Société & Civilisation', 'Divers', 'Autres'], + $this->assertEquals(['Société & Civilisation', 'Divers', 'NOS TOPS 5', 'Autres'], $this->bateau_pirate->getDomaineLibelles()); } @@ -345,7 +345,8 @@ class Import_Typo3SitothequeTest extends Import_Typo3TestCase { public function setUp() { parent::setUp(); $this->migration->import_categories(); - $this->migration->import_sites(); + $this->migration->importArticlesPages(); + $this->migration->importSites(); $this->sito = Class_Sitotheque::findFirstBy(['titre' => 'MuséoParc Alésia']); } @@ -359,17 +360,23 @@ class Import_Typo3SitothequeTest extends Import_Typo3TestCase { /** @test */ public function MuseoParcAlesiaShouldHaveCategoryMyCategory() { $id_cat = $this->sito->getIdCat(); - $this->assertEquals('Autres', + $this->assertEquals('2014', Class_SitothequeCategorie::findFirstBy(['id' => $id_cat])->getLibelle()); } /** @test */ - public function articleABordDuBateauPirateShouldBeInDomainAutres() { - $this->assertEquals(['My category', 'Divers', 'Autres'], + public function museoParcShouldBeInDomainAutres() { + $this->assertEquals(['My category', 'Divers', 'NOS TOPS 5', 'Autres'], $this->sito->getDomaineLibelles()); } + + /** @test */ + public function museoParcAlesiaShouldBeInNosTops5_Art_Autres_2014() { + $this->assertEquals('/NOS TOPS 5/Autres/2014', + $this->sito->getCategorie()->getPath()); + } } @@ -447,20 +454,20 @@ class Import_Typo3LogsTest extends Import_Typo3TestCase { /** @test */ - public function logsShouldContains7T3Categories() { - $this->assertContains("Typo3 categories found: 7", Class_Import_Typo3_Logs::getInstance()->getLogs()); + public function logsShouldContains8T3Categories() { + $this->assertContains("Typo3 categories found: 8", Class_Import_Typo3_Logs::getInstance()->getLogs()); } /** @test */ public function logsShouldContainsSavedSitoCategories() { - $this->assertContains("Typo3 Sito categories saved: 6", Class_Import_Typo3_Logs::getInstance()->getLogs()); + $this->assertContains("Typo3 Sito categories saved: 7", Class_Import_Typo3_Logs::getInstance()->getLogs()); } /** @test */ public function logsShouldContainsSavedArticleCategories() { - $this->assertContains("Typo3 Article categories saved: 9", + $this->assertContains("Typo3 Article categories saved: 10", Class_Import_Typo3_Logs::getInstance()->getLogs()); } @@ -479,7 +486,7 @@ class Import_Typo3LogsTest extends Import_Typo3TestCase { /** @test */ public function logsShouldContainsSavedDomains() { - $this->assertContains("Typo3 Domains saved: 10", Class_Import_Typo3_Logs::getInstance()->getLogs()); + $this->assertContains("Typo3 Domains saved: 11", Class_Import_Typo3_Logs::getInstance()->getLogs()); } -- GitLab