Filesystem.php 8.2 KB

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