xref: /dokuwiki/inc/Search/Index/AbstractIndex.php (revision 9f63f003f1342f30f2226786546a29e25d1b79ee)
1<?php
2
3namespace dokuwiki\Search\Index;
4
5/**
6 * Basic Building block to access individual index files
7 */
8abstract class AbstractIndex
9{
10    /** @var string name of the index */
11    protected $idx;
12
13    /** @var string $suffix of the index */
14    protected $suffix;
15
16    /** @var string full filename to the index */
17    protected $filename;
18
19    /**
20     * Initialize the index
21     *
22     * The $suffix argument is for an index that is split into multiple parts.
23     * Different index files should use different base names.
24     *
25     * @param string $idx name of the index
26     * @param string $suffix subpart identifier
27     */
28    public function __construct($idx, $suffix = '')
29    {
30        global $conf;
31        $this->filename = $conf['indexdir'] . '/' . $idx . $suffix . '.idx';
32        $this->idx = $idx;
33        $this->suffix = $suffix;
34    }
35
36    /**
37     * @return string the full path to the underlying file
38     */
39    public function getFilename()
40    {
41        return $this->filename;
42    }
43
44    /**
45     * Change a line in the index
46     *
47     * If the line doesn't exist, it will be added, creating empty
48     * lines inbetween as necessary
49     *
50     * @param int $rid the line number, count starting at 0
51     * @param string $value line content to write
52     */
53    abstract public function changeRow($rid, $value);
54
55    /**
56     * Retrieve a line from the index
57     *
58     * Returns an empty string for non-existing lines
59     *
60     * @param int $rid the line number
61     * @return string a line with trailing whitespace removed
62     */
63    abstract public function retrieveRow($rid);
64
65    /**
66     * Retrieve multiple lines from the index
67     *
68     * Ignores non-existing lines, eg the result array may be smaller than the input $rids
69     *
70     * @param int[] $rids
71     * @return array [rid => value]
72     */
73    abstract public function retrieveRows($rids);
74
75    /**
76     * Searches the Index for a given value and adds it if not found
77     *
78     * Entries previously marked as deleted will be restored.
79     *
80     * Note the existence of an entry in the index does not say anything about the existence
81     * of the real world object (eg. a page)
82     *
83     * You should preferable use accessCachedValue() instead.
84     *
85     * @param string $value
86     * @return int the RID of the entry
87     */
88    public function getRowID($value)
89    {
90        $result = $this->getRowIDs([$value]);
91        return $result[$value];
92    }
93
94    /**
95     * Searches the Index for all given values and adds them if not found
96     *
97     * @param string[] $values
98     * @return array the RIDs of the entries (value => rid)
99     */
100    abstract public function getRowIDs($values);
101
102    /**
103     * Find all RIDs matching a regular expression
104     *
105     * A full regular expression including delimiters and modifiers is expected
106     *
107     * @param string $re the regular expression to match against
108     * @return array (rid => value)
109     */
110    abstract public function search($re);
111
112    /**
113     * Clears the index by deleting its file
114     * @return void
115     */
116    public function clear()
117    {
118        @unlink($this->filename);
119    }
120
121}
122