Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Zend Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to version 1.0 of the Zend Framework
8
 * license, that is bundled with this package in the file LICENSE, and
9
 * is available through the world-wide-web at the following URL:
10
 * http://www.zend.com/license/framework/1_0.txt. If you did not receive
11
 * a copy of the Zend Framework license and are unable to obtain it
12
 * through the world-wide-web, please send a note to license@zend.com
13
 * so we can mail you a copy immediately.
14
 *
15
 * @package    Zend_Search_Lucene
16
 * @subpackage Storage
17
 * @copyright  Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
18
 * @license    http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
19
 */
20
 
21
 
22
/** Zend_Search_Lucene_Storage_Directory */
23
require_once 'Zend/Search/Lucene/Storage/Directory.php';
24
 
25
/** Zend_Search_Lucene_Storage_File_Filesystem */
26
require_once 'Zend/Search/Lucene/Storage/File/Filesystem.php';
27
 
28
 
29
/**
30
 * FileSystem implementation of Directory abstraction.
31
 *
32
 * @package    Zend_Search_Lucene
33
 * @subpackage Storage
34
 * @copyright  Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
35
 * @license    http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
36
 */
37
class Zend_Search_Lucene_Storage_Directory_Filesystem extends Zend_Search_Lucene_Storage_Directory
38
{
39
    /**
40
     * Filesystem path to the directory
41
     *
42
     * @var string
43
     */
44
    private $_dirPath = null;
45
 
46
    /**
47
     * Cache for Zend_Search_Lucene_Storage_File_Filesystem objects
48
     * Array: filename => Zend_Search_Lucene_Storage_File object
49
     *
50
     * @var array
51
     * @throws Zend_Search_Lucene_Exception
52
     */
53
    private $_fileHandlers;
54
 
55
 
56
    /**
57
     * Utility function to recursive directory creation
58
     *
59
     * @param string $dir
60
     * @param integer $mode
61
     * @param boolean $recursive
62
     * @return boolean
63
     */
64
 
65
    static public function mkdirs($dir, $mode = 0777, $recursive = true)
66
    {
67
        if (is_null($dir) || $dir === '') {
68
            return false;
69
        }
70
        if (is_dir($dir) || $dir === '/') {
71
            return true;
72
        }
73
        if (self::mkdirs(dirname($dir), $mode, $recursive)) {
74
            return mkdir($dir, $mode);
75
        }
76
        return false;
77
    }
78
 
79
 
80
    /**
81
     * Object constructor
82
     * Checks if $path is a directory or tries to create it.
83
     *
84
     * @param string $path
85
     * @throws Zend_Search_Lucene_Exception
86
     */
87
    public function __construct($path)
88
    {
89
        if (!is_dir($path)) {
90
            if (file_exists($path)) {
91
                throw new Zend_Search_Lucene_Exception('Path exists, but it\'s not a directory');
92
            } else {
93
                if (!self::mkdirs($path)) {
94
                    throw new Zend_Search_Lucene_Exception("Can't create directory '$path'.");
95
                }
96
            }
97
        }
98
        $this->_dirPath = $path;
99
        $this->_fileHandlers = array();
100
    }
101
 
102
 
103
    /**
104
     * Closes the store.
105
     *
106
     * @return void
107
     */
108
    public function close()
109
    {
110
        foreach ($this->_fileHandlers as $fileObject) {
111
            $fileObject->close();
112
        }
113
 
114
        unset($this->_fileHandlers);
115
    }
116
 
117
 
118
    /**
119
     * Returns an array of strings, one for each file in the directory.
120
     *
121
     * @return array
122
     */
123
    public function fileList()
124
    {
125
        $result = array();
126
 
127
        $dirContent = opendir( $this->_dirPath );
128
        while ($file = readdir($dirContent)) {
129
            if (($file == '..')||($file == '.'))   continue;
130
 
131
            $fullName = $this->_dirPath . '/' . $file;
132
 
133
            if( !is_dir($this->_dirPath . '/' . $file) ) {
134
                $result[] = $file;
135
            }
136
        }
137
 
138
        return $result;
139
    }
140
 
141
    /**
142
     * Creates a new, empty file in the directory with the given $filename.
143
     *
144
     * @param string $filename
145
     * @return Zend_Search_Lucene_Storage_File
146
     */
147
    public function createFile($filename)
148
    {
149
        if (isset($this->_fileHandlers[$filename])) {
150
            $this->_fileHandlers[$filename]->close();
151
        }
152
        unset($this->_fileHandlers[$filename]);
153
        $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($this->_dirPath . '/' . $filename, 'w+b');
154
        return $this->_fileHandlers[$filename];
155
    }
156
 
157
 
158
    /**
159
     * Removes an existing $filename in the directory.
160
     *
161
     * @param string $filename
162
     * @return void
163
     */
164
    public function deleteFile($filename)
165
    {
166
        if (isset($this->_fileHandlers[$filename])) {
167
            $this->_fileHandlers[$filename]->close();
168
        }
169
        unset($this->_fileHandlers[$filename]);
170
        unlink($this->_dirPath .'/'. $filename);
171
    }
172
 
173
 
174
    /**
175
     * Returns true if a file with the given $filename exists.
176
     *
177
     * @param string $filename
178
     * @return boolean
179
     */
180
    public function fileExists($filename)
181
    {
182
        return file_exists($this->_dirPath .'/'. $filename);
183
    }
184
 
185
 
186
    /**
187
     * Returns the length of a $filename in the directory.
188
     *
189
     * @param string $filename
190
     * @return integer
191
     */
192
    public function fileLength($filename)
193
    {
194
        if (isset( $this->_fileHandlers[$filename] )) {
195
            return $this->_fileHandlers[$filename]->size();
196
        }
197
        return filesize($this->_dirPath .'/'. $filename);
198
    }
199
 
200
 
201
    /**
202
     * Returns the UNIX timestamp $filename was last modified.
203
     *
204
     * @param string $filename
205
     * @return integer
206
     */
207
    public function fileModified($filename)
208
    {
209
        return filemtime($this->_dirPath .'/'. $filename);
210
    }
211
 
212
 
213
    /**
214
     * Renames an existing file in the directory.
215
     *
216
     * @param string $from
217
     * @param string $to
218
     * @return void
219
     */
220
    public function renameFile($from, $to)
221
    {
222
        if ($this->_fileHandlers[$from] !== null) {
223
            $this->_fileHandlers[$from]->close();
224
        }
225
        unset($this->_fileHandlers[$from]);
226
 
227
        if ($this->_fileHandlers[$to] !== null) {
228
            $this->_fileHandlers[$to]->close();
229
        }
230
        unset($this->_fileHandlers[$to]);
231
 
232
        if (file_exists($this->_dirPath . '/' . $to)) {
233
            unlink($this->_dirPath . '/' . $to);
234
        }
235
 
236
        return @rename($this->_dirPath . '/' . $from, $this->_dirPath . '/' . $to);
237
    }
238
 
239
 
240
    /**
241
     * Sets the modified time of $filename to now.
242
     *
243
     * @param string $filename
244
     * @return void
245
     */
246
    public function touchFile($filename)
247
    {
248
        return touch($this->_dirPath .'/'. $filename);
249
    }
250
 
251
 
252
    /**
253
     * Returns a Zend_Search_Lucene_Storage_File object for a given $filename in the directory.
254
     *
255
     * @param string $filename
256
     * @return Zend_Search_Lucene_Storage_File
257
     */
258
    public function getFileObject($filename)
259
    {
260
        if (isset( $this->_fileHandlers[$filename] )) {
261
            $this->_fileHandlers[$filename]->seek(0);
262
            return $this->_fileHandlers[$filename];
263
        }
264
 
265
        $this->_fileHandlers[$filename] = new Zend_Search_Lucene_Storage_File_Filesystem($this->_dirPath . '/' . $filename, 'rb');
266
        return $this->_fileHandlers[$filename];
267
    }
268
}
269