Passed
Push — master ( 402930...38adda )
by Roeland
37:16 queued 16:01
created
apps/dav/lib/Connector/Sabre/CustomPropertiesBackend.php 1 patch
Indentation   +310 added lines, -310 removed lines patch added patch discarded remove patch
@@ -36,315 +36,315 @@
 block discarded – undo
36 36
 
37 37
 class CustomPropertiesBackend implements BackendInterface {
38 38
 
39
-	/**
40
-	 * Ignored properties
41
-	 *
42
-	 * @var array
43
-	 */
44
-	private $ignoredProperties = array(
45
-		'{DAV:}getcontentlength',
46
-		'{DAV:}getcontenttype',
47
-		'{DAV:}getetag',
48
-		'{DAV:}quota-used-bytes',
49
-		'{DAV:}quota-available-bytes',
50
-		'{http://owncloud.org/ns}permissions',
51
-		'{http://owncloud.org/ns}downloadURL',
52
-		'{http://owncloud.org/ns}dDC',
53
-		'{http://owncloud.org/ns}size',
54
-		'{http://nextcloud.org/ns}is-encrypted',
55
-	);
56
-
57
-	/**
58
-	 * @var Tree
59
-	 */
60
-	private $tree;
61
-
62
-	/**
63
-	 * @var IDBConnection
64
-	 */
65
-	private $connection;
66
-
67
-	/**
68
-	 * @var IUser
69
-	 */
70
-	private $user;
71
-
72
-	/**
73
-	 * Properties cache
74
-	 *
75
-	 * @var array
76
-	 */
77
-	private $cache = [];
78
-
79
-	/**
80
-	 * @param Tree $tree node tree
81
-	 * @param IDBConnection $connection database connection
82
-	 * @param IUser $user owner of the tree and properties
83
-	 */
84
-	public function __construct(
85
-		Tree $tree,
86
-		IDBConnection $connection,
87
-		IUser $user) {
88
-		$this->tree = $tree;
89
-		$this->connection = $connection;
90
-		$this->user = $user->getUID();
91
-	}
92
-
93
-	/**
94
-	 * Fetches properties for a path.
95
-	 *
96
-	 * @param string $path
97
-	 * @param PropFind $propFind
98
-	 * @return void
99
-	 */
100
-	public function propFind($path, PropFind $propFind) {
101
-		try {
102
-			$node = $this->tree->getNodeForPath($path);
103
-			if (!($node instanceof Node)) {
104
-				return;
105
-			}
106
-		} catch (ServiceUnavailable $e) {
107
-			// might happen for unavailable mount points, skip
108
-			return;
109
-		} catch (NotFound $e) {
110
-			// in some rare (buggy) cases the node might not be found,
111
-			// we catch the exception to prevent breaking the whole list with a 404
112
-			// (soft fail)
113
-			\OC::$server->getLogger()->warning(
114
-				'Could not get node for path: \"' . $path . '\" : ' . $e->getMessage(),
115
-				array('app' => 'files')
116
-			);
117
-			return;
118
-		}
119
-
120
-		$requestedProps = $propFind->get404Properties();
121
-
122
-		// these might appear
123
-		$requestedProps = array_diff(
124
-			$requestedProps,
125
-			$this->ignoredProperties
126
-		);
127
-
128
-		if (empty($requestedProps)) {
129
-			return;
130
-		}
131
-
132
-		$props = $this->getProperties($node, $requestedProps);
133
-		foreach ($props as $propName => $propValue) {
134
-			$propFind->set($propName, $propValue);
135
-		}
136
-	}
137
-
138
-	/**
139
-	 * Updates properties for a path
140
-	 *
141
-	 * @param string $path
142
-	 * @param PropPatch $propPatch
143
-	 *
144
-	 * @return void
145
-	 */
146
-	public function propPatch($path, PropPatch $propPatch) {
147
-		$node = $this->tree->getNodeForPath($path);
148
-		if (!($node instanceof Node)) {
149
-			return;
150
-		}
151
-
152
-		$propPatch->handleRemaining(function($changedProps) use ($node) {
153
-			return $this->updateProperties($node, $changedProps);
154
-		});
155
-	}
156
-
157
-	/**
158
-	 * This method is called after a node is deleted.
159
-	 *
160
-	 * @param string $path path of node for which to delete properties
161
-	 */
162
-	public function delete($path) {
163
-		$statement = $this->connection->prepare(
164
-			'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
165
-		);
166
-		$statement->execute(array($this->user, '/' . $path));
167
-		$statement->closeCursor();
168
-
169
-		unset($this->cache[$path]);
170
-	}
171
-
172
-	/**
173
-	 * This method is called after a successful MOVE
174
-	 *
175
-	 * @param string $source
176
-	 * @param string $destination
177
-	 *
178
-	 * @return void
179
-	 */
180
-	public function move($source, $destination) {
181
-		$statement = $this->connection->prepare(
182
-			'UPDATE `*PREFIX*properties` SET `propertypath` = ?' .
183
-			' WHERE `userid` = ? AND `propertypath` = ?'
184
-		);
185
-		$statement->execute(array('/' . $destination, $this->user, '/' . $source));
186
-		$statement->closeCursor();
187
-	}
188
-
189
-	/**
190
-	 * Returns a list of properties for this nodes.;
191
-	 * @param Node $node
192
-	 * @param array $requestedProperties requested properties or empty array for "all"
193
-	 * @return array
194
-	 * @note The properties list is a list of propertynames the client
195
-	 * requested, encoded as xmlnamespace#tagName, for example:
196
-	 * http://www.example.org/namespace#author If the array is empty, all
197
-	 * properties should be returned
198
-	 */
199
-	private function getProperties(Node $node, array $requestedProperties) {
200
-		$path = $node->getPath();
201
-		if (isset($this->cache[$path])) {
202
-			return $this->cache[$path];
203
-		}
204
-
205
-		// TODO: chunking if more than 1000 properties
206
-		$sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?';
207
-
208
-		$whereValues = array($this->user, $path);
209
-		$whereTypes = array(null, null);
210
-
211
-		if (!empty($requestedProperties)) {
212
-			// request only a subset
213
-			$sql .= ' AND `propertyname` in (?)';
214
-			$whereValues[] = $requestedProperties;
215
-			$whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
216
-		}
217
-
218
-		$result = $this->connection->executeQuery(
219
-			$sql,
220
-			$whereValues,
221
-			$whereTypes
222
-		);
223
-
224
-		$props = [];
225
-		while ($row = $result->fetch()) {
226
-			$props[$row['propertyname']] = $row['propertyvalue'];
227
-		}
228
-
229
-		$result->closeCursor();
230
-
231
-		$this->cache[$path] = $props;
232
-		return $props;
233
-	}
234
-
235
-	/**
236
-	 * Update properties
237
-	 *
238
-	 * @param Node $node node for which to update properties
239
-	 * @param array $properties array of properties to update
240
-	 *
241
-	 * @return bool
242
-	 */
243
-	private function updateProperties($node, $properties) {
244
-		$path = $node->getPath();
245
-
246
-		$deleteStatement = 'DELETE FROM `*PREFIX*properties`' .
247
-			' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
248
-
249
-		$insertStatement = 'INSERT INTO `*PREFIX*properties`' .
250
-			' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)';
251
-
252
-		$updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' .
253
-			' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
254
-
255
-		// TODO: use "insert or update" strategy ?
256
-		$existing = $this->getProperties($node, array());
257
-		$this->connection->beginTransaction();
258
-		foreach ($properties as $propertyName => $propertyValue) {
259
-			// If it was null, we need to delete the property
260
-			if (is_null($propertyValue)) {
261
-				if (array_key_exists($propertyName, $existing)) {
262
-					$this->connection->executeUpdate($deleteStatement,
263
-						array(
264
-							$this->user,
265
-							$path,
266
-							$propertyName
267
-						)
268
-					);
269
-				}
270
-			} else {
271
-				if (!array_key_exists($propertyName, $existing)) {
272
-					$this->connection->executeUpdate($insertStatement,
273
-						array(
274
-							$this->user,
275
-							$path,
276
-							$propertyName,
277
-							$propertyValue
278
-						)
279
-					);
280
-				} else {
281
-					$this->connection->executeUpdate($updateStatement,
282
-						array(
283
-							$propertyValue,
284
-							$this->user,
285
-							$path,
286
-							$propertyName
287
-						)
288
-					);
289
-				}
290
-			}
291
-		}
292
-
293
-		$this->connection->commit();
294
-		unset($this->cache[$path]);
295
-
296
-		return true;
297
-	}
298
-
299
-	/**
300
-	 * Bulk load properties for directory children
301
-	 *
302
-	 * @param Directory $node
303
-	 * @param array $requestedProperties requested properties
304
-	 *
305
-	 * @return void
306
-	 */
307
-	private function loadChildrenProperties(Directory $node, $requestedProperties) {
308
-		$path = $node->getPath();
309
-		if (isset($this->cache[$path])) {
310
-			// we already loaded them at some point
311
-			return;
312
-		}
313
-
314
-		$childNodes = $node->getChildren();
315
-		// pre-fill cache
316
-		foreach ($childNodes as $childNode) {
317
-			$this->cache[$childNode->getPath()] = [];
318
-		}
319
-
320
-		$sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` LIKE ?';
321
-		$sql .= ' AND `propertyname` in (?) ORDER BY `propertypath`, `propertyname`';
322
-
323
-		$result = $this->connection->executeQuery(
324
-			$sql,
325
-			array($this->user, $this->connection->escapeLikeParameter(rtrim($path, '/')) . '/%', $requestedProperties),
326
-			array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
327
-		);
328
-
329
-		$oldPath = null;
330
-		$props = [];
331
-		while ($row = $result->fetch()) {
332
-			$path = $row['propertypath'];
333
-			if ($oldPath !== $path) {
334
-				// save previously gathered props
335
-				$this->cache[$oldPath] = $props;
336
-				$oldPath = $path;
337
-				// prepare props for next path
338
-				$props = [];
339
-			}
340
-			$props[$row['propertyname']] = $row['propertyvalue'];
341
-		}
342
-		if (!is_null($oldPath)) {
343
-			// save props from last run
344
-			$this->cache[$oldPath] = $props;
345
-		}
346
-
347
-		$result->closeCursor();
348
-	}
39
+    /**
40
+     * Ignored properties
41
+     *
42
+     * @var array
43
+     */
44
+    private $ignoredProperties = array(
45
+        '{DAV:}getcontentlength',
46
+        '{DAV:}getcontenttype',
47
+        '{DAV:}getetag',
48
+        '{DAV:}quota-used-bytes',
49
+        '{DAV:}quota-available-bytes',
50
+        '{http://owncloud.org/ns}permissions',
51
+        '{http://owncloud.org/ns}downloadURL',
52
+        '{http://owncloud.org/ns}dDC',
53
+        '{http://owncloud.org/ns}size',
54
+        '{http://nextcloud.org/ns}is-encrypted',
55
+    );
56
+
57
+    /**
58
+     * @var Tree
59
+     */
60
+    private $tree;
61
+
62
+    /**
63
+     * @var IDBConnection
64
+     */
65
+    private $connection;
66
+
67
+    /**
68
+     * @var IUser
69
+     */
70
+    private $user;
71
+
72
+    /**
73
+     * Properties cache
74
+     *
75
+     * @var array
76
+     */
77
+    private $cache = [];
78
+
79
+    /**
80
+     * @param Tree $tree node tree
81
+     * @param IDBConnection $connection database connection
82
+     * @param IUser $user owner of the tree and properties
83
+     */
84
+    public function __construct(
85
+        Tree $tree,
86
+        IDBConnection $connection,
87
+        IUser $user) {
88
+        $this->tree = $tree;
89
+        $this->connection = $connection;
90
+        $this->user = $user->getUID();
91
+    }
92
+
93
+    /**
94
+     * Fetches properties for a path.
95
+     *
96
+     * @param string $path
97
+     * @param PropFind $propFind
98
+     * @return void
99
+     */
100
+    public function propFind($path, PropFind $propFind) {
101
+        try {
102
+            $node = $this->tree->getNodeForPath($path);
103
+            if (!($node instanceof Node)) {
104
+                return;
105
+            }
106
+        } catch (ServiceUnavailable $e) {
107
+            // might happen for unavailable mount points, skip
108
+            return;
109
+        } catch (NotFound $e) {
110
+            // in some rare (buggy) cases the node might not be found,
111
+            // we catch the exception to prevent breaking the whole list with a 404
112
+            // (soft fail)
113
+            \OC::$server->getLogger()->warning(
114
+                'Could not get node for path: \"' . $path . '\" : ' . $e->getMessage(),
115
+                array('app' => 'files')
116
+            );
117
+            return;
118
+        }
119
+
120
+        $requestedProps = $propFind->get404Properties();
121
+
122
+        // these might appear
123
+        $requestedProps = array_diff(
124
+            $requestedProps,
125
+            $this->ignoredProperties
126
+        );
127
+
128
+        if (empty($requestedProps)) {
129
+            return;
130
+        }
131
+
132
+        $props = $this->getProperties($node, $requestedProps);
133
+        foreach ($props as $propName => $propValue) {
134
+            $propFind->set($propName, $propValue);
135
+        }
136
+    }
137
+
138
+    /**
139
+     * Updates properties for a path
140
+     *
141
+     * @param string $path
142
+     * @param PropPatch $propPatch
143
+     *
144
+     * @return void
145
+     */
146
+    public function propPatch($path, PropPatch $propPatch) {
147
+        $node = $this->tree->getNodeForPath($path);
148
+        if (!($node instanceof Node)) {
149
+            return;
150
+        }
151
+
152
+        $propPatch->handleRemaining(function($changedProps) use ($node) {
153
+            return $this->updateProperties($node, $changedProps);
154
+        });
155
+    }
156
+
157
+    /**
158
+     * This method is called after a node is deleted.
159
+     *
160
+     * @param string $path path of node for which to delete properties
161
+     */
162
+    public function delete($path) {
163
+        $statement = $this->connection->prepare(
164
+            'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
165
+        );
166
+        $statement->execute(array($this->user, '/' . $path));
167
+        $statement->closeCursor();
168
+
169
+        unset($this->cache[$path]);
170
+    }
171
+
172
+    /**
173
+     * This method is called after a successful MOVE
174
+     *
175
+     * @param string $source
176
+     * @param string $destination
177
+     *
178
+     * @return void
179
+     */
180
+    public function move($source, $destination) {
181
+        $statement = $this->connection->prepare(
182
+            'UPDATE `*PREFIX*properties` SET `propertypath` = ?' .
183
+            ' WHERE `userid` = ? AND `propertypath` = ?'
184
+        );
185
+        $statement->execute(array('/' . $destination, $this->user, '/' . $source));
186
+        $statement->closeCursor();
187
+    }
188
+
189
+    /**
190
+     * Returns a list of properties for this nodes.;
191
+     * @param Node $node
192
+     * @param array $requestedProperties requested properties or empty array for "all"
193
+     * @return array
194
+     * @note The properties list is a list of propertynames the client
195
+     * requested, encoded as xmlnamespace#tagName, for example:
196
+     * http://www.example.org/namespace#author If the array is empty, all
197
+     * properties should be returned
198
+     */
199
+    private function getProperties(Node $node, array $requestedProperties) {
200
+        $path = $node->getPath();
201
+        if (isset($this->cache[$path])) {
202
+            return $this->cache[$path];
203
+        }
204
+
205
+        // TODO: chunking if more than 1000 properties
206
+        $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?';
207
+
208
+        $whereValues = array($this->user, $path);
209
+        $whereTypes = array(null, null);
210
+
211
+        if (!empty($requestedProperties)) {
212
+            // request only a subset
213
+            $sql .= ' AND `propertyname` in (?)';
214
+            $whereValues[] = $requestedProperties;
215
+            $whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
216
+        }
217
+
218
+        $result = $this->connection->executeQuery(
219
+            $sql,
220
+            $whereValues,
221
+            $whereTypes
222
+        );
223
+
224
+        $props = [];
225
+        while ($row = $result->fetch()) {
226
+            $props[$row['propertyname']] = $row['propertyvalue'];
227
+        }
228
+
229
+        $result->closeCursor();
230
+
231
+        $this->cache[$path] = $props;
232
+        return $props;
233
+    }
234
+
235
+    /**
236
+     * Update properties
237
+     *
238
+     * @param Node $node node for which to update properties
239
+     * @param array $properties array of properties to update
240
+     *
241
+     * @return bool
242
+     */
243
+    private function updateProperties($node, $properties) {
244
+        $path = $node->getPath();
245
+
246
+        $deleteStatement = 'DELETE FROM `*PREFIX*properties`' .
247
+            ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
248
+
249
+        $insertStatement = 'INSERT INTO `*PREFIX*properties`' .
250
+            ' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)';
251
+
252
+        $updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' .
253
+            ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
254
+
255
+        // TODO: use "insert or update" strategy ?
256
+        $existing = $this->getProperties($node, array());
257
+        $this->connection->beginTransaction();
258
+        foreach ($properties as $propertyName => $propertyValue) {
259
+            // If it was null, we need to delete the property
260
+            if (is_null($propertyValue)) {
261
+                if (array_key_exists($propertyName, $existing)) {
262
+                    $this->connection->executeUpdate($deleteStatement,
263
+                        array(
264
+                            $this->user,
265
+                            $path,
266
+                            $propertyName
267
+                        )
268
+                    );
269
+                }
270
+            } else {
271
+                if (!array_key_exists($propertyName, $existing)) {
272
+                    $this->connection->executeUpdate($insertStatement,
273
+                        array(
274
+                            $this->user,
275
+                            $path,
276
+                            $propertyName,
277
+                            $propertyValue
278
+                        )
279
+                    );
280
+                } else {
281
+                    $this->connection->executeUpdate($updateStatement,
282
+                        array(
283
+                            $propertyValue,
284
+                            $this->user,
285
+                            $path,
286
+                            $propertyName
287
+                        )
288
+                    );
289
+                }
290
+            }
291
+        }
292
+
293
+        $this->connection->commit();
294
+        unset($this->cache[$path]);
295
+
296
+        return true;
297
+    }
298
+
299
+    /**
300
+     * Bulk load properties for directory children
301
+     *
302
+     * @param Directory $node
303
+     * @param array $requestedProperties requested properties
304
+     *
305
+     * @return void
306
+     */
307
+    private function loadChildrenProperties(Directory $node, $requestedProperties) {
308
+        $path = $node->getPath();
309
+        if (isset($this->cache[$path])) {
310
+            // we already loaded them at some point
311
+            return;
312
+        }
313
+
314
+        $childNodes = $node->getChildren();
315
+        // pre-fill cache
316
+        foreach ($childNodes as $childNode) {
317
+            $this->cache[$childNode->getPath()] = [];
318
+        }
319
+
320
+        $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` LIKE ?';
321
+        $sql .= ' AND `propertyname` in (?) ORDER BY `propertypath`, `propertyname`';
322
+
323
+        $result = $this->connection->executeQuery(
324
+            $sql,
325
+            array($this->user, $this->connection->escapeLikeParameter(rtrim($path, '/')) . '/%', $requestedProperties),
326
+            array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
327
+        );
328
+
329
+        $oldPath = null;
330
+        $props = [];
331
+        while ($row = $result->fetch()) {
332
+            $path = $row['propertypath'];
333
+            if ($oldPath !== $path) {
334
+                // save previously gathered props
335
+                $this->cache[$oldPath] = $props;
336
+                $oldPath = $path;
337
+                // prepare props for next path
338
+                $props = [];
339
+            }
340
+            $props[$row['propertyname']] = $row['propertyvalue'];
341
+        }
342
+        if (!is_null($oldPath)) {
343
+            // save props from last run
344
+            $this->cache[$oldPath] = $props;
345
+        }
346
+
347
+        $result->closeCursor();
348
+    }
349 349
 
350 350
 }
Please login to merge, or discard this patch.