xref: /dokuwiki/inc/Search/Index/AbstractIndex.php (revision 7fcedc39d164da3013a88e9ad6660009940e851b)
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
19*7fcedc39SAndreas Gohr    /** @var bool is this index opened for writing? */
20*7fcedc39SAndreas Gohr    protected $isWritable = false;
21*7fcedc39SAndreas Gohr
22ec5280efSAndreas Gohr    /**
23ec5280efSAndreas Gohr     * Initialize the index
24ec5280efSAndreas Gohr     *
25ec5280efSAndreas Gohr     * The $suffix argument is for an index that is split into multiple parts.
26ec5280efSAndreas Gohr     * Different index files should use different base names.
27ec5280efSAndreas Gohr     *
28ec5280efSAndreas Gohr     * @param string $idx name of the index
29ec5280efSAndreas Gohr     * @param string $suffix subpart identifier
30*7fcedc39SAndreas Gohr     * @param bool $isWritable has a sufficient lock been acquired to write to this index?
31ec5280efSAndreas Gohr     */
32*7fcedc39SAndreas Gohr    public function __construct($idx, $suffix = '', $isWritable = false)
33ec5280efSAndreas Gohr    {
34ec5280efSAndreas Gohr        global $conf;
35ec5280efSAndreas Gohr        $this->filename = $conf['indexdir'] . '/' . $idx . $suffix . '.idx';
36ec5280efSAndreas Gohr        $this->idx = $idx;
37ec5280efSAndreas Gohr        $this->suffix = $suffix;
38*7fcedc39SAndreas Gohr        $this->isWritable = $isWritable;
39ec5280efSAndreas Gohr    }
40ec5280efSAndreas Gohr
41ec5280efSAndreas Gohr    /**
42ec5280efSAndreas Gohr     * @return string the full path to the underlying file
43ec5280efSAndreas Gohr     */
44ec5280efSAndreas Gohr    public function getFilename()
45ec5280efSAndreas Gohr    {
46ec5280efSAndreas Gohr        return $this->filename;
47ec5280efSAndreas Gohr    }
48ec5280efSAndreas Gohr
49ec5280efSAndreas Gohr    /**
50ec5280efSAndreas Gohr     * Change a line in the index
51ec5280efSAndreas Gohr     *
52ec5280efSAndreas Gohr     * If the line doesn't exist, it will be added, creating empty
53ec5280efSAndreas Gohr     * lines inbetween as necessary
54ec5280efSAndreas Gohr     *
55ec5280efSAndreas Gohr     * @param int $rid the line number, count starting at 0
56ec5280efSAndreas Gohr     * @param string $value line content to write
57ec5280efSAndreas Gohr     */
58ec5280efSAndreas Gohr    abstract public function changeRow($rid, $value);
59ec5280efSAndreas Gohr
60ec5280efSAndreas Gohr    /**
61ec5280efSAndreas Gohr     * Retrieve a line from the index
62ec5280efSAndreas Gohr     *
63ec5280efSAndreas Gohr     * Returns an empty string for non-existing lines
64ec5280efSAndreas Gohr     *
65ec5280efSAndreas Gohr     * @param int $rid the line number
66ec5280efSAndreas Gohr     * @return string a line with trailing whitespace removed
67ec5280efSAndreas Gohr     */
68ec5280efSAndreas Gohr    abstract public function retrieveRow($rid);
69ec5280efSAndreas Gohr
70ec5280efSAndreas Gohr    /**
719f63f003SAndreas Gohr     * Retrieve multiple lines from the index
729f63f003SAndreas Gohr     *
739f63f003SAndreas Gohr     * Ignores non-existing lines, eg the result array may be smaller than the input $rids
749f63f003SAndreas Gohr     *
759f63f003SAndreas Gohr     * @param int[] $rids
769f63f003SAndreas Gohr     * @return array [rid => value]
779f63f003SAndreas Gohr     */
789f63f003SAndreas Gohr    abstract public function retrieveRows($rids);
799f63f003SAndreas Gohr
809f63f003SAndreas Gohr    /**
81*7fcedc39SAndreas Gohr     * Searches the Index for a given value
82d6396b6dSAndreas Gohr     *
83*7fcedc39SAndreas Gohr     * If the index is writable and the value is not found it will be added. Otherwise null is returned.
84*7fcedc39SAndreas Gohr     *
85*7fcedc39SAndreas Gohr     * Entries previously marked as deleted will be restored.  FIXME is that true?
86d6396b6dSAndreas Gohr     *
879f63f003SAndreas Gohr     * Note the existence of an entry in the index does not say anything about the existence
88d6396b6dSAndreas Gohr     * of the real world object (eg. a page)
89d6396b6dSAndreas Gohr     *
90d6396b6dSAndreas Gohr     * You should preferable use accessCachedValue() instead.
91d6396b6dSAndreas Gohr     *
92d6396b6dSAndreas Gohr     * @param string $value
93*7fcedc39SAndreas Gohr     *
94*7fcedc39SAndreas Gohr     * @return int|null the RID of the entry, null if not found and not added
95d6396b6dSAndreas Gohr     */
968ed35011SAndreas Gohr    public function getRowID($value)
97d6396b6dSAndreas Gohr    {
988ed35011SAndreas Gohr        $result = $this->getRowIDs([$value]);
99*7fcedc39SAndreas Gohr        return $result[$value] ?? null;
100d6396b6dSAndreas Gohr    }
101d6396b6dSAndreas Gohr
102d6396b6dSAndreas Gohr    /**
103*7fcedc39SAndreas Gohr     * Searches the Index for all given values
104*7fcedc39SAndreas Gohr     *
105*7fcedc39SAndreas Gohr     * If the index is writable, not found values are added
106d6396b6dSAndreas Gohr     *
107d6396b6dSAndreas Gohr     * @param string[] $values
10803a35633SAndreas Gohr     * @return array the RIDs of the entries (value => rid)
109d6396b6dSAndreas Gohr     */
1108ed35011SAndreas Gohr    abstract public function getRowIDs($values);
111d6396b6dSAndreas Gohr
112d6396b6dSAndreas Gohr    /**
11303a35633SAndreas Gohr     * Find all RIDs matching a regular expression
11403a35633SAndreas Gohr     *
11503a35633SAndreas Gohr     * A full regular expression including delimiters and modifiers is expected
11603a35633SAndreas Gohr     *
11703a35633SAndreas Gohr     * @param string $re the regular expression to match against
11803a35633SAndreas Gohr     * @return array (rid => value)
11903a35633SAndreas Gohr     */
12003a35633SAndreas Gohr    abstract public function search($re);
12103a35633SAndreas Gohr
12203a35633SAndreas Gohr    /**
123ec5280efSAndreas Gohr     * Clears the index by deleting its file
124ec5280efSAndreas Gohr     * @return void
125ec5280efSAndreas Gohr     */
126ec5280efSAndreas Gohr    public function clear()
127ec5280efSAndreas Gohr    {
128ec5280efSAndreas Gohr        @unlink($this->filename);
129ec5280efSAndreas Gohr    }
130ec5280efSAndreas Gohr
131*7fcedc39SAndreas Gohr    /**
132*7fcedc39SAndreas Gohr     * Saves the index if needed
133*7fcedc39SAndreas Gohr     *
134*7fcedc39SAndreas Gohr     * The default implementation does nothing and is only for streamlining the API of
135*7fcedc39SAndreas Gohr     * the different index classes
136*7fcedc39SAndreas Gohr     * @return void
137*7fcedc39SAndreas Gohr     */
138*7fcedc39SAndreas Gohr    public function save()
139*7fcedc39SAndreas Gohr    {
140*7fcedc39SAndreas Gohr    }
141ec5280efSAndreas Gohr}
142