@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function getTableNamesWithoutPrefix() { |
64 | 64 | $tableNames = $this->schema->getTableNames(); |
65 | - return array_map(function ($tableName) { |
|
65 | + return array_map(function($tableName) { |
|
66 | 66 | if (strpos($tableName, $this->connection->getPrefix()) === 0) { |
67 | 67 | return substr($tableName, strlen($this->connection->getPrefix())); |
68 | 68 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * @throws \Doctrine\DBAL\Schema\SchemaException |
81 | 81 | */ |
82 | 82 | public function getTable($tableName) { |
83 | - return $this->schema->getTable($this->connection->getPrefix() . $tableName); |
|
83 | + return $this->schema->getTable($this->connection->getPrefix().$tableName); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * @return boolean |
92 | 92 | */ |
93 | 93 | public function hasTable($tableName) { |
94 | - return $this->schema->hasTable($this->connection->getPrefix() . $tableName); |
|
94 | + return $this->schema->hasTable($this->connection->getPrefix().$tableName); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | * @return \Doctrine\DBAL\Schema\Table |
102 | 102 | */ |
103 | 103 | public function createTable($tableName) { |
104 | - return $this->schema->createTable($this->connection->getPrefix() . $tableName); |
|
104 | + return $this->schema->createTable($this->connection->getPrefix().$tableName); |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | /** |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | */ |
126 | 126 | public function dropTable($tableName) { |
127 | 127 | $this->tablesToDelete[$tableName] = true; |
128 | - return $this->schema->dropTable($this->connection->getPrefix() . $tableName); |
|
128 | + return $this->schema->dropTable($this->connection->getPrefix().$tableName); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |
@@ -29,107 +29,107 @@ |
||
29 | 29 | |
30 | 30 | class SchemaWrapper implements ISchemaWrapper { |
31 | 31 | |
32 | - /** @var IDBConnection|Connection */ |
|
33 | - protected $connection; |
|
34 | - |
|
35 | - /** @var Schema */ |
|
36 | - protected $schema; |
|
37 | - |
|
38 | - /** @var array */ |
|
39 | - protected $tablesToDelete = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * @param IDBConnection $connection |
|
43 | - */ |
|
44 | - public function __construct(IDBConnection $connection) { |
|
45 | - $this->connection = $connection; |
|
46 | - $this->schema = $this->connection->createSchema(); |
|
47 | - } |
|
48 | - |
|
49 | - public function getWrappedSchema() { |
|
50 | - return $this->schema; |
|
51 | - } |
|
52 | - |
|
53 | - public function performDropTableCalls() { |
|
54 | - foreach ($this->tablesToDelete as $tableName => $true) { |
|
55 | - $this->connection->dropTable($tableName); |
|
56 | - unset($this->tablesToDelete[$tableName]); |
|
57 | - } |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Gets all table names |
|
62 | - * |
|
63 | - * @return array |
|
64 | - */ |
|
65 | - public function getTableNamesWithoutPrefix() { |
|
66 | - $tableNames = $this->schema->getTableNames(); |
|
67 | - return array_map(function ($tableName) { |
|
68 | - if (strpos($tableName, $this->connection->getPrefix()) === 0) { |
|
69 | - return substr($tableName, strlen($this->connection->getPrefix())); |
|
70 | - } |
|
71 | - |
|
72 | - return $tableName; |
|
73 | - }, $tableNames); |
|
74 | - } |
|
75 | - |
|
76 | - // Overwritten methods |
|
77 | - |
|
78 | - /** |
|
79 | - * @return array |
|
80 | - */ |
|
81 | - public function getTableNames() { |
|
82 | - return $this->schema->getTableNames(); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param string $tableName |
|
87 | - * |
|
88 | - * @return \Doctrine\DBAL\Schema\Table |
|
89 | - * @throws \Doctrine\DBAL\Schema\SchemaException |
|
90 | - */ |
|
91 | - public function getTable($tableName) { |
|
92 | - return $this->schema->getTable($this->connection->getPrefix() . $tableName); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Does this schema have a table with the given name? |
|
97 | - * |
|
98 | - * @param string $tableName |
|
99 | - * |
|
100 | - * @return boolean |
|
101 | - */ |
|
102 | - public function hasTable($tableName) { |
|
103 | - return $this->schema->hasTable($this->connection->getPrefix() . $tableName); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Creates a new table. |
|
108 | - * |
|
109 | - * @param string $tableName |
|
110 | - * @return \Doctrine\DBAL\Schema\Table |
|
111 | - */ |
|
112 | - public function createTable($tableName) { |
|
113 | - return $this->schema->createTable($this->connection->getPrefix() . $tableName); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Drops a table from the schema. |
|
118 | - * |
|
119 | - * @param string $tableName |
|
120 | - * @return \Doctrine\DBAL\Schema\Schema |
|
121 | - */ |
|
122 | - public function dropTable($tableName) { |
|
123 | - $this->tablesToDelete[$tableName] = true; |
|
124 | - return $this->schema->dropTable($this->connection->getPrefix() . $tableName); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Gets all tables of this schema. |
|
129 | - * |
|
130 | - * @return \Doctrine\DBAL\Schema\Table[] |
|
131 | - */ |
|
132 | - public function getTables() { |
|
133 | - return $this->schema->getTables(); |
|
134 | - } |
|
32 | + /** @var IDBConnection|Connection */ |
|
33 | + protected $connection; |
|
34 | + |
|
35 | + /** @var Schema */ |
|
36 | + protected $schema; |
|
37 | + |
|
38 | + /** @var array */ |
|
39 | + protected $tablesToDelete = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * @param IDBConnection $connection |
|
43 | + */ |
|
44 | + public function __construct(IDBConnection $connection) { |
|
45 | + $this->connection = $connection; |
|
46 | + $this->schema = $this->connection->createSchema(); |
|
47 | + } |
|
48 | + |
|
49 | + public function getWrappedSchema() { |
|
50 | + return $this->schema; |
|
51 | + } |
|
52 | + |
|
53 | + public function performDropTableCalls() { |
|
54 | + foreach ($this->tablesToDelete as $tableName => $true) { |
|
55 | + $this->connection->dropTable($tableName); |
|
56 | + unset($this->tablesToDelete[$tableName]); |
|
57 | + } |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Gets all table names |
|
62 | + * |
|
63 | + * @return array |
|
64 | + */ |
|
65 | + public function getTableNamesWithoutPrefix() { |
|
66 | + $tableNames = $this->schema->getTableNames(); |
|
67 | + return array_map(function ($tableName) { |
|
68 | + if (strpos($tableName, $this->connection->getPrefix()) === 0) { |
|
69 | + return substr($tableName, strlen($this->connection->getPrefix())); |
|
70 | + } |
|
71 | + |
|
72 | + return $tableName; |
|
73 | + }, $tableNames); |
|
74 | + } |
|
75 | + |
|
76 | + // Overwritten methods |
|
77 | + |
|
78 | + /** |
|
79 | + * @return array |
|
80 | + */ |
|
81 | + public function getTableNames() { |
|
82 | + return $this->schema->getTableNames(); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param string $tableName |
|
87 | + * |
|
88 | + * @return \Doctrine\DBAL\Schema\Table |
|
89 | + * @throws \Doctrine\DBAL\Schema\SchemaException |
|
90 | + */ |
|
91 | + public function getTable($tableName) { |
|
92 | + return $this->schema->getTable($this->connection->getPrefix() . $tableName); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Does this schema have a table with the given name? |
|
97 | + * |
|
98 | + * @param string $tableName |
|
99 | + * |
|
100 | + * @return boolean |
|
101 | + */ |
|
102 | + public function hasTable($tableName) { |
|
103 | + return $this->schema->hasTable($this->connection->getPrefix() . $tableName); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Creates a new table. |
|
108 | + * |
|
109 | + * @param string $tableName |
|
110 | + * @return \Doctrine\DBAL\Schema\Table |
|
111 | + */ |
|
112 | + public function createTable($tableName) { |
|
113 | + return $this->schema->createTable($this->connection->getPrefix() . $tableName); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Drops a table from the schema. |
|
118 | + * |
|
119 | + * @param string $tableName |
|
120 | + * @return \Doctrine\DBAL\Schema\Schema |
|
121 | + */ |
|
122 | + public function dropTable($tableName) { |
|
123 | + $this->tablesToDelete[$tableName] = true; |
|
124 | + return $this->schema->dropTable($this->connection->getPrefix() . $tableName); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Gets all tables of this schema. |
|
129 | + * |
|
130 | + * @return \Doctrine\DBAL\Schema\Table[] |
|
131 | + */ |
|
132 | + public function getTables() { |
|
133 | + return $this->schema->getTables(); |
|
134 | + } |
|
135 | 135 | } |
@@ -26,80 +26,80 @@ |
||
26 | 26 | namespace OC\DB; |
27 | 27 | |
28 | 28 | class OracleConnection extends Connection { |
29 | - /** |
|
30 | - * Quote the keys of the array |
|
31 | - */ |
|
32 | - private function quoteKeys(array $data) { |
|
33 | - $return = []; |
|
34 | - $c = $this->getDatabasePlatform()->getIdentifierQuoteCharacter(); |
|
35 | - foreach ($data as $key => $value) { |
|
36 | - if ($key[0] !== $c) { |
|
37 | - $return[$this->quoteIdentifier($key)] = $value; |
|
38 | - } else { |
|
39 | - $return[$key] = $value; |
|
40 | - } |
|
41 | - } |
|
42 | - return $return; |
|
43 | - } |
|
29 | + /** |
|
30 | + * Quote the keys of the array |
|
31 | + */ |
|
32 | + private function quoteKeys(array $data) { |
|
33 | + $return = []; |
|
34 | + $c = $this->getDatabasePlatform()->getIdentifierQuoteCharacter(); |
|
35 | + foreach ($data as $key => $value) { |
|
36 | + if ($key[0] !== $c) { |
|
37 | + $return[$this->quoteIdentifier($key)] = $value; |
|
38 | + } else { |
|
39 | + $return[$key] = $value; |
|
40 | + } |
|
41 | + } |
|
42 | + return $return; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * {@inheritDoc} |
|
47 | - */ |
|
48 | - public function insert($tableName, array $data, array $types = []) { |
|
49 | - if ($tableName[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
50 | - $tableName = $this->quoteIdentifier($tableName); |
|
51 | - } |
|
52 | - $data = $this->quoteKeys($data); |
|
53 | - return parent::insert($tableName, $data, $types); |
|
54 | - } |
|
45 | + /** |
|
46 | + * {@inheritDoc} |
|
47 | + */ |
|
48 | + public function insert($tableName, array $data, array $types = []) { |
|
49 | + if ($tableName[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
50 | + $tableName = $this->quoteIdentifier($tableName); |
|
51 | + } |
|
52 | + $data = $this->quoteKeys($data); |
|
53 | + return parent::insert($tableName, $data, $types); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * {@inheritDoc} |
|
58 | - */ |
|
59 | - public function update($tableName, array $data, array $identifier, array $types = []) { |
|
60 | - if ($tableName[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
61 | - $tableName = $this->quoteIdentifier($tableName); |
|
62 | - } |
|
63 | - $data = $this->quoteKeys($data); |
|
64 | - $identifier = $this->quoteKeys($identifier); |
|
65 | - return parent::update($tableName, $data, $identifier, $types); |
|
66 | - } |
|
56 | + /** |
|
57 | + * {@inheritDoc} |
|
58 | + */ |
|
59 | + public function update($tableName, array $data, array $identifier, array $types = []) { |
|
60 | + if ($tableName[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
61 | + $tableName = $this->quoteIdentifier($tableName); |
|
62 | + } |
|
63 | + $data = $this->quoteKeys($data); |
|
64 | + $identifier = $this->quoteKeys($identifier); |
|
65 | + return parent::update($tableName, $data, $identifier, $types); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * {@inheritDoc} |
|
70 | - */ |
|
71 | - public function delete($tableExpression, array $identifier, array $types = []) { |
|
72 | - if ($tableExpression[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
73 | - $tableExpression = $this->quoteIdentifier($tableExpression); |
|
74 | - } |
|
75 | - $identifier = $this->quoteKeys($identifier); |
|
76 | - return parent::delete($tableExpression, $identifier); |
|
77 | - } |
|
68 | + /** |
|
69 | + * {@inheritDoc} |
|
70 | + */ |
|
71 | + public function delete($tableExpression, array $identifier, array $types = []) { |
|
72 | + if ($tableExpression[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
73 | + $tableExpression = $this->quoteIdentifier($tableExpression); |
|
74 | + } |
|
75 | + $identifier = $this->quoteKeys($identifier); |
|
76 | + return parent::delete($tableExpression, $identifier); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Drop a table from the database if it exists |
|
81 | - * |
|
82 | - * @param string $table table name without the prefix |
|
83 | - */ |
|
84 | - public function dropTable($table) { |
|
85 | - $table = $this->tablePrefix . trim($table); |
|
86 | - $table = $this->quoteIdentifier($table); |
|
87 | - $schema = $this->getSchemaManager(); |
|
88 | - if ($schema->tablesExist([$table])) { |
|
89 | - $schema->dropTable($table); |
|
90 | - } |
|
91 | - } |
|
79 | + /** |
|
80 | + * Drop a table from the database if it exists |
|
81 | + * |
|
82 | + * @param string $table table name without the prefix |
|
83 | + */ |
|
84 | + public function dropTable($table) { |
|
85 | + $table = $this->tablePrefix . trim($table); |
|
86 | + $table = $this->quoteIdentifier($table); |
|
87 | + $schema = $this->getSchemaManager(); |
|
88 | + if ($schema->tablesExist([$table])) { |
|
89 | + $schema->dropTable($table); |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Check if a table exists |
|
95 | - * |
|
96 | - * @param string $table table name without the prefix |
|
97 | - * @return bool |
|
98 | - */ |
|
99 | - public function tableExists($table) { |
|
100 | - $table = $this->tablePrefix . trim($table); |
|
101 | - $table = $this->quoteIdentifier($table); |
|
102 | - $schema = $this->getSchemaManager(); |
|
103 | - return $schema->tablesExist([$table]); |
|
104 | - } |
|
93 | + /** |
|
94 | + * Check if a table exists |
|
95 | + * |
|
96 | + * @param string $table table name without the prefix |
|
97 | + * @return bool |
|
98 | + */ |
|
99 | + public function tableExists($table) { |
|
100 | + $table = $this->tablePrefix . trim($table); |
|
101 | + $table = $this->quoteIdentifier($table); |
|
102 | + $schema = $this->getSchemaManager(); |
|
103 | + return $schema->tablesExist([$table]); |
|
104 | + } |
|
105 | 105 | } |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | * @param string $table table name without the prefix |
83 | 83 | */ |
84 | 84 | public function dropTable($table) { |
85 | - $table = $this->tablePrefix . trim($table); |
|
85 | + $table = $this->tablePrefix.trim($table); |
|
86 | 86 | $table = $this->quoteIdentifier($table); |
87 | 87 | $schema = $this->getSchemaManager(); |
88 | 88 | if ($schema->tablesExist([$table])) { |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | * @return bool |
98 | 98 | */ |
99 | 99 | public function tableExists($table) { |
100 | - $table = $this->tablePrefix . trim($table); |
|
100 | + $table = $this->tablePrefix.trim($table); |
|
101 | 101 | $table = $this->quoteIdentifier($table); |
102 | 102 | $schema = $this->getSchemaManager(); |
103 | 103 | return $schema->tablesExist([$table]); |
@@ -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 | } |
@@ -42,390 +42,390 @@ |
||
42 | 42 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
43 | 43 | |
44 | 44 | class PreviewManager implements IPreview { |
45 | - /** @var IConfig */ |
|
46 | - protected $config; |
|
45 | + /** @var IConfig */ |
|
46 | + protected $config; |
|
47 | 47 | |
48 | - /** @var IRootFolder */ |
|
49 | - protected $rootFolder; |
|
48 | + /** @var IRootFolder */ |
|
49 | + protected $rootFolder; |
|
50 | 50 | |
51 | - /** @var IAppData */ |
|
52 | - protected $appData; |
|
51 | + /** @var IAppData */ |
|
52 | + protected $appData; |
|
53 | 53 | |
54 | - /** @var EventDispatcherInterface */ |
|
55 | - protected $eventDispatcher; |
|
54 | + /** @var EventDispatcherInterface */ |
|
55 | + protected $eventDispatcher; |
|
56 | 56 | |
57 | - /** @var Generator */ |
|
58 | - private $generator; |
|
57 | + /** @var Generator */ |
|
58 | + private $generator; |
|
59 | 59 | |
60 | - /** @var GeneratorHelper */ |
|
61 | - private $helper; |
|
62 | - |
|
63 | - /** @var bool */ |
|
64 | - protected $providerListDirty = false; |
|
65 | - |
|
66 | - /** @var bool */ |
|
67 | - protected $registeredCoreProviders = false; |
|
68 | - |
|
69 | - /** @var array */ |
|
70 | - protected $providers = []; |
|
71 | - |
|
72 | - /** @var array mime type => support status */ |
|
73 | - protected $mimeTypeSupportMap = []; |
|
74 | - |
|
75 | - /** @var array */ |
|
76 | - protected $defaultProviders; |
|
77 | - |
|
78 | - /** @var string */ |
|
79 | - protected $userId; |
|
80 | - |
|
81 | - /** |
|
82 | - * PreviewManager constructor. |
|
83 | - * |
|
84 | - * @param IConfig $config |
|
85 | - * @param IRootFolder $rootFolder |
|
86 | - * @param IAppData $appData |
|
87 | - * @param EventDispatcherInterface $eventDispatcher |
|
88 | - * @param string $userId |
|
89 | - */ |
|
90 | - public function __construct(IConfig $config, |
|
91 | - IRootFolder $rootFolder, |
|
92 | - IAppData $appData, |
|
93 | - EventDispatcherInterface $eventDispatcher, |
|
94 | - GeneratorHelper $helper, |
|
95 | - $userId) { |
|
96 | - $this->config = $config; |
|
97 | - $this->rootFolder = $rootFolder; |
|
98 | - $this->appData = $appData; |
|
99 | - $this->eventDispatcher = $eventDispatcher; |
|
100 | - $this->helper = $helper; |
|
101 | - $this->userId = $userId; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * In order to improve lazy loading a closure can be registered which will be |
|
106 | - * called in case preview providers are actually requested |
|
107 | - * |
|
108 | - * $callable has to return an instance of \OCP\Preview\IProvider or \OCP\Preview\IProviderV2 |
|
109 | - * |
|
110 | - * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
|
111 | - * @param \Closure $callable |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - public function registerProvider($mimeTypeRegex, \Closure $callable) { |
|
115 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
116 | - return; |
|
117 | - } |
|
118 | - |
|
119 | - if (!isset($this->providers[$mimeTypeRegex])) { |
|
120 | - $this->providers[$mimeTypeRegex] = []; |
|
121 | - } |
|
122 | - $this->providers[$mimeTypeRegex][] = $callable; |
|
123 | - $this->providerListDirty = true; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Get all providers |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function getProviders() { |
|
131 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
132 | - return []; |
|
133 | - } |
|
134 | - |
|
135 | - $this->registerCoreProviders(); |
|
136 | - if ($this->providerListDirty) { |
|
137 | - $keys = array_map('strlen', array_keys($this->providers)); |
|
138 | - array_multisort($keys, SORT_DESC, $this->providers); |
|
139 | - $this->providerListDirty = false; |
|
140 | - } |
|
141 | - |
|
142 | - return $this->providers; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Does the manager have any providers |
|
147 | - * @return bool |
|
148 | - */ |
|
149 | - public function hasProviders() { |
|
150 | - $this->registerCoreProviders(); |
|
151 | - return !empty($this->providers); |
|
152 | - } |
|
153 | - |
|
154 | - private function getGenerator(): Generator { |
|
155 | - if ($this->generator === null) { |
|
156 | - $this->generator = new Generator( |
|
157 | - $this->config, |
|
158 | - $this, |
|
159 | - $this->appData, |
|
160 | - new GeneratorHelper( |
|
161 | - $this->rootFolder, |
|
162 | - $this->config |
|
163 | - ), |
|
164 | - $this->eventDispatcher |
|
165 | - ); |
|
166 | - } |
|
167 | - return $this->generator; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Returns a preview of a file |
|
172 | - * |
|
173 | - * The cache is searched first and if nothing usable was found then a preview is |
|
174 | - * generated by one of the providers |
|
175 | - * |
|
176 | - * @param File $file |
|
177 | - * @param int $width |
|
178 | - * @param int $height |
|
179 | - * @param bool $crop |
|
180 | - * @param string $mode |
|
181 | - * @param string $mimeType |
|
182 | - * @return ISimpleFile |
|
183 | - * @throws NotFoundException |
|
184 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
185 | - * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
|
186 | - */ |
|
187 | - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
188 | - return $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Generates previews of a file |
|
193 | - * |
|
194 | - * @param File $file |
|
195 | - * @param array $specifications |
|
196 | - * @param string $mimeType |
|
197 | - * @return ISimpleFile the last preview that was generated |
|
198 | - * @throws NotFoundException |
|
199 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
200 | - * @since 19.0.0 |
|
201 | - */ |
|
202 | - public function generatePreviews(File $file, array $specifications, $mimeType = null) { |
|
203 | - return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * returns true if the passed mime type is supported |
|
208 | - * |
|
209 | - * @param string $mimeType |
|
210 | - * @return boolean |
|
211 | - */ |
|
212 | - public function isMimeSupported($mimeType = '*') { |
|
213 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
214 | - return false; |
|
215 | - } |
|
216 | - |
|
217 | - if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
218 | - return $this->mimeTypeSupportMap[$mimeType]; |
|
219 | - } |
|
220 | - |
|
221 | - $this->registerCoreProviders(); |
|
222 | - $providerMimeTypes = array_keys($this->providers); |
|
223 | - foreach ($providerMimeTypes as $supportedMimeType) { |
|
224 | - if (preg_match($supportedMimeType, $mimeType)) { |
|
225 | - $this->mimeTypeSupportMap[$mimeType] = true; |
|
226 | - return true; |
|
227 | - } |
|
228 | - } |
|
229 | - $this->mimeTypeSupportMap[$mimeType] = false; |
|
230 | - return false; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Check if a preview can be generated for a file |
|
235 | - * |
|
236 | - * @param \OCP\Files\FileInfo $file |
|
237 | - * @return bool |
|
238 | - */ |
|
239 | - public function isAvailable(\OCP\Files\FileInfo $file) { |
|
240 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
241 | - return false; |
|
242 | - } |
|
243 | - |
|
244 | - $this->registerCoreProviders(); |
|
245 | - if (!$this->isMimeSupported($file->getMimetype())) { |
|
246 | - return false; |
|
247 | - } |
|
248 | - |
|
249 | - $mount = $file->getMountPoint(); |
|
250 | - if ($mount and !$mount->getOption('previews', true)) { |
|
251 | - return false; |
|
252 | - } |
|
253 | - |
|
254 | - foreach ($this->providers as $supportedMimeType => $providers) { |
|
255 | - if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
256 | - foreach ($providers as $providerClosure) { |
|
257 | - $provider = $this->helper->getProvider($providerClosure); |
|
258 | - if (!($provider instanceof IProviderV2)) { |
|
259 | - continue; |
|
260 | - } |
|
261 | - |
|
262 | - /** @var $provider IProvider */ |
|
263 | - if ($provider->isAvailable($file)) { |
|
264 | - return true; |
|
265 | - } |
|
266 | - } |
|
267 | - } |
|
268 | - } |
|
269 | - return false; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * List of enabled default providers |
|
274 | - * |
|
275 | - * The following providers are enabled by default: |
|
276 | - * - OC\Preview\PNG |
|
277 | - * - OC\Preview\JPEG |
|
278 | - * - OC\Preview\GIF |
|
279 | - * - OC\Preview\BMP |
|
280 | - * - OC\Preview\HEIC |
|
281 | - * - OC\Preview\XBitmap |
|
282 | - * - OC\Preview\MarkDown |
|
283 | - * - OC\Preview\MP3 |
|
284 | - * - OC\Preview\TXT |
|
285 | - * |
|
286 | - * The following providers are disabled by default due to performance or privacy concerns: |
|
287 | - * - OC\Preview\Font |
|
288 | - * - OC\Preview\Illustrator |
|
289 | - * - OC\Preview\Movie |
|
290 | - * - OC\Preview\MSOfficeDoc |
|
291 | - * - OC\Preview\MSOffice2003 |
|
292 | - * - OC\Preview\MSOffice2007 |
|
293 | - * - OC\Preview\OpenDocument |
|
294 | - * - OC\Preview\PDF |
|
295 | - * - OC\Preview\Photoshop |
|
296 | - * - OC\Preview\Postscript |
|
297 | - * - OC\Preview\StarOffice |
|
298 | - * - OC\Preview\SVG |
|
299 | - * - OC\Preview\TIFF |
|
300 | - * |
|
301 | - * @return array |
|
302 | - */ |
|
303 | - protected function getEnabledDefaultProvider() { |
|
304 | - if ($this->defaultProviders !== null) { |
|
305 | - return $this->defaultProviders; |
|
306 | - } |
|
307 | - |
|
308 | - $imageProviders = [ |
|
309 | - Preview\PNG::class, |
|
310 | - Preview\JPEG::class, |
|
311 | - Preview\GIF::class, |
|
312 | - Preview\BMP::class, |
|
313 | - Preview\HEIC::class, |
|
314 | - Preview\XBitmap::class, |
|
315 | - Preview\Krita::class, |
|
316 | - ]; |
|
317 | - |
|
318 | - $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
319 | - Preview\MarkDown::class, |
|
320 | - Preview\MP3::class, |
|
321 | - Preview\TXT::class, |
|
322 | - Preview\OpenDocument::class, |
|
323 | - ], $imageProviders)); |
|
324 | - |
|
325 | - if (in_array(Preview\Image::class, $this->defaultProviders)) { |
|
326 | - $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
327 | - } |
|
328 | - $this->defaultProviders = array_unique($this->defaultProviders); |
|
329 | - return $this->defaultProviders; |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * Register the default providers (if enabled) |
|
334 | - * |
|
335 | - * @param string $class |
|
336 | - * @param string $mimeType |
|
337 | - */ |
|
338 | - protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
339 | - if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
340 | - $this->registerProvider($mimeType, function () use ($class, $options) { |
|
341 | - return new $class($options); |
|
342 | - }); |
|
343 | - } |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Register the default providers (if enabled) |
|
348 | - */ |
|
349 | - protected function registerCoreProviders() { |
|
350 | - if ($this->registeredCoreProviders) { |
|
351 | - return; |
|
352 | - } |
|
353 | - $this->registeredCoreProviders = true; |
|
354 | - |
|
355 | - $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/'); |
|
356 | - $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/'); |
|
357 | - $this->registerCoreProvider(Preview\PNG::class, '/image\/png/'); |
|
358 | - $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/'); |
|
359 | - $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); |
|
360 | - $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); |
|
361 | - $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); |
|
362 | - $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); |
|
363 | - $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/'); |
|
364 | - $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
365 | - |
|
366 | - // SVG, Office and Bitmap require imagick |
|
367 | - if (extension_loaded('imagick')) { |
|
368 | - $checkImagick = new \Imagick(); |
|
369 | - |
|
370 | - $imagickProviders = [ |
|
371 | - 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], |
|
372 | - 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], |
|
373 | - 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class], |
|
374 | - 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class], |
|
375 | - 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class], |
|
376 | - 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class], |
|
377 | - 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class], |
|
378 | - 'HEIC' => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class], |
|
379 | - ]; |
|
380 | - |
|
381 | - foreach ($imagickProviders as $queryFormat => $provider) { |
|
382 | - $class = $provider['class']; |
|
383 | - if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
384 | - continue; |
|
385 | - } |
|
386 | - |
|
387 | - if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
388 | - $this->registerCoreProvider($class, $provider['mimetype']); |
|
389 | - } |
|
390 | - } |
|
391 | - |
|
392 | - if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
393 | - if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
394 | - $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
395 | - |
|
396 | - if (!$officeFound) { |
|
397 | - //let's see if there is libreoffice or openoffice on this machine |
|
398 | - $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
399 | - $officeFound = !empty($whichLibreOffice); |
|
400 | - if (!$officeFound) { |
|
401 | - $whichOpenOffice = shell_exec('command -v openoffice'); |
|
402 | - $officeFound = !empty($whichOpenOffice); |
|
403 | - } |
|
404 | - } |
|
405 | - |
|
406 | - if ($officeFound) { |
|
407 | - $this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/'); |
|
408 | - $this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/'); |
|
409 | - $this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/'); |
|
410 | - $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
411 | - $this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/'); |
|
412 | - } |
|
413 | - } |
|
414 | - } |
|
415 | - } |
|
416 | - |
|
417 | - // Video requires avconv or ffmpeg |
|
418 | - if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) { |
|
419 | - $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
420 | - $ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
421 | - |
|
422 | - if ($avconvBinary || $ffmpegBinary) { |
|
423 | - // FIXME // a bit hacky but didn't want to use subclasses |
|
424 | - \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
425 | - \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
426 | - |
|
427 | - $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/'); |
|
428 | - } |
|
429 | - } |
|
430 | - } |
|
60 | + /** @var GeneratorHelper */ |
|
61 | + private $helper; |
|
62 | + |
|
63 | + /** @var bool */ |
|
64 | + protected $providerListDirty = false; |
|
65 | + |
|
66 | + /** @var bool */ |
|
67 | + protected $registeredCoreProviders = false; |
|
68 | + |
|
69 | + /** @var array */ |
|
70 | + protected $providers = []; |
|
71 | + |
|
72 | + /** @var array mime type => support status */ |
|
73 | + protected $mimeTypeSupportMap = []; |
|
74 | + |
|
75 | + /** @var array */ |
|
76 | + protected $defaultProviders; |
|
77 | + |
|
78 | + /** @var string */ |
|
79 | + protected $userId; |
|
80 | + |
|
81 | + /** |
|
82 | + * PreviewManager constructor. |
|
83 | + * |
|
84 | + * @param IConfig $config |
|
85 | + * @param IRootFolder $rootFolder |
|
86 | + * @param IAppData $appData |
|
87 | + * @param EventDispatcherInterface $eventDispatcher |
|
88 | + * @param string $userId |
|
89 | + */ |
|
90 | + public function __construct(IConfig $config, |
|
91 | + IRootFolder $rootFolder, |
|
92 | + IAppData $appData, |
|
93 | + EventDispatcherInterface $eventDispatcher, |
|
94 | + GeneratorHelper $helper, |
|
95 | + $userId) { |
|
96 | + $this->config = $config; |
|
97 | + $this->rootFolder = $rootFolder; |
|
98 | + $this->appData = $appData; |
|
99 | + $this->eventDispatcher = $eventDispatcher; |
|
100 | + $this->helper = $helper; |
|
101 | + $this->userId = $userId; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * In order to improve lazy loading a closure can be registered which will be |
|
106 | + * called in case preview providers are actually requested |
|
107 | + * |
|
108 | + * $callable has to return an instance of \OCP\Preview\IProvider or \OCP\Preview\IProviderV2 |
|
109 | + * |
|
110 | + * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
|
111 | + * @param \Closure $callable |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + public function registerProvider($mimeTypeRegex, \Closure $callable) { |
|
115 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
116 | + return; |
|
117 | + } |
|
118 | + |
|
119 | + if (!isset($this->providers[$mimeTypeRegex])) { |
|
120 | + $this->providers[$mimeTypeRegex] = []; |
|
121 | + } |
|
122 | + $this->providers[$mimeTypeRegex][] = $callable; |
|
123 | + $this->providerListDirty = true; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Get all providers |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function getProviders() { |
|
131 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
132 | + return []; |
|
133 | + } |
|
134 | + |
|
135 | + $this->registerCoreProviders(); |
|
136 | + if ($this->providerListDirty) { |
|
137 | + $keys = array_map('strlen', array_keys($this->providers)); |
|
138 | + array_multisort($keys, SORT_DESC, $this->providers); |
|
139 | + $this->providerListDirty = false; |
|
140 | + } |
|
141 | + |
|
142 | + return $this->providers; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Does the manager have any providers |
|
147 | + * @return bool |
|
148 | + */ |
|
149 | + public function hasProviders() { |
|
150 | + $this->registerCoreProviders(); |
|
151 | + return !empty($this->providers); |
|
152 | + } |
|
153 | + |
|
154 | + private function getGenerator(): Generator { |
|
155 | + if ($this->generator === null) { |
|
156 | + $this->generator = new Generator( |
|
157 | + $this->config, |
|
158 | + $this, |
|
159 | + $this->appData, |
|
160 | + new GeneratorHelper( |
|
161 | + $this->rootFolder, |
|
162 | + $this->config |
|
163 | + ), |
|
164 | + $this->eventDispatcher |
|
165 | + ); |
|
166 | + } |
|
167 | + return $this->generator; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Returns a preview of a file |
|
172 | + * |
|
173 | + * The cache is searched first and if nothing usable was found then a preview is |
|
174 | + * generated by one of the providers |
|
175 | + * |
|
176 | + * @param File $file |
|
177 | + * @param int $width |
|
178 | + * @param int $height |
|
179 | + * @param bool $crop |
|
180 | + * @param string $mode |
|
181 | + * @param string $mimeType |
|
182 | + * @return ISimpleFile |
|
183 | + * @throws NotFoundException |
|
184 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
185 | + * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
|
186 | + */ |
|
187 | + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
188 | + return $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Generates previews of a file |
|
193 | + * |
|
194 | + * @param File $file |
|
195 | + * @param array $specifications |
|
196 | + * @param string $mimeType |
|
197 | + * @return ISimpleFile the last preview that was generated |
|
198 | + * @throws NotFoundException |
|
199 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
200 | + * @since 19.0.0 |
|
201 | + */ |
|
202 | + public function generatePreviews(File $file, array $specifications, $mimeType = null) { |
|
203 | + return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * returns true if the passed mime type is supported |
|
208 | + * |
|
209 | + * @param string $mimeType |
|
210 | + * @return boolean |
|
211 | + */ |
|
212 | + public function isMimeSupported($mimeType = '*') { |
|
213 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
214 | + return false; |
|
215 | + } |
|
216 | + |
|
217 | + if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
218 | + return $this->mimeTypeSupportMap[$mimeType]; |
|
219 | + } |
|
220 | + |
|
221 | + $this->registerCoreProviders(); |
|
222 | + $providerMimeTypes = array_keys($this->providers); |
|
223 | + foreach ($providerMimeTypes as $supportedMimeType) { |
|
224 | + if (preg_match($supportedMimeType, $mimeType)) { |
|
225 | + $this->mimeTypeSupportMap[$mimeType] = true; |
|
226 | + return true; |
|
227 | + } |
|
228 | + } |
|
229 | + $this->mimeTypeSupportMap[$mimeType] = false; |
|
230 | + return false; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Check if a preview can be generated for a file |
|
235 | + * |
|
236 | + * @param \OCP\Files\FileInfo $file |
|
237 | + * @return bool |
|
238 | + */ |
|
239 | + public function isAvailable(\OCP\Files\FileInfo $file) { |
|
240 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
241 | + return false; |
|
242 | + } |
|
243 | + |
|
244 | + $this->registerCoreProviders(); |
|
245 | + if (!$this->isMimeSupported($file->getMimetype())) { |
|
246 | + return false; |
|
247 | + } |
|
248 | + |
|
249 | + $mount = $file->getMountPoint(); |
|
250 | + if ($mount and !$mount->getOption('previews', true)) { |
|
251 | + return false; |
|
252 | + } |
|
253 | + |
|
254 | + foreach ($this->providers as $supportedMimeType => $providers) { |
|
255 | + if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
256 | + foreach ($providers as $providerClosure) { |
|
257 | + $provider = $this->helper->getProvider($providerClosure); |
|
258 | + if (!($provider instanceof IProviderV2)) { |
|
259 | + continue; |
|
260 | + } |
|
261 | + |
|
262 | + /** @var $provider IProvider */ |
|
263 | + if ($provider->isAvailable($file)) { |
|
264 | + return true; |
|
265 | + } |
|
266 | + } |
|
267 | + } |
|
268 | + } |
|
269 | + return false; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * List of enabled default providers |
|
274 | + * |
|
275 | + * The following providers are enabled by default: |
|
276 | + * - OC\Preview\PNG |
|
277 | + * - OC\Preview\JPEG |
|
278 | + * - OC\Preview\GIF |
|
279 | + * - OC\Preview\BMP |
|
280 | + * - OC\Preview\HEIC |
|
281 | + * - OC\Preview\XBitmap |
|
282 | + * - OC\Preview\MarkDown |
|
283 | + * - OC\Preview\MP3 |
|
284 | + * - OC\Preview\TXT |
|
285 | + * |
|
286 | + * The following providers are disabled by default due to performance or privacy concerns: |
|
287 | + * - OC\Preview\Font |
|
288 | + * - OC\Preview\Illustrator |
|
289 | + * - OC\Preview\Movie |
|
290 | + * - OC\Preview\MSOfficeDoc |
|
291 | + * - OC\Preview\MSOffice2003 |
|
292 | + * - OC\Preview\MSOffice2007 |
|
293 | + * - OC\Preview\OpenDocument |
|
294 | + * - OC\Preview\PDF |
|
295 | + * - OC\Preview\Photoshop |
|
296 | + * - OC\Preview\Postscript |
|
297 | + * - OC\Preview\StarOffice |
|
298 | + * - OC\Preview\SVG |
|
299 | + * - OC\Preview\TIFF |
|
300 | + * |
|
301 | + * @return array |
|
302 | + */ |
|
303 | + protected function getEnabledDefaultProvider() { |
|
304 | + if ($this->defaultProviders !== null) { |
|
305 | + return $this->defaultProviders; |
|
306 | + } |
|
307 | + |
|
308 | + $imageProviders = [ |
|
309 | + Preview\PNG::class, |
|
310 | + Preview\JPEG::class, |
|
311 | + Preview\GIF::class, |
|
312 | + Preview\BMP::class, |
|
313 | + Preview\HEIC::class, |
|
314 | + Preview\XBitmap::class, |
|
315 | + Preview\Krita::class, |
|
316 | + ]; |
|
317 | + |
|
318 | + $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
319 | + Preview\MarkDown::class, |
|
320 | + Preview\MP3::class, |
|
321 | + Preview\TXT::class, |
|
322 | + Preview\OpenDocument::class, |
|
323 | + ], $imageProviders)); |
|
324 | + |
|
325 | + if (in_array(Preview\Image::class, $this->defaultProviders)) { |
|
326 | + $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
327 | + } |
|
328 | + $this->defaultProviders = array_unique($this->defaultProviders); |
|
329 | + return $this->defaultProviders; |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * Register the default providers (if enabled) |
|
334 | + * |
|
335 | + * @param string $class |
|
336 | + * @param string $mimeType |
|
337 | + */ |
|
338 | + protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
339 | + if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
340 | + $this->registerProvider($mimeType, function () use ($class, $options) { |
|
341 | + return new $class($options); |
|
342 | + }); |
|
343 | + } |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Register the default providers (if enabled) |
|
348 | + */ |
|
349 | + protected function registerCoreProviders() { |
|
350 | + if ($this->registeredCoreProviders) { |
|
351 | + return; |
|
352 | + } |
|
353 | + $this->registeredCoreProviders = true; |
|
354 | + |
|
355 | + $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/'); |
|
356 | + $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/'); |
|
357 | + $this->registerCoreProvider(Preview\PNG::class, '/image\/png/'); |
|
358 | + $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/'); |
|
359 | + $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); |
|
360 | + $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); |
|
361 | + $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); |
|
362 | + $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); |
|
363 | + $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/'); |
|
364 | + $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
365 | + |
|
366 | + // SVG, Office and Bitmap require imagick |
|
367 | + if (extension_loaded('imagick')) { |
|
368 | + $checkImagick = new \Imagick(); |
|
369 | + |
|
370 | + $imagickProviders = [ |
|
371 | + 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], |
|
372 | + 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], |
|
373 | + 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class], |
|
374 | + 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class], |
|
375 | + 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class], |
|
376 | + 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class], |
|
377 | + 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class], |
|
378 | + 'HEIC' => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class], |
|
379 | + ]; |
|
380 | + |
|
381 | + foreach ($imagickProviders as $queryFormat => $provider) { |
|
382 | + $class = $provider['class']; |
|
383 | + if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
384 | + continue; |
|
385 | + } |
|
386 | + |
|
387 | + if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
388 | + $this->registerCoreProvider($class, $provider['mimetype']); |
|
389 | + } |
|
390 | + } |
|
391 | + |
|
392 | + if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
393 | + if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
394 | + $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
395 | + |
|
396 | + if (!$officeFound) { |
|
397 | + //let's see if there is libreoffice or openoffice on this machine |
|
398 | + $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
399 | + $officeFound = !empty($whichLibreOffice); |
|
400 | + if (!$officeFound) { |
|
401 | + $whichOpenOffice = shell_exec('command -v openoffice'); |
|
402 | + $officeFound = !empty($whichOpenOffice); |
|
403 | + } |
|
404 | + } |
|
405 | + |
|
406 | + if ($officeFound) { |
|
407 | + $this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/'); |
|
408 | + $this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/'); |
|
409 | + $this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/'); |
|
410 | + $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
411 | + $this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/'); |
|
412 | + } |
|
413 | + } |
|
414 | + } |
|
415 | + } |
|
416 | + |
|
417 | + // Video requires avconv or ffmpeg |
|
418 | + if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) { |
|
419 | + $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
420 | + $ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
421 | + |
|
422 | + if ($avconvBinary || $ffmpegBinary) { |
|
423 | + // FIXME // a bit hacky but didn't want to use subclasses |
|
424 | + \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
425 | + \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
426 | + |
|
427 | + $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/'); |
|
428 | + } |
|
429 | + } |
|
430 | + } |
|
431 | 431 | } |
@@ -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 | } |
@@ -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 | } |
@@ -30,140 +30,140 @@ |
||
30 | 30 | use OCP\IMemcache; |
31 | 31 | |
32 | 32 | class APCu extends Cache implements IMemcache { |
33 | - use CASTrait { |
|
34 | - cas as casEmulated; |
|
35 | - } |
|
33 | + use CASTrait { |
|
34 | + cas as casEmulated; |
|
35 | + } |
|
36 | 36 | |
37 | - use CADTrait; |
|
37 | + use CADTrait; |
|
38 | 38 | |
39 | - public function get($key) { |
|
40 | - $result = apcu_fetch($this->getPrefix() . $key, $success); |
|
41 | - if (!$success) { |
|
42 | - return null; |
|
43 | - } |
|
44 | - return $result; |
|
45 | - } |
|
39 | + public function get($key) { |
|
40 | + $result = apcu_fetch($this->getPrefix() . $key, $success); |
|
41 | + if (!$success) { |
|
42 | + return null; |
|
43 | + } |
|
44 | + return $result; |
|
45 | + } |
|
46 | 46 | |
47 | - public function set($key, $value, $ttl = 0) { |
|
48 | - return apcu_store($this->getPrefix() . $key, $value, $ttl); |
|
49 | - } |
|
47 | + public function set($key, $value, $ttl = 0) { |
|
48 | + return apcu_store($this->getPrefix() . $key, $value, $ttl); |
|
49 | + } |
|
50 | 50 | |
51 | - public function hasKey($key) { |
|
52 | - return apcu_exists($this->getPrefix() . $key); |
|
53 | - } |
|
51 | + public function hasKey($key) { |
|
52 | + return apcu_exists($this->getPrefix() . $key); |
|
53 | + } |
|
54 | 54 | |
55 | - public function remove($key) { |
|
56 | - return apcu_delete($this->getPrefix() . $key); |
|
57 | - } |
|
55 | + public function remove($key) { |
|
56 | + return apcu_delete($this->getPrefix() . $key); |
|
57 | + } |
|
58 | 58 | |
59 | - public function clear($prefix = '') { |
|
60 | - $ns = $this->getPrefix() . $prefix; |
|
61 | - $ns = preg_quote($ns, '/'); |
|
62 | - if (class_exists('\APCIterator')) { |
|
63 | - $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY); |
|
64 | - } else { |
|
65 | - $iter = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY); |
|
66 | - } |
|
67 | - return apcu_delete($iter); |
|
68 | - } |
|
59 | + public function clear($prefix = '') { |
|
60 | + $ns = $this->getPrefix() . $prefix; |
|
61 | + $ns = preg_quote($ns, '/'); |
|
62 | + if (class_exists('\APCIterator')) { |
|
63 | + $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY); |
|
64 | + } else { |
|
65 | + $iter = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY); |
|
66 | + } |
|
67 | + return apcu_delete($iter); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Set a value in the cache if it's not already stored |
|
72 | - * |
|
73 | - * @param string $key |
|
74 | - * @param mixed $value |
|
75 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
76 | - * @return bool |
|
77 | - */ |
|
78 | - public function add($key, $value, $ttl = 0) { |
|
79 | - return apcu_add($this->getPrefix() . $key, $value, $ttl); |
|
80 | - } |
|
70 | + /** |
|
71 | + * Set a value in the cache if it's not already stored |
|
72 | + * |
|
73 | + * @param string $key |
|
74 | + * @param mixed $value |
|
75 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
76 | + * @return bool |
|
77 | + */ |
|
78 | + public function add($key, $value, $ttl = 0) { |
|
79 | + return apcu_add($this->getPrefix() . $key, $value, $ttl); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Increase a stored number |
|
84 | - * |
|
85 | - * @param string $key |
|
86 | - * @param int $step |
|
87 | - * @return int | bool |
|
88 | - */ |
|
89 | - public function inc($key, $step = 1) { |
|
90 | - $this->add($key, 0); |
|
91 | - /** |
|
92 | - * TODO - hack around a PHP 7 specific issue in APCu |
|
93 | - * |
|
94 | - * on PHP 7 the apcu_inc method on a non-existing object will increment |
|
95 | - * "0" and result in "1" as value - therefore we check for existence |
|
96 | - * first |
|
97 | - * |
|
98 | - * on PHP 5.6 this is not the case |
|
99 | - * |
|
100 | - * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
101 | - * for details |
|
102 | - */ |
|
103 | - return apcu_exists($this->getPrefix() . $key) |
|
104 | - ? apcu_inc($this->getPrefix() . $key, $step) |
|
105 | - : false; |
|
106 | - } |
|
82 | + /** |
|
83 | + * Increase a stored number |
|
84 | + * |
|
85 | + * @param string $key |
|
86 | + * @param int $step |
|
87 | + * @return int | bool |
|
88 | + */ |
|
89 | + public function inc($key, $step = 1) { |
|
90 | + $this->add($key, 0); |
|
91 | + /** |
|
92 | + * TODO - hack around a PHP 7 specific issue in APCu |
|
93 | + * |
|
94 | + * on PHP 7 the apcu_inc method on a non-existing object will increment |
|
95 | + * "0" and result in "1" as value - therefore we check for existence |
|
96 | + * first |
|
97 | + * |
|
98 | + * on PHP 5.6 this is not the case |
|
99 | + * |
|
100 | + * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
101 | + * for details |
|
102 | + */ |
|
103 | + return apcu_exists($this->getPrefix() . $key) |
|
104 | + ? apcu_inc($this->getPrefix() . $key, $step) |
|
105 | + : false; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Decrease a stored number |
|
110 | - * |
|
111 | - * @param string $key |
|
112 | - * @param int $step |
|
113 | - * @return int | bool |
|
114 | - */ |
|
115 | - public function dec($key, $step = 1) { |
|
116 | - /** |
|
117 | - * TODO - hack around a PHP 7 specific issue in APCu |
|
118 | - * |
|
119 | - * on PHP 7 the apcu_dec method on a non-existing object will decrement |
|
120 | - * "0" and result in "-1" as value - therefore we check for existence |
|
121 | - * first |
|
122 | - * |
|
123 | - * on PHP 5.6 this is not the case |
|
124 | - * |
|
125 | - * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
126 | - * for details |
|
127 | - */ |
|
128 | - return apcu_exists($this->getPrefix() . $key) |
|
129 | - ? apcu_dec($this->getPrefix() . $key, $step) |
|
130 | - : false; |
|
131 | - } |
|
108 | + /** |
|
109 | + * Decrease a stored number |
|
110 | + * |
|
111 | + * @param string $key |
|
112 | + * @param int $step |
|
113 | + * @return int | bool |
|
114 | + */ |
|
115 | + public function dec($key, $step = 1) { |
|
116 | + /** |
|
117 | + * TODO - hack around a PHP 7 specific issue in APCu |
|
118 | + * |
|
119 | + * on PHP 7 the apcu_dec method on a non-existing object will decrement |
|
120 | + * "0" and result in "-1" as value - therefore we check for existence |
|
121 | + * first |
|
122 | + * |
|
123 | + * on PHP 5.6 this is not the case |
|
124 | + * |
|
125 | + * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
126 | + * for details |
|
127 | + */ |
|
128 | + return apcu_exists($this->getPrefix() . $key) |
|
129 | + ? apcu_dec($this->getPrefix() . $key, $step) |
|
130 | + : false; |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * Compare and set |
|
135 | - * |
|
136 | - * @param string $key |
|
137 | - * @param mixed $old |
|
138 | - * @param mixed $new |
|
139 | - * @return bool |
|
140 | - */ |
|
141 | - public function cas($key, $old, $new) { |
|
142 | - // apc only does cas for ints |
|
143 | - if (is_int($old) and is_int($new)) { |
|
144 | - return apcu_cas($this->getPrefix() . $key, $old, $new); |
|
145 | - } else { |
|
146 | - return $this->casEmulated($key, $old, $new); |
|
147 | - } |
|
148 | - } |
|
133 | + /** |
|
134 | + * Compare and set |
|
135 | + * |
|
136 | + * @param string $key |
|
137 | + * @param mixed $old |
|
138 | + * @param mixed $new |
|
139 | + * @return bool |
|
140 | + */ |
|
141 | + public function cas($key, $old, $new) { |
|
142 | + // apc only does cas for ints |
|
143 | + if (is_int($old) and is_int($new)) { |
|
144 | + return apcu_cas($this->getPrefix() . $key, $old, $new); |
|
145 | + } else { |
|
146 | + return $this->casEmulated($key, $old, $new); |
|
147 | + } |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * @return bool |
|
152 | - */ |
|
153 | - public static function isAvailable() { |
|
154 | - if (!extension_loaded('apcu')) { |
|
155 | - return false; |
|
156 | - } elseif (!\OC::$server->getIniWrapper()->getBool('apc.enabled')) { |
|
157 | - return false; |
|
158 | - } elseif (!\OC::$server->getIniWrapper()->getBool('apc.enable_cli') && \OC::$CLI) { |
|
159 | - return false; |
|
160 | - } elseif ( |
|
161 | - version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 && |
|
162 | - version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1 |
|
163 | - ) { |
|
164 | - return false; |
|
165 | - } else { |
|
166 | - return true; |
|
167 | - } |
|
168 | - } |
|
150 | + /** |
|
151 | + * @return bool |
|
152 | + */ |
|
153 | + public static function isAvailable() { |
|
154 | + if (!extension_loaded('apcu')) { |
|
155 | + return false; |
|
156 | + } elseif (!\OC::$server->getIniWrapper()->getBool('apc.enabled')) { |
|
157 | + return false; |
|
158 | + } elseif (!\OC::$server->getIniWrapper()->getBool('apc.enable_cli') && \OC::$CLI) { |
|
159 | + return false; |
|
160 | + } elseif ( |
|
161 | + version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 && |
|
162 | + version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1 |
|
163 | + ) { |
|
164 | + return false; |
|
165 | + } else { |
|
166 | + return true; |
|
167 | + } |
|
168 | + } |
|
169 | 169 | } |
@@ -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,176 +35,176 @@ |
||
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 IGroupManager */ |
|
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 IUserManager $userManager |
|
60 | - * @param IUserSession $userSession |
|
61 | - */ |
|
62 | - public function __construct( |
|
63 | - $user, |
|
64 | - $isAdmin, |
|
65 | - IGroupManager $groupManager, |
|
66 | - IUserSession $userSession |
|
67 | - ) { |
|
68 | - $this->user = $user; |
|
69 | - $this->isAdmin = (bool)$isAdmin; |
|
70 | - $this->groupManager = $groupManager; |
|
71 | - $this->userSession = $userSession; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * returns an array with meta data about all available groups |
|
76 | - * the array is structured as follows: |
|
77 | - * [0] array containing meta data about admin groups |
|
78 | - * [1] array containing meta data about unprivileged groups |
|
79 | - * @param string $groupSearch only effective when instance was created with |
|
80 | - * isAdmin being true |
|
81 | - * @param string $userSearch the pattern users are search for |
|
82 | - * @return array |
|
83 | - */ |
|
84 | - public function get($groupSearch = '', $userSearch = '') { |
|
85 | - $key = $groupSearch . '::' . $userSearch; |
|
86 | - if (isset($this->metaData[$key])) { |
|
87 | - return $this->metaData[$key]; |
|
88 | - } |
|
89 | - |
|
90 | - $adminGroups = []; |
|
91 | - $groups = []; |
|
92 | - $sortGroupsIndex = 0; |
|
93 | - $sortGroupsKeys = []; |
|
94 | - $sortAdminGroupsIndex = 0; |
|
95 | - $sortAdminGroupsKeys = []; |
|
96 | - |
|
97 | - foreach ($this->getGroups($groupSearch) as $group) { |
|
98 | - $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
99 | - if (strtolower($group->getGID()) !== 'admin') { |
|
100 | - $this->addEntry( |
|
101 | - $groups, |
|
102 | - $sortGroupsKeys, |
|
103 | - $sortGroupsIndex, |
|
104 | - $groupMetaData); |
|
105 | - } else { |
|
106 | - //admin group is hard coded to 'admin' for now. In future, |
|
107 | - //backends may define admin groups too. Then the if statement |
|
108 | - //has to be adjusted accordingly. |
|
109 | - $this->addEntry( |
|
110 | - $adminGroups, |
|
111 | - $sortAdminGroupsKeys, |
|
112 | - $sortAdminGroupsIndex, |
|
113 | - $groupMetaData); |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - //whether sorting is necessary is will be checked in sort() |
|
118 | - $this->sort($groups, $sortGroupsKeys); |
|
119 | - $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
120 | - |
|
121 | - $this->metaData[$key] = [$adminGroups, $groups]; |
|
122 | - return $this->metaData[$key]; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * sets the sort mode, see SORT_* constants for supported modes |
|
127 | - * |
|
128 | - * @param int $sortMode |
|
129 | - */ |
|
130 | - public function setSorting($sortMode) { |
|
131 | - switch ($sortMode) { |
|
132 | - case self::SORT_USERCOUNT: |
|
133 | - case self::SORT_GROUPNAME: |
|
134 | - $this->sorting = $sortMode; |
|
135 | - break; |
|
136 | - |
|
137 | - default: |
|
138 | - $this->sorting = self::SORT_NONE; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * adds an group entry to the resulting array |
|
144 | - * @param array $entries the resulting array, by reference |
|
145 | - * @param array $sortKeys the sort key array, by reference |
|
146 | - * @param int $sortIndex the sort key index, by reference |
|
147 | - * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
148 | - */ |
|
149 | - private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
150 | - $entries[] = $data; |
|
151 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
152 | - $sortKeys[$sortIndex] = $data['usercount']; |
|
153 | - $sortIndex++; |
|
154 | - } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
155 | - $sortKeys[$sortIndex] = $data['name']; |
|
156 | - $sortIndex++; |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * creates an array containing the group meta data |
|
162 | - * @param \OCP\IGroup $group |
|
163 | - * @param string $userSearch |
|
164 | - * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
165 | - */ |
|
166 | - private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
167 | - return [ |
|
168 | - 'id' => $group->getGID(), |
|
169 | - 'name' => $group->getDisplayName(), |
|
170 | - 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
171 | - 'disabled' => $group->countDisabled(), |
|
172 | - 'canAdd' => $group->canAddUser(), |
|
173 | - 'canRemove' => $group->canRemoveUser(), |
|
174 | - ]; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * sorts the result array, if applicable |
|
179 | - * @param array $entries the result array, by reference |
|
180 | - * @param array $sortKeys the array containing the sort keys |
|
181 | - * @param return null |
|
182 | - */ |
|
183 | - private function sort(&$entries, $sortKeys) { |
|
184 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
185 | - array_multisort($sortKeys, SORT_DESC, $entries); |
|
186 | - } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
187 | - array_multisort($sortKeys, SORT_ASC, $entries); |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * returns the available groups |
|
193 | - * @param string $search a search string |
|
194 | - * @return \OCP\IGroup[] |
|
195 | - */ |
|
196 | - public function getGroups($search = '') { |
|
197 | - if ($this->isAdmin) { |
|
198 | - return $this->groupManager->search($search); |
|
199 | - } else { |
|
200 | - $userObject = $this->userSession->getUser(); |
|
201 | - if ($userObject !== null) { |
|
202 | - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
203 | - } else { |
|
204 | - $groups = []; |
|
205 | - } |
|
206 | - |
|
207 | - return $groups; |
|
208 | - } |
|
209 | - } |
|
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 IGroupManager */ |
|
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 IUserManager $userManager |
|
60 | + * @param IUserSession $userSession |
|
61 | + */ |
|
62 | + public function __construct( |
|
63 | + $user, |
|
64 | + $isAdmin, |
|
65 | + IGroupManager $groupManager, |
|
66 | + IUserSession $userSession |
|
67 | + ) { |
|
68 | + $this->user = $user; |
|
69 | + $this->isAdmin = (bool)$isAdmin; |
|
70 | + $this->groupManager = $groupManager; |
|
71 | + $this->userSession = $userSession; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * returns an array with meta data about all available groups |
|
76 | + * the array is structured as follows: |
|
77 | + * [0] array containing meta data about admin groups |
|
78 | + * [1] array containing meta data about unprivileged groups |
|
79 | + * @param string $groupSearch only effective when instance was created with |
|
80 | + * isAdmin being true |
|
81 | + * @param string $userSearch the pattern users are search for |
|
82 | + * @return array |
|
83 | + */ |
|
84 | + public function get($groupSearch = '', $userSearch = '') { |
|
85 | + $key = $groupSearch . '::' . $userSearch; |
|
86 | + if (isset($this->metaData[$key])) { |
|
87 | + return $this->metaData[$key]; |
|
88 | + } |
|
89 | + |
|
90 | + $adminGroups = []; |
|
91 | + $groups = []; |
|
92 | + $sortGroupsIndex = 0; |
|
93 | + $sortGroupsKeys = []; |
|
94 | + $sortAdminGroupsIndex = 0; |
|
95 | + $sortAdminGroupsKeys = []; |
|
96 | + |
|
97 | + foreach ($this->getGroups($groupSearch) as $group) { |
|
98 | + $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
99 | + if (strtolower($group->getGID()) !== 'admin') { |
|
100 | + $this->addEntry( |
|
101 | + $groups, |
|
102 | + $sortGroupsKeys, |
|
103 | + $sortGroupsIndex, |
|
104 | + $groupMetaData); |
|
105 | + } else { |
|
106 | + //admin group is hard coded to 'admin' for now. In future, |
|
107 | + //backends may define admin groups too. Then the if statement |
|
108 | + //has to be adjusted accordingly. |
|
109 | + $this->addEntry( |
|
110 | + $adminGroups, |
|
111 | + $sortAdminGroupsKeys, |
|
112 | + $sortAdminGroupsIndex, |
|
113 | + $groupMetaData); |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + //whether sorting is necessary is will be checked in sort() |
|
118 | + $this->sort($groups, $sortGroupsKeys); |
|
119 | + $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
120 | + |
|
121 | + $this->metaData[$key] = [$adminGroups, $groups]; |
|
122 | + return $this->metaData[$key]; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * sets the sort mode, see SORT_* constants for supported modes |
|
127 | + * |
|
128 | + * @param int $sortMode |
|
129 | + */ |
|
130 | + public function setSorting($sortMode) { |
|
131 | + switch ($sortMode) { |
|
132 | + case self::SORT_USERCOUNT: |
|
133 | + case self::SORT_GROUPNAME: |
|
134 | + $this->sorting = $sortMode; |
|
135 | + break; |
|
136 | + |
|
137 | + default: |
|
138 | + $this->sorting = self::SORT_NONE; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * adds an group entry to the resulting array |
|
144 | + * @param array $entries the resulting array, by reference |
|
145 | + * @param array $sortKeys the sort key array, by reference |
|
146 | + * @param int $sortIndex the sort key index, by reference |
|
147 | + * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
148 | + */ |
|
149 | + private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
150 | + $entries[] = $data; |
|
151 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
152 | + $sortKeys[$sortIndex] = $data['usercount']; |
|
153 | + $sortIndex++; |
|
154 | + } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
155 | + $sortKeys[$sortIndex] = $data['name']; |
|
156 | + $sortIndex++; |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * creates an array containing the group meta data |
|
162 | + * @param \OCP\IGroup $group |
|
163 | + * @param string $userSearch |
|
164 | + * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
165 | + */ |
|
166 | + private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
167 | + return [ |
|
168 | + 'id' => $group->getGID(), |
|
169 | + 'name' => $group->getDisplayName(), |
|
170 | + 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
171 | + 'disabled' => $group->countDisabled(), |
|
172 | + 'canAdd' => $group->canAddUser(), |
|
173 | + 'canRemove' => $group->canRemoveUser(), |
|
174 | + ]; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * sorts the result array, if applicable |
|
179 | + * @param array $entries the result array, by reference |
|
180 | + * @param array $sortKeys the array containing the sort keys |
|
181 | + * @param return null |
|
182 | + */ |
|
183 | + private function sort(&$entries, $sortKeys) { |
|
184 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
185 | + array_multisort($sortKeys, SORT_DESC, $entries); |
|
186 | + } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
187 | + array_multisort($sortKeys, SORT_ASC, $entries); |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * returns the available groups |
|
193 | + * @param string $search a search string |
|
194 | + * @return \OCP\IGroup[] |
|
195 | + */ |
|
196 | + public function getGroups($search = '') { |
|
197 | + if ($this->isAdmin) { |
|
198 | + return $this->groupManager->search($search); |
|
199 | + } else { |
|
200 | + $userObject = $this->userSession->getUser(); |
|
201 | + if ($userObject !== null) { |
|
202 | + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
203 | + } else { |
|
204 | + $groups = []; |
|
205 | + } |
|
206 | + |
|
207 | + return $groups; |
|
208 | + } |
|
209 | + } |
|
210 | 210 | } |
@@ -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 | } |