Commit 92490ed5 authored by catch's avatar catch

Issue #1541684 by Rob Loach, aspilicious: Convert PagerDefault db extender to PSR-0.

parent 2d796cf1
......@@ -29,10 +29,10 @@
* inherits much of its syntax and semantics.
*
* Most Drupal database SELECT queries are performed by a call to db_query() or
* db_query_range(). Module authors should also consider using the PagerDefault
* Extender for queries that return results that need to be presented on
* multiple pages, and the Tablesort Extender for generating appropriate queries
* for sortable tables.
* db_query_range(). Module authors should also consider using the
* Drupal\Core\Database\Query\PagerSelectExtender for queries that return
* results that need to be presented on multiple pages, and the Tablesort
* Extender for generating appropriate queries for sortable tables.
*
* For example, one might wish to return a list of the most recent 10 nodes
* authored by a given user. Instead of directly issuing the SQL query
......
<?php
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\SelectExtender;
use Drupal\Core\Database\Query\SelectInterface;
/**
* @file
* Functions to aid in presenting database results as a set of pages.
*/
/**
* Query extender for pager queries.
*
* This is the "default" pager mechanism. It creates a paged query with a fixed
* number of entries per page.
*/
class PagerDefault extends SelectExtender {
/**
* The highest element we've autogenerated so far.
*
* @var int
*/
static $maxElement = 0;
/**
* The number of elements per page to allow.
*
* @var int
*/
protected $limit = 10;
/**
* The unique ID of this pager on this page.
*
* @var int
*/
protected $element = NULL;
/**
* The count query that will be used for this pager.
*
* @var SelectQueryInterface
*/
protected $customCountQuery = FALSE;
public function __construct(SelectInterface $query, Connection $connection) {
parent::__construct($query, $connection);
// Add pager tag. Do this here to ensure that it is always added before
// preExecute() is called.
$this->addTag('pager');
}
/**
* Override the execute method.
*
* Before we run the query, we need to add pager-based range() instructions
* to it.
*/
public function execute() {
// Add convenience tag to mark that this is an extended query. We have to
// do this in the constructor to ensure that it is set before preExecute()
// gets called.
if (!$this->preExecute($this)) {
return NULL;
}
// A NULL limit is the "kill switch" for pager queries.
if (empty($this->limit)) {
return;
}
$this->ensureElement();
$total_items = $this->getCountQuery()->execute()->fetchField();
$current_page = pager_default_initialize($total_items, $this->limit, $this->element);
$this->range($current_page * $this->limit, $this->limit);
// Now that we've added our pager-based range instructions, run the query normally.
return $this->query->execute();
}
/**
* Ensure that there is an element associated with this query.
* If an element was not specified previously, then the value of the
* $maxElement counter is taken, after which the counter is incremented.
*
* After running this method, access $this->element to get the element for this
* query.
*/
protected function ensureElement() {
if (!isset($this->element)) {
$this->element = self::$maxElement++;
}
}
/**
* Specify the count query object to use for this pager.
*
* You will rarely need to specify a count query directly. If not specified,
* one is generated off of the pager query itself.
*
* @param SelectQueryInterface $query
* The count query object. It must return a single row with a single column,
* which is the total number of records.
*/
public function setCountQuery(SelectInterface $query) {
$this->customCountQuery = $query;
}
/**
* Retrieve the count query for this pager.
*
* The count query may be specified manually or, by default, taken from the
* query we are extending.
*
* @return SelectQueryInterface
* A count query object.
*/
public function getCountQuery() {
if ($this->customCountQuery) {
return $this->customCountQuery;
}
else {
return $this->query->countQuery();
}
}
/**
* Specify the maximum number of elements per page for this query.
*
* The default if not specified is 10 items per page.
*
* @param $limit
* An integer specifying the number of elements per page. If passed a false
* value (FALSE, 0, NULL), the pager is disabled.
*/
public function limit($limit = 10) {
$this->limit = $limit;
return $this;
}
/**
* Specify the element ID for this pager query.
*
* The element is used to differentiate different pager queries on the same
* page so that they may be operated independently. If you do not specify an
* element, every pager query on the page will get a unique element. If for
* whatever reason you want to explicitly define an element for a given query,
* you may do so here.
*
* Setting the element here also increments the static $maxElement counter,
* which is used for determining the $element when there's none specified.
*
* Note that no collision detection is done when setting an element ID
* explicitly, so it is possible for two pagers to end up using the same ID
* if both are set explicitly.
*
* @param $element
*/
public function element($element) {
$this->element = $element;
if ($element >= self::$maxElement) {
self::$maxElement = $element + 1;
}
return $this;
}
}
/**
* Returns the current page being requested for display within a pager.
*
......@@ -206,10 +42,11 @@ function pager_find_page($element = 0) {
* If the items being displayed result from a database query performed using
* Drupal's database API, and if you have control over the construction of the
* database query, you do not need to call this function directly; instead, you
* can simply extend the query object with the 'PagerDefault' extender before
* executing it. For example:
* can simply extend the query object with the 'PagerSelectExtender' extender
* before executing it. For example:
* @code
* $query = db_select('some_table')->extend('PagerDefault');
* $query = db_select('some_table')
* ->extend('Drupal\Core\Database\Query\PagerSelectExtender');
* @endcode
*
* However, if you are using a different method for generating the items to be
......
......@@ -25,8 +25,7 @@ interface ExtendableInterface {
* @param $extender_name
* The base name of the extending class. The base name will be checked
* against the current database connection to allow driver-specific subclasses
* as well, using the same logic as the query objects themselves. For example,
* PagerDefault_mysql is the MySQL-specific override for PagerDefault.
* as well, using the same logic as the query objects themselves.
* @return Drupal\Core\Database\Query\ExtendableInterface
* The extender object, which now contains a reference to this object.
*/
......
<?php
/**
* @file
* Definition of Drupal\Core\Database\Query\PagerSelectExtender
*/
namespace Drupal\Core\Database\Query;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\SelectExtender;
use Drupal\Core\Database\Query\SelectInterface;
/**
* Query extender for pager queries.
*
* This is the "default" pager mechanism. It creates a paged query with a fixed
* number of entries per page.
*/
class PagerSelectExtender extends SelectExtender {
/**
* The highest element we've autogenerated so far.
*
* @var int
*/
static $maxElement = 0;
/**
* The number of elements per page to allow.
*
* @var int
*/
protected $limit = 10;
/**
* The unique ID of this pager on this page.
*
* @var int
*/
protected $element = NULL;
/**
* The count query that will be used for this pager.
*
* @var SelectQueryInterface
*/
protected $customCountQuery = FALSE;
public function __construct(SelectInterface $query, Connection $connection) {
parent::__construct($query, $connection);
// Add pager tag. Do this here to ensure that it is always added before
// preExecute() is called.
$this->addTag('pager');
}
/**
* Override the execute method.
*
* Before we run the query, we need to add pager-based range() instructions
* to it.
*/
public function execute() {
// Add convenience tag to mark that this is an extended query. We have to
// do this in the constructor to ensure that it is set before preExecute()
// gets called.
if (!$this->preExecute($this)) {
return NULL;
}
// A NULL limit is the "kill switch" for pager queries.
if (empty($this->limit)) {
return;
}
$this->ensureElement();
$total_items = $this->getCountQuery()->execute()->fetchField();
$current_page = pager_default_initialize($total_items, $this->limit, $this->element);
$this->range($current_page * $this->limit, $this->limit);
// Now that we've added our pager-based range instructions, run the query normally.
return $this->query->execute();
}
/**
* Ensure that there is an element associated with this query.
* If an element was not specified previously, then the value of the
* $maxElement counter is taken, after which the counter is incremented.
*
* After running this method, access $this->element to get the element for this
* query.
*/
protected function ensureElement() {
if (!isset($this->element)) {
$this->element = self::$maxElement++;
}
}
/**
* Specify the count query object to use for this pager.
*
* You will rarely need to specify a count query directly. If not specified,
* one is generated off of the pager query itself.
*
* @param SelectQueryInterface $query
* The count query object. It must return a single row with a single column,
* which is the total number of records.
*/
public function setCountQuery(SelectInterface $query) {
$this->customCountQuery = $query;
}
/**
* Retrieve the count query for this pager.
*
* The count query may be specified manually or, by default, taken from the
* query we are extending.
*
* @return SelectQueryInterface
* A count query object.
*/
public function getCountQuery() {
if ($this->customCountQuery) {
return $this->customCountQuery;
}
else {
return $this->query->countQuery();
}
}
/**
* Specify the maximum number of elements per page for this query.
*
* The default if not specified is 10 items per page.
*
* @param $limit
* An integer specifying the number of elements per page. If passed a false
* value (FALSE, 0, NULL), the pager is disabled.
*/
public function limit($limit = 10) {
$this->limit = $limit;
return $this;
}
/**
* Specify the element ID for this pager query.
*
* The element is used to differentiate different pager queries on the same
* page so that they may be operated independently. If you do not specify an
* element, every pager query on the page will get a unique element. If for
* whatever reason you want to explicitly define an element for a given query,
* you may do so here.
*
* Setting the element here also increments the static $maxElement counter,
* which is used for determining the $element when there's none specified.
*
* Note that no collision detection is done when setting an element ID
* explicitly, so it is possible for two pagers to end up using the same ID
* if both are set explicitly.
*
* @param $element
*/
public function element($element) {
$this->element = $element;
if ($element >= self::$maxElement) {
self::$maxElement = $element + 1;
}
return $this;
}
}
......@@ -134,7 +134,7 @@ function aggregator_load_feed_items($type, $data = NULL) {
}
$result = $query
->extend('PagerDefault')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->limit(20)
->orderBy('i.timestamp', 'DESC')
->orderBy('i.iid', 'DESC')
......
......@@ -79,7 +79,9 @@ function comment_admin_overview($form, &$form_state, $arg) {
'operations' => array('data' => t('Operations')),
);
$query = db_select('comment', 'c')->extend('PagerDefault')->extend('TableSort');
$query = db_select('comment', 'c')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query->join('node', 'n', 'n.nid = c.nid');
$query->addField('n', 'title', 'node_title');
$query->addTag('node_access');
......
......@@ -849,7 +849,8 @@ function comment_node_page_additions(Node $node) {
* to consider the trailing "/" so we use a substring only.
*/
function comment_get_thread(Node $node, $mode, $comments_per_page) {
$query = db_select('comment', 'c')->extend('PagerDefault');
$query = db_select('comment', 'c')
->extend('Drupal\Core\Database\Query\PagerSelectExtender');
$query->addField('c', 'cid');
$query
->condition('c.nid', $node->nid)
......
......@@ -37,7 +37,9 @@ function dblog_overview() {
array('data' => t('Operations')),
);
$query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
$query = db_select('watchdog', 'w')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query->leftJoin('users', 'u', 'w.uid = u.uid');
$query
->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link'))
......@@ -96,7 +98,9 @@ function dblog_top($type) {
$count_query->addExpression('COUNT(DISTINCT(message))');
$count_query->condition('type', $type);
$query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
$query = db_select('watchdog', 'w')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query->addExpression('COUNT(wid)', 'count');
$query = $query
->fields('w', array('message', 'variables'))
......
......@@ -9,7 +9,7 @@
use Drupal\entity\EntityFieldQueryException;
use Drupal\Core\Database\Query\Select;
use PagerDefault;
use Drupal\Core\Database\Query\PagerSelectExtender;
/**
* Retrieves entities matching a given set of conditions.
......@@ -571,10 +571,10 @@ public function range($start = NULL, $length = NULL) {
*/
public function pager($limit = 10, $element = NULL) {
if (!isset($element)) {
$element = PagerDefault::$maxElement++;
$element = PagerSelectExtender::$maxElement++;
}
elseif ($element >= PagerDefault::$maxElement) {
PagerDefault::$maxElement = $element + 1;
elseif ($element >= PagerSelectExtender::$maxElement) {
PagerSelectExtender::$maxElement = $element + 1;
}
$this->pager = array(
......
......@@ -893,7 +893,9 @@ function forum_get_topics($tid, $sortby, $forum_per_page) {
}
}
$query = db_select('forum_index', 'f')->extend('PagerDefault')->extend('TableSort');
$query = db_select('forum_index', 'f')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query->fields('f');
$query
->condition('f.tid', $tid)
......
......@@ -75,7 +75,9 @@ function _locale_translate_seek() {
$limit_language = $query['language'];
}
$sql_query = $sql_query->extend('PagerDefault')->limit(50);
$sql_query = $sql_query
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->limit(50);
$locales = $sql_query->execute();
$header = array(t('String'), t('Context'), ($limit_language) ? t('Language') : t('Languages'), array('data' => t('Operations'), 'colspan' => '2'));
......
......@@ -447,7 +447,9 @@ function node_admin_nodes() {
}
$header['operations'] = array('data' => t('Operations'));
$query = db_select('node', 'n')->extend('PagerDefault')->extend('TableSort');
$query = db_select('node', 'n')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
node_build_filter_query($query);
if (!user_access('bypass node access')) {
......
......@@ -1519,7 +1519,9 @@ function node_search_admin() {
*/
function node_search_execute($keys = NULL, $conditions = NULL) {
// Build matching conditions
$query = db_select('search_index', 'i', array('target' => 'slave'))->extend('Drupal\search\SearchQuery')->extend('PagerDefault');
$query = db_select('search_index', 'i', array('target' => 'slave'))
->extend('Drupal\search\SearchQuery')
->extend('Drupal\Core\Database\Query\PagerSelectExtender');
$query->join('node', 'n', 'n.nid = i.sid');
$query
->condition('n.status', 1)
......@@ -2488,7 +2490,7 @@ function node_page_default() {
->condition('status', 1)
->orderBy('sticky', 'DESC')
->orderBy('created', 'DESC')
->extend('PagerDefault')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->limit(variable_get('default_nodes_main', 10))
->addTag('node_access');
......
......@@ -27,7 +27,9 @@ function path_admin_overview($keys = NULL) {
}
$header[] = array('data' => t('Operations'));
$query = db_select('url_alias')->extend('PagerDefault')->extend('TableSort');
$query = db_select('url_alias')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
if ($keys) {
// Replace wildcards with PDO wildcards.
$query->condition('alias', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
......
......@@ -29,7 +29,7 @@ function poll_page() {
->groupBy('n.title')
->groupBy('p.active')
->groupBy('n.created')
->extend('PagerDefault')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->limit($polls_per_page)
->addTag('node_access');
$select->setCountQuery($count_select);
......@@ -56,7 +56,9 @@ function poll_votes($node) {
$header[] = array('data' => t('Vote'), 'field' => 'pc.chtext');
$header[] = array('data' => t('Timestamp'), 'field' => 'pv.timestamp', 'sort' => 'desc');
$select = db_select('poll_vote', 'pv')->extend('PagerDefault')->extend('TableSort');
$select = db_select('poll_vote', 'pv')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$select->join('poll_choice', 'pc', 'pv.chid = pc.chid');
$select->join('users', 'u', 'pv.uid = u.uid');
$queried_votes = $select
......
......@@ -157,8 +157,8 @@ function hook_search_admin() {
/**
* Execute a search for a set of key words.
*
* Use database API with the 'PagerDefault' query extension to perform your
* search.
* Use database API with the 'Drupal\Core\Database\Query\PagerSelectExtender'
* query extension to perform your search.
*
* If your module uses hook_update_index() and search_index() to index its
* items, use table 'search_index' aliased to 'i' as the main table in your
......@@ -195,7 +195,9 @@ function hook_search_admin() {
*/
function hook_search_execute($keys = NULL, $conditions = NULL) {
// Build matching conditions
$query = db_select('search_index', 'i', array('target' => 'slave'))->extend('Drupal\search\SearchQuery')->extend('PagerDefault');
$query = db_select('search_index', 'i', array('target' => 'slave'))
->extend('Drupal\search\SearchQuery')
->extend('Drupal\Core\Database\Query\PagerSelectExtender');
$query->join('node', 'n', 'n.nid = i.sid');
$query
->condition('n.status', 1)
......
......@@ -25,7 +25,9 @@ function statistics_recent_hits() {
array('data' => t('Operations'))
);
$query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
$query = db_select('accesslog', 'a', array('target' => 'slave'))
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query->join('users', 'u', 'a.uid = u.uid');
$query
->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid'))
......@@ -71,7 +73,9 @@ function statistics_top_pages() {
array('data' => t('Total page generation time'), 'field' => 'total_time')
);
$query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
$query = db_select('accesslog', 'a', array('target' => 'slave'))
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query->addExpression('COUNT(path)', 'hits');
// MAX(title) avoids having empty node titles which otherwise causes
// duplicates in the top pages list.
......@@ -124,7 +128,9 @@ function statistics_top_visitors() {
array('data' => t('Total page generation time'), 'field' => 'total'),
array('data' => user_access('block IP addresses') ? t('Operations') : '', 'colspan' => 2),
);
$query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
$query = db_select('accesslog', 'a', array('target' => 'slave'))
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query->leftJoin('blocked_ips', 'bl', 'a.hostname = bl.ip');
$query->leftJoin('users', 'u', 'a.uid = u.uid');
......@@ -184,7 +190,9 @@ function statistics_top_referrers() {
array('data' => t('Url'), 'field' => 'url'),
array('data' => t('Last visit'), 'field' => 'last'),
);
$query = db_select('accesslog', 'a')->extend('PagerDefault')->extend('TableSort');
$query = db_select('accesslog', 'a')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query->addExpression('COUNT(url)', 'hits');
$query->addExpression('MAX(timestamp)', 'last');
......
......@@ -25,7 +25,9 @@ function statistics_node_tracker() {
array('data' => t('User'), 'field' => 'u.name'),
array('data' => t('Operations')));
$query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
$query = db_select('accesslog', 'a', array('target' => 'slave'))
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query->join('users', 'u', 'a.uid = u.uid');
$query
......@@ -79,7 +81,9 @@ function statistics_user_tracker() {
array('data' => t('Timestamp'), 'field' => 'timestamp', 'sort' => 'desc'),
array('data' => t('Page'), 'field' => 'path'),
array('data' => t('Operations')));
$query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
$query = db_select('accesslog', 'a', array('target' => 'slave'))
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$query
->fields('a', array('aid', 'timestamp', 'path', 'title'))
->condition('uid', $account->uid)
......
......@@ -2921,7 +2921,9 @@ function system_actions_manage() {
array('data' => t('Label'), 'field' => 'label'),
array('data' => $instances_present ? t('Operations') : '', 'colspan' => '2')
);
$query = db_select('actions')->extend('PagerDefault')->extend('TableSort');
$query = db_select('actions')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('TableSort');
$result = $query
->fields('actions')
->limit(50)
......
......@@ -2087,7 +2087,7 @@ class DatabaseSelectComplexTestCase extends DatabaseTestCase {
function testHavingCountQuery() {
$query = db_select('test')
->extend('PagerDefault')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->groupBy('age')
->having('age + 1 > 0');
$query->addField('test', 'age');
......@@ -2352,7 +2352,8 @@ class DatabaseSelectPagerDefaultTestCase extends DatabaseTestCase {
* This is a regression test for #467984.
*/
function testInnerPagerQuery() {
$query = db_select('test', 't')->extend('PagerDefault');
$query = db_select('test', 't')
->extend('Drupal\Core\Database\Query\PagerSelectExtender');
$query
->fields('t', array('age'))