1*596d5287SAndreas Gohr<?php 2*596d5287SAndreas Gohr 3*596d5287SAndreas Gohrnamespace dokuwiki\Search\Index; 4*596d5287SAndreas Gohr 5*596d5287SAndreas Gohr/** 6*596d5287SAndreas Gohr * Manage locking for index writing 7*596d5287SAndreas Gohr * 8*596d5287SAndreas Gohr * Locks are directories in the dta/lock directory named after the index name 9*596d5287SAndreas Gohr */ 10*596d5287SAndreas Gohrclass Lock 11*596d5287SAndreas Gohr{ 12*596d5287SAndreas Gohr protected $lockDir = ''; 13*596d5287SAndreas Gohr protected $indexName = ''; 14*596d5287SAndreas Gohr 15*596d5287SAndreas Gohr /** 16*596d5287SAndreas Gohr * @param string $name Name of the index 17*596d5287SAndreas Gohr */ 18*596d5287SAndreas Gohr public function __construct($name) 19*596d5287SAndreas Gohr { 20*596d5287SAndreas Gohr global $conf; 21*596d5287SAndreas Gohr $this->indexName = $name; 22*596d5287SAndreas Gohr $this->lockDir = $conf['lockdir'] . $name . '.index'; 23*596d5287SAndreas Gohr } 24*596d5287SAndreas Gohr 25*596d5287SAndreas Gohr /** 26*596d5287SAndreas Gohr * Try to acquire a lock for an index 27*596d5287SAndreas Gohr * 28*596d5287SAndreas Gohr * @return bool true if a lock was acquired, otherwise false 29*596d5287SAndreas Gohr */ 30*596d5287SAndreas Gohr public function acquire() 31*596d5287SAndreas Gohr { 32*596d5287SAndreas Gohr if(@mkdir($this->lockDir)) return true; 33*596d5287SAndreas Gohr // creation of the lockdir failed, check if it's stale 34*596d5287SAndreas Gohr if(time() - filemtime($this->lockDir) > 60*5) { 35*596d5287SAndreas Gohr // try to release, then lock again 36*596d5287SAndreas Gohr $this->release(); 37*596d5287SAndreas Gohr return @mkdir($this->lockDir); 38*596d5287SAndreas Gohr } 39*596d5287SAndreas Gohr return false; 40*596d5287SAndreas Gohr } 41*596d5287SAndreas Gohr 42*596d5287SAndreas Gohr /** 43*596d5287SAndreas Gohr * Release the lock for this index 44*596d5287SAndreas Gohr * 45*596d5287SAndreas Gohr * @return void 46*596d5287SAndreas Gohr */ 47*596d5287SAndreas Gohr public function release() 48*596d5287SAndreas Gohr { 49*596d5287SAndreas Gohr @rmdir($this->lockDir); 50*596d5287SAndreas Gohr } 51*596d5287SAndreas Gohr 52*596d5287SAndreas Gohr} 53