xref: /dokuwiki/inc/Search/Index/AbstractIndex.php (revision 03a35633c932eca1b7b0d373d08b9140884a0ebe)
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    /**
66d6396b6dSAndreas Gohr     * Searches the Index for a given value and adds it if not found
67d6396b6dSAndreas Gohr     *
68d6396b6dSAndreas Gohr     * Entries previously marked as deleted will be restored.
69d6396b6dSAndreas Gohr     *
70d6396b6dSAndreas Gohr     * Note the existance of an entry in the index does not say anything about the exististance
71d6396b6dSAndreas Gohr     * of the real world object (eg. a page)
72d6396b6dSAndreas Gohr     *
73d6396b6dSAndreas Gohr     * You should preferable use accessCachedValue() instead.
74d6396b6dSAndreas Gohr     *
75d6396b6dSAndreas Gohr     * @param string $value
76d6396b6dSAndreas Gohr     * @return int the RID of the entry
77d6396b6dSAndreas Gohr     */
788ed35011SAndreas Gohr    public function getRowID($value)
79d6396b6dSAndreas Gohr    {
808ed35011SAndreas Gohr        $result = $this->getRowIDs([$value]);
81d6396b6dSAndreas Gohr        return $result[$value];
82d6396b6dSAndreas Gohr    }
83d6396b6dSAndreas Gohr
84d6396b6dSAndreas Gohr    /**
85d6396b6dSAndreas Gohr     * Searches the Index for all given values and adds them if not found
86d6396b6dSAndreas Gohr     *
87d6396b6dSAndreas Gohr     * @param string[] $values
88*03a35633SAndreas Gohr     * @return array the RIDs of the entries (value => rid)
89d6396b6dSAndreas Gohr     */
908ed35011SAndreas Gohr    abstract public function getRowIDs($values);
91d6396b6dSAndreas Gohr
92d6396b6dSAndreas Gohr    /**
93*03a35633SAndreas Gohr     * Find all RIDs matching a regular expression
94*03a35633SAndreas Gohr     *
95*03a35633SAndreas Gohr     * A full regular expression including delimiters and modifiers is expected
96*03a35633SAndreas Gohr     *
97*03a35633SAndreas Gohr     * @param string $re the regular expression to match against
98*03a35633SAndreas Gohr     * @return array (rid => value)
99*03a35633SAndreas Gohr     */
100*03a35633SAndreas Gohr    abstract public function search($re);
101*03a35633SAndreas Gohr
102*03a35633SAndreas Gohr    /**
103ec5280efSAndreas Gohr     * Clears the index by deleting its file
104ec5280efSAndreas Gohr     * @return void
105ec5280efSAndreas Gohr     */
106ec5280efSAndreas Gohr    public function clear()
107ec5280efSAndreas Gohr    {
108ec5280efSAndreas Gohr        @unlink($this->filename);
109ec5280efSAndreas Gohr    }
110ec5280efSAndreas Gohr
111ec5280efSAndreas Gohr}
112