Bladeren bron

On simule la pagination pour optimiser les requêtes en BDD (évite de tout charger puis d'en jeter les 3/4)

Marien Fressinaud 13 jaren geleden
bovenliggende
commit
a4173dd2e9
5 gewijzigde bestanden met toevoegingen van 96 en 39 verwijderingen
  1. 1 1
      app/configuration/routes.php
  2. 6 12
      app/controllers/indexController.php
  3. 68 18
      app/models/Entry.php
  4. 2 1
      app/views/index/index.phtml
  5. 19 7
      lib/Paginator.php

+ 1 - 1
app/configuration/routes.php

@@ -3,7 +3,7 @@
 return array (
 	// Index
 	array (
-		'route'      => '/\?q=([\w\d\-_]+)&p=([\d+])',
+		'route'      => '/\?q=([\w\d\-_]+)&p=([\d]+)',
 		'controller' => 'index',
 		'action'     => 'index',
 		'params'     => array ('get', 'page')

+ 6 - 12
app/controllers/indexController.php

@@ -10,6 +10,11 @@ class indexController extends ActionController {
 		$entryDAO = new EntryDAO ();
 		$catDAO = new CategoryDAO ();
 		
+		// pour optimiser
+		$page = Request::param ('page', 1);
+		$entryDAO->_nbItemsPerPage ($this->view->conf->postsPerPage ());
+		$entryDAO->_currentPage ($page);
+		
 		$default_view = $this->view->conf->defaultView ();
 		$mode = Session::param ('mode');
 		if ($mode == false) {
@@ -51,18 +56,7 @@ class indexController extends ActionController {
 			$entries = $entryDAO->listEntries ($mode, $order);
 		}
 		
-		// Gestion pagination
-		try {
-			$page = Request::param ('page', 1);
-			$this->view->entryPaginator = new Paginator ($entries);
-			$this->view->entryPaginator->_nbItemsPerPage ($this->view->conf->postsPerPage ());
-			$this->view->entryPaginator->_currentPage ($page);
-		} catch (CurrentPagePaginationException $e) {
-			Error::error (
-				404,
-				array ('error' => array ('La page que vous cherchez n\'existe pas'))
-			);
-		}
+		$this->view->entryPaginator = $entryDAO->getPaginator ($entries);
 		
 		$this->view->cat_aside = $catDAO->listCategories ();
 		$this->view->nb_favorites = $entryDAO->countFavorites ();

+ 68 - 18
app/models/Entry.php

@@ -214,7 +214,18 @@ class EntryDAO extends Model_pdo {
 			$order = '';
 		}
 		
-		$sql = 'SELECT * FROM entry' . $where . ' ORDER BY date' . $order;
+		$sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
+		$stm = $this->bd->prepare ($sql);
+		$stm->execute ();
+		$res = $stm->fetchAll (PDO::FETCH_ASSOC);
+		$this->nbItems = $res[0]['count'];
+		
+		$deb = ($this->currentPage - 1) * $this->nbItemsPerPage;
+		$fin = $this->nbItemsPerPage;
+		
+		$sql = 'SELECT * FROM entry' . $where
+		     . ' ORDER BY date' . $order
+		     . ' LIMIT ' . $deb . ', ' . $fin;
 		$stm = $this->bd->prepare ($sql);
 		$stm->execute ();
 
@@ -233,7 +244,18 @@ class EntryDAO extends Model_pdo {
 			$order = '';
 		}
 		
-		$sql = 'SELECT * FROM entry' . $where . ' ORDER BY date' . $order;
+		$sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
+		$stm = $this->bd->prepare ($sql);
+		$stm->execute ();
+		$res = $stm->fetchAll (PDO::FETCH_ASSOC);
+		$this->nbItems = $res[0]['count'];
+		
+		$deb = ($this->currentPage - 1) * $this->nbItemsPerPage;
+		$fin = $this->nbItemsPerPage;
+		
+		$sql = 'SELECT * FROM entry' . $where
+		     . ' ORDER BY date' . $order
+		     . ' LIMIT ' . $deb . ', ' . $fin;
 		$stm = $this->bd->prepare ($sql);
 		
 		$stm->execute ();
@@ -253,7 +275,18 @@ class EntryDAO extends Model_pdo {
 			$order = '';
 		}
 		
-		$sql = 'SELECT * FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where . ' ORDER BY date' . $order;
+		$sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where;
+		$stm = $this->bd->prepare ($sql);
+		$values = array ($cat);
+		$stm->execute ($values);
+		$res = $stm->fetchAll (PDO::FETCH_ASSOC);
+		$this->nbItems = $res[0]['count'];
+		
+		$deb = ($this->currentPage - 1) * $this->nbItemsPerPage;
+		$fin = $this->nbItemsPerPage;
+		$sql = 'SELECT * FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where
+		     . ' ORDER BY date' . $order
+		     . ' LIMIT ' . $deb . ', ' . $fin;
 		
 		$stm = $this->bd->prepare ($sql);
 		
@@ -290,31 +323,48 @@ class EntryDAO extends Model_pdo {
 		
 		return $res[0]['count'];
 	}
+	
+	// gestion de la pagination directement via le DAO
+	private $nbItemsPerPage = 1;
+	private $currentPage = 1;
+	private $nbItems = 0;
+	public function _nbItemsPerPage ($value) {
+		$this->nbItemsPerPage = $value;
+	}
+	public function _currentPage ($value) {
+		$this->currentPage = $value;
+	}
+	
+	public function getPaginator ($entries) {
+		$paginator = new Paginator ($entries);
+		$paginator->_nbItems ($this->nbItems);
+		$paginator->_nbItemsPerPage ($this->nbItemsPerPage);
+		$paginator->_currentPage ($this->currentPage);
+		
+		return $paginator;
+	}
 }
 
 class HelperEntry {
 	public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
 		$list = array ();
-
+		
 		if (!is_array ($listDAO)) {
 			$listDAO = array ($listDAO);
 		}
 
 		foreach ($listDAO as $key => $dao) {
-			if (($mode != 'not_read' || !$dao['is_read'])
-			 && ($favorite == false || $dao['is_favorite'])) {
-				$list[$key] = new Entry (
-					$dao['id_feed'],
-					$dao['guid'],
-					$dao['title'],
-					$dao['author'],
-					unserialize (gzinflate (base64_decode ($dao['content']))),
-					$dao['link'],
-					$dao['date'],
-					$dao['is_read'],
-					$dao['is_favorite']
-				);
-			}
+			$list[$key] = new Entry (
+				$dao['id_feed'],
+				$dao['guid'],
+				$dao['title'],
+				$dao['author'],
+				unserialize (gzinflate (base64_decode ($dao['content']))),
+				$dao['link'],
+				$dao['date'],
+				$dao['is_read'],
+				$dao['is_favorite']
+			);
 		}
 
 		return $list;

+ 2 - 1
app/views/index/index.phtml

@@ -1,4 +1,5 @@
-<?php $items = $this->entryPaginator->items (); ?>
+<?php $items = $this->entryPaginator->items (true); ?>
+
 <?php if (!empty ($items)) { ?>
 <div id="top">
 	<?php if (!login_is_conf ($this->conf) || is_logged ()) { ?>

+ 19 - 7
lib/Paginator.php

@@ -28,12 +28,18 @@ class Paginator {
 	 */
 	private $nbPage = 1;
 
+	/**
+	 * $nbItems le nombre d'éléments
+	 */
+	private $nbItems = 0;
+
 	/**
 	 * Constructeur
 	 * @param $items les éléments à gérer
 	 */
 	public function __construct ($items) {
 		$this->_items ($items);
+		$this->_nbItems (count ($this->items (true)));
 		$this->_nbItemsPerPage ($this->nbItemsPerPage);
 		$this->_currentPage ($this->currentPage);
 	}
@@ -66,7 +72,7 @@ class Paginator {
 			}
 
 			$i++;
-		} while (!$page && $i < count ($this->items));
+		} while (!$page && $i < $this->nbItems ());
 
 		return $page;
 	}
@@ -86,7 +92,7 @@ class Paginator {
 			} else {
 				$i++;
 			}
-		} while (!$find && $i < count ($this->items));
+		} while (!$find && $i < $this->nbItems ());
 
 		return $i;
 	}
@@ -98,7 +104,7 @@ class Paginator {
 	 */
 	public function itemByPosition ($pos) {
 		if ($pos < 0) {
-			$pos = count ($this->items) - 1;
+			$pos = $this->nbItems () - 1;
 		}
 		if ($pos >= count($this->items)) {
 			$pos = 0;
@@ -115,7 +121,7 @@ class Paginator {
 	 */
 	public function items ($all = false) {
 		$array = array ();
-		$nbItems = count ($this->items);
+		$nbItems = $this->nbItems ();
 
 		if ($nbItems <= $this->nbItemsPerPage || $all) {
 			$array = $this->items;
@@ -147,6 +153,9 @@ class Paginator {
 	public function nbPage () {
 		return $this->nbPage;
 	}
+	public function nbItems () {
+		return $this->nbItems;
+	}
 
 	/**
 	 * SETTEURS
@@ -159,8 +168,8 @@ class Paginator {
 		$this->_nbPage ();
 	}
 	public function _nbItemsPerPage ($nbItemsPerPage) {
-		if ($nbItemsPerPage > count ($this->items)) {
-			$nbItemsPerPage = count ($this->items);
+		if ($nbItemsPerPage > $this->nbItems ()) {
+			$nbItemsPerPage = $this->nbItems ();
 		}
 		if ($nbItemsPerPage < 0) {
 			$nbItemsPerPage = 0;
@@ -178,7 +187,10 @@ class Paginator {
 	}
 	private function _nbPage () {
 		if ($this->nbItemsPerPage > 0) {
-			$this->nbPage = ceil (count ($this->items) / $this->nbItemsPerPage);
+			$this->nbPage = ceil ($this->nbItems () / $this->nbItemsPerPage);
 		}
 	}
+	public function _nbItems ($value) {
+		$this->nbItems = $value;
+	}
 }