xref: /dokuwiki/inc/Search/Index/AbstractIndex.php (revision ec5280ef60d74097cbe378fec2fe18b93cc6b04e)
1*ec5280efSAndreas Gohr<?php
2*ec5280efSAndreas Gohr
3*ec5280efSAndreas Gohrnamespace dokuwiki\Search\Index;
4*ec5280efSAndreas Gohr
5*ec5280efSAndreas Gohr/**
6*ec5280efSAndreas Gohr * Basic Building block to access individual index files
7*ec5280efSAndreas Gohr */
8*ec5280efSAndreas Gohrabstract class AbstractIndex
9*ec5280efSAndreas Gohr{
10*ec5280efSAndreas Gohr    /** @var string name of the index */
11*ec5280efSAndreas Gohr    protected $idx;
12*ec5280efSAndreas Gohr
13*ec5280efSAndreas Gohr    /** @var string $suffix of the index */
14*ec5280efSAndreas Gohr    protected $suffix;
15*ec5280efSAndreas Gohr
16*ec5280efSAndreas Gohr    /** @var string full filename to the index */
17*ec5280efSAndreas Gohr    protected $filename;
18*ec5280efSAndreas Gohr
19*ec5280efSAndreas Gohr    /**
20*ec5280efSAndreas Gohr     * Initialize the index
21*ec5280efSAndreas Gohr     *
22*ec5280efSAndreas Gohr     * The $suffix argument is for an index that is split into multiple parts.
23*ec5280efSAndreas Gohr     * Different index files should use different base names.
24*ec5280efSAndreas Gohr     *
25*ec5280efSAndreas Gohr     * @param string $idx name of the index
26*ec5280efSAndreas Gohr     * @param string $suffix subpart identifier
27*ec5280efSAndreas Gohr     */
28*ec5280efSAndreas Gohr    public function __construct($idx, $suffix = '')
29*ec5280efSAndreas Gohr    {
30*ec5280efSAndreas Gohr        global $conf;
31*ec5280efSAndreas Gohr        $this->filename = $conf['indexdir'] . '/' . $idx . $suffix . '.idx';
32*ec5280efSAndreas Gohr        $this->idx = $idx;
33*ec5280efSAndreas Gohr        $this->suffix = $suffix;
34*ec5280efSAndreas Gohr    }
35*ec5280efSAndreas Gohr
36*ec5280efSAndreas Gohr    /**
37*ec5280efSAndreas Gohr     * @return string the full path to the underlying file
38*ec5280efSAndreas Gohr     */
39*ec5280efSAndreas Gohr    public function getFilename()
40*ec5280efSAndreas Gohr    {
41*ec5280efSAndreas Gohr        return $this->filename;
42*ec5280efSAndreas Gohr    }
43*ec5280efSAndreas Gohr
44*ec5280efSAndreas Gohr    /**
45*ec5280efSAndreas Gohr     * Change a line in the index
46*ec5280efSAndreas Gohr     *
47*ec5280efSAndreas Gohr     * If the line doesn't exist, it will be added, creating empty
48*ec5280efSAndreas Gohr     * lines inbetween as necessary
49*ec5280efSAndreas Gohr     *
50*ec5280efSAndreas Gohr     * @param int $rid the line number, count starting at 0
51*ec5280efSAndreas Gohr     * @param string $value line content to write
52*ec5280efSAndreas Gohr     */
53*ec5280efSAndreas Gohr    abstract public function changeRow($rid, $value);
54*ec5280efSAndreas Gohr
55*ec5280efSAndreas Gohr    /**
56*ec5280efSAndreas Gohr     * Retrieve a line from the index
57*ec5280efSAndreas Gohr     *
58*ec5280efSAndreas Gohr     * Returns an empty string for non-existing lines
59*ec5280efSAndreas Gohr     *
60*ec5280efSAndreas Gohr     * @param int $rid the line number
61*ec5280efSAndreas Gohr     * @return string a line with trailing whitespace removed
62*ec5280efSAndreas Gohr     */
63*ec5280efSAndreas Gohr    abstract public function retrieveRow($rid);
64*ec5280efSAndreas Gohr
65*ec5280efSAndreas Gohr    /**
66*ec5280efSAndreas Gohr     * Clears the index by deleting its file
67*ec5280efSAndreas Gohr     * @return void
68*ec5280efSAndreas Gohr     */
69*ec5280efSAndreas Gohr    public function clear()
70*ec5280efSAndreas Gohr    {
71*ec5280efSAndreas Gohr        @unlink($this->filename);
72*ec5280efSAndreas Gohr    }
73*ec5280efSAndreas Gohr
74*ec5280efSAndreas Gohr}
75