xref: /dokuwiki/inc/Search/Index/FileIndex.php (revision 7fcedc39d164da3013a88e9ad6660009940e851b)
19bd7d62fSAndreas Gohr<?php
29bd7d62fSAndreas Gohr
39bd7d62fSAndreas Gohrnamespace dokuwiki\Search\Index;
49bd7d62fSAndreas Gohr
59bd7d62fSAndreas Gohruse dokuwiki\Search\Exception\IndexAccessException;
6*7fcedc39SAndreas 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 */
199bd7d62fSAndreas Gohr    protected static $ridCache = [];
209bd7d62fSAndreas Gohr
219bd7d62fSAndreas Gohr    /**
229bd7d62fSAndreas Gohr     * @inheritdoc
239bd7d62fSAndreas Gohr     * @throws IndexWriteException
24*7fcedc39SAndreas 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
31*7fcedc39SAndreas Gohr        if (!$this->isWritable) throw new IndexLockException();
32*7fcedc39SAndreas 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
69*7fcedc39SAndreas 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
90*7fcedc39SAndreas Gohr        if(!$this->isWritable) return '';
91*7fcedc39SAndreas 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
131*7fcedc39SAndreas 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
157*7fcedc39SAndreas Gohr        if(!$this->isWritable) return $result;
158*7fcedc39SAndreas 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) {
161*7fcedc39SAndreas Gohr            if (!file_put_contents($this->filename, "$value\n", FILE_APPEND)) {
162*7fcedc39SAndreas Gohr                throw new IndexWriteException("Failed to write {$this->filename}");
163*7fcedc39SAndreas 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    /**
1939bd7d62fSAndreas Gohr     * Cached version of accessCachedValue()
1949bd7d62fSAndreas Gohr     *
1959bd7d62fSAndreas Gohr     * @param string $value
1969bd7d62fSAndreas Gohr     * @return int the RID of the entry
1979bd7d62fSAndreas Gohr     * @throws IndexAccessException
1989bd7d62fSAndreas Gohr     * @throws IndexWriteException
1999bd7d62fSAndreas Gohr     */
2009bd7d62fSAndreas Gohr    public function accessCachedValue($value)
2019bd7d62fSAndreas Gohr    {
20203a35633SAndreas Gohr        if (isset(static::$ridCache['value'])) {
20303a35633SAndreas Gohr            return static::$ridCache['value'];
20403a35633SAndreas Gohr        }
2059bd7d62fSAndreas Gohr
2069bd7d62fSAndreas Gohr        // limit cache to 10 entries by discarding the oldest element
2079bd7d62fSAndreas Gohr        // as in DokuWiki usually only the most recently
2089bd7d62fSAndreas Gohr        // added item will be requested again
20903a35633SAndreas Gohr        if (count(static::$ridCache) > 10) {
21003a35633SAndreas Gohr            array_shift(static::$ridCache);
21103a35633SAndreas Gohr        }
2128ed35011SAndreas Gohr        static::$ridCache[$value] = $this->getRowID($value);
2139bd7d62fSAndreas Gohr        return static::$ridCache[$value];
2149bd7d62fSAndreas Gohr    }
2159bd7d62fSAndreas Gohr}
216