mirror of
https://github.com/phpbb/phpbb.git
synced 2025-08-10 18:54:08 +02:00
[ticket/13713] Rework batch size handling
PHPBB3-13713
This commit is contained in:
@@ -19,6 +19,7 @@ class add_mention_settings extends \phpbb\db\migration\migration
|
||||
{
|
||||
return array(
|
||||
array('config.add', array('allow_mentions', true)),
|
||||
array('config.add', array('mention_batch_size', 50)),
|
||||
array('config.add', array('mention_names_limit', 10)),
|
||||
|
||||
// Set up user permissions
|
||||
|
@@ -52,12 +52,16 @@ class mention
|
||||
$keyword = $this->request->variable('keyword', '', true);
|
||||
$topic_id = $this->request->variable('topic_id', 0);
|
||||
$names = [];
|
||||
$hasNamesRemaining = false;
|
||||
|
||||
foreach ($this->mention_sources as $source)
|
||||
{
|
||||
$source->get($names, $keyword, $topic_id);
|
||||
$hasNamesRemaining = !$source->get($names, $keyword, $topic_id) || $hasNamesRemaining;
|
||||
}
|
||||
|
||||
return new JsonResponse(array_values($names));
|
||||
return new JsonResponse([
|
||||
'names' => array_values($names),
|
||||
'all' => !$hasNamesRemaining,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,9 @@ abstract class base_group implements source_interface
|
||||
/** @var \phpbb\db\driver\driver_interface */
|
||||
protected $db;
|
||||
|
||||
/** @var \phpbb\config\config */
|
||||
protected $config;
|
||||
|
||||
/** @var \phpbb\group\helper */
|
||||
protected $helper;
|
||||
|
||||
@@ -39,9 +42,10 @@ abstract class base_group implements source_interface
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\group\helper $helper, \phpbb\user $user, \phpbb\auth\auth $auth, $phpbb_root_path, $phpEx)
|
||||
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\group\helper $helper, \phpbb\user $user, \phpbb\auth\auth $auth, $phpbb_root_path, $phpEx)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->config = $config;
|
||||
$this->helper = $helper;
|
||||
$this->user = $user;
|
||||
$this->auth = $auth;
|
||||
@@ -138,8 +142,15 @@ abstract class base_group implements source_interface
|
||||
$matches = preg_grep('/^' . preg_quote($keyword) . '.*/i', $groups['names']);
|
||||
$group_ids = array_intersect($group_ids, array_flip($matches));
|
||||
|
||||
$i = 0;
|
||||
foreach ($group_ids as $group_id)
|
||||
{
|
||||
if ($i >= $this->config['mention_batch_size'])
|
||||
{
|
||||
// Do not exceed the names limit
|
||||
return false;
|
||||
}
|
||||
|
||||
$group_rank = phpbb_get_user_rank($groups[$group_id], false);
|
||||
array_push($names, [
|
||||
'name' => $groups[$group_id]['group_name'],
|
||||
@@ -152,6 +163,10 @@ abstract class base_group implements source_interface
|
||||
'rank' => (isset($group_rank['title'])) ? $group_rank['title'] : '',
|
||||
'priority' => $this->get_priority($groups[$group_id]),
|
||||
]);
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -15,9 +15,6 @@ namespace phpbb\mention\source;
|
||||
|
||||
abstract class base_user implements source_interface
|
||||
{
|
||||
/** @var int */
|
||||
const NAMES_BATCH_SIZE = 100;
|
||||
|
||||
/** @var \phpbb\db\driver\driver_interface */
|
||||
protected $db;
|
||||
|
||||
@@ -73,6 +70,7 @@ abstract class base_user implements source_interface
|
||||
*/
|
||||
public function get(array &$names, $keyword, $topic_id)
|
||||
{
|
||||
$fetched_all = false;
|
||||
$keyword = utf8_clean_string($keyword);
|
||||
|
||||
// Do not query all possible users (just a moderate amount), cache results for 5 minutes
|
||||
@@ -81,12 +79,13 @@ abstract class base_user implements source_interface
|
||||
$i = 0;
|
||||
$users = [];
|
||||
$user_ids = [];
|
||||
while ($i < self::NAMES_BATCH_SIZE)
|
||||
while ($i < $this->config['mention_batch_size'])
|
||||
{
|
||||
$row = $this->db->sql_fetchrow($result);
|
||||
|
||||
if (!$row)
|
||||
{
|
||||
$fetched_all = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -100,6 +99,24 @@ abstract class base_user implements source_interface
|
||||
$user_ids[] = $row['user_id'];
|
||||
}
|
||||
|
||||
// Determine whether all usernames were fetched in current batch
|
||||
if (!$fetched_all)
|
||||
{
|
||||
$fetched_all = true;
|
||||
|
||||
while ($row = $this->db->sql_fetchrow($result))
|
||||
{
|
||||
if (!empty($keyword) && strpos($row['username_clean'], $keyword) !== 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// At least one username hasn't been fetched - exit loop
|
||||
$fetched_all = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
// Load all user data with a single SQL query, needed for ranks and avatars
|
||||
@@ -120,5 +137,7 @@ abstract class base_user implements source_interface
|
||||
'priority' => $this->get_priority($user),
|
||||
]);
|
||||
}
|
||||
|
||||
return $fetched_all;
|
||||
}
|
||||
}
|
||||
|
@@ -22,6 +22,7 @@ interface source_interface
|
||||
* @param array $names Array of already fetched data with names
|
||||
* @param string $keyword Search string
|
||||
* @param int $topic_id Current topic ID
|
||||
* @return bool Whether there are no more satisfying names left
|
||||
*/
|
||||
public function get(array &$names, $keyword, $topic_id);
|
||||
|
||||
|
Reference in New Issue
Block a user