Blame view

sources/3rdparty/sabre/dav/lib/Sabre/DAV/Tree.php 4.88 KB
03e52840d   Kload   Init
1
  <?php
6d9380f96   Cédric Dupont   Update sources OC...
2
  namespace Sabre\DAV;
03e52840d   Kload   Init
3
4
5
  /**
   * Abstract tree object
   *
6d9380f96   Cédric Dupont   Update sources OC...
6
7
8
   * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
   * @author Evert Pot (http://evertpot.com/)
   * @license http://sabre.io/license/ Modified BSD License
03e52840d   Kload   Init
9
   */
6d9380f96   Cédric Dupont   Update sources OC...
10
  abstract class Tree {
03e52840d   Kload   Init
11
12
13
14
15
16
  
      /**
       * This function must return an INode object for a path
       * If a Path doesn't exist, thrown a Exception_NotFound
       *
       * @param string $path
6d9380f96   Cédric Dupont   Update sources OC...
17
18
       * @throws Exception\NotFound
       * @return INode
03e52840d   Kload   Init
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
       */
      abstract function getNodeForPath($path);
  
      /**
       * 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 {
  
              $this->getNodeForPath($path);
              return true;
6d9380f96   Cédric Dupont   Update sources OC...
37
          } catch (Exception\NotFound $e) {
03e52840d   Kload   Init
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  
              return false;
  
          }
  
      }
  
      /**
       * Copies a file from path to another
       *
       * @param string $sourcePath The source location
       * @param string $destinationPath The full destination path
       * @return void
       */
      public function copy($sourcePath, $destinationPath) {
  
          $sourceNode = $this->getNodeForPath($sourcePath);
  
          // grab the dirname and basename components
6d9380f96   Cédric Dupont   Update sources OC...
57
          list($destinationDir, $destinationName) = URLUtil::splitPath($destinationPath);
03e52840d   Kload   Init
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  
          $destinationParent = $this->getNodeForPath($destinationDir);
          $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
       * @return int
       */
      public function move($sourcePath, $destinationPath) {
6d9380f96   Cédric Dupont   Update sources OC...
74
75
          list($sourceDir, $sourceName) = URLUtil::splitPath($sourcePath);
          list($destinationDir, $destinationName) = URLUtil::splitPath($destinationPath);
03e52840d   Kload   Init
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  
          if ($sourceDir===$destinationDir) {
              $renameable = $this->getNodeForPath($sourcePath);
              $renameable->setName($destinationName);
          } else {
              $this->copy($sourcePath,$destinationPath);
              $this->getNodeForPath($sourcePath)->delete();
          }
          $this->markDirty($sourceDir);
          $this->markDirty($destinationDir);
  
      }
  
      /**
       * Deletes a node from the tree
       *
       * @param string $path
       * @return void
       */
      public function delete($path) {
  
          $node = $this->getNodeForPath($path);
          $node->delete();
6d9380f96   Cédric Dupont   Update sources OC...
99
          list($parent) = URLUtil::splitPath($path);
03e52840d   Kload   Init
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
          $this->markDirty($parent);
  
      }
  
      /**
       * Returns a list of childnodes for a given path.
       *
       * @param string $path
       * @return array
       */
      public function getChildren($path) {
  
          $node = $this->getNodeForPath($path);
          return $node->getChildren();
  
      }
  
      /**
       * 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
       * @return void
       */
      public function markDirty($path) {
  
  
      }
  
      /**
       * copyNode
       *
6d9380f96   Cédric Dupont   Update sources OC...
143
144
       * @param INode $source
       * @param ICollection $destinationParent
03e52840d   Kload   Init
145
146
147
       * @param string $destinationName
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
148
      protected function copyNode(INode $source,ICollection $destinationParent,$destinationName = null) {
03e52840d   Kload   Init
149
150
  
          if (!$destinationName) $destinationName = $source->getName();
6d9380f96   Cédric Dupont   Update sources OC...
151
          if ($source instanceof IFile) {
03e52840d   Kload   Init
152
153
154
155
156
157
158
159
160
161
162
163
  
              $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);
6d9380f96   Cédric Dupont   Update sources OC...
164
          } elseif ($source instanceof ICollection) {
03e52840d   Kload   Init
165
166
167
168
169
170
171
172
173
174
175
  
              $destinationParent->createDirectory($destinationName);
  
              $destination = $destinationParent->getChild($destinationName);
              foreach($source->getChildren() as $child) {
  
                  $this->copyNode($child,$destination);
  
              }
  
          }
6d9380f96   Cédric Dupont   Update sources OC...
176
          if ($source instanceof IProperties && $destination instanceof IProperties) {
03e52840d   Kload   Init
177
178
179
180
181
182
183
184
185
  
              $props = $source->getProperties(array());
              $destination->updateProperties($props);
  
          }
  
      }
  
  }