xref: /dokuwiki/inc/Search/Indexer.php (revision 79dae64d6746363b953e6c82844ef285c37c3310)
16225b270SMichael Große<?php
26225b270SMichael Große
36225b270SMichael Großenamespace dokuwiki\Search;
46225b270SMichael Große
5e1272c08SAndreas Gohruse dokuwiki\Debug\DebugHelper;
66225b270SMichael Großeuse dokuwiki\Extension\Event;
783b3acccSAndreas Gohruse dokuwiki\Search\Collection\PageFulltextCollection;
883b3acccSAndreas Gohruse dokuwiki\Search\Collection\PageMetaCollection;
983b3acccSAndreas Gohruse dokuwiki\Search\Collection\PageTitleCollection;
1015f699acSAndreas Gohruse dokuwiki\Search\Exception\IndexAccessException;
1121fbd01bSAndreas Gohruse dokuwiki\Search\Exception\IndexIntegrityException;
12a16bd548SSatoshi Saharause dokuwiki\Search\Exception\IndexLockException;
13a16bd548SSatoshi Saharause dokuwiki\Search\Exception\IndexWriteException;
1483b3acccSAndreas Gohruse dokuwiki\Search\Index\FileIndex;
1583b3acccSAndreas Gohruse dokuwiki\Search\Index\Lock;
16e1272c08SAndreas Gohruse dokuwiki\Search\Index\MemoryIndex;
174027a91aSSatoshi Sahara
184027a91aSSatoshi Sahara// Version tag used to force rebuild on upgrade
195d034a75SAndreas Gohrconst INDEXER_VERSION = 9;
206225b270SMichael Große
216225b270SMichael Große/**
22a32da6ddSSatoshi Sahara * Class DokuWiki Indexer
236225b270SMichael Große *
2483b3acccSAndreas Gohr * Manages the page search index by delegating to Collection classes.
2583b3acccSAndreas Gohr *
264027a91aSSatoshi Sahara * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
276225b270SMichael Große * @author     Andreas Gohr <andi@splitbrain.org>
284027a91aSSatoshi Sahara * @author Tom N Harris <tnharris@whoopdedo.org>
296225b270SMichael Große */
3083b3acccSAndreas Gohrclass Indexer
314027a91aSSatoshi Sahara{
3283b3acccSAndreas Gohr    /** @var callable|null Logging callback, receives a string message */
3383b3acccSAndreas Gohr    protected $logger;
346225b270SMichael Große
354027a91aSSatoshi Sahara    /**
3683b3acccSAndreas Gohr     * Set a logging callback
374027a91aSSatoshi Sahara     *
3883b3acccSAndreas Gohr     * The callback receives a single string message. Use this to integrate
3983b3acccSAndreas Gohr     * with different output mechanisms (TaskRunner echo, CLI output, Logger, etc.)
4083b3acccSAndreas Gohr     *
4183b3acccSAndreas Gohr     * @param callable $logger
4283b3acccSAndreas Gohr     * @return static
434027a91aSSatoshi Sahara     */
4483b3acccSAndreas Gohr    public function setLogger(callable $logger): static
454027a91aSSatoshi Sahara    {
4683b3acccSAndreas Gohr        $this->logger = $logger;
4783b3acccSAndreas Gohr        return $this;
486225b270SMichael Große    }
496225b270SMichael Große
506225b270SMichael Große    /**
5183b3acccSAndreas Gohr     * Send a message to the registered logger
526225b270SMichael Große     *
5383b3acccSAndreas Gohr     * @param string $message
546225b270SMichael Große     */
5583b3acccSAndreas Gohr    protected function log(string $message): void
564027a91aSSatoshi Sahara    {
5783b3acccSAndreas Gohr        if ($this->logger)($this->logger)($message);
586225b270SMichael Große    }
596225b270SMichael Große
606225b270SMichael Große    /**
614027a91aSSatoshi Sahara     * Version of the indexer taking into consideration the external tokenizer.
624027a91aSSatoshi Sahara     * The indexer is only compatible with data written by the same version.
636225b270SMichael Große     *
644027a91aSSatoshi Sahara     * @triggers INDEXER_VERSION_GET
654027a91aSSatoshi Sahara     * Plugins that modify what gets indexed should hook this event and
664027a91aSSatoshi Sahara     * add their version info to the event data like so:
674027a91aSSatoshi Sahara     *     $data[$plugin_name] = $plugin_version;
686225b270SMichael Große     *
694027a91aSSatoshi Sahara     * @return int|string
706225b270SMichael Große     */
719369b4a9SAndreas Gohr    public function getVersion(): int|string
724027a91aSSatoshi Sahara    {
734027a91aSSatoshi Sahara        static $indexer_version = null;
744027a91aSSatoshi Sahara        if ($indexer_version == null) {
754027a91aSSatoshi Sahara            $version = INDEXER_VERSION;
764027a91aSSatoshi Sahara
7783b3acccSAndreas Gohr            $data = ['dokuwiki' => $version];
784027a91aSSatoshi Sahara            Event::createAndTrigger('INDEXER_VERSION_GET', $data, null, false);
794027a91aSSatoshi Sahara            unset($data['dokuwiki']); // this needs to be first
804027a91aSSatoshi Sahara            ksort($data);
814027a91aSSatoshi Sahara            foreach ($data as $plugin => $vers) {
824027a91aSSatoshi Sahara                $version .= '+' . $plugin . '=' . $vers;
834027a91aSSatoshi Sahara            }
844027a91aSSatoshi Sahara            $indexer_version = $version;
854027a91aSSatoshi Sahara        }
864027a91aSSatoshi Sahara        return $indexer_version;
876225b270SMichael Große    }
886225b270SMichael Große
894027a91aSSatoshi Sahara    /**
9083b3acccSAndreas Gohr     * Return a list of all indexed pages
9183b3acccSAndreas Gohr     *
9283b3acccSAndreas Gohr     * @param bool $existsFilter only return pages that exist on disk
9383b3acccSAndreas Gohr     * @return string[] list of page names (keys are the RIDs in the page index)
9483b3acccSAndreas Gohr     */
9583b3acccSAndreas Gohr    public function getAllPages(bool $existsFilter = false): array
9683b3acccSAndreas Gohr    {
979369b4a9SAndreas Gohr        $pageIndex = new MemoryIndex('page');
9883b3acccSAndreas Gohr        return array_filter(
9983b3acccSAndreas Gohr            iterator_to_array($pageIndex),
10083b3acccSAndreas Gohr            static fn($v) => $v !== '' && (!$existsFilter || page_exists($v, '', false))
10183b3acccSAndreas Gohr        );
10283b3acccSAndreas Gohr    }
10383b3acccSAndreas Gohr
10483b3acccSAndreas Gohr    /**
10583b3acccSAndreas Gohr     * Check if a page needs (re-)indexing
10683b3acccSAndreas Gohr     *
10783b3acccSAndreas Gohr     * @param string $page
10883b3acccSAndreas Gohr     * @param bool $force
10983b3acccSAndreas Gohr     * @return bool true if indexing is needed
11083b3acccSAndreas Gohr     */
11183b3acccSAndreas Gohr    public function needsIndexing(string $page, bool $force = false): bool
11283b3acccSAndreas Gohr    {
11383b3acccSAndreas Gohr        $idxtag = metaFN($page, '.indexed');
11483b3acccSAndreas Gohr        if ($force || !file_exists($idxtag)) return true;
11583b3acccSAndreas Gohr
11683b3acccSAndreas Gohr        if (trim(io_readFile($idxtag)) != $this->getVersion()) return true;
11783b3acccSAndreas Gohr
118*79dae64dSAndreas Gohr        // the index tag is written when the page is indexed; the page only needs
119*79dae64dSAndreas Gohr        // (re-)indexing if it was changed *after* that - an equal mtime means it was
120*79dae64dSAndreas Gohr        // saved and indexed within the same second and is therefore up to date
12183b3acccSAndreas Gohr        $last = @filemtime($idxtag);
122*79dae64dSAndreas Gohr        return $last < @filemtime(wikiFN($page));
12383b3acccSAndreas Gohr    }
12483b3acccSAndreas Gohr
12583b3acccSAndreas Gohr    /**
12683b3acccSAndreas Gohr     * Add/update the search index for a page
1274027a91aSSatoshi Sahara     *
1284027a91aSSatoshi Sahara     * Locking is handled internally.
1294027a91aSSatoshi Sahara     *
13083b3acccSAndreas Gohr     * @param string $page The page to index
1314027a91aSSatoshi Sahara     * @param bool $force force reindexing even when the index is up to date
1324027a91aSSatoshi Sahara     *
133a32da6ddSSatoshi Sahara     * @throws IndexAccessException
134a16bd548SSatoshi Sahara     * @throws IndexLockException
135a16bd548SSatoshi Sahara     * @throws IndexWriteException
1364027a91aSSatoshi Sahara     */
13783b3acccSAndreas Gohr    public function addPage(string $page, bool $force = false): void
1384027a91aSSatoshi Sahara    {
13983b3acccSAndreas Gohr        if (!$this->needsIndexing($page, $force)) {
1409369b4a9SAndreas Gohr            $this->log("Indexer: index for $page up to date");
14183b3acccSAndreas Gohr            return;
142a32da6ddSSatoshi Sahara        }
143a32da6ddSSatoshi Sahara
14483b3acccSAndreas Gohr        // create shared writable page index early so we can resolve the PID for plugins
14583b3acccSAndreas Gohr        $pageIndex = new FileIndex('page', '', true);
1466225b270SMichael Große
14783b3acccSAndreas Gohr        // prepare event data
14883b3acccSAndreas Gohr        $data = [
14983b3acccSAndreas Gohr            'page' => $page,
15083b3acccSAndreas Gohr            'body' => '',
15183b3acccSAndreas Gohr            'metadata' => [
15283b3acccSAndreas Gohr                'title' => p_get_metadata($page, 'title', METADATA_RENDER_UNLIMITED),
15383b3acccSAndreas Gohr                'relation_references' => array_keys(
15483b3acccSAndreas Gohr                    p_get_metadata($page, 'relation references', METADATA_RENDER_UNLIMITED) ?? []
15583b3acccSAndreas Gohr                ),
15683b3acccSAndreas Gohr                'relation_media' => array_keys(
15783b3acccSAndreas Gohr                    p_get_metadata($page, 'relation media', METADATA_RENDER_UNLIMITED) ?? []
15883b3acccSAndreas Gohr                ),
15983b3acccSAndreas Gohr                'internal_index' => p_get_metadata($page, 'internal index', METADATA_RENDER_UNLIMITED) !== false,
16083b3acccSAndreas Gohr            ],
16183b3acccSAndreas Gohr            'pid' => $pageIndex->accessCachedValue($page),
16283b3acccSAndreas Gohr        ];
1636225b270SMichael Große
16483b3acccSAndreas Gohr        // let plugins modify the data
1654027a91aSSatoshi Sahara        $event = new Event('INDEXER_PAGE_ADD', $data);
16683b3acccSAndreas Gohr        if ($event->advise_before()) {
16783b3acccSAndreas Gohr            $data['body'] = $data['body'] . ' ' . rawWiki($data['page']);
16883b3acccSAndreas Gohr        }
1694027a91aSSatoshi Sahara        $event->advise_after();
1704027a91aSSatoshi Sahara        unset($event);
1716225b270SMichael Große
17283b3acccSAndreas Gohr        // index title
17383b3acccSAndreas Gohr        (new PageTitleCollection($pageIndex))->lock()
17483b3acccSAndreas Gohr            ->addEntity($data['page'], [$data['metadata']['title']])->unlock();
17583b3acccSAndreas Gohr        unset($data['metadata']['title']);
1766225b270SMichael Große
17783b3acccSAndreas Gohr        // index fulltext
17883b3acccSAndreas Gohr        if ($data['metadata']['internal_index']) {
17983b3acccSAndreas Gohr            $words = Tokenizer::getWords($data['body']);
18083b3acccSAndreas Gohr            (new PageFulltextCollection($pageIndex))->lock()->addEntity($data['page'], $words)->unlock();
1816225b270SMichael Große        } else {
18283b3acccSAndreas Gohr            $this->log("Indexer: full text indexing disabled for {$data['page']}");
18383b3acccSAndreas Gohr            // clear any previously stored fulltext data
18483b3acccSAndreas Gohr            (new PageFulltextCollection($pageIndex))->lock()->addEntity($data['page'], [])->unlock();
1856225b270SMichael Große        }
18683b3acccSAndreas Gohr        unset($data['metadata']['internal_index']);
18783b3acccSAndreas Gohr
18883b3acccSAndreas Gohr        // index metadata keys
18983b3acccSAndreas Gohr        foreach ($data['metadata'] as $key => $values) {
19083b3acccSAndreas Gohr            if (!is_array($values)) {
19183b3acccSAndreas Gohr                $values = ($values !== null && $values !== '') ? [$values] : [];
1926225b270SMichael Große            }
19383b3acccSAndreas Gohr            (new PageMetaCollection($key, $pageIndex))->lock()->addEntity($data['page'], $values)->unlock();
19483b3acccSAndreas Gohr        }
19583b3acccSAndreas Gohr
19683b3acccSAndreas Gohr        // update metadata registry
19783b3acccSAndreas Gohr        $this->updateMetadataRegistry(array_keys($data['metadata']));
1986225b270SMichael Große
1994027a91aSSatoshi Sahara        // update index tag file
20083b3acccSAndreas Gohr        io_saveFile(metaFN($data['page'], '.indexed'), $this->getVersion());
20183b3acccSAndreas Gohr        $this->log("Indexer: finished indexing {$data['page']}");
2026225b270SMichael Große    }
2036225b270SMichael Große
2046225b270SMichael Große    /**
2055f9bd525SSatoshi Sahara     * Remove a page from the index
2066225b270SMichael Große     *
20783b3acccSAndreas Gohr     * Clears the page's data from all collections. The entity persists in page.idx.
2086225b270SMichael Große     *
20983b3acccSAndreas Gohr     * @param string $page The page to remove
21083b3acccSAndreas Gohr     * @param bool $force force deletion even when no .indexed tag exists
2116225b270SMichael Große     *
212a32da6ddSSatoshi Sahara     * @throws IndexAccessException
213a16bd548SSatoshi Sahara     * @throws IndexLockException
214a16bd548SSatoshi Sahara     * @throws IndexWriteException
2156225b270SMichael Große     */
21683b3acccSAndreas Gohr    public function deletePage(string $page, bool $force = false): void
2174027a91aSSatoshi Sahara    {
2184027a91aSSatoshi Sahara        $idxtag = metaFN($page, '.indexed');
2194027a91aSSatoshi Sahara        if (!$force && !file_exists($idxtag)) {
2209369b4a9SAndreas Gohr            $this->log("Indexer: $page.indexed file does not exist, ignoring");
22183b3acccSAndreas Gohr            return;
2224027a91aSSatoshi Sahara        }
2236225b270SMichael Große
22483b3acccSAndreas Gohr        $pageIndex = new FileIndex('page', '', true);
225725e8e5fSSatoshi Sahara
22683b3acccSAndreas Gohr        (new PageTitleCollection($pageIndex))->lock()->addEntity($page, [])->unlock();
22783b3acccSAndreas Gohr        (new PageFulltextCollection($pageIndex))->lock()->addEntity($page, [])->unlock();
22883b3acccSAndreas Gohr
22983b3acccSAndreas Gohr        foreach ($this->getMetadataRegistryKeys() as $key) {
23083b3acccSAndreas Gohr            (new PageMetaCollection($key, $pageIndex))->lock()->addEntity($page, [])->unlock();
2314027a91aSSatoshi Sahara        }
2326225b270SMichael Große
2339369b4a9SAndreas Gohr        $this->log("Indexer: deleted $page from index");
2344027a91aSSatoshi Sahara        @unlink($idxtag);
2354027a91aSSatoshi Sahara    }
2364027a91aSSatoshi Sahara
2374027a91aSSatoshi Sahara    /**
23883b3acccSAndreas Gohr     * Rename a page in the search index
23983b3acccSAndreas Gohr     *
2402ff7e61cSAndreas Gohr     * This renames the page's entity entry in place: its entity ID (the row in the
2412ff7e61cSAndreas Gohr     * page index) is kept and only its name is changed. Because every collection
2422ff7e61cSAndreas Gohr     * (title, fulltext and all metadata keys such as relation_references) is keyed by
2432ff7e61cSAndreas Gohr     * that entity ID, all token, frequency and reverse associations are preserved and
2442ff7e61cSAndreas Gohr     * transparently belong to the new name afterwards.
2452ff7e61cSAndreas Gohr     *
2462ff7e61cSAndreas Gohr     * In particular this keeps the renamed page's *outgoing* references intact. That is
2472ff7e61cSAndreas Gohr     * essential during multi-step operations such as namespace moves: a page renamed
2482ff7e61cSAndreas Gohr     * early on must still be discoverable as a backlink source for pages that are moved
2492ff7e61cSAndreas Gohr     * later. Re-indexing from disk instead would lose this, because the destination page
2502ff7e61cSAndreas Gohr     * has usually not been written to disk yet when this method is called.
2514027a91aSSatoshi Sahara     *
2524027a91aSSatoshi Sahara     * @param string $oldpage The old page name
2534027a91aSSatoshi Sahara     * @param string $newpage The new page name
25483b3acccSAndreas Gohr     *
25583b3acccSAndreas Gohr     * @throws IndexAccessException
256a16bd548SSatoshi Sahara     * @throws IndexLockException
257a16bd548SSatoshi Sahara     * @throws IndexWriteException
2584027a91aSSatoshi Sahara     */
25983b3acccSAndreas Gohr    public function renamePage(string $oldpage, string $newpage): void
2604027a91aSSatoshi Sahara    {
2612ff7e61cSAndreas Gohr        if ($oldpage === $newpage) return;
2622ff7e61cSAndreas Gohr
2632ff7e61cSAndreas Gohr        $pageIndex = new FileIndex('page', '', true);
2642ff7e61cSAndreas Gohr
2652ff7e61cSAndreas Gohr        // locate the existing entity rows; stop as soon as both are known
2662ff7e61cSAndreas Gohr        $oldId = null;
2672ff7e61cSAndreas Gohr        $newId = null;
2682ff7e61cSAndreas Gohr        foreach ($pageIndex as $rid => $value) {
2692ff7e61cSAndreas Gohr            if ($value === $oldpage) $oldId = $rid;
2702ff7e61cSAndreas Gohr            if ($value === $newpage) $newId = $rid;
2712ff7e61cSAndreas Gohr            if ($oldId !== null && $newId !== null) break;
2722ff7e61cSAndreas Gohr        }
2732ff7e61cSAndreas Gohr
2742ff7e61cSAndreas Gohr        // nothing to rename if the old page was never indexed
2752ff7e61cSAndreas Gohr        if ($oldId === null) {
2762ff7e61cSAndreas Gohr            $pageIndex->unlock();
2772ff7e61cSAndreas Gohr            $this->log("Indexer: $oldpage is not in the index, nothing to rename");
2782ff7e61cSAndreas Gohr            return;
2792ff7e61cSAndreas Gohr        }
2802ff7e61cSAndreas Gohr
2812ff7e61cSAndreas Gohr        // If the new name already has its own entity, drop its indexed data first.
2822ff7e61cSAndreas Gohr        // deletePage() intentionally keeps the entity row in page.idx, so we additionally
2832ff7e61cSAndreas Gohr        // blank that row - an empty entry is the index's "removed" marker (see getAllPages()).
2842ff7e61cSAndreas Gohr        // Otherwise two rows would carry the new name and a lookup could resolve to the
2852ff7e61cSAndreas Gohr        // now-empty one instead of the renamed entity that holds the data.
2862ff7e61cSAndreas Gohr        if ($newId !== null) {
2872ff7e61cSAndreas Gohr            $this->deletePage($newpage, true);
2882ff7e61cSAndreas Gohr            $pageIndex->changeRow($newId, '');
2892ff7e61cSAndreas Gohr        }
2902ff7e61cSAndreas Gohr
2912ff7e61cSAndreas Gohr        // rename in place — keeps the entity ID and thus all index associations
2922ff7e61cSAndreas Gohr        $pageIndex->changeRow($oldId, $newpage);
2932ff7e61cSAndreas Gohr
2942ff7e61cSAndreas Gohr        $pageIndex->unlock();
2952ff7e61cSAndreas Gohr        $this->log("Indexer: renamed $oldpage to $newpage in index");
2966225b270SMichael Große    }
2976225b270SMichael Große
2986225b270SMichael Große    /**
29983b3acccSAndreas Gohr     * Clear all page indexes
3006225b270SMichael Große     */
30183b3acccSAndreas Gohr    public function clear(): void
3024027a91aSSatoshi Sahara    {
3036225b270SMichael Große        global $conf;
3046225b270SMichael Große
30583b3acccSAndreas Gohr        Lock::acquire('page');
3064027a91aSSatoshi Sahara
30783b3acccSAndreas Gohr        // clear metadata indexes
30883b3acccSAndreas Gohr        foreach ($this->getMetadataRegistryKeys() as $key) {
30983b3acccSAndreas Gohr            $clean = PageMetaCollection::cleanName($key);
31083b3acccSAndreas Gohr            @unlink($conf['indexdir'] . '/' . $clean . '_w.idx');
31183b3acccSAndreas Gohr            @unlink($conf['indexdir'] . '/' . $clean . '_i.idx');
31283b3acccSAndreas Gohr            @unlink($conf['indexdir'] . '/' . $clean . '_p.idx');
3136225b270SMichael Große        }
3146225b270SMichael Große
31583b3acccSAndreas Gohr        // clear fulltext indexes
31683b3acccSAndreas Gohr        $files = glob($conf['indexdir'] . '/i*.idx');
31783b3acccSAndreas Gohr        if ($files) foreach ($files as $f) @unlink($f);
31883b3acccSAndreas Gohr        $files = glob($conf['indexdir'] . '/w*.idx');
31983b3acccSAndreas Gohr        if ($files) foreach ($files as $f) @unlink($f);
32083b3acccSAndreas Gohr
32183b3acccSAndreas Gohr        @unlink($conf['indexdir'] . '/pageword.idx');
32283b3acccSAndreas Gohr        @unlink($conf['indexdir'] . '/lengths.idx');
32383b3acccSAndreas Gohr
32483b3acccSAndreas Gohr        // clear title and page indexes
32583b3acccSAndreas Gohr        @unlink($conf['indexdir'] . '/title.idx');
32683b3acccSAndreas Gohr        @unlink($conf['indexdir'] . '/page.idx');
32783b3acccSAndreas Gohr        @unlink($conf['indexdir'] . '/metadata.idx');
32883b3acccSAndreas Gohr
32983b3acccSAndreas Gohr        Lock::release('page');
33083b3acccSAndreas Gohr    }
33183b3acccSAndreas Gohr
33283b3acccSAndreas Gohr    /**
33321fbd01bSAndreas Gohr     * Check the structural integrity of all search indexes
33421fbd01bSAndreas Gohr     *
33521fbd01bSAndreas Gohr     * @throws IndexIntegrityException when a structural inconsistency is found
33621fbd01bSAndreas Gohr     */
33721fbd01bSAndreas Gohr    public function checkIntegrity(): void
33821fbd01bSAndreas Gohr    {
33921fbd01bSAndreas Gohr        (new PageFulltextCollection())->checkIntegrity();
34021fbd01bSAndreas Gohr        (new PageTitleCollection())->checkIntegrity();
34121fbd01bSAndreas Gohr
34221fbd01bSAndreas Gohr        foreach ($this->getMetadataRegistryKeys() as $key) {
34321fbd01bSAndreas Gohr            (new PageMetaCollection($key))->checkIntegrity();
34421fbd01bSAndreas Gohr        }
34521fbd01bSAndreas Gohr    }
34621fbd01bSAndreas Gohr
34721fbd01bSAndreas Gohr    /**
34821fbd01bSAndreas Gohr     * Whether the search index is empty (no fulltext data indexed yet)
34921fbd01bSAndreas Gohr     *
35021fbd01bSAndreas Gohr     * @return bool
35121fbd01bSAndreas Gohr     */
35221fbd01bSAndreas Gohr    public function isIndexEmpty(): bool
35321fbd01bSAndreas Gohr    {
35421fbd01bSAndreas Gohr        return (new PageFulltextCollection())->getTokenIndexMaximum() === 0;
35521fbd01bSAndreas Gohr    }
35621fbd01bSAndreas Gohr
35721fbd01bSAndreas Gohr    /**
35883b3acccSAndreas Gohr     * Get the list of known metadata keys from the metadata registry
35983b3acccSAndreas Gohr     *
36083b3acccSAndreas Gohr     * @return string[] list of metadata key names
36183b3acccSAndreas Gohr     */
36283b3acccSAndreas Gohr    protected function getMetadataRegistryKeys(): array
36383b3acccSAndreas Gohr    {
36483b3acccSAndreas Gohr        global $conf;
36583b3acccSAndreas Gohr        $fn = $conf['indexdir'] . '/metadata.idx';
36683b3acccSAndreas Gohr        if (!file_exists($fn)) return [];
36783b3acccSAndreas Gohr        $keys = file($fn, FILE_IGNORE_NEW_LINES);
36883b3acccSAndreas Gohr        return $keys ?: [];
36983b3acccSAndreas Gohr    }
37083b3acccSAndreas Gohr
37183b3acccSAndreas Gohr    /**
37283b3acccSAndreas Gohr     * Update the metadata registry with new keys
37383b3acccSAndreas Gohr     *
37483b3acccSAndreas Gohr     * @param string[] $keys metadata key names to ensure are registered
3756e39b4e3SAndreas Gohr     *
3766e39b4e3SAndreas Gohr     * @internal Only marked public for access via LegacyIndexer
37783b3acccSAndreas Gohr     */
3786e39b4e3SAndreas Gohr    public function updateMetadataRegistry(array $keys): void
37983b3acccSAndreas Gohr    {
38083b3acccSAndreas Gohr        global $conf;
38183b3acccSAndreas Gohr        $fn = $conf['indexdir'] . '/metadata.idx';
38283b3acccSAndreas Gohr        $existing = file_exists($fn) ? file($fn, FILE_IGNORE_NEW_LINES) : [];
38383b3acccSAndreas Gohr        if (!$existing) $existing = [];
38483b3acccSAndreas Gohr
38583b3acccSAndreas Gohr        $added = false;
38683b3acccSAndreas Gohr        foreach ($keys as $key) {
38783b3acccSAndreas Gohr            if (!in_array($key, $existing)) {
38883b3acccSAndreas Gohr                $existing[] = $key;
38983b3acccSAndreas Gohr                $added = true;
39083b3acccSAndreas Gohr            }
39183b3acccSAndreas Gohr        }
39283b3acccSAndreas Gohr
39383b3acccSAndreas Gohr        if ($added) {
39483b3acccSAndreas Gohr            io_saveFile($fn, implode("\n", $existing) . "\n");
39583b3acccSAndreas Gohr        }
39683b3acccSAndreas Gohr    }
397e1272c08SAndreas Gohr
398e1272c08SAndreas Gohr    /**
399e1272c08SAndreas Gohr     * Return a list of all indexed pages, optionally filtered by metadata key
400e1272c08SAndreas Gohr     *
4016e39b4e3SAndreas Gohr     * Kept on Indexer (not just LegacyIndexer) because several plugins call it
4026e39b4e3SAndreas Gohr     * directly on `new Indexer()` instances rather than going through
4036e39b4e3SAndreas Gohr     * idx_get_indexer().
4046e39b4e3SAndreas Gohr     *
405e1272c08SAndreas Gohr     * @param string|null $key metadata key name
406e1272c08SAndreas Gohr     * @return string[]
407e1272c08SAndreas Gohr     *
408e1272c08SAndreas Gohr     * @deprecated 2026-04-07 use MetadataSearch::getPages() or Indexer::getAllPages() instead
409e1272c08SAndreas Gohr     */
410e1272c08SAndreas Gohr    public function getPages($key = null)
411e1272c08SAndreas Gohr    {
412e1272c08SAndreas Gohr        DebugHelper::dbgDeprecatedFunction(MetadataSearch::class . '::getPages()');
413e1272c08SAndreas Gohr        return (new MetadataSearch())->getPages($key);
414e1272c08SAndreas Gohr    }
4156225b270SMichael Große}
416