diff --git a/phpBB/phpbb/mention/controller/mention.php b/phpBB/phpbb/mention/controller/mention.php index a188ea6bf8..d2b43d0914 100644 --- a/phpBB/phpbb/mention/controller/mention.php +++ b/phpBB/phpbb/mention/controller/mention.php @@ -44,10 +44,10 @@ class mention public function handle() { -// if (!$this->request->is_ajax()) -// { -// new RedirectResponse(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext)); -// } + if (!$this->request->is_ajax()) + { + new RedirectResponse(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext)); + } $keyword = $this->request->variable('keyword', '', true); $topic_id = $this->request->variable('topic_id', 0); diff --git a/phpBB/phpbb/mention/source/base_group.php b/phpBB/phpbb/mention/source/base_group.php index d7037f77ae..607fb91cc9 100644 --- a/phpBB/phpbb/mention/source/base_group.php +++ b/phpBB/phpbb/mention/source/base_group.php @@ -68,14 +68,15 @@ abstract class base_group implements source_interface 'FROM' => [ GROUPS_TABLE => 'g', ], - 'LEFT_JOIN' => array( - array( - 'FROM' => array(USER_GROUP_TABLE => 'ug'), + 'LEFT_JOIN' => [ + [ + 'FROM' => [USER_GROUP_TABLE => 'ug'], 'ON' => 'ug.group_id = g.group_id AND ug.user_pending = 0 AND ug.user_id = ' . (int) $this->user->data['user_id'], - ), - ), + ], + ], ]); - $result = $this->db->sql_query($query); + // Cache results for 5 minutes + $result = $this->db->sql_query($query, 600); $this->groups = []; while ($row = $this->db->sql_fetchrow($result)) @@ -111,8 +112,8 @@ abstract class base_group implements source_interface */ public function get(array &$names, $keyword, $topic_id) { - // Grab all group IDs - $result = $this->db->sql_query($this->query($keyword, $topic_id)); + // Grab all group IDs, cache for 5 minutes + $result = $this->db->sql_query($this->query($keyword, $topic_id), 300); $group_ids = []; while ($row = $this->db->sql_fetchrow($result)) @@ -142,7 +143,5 @@ abstract class base_group implements source_interface 'rank' => $group_rank['title'], ]); } - - return $names; } } diff --git a/phpBB/phpbb/mention/source/base_user.php b/phpBB/phpbb/mention/source/base_user.php index 7ac830fc6e..8ab1d05df1 100644 --- a/phpBB/phpbb/mention/source/base_user.php +++ b/phpBB/phpbb/mention/source/base_user.php @@ -76,10 +76,27 @@ abstract class base_user implements source_interface public function get(array &$names, $keyword, $topic_id) { $keyword = utf8_clean_string($keyword); - $result = $this->db->sql_query_limit($this->query($keyword, $topic_id), self::NAMES_BATCH_SIZE); - while ($row = $this->db->sql_fetchrow($result)) + // Do not query all possible users (just a moderate amount), cache results for 5 minutes + $result = $this->db->sql_query($this->query($keyword, $topic_id), 300); + + $i = 0; + while ($i < self::NAMES_BATCH_SIZE) { + $row = $this->db->sql_fetchrow($result); + + if (!$row) + { + break; + } + + if (!empty($keyword) && strpos($row['username_clean'], $keyword) !== 0) + { + continue; + } + + $i++; + $user_rank = $this->user_loader->get_rank($row['user_id'], true); array_push($names, [ 'name' => $row['username'], @@ -95,7 +112,5 @@ abstract class base_user implements source_interface } $this->db->sql_freeresult($result); - - return $names; } } diff --git a/phpBB/phpbb/mention/source/friend.php b/phpBB/phpbb/mention/source/friend.php index 60e706b458..dfd90a813c 100644 --- a/phpBB/phpbb/mention/source/friend.php +++ b/phpBB/phpbb/mention/source/friend.php @@ -33,8 +33,13 @@ class friend extends base_user */ protected function query($keyword, $topic_id) { + /* + * For optimization purposes all friends are returned regardless of the keyword + * Names filtering is done on the frontend + * Results will be cached on a per-user basis + */ $query = $this->db->sql_build_query('SELECT', [ - 'SELECT' => 'u.username, u.user_id', + 'SELECT' => 'u.username_clean, u.username, u.user_id', 'FROM' => [ USERS_TABLE => 'u', ], @@ -45,8 +50,7 @@ class friend extends base_user ] ], 'WHERE' => 'z.friend = 1 AND z.user_id = ' . (int) $this->user->data['user_id'] . ' - AND ' . $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()), + AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]), 'ORDER_BY' => 'u.user_lastvisit DESC' ]); return $query; diff --git a/phpBB/phpbb/mention/source/source_interface.php b/phpBB/phpbb/mention/source/source_interface.php index 43b6106363..7c7da7369f 100644 --- a/phpBB/phpbb/mention/source/source_interface.php +++ b/phpBB/phpbb/mention/source/source_interface.php @@ -17,12 +17,11 @@ interface source_interface { /** * Searches database for names to mention - * and returns and array of found items + * and alters the passed array of found items * * @param array $names Array of already fetched data with names * @param string $keyword Search string * @param int $topic_id Current topic ID - * @return array Array of names */ public function get(array &$names, $keyword, $topic_id); } diff --git a/phpBB/phpbb/mention/source/team.php b/phpBB/phpbb/mention/source/team.php index 2cfffd9f82..89c1f4071e 100644 --- a/phpBB/phpbb/mention/source/team.php +++ b/phpBB/phpbb/mention/source/team.php @@ -20,16 +20,20 @@ class team extends base_user */ protected function query($keyword, $topic_id) { + /* + * 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', [ - 'SELECT' => 'u.username, u.user_id', + 'SELECT' => 'u.username_clean, u.username, u.user_id', 'FROM' => [ USERS_TABLE => 'u', USER_GROUP_TABLE => 'ug', TEAMPAGE_TABLE => 't', ], 'WHERE' => 'ug.group_id = t.group_id AND ug.user_id = u.user_id AND ug.user_pending = 0 - AND ' . $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()), + AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]), 'ORDER_BY' => 'u.user_lastvisit DESC' ]); return $query; diff --git a/phpBB/phpbb/mention/source/topic.php b/phpBB/phpbb/mention/source/topic.php index 688495ddc7..da1d6152d6 100644 --- a/phpBB/phpbb/mention/source/topic.php +++ b/phpBB/phpbb/mention/source/topic.php @@ -20,6 +20,11 @@ class topic extends base_user */ protected function query($keyword, $topic_id) { + /* + * 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', 'FROM' => [ @@ -32,8 +37,7 @@ class topic extends base_user ] ], 'WHERE' => 'p.topic_id = ' . $topic_id . ' - AND ' . $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()), + AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]), 'ORDER_BY' => 'p.post_time DESC' ]); return $query; diff --git a/phpBB/phpbb/mention/source/user.php b/phpBB/phpbb/mention/source/user.php index 8a03bc41a8..d37ff416c0 100644 --- a/phpBB/phpbb/mention/source/user.php +++ b/phpBB/phpbb/mention/source/user.php @@ -20,13 +20,14 @@ class user extends base_user */ 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, u.user_id', + '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])/* . ' + AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char())*/, 'ORDER_BY' => 'u.user_lastvisit DESC' ]); return $query; diff --git a/phpBB/phpbb/mention/source/usergroup.php b/phpBB/phpbb/mention/source/usergroup.php index c3b95ffb49..fd1184ff60 100644 --- a/phpBB/phpbb/mention/source/usergroup.php +++ b/phpBB/phpbb/mention/source/usergroup.php @@ -31,7 +31,7 @@ class usergroup extends base_group 'ON' => 'g.group_id = ug.group_id' ] ], - 'WHERE' => 'ug.user_id = ' . (int) $this->user->data['user_id'], + 'WHERE' => 'ug.user_pending = 0 AND ug.user_id = ' . (int) $this->user->data['user_id'], ]); return $query; }