From ab989e35b4caec8b621fd31078a604bd346d88db Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Mon, 23 Feb 2015 19:46:26 +0100
Subject: [PATCH] rel #21691 : only handles diffs when reindexing domains, only
 update linked record facets instead of update all domain's records

---
 .../21691-articles-indexall-performance          |  1 +
 cosmogramme/php/integration/domaines.php         | 16 ++++++++++------
 library/Class/NoticeDomain.php                   |  8 ++++++++
 library/Trait/Indexable.php                      | 16 ++++++++++------
 4 files changed, 29 insertions(+), 12 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/21691-articles-indexall-performance

diff --git a/VERSIONS_HOTLINE/21691-articles-indexall-performance b/VERSIONS_HOTLINE/21691-articles-indexall-performance
new file mode 100644
index 00000000000..19532b04653
--- /dev/null
+++ b/VERSIONS_HOTLINE/21691-articles-indexall-performance
@@ -0,0 +1 @@
+ - ticket #21691 : Optimisation de la performance de la réindexation des données dans les domaines
\ No newline at end of file
diff --git a/cosmogramme/php/integration/domaines.php b/cosmogramme/php/integration/domaines.php
index 45dedf8405f..55924a8f0b5 100644
--- a/cosmogramme/php/integration/domaines.php
+++ b/cosmogramme/php/integration/domaines.php
@@ -53,14 +53,18 @@ if ($phase==15) {
 		$phase_data["pointeur"]=0;
 	}
 
-	$log->ecrire('<h4>Indexation des paniers dans les domaines</h4>');
-	Class_PanierNotice::indexAll();
+	if ($mode_cron) {
+		$log->ecrire('<h4>Indexation des paniers dans les domaines</h4>');
+		Class_PanierNotice::indexAll();
 
-	$log->ecrire("<h4>Indexation des articles dans les domaines</h4>");
-	Class_Article::indexAll();
+		$log->ecrire("<h4>Indexation des articles dans les domaines</h4>");
+		Class_Article::indexAll();
 
-	$log->ecrire("<h4>Indexation des sitothèques dans les domaines</h4>");
-	Class_Sitotheque::indexAll();
+		$log->ecrire("<h4>Indexation des sitothèques dans les domaines</h4>");
+		Class_Sitotheque::indexAll();
+	} else {
+		$log->ecrire("<h4>Les indexations des paniers, articles et sitothèques dans les domaines ne sont traitées qu'en mode cron</h4>");
+	}
 }
 
 ?>
diff --git a/library/Class/NoticeDomain.php b/library/Class/NoticeDomain.php
index b3b5bebabb0..70514ddd1ad 100644
--- a/library/Class/NoticeDomain.php
+++ b/library/Class/NoticeDomain.php
@@ -161,5 +161,13 @@ class Class_NoticeDomain extends Storm_Model_Abstract {
 	public function getNotice() {
 		return Class_Notice::findFirstBy(['clef_alpha' => $this->getRecordAlphaKey()]);
 	}
+
+
+	public function updateFacette() {
+		$this->getNotice()
+				 ->updateFacette($this->getDomain()->getFacette())
+				 ->save();
+		return $this;
+	}
 }
 ?>
\ No newline at end of file
diff --git a/library/Trait/Indexable.php b/library/Trait/Indexable.php
index 8120e7b975c..cbd2bf6b39f 100644
--- a/library/Trait/Indexable.php
+++ b/library/Trait/Indexable.php
@@ -37,14 +37,18 @@ trait Trait_Indexable {
 			return $this;
 		}
 
-		Class_NoticeDomain::deleteBy(['record_alpha_key' => $alpha_key]);
+		$existing = Class_NoticeDomain::findAllBy(['record_alpha_key' => $alpha_key]);
+		$existing_ids = array_map(function ($item) { return $item->getDomainId();}, $existing);
 
-		foreach($domains_ids as $domain_id) {
-			Class_NoticeDomain::newInstance(['domain_id' => $domain_id,
-																			 'record_alpha_key' => $alpha_key])
-				->save();
+		if ($to_delete = array_diff($existing_ids, $domains_ids))
+			Class_NoticeDomain::deleteBy(['record_alpha_key' => $alpha_key,
+																		'domain_id' => $to_delete]);
 
-			Class_NoticeDomain::updateRecordsFacette($domain_id);
+		foreach(array_diff($domains_ids, $existing_ids) as $domain_id) {
+			$notice_domain = Class_NoticeDomain::newInstance(['domain_id' => $domain_id,
+																												'record_alpha_key' => $alpha_key]);
+			$notice_domain->save();
+			$notice_domain->updateFacette();
 		}
 
 		return $this;
-- 
GitLab