xref: /dokuwiki/inc/Search/Index/FileIndex.php (revision d6396b6d927e3ff373c0aed3927ae61eaca8d537)
19bd7d62fSAndreas Gohr<?php
29bd7d62fSAndreas Gohr
39bd7d62fSAndreas Gohrnamespace dokuwiki\Search\Index;
49bd7d62fSAndreas Gohr
59bd7d62fSAndreas Gohruse dokuwiki\Search\Exception\IndexAccessException;
69bd7d62fSAndreas Gohruse dokuwiki\Search\Exception\IndexWriteException;
79bd7d62fSAndreas Gohr
89bd7d62fSAndreas Gohr/**
99bd7d62fSAndreas Gohr * Access to a single index file
109bd7d62fSAndreas Gohr *
119bd7d62fSAndreas Gohr * Access using this class always happens on a line-by-line basis. It is usually not read in full.
129bd7d62fSAndreas Gohr * All modifications are implicitly saved
139bd7d62fSAndreas Gohr * Should be used for large indexes that receive only few changes at once.
149bd7d62fSAndreas Gohr */
159bd7d62fSAndreas Gohrclass FileIndex extends AbstractIndex
169bd7d62fSAndreas Gohr{
179bd7d62fSAndreas Gohr    /** @var array RID cache for faster access */
189bd7d62fSAndreas Gohr    protected static $ridCache = [];
199bd7d62fSAndreas Gohr
209bd7d62fSAndreas Gohr    /**
219bd7d62fSAndreas Gohr     * @inheritdoc
229bd7d62fSAndreas Gohr     * @throws IndexWriteException
239bd7d62fSAndreas Gohr     * @author Tom N Harris <tnharris@whoopdedo.org>
249bd7d62fSAndreas Gohr     */
259bd7d62fSAndreas Gohr    public function changeRow($rid, $value)
269bd7d62fSAndreas Gohr    {
279bd7d62fSAndreas Gohr        global $conf;
289bd7d62fSAndreas Gohr
299bd7d62fSAndreas Gohr        if (substr($value, -1) !== "\n") {
309bd7d62fSAndreas Gohr            $value .= "\n";
319bd7d62fSAndreas Gohr        }
329bd7d62fSAndreas Gohr
339bd7d62fSAndreas Gohr        $tempname = $this->filename . '.tmp';
349bd7d62fSAndreas Gohr        $fh = @fopen($tempname, 'w');
359bd7d62fSAndreas Gohr        if (!$fh) throw new IndexWriteException("Failed to write {$tempname}");
369bd7d62fSAndreas Gohr        $ih = @fopen($this->filename, 'r');
379bd7d62fSAndreas Gohr
389bd7d62fSAndreas Gohr        $ln = -1; // line counter
399bd7d62fSAndreas Gohr        // copy previous index lines line-by-line, replacing the wanted line
409bd7d62fSAndreas Gohr        if ($ih) {
419bd7d62fSAndreas Gohr            while (($curline = fgets($ih)) !== false) {
429bd7d62fSAndreas Gohr                fwrite($fh, (++$ln == $rid) ? $value : $curline);
439bd7d62fSAndreas Gohr            }
449bd7d62fSAndreas Gohr            fclose($ih);
459bd7d62fSAndreas Gohr        }
469bd7d62fSAndreas Gohr        // if wanted line is beyond the current line count, insert empty lines inbetween
479bd7d62fSAndreas Gohr        if ($rid > $ln) {
489bd7d62fSAndreas Gohr            while ($rid > ++$ln) {
499bd7d62fSAndreas Gohr                fwrite($fh, "\n");
509bd7d62fSAndreas Gohr            }
519bd7d62fSAndreas Gohr            fwrite($fh, $value);
529bd7d62fSAndreas Gohr        }
539bd7d62fSAndreas Gohr        fclose($fh);
549bd7d62fSAndreas Gohr
559bd7d62fSAndreas Gohr        if ($conf['fperm']) {
569bd7d62fSAndreas Gohr            chmod($tempname, $conf['fperm']);
579bd7d62fSAndreas Gohr        }
589bd7d62fSAndreas Gohr        io_rename($tempname, $this->filename);
599bd7d62fSAndreas Gohr    }
609bd7d62fSAndreas Gohr
619bd7d62fSAndreas Gohr    /**
629bd7d62fSAndreas Gohr     * @inheritdoc
639bd7d62fSAndreas Gohr     * @author Tom N Harris <tnharris@whoopdedo.org>
649bd7d62fSAndreas Gohr     */
659bd7d62fSAndreas Gohr    public function retrieveRow($rid)
669bd7d62fSAndreas Gohr    {
679bd7d62fSAndreas Gohr        if (!file_exists($this->filename)) return '';
689bd7d62fSAndreas Gohr        $fh = @fopen($this->filename, 'r');
699bd7d62fSAndreas Gohr        if (!$fh) return '';
709bd7d62fSAndreas Gohr        $ln = -1;
719bd7d62fSAndreas Gohr        while (($line = fgets($fh)) !== false) {
729bd7d62fSAndreas Gohr            if (++$ln == $rid) break;
739bd7d62fSAndreas Gohr        }
749bd7d62fSAndreas Gohr        fclose($fh);
759bd7d62fSAndreas Gohr        return rtrim((string)$line);
769bd7d62fSAndreas Gohr    }
779bd7d62fSAndreas Gohr
789bd7d62fSAndreas Gohr    /**
79*d6396b6dSAndreas Gohr     * @inheritdoc
809bd7d62fSAndreas Gohr     * @throws IndexAccessException
819bd7d62fSAndreas Gohr     */
829bd7d62fSAndreas Gohr    public function accessValues($values)
839bd7d62fSAndreas Gohr    {
849bd7d62fSAndreas Gohr        $values = array_map('trim', $values);
859bd7d62fSAndreas Gohr        $values = array_fill_keys($values, 1); // easier access as associative array
869bd7d62fSAndreas Gohr
879bd7d62fSAndreas Gohr        // search for the values
889bd7d62fSAndreas Gohr        $result = [];
899bd7d62fSAndreas Gohr        $ln = 0;
909bd7d62fSAndreas Gohr        if (file_exists($this->filename)) {
919bd7d62fSAndreas Gohr            $fh = @fopen($this->filename, 'r');
929bd7d62fSAndreas Gohr            if (!$fh) throw new IndexAccessException("Failed to read {$this->filename}");
939bd7d62fSAndreas Gohr            while (($line = fgets($fh)) !== false && $values) {
949bd7d62fSAndreas Gohr                $line = trim($line);
959bd7d62fSAndreas Gohr                if (isset($values[$line])) {
969bd7d62fSAndreas Gohr                    $result[$line] = $ln;
979bd7d62fSAndreas Gohr                    unset($values[$line]);
989bd7d62fSAndreas Gohr                }
999bd7d62fSAndreas Gohr                $ln++;
1009bd7d62fSAndreas Gohr            }
1019bd7d62fSAndreas Gohr            fclose($fh);
1029bd7d62fSAndreas Gohr        }
1039bd7d62fSAndreas Gohr
1049bd7d62fSAndreas Gohr        // if there are still values, they have not been found and will be appended
1059bd7d62fSAndreas Gohr        foreach (array_keys($values) as $value) {
1069bd7d62fSAndreas Gohr            file_put_contents($this->filename, "$value\n", FILE_APPEND);
1079bd7d62fSAndreas Gohr            $result[$value] = $ln++;
1089bd7d62fSAndreas Gohr        }
1099bd7d62fSAndreas Gohr
1109bd7d62fSAndreas Gohr        return $result;
1119bd7d62fSAndreas Gohr    }
1129bd7d62fSAndreas Gohr
1139bd7d62fSAndreas Gohr    /**
1149bd7d62fSAndreas Gohr     * Cached version of accessCachedValue()
1159bd7d62fSAndreas Gohr     *
1169bd7d62fSAndreas Gohr     * @param string $value
1179bd7d62fSAndreas Gohr     * @return int the RID of the entry
1189bd7d62fSAndreas Gohr     * @throws IndexAccessException
1199bd7d62fSAndreas Gohr     * @throws IndexWriteException
1209bd7d62fSAndreas Gohr     */
1219bd7d62fSAndreas Gohr    public function accessCachedValue($value)
1229bd7d62fSAndreas Gohr    {
1239bd7d62fSAndreas Gohr        if (isset(static::$ridCache['value'])) return static::$ridCache['value'];
1249bd7d62fSAndreas Gohr
1259bd7d62fSAndreas Gohr        // limit cache to 10 entries by discarding the oldest element
1269bd7d62fSAndreas Gohr        // as in DokuWiki usually only the most recently
1279bd7d62fSAndreas Gohr        // added item will be requested again
1289bd7d62fSAndreas Gohr        if (count(static::$ridCache) > 10) array_shift(static::$ridCache);
1299bd7d62fSAndreas Gohr        static::$ridCache[$value] = $this->accessValue($value);
1309bd7d62fSAndreas Gohr        return static::$ridCache[$value];
1319bd7d62fSAndreas Gohr    }
1329bd7d62fSAndreas Gohr}
133