From 48af0336936173baebdc96c04007118d0b83c274 Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Fri, 25 Sep 2015 09:59:11 +0200
Subject: [PATCH] Dev #26120 - Ability to udpate sites

---
 library/Class/Import/Typo3.php              | 117 +++++++++++++++-----
 tests/library/Class/Import/Typo3Fixture.php |  15 +++
 tests/library/Class/Import/Typo3Test.php    |  21 +++-
 3 files changed, 122 insertions(+), 31 deletions(-)

diff --git a/library/Class/Import/Typo3.php b/library/Class/Import/Typo3.php
index 7f9e4d10fe5..9f5c3a7a181 100644
--- a/library/Class/Import/Typo3.php
+++ b/library/Class/Import/Typo3.php
@@ -106,6 +106,9 @@ class Class_Import_Typo3 {
       $logger->addLogRow("\n\n ******** Mise a jour Articles");
       $this->updateArticles($last_update_timestamp);
 
+      $logger->addLogRow("\n\n ******** Mise a jour Sitothèque");
+      $this->updateSites($last_update_timestamp);
+
       $logger->addLogRow("\n\n ******** Mise a jour Articles Pages");
       $this->updateArticlePages($last_update_timestamp);
 
@@ -126,13 +129,13 @@ class Class_Import_Typo3 {
       $logger->addLogRow("\n\n ******** Reimport des sitothèques");
       $this->importSites();
       $logger->addLogRow("\n\n ******** Récupération des images pour les sitothèques");
-      $this->updateSites();
+      $this->updateSitesThumbnails();
       $logger->addLogRow("\n\ Attention : Relancer une intégration cosmogramme pour que l'import soit pris en compte");
     }
 
     if ($what == 'sito') {
       $logger->addLogRow("\n\n ******** Récupération des images pour les sitothèques");
-      $this->updateSites();
+      $this->updateSitesThumbnails();
       $logger->addLogRow("\n\ Attention : Relancer une intégration cosmogramme pour que l'import soit pris en compte");
     }
 
@@ -230,6 +233,29 @@ class Class_Import_Typo3 {
   }
 
 
+  public function updateSite($site, $new) {
+    $site->updateAttributes($this->_prepareSite($new))->save();
+  }
+
+
+  public function updateSites($update_date) {
+    $this->sites_categories_map = new SiteCategoriesMap('Non classé', $this->domaine_map);
+    $t3news = $this->t3db->findAllSitesSince($update_date);
+
+    foreach($t3news as $new) {
+      if ($site = Class_Sitotheque::findFirstByCustomFieldValue(SELF::UID_TYPO3_CF,
+                                                                $new['uid'])) {
+        $this->updateSite($site,$new);
+        continue;
+      }
+
+      $element = $this->traceUnknownKohaUrls(function() use ($new) {
+                                               return $this->createSito($new);
+                                             }, $new['uid']);
+    }
+  }
+
+
   public function importSites() {
     Class_Sitotheque::deleteBy([]);
     Class_SitothequeCategorie::deleteBy([]);
@@ -249,7 +275,7 @@ class Class_Import_Typo3 {
 
 
 
-  public function updateSites() {
+  public function updateSitesThumbnails() {
 
     $t3sites=$this->t3db->findAllExternalSites();
     foreach ($t3sites as $t3site) {
@@ -274,34 +300,26 @@ class Class_Import_Typo3 {
 
 
   public function createSito($new) {
-      $date_maj = date("Y-m-d H:i:s", $new['crdate']);
-      $id_cat = $this->sites_categories_map->find($new['category'])->getId();
-      $url = $this->convertKohaToBokehUrl($this->format_url($new['ext_url']));
-
-      $tags = str_replace(', ', ';', $new['tx_danpextendnews_tags']);
+    $attributes = $this->_prepareSite($new);
+    $attributes['url'] = $this->convertKohaToBokehUrl($this->format_url($new['ext_url']));
 
-      $element = Class_Sitotheque::newInstance(['id_cat' => $id_cat,
-                                                'date_maj' => $date_maj,
-                                                'titre' => $new['title'],
-                                                'url' => $url,
-                                                'tags' => $tags,
-                                                'description' => $new['short']]);
+    $element = Class_Sitotheque::newInstance($attributes);
 
-      $element->setCustomField(self::UID_TYPO3_CF, $new['uid']);
-      if (!$element->saveWithCustomFields()) {
-        $this->report['sitotheque_errors']++;
-        Class_Import_Typo3_Logs::getInstance()
-          ->addErrorRow("site with uid : " . $new['uid'] . ' (' . $new['title'] . ') - ' . implode(', ', $element->getErrors()));
-        return $element;
-      }
+    $element->setCustomField(self::UID_TYPO3_CF, $new['uid']);
+    if (!$element->saveWithCustomFields()) {
+      $this->report['sitotheque_errors']++;
+      Class_Import_Typo3_Logs::getInstance()
+        ->addErrorRow("site with uid : " . $new['uid'] . ' (' . $new['title'] . ') - ' . implode(', ', $element->getErrors()));
+      return $element;
+    }
 
-      $foreign_uids = $this->t3db->findAllForeignUidForNewsCat($new['uid']);
-      $this->sites_categories_map->setDomainsFor($element, $foreign_uids);
+    $foreign_uids = $this->t3db->findAllForeignUidForNewsCat($new['uid']);
+    $this->sites_categories_map->setDomainsFor($element, $foreign_uids);
 
-      if ($this->isInTop5($foreign_uids))
-        $this->putSiteInTop5($element, $new);
+    if ($this->isInTop5($foreign_uids))
+      $this->putSiteInTop5($element, $new);
 
-      return $element;
+    return $element;
   }
 
 
@@ -889,6 +907,20 @@ class Class_Import_Typo3 {
     $parts = array_slice(explode('-', $matches[1]), 0, 4);
     return implode('-', $parts);
   }
+
+
+  protected function _prepareSite($new) {
+    $id_cat = $this->sites_categories_map->find($new['category'])->getId();
+    $date_maj = date("Y-m-d H:i:s", $new['crdate']);
+    $tags = str_replace(', ', ';', $new['tx_danpextendnews_tags']);
+
+    return ['id_cat' => $id_cat,
+            'date_maj' => $date_maj,
+            'titre' => $new['title'],
+            'url' => $new['ext_url'],
+            'tags' => $tags,
+            'description' => $new['short']];
+  }
 }
 
 
@@ -950,6 +982,9 @@ class UserMap {
 
 
 abstract class CategoriesMap {
+
+  const UID_TYPO3_CF = 'uid_typo3';
+
   protected
     $map = [],
     $default_cat,
@@ -1050,11 +1085,20 @@ abstract class CategoriesMap {
 
   abstract protected function incrementSaved();
 
+  protected function findByUidTypo3($t3id) {
+    return null;
+  }
 
   public function find($t3id) {
-    return isset($this->map[$t3id])
-      ? $this->map[$t3id]
-      : $this->default_cat;
+    if (isset($this->map[$t3id]))
+      return $this->map[$t3id];
+
+    if ($category = $this->findByUidTypo3($t3id)) {
+      $this->map[$t3id] = $category;
+      return $category;
+    }
+
+    return $this->default_cat;
   }
 
 
@@ -1122,6 +1166,11 @@ class ArticleCategoriesMap extends CategoriesMap {
   protected function incrementSaved() {
     Class_Import_Typo3_Logs::getInstance()->incrementArticleCategoriesSaved();
   }
+
+
+  protected function findByUidTypo3 ($t3id) {
+    return Class_ArticleCategorie::findFirstByCustomFieldValue(self::UID_TYPO3_CF, $t3id);
+  }
 }
 
 
@@ -1168,6 +1217,11 @@ class SiteCategoriesMap extends CategoriesMap {
 
     return $category;
   }
+
+
+  protected function findByUidTypo3($t3id) {
+    return Class_SitothequeCategorie::findFirstByCustomFieldValue(self::UID_TYPO3_CF, $t3id);
+  }
 }
 
 
@@ -1240,6 +1294,11 @@ class Typo3DB {
   }
 
 
+  public function findAllSitesSince($update_date) {
+    return $this->t3db->fetchAll("select * from tt_news where deleted=0 and t3ver_label  not like 'DELETED!' and hidden=0 and ext_url not like '%koha.mediathequeouestprovence.fr%'  and ext_url>'' and tstamp >= ".$update_date." order by uid ASC");
+  }
+
+
   public function findAllSites() {
     return $this->t3db->fetchAll("select * from tt_news where deleted=0 and t3ver_label  not like 'DELETED!' and hidden=0 and ext_url not like '%koha.mediathequeouestprovence.fr%'  and ext_url>'' order by uid ASC");
   }
diff --git a/tests/library/Class/Import/Typo3Fixture.php b/tests/library/Class/Import/Typo3Fixture.php
index 7fb438db98b..f58fa02ff8d 100644
--- a/tests/library/Class/Import/Typo3Fixture.php
+++ b/tests/library/Class/Import/Typo3Fixture.php
@@ -213,6 +213,21 @@ class MockTypo3DB {
   }
 
 
+  public function findAllSitesSince($last_import_date) {
+    return [
+            ['crdate' => '1412781027',
+             'category' => 1,
+             'image' => '',
+             'title' => 'Sylvissima',
+             'ext_url' => 'http://www.sylvissima.com/',
+             'tx_danpextendnews_tags' => 'Sylvie Vartan, French pop',
+             'short' => 'C\'est Sylvie',
+             'uid' => 6656,
+             'pid' => 0],
+    ];
+  }
+
+
   public function findAllArticlesSince($last_import_date) {
     $articles = $this->findAllArticles();
     $articles[5]['uid']=666;
diff --git a/tests/library/Class/Import/Typo3Test.php b/tests/library/Class/Import/Typo3Test.php
index 8d4ee5b1da5..b9c658e3fd8 100644
--- a/tests/library/Class/Import/Typo3Test.php
+++ b/tests/library/Class/Import/Typo3Test.php
@@ -533,7 +533,7 @@ class Import_Typo3CalendarTest extends Import_Typo3TestCase {
 
 
 
-class Import_Typo3SitothequeUpdateTest extends Import_Typo3TestCase {
+class Import_Typo3SitothequeUpdateThumbnailsTest extends Import_Typo3TestCase {
   protected $filewriter;
   public function setUp() {
     parent::setUp();
@@ -542,7 +542,7 @@ class Import_Typo3SitothequeUpdateTest extends Import_Typo3TestCase {
     Class_Import_Typo3::setFileWriter($this->filewriter);
     Class_WebService_WebSiteThumbnail::setFileWriter($this->filewriter);
     $this->sito = Class_Sitotheque::findFirstBy(['titre' => 'MuséoParc Alésia']);
-    $this->migration->updateSites();
+    $this->migration->updateSitesThumbnails();
   }
 
   /** @test */
@@ -566,6 +566,23 @@ class Import_Typo3SitothequeUpdateTest extends Import_Typo3TestCase {
 
 }
 
+
+
+class Import_Typo3SitothequeUpdateTest extends Import_Typo3TestCase {
+  public function setUp() {
+    parent::setUp();
+
+    $this->migration->updateSites(888);
+  }
+
+  /** @test */
+  public function sylvissimaShouldBePresent() {
+    $this->assertNotNull(Class_SitoTheque::findFirstBy(['titre' => 'Sylvissima']));
+  }
+}
+
+
+
 class Import_Typo3SitothequeTest extends Import_Typo3TestCase {
   public function setUp() {
     parent::setUp();
-- 
GitLab