/[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.2 - (show annotations)
Wed Jul 7 02:49:21 2004 UTC (20 years, 1 month ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -1 lines
updated to Tree-0.2.4

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

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