8 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
9 * @author Evert Pot (http://www.rooftopsolutions.nl/)
10 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
12 abstract class Sabre_DAV_Tree {
15 * This function must return an INode object for a path
16 * If a Path doesn't exist, thrown a Exception_NotFound
19 * @throws Sabre_DAV_Exception_NotFound
20 * @return Sabre_DAV_INode
22 abstract function getNodeForPath($path);
25 * This function allows you to check if a node exists.
27 * Implementors of this class should override this method to make
33 public function nodeExists($path) {
37 $this->getNodeForPath($path);
40 } catch (Sabre_DAV_Exception_NotFound $e) {
49 * Copies a file from path to another
51 * @param string $sourcePath The source location
52 * @param string $destinationPath The full destination path
55 public function copy($sourcePath, $destinationPath) {
57 $sourceNode = $this->getNodeForPath($sourcePath);
59 // grab the dirname and basename components
60 list($destinationDir, $destinationName) = Sabre_DAV_URLUtil::splitPath($destinationPath);
62 $destinationParent = $this->getNodeForPath($destinationDir);
63 $this->copyNode($sourceNode,$destinationParent,$destinationName);
65 $this->markDirty($destinationDir);
70 * Moves a file from one location to another
72 * @param string $sourcePath The path to the file which should be moved
73 * @param string $destinationPath The full destination path, so not just the destination parent node
76 public function move($sourcePath, $destinationPath) {
78 list($sourceDir, $sourceName) = Sabre_DAV_URLUtil::splitPath($sourcePath);
79 list($destinationDir, $destinationName) = Sabre_DAV_URLUtil::splitPath($destinationPath);
81 if ($sourceDir===$destinationDir) {
82 $renameable = $this->getNodeForPath($sourcePath);
83 $renameable->setName($destinationName);
85 $this->copy($sourcePath,$destinationPath);
86 $this->getNodeForPath($sourcePath)->delete();
88 $this->markDirty($sourceDir);
89 $this->markDirty($destinationDir);
94 * Deletes a node from the tree
99 public function delete($path) {
101 $node = $this->getNodeForPath($path);
104 list($parent) = Sabre_DAV_URLUtil::splitPath($path);
105 $this->markDirty($parent);
110 * Returns a list of childnodes for a given path.
112 * @param string $path
115 public function getChildren($path) {
117 $node = $this->getNodeForPath($path);
118 return $node->getChildren();
123 * This method is called with every tree update
125 * Examples of tree updates are:
132 * If Tree classes implement a form of caching, this will allow
133 * them to make sure caches will be expired.
135 * If a path is passed, it is assumed that the entire subtree is dirty
137 * @param string $path
140 public function markDirty($path) {
148 * @param Sabre_DAV_INode $source
149 * @param Sabre_DAV_ICollection $destinationParent
150 * @param string $destinationName
153 protected function copyNode(Sabre_DAV_INode $source,Sabre_DAV_ICollection $destinationParent,$destinationName = null) {
155 if (!$destinationName) $destinationName = $source->getName();
157 if ($source instanceof Sabre_DAV_IFile) {
159 $data = $source->get();
161 // If the body was a string, we need to convert it to a stream
162 if (is_string($data)) {
163 $stream = fopen('php://temp','r+');
164 fwrite($stream,$data);
168 $destinationParent->createFile($destinationName,$data);
169 $destination = $destinationParent->getChild($destinationName);
171 } elseif ($source instanceof Sabre_DAV_ICollection) {
173 $destinationParent->createDirectory($destinationName);
175 $destination = $destinationParent->getChild($destinationName);
176 foreach($source->getChildren() as $child) {
178 $this->copyNode($child,$destination);
183 if ($source instanceof Sabre_DAV_IProperties && $destination instanceof Sabre_DAV_IProperties) {
185 $props = $source->getProperties(array());
186 $destination->updateProperties($props);