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