@@ -28,40 +28,40 @@ |
||
28 | 28 | |
29 | 29 | class HomeObjectStoreStorage extends ObjectStoreStorage implements \OCP\Files\IHomeStorage { |
30 | 30 | |
31 | - /** |
|
32 | - * The home user storage requires a user object to create a unique storage id |
|
33 | - * @param array $params |
|
34 | - */ |
|
35 | - public function __construct($params) { |
|
36 | - if (! isset($params['user']) || ! $params['user'] instanceof User) { |
|
37 | - throw new \Exception('missing user object in parameters'); |
|
38 | - } |
|
39 | - $this->user = $params['user']; |
|
40 | - parent::__construct($params); |
|
41 | - } |
|
31 | + /** |
|
32 | + * The home user storage requires a user object to create a unique storage id |
|
33 | + * @param array $params |
|
34 | + */ |
|
35 | + public function __construct($params) { |
|
36 | + if (! isset($params['user']) || ! $params['user'] instanceof User) { |
|
37 | + throw new \Exception('missing user object in parameters'); |
|
38 | + } |
|
39 | + $this->user = $params['user']; |
|
40 | + parent::__construct($params); |
|
41 | + } |
|
42 | 42 | |
43 | - public function getId() { |
|
44 | - return 'object::user:' . $this->user->getUID(); |
|
45 | - } |
|
43 | + public function getId() { |
|
44 | + return 'object::user:' . $this->user->getUID(); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * get the owner of a path |
|
49 | - * |
|
50 | - * @param string $path The path to get the owner |
|
51 | - * @return false|string uid |
|
52 | - */ |
|
53 | - public function getOwner($path) { |
|
54 | - if (is_object($this->user)) { |
|
55 | - return $this->user->getUID(); |
|
56 | - } |
|
57 | - return false; |
|
58 | - } |
|
47 | + /** |
|
48 | + * get the owner of a path |
|
49 | + * |
|
50 | + * @param string $path The path to get the owner |
|
51 | + * @return false|string uid |
|
52 | + */ |
|
53 | + public function getOwner($path) { |
|
54 | + if (is_object($this->user)) { |
|
55 | + return $this->user->getUID(); |
|
56 | + } |
|
57 | + return false; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @param string $path, optional |
|
62 | - * @return \OC\User\User |
|
63 | - */ |
|
64 | - public function getUser($path = null) { |
|
65 | - return $this->user; |
|
66 | - } |
|
60 | + /** |
|
61 | + * @param string $path, optional |
|
62 | + * @return \OC\User\User |
|
63 | + */ |
|
64 | + public function getUser($path = null) { |
|
65 | + return $this->user; |
|
66 | + } |
|
67 | 67 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * @param array $params |
34 | 34 | */ |
35 | 35 | public function __construct($params) { |
36 | - if (! isset($params['user']) || ! $params['user'] instanceof User) { |
|
36 | + if (!isset($params['user']) || !$params['user'] instanceof User) { |
|
37 | 37 | throw new \Exception('missing user object in parameters'); |
38 | 38 | } |
39 | 39 | $this->user = $params['user']; |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | } |
42 | 42 | |
43 | 43 | public function getId() { |
44 | - return 'object::user:' . $this->user->getUID(); |
|
44 | + return 'object::user:'.$this->user->getUID(); |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
@@ -24,17 +24,17 @@ |
||
24 | 24 | namespace OC\DB; |
25 | 25 | |
26 | 26 | class MigrationException extends \Exception { |
27 | - private $table; |
|
27 | + private $table; |
|
28 | 28 | |
29 | - public function __construct($table, $message) { |
|
30 | - $this->table = $table; |
|
31 | - parent::__construct($message); |
|
32 | - } |
|
29 | + public function __construct($table, $message) { |
|
30 | + $this->table = $table; |
|
31 | + parent::__construct($message); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function getTable() { |
|
38 | - return $this->table; |
|
39 | - } |
|
34 | + /** |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function getTable() { |
|
38 | + return $this->table; |
|
39 | + } |
|
40 | 40 | } |
@@ -25,17 +25,17 @@ |
||
25 | 25 | use OCP\DB\QueryBuilder\ILiteral; |
26 | 26 | |
27 | 27 | class Literal implements ILiteral { |
28 | - /** @var mixed */ |
|
29 | - protected $literal; |
|
28 | + /** @var mixed */ |
|
29 | + protected $literal; |
|
30 | 30 | |
31 | - public function __construct($literal) { |
|
32 | - $this->literal = $literal; |
|
33 | - } |
|
31 | + public function __construct($literal) { |
|
32 | + $this->literal = $literal; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function __toString() { |
|
39 | - return (string) $this->literal; |
|
40 | - } |
|
35 | + /** |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function __toString() { |
|
39 | + return (string) $this->literal; |
|
40 | + } |
|
41 | 41 | } |
@@ -64,14 +64,14 @@ |
||
64 | 64 | if (empty($compare)) { |
65 | 65 | $compare = array_keys($input); |
66 | 66 | } |
67 | - $fieldList = '`' . implode('`,`', array_keys($input)) . '`'; |
|
67 | + $fieldList = '`'.implode('`,`', array_keys($input)).'`'; |
|
68 | 68 | $query = "INSERT INTO `$table` ($fieldList) SELECT " |
69 | 69 | . str_repeat('?,', count($input) - 1).'? ' |
70 | 70 | . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE "; |
71 | 71 | |
72 | 72 | $inserts = array_values($input); |
73 | 73 | foreach ($compare as $key) { |
74 | - $query .= '`' . $key . '`'; |
|
74 | + $query .= '`'.$key.'`'; |
|
75 | 75 | if (is_null($input[$key])) { |
76 | 76 | $query .= ' IS NULL AND '; |
77 | 77 | } else { |
@@ -31,70 +31,70 @@ |
||
31 | 31 | |
32 | 32 | class AdapterSqlite extends Adapter { |
33 | 33 | |
34 | - /** |
|
35 | - * @param string $tableName |
|
36 | - */ |
|
37 | - public function lockTable($tableName) { |
|
38 | - $this->conn->executeUpdate('BEGIN EXCLUSIVE TRANSACTION'); |
|
39 | - } |
|
34 | + /** |
|
35 | + * @param string $tableName |
|
36 | + */ |
|
37 | + public function lockTable($tableName) { |
|
38 | + $this->conn->executeUpdate('BEGIN EXCLUSIVE TRANSACTION'); |
|
39 | + } |
|
40 | 40 | |
41 | - public function unlockTable() { |
|
42 | - $this->conn->executeUpdate('COMMIT TRANSACTION'); |
|
43 | - } |
|
41 | + public function unlockTable() { |
|
42 | + $this->conn->executeUpdate('COMMIT TRANSACTION'); |
|
43 | + } |
|
44 | 44 | |
45 | - public function fixupStatement($statement) { |
|
46 | - $statement = preg_replace('/`(\w+)` ILIKE \?/', 'LOWER($1) LIKE LOWER(?)', $statement); |
|
47 | - $statement = str_replace('`', '"', $statement); |
|
48 | - $statement = str_ireplace('NOW()', 'datetime(\'now\')', $statement); |
|
49 | - $statement = str_ireplace('GREATEST(', 'MAX(', $statement); |
|
50 | - $statement = str_ireplace('UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement); |
|
51 | - return $statement; |
|
52 | - } |
|
45 | + public function fixupStatement($statement) { |
|
46 | + $statement = preg_replace('/`(\w+)` ILIKE \?/', 'LOWER($1) LIKE LOWER(?)', $statement); |
|
47 | + $statement = str_replace('`', '"', $statement); |
|
48 | + $statement = str_ireplace('NOW()', 'datetime(\'now\')', $statement); |
|
49 | + $statement = str_ireplace('GREATEST(', 'MAX(', $statement); |
|
50 | + $statement = str_ireplace('UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement); |
|
51 | + return $statement; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
56 | - * it is needed that there is also a unique constraint on the values. Then this method will |
|
57 | - * catch the exception and return 0. |
|
58 | - * |
|
59 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
60 | - * @param array $input data that should be inserted into the table (column name => value) |
|
61 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
62 | - * If this is null or an empty array, all keys of $input will be compared |
|
63 | - * Please note: text fields (clob) must not be used in the compare array |
|
64 | - * @return int number of inserted rows |
|
65 | - * @throws \Doctrine\DBAL\Exception |
|
66 | - * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
67 | - */ |
|
68 | - public function insertIfNotExist($table, $input, array $compare = null) { |
|
69 | - if (empty($compare)) { |
|
70 | - $compare = array_keys($input); |
|
71 | - } |
|
72 | - $fieldList = '`' . implode('`,`', array_keys($input)) . '`'; |
|
73 | - $query = "INSERT INTO `$table` ($fieldList) SELECT " |
|
74 | - . str_repeat('?,', count($input) - 1).'? ' |
|
75 | - . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE "; |
|
54 | + /** |
|
55 | + * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
56 | + * it is needed that there is also a unique constraint on the values. Then this method will |
|
57 | + * catch the exception and return 0. |
|
58 | + * |
|
59 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
60 | + * @param array $input data that should be inserted into the table (column name => value) |
|
61 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
62 | + * If this is null or an empty array, all keys of $input will be compared |
|
63 | + * Please note: text fields (clob) must not be used in the compare array |
|
64 | + * @return int number of inserted rows |
|
65 | + * @throws \Doctrine\DBAL\Exception |
|
66 | + * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
67 | + */ |
|
68 | + public function insertIfNotExist($table, $input, array $compare = null) { |
|
69 | + if (empty($compare)) { |
|
70 | + $compare = array_keys($input); |
|
71 | + } |
|
72 | + $fieldList = '`' . implode('`,`', array_keys($input)) . '`'; |
|
73 | + $query = "INSERT INTO `$table` ($fieldList) SELECT " |
|
74 | + . str_repeat('?,', count($input) - 1).'? ' |
|
75 | + . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE "; |
|
76 | 76 | |
77 | - $inserts = array_values($input); |
|
78 | - foreach ($compare as $key) { |
|
79 | - $query .= '`' . $key . '`'; |
|
80 | - if (is_null($input[$key])) { |
|
81 | - $query .= ' IS NULL AND '; |
|
82 | - } else { |
|
83 | - $inserts[] = $input[$key]; |
|
84 | - $query .= ' = ? AND '; |
|
85 | - } |
|
86 | - } |
|
87 | - $query = substr($query, 0, -5); |
|
88 | - $query .= ')'; |
|
77 | + $inserts = array_values($input); |
|
78 | + foreach ($compare as $key) { |
|
79 | + $query .= '`' . $key . '`'; |
|
80 | + if (is_null($input[$key])) { |
|
81 | + $query .= ' IS NULL AND '; |
|
82 | + } else { |
|
83 | + $inserts[] = $input[$key]; |
|
84 | + $query .= ' = ? AND '; |
|
85 | + } |
|
86 | + } |
|
87 | + $query = substr($query, 0, -5); |
|
88 | + $query .= ')'; |
|
89 | 89 | |
90 | - try { |
|
91 | - return $this->conn->executeUpdate($query, $inserts); |
|
92 | - } catch (UniqueConstraintViolationException $e) { |
|
93 | - // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it |
|
94 | - // it's fine to ignore this then |
|
95 | - // |
|
96 | - // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315 |
|
97 | - return 0; |
|
98 | - } |
|
99 | - } |
|
90 | + try { |
|
91 | + return $this->conn->executeUpdate($query, $inserts); |
|
92 | + } catch (UniqueConstraintViolationException $e) { |
|
93 | + // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it |
|
94 | + // it's fine to ignore this then |
|
95 | + // |
|
96 | + // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315 |
|
97 | + return 0; |
|
98 | + } |
|
99 | + } |
|
100 | 100 | } |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | $this->loadTable($schema, $child); |
85 | 85 | break; |
86 | 86 | default: |
87 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
87 | + throw new \DomainException('Unknown element: '.$child->getName()); |
|
88 | 88 | |
89 | 89 | } |
90 | 90 | } |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | */ |
105 | 105 | switch ($child->getName()) { |
106 | 106 | case 'name': |
107 | - $name = (string)$child; |
|
107 | + $name = (string) $child; |
|
108 | 108 | $name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name); |
109 | 109 | $name = $this->platform->quoteIdentifier($name); |
110 | 110 | $table = $schema->createTable($name); |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | $this->loadDeclaration($table, $child); |
121 | 121 | break; |
122 | 122 | default: |
123 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
123 | + throw new \DomainException('Unknown element: '.$child->getName()); |
|
124 | 124 | |
125 | 125 | } |
126 | 126 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | $this->loadIndex($table, $child); |
145 | 145 | break; |
146 | 146 | default: |
147 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
147 | + throw new \DomainException('Unknown element: '.$child->getName()); |
|
148 | 148 | |
149 | 149 | } |
150 | 150 | } |
@@ -156,18 +156,18 @@ discard block |
||
156 | 156 | * @throws \DomainException |
157 | 157 | */ |
158 | 158 | private function loadField($table, $xml) { |
159 | - $options = [ 'notnull' => false ]; |
|
159 | + $options = ['notnull' => false]; |
|
160 | 160 | foreach ($xml->children() as $child) { |
161 | 161 | /** |
162 | 162 | * @var \SimpleXMLElement $child |
163 | 163 | */ |
164 | 164 | switch ($child->getName()) { |
165 | 165 | case 'name': |
166 | - $name = (string)$child; |
|
166 | + $name = (string) $child; |
|
167 | 167 | $name = $this->platform->quoteIdentifier($name); |
168 | 168 | break; |
169 | 169 | case 'type': |
170 | - $type = (string)$child; |
|
170 | + $type = (string) $child; |
|
171 | 171 | switch ($type) { |
172 | 172 | case 'text': |
173 | 173 | $type = 'string'; |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | } |
185 | 185 | break; |
186 | 186 | case 'length': |
187 | - $length = (string)$child; |
|
187 | + $length = (string) $child; |
|
188 | 188 | $options['length'] = $length; |
189 | 189 | break; |
190 | 190 | case 'unsigned': |
@@ -200,11 +200,11 @@ discard block |
||
200 | 200 | $options['autoincrement'] = $autoincrement; |
201 | 201 | break; |
202 | 202 | case 'default': |
203 | - $default = (string)$child; |
|
203 | + $default = (string) $child; |
|
204 | 204 | $options['default'] = $default; |
205 | 205 | break; |
206 | 206 | case 'comments': |
207 | - $comment = (string)$child; |
|
207 | + $comment = (string) $child; |
|
208 | 208 | $options['comment'] = $comment; |
209 | 209 | break; |
210 | 210 | case 'primary': |
@@ -212,15 +212,15 @@ discard block |
||
212 | 212 | $options['primary'] = $primary; |
213 | 213 | break; |
214 | 214 | case 'precision': |
215 | - $precision = (string)$child; |
|
215 | + $precision = (string) $child; |
|
216 | 216 | $options['precision'] = $precision; |
217 | 217 | break; |
218 | 218 | case 'scale': |
219 | - $scale = (string)$child; |
|
219 | + $scale = (string) $child; |
|
220 | 220 | $options['scale'] = $scale; |
221 | 221 | break; |
222 | 222 | default: |
223 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
223 | + throw new \DomainException('Unknown element: '.$child->getName()); |
|
224 | 224 | |
225 | 225 | } |
226 | 226 | } |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | } |
243 | 243 | } |
244 | 244 | if ($type === 'integer' && isset($options['default'])) { |
245 | - $options['default'] = (int)$options['default']; |
|
245 | + $options['default'] = (int) $options['default']; |
|
246 | 246 | } |
247 | 247 | if ($type === 'integer' && isset($options['length'])) { |
248 | 248 | $length = $options['length']; |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | */ |
283 | 283 | switch ($child->getName()) { |
284 | 284 | case 'name': |
285 | - $name = (string)$child; |
|
285 | + $name = (string) $child; |
|
286 | 286 | break; |
287 | 287 | case 'primary': |
288 | 288 | $primary = $this->asBool($child); |
@@ -297,20 +297,20 @@ discard block |
||
297 | 297 | */ |
298 | 298 | switch ($field->getName()) { |
299 | 299 | case 'name': |
300 | - $field_name = (string)$field; |
|
300 | + $field_name = (string) $field; |
|
301 | 301 | $field_name = $this->platform->quoteIdentifier($field_name); |
302 | 302 | $fields[] = $field_name; |
303 | 303 | break; |
304 | 304 | case 'sorting': |
305 | 305 | break; |
306 | 306 | default: |
307 | - throw new \DomainException('Unknown element: ' . $field->getName()); |
|
307 | + throw new \DomainException('Unknown element: '.$field->getName()); |
|
308 | 308 | |
309 | 309 | } |
310 | 310 | } |
311 | 311 | break; |
312 | 312 | default: |
313 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
313 | + throw new \DomainException('Unknown element: '.$child->getName()); |
|
314 | 314 | |
315 | 315 | } |
316 | 316 | } |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | } |
329 | 329 | } |
330 | 330 | } else { |
331 | - throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true)); |
|
331 | + throw new \DomainException('Empty index definition: '.$name.' options:'.print_r($fields, true)); |
|
332 | 332 | } |
333 | 333 | } |
334 | 334 | |
@@ -337,12 +337,12 @@ discard block |
||
337 | 337 | * @return bool |
338 | 338 | */ |
339 | 339 | private function asBool($xml) { |
340 | - $result = (string)$xml; |
|
340 | + $result = (string) $xml; |
|
341 | 341 | if ($result == 'true') { |
342 | 342 | $result = true; |
343 | 343 | } elseif ($result == 'false') { |
344 | 344 | $result = false; |
345 | 345 | } |
346 | - return (bool)$result; |
|
346 | + return (bool) $result; |
|
347 | 347 | } |
348 | 348 | } |
@@ -36,320 +36,320 @@ |
||
36 | 36 | |
37 | 37 | class MDB2SchemaReader { |
38 | 38 | |
39 | - /** |
|
40 | - * @var string $DBTABLEPREFIX |
|
41 | - */ |
|
42 | - protected $DBTABLEPREFIX; |
|
39 | + /** |
|
40 | + * @var string $DBTABLEPREFIX |
|
41 | + */ |
|
42 | + protected $DBTABLEPREFIX; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
46 | - */ |
|
47 | - protected $platform; |
|
44 | + /** |
|
45 | + * @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
46 | + */ |
|
47 | + protected $platform; |
|
48 | 48 | |
49 | - /** @var IConfig */ |
|
50 | - protected $config; |
|
49 | + /** @var IConfig */ |
|
50 | + protected $config; |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param \OCP\IConfig $config |
|
54 | - * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
55 | - */ |
|
56 | - public function __construct(IConfig $config, AbstractPlatform $platform) { |
|
57 | - $this->platform = $platform; |
|
58 | - $this->config = $config; |
|
59 | - $this->DBTABLEPREFIX = $config->getSystemValue('dbtableprefix', 'oc_'); |
|
60 | - } |
|
52 | + /** |
|
53 | + * @param \OCP\IConfig $config |
|
54 | + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
55 | + */ |
|
56 | + public function __construct(IConfig $config, AbstractPlatform $platform) { |
|
57 | + $this->platform = $platform; |
|
58 | + $this->config = $config; |
|
59 | + $this->DBTABLEPREFIX = $config->getSystemValue('dbtableprefix', 'oc_'); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @param string $file |
|
64 | - * @param Schema $schema |
|
65 | - * @return Schema |
|
66 | - * @throws \DomainException |
|
67 | - */ |
|
68 | - public function loadSchemaFromFile($file, Schema $schema) { |
|
69 | - $loadEntities = libxml_disable_entity_loader(false); |
|
70 | - $xml = simplexml_load_file($file); |
|
71 | - libxml_disable_entity_loader($loadEntities); |
|
72 | - foreach ($xml->children() as $child) { |
|
73 | - /** |
|
74 | - * @var \SimpleXMLElement $child |
|
75 | - */ |
|
76 | - switch ($child->getName()) { |
|
77 | - case 'name': |
|
78 | - case 'create': |
|
79 | - case 'overwrite': |
|
80 | - case 'charset': |
|
81 | - break; |
|
82 | - case 'table': |
|
83 | - $this->loadTable($schema, $child); |
|
84 | - break; |
|
85 | - default: |
|
86 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
62 | + /** |
|
63 | + * @param string $file |
|
64 | + * @param Schema $schema |
|
65 | + * @return Schema |
|
66 | + * @throws \DomainException |
|
67 | + */ |
|
68 | + public function loadSchemaFromFile($file, Schema $schema) { |
|
69 | + $loadEntities = libxml_disable_entity_loader(false); |
|
70 | + $xml = simplexml_load_file($file); |
|
71 | + libxml_disable_entity_loader($loadEntities); |
|
72 | + foreach ($xml->children() as $child) { |
|
73 | + /** |
|
74 | + * @var \SimpleXMLElement $child |
|
75 | + */ |
|
76 | + switch ($child->getName()) { |
|
77 | + case 'name': |
|
78 | + case 'create': |
|
79 | + case 'overwrite': |
|
80 | + case 'charset': |
|
81 | + break; |
|
82 | + case 'table': |
|
83 | + $this->loadTable($schema, $child); |
|
84 | + break; |
|
85 | + default: |
|
86 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
87 | 87 | |
88 | - } |
|
89 | - } |
|
90 | - return $schema; |
|
91 | - } |
|
88 | + } |
|
89 | + } |
|
90 | + return $schema; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * @param \Doctrine\DBAL\Schema\Schema $schema |
|
95 | - * @param \SimpleXMLElement $xml |
|
96 | - * @throws \DomainException |
|
97 | - */ |
|
98 | - private function loadTable($schema, $xml) { |
|
99 | - $table = null; |
|
100 | - foreach ($xml->children() as $child) { |
|
101 | - /** |
|
102 | - * @var \SimpleXMLElement $child |
|
103 | - */ |
|
104 | - switch ($child->getName()) { |
|
105 | - case 'name': |
|
106 | - $name = (string)$child; |
|
107 | - $name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name); |
|
108 | - $name = $this->platform->quoteIdentifier($name); |
|
109 | - $table = $schema->createTable($name); |
|
110 | - break; |
|
111 | - case 'create': |
|
112 | - case 'overwrite': |
|
113 | - case 'charset': |
|
114 | - break; |
|
115 | - case 'declaration': |
|
116 | - if (is_null($table)) { |
|
117 | - throw new \DomainException('Table declaration before table name'); |
|
118 | - } |
|
119 | - $this->loadDeclaration($table, $child); |
|
120 | - break; |
|
121 | - default: |
|
122 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
93 | + /** |
|
94 | + * @param \Doctrine\DBAL\Schema\Schema $schema |
|
95 | + * @param \SimpleXMLElement $xml |
|
96 | + * @throws \DomainException |
|
97 | + */ |
|
98 | + private function loadTable($schema, $xml) { |
|
99 | + $table = null; |
|
100 | + foreach ($xml->children() as $child) { |
|
101 | + /** |
|
102 | + * @var \SimpleXMLElement $child |
|
103 | + */ |
|
104 | + switch ($child->getName()) { |
|
105 | + case 'name': |
|
106 | + $name = (string)$child; |
|
107 | + $name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name); |
|
108 | + $name = $this->platform->quoteIdentifier($name); |
|
109 | + $table = $schema->createTable($name); |
|
110 | + break; |
|
111 | + case 'create': |
|
112 | + case 'overwrite': |
|
113 | + case 'charset': |
|
114 | + break; |
|
115 | + case 'declaration': |
|
116 | + if (is_null($table)) { |
|
117 | + throw new \DomainException('Table declaration before table name'); |
|
118 | + } |
|
119 | + $this->loadDeclaration($table, $child); |
|
120 | + break; |
|
121 | + default: |
|
122 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
123 | 123 | |
124 | - } |
|
125 | - } |
|
126 | - } |
|
124 | + } |
|
125 | + } |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
130 | - * @param \SimpleXMLElement $xml |
|
131 | - * @throws \DomainException |
|
132 | - */ |
|
133 | - private function loadDeclaration($table, $xml) { |
|
134 | - foreach ($xml->children() as $child) { |
|
135 | - /** |
|
136 | - * @var \SimpleXMLElement $child |
|
137 | - */ |
|
138 | - switch ($child->getName()) { |
|
139 | - case 'field': |
|
140 | - $this->loadField($table, $child); |
|
141 | - break; |
|
142 | - case 'index': |
|
143 | - $this->loadIndex($table, $child); |
|
144 | - break; |
|
145 | - default: |
|
146 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
128 | + /** |
|
129 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
130 | + * @param \SimpleXMLElement $xml |
|
131 | + * @throws \DomainException |
|
132 | + */ |
|
133 | + private function loadDeclaration($table, $xml) { |
|
134 | + foreach ($xml->children() as $child) { |
|
135 | + /** |
|
136 | + * @var \SimpleXMLElement $child |
|
137 | + */ |
|
138 | + switch ($child->getName()) { |
|
139 | + case 'field': |
|
140 | + $this->loadField($table, $child); |
|
141 | + break; |
|
142 | + case 'index': |
|
143 | + $this->loadIndex($table, $child); |
|
144 | + break; |
|
145 | + default: |
|
146 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
147 | 147 | |
148 | - } |
|
149 | - } |
|
150 | - } |
|
148 | + } |
|
149 | + } |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
154 | - * @param \SimpleXMLElement $xml |
|
155 | - * @throws \DomainException |
|
156 | - */ |
|
157 | - private function loadField($table, $xml) { |
|
158 | - $options = [ 'notnull' => false ]; |
|
159 | - foreach ($xml->children() as $child) { |
|
160 | - /** |
|
161 | - * @var \SimpleXMLElement $child |
|
162 | - */ |
|
163 | - switch ($child->getName()) { |
|
164 | - case 'name': |
|
165 | - $name = (string)$child; |
|
166 | - $name = $this->platform->quoteIdentifier($name); |
|
167 | - break; |
|
168 | - case 'type': |
|
169 | - $type = (string)$child; |
|
170 | - switch ($type) { |
|
171 | - case 'text': |
|
172 | - $type = 'string'; |
|
173 | - break; |
|
174 | - case 'clob': |
|
175 | - $type = 'text'; |
|
176 | - break; |
|
177 | - case 'timestamp': |
|
178 | - $type = 'datetime'; |
|
179 | - break; |
|
180 | - case 'numeric': |
|
181 | - $type = 'decimal'; |
|
182 | - break; |
|
183 | - } |
|
184 | - break; |
|
185 | - case 'length': |
|
186 | - $length = (string)$child; |
|
187 | - $options['length'] = $length; |
|
188 | - break; |
|
189 | - case 'unsigned': |
|
190 | - $unsigned = $this->asBool($child); |
|
191 | - $options['unsigned'] = $unsigned; |
|
192 | - break; |
|
193 | - case 'notnull': |
|
194 | - $notnull = $this->asBool($child); |
|
195 | - $options['notnull'] = $notnull; |
|
196 | - break; |
|
197 | - case 'autoincrement': |
|
198 | - $autoincrement = $this->asBool($child); |
|
199 | - $options['autoincrement'] = $autoincrement; |
|
200 | - break; |
|
201 | - case 'default': |
|
202 | - $default = (string)$child; |
|
203 | - $options['default'] = $default; |
|
204 | - break; |
|
205 | - case 'comments': |
|
206 | - $comment = (string)$child; |
|
207 | - $options['comment'] = $comment; |
|
208 | - break; |
|
209 | - case 'primary': |
|
210 | - $primary = $this->asBool($child); |
|
211 | - $options['primary'] = $primary; |
|
212 | - break; |
|
213 | - case 'precision': |
|
214 | - $precision = (string)$child; |
|
215 | - $options['precision'] = $precision; |
|
216 | - break; |
|
217 | - case 'scale': |
|
218 | - $scale = (string)$child; |
|
219 | - $options['scale'] = $scale; |
|
220 | - break; |
|
221 | - default: |
|
222 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
152 | + /** |
|
153 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
154 | + * @param \SimpleXMLElement $xml |
|
155 | + * @throws \DomainException |
|
156 | + */ |
|
157 | + private function loadField($table, $xml) { |
|
158 | + $options = [ 'notnull' => false ]; |
|
159 | + foreach ($xml->children() as $child) { |
|
160 | + /** |
|
161 | + * @var \SimpleXMLElement $child |
|
162 | + */ |
|
163 | + switch ($child->getName()) { |
|
164 | + case 'name': |
|
165 | + $name = (string)$child; |
|
166 | + $name = $this->platform->quoteIdentifier($name); |
|
167 | + break; |
|
168 | + case 'type': |
|
169 | + $type = (string)$child; |
|
170 | + switch ($type) { |
|
171 | + case 'text': |
|
172 | + $type = 'string'; |
|
173 | + break; |
|
174 | + case 'clob': |
|
175 | + $type = 'text'; |
|
176 | + break; |
|
177 | + case 'timestamp': |
|
178 | + $type = 'datetime'; |
|
179 | + break; |
|
180 | + case 'numeric': |
|
181 | + $type = 'decimal'; |
|
182 | + break; |
|
183 | + } |
|
184 | + break; |
|
185 | + case 'length': |
|
186 | + $length = (string)$child; |
|
187 | + $options['length'] = $length; |
|
188 | + break; |
|
189 | + case 'unsigned': |
|
190 | + $unsigned = $this->asBool($child); |
|
191 | + $options['unsigned'] = $unsigned; |
|
192 | + break; |
|
193 | + case 'notnull': |
|
194 | + $notnull = $this->asBool($child); |
|
195 | + $options['notnull'] = $notnull; |
|
196 | + break; |
|
197 | + case 'autoincrement': |
|
198 | + $autoincrement = $this->asBool($child); |
|
199 | + $options['autoincrement'] = $autoincrement; |
|
200 | + break; |
|
201 | + case 'default': |
|
202 | + $default = (string)$child; |
|
203 | + $options['default'] = $default; |
|
204 | + break; |
|
205 | + case 'comments': |
|
206 | + $comment = (string)$child; |
|
207 | + $options['comment'] = $comment; |
|
208 | + break; |
|
209 | + case 'primary': |
|
210 | + $primary = $this->asBool($child); |
|
211 | + $options['primary'] = $primary; |
|
212 | + break; |
|
213 | + case 'precision': |
|
214 | + $precision = (string)$child; |
|
215 | + $options['precision'] = $precision; |
|
216 | + break; |
|
217 | + case 'scale': |
|
218 | + $scale = (string)$child; |
|
219 | + $options['scale'] = $scale; |
|
220 | + break; |
|
221 | + default: |
|
222 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
223 | 223 | |
224 | - } |
|
225 | - } |
|
226 | - if (isset($name) && isset($type)) { |
|
227 | - if (isset($options['default']) && empty($options['default'])) { |
|
228 | - if (empty($options['notnull']) || !$options['notnull']) { |
|
229 | - unset($options['default']); |
|
230 | - $options['notnull'] = false; |
|
231 | - } else { |
|
232 | - $options['default'] = ''; |
|
233 | - } |
|
234 | - if ($type == 'integer' || $type == 'decimal') { |
|
235 | - $options['default'] = 0; |
|
236 | - } elseif ($type == 'boolean') { |
|
237 | - $options['default'] = false; |
|
238 | - } |
|
239 | - if (!empty($options['autoincrement']) && $options['autoincrement']) { |
|
240 | - unset($options['default']); |
|
241 | - } |
|
242 | - } |
|
243 | - if ($type === 'integer' && isset($options['default'])) { |
|
244 | - $options['default'] = (int)$options['default']; |
|
245 | - } |
|
246 | - if ($type === 'integer' && isset($options['length'])) { |
|
247 | - $length = $options['length']; |
|
248 | - if ($length < 4) { |
|
249 | - $type = 'smallint'; |
|
250 | - } elseif ($length > 4) { |
|
251 | - $type = 'bigint'; |
|
252 | - } |
|
253 | - } |
|
254 | - if ($type === 'boolean' && isset($options['default'])) { |
|
255 | - $options['default'] = $this->asBool($options['default']); |
|
256 | - } |
|
257 | - if (!empty($options['autoincrement']) |
|
258 | - && !empty($options['notnull']) |
|
259 | - ) { |
|
260 | - $options['primary'] = true; |
|
261 | - } |
|
224 | + } |
|
225 | + } |
|
226 | + if (isset($name) && isset($type)) { |
|
227 | + if (isset($options['default']) && empty($options['default'])) { |
|
228 | + if (empty($options['notnull']) || !$options['notnull']) { |
|
229 | + unset($options['default']); |
|
230 | + $options['notnull'] = false; |
|
231 | + } else { |
|
232 | + $options['default'] = ''; |
|
233 | + } |
|
234 | + if ($type == 'integer' || $type == 'decimal') { |
|
235 | + $options['default'] = 0; |
|
236 | + } elseif ($type == 'boolean') { |
|
237 | + $options['default'] = false; |
|
238 | + } |
|
239 | + if (!empty($options['autoincrement']) && $options['autoincrement']) { |
|
240 | + unset($options['default']); |
|
241 | + } |
|
242 | + } |
|
243 | + if ($type === 'integer' && isset($options['default'])) { |
|
244 | + $options['default'] = (int)$options['default']; |
|
245 | + } |
|
246 | + if ($type === 'integer' && isset($options['length'])) { |
|
247 | + $length = $options['length']; |
|
248 | + if ($length < 4) { |
|
249 | + $type = 'smallint'; |
|
250 | + } elseif ($length > 4) { |
|
251 | + $type = 'bigint'; |
|
252 | + } |
|
253 | + } |
|
254 | + if ($type === 'boolean' && isset($options['default'])) { |
|
255 | + $options['default'] = $this->asBool($options['default']); |
|
256 | + } |
|
257 | + if (!empty($options['autoincrement']) |
|
258 | + && !empty($options['notnull']) |
|
259 | + ) { |
|
260 | + $options['primary'] = true; |
|
261 | + } |
|
262 | 262 | |
263 | - # not used anymore in the options argument |
|
264 | - # see https://github.com/doctrine/dbal/commit/138eb85234a1faeaa2e6a32cd7bcc66bb51c64e8#diff-300f55366adb50a32a40882ebdc95c163b141f64cba5f45f20bda04a907b3eb3L82 |
|
265 | - # therefore it's read before and then unset right before the addColumn call |
|
266 | - $setPrimaryKey = false; |
|
267 | - if (!empty($options['primary']) && $options['primary']) { |
|
268 | - $setPrimaryKey = true; |
|
269 | - } |
|
270 | - unset($options['primary']); |
|
271 | - $table->addColumn($name, $type, $options); |
|
272 | - if ($setPrimaryKey) { |
|
273 | - $table->setPrimaryKey([$name]); |
|
274 | - } |
|
275 | - } |
|
276 | - } |
|
263 | + # not used anymore in the options argument |
|
264 | + # see https://github.com/doctrine/dbal/commit/138eb85234a1faeaa2e6a32cd7bcc66bb51c64e8#diff-300f55366adb50a32a40882ebdc95c163b141f64cba5f45f20bda04a907b3eb3L82 |
|
265 | + # therefore it's read before and then unset right before the addColumn call |
|
266 | + $setPrimaryKey = false; |
|
267 | + if (!empty($options['primary']) && $options['primary']) { |
|
268 | + $setPrimaryKey = true; |
|
269 | + } |
|
270 | + unset($options['primary']); |
|
271 | + $table->addColumn($name, $type, $options); |
|
272 | + if ($setPrimaryKey) { |
|
273 | + $table->setPrimaryKey([$name]); |
|
274 | + } |
|
275 | + } |
|
276 | + } |
|
277 | 277 | |
278 | - /** |
|
279 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
280 | - * @param \SimpleXMLElement $xml |
|
281 | - * @throws \DomainException |
|
282 | - */ |
|
283 | - private function loadIndex($table, $xml) { |
|
284 | - $name = null; |
|
285 | - $fields = []; |
|
286 | - foreach ($xml->children() as $child) { |
|
287 | - /** |
|
288 | - * @var \SimpleXMLElement $child |
|
289 | - */ |
|
290 | - switch ($child->getName()) { |
|
291 | - case 'name': |
|
292 | - $name = (string)$child; |
|
293 | - break; |
|
294 | - case 'primary': |
|
295 | - $primary = $this->asBool($child); |
|
296 | - break; |
|
297 | - case 'unique': |
|
298 | - $unique = $this->asBool($child); |
|
299 | - break; |
|
300 | - case 'field': |
|
301 | - foreach ($child->children() as $field) { |
|
302 | - /** |
|
303 | - * @var \SimpleXMLElement $field |
|
304 | - */ |
|
305 | - switch ($field->getName()) { |
|
306 | - case 'name': |
|
307 | - $field_name = (string)$field; |
|
308 | - $field_name = $this->platform->quoteIdentifier($field_name); |
|
309 | - $fields[] = $field_name; |
|
310 | - break; |
|
311 | - case 'sorting': |
|
312 | - break; |
|
313 | - default: |
|
314 | - throw new \DomainException('Unknown element: ' . $field->getName()); |
|
278 | + /** |
|
279 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
280 | + * @param \SimpleXMLElement $xml |
|
281 | + * @throws \DomainException |
|
282 | + */ |
|
283 | + private function loadIndex($table, $xml) { |
|
284 | + $name = null; |
|
285 | + $fields = []; |
|
286 | + foreach ($xml->children() as $child) { |
|
287 | + /** |
|
288 | + * @var \SimpleXMLElement $child |
|
289 | + */ |
|
290 | + switch ($child->getName()) { |
|
291 | + case 'name': |
|
292 | + $name = (string)$child; |
|
293 | + break; |
|
294 | + case 'primary': |
|
295 | + $primary = $this->asBool($child); |
|
296 | + break; |
|
297 | + case 'unique': |
|
298 | + $unique = $this->asBool($child); |
|
299 | + break; |
|
300 | + case 'field': |
|
301 | + foreach ($child->children() as $field) { |
|
302 | + /** |
|
303 | + * @var \SimpleXMLElement $field |
|
304 | + */ |
|
305 | + switch ($field->getName()) { |
|
306 | + case 'name': |
|
307 | + $field_name = (string)$field; |
|
308 | + $field_name = $this->platform->quoteIdentifier($field_name); |
|
309 | + $fields[] = $field_name; |
|
310 | + break; |
|
311 | + case 'sorting': |
|
312 | + break; |
|
313 | + default: |
|
314 | + throw new \DomainException('Unknown element: ' . $field->getName()); |
|
315 | 315 | |
316 | - } |
|
317 | - } |
|
318 | - break; |
|
319 | - default: |
|
320 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
316 | + } |
|
317 | + } |
|
318 | + break; |
|
319 | + default: |
|
320 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
321 | 321 | |
322 | - } |
|
323 | - } |
|
324 | - if (!empty($fields)) { |
|
325 | - if (isset($primary) && $primary) { |
|
326 | - if ($table->hasPrimaryKey()) { |
|
327 | - return; |
|
328 | - } |
|
329 | - $table->setPrimaryKey($fields, $name); |
|
330 | - } else { |
|
331 | - if (isset($unique) && $unique) { |
|
332 | - $table->addUniqueIndex($fields, $name); |
|
333 | - } else { |
|
334 | - $table->addIndex($fields, $name); |
|
335 | - } |
|
336 | - } |
|
337 | - } else { |
|
338 | - throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true)); |
|
339 | - } |
|
340 | - } |
|
322 | + } |
|
323 | + } |
|
324 | + if (!empty($fields)) { |
|
325 | + if (isset($primary) && $primary) { |
|
326 | + if ($table->hasPrimaryKey()) { |
|
327 | + return; |
|
328 | + } |
|
329 | + $table->setPrimaryKey($fields, $name); |
|
330 | + } else { |
|
331 | + if (isset($unique) && $unique) { |
|
332 | + $table->addUniqueIndex($fields, $name); |
|
333 | + } else { |
|
334 | + $table->addIndex($fields, $name); |
|
335 | + } |
|
336 | + } |
|
337 | + } else { |
|
338 | + throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true)); |
|
339 | + } |
|
340 | + } |
|
341 | 341 | |
342 | - /** |
|
343 | - * @param \SimpleXMLElement|string $xml |
|
344 | - * @return bool |
|
345 | - */ |
|
346 | - private function asBool($xml) { |
|
347 | - $result = (string)$xml; |
|
348 | - if ($result == 'true') { |
|
349 | - $result = true; |
|
350 | - } elseif ($result == 'false') { |
|
351 | - $result = false; |
|
352 | - } |
|
353 | - return (bool)$result; |
|
354 | - } |
|
342 | + /** |
|
343 | + * @param \SimpleXMLElement|string $xml |
|
344 | + * @return bool |
|
345 | + */ |
|
346 | + private function asBool($xml) { |
|
347 | + $result = (string)$xml; |
|
348 | + if ($result == 'true') { |
|
349 | + $result = true; |
|
350 | + } elseif ($result == 'false') { |
|
351 | + $result = false; |
|
352 | + } |
|
353 | + return (bool)$result; |
|
354 | + } |
|
355 | 355 | } |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | * @param string $table table name without the prefix |
83 | 83 | */ |
84 | 84 | public function dropTable($table) { |
85 | - $table = $this->tablePrefix . trim($table); |
|
85 | + $table = $this->tablePrefix.trim($table); |
|
86 | 86 | $table = $this->quoteIdentifier($table); |
87 | 87 | $schema = $this->getSchemaManager(); |
88 | 88 | if ($schema->tablesExist([$table])) { |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | * @return bool |
98 | 98 | */ |
99 | 99 | public function tableExists($table) { |
100 | - $table = $this->tablePrefix . trim($table); |
|
100 | + $table = $this->tablePrefix.trim($table); |
|
101 | 101 | $table = $this->quoteIdentifier($table); |
102 | 102 | $schema = $this->getSchemaManager(); |
103 | 103 | return $schema->tablesExist([$table]); |
@@ -28,80 +28,80 @@ |
||
28 | 28 | namespace OC\DB; |
29 | 29 | |
30 | 30 | class OracleConnection extends Connection { |
31 | - /** |
|
32 | - * Quote the keys of the array |
|
33 | - */ |
|
34 | - private function quoteKeys(array $data) { |
|
35 | - $return = []; |
|
36 | - $c = $this->getDatabasePlatform()->getIdentifierQuoteCharacter(); |
|
37 | - foreach ($data as $key => $value) { |
|
38 | - if ($key[0] !== $c) { |
|
39 | - $return[$this->quoteIdentifier($key)] = $value; |
|
40 | - } else { |
|
41 | - $return[$key] = $value; |
|
42 | - } |
|
43 | - } |
|
44 | - return $return; |
|
45 | - } |
|
31 | + /** |
|
32 | + * Quote the keys of the array |
|
33 | + */ |
|
34 | + private function quoteKeys(array $data) { |
|
35 | + $return = []; |
|
36 | + $c = $this->getDatabasePlatform()->getIdentifierQuoteCharacter(); |
|
37 | + foreach ($data as $key => $value) { |
|
38 | + if ($key[0] !== $c) { |
|
39 | + $return[$this->quoteIdentifier($key)] = $value; |
|
40 | + } else { |
|
41 | + $return[$key] = $value; |
|
42 | + } |
|
43 | + } |
|
44 | + return $return; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * {@inheritDoc} |
|
49 | - */ |
|
50 | - public function insert($table, array $data, array $types = []) { |
|
51 | - if ($table[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
52 | - $table = $this->quoteIdentifier($table); |
|
53 | - } |
|
54 | - $data = $this->quoteKeys($data); |
|
55 | - return parent::insert($table, $data, $types); |
|
56 | - } |
|
47 | + /** |
|
48 | + * {@inheritDoc} |
|
49 | + */ |
|
50 | + public function insert($table, array $data, array $types = []) { |
|
51 | + if ($table[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
52 | + $table = $this->quoteIdentifier($table); |
|
53 | + } |
|
54 | + $data = $this->quoteKeys($data); |
|
55 | + return parent::insert($table, $data, $types); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * {@inheritDoc} |
|
60 | - */ |
|
61 | - public function update($table, array $data, array $criteria, array $types = []) { |
|
62 | - if ($table[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
63 | - $table = $this->quoteIdentifier($table); |
|
64 | - } |
|
65 | - $data = $this->quoteKeys($data); |
|
66 | - $criteria = $this->quoteKeys($criteria); |
|
67 | - return parent::update($table, $data, $criteria, $types); |
|
68 | - } |
|
58 | + /** |
|
59 | + * {@inheritDoc} |
|
60 | + */ |
|
61 | + public function update($table, array $data, array $criteria, array $types = []) { |
|
62 | + if ($table[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
63 | + $table = $this->quoteIdentifier($table); |
|
64 | + } |
|
65 | + $data = $this->quoteKeys($data); |
|
66 | + $criteria = $this->quoteKeys($criteria); |
|
67 | + return parent::update($table, $data, $criteria, $types); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * {@inheritDoc} |
|
72 | - */ |
|
73 | - public function delete($table, array $criteria, array $types = []) { |
|
74 | - if ($table[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
75 | - $table = $this->quoteIdentifier($table); |
|
76 | - } |
|
77 | - $criteria = $this->quoteKeys($criteria); |
|
78 | - return parent::delete($table, $criteria); |
|
79 | - } |
|
70 | + /** |
|
71 | + * {@inheritDoc} |
|
72 | + */ |
|
73 | + public function delete($table, array $criteria, array $types = []) { |
|
74 | + if ($table[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
75 | + $table = $this->quoteIdentifier($table); |
|
76 | + } |
|
77 | + $criteria = $this->quoteKeys($criteria); |
|
78 | + return parent::delete($table, $criteria); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Drop a table from the database if it exists |
|
83 | - * |
|
84 | - * @param string $table table name without the prefix |
|
85 | - */ |
|
86 | - public function dropTable($table) { |
|
87 | - $table = $this->tablePrefix . trim($table); |
|
88 | - $table = $this->quoteIdentifier($table); |
|
89 | - $schema = $this->getSchemaManager(); |
|
90 | - if ($schema->tablesExist([$table])) { |
|
91 | - $schema->dropTable($table); |
|
92 | - } |
|
93 | - } |
|
81 | + /** |
|
82 | + * Drop a table from the database if it exists |
|
83 | + * |
|
84 | + * @param string $table table name without the prefix |
|
85 | + */ |
|
86 | + public function dropTable($table) { |
|
87 | + $table = $this->tablePrefix . trim($table); |
|
88 | + $table = $this->quoteIdentifier($table); |
|
89 | + $schema = $this->getSchemaManager(); |
|
90 | + if ($schema->tablesExist([$table])) { |
|
91 | + $schema->dropTable($table); |
|
92 | + } |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Check if a table exists |
|
97 | - * |
|
98 | - * @param string $table table name without the prefix |
|
99 | - * @return bool |
|
100 | - */ |
|
101 | - public function tableExists($table) { |
|
102 | - $table = $this->tablePrefix . trim($table); |
|
103 | - $table = $this->quoteIdentifier($table); |
|
104 | - $schema = $this->getSchemaManager(); |
|
105 | - return $schema->tablesExist([$table]); |
|
106 | - } |
|
95 | + /** |
|
96 | + * Check if a table exists |
|
97 | + * |
|
98 | + * @param string $table table name without the prefix |
|
99 | + * @return bool |
|
100 | + */ |
|
101 | + public function tableExists($table) { |
|
102 | + $table = $this->tablePrefix . trim($table); |
|
103 | + $table = $this->quoteIdentifier($table); |
|
104 | + $schema = $this->getSchemaManager(); |
|
105 | + return $schema->tablesExist([$table]); |
|
106 | + } |
|
107 | 107 | } |
@@ -333,7 +333,7 @@ |
||
333 | 333 | */ |
334 | 334 | protected function registerCoreProvider($class, $mimeType, $options = []) { |
335 | 335 | if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
336 | - $this->registerProvider($mimeType, function () use ($class, $options) { |
|
336 | + $this->registerProvider($mimeType, function() use ($class, $options) { |
|
337 | 337 | return new $class($options); |
338 | 338 | }); |
339 | 339 | } |
@@ -44,393 +44,393 @@ |
||
44 | 44 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
45 | 45 | |
46 | 46 | class PreviewManager implements IPreview { |
47 | - /** @var IConfig */ |
|
48 | - protected $config; |
|
49 | - |
|
50 | - /** @var IRootFolder */ |
|
51 | - protected $rootFolder; |
|
52 | - |
|
53 | - /** @var IAppData */ |
|
54 | - protected $appData; |
|
55 | - |
|
56 | - /** @var EventDispatcherInterface */ |
|
57 | - protected $eventDispatcher; |
|
58 | - |
|
59 | - /** @var Generator */ |
|
60 | - private $generator; |
|
61 | - |
|
62 | - /** @var GeneratorHelper */ |
|
63 | - private $helper; |
|
64 | - |
|
65 | - /** @var bool */ |
|
66 | - protected $providerListDirty = false; |
|
67 | - |
|
68 | - /** @var bool */ |
|
69 | - protected $registeredCoreProviders = false; |
|
70 | - |
|
71 | - /** @var array */ |
|
72 | - protected $providers = []; |
|
73 | - |
|
74 | - /** @var array mime type => support status */ |
|
75 | - protected $mimeTypeSupportMap = []; |
|
76 | - |
|
77 | - /** @var array */ |
|
78 | - protected $defaultProviders; |
|
79 | - |
|
80 | - /** @var string */ |
|
81 | - protected $userId; |
|
82 | - |
|
83 | - /** |
|
84 | - * PreviewManager constructor. |
|
85 | - * |
|
86 | - * @param IConfig $config |
|
87 | - * @param IRootFolder $rootFolder |
|
88 | - * @param IAppData $appData |
|
89 | - * @param EventDispatcherInterface $eventDispatcher |
|
90 | - * @param string $userId |
|
91 | - */ |
|
92 | - public function __construct(IConfig $config, |
|
93 | - IRootFolder $rootFolder, |
|
94 | - IAppData $appData, |
|
95 | - EventDispatcherInterface $eventDispatcher, |
|
96 | - GeneratorHelper $helper, |
|
97 | - $userId) { |
|
98 | - $this->config = $config; |
|
99 | - $this->rootFolder = $rootFolder; |
|
100 | - $this->appData = $appData; |
|
101 | - $this->eventDispatcher = $eventDispatcher; |
|
102 | - $this->helper = $helper; |
|
103 | - $this->userId = $userId; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * In order to improve lazy loading a closure can be registered which will be |
|
108 | - * called in case preview providers are actually requested |
|
109 | - * |
|
110 | - * $callable has to return an instance of \OCP\Preview\IProvider or \OCP\Preview\IProviderV2 |
|
111 | - * |
|
112 | - * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
|
113 | - * @param \Closure $callable |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - public function registerProvider($mimeTypeRegex, \Closure $callable) { |
|
117 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
118 | - return; |
|
119 | - } |
|
120 | - |
|
121 | - if (!isset($this->providers[$mimeTypeRegex])) { |
|
122 | - $this->providers[$mimeTypeRegex] = []; |
|
123 | - } |
|
124 | - $this->providers[$mimeTypeRegex][] = $callable; |
|
125 | - $this->providerListDirty = true; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Get all providers |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - public function getProviders() { |
|
133 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
134 | - return []; |
|
135 | - } |
|
136 | - |
|
137 | - $this->registerCoreProviders(); |
|
138 | - if ($this->providerListDirty) { |
|
139 | - $keys = array_map('strlen', array_keys($this->providers)); |
|
140 | - array_multisort($keys, SORT_DESC, $this->providers); |
|
141 | - $this->providerListDirty = false; |
|
142 | - } |
|
143 | - |
|
144 | - return $this->providers; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Does the manager have any providers |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - public function hasProviders() { |
|
152 | - $this->registerCoreProviders(); |
|
153 | - return !empty($this->providers); |
|
154 | - } |
|
155 | - |
|
156 | - private function getGenerator(): Generator { |
|
157 | - if ($this->generator === null) { |
|
158 | - $this->generator = new Generator( |
|
159 | - $this->config, |
|
160 | - $this, |
|
161 | - $this->appData, |
|
162 | - new GeneratorHelper( |
|
163 | - $this->rootFolder, |
|
164 | - $this->config |
|
165 | - ), |
|
166 | - $this->eventDispatcher |
|
167 | - ); |
|
168 | - } |
|
169 | - return $this->generator; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Returns a preview of a file |
|
174 | - * |
|
175 | - * The cache is searched first and if nothing usable was found then a preview is |
|
176 | - * generated by one of the providers |
|
177 | - * |
|
178 | - * @param File $file |
|
179 | - * @param int $width |
|
180 | - * @param int $height |
|
181 | - * @param bool $crop |
|
182 | - * @param string $mode |
|
183 | - * @param string $mimeType |
|
184 | - * @return ISimpleFile |
|
185 | - * @throws NotFoundException |
|
186 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
187 | - * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
|
188 | - */ |
|
189 | - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
190 | - return $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Generates previews of a file |
|
195 | - * |
|
196 | - * @param File $file |
|
197 | - * @param array $specifications |
|
198 | - * @param string $mimeType |
|
199 | - * @return ISimpleFile the last preview that was generated |
|
200 | - * @throws NotFoundException |
|
201 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
202 | - * @since 19.0.0 |
|
203 | - */ |
|
204 | - public function generatePreviews(File $file, array $specifications, $mimeType = null) { |
|
205 | - return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * returns true if the passed mime type is supported |
|
210 | - * |
|
211 | - * @param string $mimeType |
|
212 | - * @return boolean |
|
213 | - */ |
|
214 | - public function isMimeSupported($mimeType = '*') { |
|
215 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
216 | - return false; |
|
217 | - } |
|
218 | - |
|
219 | - if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
220 | - return $this->mimeTypeSupportMap[$mimeType]; |
|
221 | - } |
|
222 | - |
|
223 | - $this->registerCoreProviders(); |
|
224 | - $providerMimeTypes = array_keys($this->providers); |
|
225 | - foreach ($providerMimeTypes as $supportedMimeType) { |
|
226 | - if (preg_match($supportedMimeType, $mimeType)) { |
|
227 | - $this->mimeTypeSupportMap[$mimeType] = true; |
|
228 | - return true; |
|
229 | - } |
|
230 | - } |
|
231 | - $this->mimeTypeSupportMap[$mimeType] = false; |
|
232 | - return false; |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Check if a preview can be generated for a file |
|
237 | - * |
|
238 | - * @param \OCP\Files\FileInfo $file |
|
239 | - * @return bool |
|
240 | - */ |
|
241 | - public function isAvailable(\OCP\Files\FileInfo $file) { |
|
242 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
243 | - return false; |
|
244 | - } |
|
245 | - |
|
246 | - $this->registerCoreProviders(); |
|
247 | - if (!$this->isMimeSupported($file->getMimetype())) { |
|
248 | - return false; |
|
249 | - } |
|
250 | - |
|
251 | - $mount = $file->getMountPoint(); |
|
252 | - if ($mount and !$mount->getOption('previews', true)) { |
|
253 | - return false; |
|
254 | - } |
|
255 | - |
|
256 | - foreach ($this->providers as $supportedMimeType => $providers) { |
|
257 | - if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
258 | - foreach ($providers as $providerClosure) { |
|
259 | - $provider = $this->helper->getProvider($providerClosure); |
|
260 | - if (!($provider instanceof IProviderV2)) { |
|
261 | - continue; |
|
262 | - } |
|
263 | - |
|
264 | - if ($provider->isAvailable($file)) { |
|
265 | - return true; |
|
266 | - } |
|
267 | - } |
|
268 | - } |
|
269 | - } |
|
270 | - return false; |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * List of enabled default providers |
|
275 | - * |
|
276 | - * The following providers are enabled by default: |
|
277 | - * - OC\Preview\PNG |
|
278 | - * - OC\Preview\JPEG |
|
279 | - * - OC\Preview\GIF |
|
280 | - * - OC\Preview\BMP |
|
281 | - * - OC\Preview\HEIC |
|
282 | - * - OC\Preview\XBitmap |
|
283 | - * - OC\Preview\MarkDown |
|
284 | - * - OC\Preview\MP3 |
|
285 | - * - OC\Preview\TXT |
|
286 | - * |
|
287 | - * The following providers are disabled by default due to performance or privacy concerns: |
|
288 | - * - OC\Preview\Font |
|
289 | - * - OC\Preview\Illustrator |
|
290 | - * - OC\Preview\Movie |
|
291 | - * - OC\Preview\MSOfficeDoc |
|
292 | - * - OC\Preview\MSOffice2003 |
|
293 | - * - OC\Preview\MSOffice2007 |
|
294 | - * - OC\Preview\OpenDocument |
|
295 | - * - OC\Preview\PDF |
|
296 | - * - OC\Preview\Photoshop |
|
297 | - * - OC\Preview\Postscript |
|
298 | - * - OC\Preview\StarOffice |
|
299 | - * - OC\Preview\SVG |
|
300 | - * - OC\Preview\TIFF |
|
301 | - * |
|
302 | - * @return array |
|
303 | - */ |
|
304 | - protected function getEnabledDefaultProvider() { |
|
305 | - if ($this->defaultProviders !== null) { |
|
306 | - return $this->defaultProviders; |
|
307 | - } |
|
308 | - |
|
309 | - $imageProviders = [ |
|
310 | - Preview\PNG::class, |
|
311 | - Preview\JPEG::class, |
|
312 | - Preview\GIF::class, |
|
313 | - Preview\BMP::class, |
|
314 | - Preview\HEIC::class, |
|
315 | - Preview\XBitmap::class, |
|
316 | - Preview\Krita::class, |
|
317 | - Preview\WebP::class, |
|
318 | - ]; |
|
319 | - |
|
320 | - $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
321 | - Preview\MarkDown::class, |
|
322 | - Preview\MP3::class, |
|
323 | - Preview\TXT::class, |
|
324 | - Preview\OpenDocument::class, |
|
325 | - ], $imageProviders)); |
|
326 | - |
|
327 | - if (in_array(Preview\Image::class, $this->defaultProviders)) { |
|
328 | - $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
329 | - } |
|
330 | - $this->defaultProviders = array_unique($this->defaultProviders); |
|
331 | - return $this->defaultProviders; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Register the default providers (if enabled) |
|
336 | - * |
|
337 | - * @param string $class |
|
338 | - * @param string $mimeType |
|
339 | - */ |
|
340 | - protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
341 | - if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
342 | - $this->registerProvider($mimeType, function () use ($class, $options) { |
|
343 | - return new $class($options); |
|
344 | - }); |
|
345 | - } |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Register the default providers (if enabled) |
|
350 | - */ |
|
351 | - protected function registerCoreProviders() { |
|
352 | - if ($this->registeredCoreProviders) { |
|
353 | - return; |
|
354 | - } |
|
355 | - $this->registeredCoreProviders = true; |
|
356 | - |
|
357 | - $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/'); |
|
358 | - $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/'); |
|
359 | - $this->registerCoreProvider(Preview\PNG::class, '/image\/png/'); |
|
360 | - $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/'); |
|
361 | - $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); |
|
362 | - $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); |
|
363 | - $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); |
|
364 | - $this->registerCoreProvider(Preview\WebP::class, '/image\/webp/'); |
|
365 | - $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); |
|
366 | - $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/'); |
|
367 | - $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
368 | - |
|
369 | - // SVG, Office and Bitmap require imagick |
|
370 | - if (extension_loaded('imagick')) { |
|
371 | - $checkImagick = new \Imagick(); |
|
372 | - |
|
373 | - $imagickProviders = [ |
|
374 | - 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], |
|
375 | - 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], |
|
376 | - 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class], |
|
377 | - 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class], |
|
378 | - 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class], |
|
379 | - 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class], |
|
380 | - 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class], |
|
381 | - 'HEIC' => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class], |
|
382 | - 'TGA' => ['mimetype' => '/image\/t(ar)?ga/', 'class' => Preview\TGA::class], |
|
383 | - 'SGI' => ['mimetype' => '/image\/sgi/', 'class' => Preview\SGI::class], |
|
384 | - ]; |
|
385 | - |
|
386 | - foreach ($imagickProviders as $queryFormat => $provider) { |
|
387 | - $class = $provider['class']; |
|
388 | - if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
389 | - continue; |
|
390 | - } |
|
391 | - |
|
392 | - if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
393 | - $this->registerCoreProvider($class, $provider['mimetype']); |
|
394 | - } |
|
395 | - } |
|
396 | - |
|
397 | - if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
398 | - if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
399 | - $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
400 | - |
|
401 | - if (!$officeFound) { |
|
402 | - //let's see if there is libreoffice or openoffice on this machine |
|
403 | - $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
404 | - $officeFound = !empty($whichLibreOffice); |
|
405 | - if (!$officeFound) { |
|
406 | - $whichOpenOffice = shell_exec('command -v openoffice'); |
|
407 | - $officeFound = !empty($whichOpenOffice); |
|
408 | - } |
|
409 | - } |
|
410 | - |
|
411 | - if ($officeFound) { |
|
412 | - $this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/'); |
|
413 | - $this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/'); |
|
414 | - $this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/'); |
|
415 | - $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
416 | - $this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/'); |
|
417 | - } |
|
418 | - } |
|
419 | - } |
|
420 | - } |
|
421 | - |
|
422 | - // Video requires avconv or ffmpeg |
|
423 | - if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) { |
|
424 | - $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
425 | - $ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
426 | - |
|
427 | - if ($avconvBinary || $ffmpegBinary) { |
|
428 | - // FIXME // a bit hacky but didn't want to use subclasses |
|
429 | - \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
430 | - \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
431 | - |
|
432 | - $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/'); |
|
433 | - } |
|
434 | - } |
|
435 | - } |
|
47 | + /** @var IConfig */ |
|
48 | + protected $config; |
|
49 | + |
|
50 | + /** @var IRootFolder */ |
|
51 | + protected $rootFolder; |
|
52 | + |
|
53 | + /** @var IAppData */ |
|
54 | + protected $appData; |
|
55 | + |
|
56 | + /** @var EventDispatcherInterface */ |
|
57 | + protected $eventDispatcher; |
|
58 | + |
|
59 | + /** @var Generator */ |
|
60 | + private $generator; |
|
61 | + |
|
62 | + /** @var GeneratorHelper */ |
|
63 | + private $helper; |
|
64 | + |
|
65 | + /** @var bool */ |
|
66 | + protected $providerListDirty = false; |
|
67 | + |
|
68 | + /** @var bool */ |
|
69 | + protected $registeredCoreProviders = false; |
|
70 | + |
|
71 | + /** @var array */ |
|
72 | + protected $providers = []; |
|
73 | + |
|
74 | + /** @var array mime type => support status */ |
|
75 | + protected $mimeTypeSupportMap = []; |
|
76 | + |
|
77 | + /** @var array */ |
|
78 | + protected $defaultProviders; |
|
79 | + |
|
80 | + /** @var string */ |
|
81 | + protected $userId; |
|
82 | + |
|
83 | + /** |
|
84 | + * PreviewManager constructor. |
|
85 | + * |
|
86 | + * @param IConfig $config |
|
87 | + * @param IRootFolder $rootFolder |
|
88 | + * @param IAppData $appData |
|
89 | + * @param EventDispatcherInterface $eventDispatcher |
|
90 | + * @param string $userId |
|
91 | + */ |
|
92 | + public function __construct(IConfig $config, |
|
93 | + IRootFolder $rootFolder, |
|
94 | + IAppData $appData, |
|
95 | + EventDispatcherInterface $eventDispatcher, |
|
96 | + GeneratorHelper $helper, |
|
97 | + $userId) { |
|
98 | + $this->config = $config; |
|
99 | + $this->rootFolder = $rootFolder; |
|
100 | + $this->appData = $appData; |
|
101 | + $this->eventDispatcher = $eventDispatcher; |
|
102 | + $this->helper = $helper; |
|
103 | + $this->userId = $userId; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * In order to improve lazy loading a closure can be registered which will be |
|
108 | + * called in case preview providers are actually requested |
|
109 | + * |
|
110 | + * $callable has to return an instance of \OCP\Preview\IProvider or \OCP\Preview\IProviderV2 |
|
111 | + * |
|
112 | + * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
|
113 | + * @param \Closure $callable |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + public function registerProvider($mimeTypeRegex, \Closure $callable) { |
|
117 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
118 | + return; |
|
119 | + } |
|
120 | + |
|
121 | + if (!isset($this->providers[$mimeTypeRegex])) { |
|
122 | + $this->providers[$mimeTypeRegex] = []; |
|
123 | + } |
|
124 | + $this->providers[$mimeTypeRegex][] = $callable; |
|
125 | + $this->providerListDirty = true; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Get all providers |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + public function getProviders() { |
|
133 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
134 | + return []; |
|
135 | + } |
|
136 | + |
|
137 | + $this->registerCoreProviders(); |
|
138 | + if ($this->providerListDirty) { |
|
139 | + $keys = array_map('strlen', array_keys($this->providers)); |
|
140 | + array_multisort($keys, SORT_DESC, $this->providers); |
|
141 | + $this->providerListDirty = false; |
|
142 | + } |
|
143 | + |
|
144 | + return $this->providers; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Does the manager have any providers |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + public function hasProviders() { |
|
152 | + $this->registerCoreProviders(); |
|
153 | + return !empty($this->providers); |
|
154 | + } |
|
155 | + |
|
156 | + private function getGenerator(): Generator { |
|
157 | + if ($this->generator === null) { |
|
158 | + $this->generator = new Generator( |
|
159 | + $this->config, |
|
160 | + $this, |
|
161 | + $this->appData, |
|
162 | + new GeneratorHelper( |
|
163 | + $this->rootFolder, |
|
164 | + $this->config |
|
165 | + ), |
|
166 | + $this->eventDispatcher |
|
167 | + ); |
|
168 | + } |
|
169 | + return $this->generator; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Returns a preview of a file |
|
174 | + * |
|
175 | + * The cache is searched first and if nothing usable was found then a preview is |
|
176 | + * generated by one of the providers |
|
177 | + * |
|
178 | + * @param File $file |
|
179 | + * @param int $width |
|
180 | + * @param int $height |
|
181 | + * @param bool $crop |
|
182 | + * @param string $mode |
|
183 | + * @param string $mimeType |
|
184 | + * @return ISimpleFile |
|
185 | + * @throws NotFoundException |
|
186 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
187 | + * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
|
188 | + */ |
|
189 | + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
190 | + return $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Generates previews of a file |
|
195 | + * |
|
196 | + * @param File $file |
|
197 | + * @param array $specifications |
|
198 | + * @param string $mimeType |
|
199 | + * @return ISimpleFile the last preview that was generated |
|
200 | + * @throws NotFoundException |
|
201 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
202 | + * @since 19.0.0 |
|
203 | + */ |
|
204 | + public function generatePreviews(File $file, array $specifications, $mimeType = null) { |
|
205 | + return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * returns true if the passed mime type is supported |
|
210 | + * |
|
211 | + * @param string $mimeType |
|
212 | + * @return boolean |
|
213 | + */ |
|
214 | + public function isMimeSupported($mimeType = '*') { |
|
215 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
216 | + return false; |
|
217 | + } |
|
218 | + |
|
219 | + if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
220 | + return $this->mimeTypeSupportMap[$mimeType]; |
|
221 | + } |
|
222 | + |
|
223 | + $this->registerCoreProviders(); |
|
224 | + $providerMimeTypes = array_keys($this->providers); |
|
225 | + foreach ($providerMimeTypes as $supportedMimeType) { |
|
226 | + if (preg_match($supportedMimeType, $mimeType)) { |
|
227 | + $this->mimeTypeSupportMap[$mimeType] = true; |
|
228 | + return true; |
|
229 | + } |
|
230 | + } |
|
231 | + $this->mimeTypeSupportMap[$mimeType] = false; |
|
232 | + return false; |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Check if a preview can be generated for a file |
|
237 | + * |
|
238 | + * @param \OCP\Files\FileInfo $file |
|
239 | + * @return bool |
|
240 | + */ |
|
241 | + public function isAvailable(\OCP\Files\FileInfo $file) { |
|
242 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
243 | + return false; |
|
244 | + } |
|
245 | + |
|
246 | + $this->registerCoreProviders(); |
|
247 | + if (!$this->isMimeSupported($file->getMimetype())) { |
|
248 | + return false; |
|
249 | + } |
|
250 | + |
|
251 | + $mount = $file->getMountPoint(); |
|
252 | + if ($mount and !$mount->getOption('previews', true)) { |
|
253 | + return false; |
|
254 | + } |
|
255 | + |
|
256 | + foreach ($this->providers as $supportedMimeType => $providers) { |
|
257 | + if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
258 | + foreach ($providers as $providerClosure) { |
|
259 | + $provider = $this->helper->getProvider($providerClosure); |
|
260 | + if (!($provider instanceof IProviderV2)) { |
|
261 | + continue; |
|
262 | + } |
|
263 | + |
|
264 | + if ($provider->isAvailable($file)) { |
|
265 | + return true; |
|
266 | + } |
|
267 | + } |
|
268 | + } |
|
269 | + } |
|
270 | + return false; |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * List of enabled default providers |
|
275 | + * |
|
276 | + * The following providers are enabled by default: |
|
277 | + * - OC\Preview\PNG |
|
278 | + * - OC\Preview\JPEG |
|
279 | + * - OC\Preview\GIF |
|
280 | + * - OC\Preview\BMP |
|
281 | + * - OC\Preview\HEIC |
|
282 | + * - OC\Preview\XBitmap |
|
283 | + * - OC\Preview\MarkDown |
|
284 | + * - OC\Preview\MP3 |
|
285 | + * - OC\Preview\TXT |
|
286 | + * |
|
287 | + * The following providers are disabled by default due to performance or privacy concerns: |
|
288 | + * - OC\Preview\Font |
|
289 | + * - OC\Preview\Illustrator |
|
290 | + * - OC\Preview\Movie |
|
291 | + * - OC\Preview\MSOfficeDoc |
|
292 | + * - OC\Preview\MSOffice2003 |
|
293 | + * - OC\Preview\MSOffice2007 |
|
294 | + * - OC\Preview\OpenDocument |
|
295 | + * - OC\Preview\PDF |
|
296 | + * - OC\Preview\Photoshop |
|
297 | + * - OC\Preview\Postscript |
|
298 | + * - OC\Preview\StarOffice |
|
299 | + * - OC\Preview\SVG |
|
300 | + * - OC\Preview\TIFF |
|
301 | + * |
|
302 | + * @return array |
|
303 | + */ |
|
304 | + protected function getEnabledDefaultProvider() { |
|
305 | + if ($this->defaultProviders !== null) { |
|
306 | + return $this->defaultProviders; |
|
307 | + } |
|
308 | + |
|
309 | + $imageProviders = [ |
|
310 | + Preview\PNG::class, |
|
311 | + Preview\JPEG::class, |
|
312 | + Preview\GIF::class, |
|
313 | + Preview\BMP::class, |
|
314 | + Preview\HEIC::class, |
|
315 | + Preview\XBitmap::class, |
|
316 | + Preview\Krita::class, |
|
317 | + Preview\WebP::class, |
|
318 | + ]; |
|
319 | + |
|
320 | + $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
321 | + Preview\MarkDown::class, |
|
322 | + Preview\MP3::class, |
|
323 | + Preview\TXT::class, |
|
324 | + Preview\OpenDocument::class, |
|
325 | + ], $imageProviders)); |
|
326 | + |
|
327 | + if (in_array(Preview\Image::class, $this->defaultProviders)) { |
|
328 | + $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
329 | + } |
|
330 | + $this->defaultProviders = array_unique($this->defaultProviders); |
|
331 | + return $this->defaultProviders; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Register the default providers (if enabled) |
|
336 | + * |
|
337 | + * @param string $class |
|
338 | + * @param string $mimeType |
|
339 | + */ |
|
340 | + protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
341 | + if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
342 | + $this->registerProvider($mimeType, function () use ($class, $options) { |
|
343 | + return new $class($options); |
|
344 | + }); |
|
345 | + } |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Register the default providers (if enabled) |
|
350 | + */ |
|
351 | + protected function registerCoreProviders() { |
|
352 | + if ($this->registeredCoreProviders) { |
|
353 | + return; |
|
354 | + } |
|
355 | + $this->registeredCoreProviders = true; |
|
356 | + |
|
357 | + $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/'); |
|
358 | + $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/'); |
|
359 | + $this->registerCoreProvider(Preview\PNG::class, '/image\/png/'); |
|
360 | + $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/'); |
|
361 | + $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); |
|
362 | + $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); |
|
363 | + $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); |
|
364 | + $this->registerCoreProvider(Preview\WebP::class, '/image\/webp/'); |
|
365 | + $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); |
|
366 | + $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/'); |
|
367 | + $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
368 | + |
|
369 | + // SVG, Office and Bitmap require imagick |
|
370 | + if (extension_loaded('imagick')) { |
|
371 | + $checkImagick = new \Imagick(); |
|
372 | + |
|
373 | + $imagickProviders = [ |
|
374 | + 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], |
|
375 | + 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], |
|
376 | + 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class], |
|
377 | + 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class], |
|
378 | + 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class], |
|
379 | + 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class], |
|
380 | + 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class], |
|
381 | + 'HEIC' => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class], |
|
382 | + 'TGA' => ['mimetype' => '/image\/t(ar)?ga/', 'class' => Preview\TGA::class], |
|
383 | + 'SGI' => ['mimetype' => '/image\/sgi/', 'class' => Preview\SGI::class], |
|
384 | + ]; |
|
385 | + |
|
386 | + foreach ($imagickProviders as $queryFormat => $provider) { |
|
387 | + $class = $provider['class']; |
|
388 | + if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
389 | + continue; |
|
390 | + } |
|
391 | + |
|
392 | + if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
393 | + $this->registerCoreProvider($class, $provider['mimetype']); |
|
394 | + } |
|
395 | + } |
|
396 | + |
|
397 | + if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
398 | + if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
399 | + $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
400 | + |
|
401 | + if (!$officeFound) { |
|
402 | + //let's see if there is libreoffice or openoffice on this machine |
|
403 | + $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
404 | + $officeFound = !empty($whichLibreOffice); |
|
405 | + if (!$officeFound) { |
|
406 | + $whichOpenOffice = shell_exec('command -v openoffice'); |
|
407 | + $officeFound = !empty($whichOpenOffice); |
|
408 | + } |
|
409 | + } |
|
410 | + |
|
411 | + if ($officeFound) { |
|
412 | + $this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/'); |
|
413 | + $this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/'); |
|
414 | + $this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/'); |
|
415 | + $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
416 | + $this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/'); |
|
417 | + } |
|
418 | + } |
|
419 | + } |
|
420 | + } |
|
421 | + |
|
422 | + // Video requires avconv or ffmpeg |
|
423 | + if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) { |
|
424 | + $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
425 | + $ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
426 | + |
|
427 | + if ($avconvBinary || $ffmpegBinary) { |
|
428 | + // FIXME // a bit hacky but didn't want to use subclasses |
|
429 | + \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
430 | + \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
431 | + |
|
432 | + $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/'); |
|
433 | + } |
|
434 | + } |
|
435 | + } |
|
436 | 436 | } |
@@ -25,12 +25,12 @@ |
||
25 | 25 | use OCP\IUser; |
26 | 26 | |
27 | 27 | trait FileAccess { |
28 | - protected function setupFS(IUser $user) { |
|
29 | - \OC_Util::setupFS($user->getUID()); |
|
30 | - } |
|
28 | + protected function setupFS(IUser $user) { |
|
29 | + \OC_Util::setupFS($user->getUID()); |
|
30 | + } |
|
31 | 31 | |
32 | - protected function getUserFolder(IUser $user) { |
|
33 | - $this->setupFS($user); |
|
34 | - return \OC::$server->getUserFolder($user->getUID()); |
|
35 | - } |
|
32 | + protected function getUserFolder(IUser $user) { |
|
33 | + $this->setupFS($user); |
|
34 | + return \OC::$server->getUserFolder($user->getUID()); |
|
35 | + } |
|
36 | 36 | } |
@@ -33,193 +33,193 @@ |
||
33 | 33 | use OCP\IMemcache; |
34 | 34 | |
35 | 35 | class Memcached extends Cache implements IMemcache { |
36 | - use CASTrait; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var \Memcached $cache |
|
40 | - */ |
|
41 | - private static $cache = null; |
|
42 | - |
|
43 | - use CADTrait; |
|
44 | - |
|
45 | - public function __construct($prefix = '') { |
|
46 | - parent::__construct($prefix); |
|
47 | - if (is_null(self::$cache)) { |
|
48 | - self::$cache = new \Memcached(); |
|
49 | - |
|
50 | - $defaultOptions = [ |
|
51 | - \Memcached::OPT_CONNECT_TIMEOUT => 50, |
|
52 | - \Memcached::OPT_RETRY_TIMEOUT => 50, |
|
53 | - \Memcached::OPT_SEND_TIMEOUT => 50, |
|
54 | - \Memcached::OPT_RECV_TIMEOUT => 50, |
|
55 | - \Memcached::OPT_POLL_TIMEOUT => 50, |
|
56 | - |
|
57 | - // Enable compression |
|
58 | - \Memcached::OPT_COMPRESSION => true, |
|
59 | - |
|
60 | - // Turn on consistent hashing |
|
61 | - \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
|
62 | - |
|
63 | - // Enable Binary Protocol |
|
64 | - //\Memcached::OPT_BINARY_PROTOCOL => true, |
|
65 | - ]; |
|
66 | - // by default enable igbinary serializer if available |
|
67 | - if (\Memcached::HAVE_IGBINARY) { |
|
68 | - $defaultOptions[\Memcached::OPT_SERIALIZER] = |
|
69 | - \Memcached::SERIALIZER_IGBINARY; |
|
70 | - } |
|
71 | - $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); |
|
72 | - if (is_array($options)) { |
|
73 | - $options = $options + $defaultOptions; |
|
74 | - self::$cache->setOptions($options); |
|
75 | - } else { |
|
76 | - throw new HintException("Expected 'memcached_options' config to be an array, got $options"); |
|
77 | - } |
|
78 | - |
|
79 | - $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); |
|
80 | - if (!$servers) { |
|
81 | - $server = \OC::$server->getSystemConfig()->getValue('memcached_server'); |
|
82 | - if ($server) { |
|
83 | - $servers = [$server]; |
|
84 | - } else { |
|
85 | - $servers = [['localhost', 11211]]; |
|
86 | - } |
|
87 | - } |
|
88 | - self::$cache->addServers($servers); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * entries in XCache gets namespaced to prevent collisions between owncloud instances and users |
|
94 | - */ |
|
95 | - protected function getNameSpace() { |
|
96 | - return $this->prefix; |
|
97 | - } |
|
98 | - |
|
99 | - public function get($key) { |
|
100 | - $result = self::$cache->get($this->getNameSpace() . $key); |
|
101 | - if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
|
102 | - return null; |
|
103 | - } else { |
|
104 | - return $result; |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - public function set($key, $value, $ttl = 0) { |
|
109 | - if ($ttl > 0) { |
|
110 | - $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); |
|
111 | - } else { |
|
112 | - $result = self::$cache->set($this->getNameSpace() . $key, $value); |
|
113 | - } |
|
114 | - if ($result !== true) { |
|
115 | - $this->verifyReturnCode(); |
|
116 | - } |
|
117 | - return $result; |
|
118 | - } |
|
119 | - |
|
120 | - public function hasKey($key) { |
|
121 | - self::$cache->get($this->getNameSpace() . $key); |
|
122 | - return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
|
123 | - } |
|
124 | - |
|
125 | - public function remove($key) { |
|
126 | - $result = self::$cache->delete($this->getNameSpace() . $key); |
|
127 | - if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
|
128 | - $this->verifyReturnCode(); |
|
129 | - } |
|
130 | - return $result; |
|
131 | - } |
|
132 | - |
|
133 | - public function clear($prefix = '') { |
|
134 | - $prefix = $this->getNameSpace() . $prefix; |
|
135 | - $allKeys = self::$cache->getAllKeys(); |
|
136 | - if ($allKeys === false) { |
|
137 | - // newer Memcached doesn't like getAllKeys(), flush everything |
|
138 | - self::$cache->flush(); |
|
139 | - return true; |
|
140 | - } |
|
141 | - $keys = []; |
|
142 | - $prefixLength = strlen($prefix); |
|
143 | - foreach ($allKeys as $key) { |
|
144 | - if (substr($key, 0, $prefixLength) === $prefix) { |
|
145 | - $keys[] = $key; |
|
146 | - } |
|
147 | - } |
|
148 | - if (method_exists(self::$cache, 'deleteMulti')) { |
|
149 | - self::$cache->deleteMulti($keys); |
|
150 | - } else { |
|
151 | - foreach ($keys as $key) { |
|
152 | - self::$cache->delete($key); |
|
153 | - } |
|
154 | - } |
|
155 | - return true; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Set a value in the cache if it's not already stored |
|
160 | - * |
|
161 | - * @param string $key |
|
162 | - * @param mixed $value |
|
163 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
164 | - * @return bool |
|
165 | - * @throws \Exception |
|
166 | - */ |
|
167 | - public function add($key, $value, $ttl = 0) { |
|
168 | - $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
169 | - if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
|
170 | - $this->verifyReturnCode(); |
|
171 | - } |
|
172 | - return $result; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Increase a stored number |
|
177 | - * |
|
178 | - * @param string $key |
|
179 | - * @param int $step |
|
180 | - * @return int | bool |
|
181 | - */ |
|
182 | - public function inc($key, $step = 1) { |
|
183 | - $this->add($key, 0); |
|
184 | - $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
185 | - |
|
186 | - if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
187 | - return false; |
|
188 | - } |
|
189 | - |
|
190 | - return $result; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Decrease a stored number |
|
195 | - * |
|
196 | - * @param string $key |
|
197 | - * @param int $step |
|
198 | - * @return int | bool |
|
199 | - */ |
|
200 | - public function dec($key, $step = 1) { |
|
201 | - $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
202 | - |
|
203 | - if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
204 | - return false; |
|
205 | - } |
|
206 | - |
|
207 | - return $result; |
|
208 | - } |
|
209 | - |
|
210 | - public static function isAvailable() { |
|
211 | - return extension_loaded('memcached'); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @throws \Exception |
|
216 | - */ |
|
217 | - private function verifyReturnCode() { |
|
218 | - $code = self::$cache->getResultCode(); |
|
219 | - if ($code === \Memcached::RES_SUCCESS) { |
|
220 | - return; |
|
221 | - } |
|
222 | - $message = self::$cache->getResultMessage(); |
|
223 | - throw new \Exception("Error $code interacting with memcached : $message"); |
|
224 | - } |
|
36 | + use CASTrait; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var \Memcached $cache |
|
40 | + */ |
|
41 | + private static $cache = null; |
|
42 | + |
|
43 | + use CADTrait; |
|
44 | + |
|
45 | + public function __construct($prefix = '') { |
|
46 | + parent::__construct($prefix); |
|
47 | + if (is_null(self::$cache)) { |
|
48 | + self::$cache = new \Memcached(); |
|
49 | + |
|
50 | + $defaultOptions = [ |
|
51 | + \Memcached::OPT_CONNECT_TIMEOUT => 50, |
|
52 | + \Memcached::OPT_RETRY_TIMEOUT => 50, |
|
53 | + \Memcached::OPT_SEND_TIMEOUT => 50, |
|
54 | + \Memcached::OPT_RECV_TIMEOUT => 50, |
|
55 | + \Memcached::OPT_POLL_TIMEOUT => 50, |
|
56 | + |
|
57 | + // Enable compression |
|
58 | + \Memcached::OPT_COMPRESSION => true, |
|
59 | + |
|
60 | + // Turn on consistent hashing |
|
61 | + \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
|
62 | + |
|
63 | + // Enable Binary Protocol |
|
64 | + //\Memcached::OPT_BINARY_PROTOCOL => true, |
|
65 | + ]; |
|
66 | + // by default enable igbinary serializer if available |
|
67 | + if (\Memcached::HAVE_IGBINARY) { |
|
68 | + $defaultOptions[\Memcached::OPT_SERIALIZER] = |
|
69 | + \Memcached::SERIALIZER_IGBINARY; |
|
70 | + } |
|
71 | + $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); |
|
72 | + if (is_array($options)) { |
|
73 | + $options = $options + $defaultOptions; |
|
74 | + self::$cache->setOptions($options); |
|
75 | + } else { |
|
76 | + throw new HintException("Expected 'memcached_options' config to be an array, got $options"); |
|
77 | + } |
|
78 | + |
|
79 | + $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); |
|
80 | + if (!$servers) { |
|
81 | + $server = \OC::$server->getSystemConfig()->getValue('memcached_server'); |
|
82 | + if ($server) { |
|
83 | + $servers = [$server]; |
|
84 | + } else { |
|
85 | + $servers = [['localhost', 11211]]; |
|
86 | + } |
|
87 | + } |
|
88 | + self::$cache->addServers($servers); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * entries in XCache gets namespaced to prevent collisions between owncloud instances and users |
|
94 | + */ |
|
95 | + protected function getNameSpace() { |
|
96 | + return $this->prefix; |
|
97 | + } |
|
98 | + |
|
99 | + public function get($key) { |
|
100 | + $result = self::$cache->get($this->getNameSpace() . $key); |
|
101 | + if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
|
102 | + return null; |
|
103 | + } else { |
|
104 | + return $result; |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + public function set($key, $value, $ttl = 0) { |
|
109 | + if ($ttl > 0) { |
|
110 | + $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); |
|
111 | + } else { |
|
112 | + $result = self::$cache->set($this->getNameSpace() . $key, $value); |
|
113 | + } |
|
114 | + if ($result !== true) { |
|
115 | + $this->verifyReturnCode(); |
|
116 | + } |
|
117 | + return $result; |
|
118 | + } |
|
119 | + |
|
120 | + public function hasKey($key) { |
|
121 | + self::$cache->get($this->getNameSpace() . $key); |
|
122 | + return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
|
123 | + } |
|
124 | + |
|
125 | + public function remove($key) { |
|
126 | + $result = self::$cache->delete($this->getNameSpace() . $key); |
|
127 | + if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
|
128 | + $this->verifyReturnCode(); |
|
129 | + } |
|
130 | + return $result; |
|
131 | + } |
|
132 | + |
|
133 | + public function clear($prefix = '') { |
|
134 | + $prefix = $this->getNameSpace() . $prefix; |
|
135 | + $allKeys = self::$cache->getAllKeys(); |
|
136 | + if ($allKeys === false) { |
|
137 | + // newer Memcached doesn't like getAllKeys(), flush everything |
|
138 | + self::$cache->flush(); |
|
139 | + return true; |
|
140 | + } |
|
141 | + $keys = []; |
|
142 | + $prefixLength = strlen($prefix); |
|
143 | + foreach ($allKeys as $key) { |
|
144 | + if (substr($key, 0, $prefixLength) === $prefix) { |
|
145 | + $keys[] = $key; |
|
146 | + } |
|
147 | + } |
|
148 | + if (method_exists(self::$cache, 'deleteMulti')) { |
|
149 | + self::$cache->deleteMulti($keys); |
|
150 | + } else { |
|
151 | + foreach ($keys as $key) { |
|
152 | + self::$cache->delete($key); |
|
153 | + } |
|
154 | + } |
|
155 | + return true; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Set a value in the cache if it's not already stored |
|
160 | + * |
|
161 | + * @param string $key |
|
162 | + * @param mixed $value |
|
163 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
164 | + * @return bool |
|
165 | + * @throws \Exception |
|
166 | + */ |
|
167 | + public function add($key, $value, $ttl = 0) { |
|
168 | + $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
169 | + if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
|
170 | + $this->verifyReturnCode(); |
|
171 | + } |
|
172 | + return $result; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Increase a stored number |
|
177 | + * |
|
178 | + * @param string $key |
|
179 | + * @param int $step |
|
180 | + * @return int | bool |
|
181 | + */ |
|
182 | + public function inc($key, $step = 1) { |
|
183 | + $this->add($key, 0); |
|
184 | + $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
185 | + |
|
186 | + if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
187 | + return false; |
|
188 | + } |
|
189 | + |
|
190 | + return $result; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Decrease a stored number |
|
195 | + * |
|
196 | + * @param string $key |
|
197 | + * @param int $step |
|
198 | + * @return int | bool |
|
199 | + */ |
|
200 | + public function dec($key, $step = 1) { |
|
201 | + $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
202 | + |
|
203 | + if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
204 | + return false; |
|
205 | + } |
|
206 | + |
|
207 | + return $result; |
|
208 | + } |
|
209 | + |
|
210 | + public static function isAvailable() { |
|
211 | + return extension_loaded('memcached'); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @throws \Exception |
|
216 | + */ |
|
217 | + private function verifyReturnCode() { |
|
218 | + $code = self::$cache->getResultCode(); |
|
219 | + if ($code === \Memcached::RES_SUCCESS) { |
|
220 | + return; |
|
221 | + } |
|
222 | + $message = self::$cache->getResultMessage(); |
|
223 | + throw new \Exception("Error $code interacting with memcached : $message"); |
|
224 | + } |
|
225 | 225 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | } |
98 | 98 | |
99 | 99 | public function get($key) { |
100 | - $result = self::$cache->get($this->getNameSpace() . $key); |
|
100 | + $result = self::$cache->get($this->getNameSpace().$key); |
|
101 | 101 | if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
102 | 102 | return null; |
103 | 103 | } else { |
@@ -107,9 +107,9 @@ discard block |
||
107 | 107 | |
108 | 108 | public function set($key, $value, $ttl = 0) { |
109 | 109 | if ($ttl > 0) { |
110 | - $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); |
|
110 | + $result = self::$cache->set($this->getNameSpace().$key, $value, $ttl); |
|
111 | 111 | } else { |
112 | - $result = self::$cache->set($this->getNameSpace() . $key, $value); |
|
112 | + $result = self::$cache->set($this->getNameSpace().$key, $value); |
|
113 | 113 | } |
114 | 114 | if ($result !== true) { |
115 | 115 | $this->verifyReturnCode(); |
@@ -118,12 +118,12 @@ discard block |
||
118 | 118 | } |
119 | 119 | |
120 | 120 | public function hasKey($key) { |
121 | - self::$cache->get($this->getNameSpace() . $key); |
|
121 | + self::$cache->get($this->getNameSpace().$key); |
|
122 | 122 | return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
123 | 123 | } |
124 | 124 | |
125 | 125 | public function remove($key) { |
126 | - $result = self::$cache->delete($this->getNameSpace() . $key); |
|
126 | + $result = self::$cache->delete($this->getNameSpace().$key); |
|
127 | 127 | if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
128 | 128 | $this->verifyReturnCode(); |
129 | 129 | } |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | } |
132 | 132 | |
133 | 133 | public function clear($prefix = '') { |
134 | - $prefix = $this->getNameSpace() . $prefix; |
|
134 | + $prefix = $this->getNameSpace().$prefix; |
|
135 | 135 | $allKeys = self::$cache->getAllKeys(); |
136 | 136 | if ($allKeys === false) { |
137 | 137 | // newer Memcached doesn't like getAllKeys(), flush everything |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | * @throws \Exception |
166 | 166 | */ |
167 | 167 | public function add($key, $value, $ttl = 0) { |
168 | - $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
168 | + $result = self::$cache->add($this->getPrefix().$key, $value, $ttl); |
|
169 | 169 | if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
170 | 170 | $this->verifyReturnCode(); |
171 | 171 | } |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | */ |
182 | 182 | public function inc($key, $step = 1) { |
183 | 183 | $this->add($key, 0); |
184 | - $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
184 | + $result = self::$cache->increment($this->getPrefix().$key, $step); |
|
185 | 185 | |
186 | 186 | if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
187 | 187 | return false; |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | * @return int | bool |
199 | 199 | */ |
200 | 200 | public function dec($key, $step = 1) { |
201 | - $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
201 | + $result = self::$cache->decrement($this->getPrefix().$key, $step); |
|
202 | 202 | |
203 | 203 | if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
204 | 204 | return false; |