Optimize search result fetching to display posts/topics

This commit is contained in:
Visman 2023-07-13 23:28:51 +07:00
parent 6cb89f39e8
commit da24c4751c
4 changed files with 49 additions and 5 deletions

View file

@ -59,7 +59,7 @@ class ActionP extends Method
$list = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
}
$this->model->numPages = (int) \ceil((\count($list) ?: 1) / $this->c->user->disp_posts);
$this->model->numPages = (int) \ceil(($this->model->count($list) ?: 1) / $this->c->user->disp_posts);
// нет такой страницы в результате поиска
if (! $this->model->hasPage()) {
@ -69,7 +69,7 @@ class ActionP extends Method
return [];
}
$this->model->idsList = \array_slice(
$this->model->idsList = $this->model->slice(
$list,
($this->model->page - 1) * $this->c->user->disp_posts,
(int) $this->c->user->disp_posts

View file

@ -115,7 +115,7 @@ class ActionT extends Method
$list = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
}
$this->model->numPages = (int) \ceil((\count($list) ?: 1) / $this->c->user->disp_topics);
$this->model->numPages = (int) \ceil(($this->model->count($list) ?: 1) / $this->c->user->disp_topics);
// нет такой страницы в результате поиска
if (! $this->model->hasPage()) {
@ -125,7 +125,7 @@ class ActionT extends Method
return [];
}
$this->model->idsList = \array_slice(
$this->model->idsList = $this->model->slice(
$list,
($this->model->page - 1) * $this->c->user->disp_topics,
(int) $this->c->user->disp_topics

View file

@ -74,7 +74,7 @@ class Execute extends Method
&& $delimiter <= $row['search_time']
) {
$result = \explode("\n", $row['search_data']);
$this->model->queryIds = '' == $result[0] ? [] : \array_map('\\intval', \explode(',', $result[0]));
$this->model->queryIds = $result[0];
$this->model->queryNoCache = false;
return true;

View file

@ -141,4 +141,48 @@ class Search extends Model
{
return \preg_match('%' . self::CJK_REGEX . '%u', $word) ? true : false; //?????
}
/**
* Выбирает срез из массива/строки номеров через запятую
*/
public function slice(string|array $data, int $offset, int $length): array
{
if (\is_array($data)) {
return \array_slice($data, $offset, $length);
}
$p = 0;
$i = 0;
while ($i < $offset) {
if (false === ($p = \strpos($data, ',', $p))) {
return [];
}
++$p;
++$i;
}
$e = $p;
$offset += $length;
while ($i < $offset) {
if (false === ($e = \strpos($data, ',', $e))) {
return \array_map('\\intval', \explode(',', \substr($data, $p)));
}
++$e;
++$i;
}
return \array_map('\\intval', \explode(',', \substr($data, $p, $e - $p - 1)));
}
/**
* Подсчитывает число элементов массива/строки номеров через запятую
*/
public function count(string|array $data): int
{
return \is_array($data) ? \count($data) : \substr_count($data, ',') + 1;
}
}