xref: /dokuwiki/inc/Search/Index/AbstractIndex.php (revision d6396b6d927e3ff373c0aed3927ae61eaca8d537)
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*d6396b6dSAndreas Gohr     * Searches the Index for a given value and adds it if not found
67*d6396b6dSAndreas Gohr     *
68*d6396b6dSAndreas Gohr     * Entries previously marked as deleted will be restored.
69*d6396b6dSAndreas Gohr     *
70*d6396b6dSAndreas Gohr     * Note the existance of an entry in the index does not say anything about the exististance
71*d6396b6dSAndreas Gohr     * of the real world object (eg. a page)
72*d6396b6dSAndreas Gohr     *
73*d6396b6dSAndreas Gohr     * You should preferable use accessCachedValue() instead.
74*d6396b6dSAndreas Gohr     *
75*d6396b6dSAndreas Gohr     * @param string $value
76*d6396b6dSAndreas Gohr     * @return int the RID of the entry
77*d6396b6dSAndreas Gohr     */
78*d6396b6dSAndreas Gohr    public function accessValue($value)
79*d6396b6dSAndreas Gohr    {
80*d6396b6dSAndreas Gohr        $result = $this->accessValues([$value]);
81*d6396b6dSAndreas Gohr        return $result[$value];
82*d6396b6dSAndreas Gohr    }
83*d6396b6dSAndreas Gohr
84*d6396b6dSAndreas Gohr    /**
85*d6396b6dSAndreas Gohr     * Searches the Index for all given values and adds them if not found
86*d6396b6dSAndreas Gohr     *
87*d6396b6dSAndreas Gohr     * @param string[] $values
88*d6396b6dSAndreas Gohr     * @return array the RIDs of the entries
89*d6396b6dSAndreas Gohr     */
90*d6396b6dSAndreas Gohr    abstract public function accessValues($values);
91*d6396b6dSAndreas Gohr
92*d6396b6dSAndreas Gohr    /**
93ec5280efSAndreas Gohr     * Clears the index by deleting its file
94ec5280efSAndreas Gohr     * @return void
95ec5280efSAndreas Gohr     */
96ec5280efSAndreas Gohr    public function clear()
97ec5280efSAndreas Gohr    {
98ec5280efSAndreas Gohr        @unlink($this->filename);
99ec5280efSAndreas Gohr    }
100ec5280efSAndreas Gohr
101ec5280efSAndreas Gohr}
102