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