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(Media $media): array
37    {
38        $dokuwikiId = $media->getPath()->getDokuwikiId();
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 string $pageIdSource
46     * @return array
47     */
48    public function getPagesWithSameLastName(string $pageIdSource): 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        $name = noNS($pageIdSource);
60        if ($name === Site::getHomePageName()) {
61            return [];
62        }
63
64        $pages = $this->indexer->getPages();
65
66        $matchesPages = [];
67        foreach ($pages as $pageId) {
68            if ($pageIdSource === $pageId) {
69                continue;
70            }
71            $page = DokuPath::createPagePathFromId($pageId);
72            if ($page->getLastName() === $name) {
73                $matchesPages[] = $pageId;
74            }
75        }
76        return $matchesPages;
77
78    }
79
80    public function deletePage(Page $page)
81    {
82
83        $this->indexer->deletePage($page->getDokuwikiId());
84
85    }
86}
87