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