19bd7d62fSAndreas Gohr<?php 29bd7d62fSAndreas Gohr 39bd7d62fSAndreas Gohrnamespace dokuwiki\Search\Index; 49bd7d62fSAndreas Gohr 59bd7d62fSAndreas Gohruse dokuwiki\Search\Exception\IndexAccessException; 69bd7d62fSAndreas Gohruse dokuwiki\Search\Exception\IndexWriteException; 79bd7d62fSAndreas Gohr 89bd7d62fSAndreas Gohr/** 99bd7d62fSAndreas Gohr * Access to a single index file 109bd7d62fSAndreas Gohr * 119bd7d62fSAndreas Gohr * Access using this class always happens on a line-by-line basis. It is usually not read in full. 129bd7d62fSAndreas Gohr * All modifications are implicitly saved 139bd7d62fSAndreas Gohr * Should be used for large indexes that receive only few changes at once. 149bd7d62fSAndreas Gohr */ 159bd7d62fSAndreas Gohrclass FileIndex extends AbstractIndex 169bd7d62fSAndreas Gohr{ 179bd7d62fSAndreas Gohr /** @var array RID cache for faster access */ 189bd7d62fSAndreas Gohr protected static $ridCache = []; 199bd7d62fSAndreas Gohr 209bd7d62fSAndreas Gohr /** 219bd7d62fSAndreas Gohr * @inheritdoc 229bd7d62fSAndreas Gohr * @throws IndexWriteException 239bd7d62fSAndreas Gohr * @author Tom N Harris <tnharris@whoopdedo.org> 249bd7d62fSAndreas Gohr */ 259bd7d62fSAndreas Gohr public function changeRow($rid, $value) 269bd7d62fSAndreas Gohr { 279bd7d62fSAndreas Gohr global $conf; 289bd7d62fSAndreas Gohr 299bd7d62fSAndreas Gohr if (substr($value, -1) !== "\n") { 309bd7d62fSAndreas Gohr $value .= "\n"; 319bd7d62fSAndreas Gohr } 329bd7d62fSAndreas Gohr 339bd7d62fSAndreas Gohr $tempname = $this->filename . '.tmp'; 349bd7d62fSAndreas Gohr $fh = @fopen($tempname, 'w'); 3503a35633SAndreas Gohr if (!$fh) { 3603a35633SAndreas Gohr throw new IndexWriteException("Failed to write {$tempname}"); 3703a35633SAndreas Gohr } 389bd7d62fSAndreas Gohr $ih = @fopen($this->filename, 'r'); 399bd7d62fSAndreas Gohr 409bd7d62fSAndreas Gohr $ln = -1; // line counter 419bd7d62fSAndreas Gohr // copy previous index lines line-by-line, replacing the wanted line 429bd7d62fSAndreas Gohr if ($ih) { 439bd7d62fSAndreas Gohr while (($curline = fgets($ih)) !== false) { 449bd7d62fSAndreas Gohr fwrite($fh, (++$ln == $rid) ? $value : $curline); 459bd7d62fSAndreas Gohr } 469bd7d62fSAndreas Gohr fclose($ih); 479bd7d62fSAndreas Gohr } 489bd7d62fSAndreas Gohr // if wanted line is beyond the current line count, insert empty lines inbetween 499bd7d62fSAndreas Gohr if ($rid > $ln) { 509bd7d62fSAndreas Gohr while ($rid > ++$ln) { 519bd7d62fSAndreas Gohr fwrite($fh, "\n"); 529bd7d62fSAndreas Gohr } 539bd7d62fSAndreas Gohr fwrite($fh, $value); 549bd7d62fSAndreas Gohr } 559bd7d62fSAndreas Gohr fclose($fh); 569bd7d62fSAndreas Gohr 579bd7d62fSAndreas Gohr if ($conf['fperm']) { 589bd7d62fSAndreas Gohr chmod($tempname, $conf['fperm']); 599bd7d62fSAndreas Gohr } 609bd7d62fSAndreas Gohr io_rename($tempname, $this->filename); 619bd7d62fSAndreas Gohr } 629bd7d62fSAndreas Gohr 639bd7d62fSAndreas Gohr /** 649bd7d62fSAndreas Gohr * @inheritdoc 659bd7d62fSAndreas Gohr * @author Tom N Harris <tnharris@whoopdedo.org> 669bd7d62fSAndreas Gohr */ 679bd7d62fSAndreas Gohr public function retrieveRow($rid) 689bd7d62fSAndreas Gohr { 6903a35633SAndreas Gohr if (!file_exists($this->filename)) { 7003a35633SAndreas Gohr return ''; 7103a35633SAndreas Gohr } 729bd7d62fSAndreas Gohr $fh = @fopen($this->filename, 'r'); 7303a35633SAndreas Gohr if (!$fh) { 7403a35633SAndreas Gohr return ''; 7503a35633SAndreas Gohr } 769bd7d62fSAndreas Gohr $ln = -1; 779bd7d62fSAndreas Gohr while (($line = fgets($fh)) !== false) { 78dec26820SAndreas Gohr if (++$ln == $rid) { 799bd7d62fSAndreas Gohr fclose($fh); 809bd7d62fSAndreas Gohr return rtrim((string)$line); 819bd7d62fSAndreas Gohr } 82dec26820SAndreas Gohr } 83dec26820SAndreas Gohr fclose($fh); 84dec26820SAndreas Gohr 85dec26820SAndreas Gohr // still here? pad the index for the given ID 86dec26820SAndreas Gohr // we do not simply call changeRow() here because appending is faster than line-by-line copying 87dec26820SAndreas Gohr if (!file_put_contents($this->filename, join("\n", array_fill(0, $rid - $ln + 1, '')), FILE_APPEND)) { 88dec26820SAndreas Gohr throw new IndexWriteException("Failed to write {$this->filename}"); 89dec26820SAndreas Gohr } 90dec26820SAndreas Gohr 91dec26820SAndreas Gohr return ''; 92dec26820SAndreas Gohr } 93*9f63f003SAndreas Gohr 94*9f63f003SAndreas Gohr /** @inheritdoc */ 95*9f63f003SAndreas Gohr public function retrieveRows($rids) 96*9f63f003SAndreas Gohr { 97*9f63f003SAndreas Gohr $result = []; 98*9f63f003SAndreas Gohr sort($rids); 99*9f63f003SAndreas Gohr $next = array_shift($rids); 100*9f63f003SAndreas Gohr 101*9f63f003SAndreas Gohr if (!file_exists($this->filename)) { 102*9f63f003SAndreas Gohr return $result; 103*9f63f003SAndreas Gohr } 104*9f63f003SAndreas Gohr $fh = @fopen($this->filename, 'r'); 105*9f63f003SAndreas Gohr if (!$fh) { 106*9f63f003SAndreas Gohr return $result; 107*9f63f003SAndreas Gohr } 108*9f63f003SAndreas Gohr $ln = -1; 109*9f63f003SAndreas Gohr while (($line = fgets($fh)) !== false) { 110*9f63f003SAndreas Gohr if (++$ln === $next) { 111*9f63f003SAndreas Gohr $result[$ln] = rtrim((string)$line); 112*9f63f003SAndreas Gohr $next = array_shift($rids); 113*9f63f003SAndreas Gohr if ($next === false) break; 114*9f63f003SAndreas Gohr } 115*9f63f003SAndreas Gohr } 116*9f63f003SAndreas Gohr fclose($fh); 117*9f63f003SAndreas Gohr return $result; 118*9f63f003SAndreas Gohr } 119*9f63f003SAndreas Gohr 1209bd7d62fSAndreas Gohr 1219bd7d62fSAndreas Gohr /** 122d6396b6dSAndreas Gohr * @inheritdoc 1239bd7d62fSAndreas Gohr * @throws IndexAccessException 1249bd7d62fSAndreas Gohr */ 1258ed35011SAndreas Gohr public function getRowIDs($values) 1269bd7d62fSAndreas Gohr { 1279bd7d62fSAndreas Gohr $values = array_map('trim', $values); 1289bd7d62fSAndreas Gohr $values = array_fill_keys($values, 1); // easier access as associative array 1299bd7d62fSAndreas Gohr 1309bd7d62fSAndreas Gohr // search for the values 1319bd7d62fSAndreas Gohr $result = []; 1329bd7d62fSAndreas Gohr $ln = 0; 1339bd7d62fSAndreas Gohr if (file_exists($this->filename)) { 1349bd7d62fSAndreas Gohr $fh = @fopen($this->filename, 'r'); 13503a35633SAndreas Gohr if (!$fh) { 13603a35633SAndreas Gohr throw new IndexAccessException("Failed to read {$this->filename}"); 13703a35633SAndreas Gohr } 1389bd7d62fSAndreas Gohr while (($line = fgets($fh)) !== false && $values) { 1399bd7d62fSAndreas Gohr $line = trim($line); 1409bd7d62fSAndreas Gohr if (isset($values[$line])) { 1419bd7d62fSAndreas Gohr $result[$line] = $ln; 1429bd7d62fSAndreas Gohr unset($values[$line]); 1439bd7d62fSAndreas Gohr } 1449bd7d62fSAndreas Gohr $ln++; 1459bd7d62fSAndreas Gohr } 1469bd7d62fSAndreas Gohr fclose($fh); 1479bd7d62fSAndreas Gohr } 1489bd7d62fSAndreas Gohr 1499bd7d62fSAndreas Gohr // if there are still values, they have not been found and will be appended 1509bd7d62fSAndreas Gohr foreach (array_keys($values) as $value) { 1519bd7d62fSAndreas Gohr file_put_contents($this->filename, "$value\n", FILE_APPEND); 1529bd7d62fSAndreas Gohr $result[$value] = $ln++; 1539bd7d62fSAndreas Gohr } 1549bd7d62fSAndreas Gohr 1559bd7d62fSAndreas Gohr return $result; 1569bd7d62fSAndreas Gohr } 1579bd7d62fSAndreas Gohr 15803a35633SAndreas Gohr /** @inheritdoc */ 15903a35633SAndreas Gohr public function search($re) 16003a35633SAndreas Gohr { 16103a35633SAndreas Gohr $result = []; 16203a35633SAndreas Gohr $ln = 0; 16303a35633SAndreas Gohr if (file_exists($this->filename)) { 16403a35633SAndreas Gohr $fh = @fopen($this->filename, 'r'); 16503a35633SAndreas Gohr if (!$fh) { 16603a35633SAndreas Gohr throw new IndexAccessException("Failed to read {$this->filename}"); 16703a35633SAndreas Gohr } 16803a35633SAndreas Gohr while (($line = fgets($fh)) !== false) { 16903a35633SAndreas Gohr $line = trim($line); 17003a35633SAndreas Gohr if (preg_match($re, $line)) { 17103a35633SAndreas Gohr $result[$ln] = $line; 17203a35633SAndreas Gohr } 17303a35633SAndreas Gohr $ln++; 17403a35633SAndreas Gohr } 17503a35633SAndreas Gohr fclose($fh); 17603a35633SAndreas Gohr } 17703a35633SAndreas Gohr return $result; 17803a35633SAndreas Gohr } 17903a35633SAndreas Gohr 1809bd7d62fSAndreas Gohr /** 1819bd7d62fSAndreas Gohr * Cached version of accessCachedValue() 1829bd7d62fSAndreas Gohr * 1839bd7d62fSAndreas Gohr * @param string $value 1849bd7d62fSAndreas Gohr * @return int the RID of the entry 1859bd7d62fSAndreas Gohr * @throws IndexAccessException 1869bd7d62fSAndreas Gohr * @throws IndexWriteException 1879bd7d62fSAndreas Gohr */ 1889bd7d62fSAndreas Gohr public function accessCachedValue($value) 1899bd7d62fSAndreas Gohr { 19003a35633SAndreas Gohr if (isset(static::$ridCache['value'])) { 19103a35633SAndreas Gohr return static::$ridCache['value']; 19203a35633SAndreas Gohr } 1939bd7d62fSAndreas Gohr 1949bd7d62fSAndreas Gohr // limit cache to 10 entries by discarding the oldest element 1959bd7d62fSAndreas Gohr // as in DokuWiki usually only the most recently 1969bd7d62fSAndreas Gohr // added item will be requested again 19703a35633SAndreas Gohr if (count(static::$ridCache) > 10) { 19803a35633SAndreas Gohr array_shift(static::$ridCache); 19903a35633SAndreas Gohr } 2008ed35011SAndreas Gohr static::$ridCache[$value] = $this->getRowID($value); 2019bd7d62fSAndreas Gohr return static::$ridCache[$value]; 2029bd7d62fSAndreas Gohr } 2039bd7d62fSAndreas Gohr} 204