diff --git a/core/includes/tablesort.inc b/core/includes/tablesort.inc index a900a612fe73..818b61d1b562 100644 --- a/core/includes/tablesort.inc +++ b/core/includes/tablesort.inc @@ -12,101 +12,6 @@ use Drupal\Core\Database\Query\SelectInterface; * column headers that the user can click on to sort the table by that column. */ -/** - * Query extender class for tablesort queries. - */ -class TableSort extends SelectExtender { - - /** - * The array of fields that can be sorted by. - * - * @var array - */ - protected $header = array(); - - public function __construct(SelectInterface $query, Connection $connection) { - parent::__construct($query, $connection); - - // 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. - $this->addTag('tablesort'); - } - - /** - * Order the query based on a header array. - * - * @see theme_table() - * @param $header - * Table header array. - * @return SelectQueryInterface - * The called object. - */ - public function orderByHeader(Array $header) { - $this->header = $header; - $ts = $this->init(); - if (!empty($ts['sql'])) { - // Based on code from db_escape_table(), but this can also contain a dot. - $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']); - - // Sort order can only be ASC or DESC. - $sort = drupal_strtoupper($ts['sort']); - $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : ''; - $this->orderBy($field, $sort); - } - return $this; - } - - /** - * Initialize the table sort context. - */ - protected function init() { - $ts = $this->order(); - $ts['sort'] = $this->getSort(); - $ts['query'] = $this->getQueryParameters(); - return $ts; - } - - /** - * Determine the current sort direction. - * - * @param $headers - * An array of column headers in the format described in theme_table(). - * @return - * The current sort direction ("asc" or "desc"). - */ - protected function getSort() { - return tablesort_get_sort($this->header); - } - - /** - * Compose a URL query parameter array to append to table sorting requests. - * - * @return - * A URL query parameter array that consists of all components of the current - * page request except for those pertaining to table sorting. - * - * @see tablesort_get_query_parameters() - */ - protected function getQueryParameters() { - return tablesort_get_query_parameters(); - } - - /** - * Determine the current sort criterion. - * - * @param $headers - * An array of column headers in the format described in theme_table(). - * @return - * An associative array describing the criterion, containing the keys: - * - "name": The localized title of the table column. - * - "sql": The name of the database field to sort on. - */ - protected function order() { - return tablesort_get_order($this->header); - } -} - /** * Initialize the table sort context. */ diff --git a/core/lib/Drupal/Core/Database/Query/TableSortExtender.php b/core/lib/Drupal/Core/Database/Query/TableSortExtender.php new file mode 100644 index 000000000000..cf1a0420cc17 --- /dev/null +++ b/core/lib/Drupal/Core/Database/Query/TableSortExtender.php @@ -0,0 +1,105 @@ +addTag('tablesort'); + } + + /** + * Order the query based on a header array. + * + * @param array $header + * Table header array. + * + * @return SelectQueryInterface + * The called object. + * + * @see theme_table() + */ + public function orderByHeader(array $header) { + $this->header = $header; + $ts = $this->init(); + if (!empty($ts['sql'])) { + // Based on code from db_escape_table(), but this can also contain a dot. + $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']); + + // Sort order can only be ASC or DESC. + $sort = drupal_strtoupper($ts['sort']); + $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : ''; + $this->orderBy($field, $sort); + } + return $this; + } + + /** + * Initialize the table sort context. + */ + protected function init() { + $ts = $this->order(); + $ts['sort'] = $this->getSort(); + $ts['query'] = $this->getQueryParameters(); + return $ts; + } + + /** + * Determine the current sort direction. + * + * @return + * The current sort direction ("asc" or "desc"). + * + * @see tablesort_get_sort() + */ + protected function getSort() { + return tablesort_get_sort($this->header); + } + + /** + * Compose a URL query parameter array to append to table sorting requests. + * + * @return + * A URL query parameter array that consists of all components of the current + * page request except for those pertaining to table sorting. + * + * @see tablesort_get_query_parameters() + */ + protected function getQueryParameters() { + return tablesort_get_query_parameters(); + } + + /** + * Determine the current sort criterion. + * + * @return + * An associative array describing the criterion, containing the keys: + * - "name": The localized title of the table column. + * - "sql": The name of the database field to sort on. + * + * @see tablesort_get_order() + */ + protected function order() { + return tablesort_get_order($this->header); + } +} diff --git a/core/modules/comment/comment.admin.inc b/core/modules/comment/comment.admin.inc index 3a853df6a814..bec68c56e1e7 100644 --- a/core/modules/comment/comment.admin.inc +++ b/core/modules/comment/comment.admin.inc @@ -81,7 +81,7 @@ function comment_admin_overview($form, &$form_state, $arg) { $query = db_select('comment', 'c') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->join('node', 'n', 'n.nid = c.nid'); $query->addField('n', 'title', 'node_title'); $query->addTag('node_access'); diff --git a/core/modules/dblog/dblog.admin.inc b/core/modules/dblog/dblog.admin.inc index dafc91b021ce..7c04b6ca0065 100644 --- a/core/modules/dblog/dblog.admin.inc +++ b/core/modules/dblog/dblog.admin.inc @@ -39,7 +39,7 @@ function dblog_overview() { $query = db_select('watchdog', 'w') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->leftJoin('users', 'u', 'w.uid = u.uid'); $query ->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link')) @@ -100,7 +100,7 @@ function dblog_top($type) { $query = db_select('watchdog', 'w') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->addExpression('COUNT(wid)', 'count'); $query = $query ->fields('w', array('message', 'variables')) diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index 25a4369363af..2f170426e255 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -895,7 +895,7 @@ function forum_get_topics($tid, $sortby, $forum_per_page) { $query = db_select('forum_index', 'f') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->fields('f'); $query ->condition('f.tid', $tid) @@ -918,7 +918,8 @@ function forum_get_topics($tid, $sortby, $forum_per_page) { if ($nids) { $nodes = node_load_multiple($nids); - $query = db_select('node', 'n')->extend('TableSort'); + $query = db_select('node', 'n') + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->fields('n', array('nid')); $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid'); diff --git a/core/modules/node/node.admin.inc b/core/modules/node/node.admin.inc index c933daaa2dc1..fc8d98a9eff8 100644 --- a/core/modules/node/node.admin.inc +++ b/core/modules/node/node.admin.inc @@ -449,7 +449,7 @@ function node_admin_nodes() { $query = db_select('node', 'n') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); node_build_filter_query($query); if (!user_access('bypass node access')) { diff --git a/core/modules/path/path.admin.inc b/core/modules/path/path.admin.inc index a884f9eaeaf8..4188effed9ae 100644 --- a/core/modules/path/path.admin.inc +++ b/core/modules/path/path.admin.inc @@ -29,7 +29,7 @@ function path_admin_overview($keys = NULL) { $query = db_select('url_alias') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); if ($keys) { // Replace wildcards with PDO wildcards. $query->condition('alias', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE'); diff --git a/core/modules/poll/poll.pages.inc b/core/modules/poll/poll.pages.inc index b04f99ccb051..732355de3e7f 100644 --- a/core/modules/poll/poll.pages.inc +++ b/core/modules/poll/poll.pages.inc @@ -58,7 +58,7 @@ function poll_votes($node) { $select = db_select('poll_vote', 'pv') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $select->join('poll_choice', 'pc', 'pv.chid = pc.chid'); $select->join('users', 'u', 'pv.uid = u.uid'); $queried_votes = $select diff --git a/core/modules/statistics/statistics.admin.inc b/core/modules/statistics/statistics.admin.inc index 84bf60d93ade..d84e07bc6c66 100644 --- a/core/modules/statistics/statistics.admin.inc +++ b/core/modules/statistics/statistics.admin.inc @@ -27,7 +27,7 @@ function statistics_recent_hits() { $query = db_select('accesslog', 'a', array('target' => 'slave')) ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->join('users', 'u', 'a.uid = u.uid'); $query ->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid')) @@ -75,7 +75,7 @@ function statistics_top_pages() { $query = db_select('accesslog', 'a', array('target' => 'slave')) ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->addExpression('COUNT(path)', 'hits'); // MAX(title) avoids having empty node titles which otherwise causes // duplicates in the top pages list. @@ -130,7 +130,7 @@ function statistics_top_visitors() { ); $query = db_select('accesslog', 'a', array('target' => 'slave')) ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->leftJoin('blocked_ips', 'bl', 'a.hostname = bl.ip'); $query->leftJoin('users', 'u', 'a.uid = u.uid'); @@ -192,7 +192,7 @@ function statistics_top_referrers() { ); $query = db_select('accesslog', 'a') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->addExpression('COUNT(url)', 'hits'); $query->addExpression('MAX(timestamp)', 'last'); diff --git a/core/modules/statistics/statistics.pages.inc b/core/modules/statistics/statistics.pages.inc index 524d59e51e79..90383bbc1e04 100644 --- a/core/modules/statistics/statistics.pages.inc +++ b/core/modules/statistics/statistics.pages.inc @@ -27,7 +27,7 @@ function statistics_node_tracker() { $query = db_select('accesslog', 'a', array('target' => 'slave')) ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query->join('users', 'u', 'a.uid = u.uid'); $query @@ -83,7 +83,7 @@ function statistics_user_tracker() { array('data' => t('Operations'))); $query = db_select('accesslog', 'a', array('target' => 'slave')) ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query ->fields('a', array('aid', 'timestamp', 'path', 'title')) ->condition('uid', $account->uid) diff --git a/core/modules/system/tests/tablesort.test b/core/modules/system/lib/Drupal/system/Tests/Common/TableSortExtenderUnitTest.php similarity index 96% rename from core/modules/system/tests/tablesort.test rename to core/modules/system/lib/Drupal/system/Tests/Common/TableSortExtenderUnitTest.php index 1dc35fee6577..dbb7d7b49370 100644 --- a/core/modules/system/tests/tablesort.test +++ b/core/modules/system/lib/Drupal/system/Tests/Common/TableSortExtenderUnitTest.php @@ -2,15 +2,17 @@ /** * @file - * Various tablesort tests. + * Definition of Drupal\system\Tests\Common\TableSortExtenderTest. */ +namespace Drupal\system\Tests\Common; + use Drupal\simpletest\UnitTestBase; /** * Test unicode handling features implemented in unicode.inc. */ -class TableSortTest extends UnitTestBase { +class TableSortExtenderUnitTest extends UnitTestBase { /** * Storage for initial value of $_GET. diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index 9c682f0ae93f..f34c766b0bc9 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -2924,7 +2924,7 @@ function system_actions_manage() { ); $query = db_select('actions') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $result = $query ->fields('actions') ->limit(50) diff --git a/core/modules/system/tests/modules/database_test/database_test.module b/core/modules/system/tests/modules/database_test/database_test.module index f248e5a00820..4500f741af50 100644 --- a/core/modules/system/tests/modules/database_test/database_test.module +++ b/core/modules/system/tests/modules/database_test/database_test.module @@ -161,7 +161,9 @@ function database_test_tablesort() { $query ->fields('t', array('tid', 'pid', 'task', 'priority')); - $query = $query->extend('TableSort')->orderByHeader($header); + $query = $query + ->extend('Drupal\Core\Database\Query\TableSortExtender') + ->orderByHeader($header); // We need all the results at once to check the sort. $tasks = $query->execute()->fetchAll(); @@ -190,7 +192,10 @@ function database_test_tablesort_first() { $query ->fields('t', array('tid', 'pid', 'task', 'priority')); - $query = $query->extend('TableSort')->orderByHeader($header)->orderBy('priority'); + $query = $query + ->extend('Drupal\Core\Database\Query\TableSortExtender') + ->orderByHeader($header) + ->orderBy('priority'); // We need all the results at once to check the sort. $tasks = $query->execute()->fetchAll(); diff --git a/core/modules/user/user.admin.inc b/core/modules/user/user.admin.inc index 9f3108c14935..be75907648f4 100644 --- a/core/modules/user/user.admin.inc +++ b/core/modules/user/user.admin.inc @@ -160,7 +160,7 @@ function user_admin_account() { $query = $query ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->extend('TableSort'); + ->extend('Drupal\Core\Database\Query\TableSortExtender'); $query ->fields('u', array('uid', 'name', 'status', 'created', 'access')) ->limit(50)