/[cvs]/nfo/php/libs/net.php.pear/Tree/Memory/Filesystem.php
ViewVC logotype

Contents of /nfo/php/libs/net.php.pear/Tree/Memory/Filesystem.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Thu Feb 27 16:49:59 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
+ initial commit, from PEAR

1 <?php
2 //
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.3 2003/01/04 11:56:28 mj Exp
20 // $Id: Filesystem.php,v 1.3 2003/01/04 11:56:28 mj Exp $
21
22 require_once('Tree/Error.php');
23
24 /**
25 * the Filesystem interface to the tree class
26 * this is a bit different, as id we use the entire path, since we know
27 * this is unique in a filesystem and an integer id could only be created
28 * virtually, it doesnt have a tight connection to the actual directory
29 * i.e. using 'add' with ids could fail since we dont know if we get the same
30 * id when we call 'add' with a parentId to create a new folder under since our id would be made up.
31 * so we use the complete path as id, which takes up a lot of memory, i know
32 * but since php is typeless its possible and it makes life easier when
33 * we want to create another dir, since we get the dir as parentId passed to the
34 * 'add' method, whcih makes it easy to create a new dir there :-)
35 * i also thought about hashing the path name but then the add method is not that
36 * 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( 'order' =>'', // which column to order by when reading the data from the DB, this sorts the data even inside every level
51 // the column-name maps are used for the "as" in the select queries
52 // so you can use any column name in the table and "map" it to the name that shall be used in the
53 // internal array, that is built, see the examples (in comments)
54 'columnNameMaps'=>array(
55 /* 'id' => 'tree_id', // use "tree_id" as "id"
56 'parentId' => 'parent_id',
57 'prevId' => 'previous_id',
58 'name' => 'nodeName'
59 */
60 ),
61 );
62
63 /**
64 * set up this object
65 *
66 * @version 2002/08/23
67 * @access public
68 * @author Wolfram Kriesing <wolfram@kriesing.de>
69 * @param string $dsn the path on the filesystem
70 * @param array $options additional options you can set
71 */
72 function Tree_Memory_Filesystem( $path , $options=array() )
73 {
74 $this->_path = $path;
75 $this->_options = $options; // not in use currently
76 /* $this->Tree_OptionsDB( $dsn , $options ); // instanciate DB
77 if( is_string($options) ) // just to be backward compatible, or to make the second paramter shorter
78 {
79 $this->setOption( 'order' , $options );
80 }
81
82 $this->table = $this->getOption('table');
83 */
84 } // end of function
85
86 /**
87 * retreive all the navigation data from the db and call build to build the
88 * tree in the array data and structure
89 *
90 * @version 2001/11/20
91 * @access public
92 * @author Wolfram Kriesing <wolfram@kriesing.de>
93 * @return boolean true on success
94 */
95 function setup()
96 {
97 unset($this->data); // unset the data to be sure to get the real data again, no old data
98 if( is_dir($this->_path) )
99 {
100 $this->data[$this->_path] = array('id'=>$this->_path,'name'=>$this->_path,'parentId'=>0);
101 $this->_setup($this->_path,$this->_path);
102 }
103
104 return $this->data;
105 }
106
107 function _setup( $path , $parentId=0 )
108 {
109 if ($handle = opendir($path))
110 {
111 while (false !== ($file = readdir($handle)))
112 {
113 if( $file != '.' && $file != '..' && is_dir("$path/$file"))
114 {
115 #$id = sizeof($this->data);
116 $this->data[] = array('id'=>"$path/$file",'name'=>$file,'parentId'=>$parentId);
117 $this->_setup( "$path/$file" , "$path/$file" );
118 }
119 }
120 closedir($handle);
121 }
122
123 }
124
125
126 /**
127 * this is tricky on a filesystem, since we are working with id's
128 * as identifiers and we need to be sure, the parentId to create a node under
129 * is the same as when the tree was last read, but this might be tricky
130 *
131 */
132 function add( $newValues , $parent=0 , $prevId=0 )
133 {
134 if( !$parent )
135 $parent = $this->path;
136
137 # FIXXME do the mapping
138 if( !@mkdir( "$parent/{$newValues['name']}" , 0700 ) )
139 return $this->_throwError('couldnt create dir '.$newValues['name'].' under '.$parent,__LINE__);
140 return "$parent/{$newValues['name']}";
141 }
142
143 function remove( $id )
144 {
145 if( !@rmdir( $id ) )
146 return $this->_throwError('couldnt remove dir '.$id,__LINE__);
147 return true;
148 }
149
150 function copy( $srcId , $destId )
151 {
152 # if( !@copy( $srcId , $destId ) ) this would only be for files :-)
153 # FIXXME loop through the directory to copy the children too !!!
154
155 $dest = $destId.'/'.preg_replace('/^.*\//','',$srcId);
156
157 if( is_dir( $dest ) )
158 return $this->_throwError("couldnt copy, $destId already exists in $srcId " , __LINE__ );
159
160 if( !@mkdir( $dest , 0700 ) )
161 return $this->_throwError("couldnt copy dir from $srcId to $destId " , __LINE__ );
162 return true;
163 }
164
165 /**
166 *
167 *
168 * @access private
169 * @version 2002/03/02
170 * @author Wolfram Kriesing <wolfram@kriesing.de>
171 * @param
172 * @return
173 */
174 function _throwError( $msg , $line , $mode=null )
175 {
176 return new Tree_Error( $msg , $line , __FILE__ , $mode , $this->db->last_query );
177 }
178
179 /**
180 * prepare multiple results
181 *
182 * @see _prepareResult()
183 * @access private
184 * @version 2002/03/03
185 * @author Wolfram Kriesing <wolfram@kriesing.de>
186 * @param
187 * @return
188 */
189 function _prepareResults( $results )
190 {
191 $newResults = array();
192 foreach( $results as $aResult )
193 $newResults[] = $this->_prepareResult($aResult);
194 return $newResults;
195 }
196
197 /**
198 * map back the index names to get what is expected
199 *
200 * @access private
201 * @version 2002/03/03
202 * @author Wolfram Kriesing <wolfram@kriesing.de>
203 * @param
204 * @return
205 */
206 function _prepareResult( $result )
207 {
208 $map = $this->getOption('columnNameMaps');
209
210 if( $map )
211 foreach( $map as $key=>$columnName )
212 {
213 $result[$key] = $result[$columnName];
214 unset($result[$columnName]);
215 }
216 return $result;
217 }
218
219
220 } // end of class
221 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed