xref: /dokuwiki/inc/Search/Index/FileIndex.php (revision 21fbd01b3c3eea88b767376b7b158f31f0f63127)
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 */
19fb5311ecSAndreas Gohr    protected $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     */
279bd7d62fSAndreas Gohr    public function changeRow($rid, $value)
289bd7d62fSAndreas Gohr    {
299bd7d62fSAndreas Gohr        global $conf;
309bd7d62fSAndreas Gohr
317fcedc39SAndreas Gohr        if (!$this->isWritable) throw new IndexLockException();
327fcedc39SAndreas Gohr
339bd7d62fSAndreas Gohr        if (substr($value, -1) !== "\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) {
4003a35633SAndreas 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
679bd7d62fSAndreas Gohr    /**
689bd7d62fSAndreas Gohr     * @inheritdoc
697fcedc39SAndreas Gohr     * @throws IndexWriteException
709bd7d62fSAndreas Gohr     * @author Tom N Harris <tnharris@whoopdedo.org>
719bd7d62fSAndreas Gohr     */
729bd7d62fSAndreas Gohr    public function retrieveRow($rid)
739bd7d62fSAndreas Gohr    {
7403a35633SAndreas Gohr        if (!file_exists($this->filename)) {
7503a35633SAndreas Gohr            return '';
7603a35633SAndreas Gohr        }
779bd7d62fSAndreas Gohr        $fh = @fopen($this->filename, 'r');
7803a35633SAndreas Gohr        if (!$fh) {
7903a35633SAndreas Gohr            return '';
8003a35633SAndreas Gohr        }
819bd7d62fSAndreas Gohr        $ln = -1;
829bd7d62fSAndreas Gohr        while (($line = fgets($fh)) !== false) {
83dec26820SAndreas Gohr            if (++$ln == $rid) {
849bd7d62fSAndreas Gohr                fclose($fh);
859bd7d62fSAndreas Gohr                return rtrim((string)$line);
869bd7d62fSAndreas Gohr            }
87dec26820SAndreas Gohr        }
88dec26820SAndreas Gohr        fclose($fh);
89dec26820SAndreas Gohr
907fcedc39SAndreas Gohr        if (!$this->isWritable) return '';
917fcedc39SAndreas Gohr
92dec26820SAndreas Gohr        // still here? pad the index for the given ID
93dec26820SAndreas Gohr        // we do not simply call changeRow() here because appending is faster than line-by-line copying
94dec26820SAndreas Gohr        if (!file_put_contents($this->filename, join("\n", array_fill(0, $rid - $ln + 1, '')), FILE_APPEND)) {
95dec26820SAndreas Gohr            throw new IndexWriteException("Failed to write {$this->filename}");
96dec26820SAndreas Gohr        }
97dec26820SAndreas Gohr
98dec26820SAndreas Gohr        return '';
99dec26820SAndreas Gohr    }
1009f63f003SAndreas Gohr
1019f63f003SAndreas Gohr    /** @inheritdoc */
1029f63f003SAndreas Gohr    public function retrieveRows($rids)
1039f63f003SAndreas Gohr    {
1049f63f003SAndreas Gohr        $result = [];
1059f63f003SAndreas Gohr        sort($rids);
1069f63f003SAndreas Gohr        $next = array_shift($rids);
1079f63f003SAndreas Gohr
1089f63f003SAndreas Gohr        if (!file_exists($this->filename)) {
1099f63f003SAndreas Gohr            return $result;
1109f63f003SAndreas Gohr        }
1119f63f003SAndreas Gohr        $fh = @fopen($this->filename, 'r');
1129f63f003SAndreas Gohr        if (!$fh) {
1139f63f003SAndreas Gohr            return $result;
1149f63f003SAndreas Gohr        }
1159f63f003SAndreas Gohr        $ln = -1;
1169f63f003SAndreas Gohr        while (($line = fgets($fh)) !== false) {
1179f63f003SAndreas Gohr            if (++$ln === $next) {
1189f63f003SAndreas Gohr                $result[$ln] = rtrim((string)$line);
1199f63f003SAndreas Gohr                $next = array_shift($rids);
1209f63f003SAndreas Gohr                if ($next === false) break;
1219f63f003SAndreas Gohr            }
1229f63f003SAndreas Gohr        }
1239f63f003SAndreas Gohr        fclose($fh);
1249f63f003SAndreas Gohr        return $result;
1259f63f003SAndreas Gohr    }
1269f63f003SAndreas Gohr
1279bd7d62fSAndreas Gohr
1289bd7d62fSAndreas Gohr    /**
129d6396b6dSAndreas Gohr     * @inheritdoc
1309bd7d62fSAndreas Gohr     * @throws IndexAccessException
1317fcedc39SAndreas Gohr     * @throws IndexWriteException
1329bd7d62fSAndreas Gohr     */
1338ed35011SAndreas Gohr    public function getRowIDs($values)
1349bd7d62fSAndreas Gohr    {
1359bd7d62fSAndreas Gohr        $values = array_map('trim', $values);
1369bd7d62fSAndreas Gohr        $values = array_fill_keys($values, 1); // easier access as associative array
1379bd7d62fSAndreas Gohr
1389bd7d62fSAndreas Gohr        // search for the values
1399bd7d62fSAndreas Gohr        $result = [];
1409bd7d62fSAndreas Gohr        $ln = 0;
1419bd7d62fSAndreas Gohr        if (file_exists($this->filename)) {
1429bd7d62fSAndreas Gohr            $fh = @fopen($this->filename, 'r');
14303a35633SAndreas Gohr            if (!$fh) {
14403a35633SAndreas Gohr                throw new IndexAccessException("Failed to read {$this->filename}");
14503a35633SAndreas Gohr            }
1469bd7d62fSAndreas Gohr            while (($line = fgets($fh)) !== false && $values) {
1479bd7d62fSAndreas Gohr                $line = trim($line);
1489bd7d62fSAndreas Gohr                if (isset($values[$line])) {
1499bd7d62fSAndreas Gohr                    $result[$line] = $ln;
1509bd7d62fSAndreas Gohr                    unset($values[$line]);
1519bd7d62fSAndreas Gohr                }
1529bd7d62fSAndreas Gohr                $ln++;
1539bd7d62fSAndreas Gohr            }
1549bd7d62fSAndreas Gohr            fclose($fh);
1559bd7d62fSAndreas Gohr        }
1569bd7d62fSAndreas Gohr
1577fcedc39SAndreas Gohr        if (!$this->isWritable) return $result;
1587fcedc39SAndreas Gohr
1599bd7d62fSAndreas Gohr        // if there are still values, they have not been found and will be appended
1609bd7d62fSAndreas Gohr        foreach (array_keys($values) as $value) {
1617fcedc39SAndreas Gohr            if (!file_put_contents($this->filename, "$value\n", FILE_APPEND)) {
1627fcedc39SAndreas Gohr                throw new IndexWriteException("Failed to write {$this->filename}");
1637fcedc39SAndreas Gohr            }
1649bd7d62fSAndreas Gohr            $result[$value] = $ln++;
1659bd7d62fSAndreas Gohr        }
1669bd7d62fSAndreas Gohr
1679bd7d62fSAndreas Gohr        return $result;
1689bd7d62fSAndreas Gohr    }
1699bd7d62fSAndreas Gohr
17003a35633SAndreas Gohr    /** @inheritdoc */
17103a35633SAndreas Gohr    public function search($re)
17203a35633SAndreas Gohr    {
17303a35633SAndreas Gohr        $result = [];
17403a35633SAndreas Gohr        $ln = 0;
17503a35633SAndreas Gohr        if (file_exists($this->filename)) {
17603a35633SAndreas Gohr            $fh = @fopen($this->filename, 'r');
17703a35633SAndreas Gohr            if (!$fh) {
17803a35633SAndreas Gohr                throw new IndexAccessException("Failed to read {$this->filename}");
17903a35633SAndreas Gohr            }
18003a35633SAndreas Gohr            while (($line = fgets($fh)) !== false) {
18103a35633SAndreas Gohr                $line = trim($line);
18203a35633SAndreas Gohr                if (preg_match($re, $line)) {
18303a35633SAndreas Gohr                    $result[$ln] = $line;
18403a35633SAndreas Gohr                }
18503a35633SAndreas Gohr                $ln++;
18603a35633SAndreas Gohr            }
18703a35633SAndreas Gohr            fclose($fh);
18803a35633SAndreas Gohr        }
18903a35633SAndreas Gohr        return $result;
19003a35633SAndreas Gohr    }
19103a35633SAndreas Gohr
1929bd7d62fSAndreas Gohr    /**
193596d5287SAndreas Gohr     * Cached mechanism to retrieve a single value
1949bd7d62fSAndreas Gohr     *
1959bd7d62fSAndreas Gohr     * @param string $value
1969bd7d62fSAndreas Gohr     * @return int the RID of the entry
1979bd7d62fSAndreas Gohr     * @throws IndexAccessException
1989bd7d62fSAndreas Gohr     * @throws IndexWriteException
199596d5287SAndreas Gohr     * @see getRowID()
2009bd7d62fSAndreas Gohr     */
2019bd7d62fSAndreas Gohr    public function accessCachedValue($value)
2029bd7d62fSAndreas Gohr    {
203fb5311ecSAndreas Gohr        if (isset($this->ridCache[$value])) {
204fb5311ecSAndreas Gohr            return $this->ridCache[$value];
20503a35633SAndreas Gohr        }
2069bd7d62fSAndreas Gohr
2079bd7d62fSAndreas Gohr        // limit cache to 10 entries by discarding the oldest element
2089bd7d62fSAndreas Gohr        // as in DokuWiki usually only the most recently
2099bd7d62fSAndreas Gohr        // added item will be requested again
210fb5311ecSAndreas Gohr        if (count($this->ridCache) > 10) {
211fb5311ecSAndreas Gohr            array_shift($this->ridCache);
21203a35633SAndreas Gohr        }
213fb5311ecSAndreas Gohr        $this->ridCache[$value] = $this->getRowID($value);
214fb5311ecSAndreas Gohr        return $this->ridCache[$value];
2159bd7d62fSAndreas Gohr    }
21683b3acccSAndreas Gohr
21783b3acccSAndreas Gohr    /** @inheritdoc */
218*21fbd01bSAndreas Gohr    public function count(): int
219*21fbd01bSAndreas Gohr    {
220*21fbd01bSAndreas Gohr        if (!file_exists($this->filename)) return 0;
221*21fbd01bSAndreas Gohr        $fh = @fopen($this->filename, 'r');
222*21fbd01bSAndreas Gohr        if (!$fh) return 0;
223*21fbd01bSAndreas Gohr        $count = 0;
224*21fbd01bSAndreas Gohr        while (fgets($fh) !== false) $count++;
225*21fbd01bSAndreas Gohr        fclose($fh);
226*21fbd01bSAndreas Gohr        return $count;
227*21fbd01bSAndreas Gohr    }
228*21fbd01bSAndreas Gohr
229*21fbd01bSAndreas Gohr    /** @inheritdoc */
23083b3acccSAndreas Gohr    public function getIterator(): \Generator
23183b3acccSAndreas Gohr    {
23283b3acccSAndreas Gohr        if (!file_exists($this->filename)) return;
23383b3acccSAndreas Gohr        $fh = @fopen($this->filename, 'r');
23483b3acccSAndreas Gohr        if (!$fh) return;
23583b3acccSAndreas Gohr        $ln = 0;
23683b3acccSAndreas Gohr        while (($line = fgets($fh)) !== false) {
23783b3acccSAndreas Gohr            yield $ln++ => rtrim($line);
23883b3acccSAndreas Gohr        }
23983b3acccSAndreas Gohr        fclose($fh);
24083b3acccSAndreas Gohr    }
2419bd7d62fSAndreas Gohr}
242