1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
<?php
declare(strict_types=1);
namespace Sabre\DAV;
use Sabre\Uri;
/**
* The tree object is responsible for basic tree operations.
*
* It allows for fetching nodes by path, facilitates deleting, copying and
* moving.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class Tree
{
/**
* The root node.
*
* @var ICollection
*/
protected $rootNode;
/**
* This is the node cache. Accessed nodes are stored here.
* Arrays keys are path names, values are the actual nodes.
*
* @var array
*/
protected $cache = [];
/**
* Creates the object.
*
* This method expects the rootObject to be passed as a parameter
*/
public function __construct(ICollection $rootNode)
{
$this->rootNode = $rootNode;
}
/**
* Returns the INode object for the requested path.
*
* @param string $path
*
* @return INode
*/
public function getNodeForPath($path)
{
$path = trim($path, '/');
if (isset($this->cache[$path])) {
return $this->cache[$path];
}
// Is it the root node?
if (!strlen($path)) {
return $this->rootNode;
}
// Attempting to fetch its parent
list($parentName, $baseName) = Uri\split($path);
// If there was no parent, we must simply ask it from the root node.
if ('' === $parentName) {
$node = $this->rootNode->getChild($baseName);
} else {
// Otherwise, we recursively grab the parent and ask him/her.
$parent = $this->getNodeForPath($parentName);
if (!($parent instanceof ICollection)) {
throw new Exception\NotFound('Could not find node at path: '.$path);
}
$node = $parent->getChild($baseName);
}
$this->cache[$path] = $node;
return $node;
}
/**
* This function allows you to check if a node exists.
*
* Implementors of this class should override this method to make
* it cheaper.
*
* @param string $path
*
* @return bool
*/
public function nodeExists($path)
{
try {
// The root always exists
if ('' === $path) {
return true;
}
list($parent, $base) = Uri\split($path);
$parentNode = $this->getNodeForPath($parent);
if (!$parentNode instanceof ICollection) {
return false;
}
return $parentNode->childExists($base);
} catch (Exception\NotFound $e) {
return false;
}
}
/**
* Copies a file from path to another.
*
* @param string $sourcePath The source location
* @param string $destinationPath The full destination path
*/
public function copy($sourcePath, $destinationPath)
{
$sourceNode = $this->getNodeForPath($sourcePath);
// grab the dirname and basename components
list($destinationDir, $destinationName) = Uri\split($destinationPath);
$destinationParent = $this->getNodeForPath($destinationDir);
// Check if the target can handle the copy itself. If not, we do it ourselves.
if (!$destinationParent instanceof ICopyTarget || !$destinationParent->copyInto($destinationName, $sourcePath, $sourceNode)) {
$this->copyNode($sourceNode, $destinationParent, $destinationName);
}
$this->markDirty($destinationDir);
}
/**
* Moves a file from one location to another.
*
* @param string $sourcePath The path to the file which should be moved
* @param string $destinationPath The full destination path, so not just the destination parent node
*/
public function move($sourcePath, $destinationPath)
{
list($sourceDir) = Uri\split($sourcePath);
list($destinationDir, $destinationName) = Uri\split($destinationPath);
if ($sourceDir === $destinationDir) {
// If this is a 'local' rename, it means we can just trigger a rename.
$sourceNode = $this->getNodeForPath($sourcePath);
$sourceNode->setName($destinationName);
} else {
$newParentNode = $this->getNodeForPath($destinationDir);
$moveSuccess = false;
if ($newParentNode instanceof IMoveTarget) {
// The target collection may be able to handle the move
$sourceNode = $this->getNodeForPath($sourcePath);
$moveSuccess = $newParentNode->moveInto($destinationName, $sourcePath, $sourceNode);
}
if (!$moveSuccess) {
$this->copy($sourcePath, $destinationPath);
$this->getNodeForPath($sourcePath)->delete();
}
}
$this->markDirty($sourceDir);
$this->markDirty($destinationDir);
}
/**
* Deletes a node from the tree.
*
* @param string $path
*/
public function delete($path)
{
$node = $this->getNodeForPath($path);
$node->delete();
list($parent) = Uri\split($path);
$this->markDirty($parent);
}
/**
* Returns a list of childnodes for a given path.
*
* @param string $path
*
* @return \Traversable
*/
public function getChildren($path)
{
$node = $this->getNodeForPath($path);
$basePath = trim($path, '/');
if ('' !== $basePath) {
$basePath .= '/';
}
foreach ($node->getChildren() as $child) {
$this->cache[$basePath.$child->getName()] = $child;
yield $child;
}
}
/**
* This method is called with every tree update.
*
* Examples of tree updates are:
* * node deletions
* * node creations
* * copy
* * move
* * renaming nodes
*
* If Tree classes implement a form of caching, this will allow
* them to make sure caches will be expired.
*
* If a path is passed, it is assumed that the entire subtree is dirty
*
* @param string $path
*/
public function markDirty($path)
{
// We don't care enough about sub-paths
// flushing the entire cache
$path = trim($path, '/');
foreach ($this->cache as $nodePath => $node) {
if ('' === $path || $nodePath == $path || 0 === strpos((string) $nodePath, $path.'/')) {
unset($this->cache[$nodePath]);
}
}
}
/**
* This method tells the tree system to pre-fetch and cache a list of
* children of a single parent.
*
* There are a bunch of operations in the WebDAV stack that request many
* children (based on uris), and sometimes fetching many at once can
* optimize this.
*
* This method returns an array with the found nodes. It's keys are the
* original paths. The result may be out of order.
*
* @param array $paths list of nodes that must be fetched
*
* @return array
*/
public function getMultipleNodes($paths)
{
// Finding common parents
$parents = [];
foreach ($paths as $path) {
list($parent, $node) = Uri\split($path);
if (!isset($parents[$parent])) {
$parents[$parent] = [$node];
} else {
$parents[$parent][] = $node;
}
}
$result = [];
foreach ($parents as $parent => $children) {
$parentNode = $this->getNodeForPath($parent);
if ($parentNode instanceof IMultiGet) {
foreach ($parentNode->getMultipleChildren($children) as $childNode) {
$fullPath = $parent.'/'.$childNode->getName();
$result[$fullPath] = $childNode;
$this->cache[$fullPath] = $childNode;
}
} else {
foreach ($children as $child) {
$fullPath = $parent.'/'.$child;
$result[$fullPath] = $this->getNodeForPath($fullPath);
}
}
}
return $result;
}
/**
* copyNode.
*
* @param string $destinationName
*/
protected function copyNode(INode $source, ICollection $destinationParent, $destinationName = null)
{
if ('' === (string) $destinationName) {
$destinationName = $source->getName();
}
$destination = null;
if ($source instanceof IFile) {
$data = $source->get();
// If the body was a string, we need to convert it to a stream
if (is_string($data)) {
$stream = fopen('php://temp', 'r+');
fwrite($stream, $data);
rewind($stream);
$data = $stream;
}
$destinationParent->createFile($destinationName, $data);
$destination = $destinationParent->getChild($destinationName);
} elseif ($source instanceof ICollection) {
$destinationParent->createDirectory($destinationName);
$destination = $destinationParent->getChild($destinationName);
foreach ($source->getChildren() as $child) {
$this->copyNode($child, $destination);
}
}
if ($source instanceof IProperties && $destination instanceof IProperties) {
$props = $source->getProperties([]);
$propPatch = new PropPatch($props);
$destination->propPatch($propPatch);
$propPatch->commit();
}
}
}
|