From a638391693c9144268013c424f0acbac7e6b313a Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Fri, 8 Mar 2013 15:06:46 +0000
Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20perfs=20chargement=20sitoth?=
 =?UTF-8?q?eque=20Int=C3=A9gration=20du=20profiler=20XHProf?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitattributes                                |  1 +
 library/Class/Sitotheque.php                  | 56 +++---------------
 .../ZendAfi/Controller/Plugin/XHProfile.php   | 58 +++++++++++++++++++
 library/startup.php                           |  1 +
 4 files changed, 69 insertions(+), 47 deletions(-)
 create mode 100644 library/ZendAfi/Controller/Plugin/XHProfile.php

diff --git a/.gitattributes b/.gitattributes
index 6412bc5277d..d755508b713 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -2505,6 +2505,7 @@ library/ZendAfi/Controller/Plugin/InitModule.php -text
 library/ZendAfi/Controller/Plugin/SelectionBib.php -text
 library/ZendAfi/Controller/Plugin/SetupLocale.php -text
 library/ZendAfi/Controller/Plugin/System.php -text
+library/ZendAfi/Controller/Plugin/XHProfile.php -text
 library/ZendAfi/Feed.php -text
 library/ZendAfi/Filters/Post.php -text
 library/ZendAfi/Filters/ReadSql.php -text
diff --git a/library/Class/Sitotheque.php b/library/Class/Sitotheque.php
index f80ee7cdb57..059efb2df25 100644
--- a/library/Class/Sitotheque.php
+++ b/library/Class/Sitotheque.php
@@ -21,13 +21,17 @@
 require_once dirname(__FILE__)."/CompositeBuilder.php";
 
 class SitoCategorie extends Zend_Db_Table_Abstract {
+	protected static $_instance;
 	protected $_name = 'sito_categorie';
-}
 
-class SitoUrl extends Zend_Db_Table_Abstract {
-	protected $_name = 'sito_url';
+	public static function getInstance() {
+		if (!isset(self::$_instance))
+			self::$_instance = new self();
+		return self::$_instance;
+	}
 }
 
+
 class Class_SitothequeModelCategorie extends ItemCategory {
 	public function getLabel(){
 		return $this->libelle;
@@ -119,22 +123,6 @@ class Class_Sitotheque extends Storm_Model_Abstract {
 
 
 	private $_dataBaseError = "Problème d'accès à la base de données";
-	private $_sitoCategorie;
-	private $_sitoUrl;
-	private $sql;
-
-
-	public function __construct()
-	{
-		try{
-			$this->_sitoCategorie = new SitoCategorie();
-			$this->_sitoUrl = new SitoUrl();
-			$this->sql = Zend_Registry::get('sql');
-		}catch (Exception $e){
-			logErrorMessage('Class: Class_Sitotheque; Function: __construct' . NL . $e->getMessage());
-		}
-	}
-
 
 	public function getIdZone() {
 		if (!$this->hasZone())
@@ -200,10 +188,10 @@ class Class_Sitotheque extends Storm_Model_Abstract {
 	public function getAllSousCategorie($id_cat_mere = "all")
 	{
 		try{
-			if($id_cat_mere == "all") { return $fetch = $this->_sitoCategorie->fetchAll(); }
+			if($id_cat_mere == "all") { return $fetch = SitoCategorie::getInstance()->fetchAll(); }
 			else
 				{
-					$select = $this->_sitoCategorie->getAdapter()->select()
+					$select = SitoCategorie::getInstance()>getAdapter()->select()
 						->from('sito_categorie',array('ID_CAT'=>'ID_CAT', 'ID_CAT_MERE'=>'ID_CAT_MERE','LIBELLE'=>'LIBELLE','ID_SITE'=>'ID_SITE'))
 						->where('ID_CAT_MERE=?', $id_cat_mere);
 					$stmt = $select->query();
@@ -216,32 +204,6 @@ class Class_Sitotheque extends Storm_Model_Abstract {
 		}
 	}
 
-	public function getAllSitoByIdCat($id_cat)
-	{
-		try{
-			$select = $this->_sitoUrl->getAdapter()->select()
-				->from('sito_url',array('ID_CAT'=>'ID_CAT','ID_SITO'=>'ID_SITO', 'TITRE'=>'TITRE', 'DESCRIPTION' => 'DESCRIPTION','URL' => 'URL',))
-				->where('ID_CAT=?', $id_cat);
-			$stmt = $select->query();
-			$row = $stmt->fetchAll();
-			return $row;
-		}catch (Exception $e){
-			logErrorMessage('Class: Class_Sitotheque; Function: getAllSitoByIdCat' . NL . $e->getMessage());
-			return $this->_dataBaseError;
-		}
-	}
-
-	public function getSitoById($id_sito)
-	{
-		if ( $this->_sitoUrl == null ){return $this->_dataBaseError;}
-		try{
-			$where = $this->_sitoUrl->getAdapter()->quoteInto('ID_SITO=?', $id_sito);
-			return $fetch = $this->_sitoUrl->fetchRow($where);
-		}catch (Exception $e){
-			logErrorMessage('Class: Class_Sitotheque; Function: getSitoById' . NL . $e->getMessage());
-			return $this->_dataBaseError;
-		}
-	}
 
 	public function getLastSito($nb)  {
 		$sql = Zend_Registry::get('sql');
diff --git a/library/ZendAfi/Controller/Plugin/XHProfile.php b/library/ZendAfi/Controller/Plugin/XHProfile.php
new file mode 100644
index 00000000000..61550cde3fe
--- /dev/null
+++ b/library/ZendAfi/Controller/Plugin/XHProfile.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 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).
+ *
+ * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+class ZendAfi_Controller_Plugin_XHProfile extends Zend_Controller_Plugin_Abstract {
+	protected $_enabled = false;
+
+	public function preDispatch(Zend_Controller_Request_Abstract  $request) {
+		if (function_exists('xhprof_enable'))
+			Class_ScriptLoader::getInstance()
+				->addJQueryReady('$("<button>Profile</button>")
+                            .attr("onclick", "window.open(window.location+\'?xhprof=1\', \'_blank\')")
+                            .attr("style", "position:absolute;top:0;right:0")
+                            .appendTo(\'body\')');
+
+		if (!$request->getParam('xhprof', false))
+			return;
+
+		$this->_enabled = true;
+		xhprof_enable();
+	}
+
+ 
+	public function postDispatch(Zend_Controller_Request_Abstract $request) {
+		if (!$this->_enabled)
+			return;
+
+		$xhprof_data = xhprof_disable();
+		$XHPROF_ROOT = "/usr/share/webapps/xhprof";
+		include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
+		include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
+		$xhprof_runs = new XHProfRuns_Default();
+		$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
+
+		$this->_response
+			->setRedirect("http://localhost/xhprof/index.php?run={$run_id}&source=xhprof_testing\n")
+			->clearBody();
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/library/startup.php b/library/startup.php
index 01cfabca08a..196f6c106bd 100644
--- a/library/startup.php
+++ b/library/startup.php
@@ -266,6 +266,7 @@ function setupFrontController() {
 		->registerPlugin(new ZendAfi_Controller_Plugin_InitModule())
 		->registerPlugin(new ZendAfi_Controller_Plugin_SelectionBib())
 		->registerPlugin(new ZendAfi_Controller_Plugin_System())
+		->registerPlugin(new ZendAfi_Controller_Plugin_XHProfile())
 		->setParam('useDefaultControllerAlways', true);
 
 	return setupRoutes($front_controller);
-- 
GitLab