@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | * @return string |
136 | 136 | */ |
137 | 137 | protected function generateTemporaryTableName($name) { |
138 | - return $this->config->getSystemValue('dbtableprefix', 'oc_') . $name . '_' . $this->random->generate(13, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); |
|
138 | + return $this->config->getSystemValue('dbtableprefix', 'oc_').$name.'_'.$this->random->generate(13, ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | /** |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | $indexName = $index->getName(); |
187 | 187 | } else { |
188 | 188 | // avoid conflicts in index names |
189 | - $indexName = $this->config->getSystemValue('dbtableprefix', 'oc_') . $this->random->generate(13, ISecureRandom::CHAR_LOWER); |
|
189 | + $indexName = $this->config->getSystemValue('dbtableprefix', 'oc_').$this->random->generate(13, ISecureRandom::CHAR_LOWER); |
|
190 | 190 | } |
191 | 191 | $newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary()); |
192 | 192 | } |
@@ -271,15 +271,15 @@ discard block |
||
271 | 271 | $quotedSource = $this->connection->quoteIdentifier($sourceName); |
272 | 272 | $quotedTarget = $this->connection->quoteIdentifier($targetName); |
273 | 273 | |
274 | - $this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')'); |
|
275 | - $this->connection->exec('INSERT INTO ' . $quotedTarget . ' SELECT * FROM ' . $quotedSource); |
|
274 | + $this->connection->exec('CREATE TABLE '.$quotedTarget.' (LIKE '.$quotedSource.')'); |
|
275 | + $this->connection->exec('INSERT INTO '.$quotedTarget.' SELECT * FROM '.$quotedSource); |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | /** |
279 | 279 | * @param string $name |
280 | 280 | */ |
281 | 281 | protected function dropTable($name) { |
282 | - $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($name)); |
|
282 | + $this->connection->exec('DROP TABLE '.$this->connection->quoteIdentifier($name)); |
|
283 | 283 | } |
284 | 284 | |
285 | 285 | /** |
@@ -287,14 +287,14 @@ discard block |
||
287 | 287 | * @return string |
288 | 288 | */ |
289 | 289 | protected function convertStatementToScript($statement) { |
290 | - $script = $statement . ';'; |
|
290 | + $script = $statement.';'; |
|
291 | 291 | $script .= PHP_EOL; |
292 | 292 | $script .= PHP_EOL; |
293 | 293 | return $script; |
294 | 294 | } |
295 | 295 | |
296 | 296 | protected function getFilterExpression() { |
297 | - return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
297 | + return '/^'.preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')).'/'; |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | protected function emit($sql, $step, $max) { |
@@ -46,271 +46,271 @@ |
||
46 | 46 | |
47 | 47 | class Migrator { |
48 | 48 | |
49 | - /** @var \Doctrine\DBAL\Connection */ |
|
50 | - protected $connection; |
|
51 | - |
|
52 | - /** @var ISecureRandom */ |
|
53 | - private $random; |
|
54 | - |
|
55 | - /** @var IConfig */ |
|
56 | - protected $config; |
|
57 | - |
|
58 | - /** @var EventDispatcherInterface */ |
|
59 | - private $dispatcher; |
|
60 | - |
|
61 | - /** @var bool */ |
|
62 | - private $noEmit = false; |
|
63 | - |
|
64 | - /** |
|
65 | - * @param \Doctrine\DBAL\Connection $connection |
|
66 | - * @param ISecureRandom $random |
|
67 | - * @param IConfig $config |
|
68 | - * @param EventDispatcherInterface $dispatcher |
|
69 | - */ |
|
70 | - public function __construct(\Doctrine\DBAL\Connection $connection, |
|
71 | - ISecureRandom $random, |
|
72 | - IConfig $config, |
|
73 | - EventDispatcherInterface $dispatcher = null) { |
|
74 | - $this->connection = $connection; |
|
75 | - $this->random = $random; |
|
76 | - $this->config = $config; |
|
77 | - $this->dispatcher = $dispatcher; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
82 | - */ |
|
83 | - public function migrate(Schema $targetSchema) { |
|
84 | - $this->noEmit = true; |
|
85 | - $this->applySchema($targetSchema); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - public function generateChangeScript(Schema $targetSchema) { |
|
93 | - $schemaDiff = $this->getDiff($targetSchema, $this->connection); |
|
94 | - |
|
95 | - $script = ''; |
|
96 | - $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform()); |
|
97 | - foreach ($sqls as $sql) { |
|
98 | - $script .= $this->convertStatementToScript($sql); |
|
99 | - } |
|
100 | - |
|
101 | - return $script; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @param Schema $targetSchema |
|
106 | - * @throws \OC\DB\MigrationException |
|
107 | - */ |
|
108 | - public function checkMigrate(Schema $targetSchema) { |
|
109 | - $this->noEmit = true; |
|
110 | - /**@var \Doctrine\DBAL\Schema\Table[] $tables */ |
|
111 | - $tables = $targetSchema->getTables(); |
|
112 | - $filterExpression = $this->getFilterExpression(); |
|
113 | - $this->connection->getConfiguration()-> |
|
114 | - setFilterSchemaAssetsExpression($filterExpression); |
|
115 | - $existingTables = $this->connection->getSchemaManager()->listTableNames(); |
|
116 | - |
|
117 | - $step = 0; |
|
118 | - foreach ($tables as $table) { |
|
119 | - if (strpos($table->getName(), '.')) { |
|
120 | - list(, $tableName) = explode('.', $table->getName()); |
|
121 | - } else { |
|
122 | - $tableName = $table->getName(); |
|
123 | - } |
|
124 | - $this->emitCheckStep($tableName, $step++, count($tables)); |
|
125 | - // don't need to check for new tables |
|
126 | - if (array_search($tableName, $existingTables) !== false) { |
|
127 | - $this->checkTableMigrate($table); |
|
128 | - } |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Create a unique name for the temporary table |
|
134 | - * |
|
135 | - * @param string $name |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - protected function generateTemporaryTableName($name) { |
|
139 | - return $this->config->getSystemValue('dbtableprefix', 'oc_') . $name . '_' . $this->random->generate(13, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Check the migration of a table on a copy so we can detect errors before messing with the real table |
|
144 | - * |
|
145 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
146 | - * @throws \OC\DB\MigrationException |
|
147 | - */ |
|
148 | - protected function checkTableMigrate(Table $table) { |
|
149 | - $name = $table->getName(); |
|
150 | - $tmpName = $this->generateTemporaryTableName($name); |
|
151 | - |
|
152 | - $this->copyTable($name, $tmpName); |
|
153 | - |
|
154 | - //create the migration schema for the temporary table |
|
155 | - $tmpTable = $this->renameTableSchema($table, $tmpName); |
|
156 | - $schemaConfig = new SchemaConfig(); |
|
157 | - $schemaConfig->setName($this->connection->getDatabase()); |
|
158 | - $schema = new Schema([$tmpTable], [], $schemaConfig); |
|
159 | - |
|
160 | - try { |
|
161 | - $this->applySchema($schema); |
|
162 | - $this->dropTable($tmpName); |
|
163 | - } catch (DBALException $e) { |
|
164 | - // pgsql needs to commit it's failed transaction before doing anything else |
|
165 | - if ($this->connection->isTransactionActive()) { |
|
166 | - $this->connection->commit(); |
|
167 | - } |
|
168 | - $this->dropTable($tmpName); |
|
169 | - throw new MigrationException($table->getName(), $e->getMessage()); |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
175 | - * @param string $newName |
|
176 | - * @return \Doctrine\DBAL\Schema\Table |
|
177 | - */ |
|
178 | - protected function renameTableSchema(Table $table, $newName) { |
|
179 | - /** |
|
180 | - * @var \Doctrine\DBAL\Schema\Index[] $indexes |
|
181 | - */ |
|
182 | - $indexes = $table->getIndexes(); |
|
183 | - $newIndexes = []; |
|
184 | - foreach ($indexes as $index) { |
|
185 | - if ($index->isPrimary()) { |
|
186 | - // do not rename primary key |
|
187 | - $indexName = $index->getName(); |
|
188 | - } else { |
|
189 | - // avoid conflicts in index names |
|
190 | - $indexName = $this->config->getSystemValue('dbtableprefix', 'oc_') . $this->random->generate(13, ISecureRandom::CHAR_LOWER); |
|
191 | - } |
|
192 | - $newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary()); |
|
193 | - } |
|
194 | - |
|
195 | - // foreign keys are not supported so we just set it to an empty array |
|
196 | - return new Table($newName, $table->getColumns(), $newIndexes, [], 0, $table->getOptions()); |
|
197 | - } |
|
198 | - |
|
199 | - public function createSchema() { |
|
200 | - $filterExpression = $this->getFilterExpression(); |
|
201 | - $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
202 | - return $this->connection->getSchemaManager()->createSchema(); |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * @param Schema $targetSchema |
|
207 | - * @param \Doctrine\DBAL\Connection $connection |
|
208 | - * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
209 | - * @throws DBALException |
|
210 | - */ |
|
211 | - protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
212 | - // adjust varchar columns with a length higher then getVarcharMaxLength to clob |
|
213 | - foreach ($targetSchema->getTables() as $table) { |
|
214 | - foreach ($table->getColumns() as $column) { |
|
215 | - if ($column->getType() instanceof StringType) { |
|
216 | - if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) { |
|
217 | - $column->setType(Type::getType('text')); |
|
218 | - $column->setLength(null); |
|
219 | - } |
|
220 | - } |
|
221 | - } |
|
222 | - } |
|
223 | - |
|
224 | - $filterExpression = $this->getFilterExpression(); |
|
225 | - $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
226 | - $sourceSchema = $connection->getSchemaManager()->createSchema(); |
|
227 | - |
|
228 | - // remove tables we don't know about |
|
229 | - foreach ($sourceSchema->getTables() as $table) { |
|
230 | - if (!$targetSchema->hasTable($table->getName())) { |
|
231 | - $sourceSchema->dropTable($table->getName()); |
|
232 | - } |
|
233 | - } |
|
234 | - // remove sequences we don't know about |
|
235 | - foreach ($sourceSchema->getSequences() as $table) { |
|
236 | - if (!$targetSchema->hasSequence($table->getName())) { |
|
237 | - $sourceSchema->dropSequence($table->getName()); |
|
238 | - } |
|
239 | - } |
|
240 | - |
|
241 | - $comparator = new Comparator(); |
|
242 | - return $comparator->compare($sourceSchema, $targetSchema); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
247 | - * @param \Doctrine\DBAL\Connection $connection |
|
248 | - */ |
|
249 | - protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { |
|
250 | - if (is_null($connection)) { |
|
251 | - $connection = $this->connection; |
|
252 | - } |
|
253 | - |
|
254 | - $schemaDiff = $this->getDiff($targetSchema, $connection); |
|
255 | - |
|
256 | - $connection->beginTransaction(); |
|
257 | - $sqls = $schemaDiff->toSql($connection->getDatabasePlatform()); |
|
258 | - $step = 0; |
|
259 | - foreach ($sqls as $sql) { |
|
260 | - $this->emit($sql, $step++, count($sqls)); |
|
261 | - $connection->query($sql); |
|
262 | - } |
|
263 | - $connection->commit(); |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * @param string $sourceName |
|
268 | - * @param string $targetName |
|
269 | - */ |
|
270 | - protected function copyTable($sourceName, $targetName) { |
|
271 | - $quotedSource = $this->connection->quoteIdentifier($sourceName); |
|
272 | - $quotedTarget = $this->connection->quoteIdentifier($targetName); |
|
273 | - |
|
274 | - $this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')'); |
|
275 | - $this->connection->exec('INSERT INTO ' . $quotedTarget . ' SELECT * FROM ' . $quotedSource); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param string $name |
|
280 | - */ |
|
281 | - protected function dropTable($name) { |
|
282 | - $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($name)); |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * @param $statement |
|
287 | - * @return string |
|
288 | - */ |
|
289 | - protected function convertStatementToScript($statement) { |
|
290 | - $script = $statement . ';'; |
|
291 | - $script .= PHP_EOL; |
|
292 | - $script .= PHP_EOL; |
|
293 | - return $script; |
|
294 | - } |
|
295 | - |
|
296 | - protected function getFilterExpression() { |
|
297 | - return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
298 | - } |
|
299 | - |
|
300 | - protected function emit($sql, $step, $max) { |
|
301 | - if ($this->noEmit) { |
|
302 | - return; |
|
303 | - } |
|
304 | - if (is_null($this->dispatcher)) { |
|
305 | - return; |
|
306 | - } |
|
307 | - $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step + 1, $max])); |
|
308 | - } |
|
309 | - |
|
310 | - private function emitCheckStep($tableName, $step, $max) { |
|
311 | - if (is_null($this->dispatcher)) { |
|
312 | - return; |
|
313 | - } |
|
314 | - $this->dispatcher->dispatch('\OC\DB\Migrator::checkTable', new GenericEvent($tableName, [$step + 1, $max])); |
|
315 | - } |
|
49 | + /** @var \Doctrine\DBAL\Connection */ |
|
50 | + protected $connection; |
|
51 | + |
|
52 | + /** @var ISecureRandom */ |
|
53 | + private $random; |
|
54 | + |
|
55 | + /** @var IConfig */ |
|
56 | + protected $config; |
|
57 | + |
|
58 | + /** @var EventDispatcherInterface */ |
|
59 | + private $dispatcher; |
|
60 | + |
|
61 | + /** @var bool */ |
|
62 | + private $noEmit = false; |
|
63 | + |
|
64 | + /** |
|
65 | + * @param \Doctrine\DBAL\Connection $connection |
|
66 | + * @param ISecureRandom $random |
|
67 | + * @param IConfig $config |
|
68 | + * @param EventDispatcherInterface $dispatcher |
|
69 | + */ |
|
70 | + public function __construct(\Doctrine\DBAL\Connection $connection, |
|
71 | + ISecureRandom $random, |
|
72 | + IConfig $config, |
|
73 | + EventDispatcherInterface $dispatcher = null) { |
|
74 | + $this->connection = $connection; |
|
75 | + $this->random = $random; |
|
76 | + $this->config = $config; |
|
77 | + $this->dispatcher = $dispatcher; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
82 | + */ |
|
83 | + public function migrate(Schema $targetSchema) { |
|
84 | + $this->noEmit = true; |
|
85 | + $this->applySchema($targetSchema); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + public function generateChangeScript(Schema $targetSchema) { |
|
93 | + $schemaDiff = $this->getDiff($targetSchema, $this->connection); |
|
94 | + |
|
95 | + $script = ''; |
|
96 | + $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform()); |
|
97 | + foreach ($sqls as $sql) { |
|
98 | + $script .= $this->convertStatementToScript($sql); |
|
99 | + } |
|
100 | + |
|
101 | + return $script; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @param Schema $targetSchema |
|
106 | + * @throws \OC\DB\MigrationException |
|
107 | + */ |
|
108 | + public function checkMigrate(Schema $targetSchema) { |
|
109 | + $this->noEmit = true; |
|
110 | + /**@var \Doctrine\DBAL\Schema\Table[] $tables */ |
|
111 | + $tables = $targetSchema->getTables(); |
|
112 | + $filterExpression = $this->getFilterExpression(); |
|
113 | + $this->connection->getConfiguration()-> |
|
114 | + setFilterSchemaAssetsExpression($filterExpression); |
|
115 | + $existingTables = $this->connection->getSchemaManager()->listTableNames(); |
|
116 | + |
|
117 | + $step = 0; |
|
118 | + foreach ($tables as $table) { |
|
119 | + if (strpos($table->getName(), '.')) { |
|
120 | + list(, $tableName) = explode('.', $table->getName()); |
|
121 | + } else { |
|
122 | + $tableName = $table->getName(); |
|
123 | + } |
|
124 | + $this->emitCheckStep($tableName, $step++, count($tables)); |
|
125 | + // don't need to check for new tables |
|
126 | + if (array_search($tableName, $existingTables) !== false) { |
|
127 | + $this->checkTableMigrate($table); |
|
128 | + } |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Create a unique name for the temporary table |
|
134 | + * |
|
135 | + * @param string $name |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + protected function generateTemporaryTableName($name) { |
|
139 | + return $this->config->getSystemValue('dbtableprefix', 'oc_') . $name . '_' . $this->random->generate(13, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Check the migration of a table on a copy so we can detect errors before messing with the real table |
|
144 | + * |
|
145 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
146 | + * @throws \OC\DB\MigrationException |
|
147 | + */ |
|
148 | + protected function checkTableMigrate(Table $table) { |
|
149 | + $name = $table->getName(); |
|
150 | + $tmpName = $this->generateTemporaryTableName($name); |
|
151 | + |
|
152 | + $this->copyTable($name, $tmpName); |
|
153 | + |
|
154 | + //create the migration schema for the temporary table |
|
155 | + $tmpTable = $this->renameTableSchema($table, $tmpName); |
|
156 | + $schemaConfig = new SchemaConfig(); |
|
157 | + $schemaConfig->setName($this->connection->getDatabase()); |
|
158 | + $schema = new Schema([$tmpTable], [], $schemaConfig); |
|
159 | + |
|
160 | + try { |
|
161 | + $this->applySchema($schema); |
|
162 | + $this->dropTable($tmpName); |
|
163 | + } catch (DBALException $e) { |
|
164 | + // pgsql needs to commit it's failed transaction before doing anything else |
|
165 | + if ($this->connection->isTransactionActive()) { |
|
166 | + $this->connection->commit(); |
|
167 | + } |
|
168 | + $this->dropTable($tmpName); |
|
169 | + throw new MigrationException($table->getName(), $e->getMessage()); |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
175 | + * @param string $newName |
|
176 | + * @return \Doctrine\DBAL\Schema\Table |
|
177 | + */ |
|
178 | + protected function renameTableSchema(Table $table, $newName) { |
|
179 | + /** |
|
180 | + * @var \Doctrine\DBAL\Schema\Index[] $indexes |
|
181 | + */ |
|
182 | + $indexes = $table->getIndexes(); |
|
183 | + $newIndexes = []; |
|
184 | + foreach ($indexes as $index) { |
|
185 | + if ($index->isPrimary()) { |
|
186 | + // do not rename primary key |
|
187 | + $indexName = $index->getName(); |
|
188 | + } else { |
|
189 | + // avoid conflicts in index names |
|
190 | + $indexName = $this->config->getSystemValue('dbtableprefix', 'oc_') . $this->random->generate(13, ISecureRandom::CHAR_LOWER); |
|
191 | + } |
|
192 | + $newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary()); |
|
193 | + } |
|
194 | + |
|
195 | + // foreign keys are not supported so we just set it to an empty array |
|
196 | + return new Table($newName, $table->getColumns(), $newIndexes, [], 0, $table->getOptions()); |
|
197 | + } |
|
198 | + |
|
199 | + public function createSchema() { |
|
200 | + $filterExpression = $this->getFilterExpression(); |
|
201 | + $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
202 | + return $this->connection->getSchemaManager()->createSchema(); |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * @param Schema $targetSchema |
|
207 | + * @param \Doctrine\DBAL\Connection $connection |
|
208 | + * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
209 | + * @throws DBALException |
|
210 | + */ |
|
211 | + protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
212 | + // adjust varchar columns with a length higher then getVarcharMaxLength to clob |
|
213 | + foreach ($targetSchema->getTables() as $table) { |
|
214 | + foreach ($table->getColumns() as $column) { |
|
215 | + if ($column->getType() instanceof StringType) { |
|
216 | + if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) { |
|
217 | + $column->setType(Type::getType('text')); |
|
218 | + $column->setLength(null); |
|
219 | + } |
|
220 | + } |
|
221 | + } |
|
222 | + } |
|
223 | + |
|
224 | + $filterExpression = $this->getFilterExpression(); |
|
225 | + $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
226 | + $sourceSchema = $connection->getSchemaManager()->createSchema(); |
|
227 | + |
|
228 | + // remove tables we don't know about |
|
229 | + foreach ($sourceSchema->getTables() as $table) { |
|
230 | + if (!$targetSchema->hasTable($table->getName())) { |
|
231 | + $sourceSchema->dropTable($table->getName()); |
|
232 | + } |
|
233 | + } |
|
234 | + // remove sequences we don't know about |
|
235 | + foreach ($sourceSchema->getSequences() as $table) { |
|
236 | + if (!$targetSchema->hasSequence($table->getName())) { |
|
237 | + $sourceSchema->dropSequence($table->getName()); |
|
238 | + } |
|
239 | + } |
|
240 | + |
|
241 | + $comparator = new Comparator(); |
|
242 | + return $comparator->compare($sourceSchema, $targetSchema); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
247 | + * @param \Doctrine\DBAL\Connection $connection |
|
248 | + */ |
|
249 | + protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { |
|
250 | + if (is_null($connection)) { |
|
251 | + $connection = $this->connection; |
|
252 | + } |
|
253 | + |
|
254 | + $schemaDiff = $this->getDiff($targetSchema, $connection); |
|
255 | + |
|
256 | + $connection->beginTransaction(); |
|
257 | + $sqls = $schemaDiff->toSql($connection->getDatabasePlatform()); |
|
258 | + $step = 0; |
|
259 | + foreach ($sqls as $sql) { |
|
260 | + $this->emit($sql, $step++, count($sqls)); |
|
261 | + $connection->query($sql); |
|
262 | + } |
|
263 | + $connection->commit(); |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * @param string $sourceName |
|
268 | + * @param string $targetName |
|
269 | + */ |
|
270 | + protected function copyTable($sourceName, $targetName) { |
|
271 | + $quotedSource = $this->connection->quoteIdentifier($sourceName); |
|
272 | + $quotedTarget = $this->connection->quoteIdentifier($targetName); |
|
273 | + |
|
274 | + $this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')'); |
|
275 | + $this->connection->exec('INSERT INTO ' . $quotedTarget . ' SELECT * FROM ' . $quotedSource); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param string $name |
|
280 | + */ |
|
281 | + protected function dropTable($name) { |
|
282 | + $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($name)); |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * @param $statement |
|
287 | + * @return string |
|
288 | + */ |
|
289 | + protected function convertStatementToScript($statement) { |
|
290 | + $script = $statement . ';'; |
|
291 | + $script .= PHP_EOL; |
|
292 | + $script .= PHP_EOL; |
|
293 | + return $script; |
|
294 | + } |
|
295 | + |
|
296 | + protected function getFilterExpression() { |
|
297 | + return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
298 | + } |
|
299 | + |
|
300 | + protected function emit($sql, $step, $max) { |
|
301 | + if ($this->noEmit) { |
|
302 | + return; |
|
303 | + } |
|
304 | + if (is_null($this->dispatcher)) { |
|
305 | + return; |
|
306 | + } |
|
307 | + $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step + 1, $max])); |
|
308 | + } |
|
309 | + |
|
310 | + private function emitCheckStep($tableName, $step, $max) { |
|
311 | + if (is_null($this->dispatcher)) { |
|
312 | + return; |
|
313 | + } |
|
314 | + $this->dispatcher->dispatch('\OC\DB\Migrator::checkTable', new GenericEvent($tableName, [$step + 1, $max])); |
|
315 | + } |
|
316 | 316 | } |
@@ -31,150 +31,150 @@ |
||
31 | 31 | |
32 | 32 | class MDB2SchemaWriter { |
33 | 33 | |
34 | - /** |
|
35 | - * @param string $file |
|
36 | - * @param \OC\DB\Connection $conn |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - public static function saveSchemaToFile($file, \OC\DB\Connection $conn) { |
|
40 | - $config = \OC::$server->getConfig(); |
|
34 | + /** |
|
35 | + * @param string $file |
|
36 | + * @param \OC\DB\Connection $conn |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + public static function saveSchemaToFile($file, \OC\DB\Connection $conn) { |
|
40 | + $config = \OC::$server->getConfig(); |
|
41 | 41 | |
42 | - $xml = new \SimpleXMLElement('<database/>'); |
|
43 | - $xml->addChild('name', $config->getSystemValue('dbname', 'owncloud')); |
|
44 | - $xml->addChild('create', 'true'); |
|
45 | - $xml->addChild('overwrite', 'false'); |
|
46 | - if ($config->getSystemValue('dbtype', 'sqlite') === 'mysql' && $config->getSystemValue('mysql.utf8mb4', false)) { |
|
47 | - $xml->addChild('charset', 'utf8mb4'); |
|
48 | - } else { |
|
49 | - $xml->addChild('charset', 'utf8'); |
|
50 | - } |
|
42 | + $xml = new \SimpleXMLElement('<database/>'); |
|
43 | + $xml->addChild('name', $config->getSystemValue('dbname', 'owncloud')); |
|
44 | + $xml->addChild('create', 'true'); |
|
45 | + $xml->addChild('overwrite', 'false'); |
|
46 | + if ($config->getSystemValue('dbtype', 'sqlite') === 'mysql' && $config->getSystemValue('mysql.utf8mb4', false)) { |
|
47 | + $xml->addChild('charset', 'utf8mb4'); |
|
48 | + } else { |
|
49 | + $xml->addChild('charset', 'utf8'); |
|
50 | + } |
|
51 | 51 | |
52 | - // FIX ME: bloody work around |
|
53 | - if ($config->getSystemValue('dbtype', 'sqlite') === 'oci') { |
|
54 | - $filterExpression = '/^"' . preg_quote($conn->getPrefix()) . '/'; |
|
55 | - } else { |
|
56 | - $filterExpression = '/^' . preg_quote($conn->getPrefix()) . '/'; |
|
57 | - } |
|
58 | - $conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
52 | + // FIX ME: bloody work around |
|
53 | + if ($config->getSystemValue('dbtype', 'sqlite') === 'oci') { |
|
54 | + $filterExpression = '/^"' . preg_quote($conn->getPrefix()) . '/'; |
|
55 | + } else { |
|
56 | + $filterExpression = '/^' . preg_quote($conn->getPrefix()) . '/'; |
|
57 | + } |
|
58 | + $conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
59 | 59 | |
60 | - foreach ($conn->getSchemaManager()->listTables() as $table) { |
|
61 | - self::saveTable($table, $xml->addChild('table')); |
|
62 | - } |
|
63 | - file_put_contents($file, $xml->asXML()); |
|
64 | - return true; |
|
65 | - } |
|
60 | + foreach ($conn->getSchemaManager()->listTables() as $table) { |
|
61 | + self::saveTable($table, $xml->addChild('table')); |
|
62 | + } |
|
63 | + file_put_contents($file, $xml->asXML()); |
|
64 | + return true; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
69 | - * @param \SimpleXMLElement $xml |
|
70 | - */ |
|
71 | - private static function saveTable($table, $xml) { |
|
72 | - $xml->addChild('name', $table->getName()); |
|
73 | - $declaration = $xml->addChild('declaration'); |
|
74 | - foreach ($table->getColumns() as $column) { |
|
75 | - self::saveColumn($column, $declaration->addChild('field')); |
|
76 | - } |
|
77 | - foreach ($table->getIndexes() as $index) { |
|
78 | - if ($index->getName() == 'PRIMARY') { |
|
79 | - $autoincrement = false; |
|
80 | - foreach ($index->getColumns() as $column) { |
|
81 | - if ($table->getColumn($column)->getAutoincrement()) { |
|
82 | - $autoincrement = true; |
|
83 | - } |
|
84 | - } |
|
85 | - if ($autoincrement) { |
|
86 | - continue; |
|
87 | - } |
|
88 | - } |
|
89 | - self::saveIndex($index, $declaration->addChild('index')); |
|
90 | - } |
|
91 | - } |
|
67 | + /** |
|
68 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
69 | + * @param \SimpleXMLElement $xml |
|
70 | + */ |
|
71 | + private static function saveTable($table, $xml) { |
|
72 | + $xml->addChild('name', $table->getName()); |
|
73 | + $declaration = $xml->addChild('declaration'); |
|
74 | + foreach ($table->getColumns() as $column) { |
|
75 | + self::saveColumn($column, $declaration->addChild('field')); |
|
76 | + } |
|
77 | + foreach ($table->getIndexes() as $index) { |
|
78 | + if ($index->getName() == 'PRIMARY') { |
|
79 | + $autoincrement = false; |
|
80 | + foreach ($index->getColumns() as $column) { |
|
81 | + if ($table->getColumn($column)->getAutoincrement()) { |
|
82 | + $autoincrement = true; |
|
83 | + } |
|
84 | + } |
|
85 | + if ($autoincrement) { |
|
86 | + continue; |
|
87 | + } |
|
88 | + } |
|
89 | + self::saveIndex($index, $declaration->addChild('index')); |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * @param Column $column |
|
95 | - * @param \SimpleXMLElement $xml |
|
96 | - */ |
|
97 | - private static function saveColumn($column, $xml) { |
|
98 | - $xml->addChild('name', $column->getName()); |
|
99 | - switch ($column->getType()) { |
|
100 | - case 'SmallInt': |
|
101 | - case 'Integer': |
|
102 | - case 'BigInt': |
|
103 | - $xml->addChild('type', 'integer'); |
|
104 | - $default = $column->getDefault(); |
|
105 | - if (is_null($default) && $column->getAutoincrement()) { |
|
106 | - $default = '0'; |
|
107 | - } |
|
108 | - $xml->addChild('default', $default); |
|
109 | - $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
110 | - if ($column->getAutoincrement()) { |
|
111 | - $xml->addChild('autoincrement', '1'); |
|
112 | - } |
|
113 | - if ($column->getUnsigned()) { |
|
114 | - $xml->addChild('unsigned', 'true'); |
|
115 | - } |
|
116 | - $length = '4'; |
|
117 | - if ($column->getType() == 'SmallInt') { |
|
118 | - $length = '2'; |
|
119 | - } elseif ($column->getType() == 'BigInt') { |
|
120 | - $length = '8'; |
|
121 | - } |
|
122 | - $xml->addChild('length', $length); |
|
123 | - break; |
|
124 | - case 'String': |
|
125 | - $xml->addChild('type', 'text'); |
|
126 | - $default = trim($column->getDefault()); |
|
127 | - if ($default === '') { |
|
128 | - $default = false; |
|
129 | - } |
|
130 | - $xml->addChild('default', $default); |
|
131 | - $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
132 | - $xml->addChild('length', $column->getLength()); |
|
133 | - break; |
|
134 | - case 'Text': |
|
135 | - $xml->addChild('type', 'clob'); |
|
136 | - $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
137 | - break; |
|
138 | - case 'Decimal': |
|
139 | - $xml->addChild('type', 'decimal'); |
|
140 | - $xml->addChild('default', $column->getDefault()); |
|
141 | - $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
142 | - $xml->addChild('length', '15'); |
|
143 | - break; |
|
144 | - case 'Boolean': |
|
145 | - $xml->addChild('type', 'integer'); |
|
146 | - $xml->addChild('default', $column->getDefault()); |
|
147 | - $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
148 | - $xml->addChild('length', '1'); |
|
149 | - break; |
|
150 | - case 'DateTime': |
|
151 | - $xml->addChild('type', 'timestamp'); |
|
152 | - $xml->addChild('default', $column->getDefault()); |
|
153 | - $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
154 | - break; |
|
93 | + /** |
|
94 | + * @param Column $column |
|
95 | + * @param \SimpleXMLElement $xml |
|
96 | + */ |
|
97 | + private static function saveColumn($column, $xml) { |
|
98 | + $xml->addChild('name', $column->getName()); |
|
99 | + switch ($column->getType()) { |
|
100 | + case 'SmallInt': |
|
101 | + case 'Integer': |
|
102 | + case 'BigInt': |
|
103 | + $xml->addChild('type', 'integer'); |
|
104 | + $default = $column->getDefault(); |
|
105 | + if (is_null($default) && $column->getAutoincrement()) { |
|
106 | + $default = '0'; |
|
107 | + } |
|
108 | + $xml->addChild('default', $default); |
|
109 | + $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
110 | + if ($column->getAutoincrement()) { |
|
111 | + $xml->addChild('autoincrement', '1'); |
|
112 | + } |
|
113 | + if ($column->getUnsigned()) { |
|
114 | + $xml->addChild('unsigned', 'true'); |
|
115 | + } |
|
116 | + $length = '4'; |
|
117 | + if ($column->getType() == 'SmallInt') { |
|
118 | + $length = '2'; |
|
119 | + } elseif ($column->getType() == 'BigInt') { |
|
120 | + $length = '8'; |
|
121 | + } |
|
122 | + $xml->addChild('length', $length); |
|
123 | + break; |
|
124 | + case 'String': |
|
125 | + $xml->addChild('type', 'text'); |
|
126 | + $default = trim($column->getDefault()); |
|
127 | + if ($default === '') { |
|
128 | + $default = false; |
|
129 | + } |
|
130 | + $xml->addChild('default', $default); |
|
131 | + $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
132 | + $xml->addChild('length', $column->getLength()); |
|
133 | + break; |
|
134 | + case 'Text': |
|
135 | + $xml->addChild('type', 'clob'); |
|
136 | + $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
137 | + break; |
|
138 | + case 'Decimal': |
|
139 | + $xml->addChild('type', 'decimal'); |
|
140 | + $xml->addChild('default', $column->getDefault()); |
|
141 | + $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
142 | + $xml->addChild('length', '15'); |
|
143 | + break; |
|
144 | + case 'Boolean': |
|
145 | + $xml->addChild('type', 'integer'); |
|
146 | + $xml->addChild('default', $column->getDefault()); |
|
147 | + $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
148 | + $xml->addChild('length', '1'); |
|
149 | + break; |
|
150 | + case 'DateTime': |
|
151 | + $xml->addChild('type', 'timestamp'); |
|
152 | + $xml->addChild('default', $column->getDefault()); |
|
153 | + $xml->addChild('notnull', self::toBool($column->getNotnull())); |
|
154 | + break; |
|
155 | 155 | |
156 | - } |
|
157 | - } |
|
156 | + } |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * @param Index $index |
|
161 | - * @param \SimpleXMLElement $xml |
|
162 | - */ |
|
163 | - private static function saveIndex($index, $xml) { |
|
164 | - $xml->addChild('name', $index->getName()); |
|
165 | - if ($index->isPrimary()) { |
|
166 | - $xml->addChild('primary', 'true'); |
|
167 | - } elseif ($index->isUnique()) { |
|
168 | - $xml->addChild('unique', 'true'); |
|
169 | - } |
|
170 | - foreach ($index->getColumns() as $column) { |
|
171 | - $field = $xml->addChild('field'); |
|
172 | - $field->addChild('name', $column); |
|
173 | - $field->addChild('sorting', 'ascending'); |
|
174 | - } |
|
175 | - } |
|
159 | + /** |
|
160 | + * @param Index $index |
|
161 | + * @param \SimpleXMLElement $xml |
|
162 | + */ |
|
163 | + private static function saveIndex($index, $xml) { |
|
164 | + $xml->addChild('name', $index->getName()); |
|
165 | + if ($index->isPrimary()) { |
|
166 | + $xml->addChild('primary', 'true'); |
|
167 | + } elseif ($index->isUnique()) { |
|
168 | + $xml->addChild('unique', 'true'); |
|
169 | + } |
|
170 | + foreach ($index->getColumns() as $column) { |
|
171 | + $field = $xml->addChild('field'); |
|
172 | + $field->addChild('name', $column); |
|
173 | + $field->addChild('sorting', 'ascending'); |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | - private static function toBool($bool) { |
|
178 | - return $bool ? 'true' : 'false'; |
|
179 | - } |
|
177 | + private static function toBool($bool) { |
|
178 | + return $bool ? 'true' : 'false'; |
|
179 | + } |
|
180 | 180 | } |
@@ -51,9 +51,9 @@ |
||
51 | 51 | |
52 | 52 | // FIX ME: bloody work around |
53 | 53 | if ($config->getSystemValue('dbtype', 'sqlite') === 'oci') { |
54 | - $filterExpression = '/^"' . preg_quote($conn->getPrefix()) . '/'; |
|
54 | + $filterExpression = '/^"'.preg_quote($conn->getPrefix()).'/'; |
|
55 | 55 | } else { |
56 | - $filterExpression = '/^' . preg_quote($conn->getPrefix()) . '/'; |
|
56 | + $filterExpression = '/^'.preg_quote($conn->getPrefix()).'/'; |
|
57 | 57 | } |
58 | 58 | $conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
59 | 59 |
@@ -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,389 +44,389 @@ |
||
44 | 44 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
45 | 45 | |
46 | 46 | class PreviewManager implements IPreview { |
47 | - /** @var IConfig */ |
|
48 | - protected $config; |
|
47 | + /** @var IConfig */ |
|
48 | + protected $config; |
|
49 | 49 | |
50 | - /** @var IRootFolder */ |
|
51 | - protected $rootFolder; |
|
50 | + /** @var IRootFolder */ |
|
51 | + protected $rootFolder; |
|
52 | 52 | |
53 | - /** @var IAppData */ |
|
54 | - protected $appData; |
|
53 | + /** @var IAppData */ |
|
54 | + protected $appData; |
|
55 | 55 | |
56 | - /** @var EventDispatcherInterface */ |
|
57 | - protected $eventDispatcher; |
|
56 | + /** @var EventDispatcherInterface */ |
|
57 | + protected $eventDispatcher; |
|
58 | 58 | |
59 | - /** @var Generator */ |
|
60 | - private $generator; |
|
59 | + /** @var Generator */ |
|
60 | + private $generator; |
|
61 | 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 | - ]; |
|
318 | - |
|
319 | - $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
320 | - Preview\MarkDown::class, |
|
321 | - Preview\MP3::class, |
|
322 | - Preview\TXT::class, |
|
323 | - Preview\OpenDocument::class, |
|
324 | - ], $imageProviders)); |
|
325 | - |
|
326 | - if (in_array(Preview\Image::class, $this->defaultProviders)) { |
|
327 | - $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
328 | - } |
|
329 | - $this->defaultProviders = array_unique($this->defaultProviders); |
|
330 | - return $this->defaultProviders; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Register the default providers (if enabled) |
|
335 | - * |
|
336 | - * @param string $class |
|
337 | - * @param string $mimeType |
|
338 | - */ |
|
339 | - protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
340 | - if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
341 | - $this->registerProvider($mimeType, function () use ($class, $options) { |
|
342 | - return new $class($options); |
|
343 | - }); |
|
344 | - } |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Register the default providers (if enabled) |
|
349 | - */ |
|
350 | - protected function registerCoreProviders() { |
|
351 | - if ($this->registeredCoreProviders) { |
|
352 | - return; |
|
353 | - } |
|
354 | - $this->registeredCoreProviders = true; |
|
355 | - |
|
356 | - $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/'); |
|
357 | - $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/'); |
|
358 | - $this->registerCoreProvider(Preview\PNG::class, '/image\/png/'); |
|
359 | - $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/'); |
|
360 | - $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); |
|
361 | - $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); |
|
362 | - $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); |
|
363 | - $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); |
|
364 | - $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/'); |
|
365 | - $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
366 | - |
|
367 | - // SVG, Office and Bitmap require imagick |
|
368 | - if (extension_loaded('imagick')) { |
|
369 | - $checkImagick = new \Imagick(); |
|
370 | - |
|
371 | - $imagickProviders = [ |
|
372 | - 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], |
|
373 | - 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], |
|
374 | - 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class], |
|
375 | - 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class], |
|
376 | - 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class], |
|
377 | - 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class], |
|
378 | - 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class], |
|
379 | - 'HEIC' => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class], |
|
380 | - ]; |
|
381 | - |
|
382 | - foreach ($imagickProviders as $queryFormat => $provider) { |
|
383 | - $class = $provider['class']; |
|
384 | - if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
385 | - continue; |
|
386 | - } |
|
387 | - |
|
388 | - if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
389 | - $this->registerCoreProvider($class, $provider['mimetype']); |
|
390 | - } |
|
391 | - } |
|
392 | - |
|
393 | - if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
394 | - if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
395 | - $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
396 | - |
|
397 | - if (!$officeFound) { |
|
398 | - //let's see if there is libreoffice or openoffice on this machine |
|
399 | - $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
400 | - $officeFound = !empty($whichLibreOffice); |
|
401 | - if (!$officeFound) { |
|
402 | - $whichOpenOffice = shell_exec('command -v openoffice'); |
|
403 | - $officeFound = !empty($whichOpenOffice); |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - if ($officeFound) { |
|
408 | - $this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/'); |
|
409 | - $this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/'); |
|
410 | - $this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/'); |
|
411 | - $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
412 | - $this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/'); |
|
413 | - } |
|
414 | - } |
|
415 | - } |
|
416 | - } |
|
417 | - |
|
418 | - // Video requires avconv or ffmpeg |
|
419 | - if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) { |
|
420 | - $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
421 | - $ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
422 | - |
|
423 | - if ($avconvBinary || $ffmpegBinary) { |
|
424 | - // FIXME // a bit hacky but didn't want to use subclasses |
|
425 | - \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
426 | - \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
427 | - |
|
428 | - $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/'); |
|
429 | - } |
|
430 | - } |
|
431 | - } |
|
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 | + ]; |
|
318 | + |
|
319 | + $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
320 | + Preview\MarkDown::class, |
|
321 | + Preview\MP3::class, |
|
322 | + Preview\TXT::class, |
|
323 | + Preview\OpenDocument::class, |
|
324 | + ], $imageProviders)); |
|
325 | + |
|
326 | + if (in_array(Preview\Image::class, $this->defaultProviders)) { |
|
327 | + $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
328 | + } |
|
329 | + $this->defaultProviders = array_unique($this->defaultProviders); |
|
330 | + return $this->defaultProviders; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Register the default providers (if enabled) |
|
335 | + * |
|
336 | + * @param string $class |
|
337 | + * @param string $mimeType |
|
338 | + */ |
|
339 | + protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
340 | + if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
341 | + $this->registerProvider($mimeType, function () use ($class, $options) { |
|
342 | + return new $class($options); |
|
343 | + }); |
|
344 | + } |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Register the default providers (if enabled) |
|
349 | + */ |
|
350 | + protected function registerCoreProviders() { |
|
351 | + if ($this->registeredCoreProviders) { |
|
352 | + return; |
|
353 | + } |
|
354 | + $this->registeredCoreProviders = true; |
|
355 | + |
|
356 | + $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/'); |
|
357 | + $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/'); |
|
358 | + $this->registerCoreProvider(Preview\PNG::class, '/image\/png/'); |
|
359 | + $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/'); |
|
360 | + $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); |
|
361 | + $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); |
|
362 | + $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); |
|
363 | + $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); |
|
364 | + $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/'); |
|
365 | + $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
366 | + |
|
367 | + // SVG, Office and Bitmap require imagick |
|
368 | + if (extension_loaded('imagick')) { |
|
369 | + $checkImagick = new \Imagick(); |
|
370 | + |
|
371 | + $imagickProviders = [ |
|
372 | + 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], |
|
373 | + 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], |
|
374 | + 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class], |
|
375 | + 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class], |
|
376 | + 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class], |
|
377 | + 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class], |
|
378 | + 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class], |
|
379 | + 'HEIC' => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class], |
|
380 | + ]; |
|
381 | + |
|
382 | + foreach ($imagickProviders as $queryFormat => $provider) { |
|
383 | + $class = $provider['class']; |
|
384 | + if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
385 | + continue; |
|
386 | + } |
|
387 | + |
|
388 | + if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
389 | + $this->registerCoreProvider($class, $provider['mimetype']); |
|
390 | + } |
|
391 | + } |
|
392 | + |
|
393 | + if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
394 | + if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
395 | + $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
396 | + |
|
397 | + if (!$officeFound) { |
|
398 | + //let's see if there is libreoffice or openoffice on this machine |
|
399 | + $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
400 | + $officeFound = !empty($whichLibreOffice); |
|
401 | + if (!$officeFound) { |
|
402 | + $whichOpenOffice = shell_exec('command -v openoffice'); |
|
403 | + $officeFound = !empty($whichOpenOffice); |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + if ($officeFound) { |
|
408 | + $this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/'); |
|
409 | + $this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/'); |
|
410 | + $this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/'); |
|
411 | + $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
412 | + $this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/'); |
|
413 | + } |
|
414 | + } |
|
415 | + } |
|
416 | + } |
|
417 | + |
|
418 | + // Video requires avconv or ffmpeg |
|
419 | + if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) { |
|
420 | + $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
421 | + $ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
422 | + |
|
423 | + if ($avconvBinary || $ffmpegBinary) { |
|
424 | + // FIXME // a bit hacky but didn't want to use subclasses |
|
425 | + \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
426 | + \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
427 | + |
|
428 | + $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/'); |
|
429 | + } |
|
430 | + } |
|
431 | + } |
|
432 | 432 | } |
@@ -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; |
@@ -27,47 +27,47 @@ |
||
27 | 27 | namespace OC\Memcache; |
28 | 28 | |
29 | 29 | class NullCache extends Cache implements \OCP\IMemcache { |
30 | - public function get($key) { |
|
31 | - return null; |
|
32 | - } |
|
30 | + public function get($key) { |
|
31 | + return null; |
|
32 | + } |
|
33 | 33 | |
34 | - public function set($key, $value, $ttl = 0) { |
|
35 | - return true; |
|
36 | - } |
|
34 | + public function set($key, $value, $ttl = 0) { |
|
35 | + return true; |
|
36 | + } |
|
37 | 37 | |
38 | - public function hasKey($key) { |
|
39 | - return false; |
|
40 | - } |
|
38 | + public function hasKey($key) { |
|
39 | + return false; |
|
40 | + } |
|
41 | 41 | |
42 | - public function remove($key) { |
|
43 | - return true; |
|
44 | - } |
|
42 | + public function remove($key) { |
|
43 | + return true; |
|
44 | + } |
|
45 | 45 | |
46 | - public function add($key, $value, $ttl = 0) { |
|
47 | - return true; |
|
48 | - } |
|
46 | + public function add($key, $value, $ttl = 0) { |
|
47 | + return true; |
|
48 | + } |
|
49 | 49 | |
50 | - public function inc($key, $step = 1) { |
|
51 | - return true; |
|
52 | - } |
|
50 | + public function inc($key, $step = 1) { |
|
51 | + return true; |
|
52 | + } |
|
53 | 53 | |
54 | - public function dec($key, $step = 1) { |
|
55 | - return true; |
|
56 | - } |
|
54 | + public function dec($key, $step = 1) { |
|
55 | + return true; |
|
56 | + } |
|
57 | 57 | |
58 | - public function cas($key, $old, $new) { |
|
59 | - return true; |
|
60 | - } |
|
58 | + public function cas($key, $old, $new) { |
|
59 | + return true; |
|
60 | + } |
|
61 | 61 | |
62 | - public function cad($key, $old) { |
|
63 | - return true; |
|
64 | - } |
|
62 | + public function cad($key, $old) { |
|
63 | + return true; |
|
64 | + } |
|
65 | 65 | |
66 | - public function clear($prefix = '') { |
|
67 | - return true; |
|
68 | - } |
|
66 | + public function clear($prefix = '') { |
|
67 | + return true; |
|
68 | + } |
|
69 | 69 | |
70 | - public static function isAvailable() { |
|
71 | - return true; |
|
72 | - } |
|
70 | + public static function isAvailable() { |
|
71 | + return true; |
|
72 | + } |
|
73 | 73 | } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | use CADTrait; |
38 | 38 | |
39 | 39 | public function get($key) { |
40 | - $result = apcu_fetch($this->getPrefix() . $key, $success); |
|
40 | + $result = apcu_fetch($this->getPrefix().$key, $success); |
|
41 | 41 | if (!$success) { |
42 | 42 | return null; |
43 | 43 | } |
@@ -45,24 +45,24 @@ discard block |
||
45 | 45 | } |
46 | 46 | |
47 | 47 | public function set($key, $value, $ttl = 0) { |
48 | - return apcu_store($this->getPrefix() . $key, $value, $ttl); |
|
48 | + return apcu_store($this->getPrefix().$key, $value, $ttl); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | public function hasKey($key) { |
52 | - return apcu_exists($this->getPrefix() . $key); |
|
52 | + return apcu_exists($this->getPrefix().$key); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | public function remove($key) { |
56 | - return apcu_delete($this->getPrefix() . $key); |
|
56 | + return apcu_delete($this->getPrefix().$key); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | public function clear($prefix = '') { |
60 | - $ns = $this->getPrefix() . $prefix; |
|
60 | + $ns = $this->getPrefix().$prefix; |
|
61 | 61 | $ns = preg_quote($ns, '/'); |
62 | 62 | if (class_exists('\APCIterator')) { |
63 | - $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY); |
|
63 | + $iter = new \APCIterator('user', '/^'.$ns.'/', APC_ITER_KEY); |
|
64 | 64 | } else { |
65 | - $iter = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY); |
|
65 | + $iter = new \APCUIterator('/^'.$ns.'/', APC_ITER_KEY); |
|
66 | 66 | } |
67 | 67 | return apcu_delete($iter); |
68 | 68 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * @return bool |
77 | 77 | */ |
78 | 78 | public function add($key, $value, $ttl = 0) { |
79 | - return apcu_add($this->getPrefix() . $key, $value, $ttl); |
|
79 | + return apcu_add($this->getPrefix().$key, $value, $ttl); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -100,8 +100,8 @@ discard block |
||
100 | 100 | * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
101 | 101 | * for details |
102 | 102 | */ |
103 | - return apcu_exists($this->getPrefix() . $key) |
|
104 | - ? apcu_inc($this->getPrefix() . $key, $step) |
|
103 | + return apcu_exists($this->getPrefix().$key) |
|
104 | + ? apcu_inc($this->getPrefix().$key, $step) |
|
105 | 105 | : false; |
106 | 106 | } |
107 | 107 | |
@@ -125,8 +125,8 @@ discard block |
||
125 | 125 | * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
126 | 126 | * for details |
127 | 127 | */ |
128 | - return apcu_exists($this->getPrefix() . $key) |
|
129 | - ? apcu_dec($this->getPrefix() . $key, $step) |
|
128 | + return apcu_exists($this->getPrefix().$key) |
|
129 | + ? apcu_dec($this->getPrefix().$key, $step) |
|
130 | 130 | : false; |
131 | 131 | } |
132 | 132 | |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | public function cas($key, $old, $new) { |
142 | 142 | // apc only does cas for ints |
143 | 143 | if (is_int($old) and is_int($new)) { |
144 | - return apcu_cas($this->getPrefix() . $key, $old, $new); |
|
144 | + return apcu_cas($this->getPrefix().$key, $old, $new); |
|
145 | 145 | } else { |
146 | 146 | return $this->casEmulated($key, $old, $new); |
147 | 147 | } |
@@ -32,140 +32,140 @@ |
||
32 | 32 | use OCP\IMemcache; |
33 | 33 | |
34 | 34 | class APCu extends Cache implements IMemcache { |
35 | - use CASTrait { |
|
36 | - cas as casEmulated; |
|
37 | - } |
|
35 | + use CASTrait { |
|
36 | + cas as casEmulated; |
|
37 | + } |
|
38 | 38 | |
39 | - use CADTrait; |
|
39 | + use CADTrait; |
|
40 | 40 | |
41 | - public function get($key) { |
|
42 | - $result = apcu_fetch($this->getPrefix() . $key, $success); |
|
43 | - if (!$success) { |
|
44 | - return null; |
|
45 | - } |
|
46 | - return $result; |
|
47 | - } |
|
41 | + public function get($key) { |
|
42 | + $result = apcu_fetch($this->getPrefix() . $key, $success); |
|
43 | + if (!$success) { |
|
44 | + return null; |
|
45 | + } |
|
46 | + return $result; |
|
47 | + } |
|
48 | 48 | |
49 | - public function set($key, $value, $ttl = 0) { |
|
50 | - return apcu_store($this->getPrefix() . $key, $value, $ttl); |
|
51 | - } |
|
49 | + public function set($key, $value, $ttl = 0) { |
|
50 | + return apcu_store($this->getPrefix() . $key, $value, $ttl); |
|
51 | + } |
|
52 | 52 | |
53 | - public function hasKey($key) { |
|
54 | - return apcu_exists($this->getPrefix() . $key); |
|
55 | - } |
|
53 | + public function hasKey($key) { |
|
54 | + return apcu_exists($this->getPrefix() . $key); |
|
55 | + } |
|
56 | 56 | |
57 | - public function remove($key) { |
|
58 | - return apcu_delete($this->getPrefix() . $key); |
|
59 | - } |
|
57 | + public function remove($key) { |
|
58 | + return apcu_delete($this->getPrefix() . $key); |
|
59 | + } |
|
60 | 60 | |
61 | - public function clear($prefix = '') { |
|
62 | - $ns = $this->getPrefix() . $prefix; |
|
63 | - $ns = preg_quote($ns, '/'); |
|
64 | - if (class_exists('\APCIterator')) { |
|
65 | - $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY); |
|
66 | - } else { |
|
67 | - $iter = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY); |
|
68 | - } |
|
69 | - return apcu_delete($iter); |
|
70 | - } |
|
61 | + public function clear($prefix = '') { |
|
62 | + $ns = $this->getPrefix() . $prefix; |
|
63 | + $ns = preg_quote($ns, '/'); |
|
64 | + if (class_exists('\APCIterator')) { |
|
65 | + $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY); |
|
66 | + } else { |
|
67 | + $iter = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY); |
|
68 | + } |
|
69 | + return apcu_delete($iter); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Set a value in the cache if it's not already stored |
|
74 | - * |
|
75 | - * @param string $key |
|
76 | - * @param mixed $value |
|
77 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
78 | - * @return bool |
|
79 | - */ |
|
80 | - public function add($key, $value, $ttl = 0) { |
|
81 | - return apcu_add($this->getPrefix() . $key, $value, $ttl); |
|
82 | - } |
|
72 | + /** |
|
73 | + * Set a value in the cache if it's not already stored |
|
74 | + * |
|
75 | + * @param string $key |
|
76 | + * @param mixed $value |
|
77 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | + public function add($key, $value, $ttl = 0) { |
|
81 | + return apcu_add($this->getPrefix() . $key, $value, $ttl); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Increase a stored number |
|
86 | - * |
|
87 | - * @param string $key |
|
88 | - * @param int $step |
|
89 | - * @return int | bool |
|
90 | - */ |
|
91 | - public function inc($key, $step = 1) { |
|
92 | - $this->add($key, 0); |
|
93 | - /** |
|
94 | - * TODO - hack around a PHP 7 specific issue in APCu |
|
95 | - * |
|
96 | - * on PHP 7 the apcu_inc method on a non-existing object will increment |
|
97 | - * "0" and result in "1" as value - therefore we check for existence |
|
98 | - * first |
|
99 | - * |
|
100 | - * on PHP 5.6 this is not the case |
|
101 | - * |
|
102 | - * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
103 | - * for details |
|
104 | - */ |
|
105 | - return apcu_exists($this->getPrefix() . $key) |
|
106 | - ? apcu_inc($this->getPrefix() . $key, $step) |
|
107 | - : false; |
|
108 | - } |
|
84 | + /** |
|
85 | + * Increase a stored number |
|
86 | + * |
|
87 | + * @param string $key |
|
88 | + * @param int $step |
|
89 | + * @return int | bool |
|
90 | + */ |
|
91 | + public function inc($key, $step = 1) { |
|
92 | + $this->add($key, 0); |
|
93 | + /** |
|
94 | + * TODO - hack around a PHP 7 specific issue in APCu |
|
95 | + * |
|
96 | + * on PHP 7 the apcu_inc method on a non-existing object will increment |
|
97 | + * "0" and result in "1" as value - therefore we check for existence |
|
98 | + * first |
|
99 | + * |
|
100 | + * on PHP 5.6 this is not the case |
|
101 | + * |
|
102 | + * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
103 | + * for details |
|
104 | + */ |
|
105 | + return apcu_exists($this->getPrefix() . $key) |
|
106 | + ? apcu_inc($this->getPrefix() . $key, $step) |
|
107 | + : false; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Decrease a stored number |
|
112 | - * |
|
113 | - * @param string $key |
|
114 | - * @param int $step |
|
115 | - * @return int | bool |
|
116 | - */ |
|
117 | - public function dec($key, $step = 1) { |
|
118 | - /** |
|
119 | - * TODO - hack around a PHP 7 specific issue in APCu |
|
120 | - * |
|
121 | - * on PHP 7 the apcu_dec method on a non-existing object will decrement |
|
122 | - * "0" and result in "-1" as value - therefore we check for existence |
|
123 | - * first |
|
124 | - * |
|
125 | - * on PHP 5.6 this is not the case |
|
126 | - * |
|
127 | - * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
128 | - * for details |
|
129 | - */ |
|
130 | - return apcu_exists($this->getPrefix() . $key) |
|
131 | - ? apcu_dec($this->getPrefix() . $key, $step) |
|
132 | - : false; |
|
133 | - } |
|
110 | + /** |
|
111 | + * Decrease a stored number |
|
112 | + * |
|
113 | + * @param string $key |
|
114 | + * @param int $step |
|
115 | + * @return int | bool |
|
116 | + */ |
|
117 | + public function dec($key, $step = 1) { |
|
118 | + /** |
|
119 | + * TODO - hack around a PHP 7 specific issue in APCu |
|
120 | + * |
|
121 | + * on PHP 7 the apcu_dec method on a non-existing object will decrement |
|
122 | + * "0" and result in "-1" as value - therefore we check for existence |
|
123 | + * first |
|
124 | + * |
|
125 | + * on PHP 5.6 this is not the case |
|
126 | + * |
|
127 | + * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
128 | + * for details |
|
129 | + */ |
|
130 | + return apcu_exists($this->getPrefix() . $key) |
|
131 | + ? apcu_dec($this->getPrefix() . $key, $step) |
|
132 | + : false; |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Compare and set |
|
137 | - * |
|
138 | - * @param string $key |
|
139 | - * @param mixed $old |
|
140 | - * @param mixed $new |
|
141 | - * @return bool |
|
142 | - */ |
|
143 | - public function cas($key, $old, $new) { |
|
144 | - // apc only does cas for ints |
|
145 | - if (is_int($old) and is_int($new)) { |
|
146 | - return apcu_cas($this->getPrefix() . $key, $old, $new); |
|
147 | - } else { |
|
148 | - return $this->casEmulated($key, $old, $new); |
|
149 | - } |
|
150 | - } |
|
135 | + /** |
|
136 | + * Compare and set |
|
137 | + * |
|
138 | + * @param string $key |
|
139 | + * @param mixed $old |
|
140 | + * @param mixed $new |
|
141 | + * @return bool |
|
142 | + */ |
|
143 | + public function cas($key, $old, $new) { |
|
144 | + // apc only does cas for ints |
|
145 | + if (is_int($old) and is_int($new)) { |
|
146 | + return apcu_cas($this->getPrefix() . $key, $old, $new); |
|
147 | + } else { |
|
148 | + return $this->casEmulated($key, $old, $new); |
|
149 | + } |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @return bool |
|
154 | - */ |
|
155 | - public static function isAvailable() { |
|
156 | - if (!extension_loaded('apcu')) { |
|
157 | - return false; |
|
158 | - } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enabled')) { |
|
159 | - return false; |
|
160 | - } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enable_cli') && \OC::$CLI) { |
|
161 | - return false; |
|
162 | - } elseif ( |
|
163 | - version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 && |
|
164 | - version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1 |
|
165 | - ) { |
|
166 | - return false; |
|
167 | - } else { |
|
168 | - return true; |
|
169 | - } |
|
170 | - } |
|
152 | + /** |
|
153 | + * @return bool |
|
154 | + */ |
|
155 | + public static function isAvailable() { |
|
156 | + if (!extension_loaded('apcu')) { |
|
157 | + return false; |
|
158 | + } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enabled')) { |
|
159 | + return false; |
|
160 | + } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enable_cli') && \OC::$CLI) { |
|
161 | + return false; |
|
162 | + } elseif ( |
|
163 | + version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 && |
|
164 | + version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1 |
|
165 | + ) { |
|
166 | + return false; |
|
167 | + } else { |
|
168 | + return true; |
|
169 | + } |
|
170 | + } |
|
171 | 171 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | IUserSession $userSession |
61 | 61 | ) { |
62 | 62 | $this->user = $user; |
63 | - $this->isAdmin = (bool)$isAdmin; |
|
63 | + $this->isAdmin = (bool) $isAdmin; |
|
64 | 64 | $this->groupManager = $groupManager; |
65 | 65 | $this->userSession = $userSession; |
66 | 66 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * @return array |
77 | 77 | */ |
78 | 78 | public function get($groupSearch = '', $userSearch = '') { |
79 | - $key = $groupSearch . '::' . $userSearch; |
|
79 | + $key = $groupSearch.'::'.$userSearch; |
|
80 | 80 | if (isset($this->metaData[$key])) { |
81 | 81 | return $this->metaData[$key]; |
82 | 82 | } |
@@ -35,175 +35,175 @@ |
||
35 | 35 | use OCP\IUserSession; |
36 | 36 | |
37 | 37 | class MetaData { |
38 | - public const SORT_NONE = 0; |
|
39 | - public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
40 | - public const SORT_GROUPNAME = 2; |
|
41 | - |
|
42 | - /** @var string */ |
|
43 | - protected $user; |
|
44 | - /** @var bool */ |
|
45 | - protected $isAdmin; |
|
46 | - /** @var array */ |
|
47 | - protected $metaData = []; |
|
48 | - /** @var GroupManager */ |
|
49 | - protected $groupManager; |
|
50 | - /** @var bool */ |
|
51 | - protected $sorting = false; |
|
52 | - /** @var IUserSession */ |
|
53 | - protected $userSession; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param string $user the uid of the current user |
|
57 | - * @param bool $isAdmin whether the current users is an admin |
|
58 | - * @param IGroupManager $groupManager |
|
59 | - * @param IUserSession $userSession |
|
60 | - */ |
|
61 | - public function __construct( |
|
62 | - $user, |
|
63 | - $isAdmin, |
|
64 | - IGroupManager $groupManager, |
|
65 | - IUserSession $userSession |
|
66 | - ) { |
|
67 | - $this->user = $user; |
|
68 | - $this->isAdmin = (bool)$isAdmin; |
|
69 | - $this->groupManager = $groupManager; |
|
70 | - $this->userSession = $userSession; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * returns an array with meta data about all available groups |
|
75 | - * the array is structured as follows: |
|
76 | - * [0] array containing meta data about admin groups |
|
77 | - * [1] array containing meta data about unprivileged groups |
|
78 | - * @param string $groupSearch only effective when instance was created with |
|
79 | - * isAdmin being true |
|
80 | - * @param string $userSearch the pattern users are search for |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - public function get($groupSearch = '', $userSearch = '') { |
|
84 | - $key = $groupSearch . '::' . $userSearch; |
|
85 | - if (isset($this->metaData[$key])) { |
|
86 | - return $this->metaData[$key]; |
|
87 | - } |
|
88 | - |
|
89 | - $adminGroups = []; |
|
90 | - $groups = []; |
|
91 | - $sortGroupsIndex = 0; |
|
92 | - $sortGroupsKeys = []; |
|
93 | - $sortAdminGroupsIndex = 0; |
|
94 | - $sortAdminGroupsKeys = []; |
|
95 | - |
|
96 | - foreach ($this->getGroups($groupSearch) as $group) { |
|
97 | - $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
98 | - if (strtolower($group->getGID()) !== 'admin') { |
|
99 | - $this->addEntry( |
|
100 | - $groups, |
|
101 | - $sortGroupsKeys, |
|
102 | - $sortGroupsIndex, |
|
103 | - $groupMetaData); |
|
104 | - } else { |
|
105 | - //admin group is hard coded to 'admin' for now. In future, |
|
106 | - //backends may define admin groups too. Then the if statement |
|
107 | - //has to be adjusted accordingly. |
|
108 | - $this->addEntry( |
|
109 | - $adminGroups, |
|
110 | - $sortAdminGroupsKeys, |
|
111 | - $sortAdminGroupsIndex, |
|
112 | - $groupMetaData); |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - //whether sorting is necessary is will be checked in sort() |
|
117 | - $this->sort($groups, $sortGroupsKeys); |
|
118 | - $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
119 | - |
|
120 | - $this->metaData[$key] = [$adminGroups, $groups]; |
|
121 | - return $this->metaData[$key]; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * sets the sort mode, see SORT_* constants for supported modes |
|
126 | - * |
|
127 | - * @param int $sortMode |
|
128 | - */ |
|
129 | - public function setSorting($sortMode) { |
|
130 | - switch ($sortMode) { |
|
131 | - case self::SORT_USERCOUNT: |
|
132 | - case self::SORT_GROUPNAME: |
|
133 | - $this->sorting = $sortMode; |
|
134 | - break; |
|
135 | - |
|
136 | - default: |
|
137 | - $this->sorting = self::SORT_NONE; |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * adds an group entry to the resulting array |
|
143 | - * @param array $entries the resulting array, by reference |
|
144 | - * @param array $sortKeys the sort key array, by reference |
|
145 | - * @param int $sortIndex the sort key index, by reference |
|
146 | - * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
147 | - */ |
|
148 | - private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
149 | - $entries[] = $data; |
|
150 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
151 | - $sortKeys[$sortIndex] = $data['usercount']; |
|
152 | - $sortIndex++; |
|
153 | - } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
154 | - $sortKeys[$sortIndex] = $data['name']; |
|
155 | - $sortIndex++; |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * creates an array containing the group meta data |
|
161 | - * @param \OCP\IGroup $group |
|
162 | - * @param string $userSearch |
|
163 | - * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
164 | - */ |
|
165 | - private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
166 | - return [ |
|
167 | - 'id' => $group->getGID(), |
|
168 | - 'name' => $group->getDisplayName(), |
|
169 | - 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
170 | - 'disabled' => $group->countDisabled(), |
|
171 | - 'canAdd' => $group->canAddUser(), |
|
172 | - 'canRemove' => $group->canRemoveUser(), |
|
173 | - ]; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * sorts the result array, if applicable |
|
178 | - * @param array $entries the result array, by reference |
|
179 | - * @param array $sortKeys the array containing the sort keys |
|
180 | - * @param return null |
|
181 | - */ |
|
182 | - private function sort(&$entries, $sortKeys) { |
|
183 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
184 | - array_multisort($sortKeys, SORT_DESC, $entries); |
|
185 | - } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
186 | - array_multisort($sortKeys, SORT_ASC, $entries); |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * returns the available groups |
|
192 | - * @param string $search a search string |
|
193 | - * @return \OCP\IGroup[] |
|
194 | - */ |
|
195 | - public function getGroups($search = '') { |
|
196 | - if ($this->isAdmin) { |
|
197 | - return $this->groupManager->search($search); |
|
198 | - } else { |
|
199 | - $userObject = $this->userSession->getUser(); |
|
200 | - if ($userObject !== null) { |
|
201 | - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
202 | - } else { |
|
203 | - $groups = []; |
|
204 | - } |
|
205 | - |
|
206 | - return $groups; |
|
207 | - } |
|
208 | - } |
|
38 | + public const SORT_NONE = 0; |
|
39 | + public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
40 | + public const SORT_GROUPNAME = 2; |
|
41 | + |
|
42 | + /** @var string */ |
|
43 | + protected $user; |
|
44 | + /** @var bool */ |
|
45 | + protected $isAdmin; |
|
46 | + /** @var array */ |
|
47 | + protected $metaData = []; |
|
48 | + /** @var GroupManager */ |
|
49 | + protected $groupManager; |
|
50 | + /** @var bool */ |
|
51 | + protected $sorting = false; |
|
52 | + /** @var IUserSession */ |
|
53 | + protected $userSession; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param string $user the uid of the current user |
|
57 | + * @param bool $isAdmin whether the current users is an admin |
|
58 | + * @param IGroupManager $groupManager |
|
59 | + * @param IUserSession $userSession |
|
60 | + */ |
|
61 | + public function __construct( |
|
62 | + $user, |
|
63 | + $isAdmin, |
|
64 | + IGroupManager $groupManager, |
|
65 | + IUserSession $userSession |
|
66 | + ) { |
|
67 | + $this->user = $user; |
|
68 | + $this->isAdmin = (bool)$isAdmin; |
|
69 | + $this->groupManager = $groupManager; |
|
70 | + $this->userSession = $userSession; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * returns an array with meta data about all available groups |
|
75 | + * the array is structured as follows: |
|
76 | + * [0] array containing meta data about admin groups |
|
77 | + * [1] array containing meta data about unprivileged groups |
|
78 | + * @param string $groupSearch only effective when instance was created with |
|
79 | + * isAdmin being true |
|
80 | + * @param string $userSearch the pattern users are search for |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + public function get($groupSearch = '', $userSearch = '') { |
|
84 | + $key = $groupSearch . '::' . $userSearch; |
|
85 | + if (isset($this->metaData[$key])) { |
|
86 | + return $this->metaData[$key]; |
|
87 | + } |
|
88 | + |
|
89 | + $adminGroups = []; |
|
90 | + $groups = []; |
|
91 | + $sortGroupsIndex = 0; |
|
92 | + $sortGroupsKeys = []; |
|
93 | + $sortAdminGroupsIndex = 0; |
|
94 | + $sortAdminGroupsKeys = []; |
|
95 | + |
|
96 | + foreach ($this->getGroups($groupSearch) as $group) { |
|
97 | + $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
98 | + if (strtolower($group->getGID()) !== 'admin') { |
|
99 | + $this->addEntry( |
|
100 | + $groups, |
|
101 | + $sortGroupsKeys, |
|
102 | + $sortGroupsIndex, |
|
103 | + $groupMetaData); |
|
104 | + } else { |
|
105 | + //admin group is hard coded to 'admin' for now. In future, |
|
106 | + //backends may define admin groups too. Then the if statement |
|
107 | + //has to be adjusted accordingly. |
|
108 | + $this->addEntry( |
|
109 | + $adminGroups, |
|
110 | + $sortAdminGroupsKeys, |
|
111 | + $sortAdminGroupsIndex, |
|
112 | + $groupMetaData); |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + //whether sorting is necessary is will be checked in sort() |
|
117 | + $this->sort($groups, $sortGroupsKeys); |
|
118 | + $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
119 | + |
|
120 | + $this->metaData[$key] = [$adminGroups, $groups]; |
|
121 | + return $this->metaData[$key]; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * sets the sort mode, see SORT_* constants for supported modes |
|
126 | + * |
|
127 | + * @param int $sortMode |
|
128 | + */ |
|
129 | + public function setSorting($sortMode) { |
|
130 | + switch ($sortMode) { |
|
131 | + case self::SORT_USERCOUNT: |
|
132 | + case self::SORT_GROUPNAME: |
|
133 | + $this->sorting = $sortMode; |
|
134 | + break; |
|
135 | + |
|
136 | + default: |
|
137 | + $this->sorting = self::SORT_NONE; |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * adds an group entry to the resulting array |
|
143 | + * @param array $entries the resulting array, by reference |
|
144 | + * @param array $sortKeys the sort key array, by reference |
|
145 | + * @param int $sortIndex the sort key index, by reference |
|
146 | + * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
147 | + */ |
|
148 | + private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
149 | + $entries[] = $data; |
|
150 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
151 | + $sortKeys[$sortIndex] = $data['usercount']; |
|
152 | + $sortIndex++; |
|
153 | + } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
154 | + $sortKeys[$sortIndex] = $data['name']; |
|
155 | + $sortIndex++; |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * creates an array containing the group meta data |
|
161 | + * @param \OCP\IGroup $group |
|
162 | + * @param string $userSearch |
|
163 | + * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
164 | + */ |
|
165 | + private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
166 | + return [ |
|
167 | + 'id' => $group->getGID(), |
|
168 | + 'name' => $group->getDisplayName(), |
|
169 | + 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
170 | + 'disabled' => $group->countDisabled(), |
|
171 | + 'canAdd' => $group->canAddUser(), |
|
172 | + 'canRemove' => $group->canRemoveUser(), |
|
173 | + ]; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * sorts the result array, if applicable |
|
178 | + * @param array $entries the result array, by reference |
|
179 | + * @param array $sortKeys the array containing the sort keys |
|
180 | + * @param return null |
|
181 | + */ |
|
182 | + private function sort(&$entries, $sortKeys) { |
|
183 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
184 | + array_multisort($sortKeys, SORT_DESC, $entries); |
|
185 | + } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
186 | + array_multisort($sortKeys, SORT_ASC, $entries); |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * returns the available groups |
|
192 | + * @param string $search a search string |
|
193 | + * @return \OCP\IGroup[] |
|
194 | + */ |
|
195 | + public function getGroups($search = '') { |
|
196 | + if ($this->isAdmin) { |
|
197 | + return $this->groupManager->search($search); |
|
198 | + } else { |
|
199 | + $userObject = $this->userSession->getUser(); |
|
200 | + if ($userObject !== null) { |
|
201 | + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
202 | + } else { |
|
203 | + $groups = []; |
|
204 | + } |
|
205 | + |
|
206 | + return $groups; |
|
207 | + } |
|
208 | + } |
|
209 | 209 | } |
@@ -67,7 +67,7 @@ |
||
67 | 67 | * compared with \OC\Group\Backend::CREATE_GROUP etc. |
68 | 68 | */ |
69 | 69 | public function implementsActions($actions) { |
70 | - return (bool)($this->getSupportedActions() & $actions); |
|
70 | + return (bool) ($this->getSupportedActions() & $actions); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | /** |
@@ -29,107 +29,107 @@ |
||
29 | 29 | * Abstract base class for user management |
30 | 30 | */ |
31 | 31 | abstract class Backend implements \OCP\GroupInterface { |
32 | - /** |
|
33 | - * error code for functions not provided by the group backend |
|
34 | - */ |
|
35 | - public const NOT_IMPLEMENTED = -501; |
|
32 | + /** |
|
33 | + * error code for functions not provided by the group backend |
|
34 | + */ |
|
35 | + public const NOT_IMPLEMENTED = -501; |
|
36 | 36 | |
37 | - protected $possibleActions = [ |
|
38 | - self::CREATE_GROUP => 'createGroup', |
|
39 | - self::DELETE_GROUP => 'deleteGroup', |
|
40 | - self::ADD_TO_GROUP => 'addToGroup', |
|
41 | - self::REMOVE_FROM_GOUP => 'removeFromGroup', |
|
42 | - self::COUNT_USERS => 'countUsersInGroup', |
|
43 | - self::GROUP_DETAILS => 'getGroupDetails', |
|
44 | - self::IS_ADMIN => 'isAdmin', |
|
45 | - ]; |
|
37 | + protected $possibleActions = [ |
|
38 | + self::CREATE_GROUP => 'createGroup', |
|
39 | + self::DELETE_GROUP => 'deleteGroup', |
|
40 | + self::ADD_TO_GROUP => 'addToGroup', |
|
41 | + self::REMOVE_FROM_GOUP => 'removeFromGroup', |
|
42 | + self::COUNT_USERS => 'countUsersInGroup', |
|
43 | + self::GROUP_DETAILS => 'getGroupDetails', |
|
44 | + self::IS_ADMIN => 'isAdmin', |
|
45 | + ]; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Get all supported actions |
|
49 | - * @return int bitwise-or'ed actions |
|
50 | - * |
|
51 | - * Returns the supported actions as int to be |
|
52 | - * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
53 | - */ |
|
54 | - public function getSupportedActions() { |
|
55 | - $actions = 0; |
|
56 | - foreach ($this->possibleActions as $action => $methodName) { |
|
57 | - if (method_exists($this, $methodName)) { |
|
58 | - $actions |= $action; |
|
59 | - } |
|
60 | - } |
|
47 | + /** |
|
48 | + * Get all supported actions |
|
49 | + * @return int bitwise-or'ed actions |
|
50 | + * |
|
51 | + * Returns the supported actions as int to be |
|
52 | + * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
53 | + */ |
|
54 | + public function getSupportedActions() { |
|
55 | + $actions = 0; |
|
56 | + foreach ($this->possibleActions as $action => $methodName) { |
|
57 | + if (method_exists($this, $methodName)) { |
|
58 | + $actions |= $action; |
|
59 | + } |
|
60 | + } |
|
61 | 61 | |
62 | - return $actions; |
|
63 | - } |
|
62 | + return $actions; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Check if backend implements actions |
|
67 | - * @param int $actions bitwise-or'ed actions |
|
68 | - * @return bool |
|
69 | - * |
|
70 | - * Returns the supported actions as int to be |
|
71 | - * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
72 | - */ |
|
73 | - public function implementsActions($actions) { |
|
74 | - return (bool)($this->getSupportedActions() & $actions); |
|
75 | - } |
|
65 | + /** |
|
66 | + * Check if backend implements actions |
|
67 | + * @param int $actions bitwise-or'ed actions |
|
68 | + * @return bool |
|
69 | + * |
|
70 | + * Returns the supported actions as int to be |
|
71 | + * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
72 | + */ |
|
73 | + public function implementsActions($actions) { |
|
74 | + return (bool)($this->getSupportedActions() & $actions); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * is user in group? |
|
79 | - * @param string $uid uid of the user |
|
80 | - * @param string $gid gid of the group |
|
81 | - * @return bool |
|
82 | - * |
|
83 | - * Checks whether the user is member of a group or not. |
|
84 | - */ |
|
85 | - public function inGroup($uid, $gid) { |
|
86 | - return in_array($gid, $this->getUserGroups($uid)); |
|
87 | - } |
|
77 | + /** |
|
78 | + * is user in group? |
|
79 | + * @param string $uid uid of the user |
|
80 | + * @param string $gid gid of the group |
|
81 | + * @return bool |
|
82 | + * |
|
83 | + * Checks whether the user is member of a group or not. |
|
84 | + */ |
|
85 | + public function inGroup($uid, $gid) { |
|
86 | + return in_array($gid, $this->getUserGroups($uid)); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Get all groups a user belongs to |
|
91 | - * @param string $uid Name of the user |
|
92 | - * @return array an array of group names |
|
93 | - * |
|
94 | - * This function fetches all groups a user belongs to. It does not check |
|
95 | - * if the user exists at all. |
|
96 | - */ |
|
97 | - public function getUserGroups($uid) { |
|
98 | - return []; |
|
99 | - } |
|
89 | + /** |
|
90 | + * Get all groups a user belongs to |
|
91 | + * @param string $uid Name of the user |
|
92 | + * @return array an array of group names |
|
93 | + * |
|
94 | + * This function fetches all groups a user belongs to. It does not check |
|
95 | + * if the user exists at all. |
|
96 | + */ |
|
97 | + public function getUserGroups($uid) { |
|
98 | + return []; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * get a list of all groups |
|
103 | - * @param string $search |
|
104 | - * @param int $limit |
|
105 | - * @param int $offset |
|
106 | - * @return array an array of group names |
|
107 | - * |
|
108 | - * Returns a list with all groups |
|
109 | - */ |
|
101 | + /** |
|
102 | + * get a list of all groups |
|
103 | + * @param string $search |
|
104 | + * @param int $limit |
|
105 | + * @param int $offset |
|
106 | + * @return array an array of group names |
|
107 | + * |
|
108 | + * Returns a list with all groups |
|
109 | + */ |
|
110 | 110 | |
111 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
112 | - return []; |
|
113 | - } |
|
111 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
112 | + return []; |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * check if a group exists |
|
117 | - * @param string $gid |
|
118 | - * @return bool |
|
119 | - */ |
|
120 | - public function groupExists($gid) { |
|
121 | - return in_array($gid, $this->getGroups($gid, 1)); |
|
122 | - } |
|
115 | + /** |
|
116 | + * check if a group exists |
|
117 | + * @param string $gid |
|
118 | + * @return bool |
|
119 | + */ |
|
120 | + public function groupExists($gid) { |
|
121 | + return in_array($gid, $this->getGroups($gid, 1)); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * get a list of all users in a group |
|
126 | - * @param string $gid |
|
127 | - * @param string $search |
|
128 | - * @param int $limit |
|
129 | - * @param int $offset |
|
130 | - * @return array an array of user ids |
|
131 | - */ |
|
132 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
133 | - return []; |
|
134 | - } |
|
124 | + /** |
|
125 | + * get a list of all users in a group |
|
126 | + * @param string $gid |
|
127 | + * @param string $search |
|
128 | + * @param int $limit |
|
129 | + * @param int $offset |
|
130 | + * @return array an array of user ids |
|
131 | + */ |
|
132 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
133 | + return []; |
|
134 | + } |
|
135 | 135 | } |