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'); 359bd7d62fSAndreas Gohr if (!$fh) throw new IndexWriteException("Failed to write {$tempname}"); 369bd7d62fSAndreas Gohr $ih = @fopen($this->filename, 'r'); 379bd7d62fSAndreas Gohr 389bd7d62fSAndreas Gohr $ln = -1; // line counter 399bd7d62fSAndreas Gohr // copy previous index lines line-by-line, replacing the wanted line 409bd7d62fSAndreas Gohr if ($ih) { 419bd7d62fSAndreas Gohr while (($curline = fgets($ih)) !== false) { 429bd7d62fSAndreas Gohr fwrite($fh, (++$ln == $rid) ? $value : $curline); 439bd7d62fSAndreas Gohr } 449bd7d62fSAndreas Gohr fclose($ih); 459bd7d62fSAndreas Gohr } 469bd7d62fSAndreas Gohr // if wanted line is beyond the current line count, insert empty lines inbetween 479bd7d62fSAndreas Gohr if ($rid > $ln) { 489bd7d62fSAndreas Gohr while ($rid > ++$ln) { 499bd7d62fSAndreas Gohr fwrite($fh, "\n"); 509bd7d62fSAndreas Gohr } 519bd7d62fSAndreas Gohr fwrite($fh, $value); 529bd7d62fSAndreas Gohr } 539bd7d62fSAndreas Gohr fclose($fh); 549bd7d62fSAndreas Gohr 559bd7d62fSAndreas Gohr if ($conf['fperm']) { 569bd7d62fSAndreas Gohr chmod($tempname, $conf['fperm']); 579bd7d62fSAndreas Gohr } 589bd7d62fSAndreas Gohr io_rename($tempname, $this->filename); 599bd7d62fSAndreas Gohr } 609bd7d62fSAndreas Gohr 619bd7d62fSAndreas Gohr /** 629bd7d62fSAndreas Gohr * @inheritdoc 639bd7d62fSAndreas Gohr * @author Tom N Harris <tnharris@whoopdedo.org> 649bd7d62fSAndreas Gohr */ 659bd7d62fSAndreas Gohr public function retrieveRow($rid) 669bd7d62fSAndreas Gohr { 679bd7d62fSAndreas Gohr if (!file_exists($this->filename)) return ''; 689bd7d62fSAndreas Gohr $fh = @fopen($this->filename, 'r'); 699bd7d62fSAndreas Gohr if (!$fh) return ''; 709bd7d62fSAndreas Gohr $ln = -1; 719bd7d62fSAndreas Gohr while (($line = fgets($fh)) !== false) { 72*dec26820SAndreas Gohr if (++$ln == $rid) { 739bd7d62fSAndreas Gohr fclose($fh); 749bd7d62fSAndreas Gohr return rtrim((string)$line); 759bd7d62fSAndreas Gohr } 76*dec26820SAndreas Gohr } 77*dec26820SAndreas Gohr fclose($fh); 78*dec26820SAndreas Gohr 79*dec26820SAndreas Gohr // still here? pad the index for the given ID 80*dec26820SAndreas Gohr // we do not simply call changeRow() here because appending is faster than line-by-line copying 81*dec26820SAndreas Gohr if (!file_put_contents($this->filename, join("\n", array_fill(0, $rid - $ln + 1, '')), FILE_APPEND)) { 82*dec26820SAndreas Gohr throw new IndexWriteException("Failed to write {$this->filename}"); 83*dec26820SAndreas Gohr } 84*dec26820SAndreas Gohr 85*dec26820SAndreas Gohr return ''; 86*dec26820SAndreas Gohr } 879bd7d62fSAndreas Gohr 889bd7d62fSAndreas Gohr /** 89d6396b6dSAndreas Gohr * @inheritdoc 909bd7d62fSAndreas Gohr * @throws IndexAccessException 919bd7d62fSAndreas Gohr */ 928ed35011SAndreas Gohr public function getRowIDs($values) 939bd7d62fSAndreas Gohr { 949bd7d62fSAndreas Gohr $values = array_map('trim', $values); 959bd7d62fSAndreas Gohr $values = array_fill_keys($values, 1); // easier access as associative array 969bd7d62fSAndreas Gohr 979bd7d62fSAndreas Gohr // search for the values 989bd7d62fSAndreas Gohr $result = []; 999bd7d62fSAndreas Gohr $ln = 0; 1009bd7d62fSAndreas Gohr if (file_exists($this->filename)) { 1019bd7d62fSAndreas Gohr $fh = @fopen($this->filename, 'r'); 1029bd7d62fSAndreas Gohr if (!$fh) throw new IndexAccessException("Failed to read {$this->filename}"); 1039bd7d62fSAndreas Gohr while (($line = fgets($fh)) !== false && $values) { 1049bd7d62fSAndreas Gohr $line = trim($line); 1059bd7d62fSAndreas Gohr if (isset($values[$line])) { 1069bd7d62fSAndreas Gohr $result[$line] = $ln; 1079bd7d62fSAndreas Gohr unset($values[$line]); 1089bd7d62fSAndreas Gohr } 1099bd7d62fSAndreas Gohr $ln++; 1109bd7d62fSAndreas Gohr } 1119bd7d62fSAndreas Gohr fclose($fh); 1129bd7d62fSAndreas Gohr } 1139bd7d62fSAndreas Gohr 1149bd7d62fSAndreas Gohr // if there are still values, they have not been found and will be appended 1159bd7d62fSAndreas Gohr foreach (array_keys($values) as $value) { 1169bd7d62fSAndreas Gohr file_put_contents($this->filename, "$value\n", FILE_APPEND); 1179bd7d62fSAndreas Gohr $result[$value] = $ln++; 1189bd7d62fSAndreas Gohr } 1199bd7d62fSAndreas Gohr 1209bd7d62fSAndreas Gohr return $result; 1219bd7d62fSAndreas Gohr } 1229bd7d62fSAndreas Gohr 1239bd7d62fSAndreas Gohr /** 1249bd7d62fSAndreas Gohr * Cached version of accessCachedValue() 1259bd7d62fSAndreas Gohr * 1269bd7d62fSAndreas Gohr * @param string $value 1279bd7d62fSAndreas Gohr * @return int the RID of the entry 1289bd7d62fSAndreas Gohr * @throws IndexAccessException 1299bd7d62fSAndreas Gohr * @throws IndexWriteException 1309bd7d62fSAndreas Gohr */ 1319bd7d62fSAndreas Gohr public function accessCachedValue($value) 1329bd7d62fSAndreas Gohr { 1339bd7d62fSAndreas Gohr if (isset(static::$ridCache['value'])) return static::$ridCache['value']; 1349bd7d62fSAndreas Gohr 1359bd7d62fSAndreas Gohr // limit cache to 10 entries by discarding the oldest element 1369bd7d62fSAndreas Gohr // as in DokuWiki usually only the most recently 1379bd7d62fSAndreas Gohr // added item will be requested again 1389bd7d62fSAndreas Gohr if (count(static::$ridCache) > 10) array_shift(static::$ridCache); 1398ed35011SAndreas Gohr static::$ridCache[$value] = $this->getRowID($value); 1409bd7d62fSAndreas Gohr return static::$ridCache[$value]; 1419bd7d62fSAndreas Gohr } 1429bd7d62fSAndreas Gohr} 143