From 8cfa0f1ea60a55df071495c3b9e4d8ea1bef5f17 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Fri, 14 Nov 2014 02:47:02 +0100 Subject: [PATCH 1/4] Speeded up the process to get slugs for routing. --- SimplePagesPlugin.php | 22 +++++++++++++--------- models/SimplePagesPageTable.php | 14 +++++++++++++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/SimplePagesPlugin.php b/SimplePagesPlugin.php index 4eb8540..7379c51 100644 --- a/SimplePagesPlugin.php +++ b/SimplePagesPlugin.php @@ -197,18 +197,22 @@ public function hookDefineRoutes($args) $router = $args['router']; - // Add custom routes based on the page slug. - $pages = get_db()->getTable('SimplePagesPage')->findAll(); - foreach ($pages as $page) { + // Add custom routes based on the page slug, with one query. + $slugs = get_db()->getTable('SimplePagesPage')->findPairsForSelectForm(); + if (empty($slugs)) { + return; + } + + foreach ($slugs as $id => $slug) { $router->addRoute( - 'simple_pages_show_page_' . $page->id, + 'simple_pages_show_page_' . $id, new Zend_Controller_Router_Route( - $page->slug, + $slug, array( - 'module' => 'simple-pages', - 'controller' => 'page', - 'action' => 'show', - 'id' => $page->id + 'module' => 'simple-pages', + 'controller' => 'page', + 'action' => 'show', + 'id' => $id, ) ) ); diff --git a/models/SimplePagesPageTable.php b/models/SimplePagesPageTable.php index 594eaac..a355656 100644 --- a/models/SimplePagesPageTable.php +++ b/models/SimplePagesPageTable.php @@ -23,7 +23,19 @@ public function findAllPagesOrderBySlug() $select = $this->getSelect()->order('slug'); return $this->fetchObjects($select); } - + + /** + * Retrieve the array of columns that are used by findPairsForSelectForm(). + * + * @see Omeka_Db_Table::findPairsForSelectForm() + * @return array + */ + protected function _getColumnPairs() + { + $alias = $this->getTableAlias(); + return array($alias . '.id', $alias . '.slug'); + } + public function applySearchFilters($select, $params) { $alias = $this->getTableAlias(); From 4d2b347604dd01ea25d99cdff473c18defb7a31d Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Fri, 14 Nov 2014 02:47:02 +0100 Subject: [PATCH 2/4] Simplified and improved the route of simple pages. --- SimplePagesPlugin.php | 31 ++++++++++++++----------------- controllers/PageController.php | 11 +++++++---- models/SimplePagesPageTable.php | 19 +++++++++++++++++-- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/SimplePagesPlugin.php b/SimplePagesPlugin.php index 7379c51..07bb240 100644 --- a/SimplePagesPlugin.php +++ b/SimplePagesPlugin.php @@ -195,28 +195,25 @@ public function hookDefineRoutes($args) return; } - $router = $args['router']; - - // Add custom routes based on the page slug, with one query. + // Add a custom route based on the page slugs. $slugs = get_db()->getTable('SimplePagesPage')->findPairsForSelectForm(); if (empty($slugs)) { return; } - foreach ($slugs as $id => $slug) { - $router->addRoute( - 'simple_pages_show_page_' . $id, - new Zend_Controller_Router_Route( - $slug, - array( - 'module' => 'simple-pages', - 'controller' => 'page', - 'action' => 'show', - 'id' => $id, - ) - ) - ); - } + $router = $args['router']; + $quotedSlugs = array_map('preg_quote', $slugs); + $router->addRoute('simple_pages_show_pages', new Zend_Controller_Router_Route_Regex( + '(' . implode('|', $quotedSlugs) . ')', + array( + 'module' => 'simple-pages', + 'controller' => 'page', + 'action' => 'show', + ), + array( + 1 => 'slug', + ) + )); } /** diff --git a/controllers/PageController.php b/controllers/PageController.php index 9de33d3..e562e50 100644 --- a/controllers/PageController.php +++ b/controllers/PageController.php @@ -13,12 +13,15 @@ */ class SimplePages_PageController extends Omeka_Controller_AbstractActionController { + /** + * Show a simple page. + */ public function showAction() { - // Get the page object from the passed ID. - $pageId = $this->_getParam('id'); - $page = $this->_helper->db->getTable('SimplePagesPage')->find($pageId); - + // Get the page object from the passed slug. + $slug = $this->_getParam('slug'); + $page = $this->_helper->db->getTable('SimplePagesPage')->findBySlug($slug); + // Restrict access to the page when it is not published. if (!$page->is_published && !$this->_helper->acl->isAllowed('show-unpublished')) { diff --git a/models/SimplePagesPageTable.php b/models/SimplePagesPageTable.php index a355656..8c49ef1 100644 --- a/models/SimplePagesPageTable.php +++ b/models/SimplePagesPageTable.php @@ -64,9 +64,24 @@ public function applySearchFilters($select, $params) $select->order("{$alias}.title ASC"); break; } - } + } } - + + /** + * Retrieve a simple page by slug. + * + * @param sllug $slug Slug of the page to retrieve. + * @return SimplePagePage|null The simple page that is returned. + */ + public function findBySlug($slug) + { + $select = $this->getSelect(); + $select->where($this->getTableAlias() . '.slug = ?', $slug); + $select->limit(1); + $select->reset(Zend_Db_Select::ORDER); + return $this->fetchObject($select, array()); + } + /** * Retrieve child pages from list of pages matching page ID. * From 843c8b29149d0fd80a0ca874f4fc0c45e1f6674c Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 17 Nov 2014 02:47:02 +0100 Subject: [PATCH 3/4] Replaced the select used to define routes. --- SimplePagesPlugin.php | 2 +- models/SimplePagesPageTable.php | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/SimplePagesPlugin.php b/SimplePagesPlugin.php index 07bb240..ef5af24 100644 --- a/SimplePagesPlugin.php +++ b/SimplePagesPlugin.php @@ -196,7 +196,7 @@ public function hookDefineRoutes($args) } // Add a custom route based on the page slugs. - $slugs = get_db()->getTable('SimplePagesPage')->findPairsForSelectForm(); + $slugs = get_db()->getTable('SimplePagesPage')->findSlugs(); if (empty($slugs)) { return; } diff --git a/models/SimplePagesPageTable.php b/models/SimplePagesPageTable.php index 8c49ef1..a4c0879 100644 --- a/models/SimplePagesPageTable.php +++ b/models/SimplePagesPageTable.php @@ -24,16 +24,18 @@ public function findAllPagesOrderBySlug() return $this->fetchObjects($select); } - /** - * Retrieve the array of columns that are used by findPairsForSelectForm(). + /** + * Find all slugs. * - * @see Omeka_Db_Table::findPairsForSelectForm() - * @return array + * @return array All slugs of pages. */ - protected function _getColumnPairs() + public function findSlugs() { $alias = $this->getTableAlias(); - return array($alias . '.id', $alias . '.slug'); + $select = $this->getSelect(); + $select->reset(Zend_Db_Select::COLUMNS); + $select->from(array(), array($alias . '.slug')); + return $this->fetchCol($select); } public function applySearchFilters($select, $params) From a2878a221562495a38781fb8d94d368645b948b3 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Fri, 28 Nov 2014 02:47:02 +0100 Subject: [PATCH 4/4] Fixed Zend reverse route. --- SimplePagesPlugin.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SimplePagesPlugin.php b/SimplePagesPlugin.php index ef5af24..ebed937 100644 --- a/SimplePagesPlugin.php +++ b/SimplePagesPlugin.php @@ -212,7 +212,8 @@ public function hookDefineRoutes($args) ), array( 1 => 'slug', - ) + ), + '%s' )); }