@@ -26,16 +26,16 @@ |
||
26 | 26 | use OC\Authentication\Token\IToken; |
27 | 27 | |
28 | 28 | class ExpiredTokenException extends InvalidTokenException { |
29 | - /** @var IToken */ |
|
30 | - private $token; |
|
29 | + /** @var IToken */ |
|
30 | + private $token; |
|
31 | 31 | |
32 | - public function __construct(IToken $token) { |
|
33 | - parent::__construct(); |
|
32 | + public function __construct(IToken $token) { |
|
33 | + parent::__construct(); |
|
34 | 34 | |
35 | - $this->token = $token; |
|
36 | - } |
|
35 | + $this->token = $token; |
|
36 | + } |
|
37 | 37 | |
38 | - public function getToken(): IToken { |
|
39 | - return $this->token; |
|
40 | - } |
|
38 | + public function getToken(): IToken { |
|
39 | + return $this->token; |
|
40 | + } |
|
41 | 41 | } |
@@ -25,12 +25,12 @@ |
||
25 | 25 | * @since 15.0.0 |
26 | 26 | */ |
27 | 27 | interface IVersionManager extends IVersionBackend { |
28 | - /** |
|
29 | - * Register a new backend |
|
30 | - * |
|
31 | - * @param string $storageType |
|
32 | - * @param IVersionBackend $backend |
|
33 | - * @since 15.0.0 |
|
34 | - */ |
|
35 | - public function registerBackend(string $storageType, IVersionBackend $backend); |
|
28 | + /** |
|
29 | + * Register a new backend |
|
30 | + * |
|
31 | + * @param string $storageType |
|
32 | + * @param IVersionBackend $backend |
|
33 | + * @since 15.0.0 |
|
34 | + */ |
|
35 | + public function registerBackend(string $storageType, IVersionBackend $backend); |
|
36 | 36 | } |
@@ -71,7 +71,7 @@ |
||
71 | 71 | public function getChild($name) { |
72 | 72 | $userFolder = $this->rootFolder->getUserFolder($this->user->getUID()); |
73 | 73 | |
74 | - $fileId = (int)$name; |
|
74 | + $fileId = (int) $name; |
|
75 | 75 | $nodes = $userFolder->getById($fileId); |
76 | 76 | |
77 | 77 | if ($nodes === []) { |
@@ -36,74 +36,74 @@ |
||
36 | 36 | |
37 | 37 | class VersionRoot implements ICollection { |
38 | 38 | |
39 | - /** @var IUser */ |
|
40 | - private $user; |
|
39 | + /** @var IUser */ |
|
40 | + private $user; |
|
41 | 41 | |
42 | - /** @var IRootFolder */ |
|
43 | - private $rootFolder; |
|
42 | + /** @var IRootFolder */ |
|
43 | + private $rootFolder; |
|
44 | 44 | |
45 | - /** @var IVersionManager */ |
|
46 | - private $versionManager; |
|
45 | + /** @var IVersionManager */ |
|
46 | + private $versionManager; |
|
47 | 47 | |
48 | - public function __construct(IUser $user, IRootFolder $rootFolder, IVersionManager $versionManager) { |
|
49 | - $this->user = $user; |
|
50 | - $this->rootFolder = $rootFolder; |
|
51 | - $this->versionManager = $versionManager; |
|
52 | - } |
|
48 | + public function __construct(IUser $user, IRootFolder $rootFolder, IVersionManager $versionManager) { |
|
49 | + $this->user = $user; |
|
50 | + $this->rootFolder = $rootFolder; |
|
51 | + $this->versionManager = $versionManager; |
|
52 | + } |
|
53 | 53 | |
54 | - public function delete() { |
|
55 | - throw new Forbidden(); |
|
56 | - } |
|
54 | + public function delete() { |
|
55 | + throw new Forbidden(); |
|
56 | + } |
|
57 | 57 | |
58 | - public function getName(): string { |
|
59 | - return 'versions'; |
|
60 | - } |
|
58 | + public function getName(): string { |
|
59 | + return 'versions'; |
|
60 | + } |
|
61 | 61 | |
62 | - public function setName($name) { |
|
63 | - throw new Forbidden(); |
|
64 | - } |
|
62 | + public function setName($name) { |
|
63 | + throw new Forbidden(); |
|
64 | + } |
|
65 | 65 | |
66 | - public function createFile($name, $data = null) { |
|
67 | - throw new Forbidden(); |
|
68 | - } |
|
66 | + public function createFile($name, $data = null) { |
|
67 | + throw new Forbidden(); |
|
68 | + } |
|
69 | 69 | |
70 | - public function createDirectory($name) { |
|
71 | - throw new Forbidden(); |
|
72 | - } |
|
70 | + public function createDirectory($name) { |
|
71 | + throw new Forbidden(); |
|
72 | + } |
|
73 | 73 | |
74 | - public function getChild($name) { |
|
75 | - $userFolder = $this->rootFolder->getUserFolder($this->user->getUID()); |
|
74 | + public function getChild($name) { |
|
75 | + $userFolder = $this->rootFolder->getUserFolder($this->user->getUID()); |
|
76 | 76 | |
77 | - $fileId = (int)$name; |
|
78 | - $nodes = $userFolder->getById($fileId); |
|
77 | + $fileId = (int)$name; |
|
78 | + $nodes = $userFolder->getById($fileId); |
|
79 | 79 | |
80 | - if ($nodes === []) { |
|
81 | - throw new NotFound(); |
|
82 | - } |
|
80 | + if ($nodes === []) { |
|
81 | + throw new NotFound(); |
|
82 | + } |
|
83 | 83 | |
84 | - $node = array_pop($nodes); |
|
84 | + $node = array_pop($nodes); |
|
85 | 85 | |
86 | - if (!$node instanceof File) { |
|
87 | - throw new NotFound(); |
|
88 | - } |
|
86 | + if (!$node instanceof File) { |
|
87 | + throw new NotFound(); |
|
88 | + } |
|
89 | 89 | |
90 | - return new VersionCollection($node, $this->user, $this->versionManager); |
|
91 | - } |
|
90 | + return new VersionCollection($node, $this->user, $this->versionManager); |
|
91 | + } |
|
92 | 92 | |
93 | - public function getChildren(): array { |
|
94 | - return []; |
|
95 | - } |
|
93 | + public function getChildren(): array { |
|
94 | + return []; |
|
95 | + } |
|
96 | 96 | |
97 | - public function childExists($name): bool { |
|
98 | - try { |
|
99 | - $this->getChild($name); |
|
100 | - return true; |
|
101 | - } catch (NotFound $e) { |
|
102 | - return false; |
|
103 | - } |
|
104 | - } |
|
97 | + public function childExists($name): bool { |
|
98 | + try { |
|
99 | + $this->getChild($name); |
|
100 | + return true; |
|
101 | + } catch (NotFound $e) { |
|
102 | + return false; |
|
103 | + } |
|
104 | + } |
|
105 | 105 | |
106 | - public function getLastModified(): int { |
|
107 | - return 0; |
|
108 | - } |
|
106 | + public function getLastModified(): int { |
|
107 | + return 0; |
|
108 | + } |
|
109 | 109 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | public function getChildren(): array { |
79 | 79 | $versions = $this->versionManager->getVersionsForFile($this->user, $this->file); |
80 | 80 | |
81 | - return array_map(function (IVersion $version) { |
|
81 | + return array_map(function(IVersion $version) { |
|
82 | 82 | return new VersionFile($version, $this->versionManager); |
83 | 83 | }, $versions); |
84 | 84 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | } |
98 | 98 | |
99 | 99 | public function getName(): string { |
100 | - return (string)$this->file->getId(); |
|
100 | + return (string) $this->file->getId(); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | public function setName($name) { |
@@ -36,72 +36,72 @@ |
||
36 | 36 | |
37 | 37 | class VersionCollection implements ICollection { |
38 | 38 | |
39 | - /** @var File */ |
|
40 | - private $file; |
|
41 | - |
|
42 | - /** @var IUser */ |
|
43 | - private $user; |
|
44 | - |
|
45 | - /** @var IVersionManager */ |
|
46 | - private $versionManager; |
|
47 | - |
|
48 | - public function __construct(File $file, IUser $user, IVersionManager $versionManager) { |
|
49 | - $this->file = $file; |
|
50 | - $this->user = $user; |
|
51 | - $this->versionManager = $versionManager; |
|
52 | - } |
|
53 | - |
|
54 | - public function createFile($name, $data = null) { |
|
55 | - throw new Forbidden(); |
|
56 | - } |
|
57 | - |
|
58 | - public function createDirectory($name) { |
|
59 | - throw new Forbidden(); |
|
60 | - } |
|
61 | - |
|
62 | - public function getChild($name) { |
|
63 | - /** @var VersionFile[] $versions */ |
|
64 | - $versions = $this->getChildren(); |
|
65 | - |
|
66 | - foreach ($versions as $version) { |
|
67 | - if ($version->getName() === $name) { |
|
68 | - return $version; |
|
69 | - } |
|
70 | - } |
|
71 | - |
|
72 | - throw new NotFound(); |
|
73 | - } |
|
74 | - |
|
75 | - public function getChildren(): array { |
|
76 | - $versions = $this->versionManager->getVersionsForFile($this->user, $this->file); |
|
77 | - |
|
78 | - return array_map(function (IVersion $version) { |
|
79 | - return new VersionFile($version, $this->versionManager); |
|
80 | - }, $versions); |
|
81 | - } |
|
82 | - |
|
83 | - public function childExists($name): bool { |
|
84 | - try { |
|
85 | - $this->getChild($name); |
|
86 | - return true; |
|
87 | - } catch (NotFound $e) { |
|
88 | - return false; |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - public function delete() { |
|
93 | - throw new Forbidden(); |
|
94 | - } |
|
95 | - |
|
96 | - public function getName(): string { |
|
97 | - return (string)$this->file->getId(); |
|
98 | - } |
|
99 | - |
|
100 | - public function setName($name) { |
|
101 | - throw new Forbidden(); |
|
102 | - } |
|
103 | - |
|
104 | - public function getLastModified(): int { |
|
105 | - return 0; |
|
106 | - } |
|
39 | + /** @var File */ |
|
40 | + private $file; |
|
41 | + |
|
42 | + /** @var IUser */ |
|
43 | + private $user; |
|
44 | + |
|
45 | + /** @var IVersionManager */ |
|
46 | + private $versionManager; |
|
47 | + |
|
48 | + public function __construct(File $file, IUser $user, IVersionManager $versionManager) { |
|
49 | + $this->file = $file; |
|
50 | + $this->user = $user; |
|
51 | + $this->versionManager = $versionManager; |
|
52 | + } |
|
53 | + |
|
54 | + public function createFile($name, $data = null) { |
|
55 | + throw new Forbidden(); |
|
56 | + } |
|
57 | + |
|
58 | + public function createDirectory($name) { |
|
59 | + throw new Forbidden(); |
|
60 | + } |
|
61 | + |
|
62 | + public function getChild($name) { |
|
63 | + /** @var VersionFile[] $versions */ |
|
64 | + $versions = $this->getChildren(); |
|
65 | + |
|
66 | + foreach ($versions as $version) { |
|
67 | + if ($version->getName() === $name) { |
|
68 | + return $version; |
|
69 | + } |
|
70 | + } |
|
71 | + |
|
72 | + throw new NotFound(); |
|
73 | + } |
|
74 | + |
|
75 | + public function getChildren(): array { |
|
76 | + $versions = $this->versionManager->getVersionsForFile($this->user, $this->file); |
|
77 | + |
|
78 | + return array_map(function (IVersion $version) { |
|
79 | + return new VersionFile($version, $this->versionManager); |
|
80 | + }, $versions); |
|
81 | + } |
|
82 | + |
|
83 | + public function childExists($name): bool { |
|
84 | + try { |
|
85 | + $this->getChild($name); |
|
86 | + return true; |
|
87 | + } catch (NotFound $e) { |
|
88 | + return false; |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + public function delete() { |
|
93 | + throw new Forbidden(); |
|
94 | + } |
|
95 | + |
|
96 | + public function getName(): string { |
|
97 | + return (string)$this->file->getId(); |
|
98 | + } |
|
99 | + |
|
100 | + public function setName($name) { |
|
101 | + throw new Forbidden(); |
|
102 | + } |
|
103 | + |
|
104 | + public function getLastModified(): int { |
|
105 | + return 0; |
|
106 | + } |
|
107 | 107 | } |
@@ -32,60 +32,60 @@ |
||
32 | 32 | use OCP\User\Backend\IPasswordConfirmationBackend; |
33 | 33 | |
34 | 34 | class PasswordConfirmationMiddleware extends Middleware { |
35 | - /** @var ControllerMethodReflector */ |
|
36 | - private $reflector; |
|
37 | - /** @var ISession */ |
|
38 | - private $session; |
|
39 | - /** @var IUserSession */ |
|
40 | - private $userSession; |
|
41 | - /** @var ITimeFactory */ |
|
42 | - private $timeFactory; |
|
43 | - /** @var array */ |
|
44 | - private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true]; |
|
35 | + /** @var ControllerMethodReflector */ |
|
36 | + private $reflector; |
|
37 | + /** @var ISession */ |
|
38 | + private $session; |
|
39 | + /** @var IUserSession */ |
|
40 | + private $userSession; |
|
41 | + /** @var ITimeFactory */ |
|
42 | + private $timeFactory; |
|
43 | + /** @var array */ |
|
44 | + private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true]; |
|
45 | 45 | |
46 | - /** |
|
47 | - * PasswordConfirmationMiddleware constructor. |
|
48 | - * |
|
49 | - * @param ControllerMethodReflector $reflector |
|
50 | - * @param ISession $session |
|
51 | - * @param IUserSession $userSession |
|
52 | - * @param ITimeFactory $timeFactory |
|
53 | - */ |
|
54 | - public function __construct(ControllerMethodReflector $reflector, |
|
55 | - ISession $session, |
|
56 | - IUserSession $userSession, |
|
57 | - ITimeFactory $timeFactory) { |
|
58 | - $this->reflector = $reflector; |
|
59 | - $this->session = $session; |
|
60 | - $this->userSession = $userSession; |
|
61 | - $this->timeFactory = $timeFactory; |
|
62 | - } |
|
46 | + /** |
|
47 | + * PasswordConfirmationMiddleware constructor. |
|
48 | + * |
|
49 | + * @param ControllerMethodReflector $reflector |
|
50 | + * @param ISession $session |
|
51 | + * @param IUserSession $userSession |
|
52 | + * @param ITimeFactory $timeFactory |
|
53 | + */ |
|
54 | + public function __construct(ControllerMethodReflector $reflector, |
|
55 | + ISession $session, |
|
56 | + IUserSession $userSession, |
|
57 | + ITimeFactory $timeFactory) { |
|
58 | + $this->reflector = $reflector; |
|
59 | + $this->session = $session; |
|
60 | + $this->userSession = $userSession; |
|
61 | + $this->timeFactory = $timeFactory; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @param Controller $controller |
|
66 | - * @param string $methodName |
|
67 | - * @throws NotConfirmedException |
|
68 | - */ |
|
69 | - public function beforeController($controller, $methodName) { |
|
70 | - if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) { |
|
71 | - $user = $this->userSession->getUser(); |
|
72 | - $backendClassName = ''; |
|
73 | - if ($user !== null) { |
|
74 | - $backend = $user->getBackend(); |
|
75 | - if ($backend instanceof IPasswordConfirmationBackend) { |
|
76 | - if (!$backend->canConfirmPassword($user->getUID())) { |
|
77 | - return; |
|
78 | - } |
|
79 | - } |
|
64 | + /** |
|
65 | + * @param Controller $controller |
|
66 | + * @param string $methodName |
|
67 | + * @throws NotConfirmedException |
|
68 | + */ |
|
69 | + public function beforeController($controller, $methodName) { |
|
70 | + if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) { |
|
71 | + $user = $this->userSession->getUser(); |
|
72 | + $backendClassName = ''; |
|
73 | + if ($user !== null) { |
|
74 | + $backend = $user->getBackend(); |
|
75 | + if ($backend instanceof IPasswordConfirmationBackend) { |
|
76 | + if (!$backend->canConfirmPassword($user->getUID())) { |
|
77 | + return; |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - $backendClassName = $user->getBackendClassName(); |
|
82 | - } |
|
81 | + $backendClassName = $user->getBackendClassName(); |
|
82 | + } |
|
83 | 83 | |
84 | - $lastConfirm = (int) $this->session->get('last-password-confirm'); |
|
85 | - // we can't check the password against a SAML backend, so skip password confirmation in this case |
|
86 | - if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay |
|
87 | - throw new NotConfirmedException(); |
|
88 | - } |
|
89 | - } |
|
90 | - } |
|
84 | + $lastConfirm = (int) $this->session->get('last-password-confirm'); |
|
85 | + // we can't check the password against a SAML backend, so skip password confirmation in this case |
|
86 | + if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay |
|
87 | + throw new NotConfirmedException(); |
|
88 | + } |
|
89 | + } |
|
90 | + } |
|
91 | 91 | } |
@@ -170,7 +170,7 @@ |
||
170 | 170 | )) |
171 | 171 | ->andWhere($update->expr()->like( |
172 | 172 | $update->func()->lower('name'), |
173 | - $update->createNamedParameter('%' . $this->dbConnection->escapeLikeParameter('.' . $ext)) |
|
173 | + $update->createNamedParameter('%'.$this->dbConnection->escapeLikeParameter('.'.$ext)) |
|
174 | 174 | )); |
175 | 175 | return $update->execute(); |
176 | 176 | } |
@@ -33,148 +33,148 @@ |
||
33 | 33 | * @package OC\Files\Type |
34 | 34 | */ |
35 | 35 | class Loader implements IMimeTypeLoader { |
36 | - /** @var IDBConnection */ |
|
37 | - private $dbConnection; |
|
38 | - |
|
39 | - /** @var array [id => mimetype] */ |
|
40 | - protected $mimetypes; |
|
41 | - |
|
42 | - /** @var array [mimetype => id] */ |
|
43 | - protected $mimetypeIds; |
|
44 | - |
|
45 | - /** |
|
46 | - * @param IDBConnection $dbConnection |
|
47 | - */ |
|
48 | - public function __construct(IDBConnection $dbConnection) { |
|
49 | - $this->dbConnection = $dbConnection; |
|
50 | - $this->mimetypes = []; |
|
51 | - $this->mimetypeIds = []; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Get a mimetype from its ID |
|
56 | - * |
|
57 | - * @param int $id |
|
58 | - * @return string|null |
|
59 | - */ |
|
60 | - public function getMimetypeById($id) { |
|
61 | - if (!$this->mimetypes) { |
|
62 | - $this->loadMimetypes(); |
|
63 | - } |
|
64 | - if (isset($this->mimetypes[$id])) { |
|
65 | - return $this->mimetypes[$id]; |
|
66 | - } |
|
67 | - return null; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Get a mimetype ID, adding the mimetype to the DB if it does not exist |
|
72 | - * |
|
73 | - * @param string $mimetype |
|
74 | - * @return int |
|
75 | - */ |
|
76 | - public function getId($mimetype) { |
|
77 | - if (!$this->mimetypeIds) { |
|
78 | - $this->loadMimetypes(); |
|
79 | - } |
|
80 | - if (isset($this->mimetypeIds[$mimetype])) { |
|
81 | - return $this->mimetypeIds[$mimetype]; |
|
82 | - } |
|
83 | - return $this->store($mimetype); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Test if a mimetype exists in the database |
|
88 | - * |
|
89 | - * @param string $mimetype |
|
90 | - * @return bool |
|
91 | - */ |
|
92 | - public function exists($mimetype) { |
|
93 | - if (!$this->mimetypeIds) { |
|
94 | - $this->loadMimetypes(); |
|
95 | - } |
|
96 | - return isset($this->mimetypeIds[$mimetype]); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Clear all loaded mimetypes, allow for re-loading |
|
101 | - */ |
|
102 | - public function reset() { |
|
103 | - $this->mimetypes = []; |
|
104 | - $this->mimetypeIds = []; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Store a mimetype in the DB |
|
109 | - * |
|
110 | - * @param string $mimetype |
|
111 | - * @param int inserted ID |
|
112 | - */ |
|
113 | - protected function store($mimetype) { |
|
114 | - $this->dbConnection->insertIfNotExist('*PREFIX*mimetypes', [ |
|
115 | - 'mimetype' => $mimetype |
|
116 | - ]); |
|
117 | - |
|
118 | - $fetch = $this->dbConnection->getQueryBuilder(); |
|
119 | - $fetch->select('id') |
|
120 | - ->from('mimetypes') |
|
121 | - ->where( |
|
122 | - $fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype) |
|
123 | - )); |
|
124 | - |
|
125 | - $result = $fetch->execute(); |
|
126 | - $row = $result->fetch(); |
|
127 | - $result->closeCursor(); |
|
128 | - |
|
129 | - if (!$row) { |
|
130 | - throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it"); |
|
131 | - } |
|
132 | - |
|
133 | - $this->mimetypes[$row['id']] = $mimetype; |
|
134 | - $this->mimetypeIds[$mimetype] = $row['id']; |
|
135 | - return $row['id']; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Load all mimetypes from DB |
|
140 | - */ |
|
141 | - private function loadMimetypes() { |
|
142 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
143 | - $qb->select('id', 'mimetype') |
|
144 | - ->from('mimetypes'); |
|
145 | - |
|
146 | - $result = $qb->execute(); |
|
147 | - $results = $result->fetchAll(); |
|
148 | - $result->closeCursor(); |
|
149 | - |
|
150 | - foreach ($results as $row) { |
|
151 | - $this->mimetypes[$row['id']] = $row['mimetype']; |
|
152 | - $this->mimetypeIds[$row['mimetype']] = $row['id']; |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Update filecache mimetype based on file extension |
|
158 | - * |
|
159 | - * @param string $ext file extension |
|
160 | - * @param int $mimeTypeId |
|
161 | - * @return int number of changed rows |
|
162 | - */ |
|
163 | - public function updateFilecache($ext, $mimeTypeId) { |
|
164 | - $folderMimeTypeId = $this->getId('httpd/unix-directory'); |
|
165 | - $update = $this->dbConnection->getQueryBuilder(); |
|
166 | - $update->update('filecache') |
|
167 | - ->set('mimetype', $update->createNamedParameter($mimeTypeId)) |
|
168 | - ->where($update->expr()->neq( |
|
169 | - 'mimetype', $update->createNamedParameter($mimeTypeId) |
|
170 | - )) |
|
171 | - ->andWhere($update->expr()->neq( |
|
172 | - 'mimetype', $update->createNamedParameter($folderMimeTypeId) |
|
173 | - )) |
|
174 | - ->andWhere($update->expr()->like( |
|
175 | - $update->func()->lower('name'), |
|
176 | - $update->createNamedParameter('%' . $this->dbConnection->escapeLikeParameter('.' . $ext)) |
|
177 | - )); |
|
178 | - return $update->execute(); |
|
179 | - } |
|
36 | + /** @var IDBConnection */ |
|
37 | + private $dbConnection; |
|
38 | + |
|
39 | + /** @var array [id => mimetype] */ |
|
40 | + protected $mimetypes; |
|
41 | + |
|
42 | + /** @var array [mimetype => id] */ |
|
43 | + protected $mimetypeIds; |
|
44 | + |
|
45 | + /** |
|
46 | + * @param IDBConnection $dbConnection |
|
47 | + */ |
|
48 | + public function __construct(IDBConnection $dbConnection) { |
|
49 | + $this->dbConnection = $dbConnection; |
|
50 | + $this->mimetypes = []; |
|
51 | + $this->mimetypeIds = []; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Get a mimetype from its ID |
|
56 | + * |
|
57 | + * @param int $id |
|
58 | + * @return string|null |
|
59 | + */ |
|
60 | + public function getMimetypeById($id) { |
|
61 | + if (!$this->mimetypes) { |
|
62 | + $this->loadMimetypes(); |
|
63 | + } |
|
64 | + if (isset($this->mimetypes[$id])) { |
|
65 | + return $this->mimetypes[$id]; |
|
66 | + } |
|
67 | + return null; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Get a mimetype ID, adding the mimetype to the DB if it does not exist |
|
72 | + * |
|
73 | + * @param string $mimetype |
|
74 | + * @return int |
|
75 | + */ |
|
76 | + public function getId($mimetype) { |
|
77 | + if (!$this->mimetypeIds) { |
|
78 | + $this->loadMimetypes(); |
|
79 | + } |
|
80 | + if (isset($this->mimetypeIds[$mimetype])) { |
|
81 | + return $this->mimetypeIds[$mimetype]; |
|
82 | + } |
|
83 | + return $this->store($mimetype); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Test if a mimetype exists in the database |
|
88 | + * |
|
89 | + * @param string $mimetype |
|
90 | + * @return bool |
|
91 | + */ |
|
92 | + public function exists($mimetype) { |
|
93 | + if (!$this->mimetypeIds) { |
|
94 | + $this->loadMimetypes(); |
|
95 | + } |
|
96 | + return isset($this->mimetypeIds[$mimetype]); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Clear all loaded mimetypes, allow for re-loading |
|
101 | + */ |
|
102 | + public function reset() { |
|
103 | + $this->mimetypes = []; |
|
104 | + $this->mimetypeIds = []; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Store a mimetype in the DB |
|
109 | + * |
|
110 | + * @param string $mimetype |
|
111 | + * @param int inserted ID |
|
112 | + */ |
|
113 | + protected function store($mimetype) { |
|
114 | + $this->dbConnection->insertIfNotExist('*PREFIX*mimetypes', [ |
|
115 | + 'mimetype' => $mimetype |
|
116 | + ]); |
|
117 | + |
|
118 | + $fetch = $this->dbConnection->getQueryBuilder(); |
|
119 | + $fetch->select('id') |
|
120 | + ->from('mimetypes') |
|
121 | + ->where( |
|
122 | + $fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype) |
|
123 | + )); |
|
124 | + |
|
125 | + $result = $fetch->execute(); |
|
126 | + $row = $result->fetch(); |
|
127 | + $result->closeCursor(); |
|
128 | + |
|
129 | + if (!$row) { |
|
130 | + throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it"); |
|
131 | + } |
|
132 | + |
|
133 | + $this->mimetypes[$row['id']] = $mimetype; |
|
134 | + $this->mimetypeIds[$mimetype] = $row['id']; |
|
135 | + return $row['id']; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Load all mimetypes from DB |
|
140 | + */ |
|
141 | + private function loadMimetypes() { |
|
142 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
143 | + $qb->select('id', 'mimetype') |
|
144 | + ->from('mimetypes'); |
|
145 | + |
|
146 | + $result = $qb->execute(); |
|
147 | + $results = $result->fetchAll(); |
|
148 | + $result->closeCursor(); |
|
149 | + |
|
150 | + foreach ($results as $row) { |
|
151 | + $this->mimetypes[$row['id']] = $row['mimetype']; |
|
152 | + $this->mimetypeIds[$row['mimetype']] = $row['id']; |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Update filecache mimetype based on file extension |
|
158 | + * |
|
159 | + * @param string $ext file extension |
|
160 | + * @param int $mimeTypeId |
|
161 | + * @return int number of changed rows |
|
162 | + */ |
|
163 | + public function updateFilecache($ext, $mimeTypeId) { |
|
164 | + $folderMimeTypeId = $this->getId('httpd/unix-directory'); |
|
165 | + $update = $this->dbConnection->getQueryBuilder(); |
|
166 | + $update->update('filecache') |
|
167 | + ->set('mimetype', $update->createNamedParameter($mimeTypeId)) |
|
168 | + ->where($update->expr()->neq( |
|
169 | + 'mimetype', $update->createNamedParameter($mimeTypeId) |
|
170 | + )) |
|
171 | + ->andWhere($update->expr()->neq( |
|
172 | + 'mimetype', $update->createNamedParameter($folderMimeTypeId) |
|
173 | + )) |
|
174 | + ->andWhere($update->expr()->like( |
|
175 | + $update->func()->lower('name'), |
|
176 | + $update->createNamedParameter('%' . $this->dbConnection->escapeLikeParameter('.' . $ext)) |
|
177 | + )); |
|
178 | + return $update->execute(); |
|
179 | + } |
|
180 | 180 | } |
@@ -32,18 +32,18 @@ |
||
32 | 32 | |
33 | 33 | class Version1008Date20181105112049 extends SimpleMigrationStep { |
34 | 34 | |
35 | - /** |
|
36 | - * @param IOutput $output |
|
37 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
38 | - * @param array $options |
|
39 | - * @return null|ISchemaWrapper |
|
40 | - */ |
|
41 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
|
42 | - /** @var ISchemaWrapper $schema */ |
|
43 | - $schema = $schemaClosure(); |
|
44 | - $table = $schema->getTable('calendarsubscriptions'); |
|
45 | - $table->dropColumn('source_copy'); |
|
35 | + /** |
|
36 | + * @param IOutput $output |
|
37 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
38 | + * @param array $options |
|
39 | + * @return null|ISchemaWrapper |
|
40 | + */ |
|
41 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
|
42 | + /** @var ISchemaWrapper $schema */ |
|
43 | + $schema = $schemaClosure(); |
|
44 | + $table = $schema->getTable('calendarsubscriptions'); |
|
45 | + $table->dropColumn('source_copy'); |
|
46 | 46 | |
47 | - return $schema; |
|
48 | - } |
|
47 | + return $schema; |
|
48 | + } |
|
49 | 49 | } |
@@ -32,18 +32,18 @@ |
||
32 | 32 | |
33 | 33 | class Version1008Date20181105104833 extends SimpleMigrationStep { |
34 | 34 | |
35 | - /** |
|
36 | - * @param IOutput $output |
|
37 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
38 | - * @param array $options |
|
39 | - * @return null|ISchemaWrapper |
|
40 | - */ |
|
41 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
|
42 | - /** @var ISchemaWrapper $schema */ |
|
43 | - $schema = $schemaClosure(); |
|
44 | - $table = $schema->getTable('calendarsubscriptions'); |
|
45 | - $table->dropColumn('source'); |
|
35 | + /** |
|
36 | + * @param IOutput $output |
|
37 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
38 | + * @param array $options |
|
39 | + * @return null|ISchemaWrapper |
|
40 | + */ |
|
41 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
|
42 | + /** @var ISchemaWrapper $schema */ |
|
43 | + $schema = $schemaClosure(); |
|
44 | + $table = $schema->getTable('calendarsubscriptions'); |
|
45 | + $table->dropColumn('source'); |
|
46 | 46 | |
47 | - return $schema; |
|
48 | - } |
|
47 | + return $schema; |
|
48 | + } |
|
49 | 49 | } |
@@ -33,27 +33,27 @@ |
||
33 | 33 | class Version1008Date20181114084440 extends SimpleMigrationStep { |
34 | 34 | |
35 | 35 | |
36 | - /** |
|
37 | - * @param IOutput $output |
|
38 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
39 | - * @param array $options |
|
40 | - * @return null|ISchemaWrapper |
|
41 | - */ |
|
42 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
|
43 | - /** @var ISchemaWrapper $schema */ |
|
44 | - $schema = $schemaClosure(); |
|
45 | - |
|
46 | - if ($schema->hasTable('calendarchanges')) { |
|
47 | - $calendarChangesTable = $schema->getTable('calendarchanges'); |
|
48 | - if ($calendarChangesTable->hasIndex('calendarid_calendartype_synctoken')) { |
|
49 | - $calendarChangesTable->dropIndex('calendarid_calendartype_synctoken'); |
|
50 | - } |
|
51 | - |
|
52 | - if (!$calendarChangesTable->hasIndex('calid_type_synctoken')) { |
|
53 | - $calendarChangesTable->addIndex(['calendarid', 'calendartype', 'synctoken'], 'calid_type_synctoken'); |
|
54 | - } |
|
55 | - } |
|
56 | - |
|
57 | - return $schema; |
|
58 | - } |
|
36 | + /** |
|
37 | + * @param IOutput $output |
|
38 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
39 | + * @param array $options |
|
40 | + * @return null|ISchemaWrapper |
|
41 | + */ |
|
42 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
|
43 | + /** @var ISchemaWrapper $schema */ |
|
44 | + $schema = $schemaClosure(); |
|
45 | + |
|
46 | + if ($schema->hasTable('calendarchanges')) { |
|
47 | + $calendarChangesTable = $schema->getTable('calendarchanges'); |
|
48 | + if ($calendarChangesTable->hasIndex('calendarid_calendartype_synctoken')) { |
|
49 | + $calendarChangesTable->dropIndex('calendarid_calendartype_synctoken'); |
|
50 | + } |
|
51 | + |
|
52 | + if (!$calendarChangesTable->hasIndex('calid_type_synctoken')) { |
|
53 | + $calendarChangesTable->addIndex(['calendarid', 'calendartype', 'synctoken'], 'calid_type_synctoken'); |
|
54 | + } |
|
55 | + } |
|
56 | + |
|
57 | + return $schema; |
|
58 | + } |
|
59 | 59 | } |