xref: /dokuwiki/inc/Search/Index/AbstractIndex.php (revision ec5280ef60d74097cbe378fec2fe18b93cc6b04e)
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     * Clears the index by deleting its file
67     * @return void
68     */
69    public function clear()
70    {
71        @unlink($this->filename);
72    }
73
74}
75