From e616ec025c287d70c98cb59919f9c7e9b8e7d26f Mon Sep 17 00:00:00 2001 From: lavigor Date: Sun, 22 Jul 2018 03:58:00 +0300 Subject: [PATCH] [ticket/13713] Fix priorities PHPBB3-13713 --- phpBB/phpbb/mention/controller/mention.php | 2 +- phpBB/phpbb/mention/source/group.php | 14 ++++++++++ .../phpbb/mention/source/source_interface.php | 2 ++ phpBB/phpbb/mention/source/team.php | 5 +++- phpBB/phpbb/mention/source/topic.php | 28 +++++++++++++++++-- phpBB/phpbb/mention/source/user.php | 18 ++++++++++-- 6 files changed, 62 insertions(+), 7 deletions(-) diff --git a/phpBB/phpbb/mention/controller/mention.php b/phpBB/phpbb/mention/controller/mention.php index d2b43d0914..770f84b015 100644 --- a/phpBB/phpbb/mention/controller/mention.php +++ b/phpBB/phpbb/mention/controller/mention.php @@ -46,7 +46,7 @@ class mention { if (!$this->request->is_ajax()) { - new RedirectResponse(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext)); + return new RedirectResponse(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext)); } $keyword = $this->request->variable('keyword', '', true); diff --git a/phpBB/phpbb/mention/source/group.php b/phpBB/phpbb/mention/source/group.php index d063d64324..cdd3596d75 100644 --- a/phpBB/phpbb/mention/source/group.php +++ b/phpBB/phpbb/mention/source/group.php @@ -15,6 +15,20 @@ namespace phpbb\mention\source; class group extends base_group { + /** + * {@inheritdoc} + */ + public function get_priority($row) + { + /* + * Presence in array with all names for this type should not increase the priority + * Otherwise names will not be properly sorted because we fetch them in batches + * and the name from 'special' source can be absent from the array with all names + * and therefore it will appear lower than needed + */ + return 0; + } + /** * {@inheritdoc} */ diff --git a/phpBB/phpbb/mention/source/source_interface.php b/phpBB/phpbb/mention/source/source_interface.php index 731aedc763..075bea295d 100644 --- a/phpBB/phpbb/mention/source/source_interface.php +++ b/phpBB/phpbb/mention/source/source_interface.php @@ -27,6 +27,8 @@ interface source_interface /** * Returns the priority of the currently selected name + * Please note that simple inner priorities for a certain source + * can be set with ORDER BY SQL clause * * @param array $row Array of fetched data for the name type (e.g. user row) * @return int Priority (defaults to 1) diff --git a/phpBB/phpbb/mention/source/team.php b/phpBB/phpbb/mention/source/team.php index 89c1f4071e..cf373e8b03 100644 --- a/phpBB/phpbb/mention/source/team.php +++ b/phpBB/phpbb/mention/source/team.php @@ -21,11 +21,14 @@ class team extends base_user protected function query($keyword, $topic_id) { /* + * Select unique names of team members: each name should be selected only once + * regardless of the number of groups the certain user is a member of + * * For optimization purposes all team members are returned regardless of the keyword * Names filtering is done on the frontend * Results will be cached in a single file */ - $query = $this->db->sql_build_query('SELECT', [ + $query = $this->db->sql_build_query('SELECT_DISTINCT', [ 'SELECT' => 'u.username_clean, u.username, u.user_id', 'FROM' => [ USERS_TABLE => 'u', diff --git a/phpBB/phpbb/mention/source/topic.php b/phpBB/phpbb/mention/source/topic.php index da1d6152d6..86b01d37fc 100644 --- a/phpBB/phpbb/mention/source/topic.php +++ b/phpBB/phpbb/mention/source/topic.php @@ -15,18 +15,38 @@ namespace phpbb\mention\source; class topic extends base_user { + /** + * {@inheritdoc} + */ + public function get_priority($row) + { + /* + * Topic's open poster is probably the most mentionable user in the topic + * so we give him a significant priority + */ + if ($row['user_id'] === $row['topic_poster']) + { + return 5; + } + + return 1; + } + /** * {@inheritdoc} */ protected function query($keyword, $topic_id) { /* + * Select poster's username together with topic author's ID + * that will be later used for priotirisation + * * For optimization purposes all users are returned regardless of the keyword * Names filtering is done on the frontend * Results will be cached on a per-topic basis */ $query = $this->db->sql_build_query('SELECT', [ - 'SELECT' => 'u.username, u.user_id', + 'SELECT' => 'u.username, u.user_id, t.topic_poster', 'FROM' => [ USERS_TABLE => 'u', ], @@ -34,7 +54,11 @@ class topic extends base_user [ 'FROM' => [POSTS_TABLE => 'p'], 'ON' => 'u.user_id = p.poster_id' - ] + ], + [ + 'FROM' => [TOPICS_TABLE => 't'], + 'ON' => 't.topic_id = p.topic_id' + ], ], 'WHERE' => 'p.topic_id = ' . $topic_id . ' AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]), diff --git a/phpBB/phpbb/mention/source/user.php b/phpBB/phpbb/mention/source/user.php index d37ff416c0..dedafd7d24 100644 --- a/phpBB/phpbb/mention/source/user.php +++ b/phpBB/phpbb/mention/source/user.php @@ -15,19 +15,31 @@ namespace phpbb\mention\source; class user extends base_user { + /** + * {@inheritdoc} + */ + public function get_priority($row) + { + /* + * Presence in array with all names for this type should not increase the priority + * Otherwise names will not be properly sorted because we fetch them in batches + * and the name from 'special' source can be absent from the array with all names + * and therefore it will appear lower than needed + */ + return 0; + } + /** * {@inheritdoc} */ protected function query($keyword, $topic_id) { - // TODO: think about caching ALL users: 1m users results to ~40MB file $query = $this->db->sql_build_query('SELECT', [ 'SELECT' => 'u.username_clean, u.username, u.user_id', 'FROM' => [ USERS_TABLE => 'u', ], - 'WHERE' => $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER])/* . ' - AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char())*/, + 'WHERE' => $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]), 'ORDER_BY' => 'u.user_lastvisit DESC' ]); return $query;