xref: /plugin/combo/ComboStrap/Index.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3
4namespace ComboStrap;
5
6
7class Index
8{
9    /**
10     * @var Index
11     */
12    private static $index;
13
14    /**
15     * @var \dokuwiki\Search\Indexer
16     */
17    private $indexer;
18
19
20    /**
21     * Index constructor.
22     */
23    public function __construct()
24    {
25        $this->indexer = idx_get_indexer();
26    }
27
28    public static function getOrCreate(): Index
29    {
30        if (self::$index === null) {
31            self::$index = new Index();
32        }
33        return self::$index;
34    }
35
36    public function getPagesForMedia(WikiPath $media): array
37    {
38        $dokuwikiId = $media->getWikiId();
39        return $this->indexer->lookupKey('relation_media', $dokuwikiId);
40    }
41
42    /**
43     * Return a list of page id that have the same last name
44     *
45     * @param MarkupPath $pageToMatch
46     * @return MarkupPath[]
47     */
48    public function getPagesWithSameLastName(MarkupPath $pageToMatch): array
49    {
50        /**
51         * * A shortcut to:
52         * ```
53         * $pagesWithSameName = ft_pageLookup($pageName);
54         * ```
55         * where {@link ft_pageLookup()}
56         */
57
58        // There is two much pages with the start name
59        $lastName = $pageToMatch->getPathObject()->getLastName();
60        if ($lastName === Site::getIndexPageName()) {
61            return [];
62        }
63
64        $pageIdList = $this->indexer->getPages();
65
66        $matchedPages = [];
67        foreach ($pageIdList as $pageId) {
68            if ($pageToMatch->getWikiId() === $pageId) {
69                continue;
70            }
71            $actualPage = MarkupPath::createMarkupFromId($pageId);
72            if ($actualPage->getPathObject()->getLastName() === $lastName) {
73                $matchedPages[] = $actualPage;
74            }
75        }
76        return $matchedPages;
77
78    }
79
80    public function deletePage(MarkupPath $page)
81    {
82
83        $this->indexer->deletePage($page->getWikiId());
84
85    }
86}
87