Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.02 of the PHP license,      |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Wolfram Kriesing <wolfram@kriesing.de>                      |
17
// +----------------------------------------------------------------------+
18
//
19
//  $Id: Filesystem.php,v 1.9.2.2 2009/03/12 17:19:48 dufuz Exp $
20
 
21
require_once 'Tree/Error.php';
22
 
23
/**
24
 * the Filesystem interface to the tree class
25
 * this is a bit different, as id we use the entire path, since we know
26
 * this is unique in a filesystem and an integer id could only be created
27
 * virtually, it doesnt have a tight connection to the actual directory
28
 * i.e. using 'add' with ids could fail since we dont know if we get the same
29
 * id when we call 'add' with a parentId to create a new folder under since
30
 * our id would be made up. So we use the complete path as id, which takes up
31
 * a lot of memory, i know but since php is typeless its possible and it makes
32
 * life easier when we want to create another dir, since we get the dir
33
 * as parentId passed to the 'add' method, which makes it easy to create
34
 * a new dir there :-)
35
 * I also thought about hashing the path name but then the add method is
36
 * not that easy to implement ... may be one day :-)
37
 *
38
 * @access     public
39
 * @author     Wolfram Kriesing <wolfram@kriesing.de>
40
 * @version    2001/06/27
41
 * @package    Tree
42
 */
43
 class Tree_Memory_Filesystem
44
 {
45
 
46
    /**
47
     * @access public
48
     * @var    array   saves the options passed to the constructor
49
     */
50
    var $options =  array(
51
                        'order'     => '',
52
                        'columnNameMaps' => array(),
53
                    );
54
 
55
    // {{{ Tree_Memory_Filesystem()
56
 
57
    /**
58
     * set up this object
59
     *
60
     * @version    2002/08/23
61
     * @access     public
62
     * @author     Wolfram Kriesing <wolfram@kriesing.de>
63
     * @param      string  $dsn    the path on the filesystem
64
     * @param      array   $options  additional options you can set
65
     */
66
    function Tree_Memory_Filesystem ($path, $options = array())
67
    {
68
        $this->_path = $path;
69
        // not in use currently
70
        $this->_options = $options;
71
    }
72
 
73
    // }}}
74
    // {{{ setup()
75
 
76
    /**
77
     * retreive all the navigation data from the db and call build to build
78
     * the tree in the array data and structure
79
     *
80
     * @version    2001/11/20
81
     * @access     public
82
     * @author     Wolfram Kriesing <wolfram@kriesing.de>
83
     * @return     boolean     true on success
84
     */
85
    function setup()
86
    {
87
        // unset the data to be sure to get the real data again, no old data
88
        unset($this->data);
89
        if (is_dir($this->_path)) {
90
            $this->data[$this->_path] = array(
91
                                          'id'       => $this->_path,
92
                                          'name'     => $this->_path,
93
                                          'parentId' => 0
94
                                        );
95
            $this->_setup($this->_path, $this->_path);
96
        }
97
        return $this->data;
98
    }
99
 
100
    // }}}
101
    // {{{ _setup()
102
 
103
    function _setup($path, $parentId = 0)
104
    {
105
        if ($handle = opendir($path)) {
106
            while (false !== ($file = readdir($handle))) {
107
                if ($file != '.' && $file != '..'
108
                    && is_dir("$path/$file")) {
109
                    $this->data[] = array(
110
                                        'id'       => "$path/$file",
111
                                        'name'     => $file,
112
                                        'parentId' => $parentId
113
                                    );
114
                    $this->_setup("$path/$file", "$path/$file");
115
                }
116
            }
117
            closedir($handle);
118
        }
119
    }
120
 
121
    // }}}
122
    // {{{ add()
123
 
124
    /**
125
     * this is tricky on a filesystem, since we are working with id's
126
     * as identifiers and we need to be sure, the parentId to create
127
     * a node under is the same as when the tree was last read.
128
     * but this might be tricky.
129
     */
130
    function add($newValues, $parent = 0, $prevId = 0)
131
    {
132
        if (!$parent) {
133
            $parent = $this->path;
134
        }
135
        # FIXXME do the mapping
136
        if (!@mkdir("$parent/{$newValues['name']}", 0700)) {
137
            return $this->_raiseError(TREE_ERROR_CANNOT_CREATE_FOLDER,
138
                                      $newValues['name'].' under '.$parent,
139
                                      __LINE__
140
                                     );
141
        }
142
        return "$parent/{$newValues['name']}";
143
    }
144
 
145
    // }}}
146
    // {{{ remove()
147
 
148
    function remove($id)
149
    {
150
        if (!@rmdir($id)) {
151
            return $this->_throwError('couldnt remove dir '.$id, __LINE__);
152
        }
153
        return true;
154
    }
155
 
156
    // }}}
157
    // {{{ copy()
158
 
159
    function copy($srcId, $destId)
160
    {
161
        # if(!@copy($srcId, $destId)) this would only be for files :-)
162
        # FIXXME loop through the directory to copy the children too !!!
163
        $dest = $destId.'/'.preg_replace('/^.*\//','',$srcId);
164
        if (is_dir($dest)) {
165
            return $this->_throwError(
166
                    "couldnt copy, $destId already exists in $srcId ", __LINE__
167
                );
168
        }
169
        if (!@mkdir($dest, 0700)) {
170
            return $this->_throwError(
171
                    "couldnt copy dir from $srcId to $destId ", __LINE__
172
                );
173
        }
174
        return true;
175
    }
176
 
177
    // }}}
178
    // {{{ _prepareResults()
179
 
180
    /**
181
     * prepare multiple results
182
     *
183
     * @see        _prepareResult()
184
     * @access     private
185
     * @version    2002/03/03
186
     * @author     Wolfram Kriesing <wolfram@kriesing.de>
187
     */
188
    function _prepareResults($results)
189
    {
190
        $newResults = array();
191
        foreach ($results as $aResult) {
192
            $newResults[] = $this->_prepareResult($aResult);
193
        }
194
        return $newResults;
195
    }
196
 
197
    // }}}
198
    // {{{ _prepareResult()
199
 
200
    /**
201
     * map back the index names to get what is expected
202
     *
203
     * @access     private
204
     * @version    2002/03/03
205
     * @author     Wolfram Kriesing <wolfram@kriesing.de>
206
     */
207
    function _prepareResult($result)
208
    {
209
        $map = $this->getOption('columnNameMaps');
210
        if ($map) {
211
            foreach ($map as $key => $columnName) {
212
                $result[$key] = $result[$columnName];
213
                unset($result[$columnName]);
214
            }
215
        }
216
        return $result;
217
    }
218
 
219
    // }}}
220
}