diff --git a/phpbb/config/default/container/services_mention.yml b/phpbb/config/default/container/services_mention.yml index c7fc969182..e1e8fa9a35 100644 --- a/phpbb/config/default/container/services_mention.yml +++ b/phpbb/config/default/container/services_mention.yml @@ -3,6 +3,7 @@ services: phpbb.mention.controller: class: phpbb\mention\controller\mention arguments: + - '@phpbb.mention.source_collection' - '@request' - '%core.root_path%' - '%core.php_ext%' @@ -15,11 +16,17 @@ services: tags: - { name: service_collection, tag: mention.source } + phpbb.mention.source.friend: + class: phpbb\mention\source\friend + arguments: + - '@dbal.conn' + - '@user' + tags: + - { name: mention.source } + phpbb.mention.source.topic: class: phpbb\mention\source\topic arguments: - '@dbal.conn' - - '@request' tags: - { name: mention.source } - diff --git a/phpbb/phpbb/mention/controller/mention.php b/phpbb/phpbb/mention/controller/mention.php index b9e51eed28..14ec892269 100644 --- a/phpbb/phpbb/mention/controller/mention.php +++ b/phpbb/phpbb/mention/controller/mention.php @@ -17,6 +17,9 @@ use Symfony\Component\HttpFoundation\JsonResponse; class mention { + /** @var \phpbb\di\service_collection */ + protected $mention_sources; + /** @var \phpbb\request\request_interface */ protected $request; @@ -30,8 +33,9 @@ class mention * Constructor * */ - public function __construct(\phpbb\request\request_interface $request, $phpbb_root_path, $phpEx) + public function __construct($mention_sources, \phpbb\request\request_interface $request, $phpbb_root_path, $phpEx) { + $this->mention_sources = $mention_sources; $this->request = $request; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $phpEx; @@ -44,9 +48,15 @@ class mention redirect(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext)); } + $keyword = $this->request->variable('keyword', '', true); $topic_id = $this->request->variable('topic_id', 0); - // TODO + $names = []; - return new JsonResponse(); + foreach ($this->mention_sources as $source) + { + $names = array_merge($names, $source->get($keyword, $topic_id)); + } + + return new JsonResponse($names); } } diff --git a/phpbb/phpbb/mention/source/friend.php b/phpbb/phpbb/mention/source/friend.php new file mode 100644 index 0000000000..c0cbc261ce --- /dev/null +++ b/phpbb/phpbb/mention/source/friend.php @@ -0,0 +1,56 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\mention\source; + +class friend extends user +{ + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var \phpbb\user */ + protected $user; + + /** + * Constructor + */ + public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user) + { + $this->db = $db; + $this->user = $user; + + parent::__construct($db); + } + + protected function query($keyword, $topic_id) + { + $query = $this->db->sql_build_query('SELECT', [ + 'SELECT' => 'u.username, u.user_id', + 'FROM' => [ + USERS_TABLE => 'u', + ], + 'LEFT_JOIN' => [ + [ + 'FROM' => [ZEBRA_TABLE => 'z'], + 'ON' => 'u.user_id = z.zebra_id' + ] + ], + 'WHERE' => 'z.friend = 1 AND z.user_id = ' . (int) $this->user->data['user_id'] . ' + AND u.user_id <> ' . ANONYMOUS . ' + 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()), + '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 739be108cd..553abb07b6 100644 --- a/phpbb/phpbb/mention/source/topic.php +++ b/phpbb/phpbb/mention/source/topic.php @@ -1,53 +1,38 @@ -* @license GNU General Public License, version 2 (GPL-2.0) -* -* For full copyright and license information, please see -* the docs/CREDITS.txt file. -* -*/ + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ -namespace phpbb\mention\method; +namespace phpbb\mention\source; -class topic +class topic extends user { - /** @var \phpbb\db\driver\driver_interface */ - protected $db; - - /** @var \phpbb\request\request_interface */ - protected $request; - - /** - * Constructor - */ - public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request) - { - $this->db = $db; - $this->request = $request; - } - - public function get($keyword, $topic_id) + protected function query($keyword, $topic_id) { $query = $this->db->sql_build_query('SELECT', [ - 'SELECT' => 'u.username, u.user_id', - 'FROM' => [ + 'SELECT' => 'u.username, u.user_id', + 'FROM' => [ USERS_TABLE => 'u', ], 'LEFT_JOIN' => [ - 'FROM' => [POSTS_TABLE => 'p'], - 'ON' => 'u.user_id = p.poster_id' + [ + 'FROM' => [POSTS_TABLE => 'p'], + 'ON' => 'u.user_id = p.poster_id' + ] ], - 'WHERE' => 'p.topic_id = ' . $topic_id . ' AND u.user_id <> ' . ANONYMOUS . ' - AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . ' + 'WHERE' => 'p.topic_id = ' . $topic_id . ' AND u.user_id <> ' . ANONYMOUS . ' + 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()), - 'ORDER_BY' => 'p.post_time DESC' + 'ORDER_BY' => 'p.post_time DESC' ]); - $res = $this->db->sql_query_limit($query, 5); - - return $this->db->sql_fetchrowset($res); + return $query; } } diff --git a/phpbb/phpbb/mention/source/user.php b/phpbb/phpbb/mention/source/user.php new file mode 100644 index 0000000000..216fafa36d --- /dev/null +++ b/phpbb/phpbb/mention/source/user.php @@ -0,0 +1,44 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\mention\source; + +abstract class user +{ + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** + * Constructor + */ + public function __construct(\phpbb\db\driver\driver_interface $db) + { + $this->db = $db; + } + + abstract protected function query($keyword, $topic_id); + + public function get($keyword, $topic_id) + { + $keyword = utf8_clean_string($keyword); + $res = $this->db->sql_query_limit($this->query($keyword, $topic_id), 5); + + $names = []; + while ($row = $this->db->sql_fetchrow($res)) + { + $names['u' . $row['user_id']] = $row['username']; + } + + return $names; + } +}