Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Glpi/Controller/Knowbase/AsideSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public function __invoke(Request $request): JsonResponse

// Get article IDs that match this filter
$criteria = KnowbaseItem::getListRequest(['contains' => $contains], 'search');

// The response is only used for membership tests, so neither the article
// columns (`glpi_knowbaseitems.*` includes `answer`) nor the relevance
// ordering are needed.
$criteria['SELECT'] = [KnowbaseItem::getTableField('id')];
unset($criteria['ORDERBY']);

$ids = [];
foreach ($DB->request($criteria) as $data) {
$ids[] = (int) $data['id'];
Expand Down
29 changes: 17 additions & 12 deletions src/Glpi/Knowbase/Aside/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@
*/
final class Builder
{
/** @var int[] */
private array $folded_ids = [];
public const array LIST_COLUMNS = [
'glpi_knowbaseitems.id',
'glpi_knowbaseitems.name',
'glpi_knowbaseitems.illustration',
];

/** @var array<int, true> */
private array $folded_ids_lookup_map = [];

public function __construct(private readonly int $current_id = 0) {}

Expand All @@ -60,10 +66,12 @@ public function buildTree(): Tree
global $DB;

// Articles the current user has collapsed, restored on each render.
$this->folded_ids = KnowbaseItem::getFoldedIdsForCurrentUser();
$this->folded_ids_lookup_map = array_fill_keys(KnowbaseItem::getFoldedIdsForCurrentUser(), true);

// 1) All articles the current user may see (visibility applied).
$rows = $DB->request(KnowbaseItem::getListRequest([], 'browse'));
$criteria = KnowbaseItem::getListRequest([], 'browse');
$criteria['SELECT'] = self::LIST_COLUMNS;
$rows = $DB->request($criteria);
$data = []; // id => row
foreach ($rows as $row) {
$data[(int) $row['id']] = $row;
Expand All @@ -76,15 +84,12 @@ public function buildTree(): Tree
// 2) Visible parent -> [visible children] adjacency, and child -> has a visible parent?
$children_of = []; // parent_id => int[] child ids
$has_visible_parent = []; // child_id => true
foreach ($DB->request([
'FROM' => KnowbaseItem_KnowbaseItem::getTable(),
'WHERE' => [
'knowbaseitems_id' => $visible_ids,
'knowbaseitems_id_parent' => $visible_ids,
],
]) as $link) {
foreach ($DB->request(['FROM' => KnowbaseItem_KnowbaseItem::getTable()]) as $link) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
foreach ($DB->request(['FROM' => KnowbaseItem_KnowbaseItem::getTable()]) as $link) {
$links = $DB->request(['FROM' => KnowbaseItem_KnowbaseItem::getTable()]);
foreach ($links as $link) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong with this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's far better for performances to not call "costly" methods in a foreach loop

$child = (int) $link['knowbaseitems_id'];
$parent = (int) $link['knowbaseitems_id_parent'];
if (!isset($data[$child], $data[$parent])) {
continue; // one of the ends is not visible to the current user
}
$children_of[$parent][] = $child;
$has_visible_parent[$child] = true;
}
Expand Down Expand Up @@ -113,7 +118,7 @@ private function buildArticle(int $id, array $data, array $children_of, array $a
illustration: $row['illustration'] ?? '',
link: KnowbaseItem::getFormURLWithID($id),
is_current: $this->current_id > 0 && $id === $this->current_id,
collapsed: in_array($id, $this->folded_ids, true),
collapsed: isset($this->folded_ids_lookup_map[$id]),
);
$ancestors[$id] = true;
foreach ($children_of[$id] ?? [] as $child_id) {
Expand Down
1 change: 1 addition & 0 deletions src/KnowbaseItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3373,6 +3373,7 @@ private function getCurrentArticleAndFavorites(int $current_id = 0): array
}

$criteria = self::getListRequest([], 'browse');
$criteria['SELECT'] = Builder::LIST_COLUMNS;

$is_favorite_condition = [
self::getTable() . '.id' => new QuerySubQuery([
Expand Down
Loading