@@ -29,68 +29,68 @@ |
||
29 | 29 | use OCP\Diagnostics\IQueryLogger; |
30 | 30 | |
31 | 31 | class QueryLogger implements IQueryLogger { |
32 | - /** |
|
33 | - * @var \OC\Diagnostics\Query |
|
34 | - */ |
|
35 | - protected $activeQuery; |
|
32 | + /** |
|
33 | + * @var \OC\Diagnostics\Query |
|
34 | + */ |
|
35 | + protected $activeQuery; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @var CappedMemoryCache |
|
39 | - */ |
|
40 | - protected $queries; |
|
37 | + /** |
|
38 | + * @var CappedMemoryCache |
|
39 | + */ |
|
40 | + protected $queries; |
|
41 | 41 | |
42 | - /** |
|
43 | - * QueryLogger constructor. |
|
44 | - */ |
|
45 | - public function __construct() { |
|
46 | - $this->queries = new CappedMemoryCache(1024); |
|
47 | - } |
|
42 | + /** |
|
43 | + * QueryLogger constructor. |
|
44 | + */ |
|
45 | + public function __construct() { |
|
46 | + $this->queries = new CappedMemoryCache(1024); |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | - /** |
|
51 | - * @var bool - Module needs to be activated by some app |
|
52 | - */ |
|
53 | - private $activated = false; |
|
50 | + /** |
|
51 | + * @var bool - Module needs to be activated by some app |
|
52 | + */ |
|
53 | + private $activated = false; |
|
54 | 54 | |
55 | - /** |
|
56 | - * @inheritdoc |
|
57 | - */ |
|
58 | - public function startQuery($sql, array $params = null, array $types = null) { |
|
59 | - if ($this->activated) { |
|
60 | - $this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack()); |
|
61 | - } |
|
62 | - } |
|
55 | + /** |
|
56 | + * @inheritdoc |
|
57 | + */ |
|
58 | + public function startQuery($sql, array $params = null, array $types = null) { |
|
59 | + if ($this->activated) { |
|
60 | + $this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack()); |
|
61 | + } |
|
62 | + } |
|
63 | 63 | |
64 | - private function getStack() { |
|
65 | - $stack = debug_backtrace(); |
|
66 | - array_shift($stack); |
|
67 | - array_shift($stack); |
|
68 | - array_shift($stack); |
|
69 | - return $stack; |
|
70 | - } |
|
64 | + private function getStack() { |
|
65 | + $stack = debug_backtrace(); |
|
66 | + array_shift($stack); |
|
67 | + array_shift($stack); |
|
68 | + array_shift($stack); |
|
69 | + return $stack; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @inheritdoc |
|
74 | - */ |
|
75 | - public function stopQuery() { |
|
76 | - if ($this->activated && $this->activeQuery) { |
|
77 | - $this->activeQuery->end(microtime(true)); |
|
78 | - $this->queries[] = $this->activeQuery; |
|
79 | - $this->activeQuery = null; |
|
80 | - } |
|
81 | - } |
|
72 | + /** |
|
73 | + * @inheritdoc |
|
74 | + */ |
|
75 | + public function stopQuery() { |
|
76 | + if ($this->activated && $this->activeQuery) { |
|
77 | + $this->activeQuery->end(microtime(true)); |
|
78 | + $this->queries[] = $this->activeQuery; |
|
79 | + $this->activeQuery = null; |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @inheritdoc |
|
85 | - */ |
|
86 | - public function getQueries() { |
|
87 | - return $this->queries->getData(); |
|
88 | - } |
|
83 | + /** |
|
84 | + * @inheritdoc |
|
85 | + */ |
|
86 | + public function getQueries() { |
|
87 | + return $this->queries->getData(); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @inheritdoc |
|
92 | - */ |
|
93 | - public function activate() { |
|
94 | - $this->activated = true; |
|
95 | - } |
|
90 | + /** |
|
91 | + * @inheritdoc |
|
92 | + */ |
|
93 | + public function activate() { |
|
94 | + $this->activated = true; |
|
95 | + } |
|
96 | 96 | } |
@@ -24,15 +24,15 @@ |
||
24 | 24 | namespace OC\Hooks; |
25 | 25 | |
26 | 26 | abstract class LegacyEmitter extends BasicEmitter { |
27 | - /** |
|
28 | - * @param string $scope |
|
29 | - * @param string $method |
|
30 | - * @param array $arguments |
|
31 | - * |
|
32 | - * @suppress PhanAccessMethodProtected |
|
33 | - */ |
|
34 | - protected function emit($scope, $method, array $arguments = array()) { |
|
35 | - \OC_Hook::emit($scope, $method, $arguments); |
|
36 | - parent::emit($scope, $method, $arguments); |
|
37 | - } |
|
27 | + /** |
|
28 | + * @param string $scope |
|
29 | + * @param string $method |
|
30 | + * @param array $arguments |
|
31 | + * |
|
32 | + * @suppress PhanAccessMethodProtected |
|
33 | + */ |
|
34 | + protected function emit($scope, $method, array $arguments = array()) { |
|
35 | + \OC_Hook::emit($scope, $method, $arguments); |
|
36 | + parent::emit($scope, $method, $arguments); |
|
37 | + } |
|
38 | 38 | } |
@@ -24,14 +24,14 @@ |
||
24 | 24 | namespace OC\Hooks; |
25 | 25 | |
26 | 26 | class PublicEmitter extends BasicEmitter { |
27 | - /** |
|
28 | - * @param string $scope |
|
29 | - * @param string $method |
|
30 | - * @param array $arguments optional |
|
31 | - * |
|
32 | - * @suppress PhanAccessMethodProtected |
|
33 | - */ |
|
34 | - public function emit($scope, $method, array $arguments = array()) { |
|
35 | - parent::emit($scope, $method, $arguments); |
|
36 | - } |
|
27 | + /** |
|
28 | + * @param string $scope |
|
29 | + * @param string $method |
|
30 | + * @param array $arguments optional |
|
31 | + * |
|
32 | + * @suppress PhanAccessMethodProtected |
|
33 | + */ |
|
34 | + public function emit($scope, $method, array $arguments = array()) { |
|
35 | + parent::emit($scope, $method, $arguments); |
|
36 | + } |
|
37 | 37 | } |
@@ -271,7 +271,7 @@ |
||
271 | 271 | * Return the explicit version for the aliases; current, next, prev, latest |
272 | 272 | * |
273 | 273 | * @param string $alias |
274 | - * @return mixed|null|string |
|
274 | + * @return string |
|
275 | 275 | */ |
276 | 276 | public function getMigration($alias) { |
277 | 277 | switch($alias) { |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | } |
67 | 67 | |
68 | 68 | if ($appName === 'core') { |
69 | - $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
69 | + $this->migrationsPath = \OC::$SERVERROOT.'/core/Migrations'; |
|
70 | 70 | $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
71 | 71 | } else { |
72 | 72 | if (null === $appLocator) { |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | $appPath = $appLocator->getAppPath($appName); |
76 | 76 | $namespace = App::buildAppNamespace($appName); |
77 | 77 | $this->migrationsPath = "$appPath/lib/Migration"; |
78 | - $this->migrationsNamespace = $namespace . '\\Migration'; |
|
78 | + $this->migrationsNamespace = $namespace.'\\Migration'; |
|
79 | 79 | } |
80 | 80 | } |
81 | 81 | |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | \RegexIterator::GET_MATCH); |
198 | 198 | |
199 | 199 | $files = array_keys(iterator_to_array($iterator)); |
200 | - uasort($files, function ($a, $b) { |
|
200 | + uasort($files, function($a, $b) { |
|
201 | 201 | preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
202 | 202 | preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
203 | 203 | if (!empty($matchA) && !empty($matchB)) { |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | * @return string |
276 | 276 | */ |
277 | 277 | public function getMigrationsTableName() { |
278 | - return $this->connection->getPrefix() . 'migrations'; |
|
278 | + return $this->connection->getPrefix().'migrations'; |
|
279 | 279 | } |
280 | 280 | |
281 | 281 | /** |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | * @return mixed|null|string |
304 | 304 | */ |
305 | 305 | public function getMigration($alias) { |
306 | - switch($alias) { |
|
306 | + switch ($alias) { |
|
307 | 307 | case 'current': |
308 | 308 | return $this->getCurrentVersion(); |
309 | 309 | case 'next': |
@@ -35,431 +35,431 @@ |
||
35 | 35 | |
36 | 36 | class MigrationService { |
37 | 37 | |
38 | - /** @var boolean */ |
|
39 | - private $migrationTableCreated; |
|
40 | - /** @var array */ |
|
41 | - private $migrations; |
|
42 | - /** @var IOutput */ |
|
43 | - private $output; |
|
44 | - /** @var Connection */ |
|
45 | - private $connection; |
|
46 | - /** @var string */ |
|
47 | - private $appName; |
|
48 | - |
|
49 | - /** |
|
50 | - * MigrationService constructor. |
|
51 | - * |
|
52 | - * @param $appName |
|
53 | - * @param IDBConnection $connection |
|
54 | - * @param AppLocator $appLocator |
|
55 | - * @param IOutput|null $output |
|
56 | - * @throws \Exception |
|
57 | - */ |
|
58 | - public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
|
59 | - $this->appName = $appName; |
|
60 | - $this->connection = $connection; |
|
61 | - $this->output = $output; |
|
62 | - if (null === $this->output) { |
|
63 | - $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName); |
|
64 | - } |
|
65 | - |
|
66 | - if ($appName === 'core') { |
|
67 | - $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
68 | - $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
|
69 | - } else { |
|
70 | - if (null === $appLocator) { |
|
71 | - $appLocator = new AppLocator(); |
|
72 | - } |
|
73 | - $appPath = $appLocator->getAppPath($appName); |
|
74 | - $namespace = App::buildAppNamespace($appName); |
|
75 | - $this->migrationsPath = "$appPath/lib/Migration"; |
|
76 | - $this->migrationsNamespace = $namespace . '\\Migration'; |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Returns the name of the app for which this migration is executed |
|
82 | - * |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function getApp() { |
|
86 | - return $this->appName; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @return bool |
|
91 | - * @codeCoverageIgnore - this will implicitly tested on installation |
|
92 | - */ |
|
93 | - private function createMigrationTable() { |
|
94 | - if ($this->migrationTableCreated) { |
|
95 | - return false; |
|
96 | - } |
|
97 | - |
|
98 | - $schema = new SchemaWrapper($this->connection); |
|
99 | - |
|
100 | - /** |
|
101 | - * We drop the table when it has different columns or the definition does not |
|
102 | - * match. E.g. ownCloud uses a length of 177 for app and 14 for version. |
|
103 | - */ |
|
104 | - try { |
|
105 | - $table = $schema->getTable('migrations'); |
|
106 | - $columns = $table->getColumns(); |
|
107 | - |
|
108 | - if (count($columns) === 2) { |
|
109 | - try { |
|
110 | - $column = $table->getColumn('app'); |
|
111 | - $schemaMismatch = $column->getLength() !== 255; |
|
112 | - |
|
113 | - if (!$schemaMismatch) { |
|
114 | - $column = $table->getColumn('version'); |
|
115 | - $schemaMismatch = $column->getLength() !== 255; |
|
116 | - } |
|
117 | - } catch (SchemaException $e) { |
|
118 | - // One of the columns is missing |
|
119 | - $schemaMismatch = true; |
|
120 | - } |
|
121 | - |
|
122 | - if (!$schemaMismatch) { |
|
123 | - // Table exists and schema matches: return back! |
|
124 | - $this->migrationTableCreated = true; |
|
125 | - return false; |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - // Drop the table, when it didn't match our expectations. |
|
130 | - $this->connection->dropTable('migrations'); |
|
131 | - |
|
132 | - // Recreate the schema after the table was dropped. |
|
133 | - $schema = new SchemaWrapper($this->connection); |
|
134 | - |
|
135 | - } catch (SchemaException $e) { |
|
136 | - // Table not found, no need to panic, we will create it. |
|
137 | - } |
|
138 | - |
|
139 | - $table = $schema->createTable('migrations'); |
|
140 | - $table->addColumn('app', Type::STRING, ['length' => 255]); |
|
141 | - $table->addColumn('version', Type::STRING, ['length' => 255]); |
|
142 | - $table->setPrimaryKey(['app', 'version']); |
|
143 | - |
|
144 | - $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
145 | - |
|
146 | - $this->migrationTableCreated = true; |
|
147 | - |
|
148 | - return true; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Returns all versions which have already been applied |
|
153 | - * |
|
154 | - * @return string[] |
|
155 | - * @codeCoverageIgnore - no need to test this |
|
156 | - */ |
|
157 | - public function getMigratedVersions() { |
|
158 | - $this->createMigrationTable(); |
|
159 | - $qb = $this->connection->getQueryBuilder(); |
|
160 | - |
|
161 | - $qb->select('version') |
|
162 | - ->from('migrations') |
|
163 | - ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) |
|
164 | - ->orderBy('version'); |
|
165 | - |
|
166 | - $result = $qb->execute(); |
|
167 | - $rows = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
168 | - $result->closeCursor(); |
|
169 | - |
|
170 | - return $rows; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Returns all versions which are available in the migration folder |
|
175 | - * |
|
176 | - * @return array |
|
177 | - */ |
|
178 | - public function getAvailableVersions() { |
|
179 | - $this->ensureMigrationsAreLoaded(); |
|
180 | - return array_map('strval', array_keys($this->migrations)); |
|
181 | - } |
|
182 | - |
|
183 | - protected function findMigrations() { |
|
184 | - $directory = realpath($this->migrationsPath); |
|
185 | - if ($directory === false || !file_exists($directory) || !is_dir($directory)) { |
|
186 | - return []; |
|
187 | - } |
|
188 | - |
|
189 | - $iterator = new \RegexIterator( |
|
190 | - new \RecursiveIteratorIterator( |
|
191 | - new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), |
|
192 | - \RecursiveIteratorIterator::LEAVES_ONLY |
|
193 | - ), |
|
194 | - '#^.+\\/Version[^\\/]{1,255}\\.php$#i', |
|
195 | - \RegexIterator::GET_MATCH); |
|
196 | - |
|
197 | - $files = array_keys(iterator_to_array($iterator)); |
|
198 | - uasort($files, function ($a, $b) { |
|
199 | - preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
|
200 | - preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
|
201 | - if (!empty($matchA) && !empty($matchB)) { |
|
202 | - if ($matchA[1] !== $matchB[1]) { |
|
203 | - return ($matchA[1] < $matchB[1]) ? -1 : 1; |
|
204 | - } |
|
205 | - return ($matchA[2] < $matchB[2]) ? -1 : 1; |
|
206 | - } |
|
207 | - return (basename($a) < basename($b)) ? -1 : 1; |
|
208 | - }); |
|
209 | - |
|
210 | - $migrations = []; |
|
211 | - |
|
212 | - foreach ($files as $file) { |
|
213 | - $className = basename($file, '.php'); |
|
214 | - $version = (string) substr($className, 7); |
|
215 | - if ($version === '0') { |
|
216 | - throw new \InvalidArgumentException( |
|
217 | - "Cannot load a migrations with the name '$version' because it is a reserved number" |
|
218 | - ); |
|
219 | - } |
|
220 | - $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); |
|
221 | - } |
|
222 | - |
|
223 | - return $migrations; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @param string $to |
|
228 | - * @return string[] |
|
229 | - */ |
|
230 | - private function getMigrationsToExecute($to) { |
|
231 | - $knownMigrations = $this->getMigratedVersions(); |
|
232 | - $availableMigrations = $this->getAvailableVersions(); |
|
233 | - |
|
234 | - $toBeExecuted = []; |
|
235 | - foreach ($availableMigrations as $v) { |
|
236 | - if ($to !== 'latest' && $v > $to) { |
|
237 | - continue; |
|
238 | - } |
|
239 | - if ($this->shallBeExecuted($v, $knownMigrations)) { |
|
240 | - $toBeExecuted[] = $v; |
|
241 | - } |
|
242 | - } |
|
243 | - |
|
244 | - return $toBeExecuted; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * @param string $m |
|
249 | - * @param string[] $knownMigrations |
|
250 | - * @return bool |
|
251 | - */ |
|
252 | - private function shallBeExecuted($m, $knownMigrations) { |
|
253 | - if (in_array($m, $knownMigrations)) { |
|
254 | - return false; |
|
255 | - } |
|
256 | - |
|
257 | - return true; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * @param string $version |
|
262 | - */ |
|
263 | - private function markAsExecuted($version) { |
|
264 | - $this->connection->insertIfNotExist('*PREFIX*migrations', [ |
|
265 | - 'app' => $this->appName, |
|
266 | - 'version' => $version |
|
267 | - ]); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Returns the name of the table which holds the already applied versions |
|
272 | - * |
|
273 | - * @return string |
|
274 | - */ |
|
275 | - public function getMigrationsTableName() { |
|
276 | - return $this->connection->getPrefix() . 'migrations'; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Returns the namespace of the version classes |
|
281 | - * |
|
282 | - * @return string |
|
283 | - */ |
|
284 | - public function getMigrationsNamespace() { |
|
285 | - return $this->migrationsNamespace; |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Returns the directory which holds the versions |
|
290 | - * |
|
291 | - * @return string |
|
292 | - */ |
|
293 | - public function getMigrationsDirectory() { |
|
294 | - return $this->migrationsPath; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Return the explicit version for the aliases; current, next, prev, latest |
|
299 | - * |
|
300 | - * @param string $alias |
|
301 | - * @return mixed|null|string |
|
302 | - */ |
|
303 | - public function getMigration($alias) { |
|
304 | - switch($alias) { |
|
305 | - case 'current': |
|
306 | - return $this->getCurrentVersion(); |
|
307 | - case 'next': |
|
308 | - return $this->getRelativeVersion($this->getCurrentVersion(), 1); |
|
309 | - case 'prev': |
|
310 | - return $this->getRelativeVersion($this->getCurrentVersion(), -1); |
|
311 | - case 'latest': |
|
312 | - $this->ensureMigrationsAreLoaded(); |
|
313 | - |
|
314 | - $migrations = $this->getAvailableVersions(); |
|
315 | - return @end($migrations); |
|
316 | - } |
|
317 | - return '0'; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * @param string $version |
|
322 | - * @param int $delta |
|
323 | - * @return null|string |
|
324 | - */ |
|
325 | - private function getRelativeVersion($version, $delta) { |
|
326 | - $this->ensureMigrationsAreLoaded(); |
|
327 | - |
|
328 | - $versions = $this->getAvailableVersions(); |
|
329 | - array_unshift($versions, 0); |
|
330 | - $offset = array_search($version, $versions, true); |
|
331 | - if ($offset === false || !isset($versions[$offset + $delta])) { |
|
332 | - // Unknown version or delta out of bounds. |
|
333 | - return null; |
|
334 | - } |
|
335 | - |
|
336 | - return (string) $versions[$offset + $delta]; |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * @return string |
|
341 | - */ |
|
342 | - private function getCurrentVersion() { |
|
343 | - $m = $this->getMigratedVersions(); |
|
344 | - if (count($m) === 0) { |
|
345 | - return '0'; |
|
346 | - } |
|
347 | - $migrations = array_values($m); |
|
348 | - return @end($migrations); |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * @param string $version |
|
353 | - * @return string |
|
354 | - * @throws \InvalidArgumentException |
|
355 | - */ |
|
356 | - private function getClass($version) { |
|
357 | - $this->ensureMigrationsAreLoaded(); |
|
358 | - |
|
359 | - if (isset($this->migrations[$version])) { |
|
360 | - return $this->migrations[$version]; |
|
361 | - } |
|
362 | - |
|
363 | - throw new \InvalidArgumentException("Version $version is unknown."); |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Allows to set an IOutput implementation which is used for logging progress and messages |
|
368 | - * |
|
369 | - * @param IOutput $output |
|
370 | - */ |
|
371 | - public function setOutput(IOutput $output) { |
|
372 | - $this->output = $output; |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Applies all not yet applied versions up to $to |
|
377 | - * |
|
378 | - * @param string $to |
|
379 | - * @throws \InvalidArgumentException |
|
380 | - */ |
|
381 | - public function migrate($to = 'latest') { |
|
382 | - // read known migrations |
|
383 | - $toBeExecuted = $this->getMigrationsToExecute($to); |
|
384 | - foreach ($toBeExecuted as $version) { |
|
385 | - $this->executeStep($version); |
|
386 | - } |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Get the human readable descriptions for the migration steps to run |
|
391 | - * |
|
392 | - * @param string $to |
|
393 | - * @return string[] [$name => $description] |
|
394 | - */ |
|
395 | - public function describeMigrationStep($to = 'latest') { |
|
396 | - $toBeExecuted = $this->getMigrationsToExecute($to); |
|
397 | - $description = []; |
|
398 | - foreach ($toBeExecuted as $version) { |
|
399 | - $migration = $this->createInstance($version); |
|
400 | - if ($migration->name()) { |
|
401 | - $description[$migration->name()] = $migration->description(); |
|
402 | - } |
|
403 | - } |
|
404 | - return $description; |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * @param string $version |
|
409 | - * @return IMigrationStep |
|
410 | - * @throws \InvalidArgumentException |
|
411 | - */ |
|
412 | - protected function createInstance($version) { |
|
413 | - $class = $this->getClass($version); |
|
414 | - try { |
|
415 | - $s = \OC::$server->query($class); |
|
416 | - |
|
417 | - if (!$s instanceof IMigrationStep) { |
|
418 | - throw new \InvalidArgumentException('Not a valid migration'); |
|
419 | - } |
|
420 | - } catch (QueryException $e) { |
|
421 | - if (class_exists($class)) { |
|
422 | - $s = new $class(); |
|
423 | - } else { |
|
424 | - throw new \InvalidArgumentException("Migration step '$class' is unknown"); |
|
425 | - } |
|
426 | - } |
|
427 | - |
|
428 | - return $s; |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Executes one explicit version |
|
433 | - * |
|
434 | - * @param string $version |
|
435 | - * @throws \InvalidArgumentException |
|
436 | - */ |
|
437 | - public function executeStep($version) { |
|
438 | - $instance = $this->createInstance($version); |
|
439 | - |
|
440 | - $instance->preSchemaChange($this->output, function() { |
|
441 | - return new SchemaWrapper($this->connection); |
|
442 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
443 | - |
|
444 | - $toSchema = $instance->changeSchema($this->output, function() { |
|
445 | - return new SchemaWrapper($this->connection); |
|
446 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
447 | - |
|
448 | - if ($toSchema instanceof SchemaWrapper) { |
|
449 | - $this->connection->migrateToSchema($toSchema->getWrappedSchema()); |
|
450 | - $toSchema->performDropTableCalls(); |
|
451 | - } |
|
452 | - |
|
453 | - $instance->postSchemaChange($this->output, function() { |
|
454 | - return new SchemaWrapper($this->connection); |
|
455 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
456 | - |
|
457 | - $this->markAsExecuted($version); |
|
458 | - } |
|
459 | - |
|
460 | - private function ensureMigrationsAreLoaded() { |
|
461 | - if (empty($this->migrations)) { |
|
462 | - $this->migrations = $this->findMigrations(); |
|
463 | - } |
|
464 | - } |
|
38 | + /** @var boolean */ |
|
39 | + private $migrationTableCreated; |
|
40 | + /** @var array */ |
|
41 | + private $migrations; |
|
42 | + /** @var IOutput */ |
|
43 | + private $output; |
|
44 | + /** @var Connection */ |
|
45 | + private $connection; |
|
46 | + /** @var string */ |
|
47 | + private $appName; |
|
48 | + |
|
49 | + /** |
|
50 | + * MigrationService constructor. |
|
51 | + * |
|
52 | + * @param $appName |
|
53 | + * @param IDBConnection $connection |
|
54 | + * @param AppLocator $appLocator |
|
55 | + * @param IOutput|null $output |
|
56 | + * @throws \Exception |
|
57 | + */ |
|
58 | + public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
|
59 | + $this->appName = $appName; |
|
60 | + $this->connection = $connection; |
|
61 | + $this->output = $output; |
|
62 | + if (null === $this->output) { |
|
63 | + $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName); |
|
64 | + } |
|
65 | + |
|
66 | + if ($appName === 'core') { |
|
67 | + $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
68 | + $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
|
69 | + } else { |
|
70 | + if (null === $appLocator) { |
|
71 | + $appLocator = new AppLocator(); |
|
72 | + } |
|
73 | + $appPath = $appLocator->getAppPath($appName); |
|
74 | + $namespace = App::buildAppNamespace($appName); |
|
75 | + $this->migrationsPath = "$appPath/lib/Migration"; |
|
76 | + $this->migrationsNamespace = $namespace . '\\Migration'; |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Returns the name of the app for which this migration is executed |
|
82 | + * |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function getApp() { |
|
86 | + return $this->appName; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @return bool |
|
91 | + * @codeCoverageIgnore - this will implicitly tested on installation |
|
92 | + */ |
|
93 | + private function createMigrationTable() { |
|
94 | + if ($this->migrationTableCreated) { |
|
95 | + return false; |
|
96 | + } |
|
97 | + |
|
98 | + $schema = new SchemaWrapper($this->connection); |
|
99 | + |
|
100 | + /** |
|
101 | + * We drop the table when it has different columns or the definition does not |
|
102 | + * match. E.g. ownCloud uses a length of 177 for app and 14 for version. |
|
103 | + */ |
|
104 | + try { |
|
105 | + $table = $schema->getTable('migrations'); |
|
106 | + $columns = $table->getColumns(); |
|
107 | + |
|
108 | + if (count($columns) === 2) { |
|
109 | + try { |
|
110 | + $column = $table->getColumn('app'); |
|
111 | + $schemaMismatch = $column->getLength() !== 255; |
|
112 | + |
|
113 | + if (!$schemaMismatch) { |
|
114 | + $column = $table->getColumn('version'); |
|
115 | + $schemaMismatch = $column->getLength() !== 255; |
|
116 | + } |
|
117 | + } catch (SchemaException $e) { |
|
118 | + // One of the columns is missing |
|
119 | + $schemaMismatch = true; |
|
120 | + } |
|
121 | + |
|
122 | + if (!$schemaMismatch) { |
|
123 | + // Table exists and schema matches: return back! |
|
124 | + $this->migrationTableCreated = true; |
|
125 | + return false; |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + // Drop the table, when it didn't match our expectations. |
|
130 | + $this->connection->dropTable('migrations'); |
|
131 | + |
|
132 | + // Recreate the schema after the table was dropped. |
|
133 | + $schema = new SchemaWrapper($this->connection); |
|
134 | + |
|
135 | + } catch (SchemaException $e) { |
|
136 | + // Table not found, no need to panic, we will create it. |
|
137 | + } |
|
138 | + |
|
139 | + $table = $schema->createTable('migrations'); |
|
140 | + $table->addColumn('app', Type::STRING, ['length' => 255]); |
|
141 | + $table->addColumn('version', Type::STRING, ['length' => 255]); |
|
142 | + $table->setPrimaryKey(['app', 'version']); |
|
143 | + |
|
144 | + $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
145 | + |
|
146 | + $this->migrationTableCreated = true; |
|
147 | + |
|
148 | + return true; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Returns all versions which have already been applied |
|
153 | + * |
|
154 | + * @return string[] |
|
155 | + * @codeCoverageIgnore - no need to test this |
|
156 | + */ |
|
157 | + public function getMigratedVersions() { |
|
158 | + $this->createMigrationTable(); |
|
159 | + $qb = $this->connection->getQueryBuilder(); |
|
160 | + |
|
161 | + $qb->select('version') |
|
162 | + ->from('migrations') |
|
163 | + ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) |
|
164 | + ->orderBy('version'); |
|
165 | + |
|
166 | + $result = $qb->execute(); |
|
167 | + $rows = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
168 | + $result->closeCursor(); |
|
169 | + |
|
170 | + return $rows; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Returns all versions which are available in the migration folder |
|
175 | + * |
|
176 | + * @return array |
|
177 | + */ |
|
178 | + public function getAvailableVersions() { |
|
179 | + $this->ensureMigrationsAreLoaded(); |
|
180 | + return array_map('strval', array_keys($this->migrations)); |
|
181 | + } |
|
182 | + |
|
183 | + protected function findMigrations() { |
|
184 | + $directory = realpath($this->migrationsPath); |
|
185 | + if ($directory === false || !file_exists($directory) || !is_dir($directory)) { |
|
186 | + return []; |
|
187 | + } |
|
188 | + |
|
189 | + $iterator = new \RegexIterator( |
|
190 | + new \RecursiveIteratorIterator( |
|
191 | + new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), |
|
192 | + \RecursiveIteratorIterator::LEAVES_ONLY |
|
193 | + ), |
|
194 | + '#^.+\\/Version[^\\/]{1,255}\\.php$#i', |
|
195 | + \RegexIterator::GET_MATCH); |
|
196 | + |
|
197 | + $files = array_keys(iterator_to_array($iterator)); |
|
198 | + uasort($files, function ($a, $b) { |
|
199 | + preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
|
200 | + preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
|
201 | + if (!empty($matchA) && !empty($matchB)) { |
|
202 | + if ($matchA[1] !== $matchB[1]) { |
|
203 | + return ($matchA[1] < $matchB[1]) ? -1 : 1; |
|
204 | + } |
|
205 | + return ($matchA[2] < $matchB[2]) ? -1 : 1; |
|
206 | + } |
|
207 | + return (basename($a) < basename($b)) ? -1 : 1; |
|
208 | + }); |
|
209 | + |
|
210 | + $migrations = []; |
|
211 | + |
|
212 | + foreach ($files as $file) { |
|
213 | + $className = basename($file, '.php'); |
|
214 | + $version = (string) substr($className, 7); |
|
215 | + if ($version === '0') { |
|
216 | + throw new \InvalidArgumentException( |
|
217 | + "Cannot load a migrations with the name '$version' because it is a reserved number" |
|
218 | + ); |
|
219 | + } |
|
220 | + $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); |
|
221 | + } |
|
222 | + |
|
223 | + return $migrations; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @param string $to |
|
228 | + * @return string[] |
|
229 | + */ |
|
230 | + private function getMigrationsToExecute($to) { |
|
231 | + $knownMigrations = $this->getMigratedVersions(); |
|
232 | + $availableMigrations = $this->getAvailableVersions(); |
|
233 | + |
|
234 | + $toBeExecuted = []; |
|
235 | + foreach ($availableMigrations as $v) { |
|
236 | + if ($to !== 'latest' && $v > $to) { |
|
237 | + continue; |
|
238 | + } |
|
239 | + if ($this->shallBeExecuted($v, $knownMigrations)) { |
|
240 | + $toBeExecuted[] = $v; |
|
241 | + } |
|
242 | + } |
|
243 | + |
|
244 | + return $toBeExecuted; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * @param string $m |
|
249 | + * @param string[] $knownMigrations |
|
250 | + * @return bool |
|
251 | + */ |
|
252 | + private function shallBeExecuted($m, $knownMigrations) { |
|
253 | + if (in_array($m, $knownMigrations)) { |
|
254 | + return false; |
|
255 | + } |
|
256 | + |
|
257 | + return true; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * @param string $version |
|
262 | + */ |
|
263 | + private function markAsExecuted($version) { |
|
264 | + $this->connection->insertIfNotExist('*PREFIX*migrations', [ |
|
265 | + 'app' => $this->appName, |
|
266 | + 'version' => $version |
|
267 | + ]); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Returns the name of the table which holds the already applied versions |
|
272 | + * |
|
273 | + * @return string |
|
274 | + */ |
|
275 | + public function getMigrationsTableName() { |
|
276 | + return $this->connection->getPrefix() . 'migrations'; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Returns the namespace of the version classes |
|
281 | + * |
|
282 | + * @return string |
|
283 | + */ |
|
284 | + public function getMigrationsNamespace() { |
|
285 | + return $this->migrationsNamespace; |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Returns the directory which holds the versions |
|
290 | + * |
|
291 | + * @return string |
|
292 | + */ |
|
293 | + public function getMigrationsDirectory() { |
|
294 | + return $this->migrationsPath; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Return the explicit version for the aliases; current, next, prev, latest |
|
299 | + * |
|
300 | + * @param string $alias |
|
301 | + * @return mixed|null|string |
|
302 | + */ |
|
303 | + public function getMigration($alias) { |
|
304 | + switch($alias) { |
|
305 | + case 'current': |
|
306 | + return $this->getCurrentVersion(); |
|
307 | + case 'next': |
|
308 | + return $this->getRelativeVersion($this->getCurrentVersion(), 1); |
|
309 | + case 'prev': |
|
310 | + return $this->getRelativeVersion($this->getCurrentVersion(), -1); |
|
311 | + case 'latest': |
|
312 | + $this->ensureMigrationsAreLoaded(); |
|
313 | + |
|
314 | + $migrations = $this->getAvailableVersions(); |
|
315 | + return @end($migrations); |
|
316 | + } |
|
317 | + return '0'; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * @param string $version |
|
322 | + * @param int $delta |
|
323 | + * @return null|string |
|
324 | + */ |
|
325 | + private function getRelativeVersion($version, $delta) { |
|
326 | + $this->ensureMigrationsAreLoaded(); |
|
327 | + |
|
328 | + $versions = $this->getAvailableVersions(); |
|
329 | + array_unshift($versions, 0); |
|
330 | + $offset = array_search($version, $versions, true); |
|
331 | + if ($offset === false || !isset($versions[$offset + $delta])) { |
|
332 | + // Unknown version or delta out of bounds. |
|
333 | + return null; |
|
334 | + } |
|
335 | + |
|
336 | + return (string) $versions[$offset + $delta]; |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * @return string |
|
341 | + */ |
|
342 | + private function getCurrentVersion() { |
|
343 | + $m = $this->getMigratedVersions(); |
|
344 | + if (count($m) === 0) { |
|
345 | + return '0'; |
|
346 | + } |
|
347 | + $migrations = array_values($m); |
|
348 | + return @end($migrations); |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * @param string $version |
|
353 | + * @return string |
|
354 | + * @throws \InvalidArgumentException |
|
355 | + */ |
|
356 | + private function getClass($version) { |
|
357 | + $this->ensureMigrationsAreLoaded(); |
|
358 | + |
|
359 | + if (isset($this->migrations[$version])) { |
|
360 | + return $this->migrations[$version]; |
|
361 | + } |
|
362 | + |
|
363 | + throw new \InvalidArgumentException("Version $version is unknown."); |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Allows to set an IOutput implementation which is used for logging progress and messages |
|
368 | + * |
|
369 | + * @param IOutput $output |
|
370 | + */ |
|
371 | + public function setOutput(IOutput $output) { |
|
372 | + $this->output = $output; |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Applies all not yet applied versions up to $to |
|
377 | + * |
|
378 | + * @param string $to |
|
379 | + * @throws \InvalidArgumentException |
|
380 | + */ |
|
381 | + public function migrate($to = 'latest') { |
|
382 | + // read known migrations |
|
383 | + $toBeExecuted = $this->getMigrationsToExecute($to); |
|
384 | + foreach ($toBeExecuted as $version) { |
|
385 | + $this->executeStep($version); |
|
386 | + } |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Get the human readable descriptions for the migration steps to run |
|
391 | + * |
|
392 | + * @param string $to |
|
393 | + * @return string[] [$name => $description] |
|
394 | + */ |
|
395 | + public function describeMigrationStep($to = 'latest') { |
|
396 | + $toBeExecuted = $this->getMigrationsToExecute($to); |
|
397 | + $description = []; |
|
398 | + foreach ($toBeExecuted as $version) { |
|
399 | + $migration = $this->createInstance($version); |
|
400 | + if ($migration->name()) { |
|
401 | + $description[$migration->name()] = $migration->description(); |
|
402 | + } |
|
403 | + } |
|
404 | + return $description; |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * @param string $version |
|
409 | + * @return IMigrationStep |
|
410 | + * @throws \InvalidArgumentException |
|
411 | + */ |
|
412 | + protected function createInstance($version) { |
|
413 | + $class = $this->getClass($version); |
|
414 | + try { |
|
415 | + $s = \OC::$server->query($class); |
|
416 | + |
|
417 | + if (!$s instanceof IMigrationStep) { |
|
418 | + throw new \InvalidArgumentException('Not a valid migration'); |
|
419 | + } |
|
420 | + } catch (QueryException $e) { |
|
421 | + if (class_exists($class)) { |
|
422 | + $s = new $class(); |
|
423 | + } else { |
|
424 | + throw new \InvalidArgumentException("Migration step '$class' is unknown"); |
|
425 | + } |
|
426 | + } |
|
427 | + |
|
428 | + return $s; |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Executes one explicit version |
|
433 | + * |
|
434 | + * @param string $version |
|
435 | + * @throws \InvalidArgumentException |
|
436 | + */ |
|
437 | + public function executeStep($version) { |
|
438 | + $instance = $this->createInstance($version); |
|
439 | + |
|
440 | + $instance->preSchemaChange($this->output, function() { |
|
441 | + return new SchemaWrapper($this->connection); |
|
442 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
443 | + |
|
444 | + $toSchema = $instance->changeSchema($this->output, function() { |
|
445 | + return new SchemaWrapper($this->connection); |
|
446 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
447 | + |
|
448 | + if ($toSchema instanceof SchemaWrapper) { |
|
449 | + $this->connection->migrateToSchema($toSchema->getWrappedSchema()); |
|
450 | + $toSchema->performDropTableCalls(); |
|
451 | + } |
|
452 | + |
|
453 | + $instance->postSchemaChange($this->output, function() { |
|
454 | + return new SchemaWrapper($this->connection); |
|
455 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
456 | + |
|
457 | + $this->markAsExecuted($version); |
|
458 | + } |
|
459 | + |
|
460 | + private function ensureMigrationsAreLoaded() { |
|
461 | + if (empty($this->migrations)) { |
|
462 | + $this->migrations = $this->findMigrations(); |
|
463 | + } |
|
464 | + } |
|
465 | 465 | } |
@@ -60,14 +60,14 @@ discard block |
||
60 | 60 | $appName = $input->getArgument('app'); |
61 | 61 | $version = $input->getArgument('version'); |
62 | 62 | |
63 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
63 | + if (!preg_match('/^\d{1,16}$/', $version)) { |
|
64 | 64 | $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
65 | 65 | return 1; |
66 | 66 | } |
67 | 67 | |
68 | - $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
68 | + $schemaFile = $this->appManager->getAppPath($appName).'/appinfo/database.xml'; |
|
69 | 69 | if (!file_exists($schemaFile)) { |
70 | - $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
70 | + $output->writeln('<error>App '.$appName.' does not have a database.xml file</error>'); |
|
71 | 71 | return 2; |
72 | 72 | } |
73 | 73 | |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
81 | 81 | |
82 | 82 | $date = date('YmdHis'); |
83 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
83 | + $path = $this->generateMigration($ms, 'Version'.$version.'Date'.$date, $schemaBody); |
|
84 | 84 | |
85 | 85 | $output->writeln("New migration class has been generated to <info>$path</info>"); |
86 | 86 | return 0; |
@@ -36,166 +36,166 @@ |
||
36 | 36 | |
37 | 37 | class GenerateFromSchemaFileCommand extends GenerateCommand { |
38 | 38 | |
39 | - /** @var IConfig */ |
|
40 | - protected $config; |
|
39 | + /** @var IConfig */ |
|
40 | + protected $config; |
|
41 | 41 | |
42 | - public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
43 | - parent::__construct($connection, $appManager); |
|
44 | - $this->config = $config; |
|
45 | - } |
|
42 | + public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
43 | + parent::__construct($connection, $appManager); |
|
44 | + $this->config = $config; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | - protected function configure() { |
|
49 | - parent::configure(); |
|
48 | + protected function configure() { |
|
49 | + parent::configure(); |
|
50 | 50 | |
51 | - $this->setName('migrations:generate-from-schema'); |
|
52 | - } |
|
51 | + $this->setName('migrations:generate-from-schema'); |
|
52 | + } |
|
53 | 53 | |
54 | - public function execute(InputInterface $input, OutputInterface $output) { |
|
55 | - $appName = $input->getArgument('app'); |
|
56 | - $version = $input->getArgument('version'); |
|
54 | + public function execute(InputInterface $input, OutputInterface $output) { |
|
55 | + $appName = $input->getArgument('app'); |
|
56 | + $version = $input->getArgument('version'); |
|
57 | 57 | |
58 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
59 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
60 | - return 1; |
|
61 | - } |
|
58 | + if (!preg_match('/^\d{1,16}$/',$version)) { |
|
59 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
60 | + return 1; |
|
61 | + } |
|
62 | 62 | |
63 | - $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
64 | - if (!file_exists($schemaFile)) { |
|
65 | - $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
66 | - return 2; |
|
67 | - } |
|
63 | + $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
64 | + if (!file_exists($schemaFile)) { |
|
65 | + $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
66 | + return 2; |
|
67 | + } |
|
68 | 68 | |
69 | - $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
70 | - $schema = new Schema(); |
|
71 | - $reader->loadSchemaFromFile($schemaFile, $schema); |
|
69 | + $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
70 | + $schema = new Schema(); |
|
71 | + $reader->loadSchemaFromFile($schemaFile, $schema); |
|
72 | 72 | |
73 | - $schemaBody = $this->schemaToMigration($schema); |
|
73 | + $schemaBody = $this->schemaToMigration($schema); |
|
74 | 74 | |
75 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
75 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
76 | 76 | |
77 | - $date = date('YmdHis'); |
|
78 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
77 | + $date = date('YmdHis'); |
|
78 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
79 | 79 | |
80 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
81 | - return 0; |
|
82 | - } |
|
80 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
81 | + return 0; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @param Schema $schema |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - protected function schemaToMigration(Schema $schema) { |
|
89 | - $content = <<<'EOT' |
|
84 | + /** |
|
85 | + * @param Schema $schema |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + protected function schemaToMigration(Schema $schema) { |
|
89 | + $content = <<<'EOT' |
|
90 | 90 | /** @var ISchemaWrapper $schema */ |
91 | 91 | $schema = $schemaClosure(); |
92 | 92 | |
93 | 93 | EOT; |
94 | 94 | |
95 | - foreach ($schema->getTables() as $table) { |
|
96 | - $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
95 | + foreach ($schema->getTables() as $table) { |
|
96 | + $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
97 | 97 | |
98 | 98 | if (!$schema->hasTable('{{table-name}}')) { |
99 | 99 | $table = $schema->createTable('{{table-name}}'); |
100 | 100 | |
101 | 101 | EOT |
102 | - ); |
|
102 | + ); |
|
103 | 103 | |
104 | - foreach ($table->getColumns() as $column) { |
|
105 | - $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
104 | + foreach ($table->getColumns() as $column) { |
|
105 | + $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
106 | 106 | $table->addColumn('{{name}}', '{{type}}', [ |
107 | 107 | |
108 | 108 | EOT |
109 | - ); |
|
110 | - if ($column->getAutoincrement()) { |
|
111 | - $content .= <<<'EOT' |
|
109 | + ); |
|
110 | + if ($column->getAutoincrement()) { |
|
111 | + $content .= <<<'EOT' |
|
112 | 112 | 'autoincrement' => true, |
113 | 113 | |
114 | 114 | EOT; |
115 | - } |
|
116 | - $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
115 | + } |
|
116 | + $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
117 | 117 | 'notnull' => {{notnull}}, |
118 | 118 | |
119 | 119 | EOT |
120 | - ); |
|
121 | - if ($column->getLength() !== null) { |
|
122 | - $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
120 | + ); |
|
121 | + if ($column->getLength() !== null) { |
|
122 | + $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
123 | 123 | 'length' => {{length}}, |
124 | 124 | |
125 | 125 | EOT |
126 | - ); |
|
127 | - } |
|
128 | - $default = $column->getDefault(); |
|
129 | - if ($default !== null) { |
|
130 | - $default = is_numeric($default) ? $default : "'$default'"; |
|
131 | - $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
126 | + ); |
|
127 | + } |
|
128 | + $default = $column->getDefault(); |
|
129 | + if ($default !== null) { |
|
130 | + $default = is_numeric($default) ? $default : "'$default'"; |
|
131 | + $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
132 | 132 | 'default' => {{default}}, |
133 | 133 | |
134 | 134 | EOT |
135 | - ); |
|
136 | - } |
|
137 | - if ($column->getUnsigned()) { |
|
138 | - $content .= <<<'EOT' |
|
135 | + ); |
|
136 | + } |
|
137 | + if ($column->getUnsigned()) { |
|
138 | + $content .= <<<'EOT' |
|
139 | 139 | 'unsigned' => true, |
140 | 140 | |
141 | 141 | EOT; |
142 | - } |
|
142 | + } |
|
143 | 143 | |
144 | - $content .= <<<'EOT' |
|
144 | + $content .= <<<'EOT' |
|
145 | 145 | ]); |
146 | 146 | |
147 | 147 | EOT; |
148 | - } |
|
148 | + } |
|
149 | 149 | |
150 | - $content .= <<<'EOT' |
|
150 | + $content .= <<<'EOT' |
|
151 | 151 | |
152 | 152 | EOT; |
153 | 153 | |
154 | - $primaryKey = $table->getPrimaryKey(); |
|
155 | - if ($primaryKey !== null) { |
|
156 | - $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
154 | + $primaryKey = $table->getPrimaryKey(); |
|
155 | + if ($primaryKey !== null) { |
|
156 | + $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
157 | 157 | $table->setPrimaryKey(['{{columns}}']); |
158 | 158 | |
159 | 159 | EOT |
160 | - ); |
|
161 | - } |
|
162 | - |
|
163 | - foreach ($table->getIndexes() as $index) { |
|
164 | - if ($index->isPrimary()) { |
|
165 | - continue; |
|
166 | - } |
|
167 | - |
|
168 | - if ($index->isUnique()) { |
|
169 | - $content .= str_replace( |
|
170 | - ['{{columns}}', '{{name}}'], |
|
171 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
172 | - <<<'EOT' |
|
160 | + ); |
|
161 | + } |
|
162 | + |
|
163 | + foreach ($table->getIndexes() as $index) { |
|
164 | + if ($index->isPrimary()) { |
|
165 | + continue; |
|
166 | + } |
|
167 | + |
|
168 | + if ($index->isUnique()) { |
|
169 | + $content .= str_replace( |
|
170 | + ['{{columns}}', '{{name}}'], |
|
171 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
172 | + <<<'EOT' |
|
173 | 173 | $table->addUniqueIndex(['{{columns}}'], '{{name}}'); |
174 | 174 | |
175 | 175 | EOT |
176 | - ); |
|
177 | - } else { |
|
178 | - $content .= str_replace( |
|
179 | - ['{{columns}}', '{{name}}'], |
|
180 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
181 | - <<<'EOT' |
|
176 | + ); |
|
177 | + } else { |
|
178 | + $content .= str_replace( |
|
179 | + ['{{columns}}', '{{name}}'], |
|
180 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
181 | + <<<'EOT' |
|
182 | 182 | $table->addIndex(['{{columns}}'], '{{name}}'); |
183 | 183 | |
184 | 184 | EOT |
185 | - ); |
|
186 | - } |
|
187 | - } |
|
185 | + ); |
|
186 | + } |
|
187 | + } |
|
188 | 188 | |
189 | - $content .= <<<'EOT' |
|
189 | + $content .= <<<'EOT' |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | EOT; |
193 | - } |
|
193 | + } |
|
194 | 194 | |
195 | - $content .= <<<'EOT' |
|
195 | + $content .= <<<'EOT' |
|
196 | 196 | return $schema; |
197 | 197 | EOT; |
198 | 198 | |
199 | - return $content; |
|
200 | - } |
|
199 | + return $content; |
|
200 | + } |
|
201 | 201 | } |
@@ -27,72 +27,72 @@ |
||
27 | 27 | |
28 | 28 | class FileDeleted implements ISetting { |
29 | 29 | |
30 | - /** @var IL10N */ |
|
31 | - protected $l; |
|
30 | + /** @var IL10N */ |
|
31 | + protected $l; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param IL10N $l |
|
35 | - */ |
|
36 | - public function __construct(IL10N $l) { |
|
37 | - $this->l = $l; |
|
38 | - } |
|
33 | + /** |
|
34 | + * @param IL10N $l |
|
35 | + */ |
|
36 | + public function __construct(IL10N $l) { |
|
37 | + $this->l = $l; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string Lowercase a-z and underscore only identifier |
|
42 | - * @since 11.0.0 |
|
43 | - */ |
|
44 | - public function getIdentifier() { |
|
45 | - return 'file_deleted'; |
|
46 | - } |
|
40 | + /** |
|
41 | + * @return string Lowercase a-z and underscore only identifier |
|
42 | + * @since 11.0.0 |
|
43 | + */ |
|
44 | + public function getIdentifier() { |
|
45 | + return 'file_deleted'; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return string A translated string |
|
50 | - * @since 11.0.0 |
|
51 | - */ |
|
52 | - public function getName() { |
|
53 | - return $this->l->t('A file or folder has been <strong>deleted</strong>'); |
|
54 | - } |
|
48 | + /** |
|
49 | + * @return string A translated string |
|
50 | + * @since 11.0.0 |
|
51 | + */ |
|
52 | + public function getName() { |
|
53 | + return $this->l->t('A file or folder has been <strong>deleted</strong>'); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return int whether the filter should be rather on the top or bottom of |
|
58 | - * the admin section. The filters are arranged in ascending order of the |
|
59 | - * priority values. It is required to return a value between 0 and 100. |
|
60 | - * @since 11.0.0 |
|
61 | - */ |
|
62 | - public function getPriority() { |
|
63 | - return 3; |
|
64 | - } |
|
56 | + /** |
|
57 | + * @return int whether the filter should be rather on the top or bottom of |
|
58 | + * the admin section. The filters are arranged in ascending order of the |
|
59 | + * priority values. It is required to return a value between 0 and 100. |
|
60 | + * @since 11.0.0 |
|
61 | + */ |
|
62 | + public function getPriority() { |
|
63 | + return 3; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return bool True when the option can be changed for the stream |
|
68 | - * @since 11.0.0 |
|
69 | - */ |
|
70 | - public function canChangeStream() { |
|
71 | - return true; |
|
72 | - } |
|
66 | + /** |
|
67 | + * @return bool True when the option can be changed for the stream |
|
68 | + * @since 11.0.0 |
|
69 | + */ |
|
70 | + public function canChangeStream() { |
|
71 | + return true; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return bool True when the option can be changed for the stream |
|
76 | - * @since 11.0.0 |
|
77 | - */ |
|
78 | - public function isDefaultEnabledStream() { |
|
79 | - return true; |
|
80 | - } |
|
74 | + /** |
|
75 | + * @return bool True when the option can be changed for the stream |
|
76 | + * @since 11.0.0 |
|
77 | + */ |
|
78 | + public function isDefaultEnabledStream() { |
|
79 | + return true; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return bool True when the option can be changed for the mail |
|
84 | - * @since 11.0.0 |
|
85 | - */ |
|
86 | - public function canChangeMail() { |
|
87 | - return true; |
|
88 | - } |
|
82 | + /** |
|
83 | + * @return bool True when the option can be changed for the mail |
|
84 | + * @since 11.0.0 |
|
85 | + */ |
|
86 | + public function canChangeMail() { |
|
87 | + return true; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @return bool True when the option can be changed for the stream |
|
92 | - * @since 11.0.0 |
|
93 | - */ |
|
94 | - public function isDefaultEnabledMail() { |
|
95 | - return false; |
|
96 | - } |
|
90 | + /** |
|
91 | + * @return bool True when the option can be changed for the stream |
|
92 | + * @since 11.0.0 |
|
93 | + */ |
|
94 | + public function isDefaultEnabledMail() { |
|
95 | + return false; |
|
96 | + } |
|
97 | 97 | } |
98 | 98 |
@@ -27,72 +27,72 @@ |
||
27 | 27 | |
28 | 28 | class FileRestored implements ISetting { |
29 | 29 | |
30 | - /** @var IL10N */ |
|
31 | - protected $l; |
|
30 | + /** @var IL10N */ |
|
31 | + protected $l; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param IL10N $l |
|
35 | - */ |
|
36 | - public function __construct(IL10N $l) { |
|
37 | - $this->l = $l; |
|
38 | - } |
|
33 | + /** |
|
34 | + * @param IL10N $l |
|
35 | + */ |
|
36 | + public function __construct(IL10N $l) { |
|
37 | + $this->l = $l; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string Lowercase a-z and underscore only identifier |
|
42 | - * @since 11.0.0 |
|
43 | - */ |
|
44 | - public function getIdentifier() { |
|
45 | - return 'file_restored'; |
|
46 | - } |
|
40 | + /** |
|
41 | + * @return string Lowercase a-z and underscore only identifier |
|
42 | + * @since 11.0.0 |
|
43 | + */ |
|
44 | + public function getIdentifier() { |
|
45 | + return 'file_restored'; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return string A translated string |
|
50 | - * @since 11.0.0 |
|
51 | - */ |
|
52 | - public function getName() { |
|
53 | - return $this->l->t('A file or folder has been <strong>restored</strong>'); |
|
54 | - } |
|
48 | + /** |
|
49 | + * @return string A translated string |
|
50 | + * @since 11.0.0 |
|
51 | + */ |
|
52 | + public function getName() { |
|
53 | + return $this->l->t('A file or folder has been <strong>restored</strong>'); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return int whether the filter should be rather on the top or bottom of |
|
58 | - * the admin section. The filters are arranged in ascending order of the |
|
59 | - * priority values. It is required to return a value between 0 and 100. |
|
60 | - * @since 11.0.0 |
|
61 | - */ |
|
62 | - public function getPriority() { |
|
63 | - return 4; |
|
64 | - } |
|
56 | + /** |
|
57 | + * @return int whether the filter should be rather on the top or bottom of |
|
58 | + * the admin section. The filters are arranged in ascending order of the |
|
59 | + * priority values. It is required to return a value between 0 and 100. |
|
60 | + * @since 11.0.0 |
|
61 | + */ |
|
62 | + public function getPriority() { |
|
63 | + return 4; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return bool True when the option can be changed for the stream |
|
68 | - * @since 11.0.0 |
|
69 | - */ |
|
70 | - public function canChangeStream() { |
|
71 | - return true; |
|
72 | - } |
|
66 | + /** |
|
67 | + * @return bool True when the option can be changed for the stream |
|
68 | + * @since 11.0.0 |
|
69 | + */ |
|
70 | + public function canChangeStream() { |
|
71 | + return true; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return bool True when the option can be changed for the stream |
|
76 | - * @since 11.0.0 |
|
77 | - */ |
|
78 | - public function isDefaultEnabledStream() { |
|
79 | - return true; |
|
80 | - } |
|
74 | + /** |
|
75 | + * @return bool True when the option can be changed for the stream |
|
76 | + * @since 11.0.0 |
|
77 | + */ |
|
78 | + public function isDefaultEnabledStream() { |
|
79 | + return true; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return bool True when the option can be changed for the mail |
|
84 | - * @since 11.0.0 |
|
85 | - */ |
|
86 | - public function canChangeMail() { |
|
87 | - return true; |
|
88 | - } |
|
82 | + /** |
|
83 | + * @return bool True when the option can be changed for the mail |
|
84 | + * @since 11.0.0 |
|
85 | + */ |
|
86 | + public function canChangeMail() { |
|
87 | + return true; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @return bool True when the option can be changed for the stream |
|
92 | - * @since 11.0.0 |
|
93 | - */ |
|
94 | - public function isDefaultEnabledMail() { |
|
95 | - return false; |
|
96 | - } |
|
90 | + /** |
|
91 | + * @return bool True when the option can be changed for the stream |
|
92 | + * @since 11.0.0 |
|
93 | + */ |
|
94 | + public function isDefaultEnabledMail() { |
|
95 | + return false; |
|
96 | + } |
|
97 | 97 | } |
98 | 98 |
@@ -44,116 +44,116 @@ |
||
44 | 44 | */ |
45 | 45 | abstract class Controller { |
46 | 46 | |
47 | - /** |
|
48 | - * app name |
|
49 | - * @var string |
|
50 | - * @since 7.0.0 |
|
51 | - */ |
|
52 | - protected $appName; |
|
53 | - |
|
54 | - /** |
|
55 | - * current request |
|
56 | - * @var \OCP\IRequest |
|
57 | - * @since 6.0.0 |
|
58 | - */ |
|
59 | - protected $request; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var array |
|
63 | - * @since 7.0.0 |
|
64 | - */ |
|
65 | - private $responders; |
|
66 | - |
|
67 | - /** |
|
68 | - * constructor of the controller |
|
69 | - * @param string $appName the name of the app |
|
70 | - * @param IRequest $request an instance of the request |
|
71 | - * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
72 | - */ |
|
73 | - public function __construct($appName, |
|
74 | - IRequest $request) { |
|
75 | - $this->appName = $appName; |
|
76 | - $this->request = $request; |
|
77 | - |
|
78 | - // default responders |
|
79 | - $this->responders = array( |
|
80 | - 'json' => function ($data) { |
|
81 | - if ($data instanceof DataResponse) { |
|
82 | - $response = new JSONResponse( |
|
83 | - $data->getData(), |
|
84 | - $data->getStatus() |
|
85 | - ); |
|
86 | - $dataHeaders = $data->getHeaders(); |
|
87 | - $headers = $response->getHeaders(); |
|
88 | - // do not overwrite Content-Type if it already exists |
|
89 | - if (isset($dataHeaders['Content-Type'])) { |
|
90 | - unset($headers['Content-Type']); |
|
91 | - } |
|
92 | - $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
93 | - return $response; |
|
94 | - } |
|
95 | - return new JSONResponse($data); |
|
96 | - } |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Parses an HTTP accept header and returns the supported responder type |
|
103 | - * @param string $acceptHeader |
|
104 | - * @param string $default |
|
105 | - * @return string the responder type |
|
106 | - * @since 7.0.0 |
|
107 | - * @since 9.1.0 Added default parameter |
|
108 | - */ |
|
109 | - public function getResponderByHTTPHeader($acceptHeader, $default='json') { |
|
110 | - $headers = explode(',', $acceptHeader); |
|
111 | - |
|
112 | - // return the first matching responder |
|
113 | - foreach ($headers as $header) { |
|
114 | - $header = strtolower(trim($header)); |
|
115 | - |
|
116 | - $responder = str_replace('application/', '', $header); |
|
117 | - |
|
118 | - if (array_key_exists($responder, $this->responders)) { |
|
119 | - return $responder; |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - // no matching header return default |
|
124 | - return $default; |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Registers a formatter for a type |
|
130 | - * @param string $format |
|
131 | - * @param \Closure $responder |
|
132 | - * @since 7.0.0 |
|
133 | - */ |
|
134 | - protected function registerResponder($format, \Closure $responder) { |
|
135 | - $this->responders[$format] = $responder; |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * Serializes and formats a response |
|
141 | - * @param mixed $response the value that was returned from a controller and |
|
142 | - * is not a Response instance |
|
143 | - * @param string $format the format for which a formatter has been registered |
|
144 | - * @throws \DomainException if format does not match a registered formatter |
|
145 | - * @return Response |
|
146 | - * @since 7.0.0 |
|
147 | - */ |
|
148 | - public function buildResponse($response, $format='json') { |
|
149 | - if(array_key_exists($format, $this->responders)) { |
|
150 | - |
|
151 | - $responder = $this->responders[$format]; |
|
152 | - |
|
153 | - return $responder($response); |
|
154 | - |
|
155 | - } |
|
156 | - throw new \DomainException('No responder registered for format '. |
|
157 | - $format . '!'); |
|
158 | - } |
|
47 | + /** |
|
48 | + * app name |
|
49 | + * @var string |
|
50 | + * @since 7.0.0 |
|
51 | + */ |
|
52 | + protected $appName; |
|
53 | + |
|
54 | + /** |
|
55 | + * current request |
|
56 | + * @var \OCP\IRequest |
|
57 | + * @since 6.0.0 |
|
58 | + */ |
|
59 | + protected $request; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var array |
|
63 | + * @since 7.0.0 |
|
64 | + */ |
|
65 | + private $responders; |
|
66 | + |
|
67 | + /** |
|
68 | + * constructor of the controller |
|
69 | + * @param string $appName the name of the app |
|
70 | + * @param IRequest $request an instance of the request |
|
71 | + * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
72 | + */ |
|
73 | + public function __construct($appName, |
|
74 | + IRequest $request) { |
|
75 | + $this->appName = $appName; |
|
76 | + $this->request = $request; |
|
77 | + |
|
78 | + // default responders |
|
79 | + $this->responders = array( |
|
80 | + 'json' => function ($data) { |
|
81 | + if ($data instanceof DataResponse) { |
|
82 | + $response = new JSONResponse( |
|
83 | + $data->getData(), |
|
84 | + $data->getStatus() |
|
85 | + ); |
|
86 | + $dataHeaders = $data->getHeaders(); |
|
87 | + $headers = $response->getHeaders(); |
|
88 | + // do not overwrite Content-Type if it already exists |
|
89 | + if (isset($dataHeaders['Content-Type'])) { |
|
90 | + unset($headers['Content-Type']); |
|
91 | + } |
|
92 | + $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
93 | + return $response; |
|
94 | + } |
|
95 | + return new JSONResponse($data); |
|
96 | + } |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Parses an HTTP accept header and returns the supported responder type |
|
103 | + * @param string $acceptHeader |
|
104 | + * @param string $default |
|
105 | + * @return string the responder type |
|
106 | + * @since 7.0.0 |
|
107 | + * @since 9.1.0 Added default parameter |
|
108 | + */ |
|
109 | + public function getResponderByHTTPHeader($acceptHeader, $default='json') { |
|
110 | + $headers = explode(',', $acceptHeader); |
|
111 | + |
|
112 | + // return the first matching responder |
|
113 | + foreach ($headers as $header) { |
|
114 | + $header = strtolower(trim($header)); |
|
115 | + |
|
116 | + $responder = str_replace('application/', '', $header); |
|
117 | + |
|
118 | + if (array_key_exists($responder, $this->responders)) { |
|
119 | + return $responder; |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + // no matching header return default |
|
124 | + return $default; |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Registers a formatter for a type |
|
130 | + * @param string $format |
|
131 | + * @param \Closure $responder |
|
132 | + * @since 7.0.0 |
|
133 | + */ |
|
134 | + protected function registerResponder($format, \Closure $responder) { |
|
135 | + $this->responders[$format] = $responder; |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * Serializes and formats a response |
|
141 | + * @param mixed $response the value that was returned from a controller and |
|
142 | + * is not a Response instance |
|
143 | + * @param string $format the format for which a formatter has been registered |
|
144 | + * @throws \DomainException if format does not match a registered formatter |
|
145 | + * @return Response |
|
146 | + * @since 7.0.0 |
|
147 | + */ |
|
148 | + public function buildResponse($response, $format='json') { |
|
149 | + if(array_key_exists($format, $this->responders)) { |
|
150 | + |
|
151 | + $responder = $this->responders[$format]; |
|
152 | + |
|
153 | + return $responder($response); |
|
154 | + |
|
155 | + } |
|
156 | + throw new \DomainException('No responder registered for format '. |
|
157 | + $format . '!'); |
|
158 | + } |
|
159 | 159 | } |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | |
78 | 78 | // default responders |
79 | 79 | $this->responders = array( |
80 | - 'json' => function ($data) { |
|
80 | + 'json' => function($data) { |
|
81 | 81 | if ($data instanceof DataResponse) { |
82 | 82 | $response = new JSONResponse( |
83 | 83 | $data->getData(), |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | * @since 7.0.0 |
107 | 107 | * @since 9.1.0 Added default parameter |
108 | 108 | */ |
109 | - public function getResponderByHTTPHeader($acceptHeader, $default='json') { |
|
109 | + public function getResponderByHTTPHeader($acceptHeader, $default = 'json') { |
|
110 | 110 | $headers = explode(',', $acceptHeader); |
111 | 111 | |
112 | 112 | // return the first matching responder |
@@ -145,8 +145,8 @@ discard block |
||
145 | 145 | * @return Response |
146 | 146 | * @since 7.0.0 |
147 | 147 | */ |
148 | - public function buildResponse($response, $format='json') { |
|
149 | - if(array_key_exists($format, $this->responders)) { |
|
148 | + public function buildResponse($response, $format = 'json') { |
|
149 | + if (array_key_exists($format, $this->responders)) { |
|
150 | 150 | |
151 | 151 | $responder = $this->responders[$format]; |
152 | 152 | |
@@ -154,6 +154,6 @@ discard block |
||
154 | 154 | |
155 | 155 | } |
156 | 156 | throw new \DomainException('No responder registered for format '. |
157 | - $format . '!'); |
|
157 | + $format.'!'); |
|
158 | 158 | } |
159 | 159 | } |
@@ -38,21 +38,21 @@ discard block |
||
38 | 38 | |
39 | 39 | // Backends |
40 | 40 | $authBackend = new OCA\DAV\Connector\PublicAuth( |
41 | - \OC::$server->getRequest(), |
|
42 | - \OC::$server->getShareManager(), |
|
43 | - \OC::$server->getSession() |
|
41 | + \OC::$server->getRequest(), |
|
42 | + \OC::$server->getShareManager(), |
|
43 | + \OC::$server->getSession() |
|
44 | 44 | ); |
45 | 45 | $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend); |
46 | 46 | |
47 | 47 | $serverFactory = new OCA\DAV\Connector\Sabre\ServerFactory( |
48 | - \OC::$server->getConfig(), |
|
49 | - \OC::$server->getLogger(), |
|
50 | - \OC::$server->getDatabaseConnection(), |
|
51 | - \OC::$server->getUserSession(), |
|
52 | - \OC::$server->getMountManager(), |
|
53 | - \OC::$server->getTagManager(), |
|
54 | - \OC::$server->getRequest(), |
|
55 | - \OC::$server->getPreviewManager() |
|
48 | + \OC::$server->getConfig(), |
|
49 | + \OC::$server->getLogger(), |
|
50 | + \OC::$server->getDatabaseConnection(), |
|
51 | + \OC::$server->getUserSession(), |
|
52 | + \OC::$server->getMountManager(), |
|
53 | + \OC::$server->getTagManager(), |
|
54 | + \OC::$server->getRequest(), |
|
55 | + \OC::$server->getPreviewManager() |
|
56 | 56 | ); |
57 | 57 | |
58 | 58 | $requestUri = \OC::$server->getRequest()->getRequestUri(); |
@@ -61,43 +61,43 @@ discard block |
||
61 | 61 | $filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin(); |
62 | 62 | |
63 | 63 | $server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) { |
64 | - $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); |
|
65 | - $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
66 | - $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); |
|
67 | - if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) { |
|
68 | - // this is what is thrown when trying to access a non-existing share |
|
69 | - throw new \Sabre\DAV\Exception\NotAuthenticated(); |
|
70 | - } |
|
71 | - |
|
72 | - $share = $authBackend->getShare(); |
|
73 | - $owner = $share->getShareOwner(); |
|
74 | - $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ; |
|
75 | - $fileId = $share->getNodeId(); |
|
76 | - |
|
77 | - // FIXME: should not add storage wrappers outside of preSetup, need to find a better way |
|
78 | - $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
79 | - \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) { |
|
80 | - return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE)); |
|
81 | - }); |
|
82 | - |
|
83 | - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog); |
|
84 | - |
|
85 | - OC_Util::tearDownFS(); |
|
86 | - OC_Util::setupFS($owner); |
|
87 | - $ownerView = new \OC\Files\View('/'. $owner . '/files'); |
|
88 | - $path = $ownerView->getPath($fileId); |
|
89 | - $fileInfo = $ownerView->getFileInfo($path); |
|
90 | - $linkCheckPlugin->setFileInfo($fileInfo); |
|
91 | - |
|
92 | - // If not readble (files_drop) enable the filesdrop plugin |
|
93 | - if (!$isReadable) { |
|
94 | - $filesDropPlugin->enable(); |
|
95 | - } |
|
96 | - |
|
97 | - $view = new \OC\Files\View($ownerView->getAbsolutePath($path)); |
|
98 | - $filesDropPlugin->setView($view); |
|
99 | - |
|
100 | - return $view; |
|
64 | + $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); |
|
65 | + $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
66 | + $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); |
|
67 | + if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) { |
|
68 | + // this is what is thrown when trying to access a non-existing share |
|
69 | + throw new \Sabre\DAV\Exception\NotAuthenticated(); |
|
70 | + } |
|
71 | + |
|
72 | + $share = $authBackend->getShare(); |
|
73 | + $owner = $share->getShareOwner(); |
|
74 | + $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ; |
|
75 | + $fileId = $share->getNodeId(); |
|
76 | + |
|
77 | + // FIXME: should not add storage wrappers outside of preSetup, need to find a better way |
|
78 | + $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
79 | + \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) { |
|
80 | + return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE)); |
|
81 | + }); |
|
82 | + |
|
83 | + \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog); |
|
84 | + |
|
85 | + OC_Util::tearDownFS(); |
|
86 | + OC_Util::setupFS($owner); |
|
87 | + $ownerView = new \OC\Files\View('/'. $owner . '/files'); |
|
88 | + $path = $ownerView->getPath($fileId); |
|
89 | + $fileInfo = $ownerView->getFileInfo($path); |
|
90 | + $linkCheckPlugin->setFileInfo($fileInfo); |
|
91 | + |
|
92 | + // If not readble (files_drop) enable the filesdrop plugin |
|
93 | + if (!$isReadable) { |
|
94 | + $filesDropPlugin->enable(); |
|
95 | + } |
|
96 | + |
|
97 | + $view = new \OC\Files\View($ownerView->getAbsolutePath($path)); |
|
98 | + $filesDropPlugin->setView($view); |
|
99 | + |
|
100 | + return $view; |
|
101 | 101 | }); |
102 | 102 | |
103 | 103 | $server->addPlugin($linkCheckPlugin); |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | $linkCheckPlugin = new \OCA\DAV\Files\Sharing\PublicLinkCheckPlugin(); |
61 | 61 | $filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin(); |
62 | 62 | |
63 | -$server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) { |
|
63 | +$server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function(\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) { |
|
64 | 64 | $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); |
65 | 65 | $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
66 | 66 | $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | |
77 | 77 | // FIXME: should not add storage wrappers outside of preSetup, need to find a better way |
78 | 78 | $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); |
79 | - \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) { |
|
79 | + \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function($mountPoint, $storage) use ($share) { |
|
80 | 80 | return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE)); |
81 | 81 | }); |
82 | 82 | |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | |
85 | 85 | OC_Util::tearDownFS(); |
86 | 86 | OC_Util::setupFS($owner); |
87 | - $ownerView = new \OC\Files\View('/'. $owner . '/files'); |
|
87 | + $ownerView = new \OC\Files\View('/'.$owner.'/files'); |
|
88 | 88 | $path = $ownerView->getPath($fileId); |
89 | 89 | $fileInfo = $ownerView->getFileInfo($path); |
90 | 90 | $linkCheckPlugin->setFileInfo($fileInfo); |