xref: /dokuwiki/inc/Search/Index/FileIndex.php (revision 06053dca2fac9a1da4eb1accf8c2488942da5d2a)
19bd7d62fSAndreas Gohr<?php
29bd7d62fSAndreas Gohr
39bd7d62fSAndreas Gohrnamespace dokuwiki\Search\Index;
49bd7d62fSAndreas Gohr
59bd7d62fSAndreas Gohruse dokuwiki\Search\Exception\IndexAccessException;
67fcedc39SAndreas Gohruse dokuwiki\Search\Exception\IndexLockException;
79bd7d62fSAndreas Gohruse dokuwiki\Search\Exception\IndexWriteException;
89bd7d62fSAndreas Gohr
99bd7d62fSAndreas Gohr/**
109bd7d62fSAndreas Gohr * Access to a single index file
119bd7d62fSAndreas Gohr *
129bd7d62fSAndreas Gohr * Access using this class always happens on a line-by-line basis. It is usually not read in full.
139bd7d62fSAndreas Gohr * All modifications are implicitly saved
149bd7d62fSAndreas Gohr * Should be used for large indexes that receive only few changes at once.
159bd7d62fSAndreas Gohr */
169bd7d62fSAndreas Gohrclass FileIndex extends AbstractIndex
179bd7d62fSAndreas Gohr{
189bd7d62fSAndreas Gohr    /** @var array RID cache for faster access */
199369b4a9SAndreas Gohr    protected array $ridCache = [];
209bd7d62fSAndreas Gohr
219bd7d62fSAndreas Gohr    /**
229bd7d62fSAndreas Gohr     * @inheritdoc
239bd7d62fSAndreas Gohr     * @throws IndexWriteException
247fcedc39SAndreas Gohr     * @throws IndexLockException
259bd7d62fSAndreas Gohr     * @author Tom N Harris <tnharris@whoopdedo.org>
269bd7d62fSAndreas Gohr     */
279369b4a9SAndreas Gohr    public function changeRow(int $rid, string $value): void
289bd7d62fSAndreas Gohr    {
299bd7d62fSAndreas Gohr        global $conf;
309bd7d62fSAndreas Gohr
317fcedc39SAndreas Gohr        if (!$this->isWritable) throw new IndexLockException();
327fcedc39SAndreas Gohr
339369b4a9SAndreas Gohr        if (!str_ends_with($value, "\n")) {
349bd7d62fSAndreas Gohr            $value .= "\n";
359bd7d62fSAndreas Gohr        }
369bd7d62fSAndreas Gohr
379bd7d62fSAndreas Gohr        $tempname = $this->filename . '.tmp';
389bd7d62fSAndreas Gohr        $fh = @fopen($tempname, 'w');
3903a35633SAndreas Gohr        if (!$fh) {
409369b4a9SAndreas Gohr            throw new IndexWriteException("Failed to write $tempname");
4103a35633SAndreas Gohr        }
429bd7d62fSAndreas Gohr        $ih = @fopen($this->filename, 'r');
439bd7d62fSAndreas Gohr
449bd7d62fSAndreas Gohr        $ln = -1; // line counter
459bd7d62fSAndreas Gohr        // copy previous index lines line-by-line, replacing the wanted line
469bd7d62fSAndreas Gohr        if ($ih) {
479bd7d62fSAndreas Gohr            while (($curline = fgets($ih)) !== false) {
489bd7d62fSAndreas Gohr                fwrite($fh, (++$ln == $rid) ? $value : $curline);
499bd7d62fSAndreas Gohr            }
509bd7d62fSAndreas Gohr            fclose($ih);
519bd7d62fSAndreas Gohr        }
529bd7d62fSAndreas Gohr        // if wanted line is beyond the current line count, insert empty lines inbetween
539bd7d62fSAndreas Gohr        if ($rid > $ln) {
549bd7d62fSAndreas Gohr            while ($rid > ++$ln) {
559bd7d62fSAndreas Gohr                fwrite($fh, "\n");
569bd7d62fSAndreas Gohr            }
579bd7d62fSAndreas Gohr            fwrite($fh, $value);
589bd7d62fSAndreas Gohr        }
599bd7d62fSAndreas Gohr        fclose($fh);
609bd7d62fSAndreas Gohr
619bd7d62fSAndreas Gohr        if ($conf['fperm']) {
629bd7d62fSAndreas Gohr            chmod($tempname, $conf['fperm']);
639bd7d62fSAndreas Gohr        }
649bd7d62fSAndreas Gohr        io_rename($tempname, $this->filename);
659bd7d62fSAndreas Gohr    }
669bd7d62fSAndreas Gohr
67*06053dcaSAndreas Gohr    /** @inheritdoc */
689369b4a9SAndreas Gohr    public function retrieveRow(int $rid): string
699bd7d62fSAndreas Gohr    {
7003a35633SAndreas Gohr        if (!file_exists($this->filename)) {
7103a35633SAndreas Gohr            return '';
7203a35633SAndreas Gohr        }
739bd7d62fSAndreas Gohr        $fh = @fopen($this->filename, 'r');
7403a35633SAndreas Gohr        if (!$fh) {
7503a35633SAndreas Gohr            return '';
7603a35633SAndreas Gohr        }
779bd7d62fSAndreas Gohr        $ln = -1;
789bd7d62fSAndreas Gohr        while (($line = fgets($fh)) !== false) {
79dec26820SAndreas Gohr            if (++$ln == $rid) {
809bd7d62fSAndreas Gohr                fclose($fh);
819369b4a9SAndreas Gohr                return rtrim($line);
829bd7d62fSAndreas Gohr            }
83dec26820SAndreas Gohr        }
84dec26820SAndreas Gohr        fclose($fh);
85dec26820SAndreas Gohr
86dec26820SAndreas Gohr        return '';
87dec26820SAndreas Gohr    }
889f63f003SAndreas Gohr
899f63f003SAndreas Gohr    /** @inheritdoc */
909369b4a9SAndreas Gohr    public function retrieveRows(array $rids): array
919f63f003SAndreas Gohr    {
929f63f003SAndreas Gohr        $result = [];
939f63f003SAndreas Gohr        sort($rids);
949f63f003SAndreas Gohr        $next = array_shift($rids);
959f63f003SAndreas Gohr
969f63f003SAndreas Gohr        if (!file_exists($this->filename)) {
979f63f003SAndreas Gohr            return $result;
989f63f003SAndreas Gohr        }
999f63f003SAndreas Gohr        $fh = @fopen($this->filename, 'r');
1009f63f003SAndreas Gohr        if (!$fh) {
1019f63f003SAndreas Gohr            return $result;
1029f63f003SAndreas Gohr        }
1039f63f003SAndreas Gohr        $ln = -1;
1049f63f003SAndreas Gohr        while (($line = fgets($fh)) !== false) {
1059f63f003SAndreas Gohr            if (++$ln === $next) {
1069369b4a9SAndreas Gohr                $result[$ln] = rtrim($line);
1079f63f003SAndreas Gohr                $next = array_shift($rids);
1089f63f003SAndreas Gohr                if ($next === false) break;
1099f63f003SAndreas Gohr            }
1109f63f003SAndreas Gohr        }
1119f63f003SAndreas Gohr        fclose($fh);
1129f63f003SAndreas Gohr        return $result;
1139f63f003SAndreas Gohr    }
1149f63f003SAndreas Gohr
1159bd7d62fSAndreas Gohr
1169bd7d62fSAndreas Gohr    /**
117d6396b6dSAndreas Gohr     * @inheritdoc
1189bd7d62fSAndreas Gohr     * @throws IndexAccessException
1197fcedc39SAndreas Gohr     * @throws IndexWriteException
1209bd7d62fSAndreas Gohr     */
1219369b4a9SAndreas Gohr    public function getRowIDs(array $values): array
1229bd7d62fSAndreas Gohr    {
1239369b4a9SAndreas Gohr        $values = array_map(trim(...), $values);
1249bd7d62fSAndreas Gohr        $values = array_fill_keys($values, 1); // easier access as associative array
1259bd7d62fSAndreas Gohr
1269bd7d62fSAndreas Gohr        // search for the values
1279bd7d62fSAndreas Gohr        $result = [];
1289bd7d62fSAndreas Gohr        $ln = 0;
1299bd7d62fSAndreas Gohr        if (file_exists($this->filename)) {
1309bd7d62fSAndreas Gohr            $fh = @fopen($this->filename, 'r');
13103a35633SAndreas Gohr            if (!$fh) {
1329369b4a9SAndreas Gohr                throw new IndexAccessException("Failed to read $this->filename");
13303a35633SAndreas Gohr            }
1349bd7d62fSAndreas Gohr            while (($line = fgets($fh)) !== false && $values) {
1359bd7d62fSAndreas Gohr                $line = trim($line);
1369bd7d62fSAndreas Gohr                if (isset($values[$line])) {
1379bd7d62fSAndreas Gohr                    $result[$line] = $ln;
1389bd7d62fSAndreas Gohr                    unset($values[$line]);
1399bd7d62fSAndreas Gohr                }
1409bd7d62fSAndreas Gohr                $ln++;
1419bd7d62fSAndreas Gohr            }
1429bd7d62fSAndreas Gohr            fclose($fh);
1439bd7d62fSAndreas Gohr        }
1449bd7d62fSAndreas Gohr
1457fcedc39SAndreas Gohr        if (!$this->isWritable) return $result;
1467fcedc39SAndreas Gohr
1479bd7d62fSAndreas Gohr        // if there are still values, they have not been found and will be appended
1489bd7d62fSAndreas Gohr        foreach (array_keys($values) as $value) {
1497fcedc39SAndreas Gohr            if (!file_put_contents($this->filename, "$value\n", FILE_APPEND)) {
1509369b4a9SAndreas Gohr                throw new IndexWriteException("Failed to write $this->filename");
1517fcedc39SAndreas Gohr            }
1529bd7d62fSAndreas Gohr            $result[$value] = $ln++;
1539bd7d62fSAndreas Gohr        }
1549bd7d62fSAndreas Gohr
1559bd7d62fSAndreas Gohr        return $result;
1569bd7d62fSAndreas Gohr    }
1579bd7d62fSAndreas Gohr
15803a35633SAndreas Gohr    /** @inheritdoc */
1599369b4a9SAndreas Gohr    public function search(string $re): array
16003a35633SAndreas Gohr    {
16103a35633SAndreas Gohr        $result = [];
16203a35633SAndreas Gohr        $ln = 0;
16303a35633SAndreas Gohr        if (file_exists($this->filename)) {
16403a35633SAndreas Gohr            $fh = @fopen($this->filename, 'r');
16503a35633SAndreas Gohr            if (!$fh) {
1669369b4a9SAndreas Gohr                throw new IndexAccessException("Failed to read $this->filename");
16703a35633SAndreas Gohr            }
16803a35633SAndreas Gohr            while (($line = fgets($fh)) !== false) {
16903a35633SAndreas Gohr                $line = trim($line);
17003a35633SAndreas Gohr                if (preg_match($re, $line)) {
17103a35633SAndreas Gohr                    $result[$ln] = $line;
17203a35633SAndreas Gohr                }
17303a35633SAndreas Gohr                $ln++;
17403a35633SAndreas Gohr            }
17503a35633SAndreas Gohr            fclose($fh);
17603a35633SAndreas Gohr        }
17703a35633SAndreas Gohr        return $result;
17803a35633SAndreas Gohr    }
17903a35633SAndreas Gohr
1809bd7d62fSAndreas Gohr    /**
181596d5287SAndreas Gohr     * Cached mechanism to retrieve a single value
1829bd7d62fSAndreas Gohr     *
1839bd7d62fSAndreas Gohr     * @param string $value
1849bd7d62fSAndreas Gohr     * @return int the RID of the entry
185596d5287SAndreas Gohr     * @see getRowID()
1869bd7d62fSAndreas Gohr     */
1879369b4a9SAndreas Gohr    public function accessCachedValue(string $value): int
1889bd7d62fSAndreas Gohr    {
189fb5311ecSAndreas Gohr        if (isset($this->ridCache[$value])) {
190fb5311ecSAndreas Gohr            return $this->ridCache[$value];
19103a35633SAndreas Gohr        }
1929bd7d62fSAndreas Gohr
1939bd7d62fSAndreas Gohr        // limit cache to 10 entries by discarding the oldest element
1949bd7d62fSAndreas Gohr        // as in DokuWiki usually only the most recently
1959bd7d62fSAndreas Gohr        // added item will be requested again
196fb5311ecSAndreas Gohr        if (count($this->ridCache) > 10) {
197fb5311ecSAndreas Gohr            array_shift($this->ridCache);
19803a35633SAndreas Gohr        }
199fb5311ecSAndreas Gohr        $this->ridCache[$value] = $this->getRowID($value);
200fb5311ecSAndreas Gohr        return $this->ridCache[$value];
2019bd7d62fSAndreas Gohr    }
20283b3acccSAndreas Gohr
20383b3acccSAndreas Gohr    /** @inheritdoc */
20421fbd01bSAndreas Gohr    public function count(): int
20521fbd01bSAndreas Gohr    {
20621fbd01bSAndreas Gohr        if (!file_exists($this->filename)) return 0;
20721fbd01bSAndreas Gohr        $fh = @fopen($this->filename, 'r');
20821fbd01bSAndreas Gohr        if (!$fh) return 0;
20921fbd01bSAndreas Gohr        $count = 0;
21021fbd01bSAndreas Gohr        while (fgets($fh) !== false) $count++;
21121fbd01bSAndreas Gohr        fclose($fh);
21221fbd01bSAndreas Gohr        return $count;
21321fbd01bSAndreas Gohr    }
21421fbd01bSAndreas Gohr
21521fbd01bSAndreas Gohr    /** @inheritdoc */
21683b3acccSAndreas Gohr    public function getIterator(): \Generator
21783b3acccSAndreas Gohr    {
21883b3acccSAndreas Gohr        if (!file_exists($this->filename)) return;
21983b3acccSAndreas Gohr        $fh = @fopen($this->filename, 'r');
22083b3acccSAndreas Gohr        if (!$fh) return;
22183b3acccSAndreas Gohr        $ln = 0;
22283b3acccSAndreas Gohr        while (($line = fgets($fh)) !== false) {
22383b3acccSAndreas Gohr            yield $ln++ => rtrim($line);
22483b3acccSAndreas Gohr        }
22583b3acccSAndreas Gohr        fclose($fh);
22683b3acccSAndreas Gohr    }
2279bd7d62fSAndreas Gohr}
228