@@ -27,45 +27,45 @@ |
||
27 | 27 | |
28 | 28 | class Config implements \OCP\GlobalScale\IConfig { |
29 | 29 | |
30 | - /** @var IConfig */ |
|
31 | - private $config; |
|
30 | + /** @var IConfig */ |
|
31 | + private $config; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Config constructor. |
|
35 | - * |
|
36 | - * @param IConfig $config |
|
37 | - */ |
|
38 | - public function __construct(IConfig $config) { |
|
39 | - $this->config = $config; |
|
40 | - } |
|
33 | + /** |
|
34 | + * Config constructor. |
|
35 | + * |
|
36 | + * @param IConfig $config |
|
37 | + */ |
|
38 | + public function __construct(IConfig $config) { |
|
39 | + $this->config = $config; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * check if global scale is enabled |
|
44 | - * |
|
45 | - * @since 12.0.1 |
|
46 | - * @return bool |
|
47 | - */ |
|
48 | - public function isGlobalScaleEnabled() { |
|
49 | - $enabled = $this->config->getSystemValue('gs.enabled', false); |
|
50 | - return $enabled !== false; |
|
51 | - } |
|
42 | + /** |
|
43 | + * check if global scale is enabled |
|
44 | + * |
|
45 | + * @since 12.0.1 |
|
46 | + * @return bool |
|
47 | + */ |
|
48 | + public function isGlobalScaleEnabled() { |
|
49 | + $enabled = $this->config->getSystemValue('gs.enabled', false); |
|
50 | + return $enabled !== false; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * check if federation should only be used internally in a global scale setup |
|
55 | - * |
|
56 | - * @since 12.0.1 |
|
57 | - * @return bool |
|
58 | - */ |
|
59 | - public function onlyInternalFederation() { |
|
60 | - // if global scale is disabled federation works always globally |
|
61 | - $gsEnabled = $this->isGlobalScaleEnabled(); |
|
62 | - if ($gsEnabled === false) { |
|
63 | - return false; |
|
64 | - } |
|
53 | + /** |
|
54 | + * check if federation should only be used internally in a global scale setup |
|
55 | + * |
|
56 | + * @since 12.0.1 |
|
57 | + * @return bool |
|
58 | + */ |
|
59 | + public function onlyInternalFederation() { |
|
60 | + // if global scale is disabled federation works always globally |
|
61 | + $gsEnabled = $this->isGlobalScaleEnabled(); |
|
62 | + if ($gsEnabled === false) { |
|
63 | + return false; |
|
64 | + } |
|
65 | 65 | |
66 | - $enabled = $this->config->getSystemValue('gs.federation', 'internal'); |
|
66 | + $enabled = $this->config->getSystemValue('gs.federation', 'internal'); |
|
67 | 67 | |
68 | - return $enabled === 'internal'; |
|
69 | - } |
|
68 | + return $enabled === 'internal'; |
|
69 | + } |
|
70 | 70 | |
71 | 71 | } |
@@ -32,20 +32,20 @@ |
||
32 | 32 | */ |
33 | 33 | interface IConfig { |
34 | 34 | |
35 | - /** |
|
36 | - * check if global scale is enabled |
|
37 | - * |
|
38 | - * @since 12.0.1 |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - public function isGlobalScaleEnabled(); |
|
35 | + /** |
|
36 | + * check if global scale is enabled |
|
37 | + * |
|
38 | + * @since 12.0.1 |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + public function isGlobalScaleEnabled(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * check if federation should only be used internally in a global scale setup |
|
45 | - * |
|
46 | - * @since 12.0.1 |
|
47 | - * @return bool |
|
48 | - */ |
|
49 | - public function onlyInternalFederation(); |
|
43 | + /** |
|
44 | + * check if federation should only be used internally in a global scale setup |
|
45 | + * |
|
46 | + * @since 12.0.1 |
|
47 | + * @return bool |
|
48 | + */ |
|
49 | + public function onlyInternalFederation(); |
|
50 | 50 | |
51 | 51 | } |
@@ -35,161 +35,161 @@ |
||
35 | 35 | */ |
36 | 36 | interface IEncryptionModule { |
37 | 37 | |
38 | - /** |
|
39 | - * @return string defining the technical unique id |
|
40 | - * @since 8.1.0 |
|
41 | - */ |
|
42 | - public function getId(); |
|
43 | - |
|
44 | - /** |
|
45 | - * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
46 | - * |
|
47 | - * @return string |
|
48 | - * @since 8.1.0 |
|
49 | - */ |
|
50 | - public function getDisplayName(); |
|
51 | - |
|
52 | - /** |
|
53 | - * start receiving chunks from a file. This is the place where you can |
|
54 | - * perform some initial step before starting encrypting/decrypting the |
|
55 | - * chunks |
|
56 | - * |
|
57 | - * @param string $path to the file |
|
58 | - * @param string $user who read/write the file (null for public access) |
|
59 | - * @param string $mode php stream open mode |
|
60 | - * @param array $header contains the header data read from the file |
|
61 | - * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
62 | - * |
|
63 | - * $return array $header contain data as key-value pairs which should be |
|
64 | - * written to the header, in case of a write operation |
|
65 | - * or if no additional data is needed return a empty array |
|
66 | - * @since 8.1.0 |
|
67 | - */ |
|
68 | - public function begin($path, $user, $mode, array $header, array $accessList); |
|
69 | - |
|
70 | - /** |
|
71 | - * last chunk received. This is the place where you can perform some final |
|
72 | - * operation and return some remaining data if something is left in your |
|
73 | - * buffer. |
|
74 | - * |
|
75 | - * @param string $path to the file |
|
76 | - * @param string $position id of the last block (looks like "<Number>end") |
|
77 | - * |
|
78 | - * @return string remained data which should be written to the file in case |
|
79 | - * of a write operation |
|
80 | - * |
|
81 | - * @since 8.1.0 |
|
82 | - * @since 9.0.0 parameter $position added |
|
83 | - */ |
|
84 | - public function end($path, $position); |
|
85 | - |
|
86 | - /** |
|
87 | - * encrypt data |
|
88 | - * |
|
89 | - * @param string $data you want to encrypt |
|
90 | - * @param string $position position of the block we want to encrypt (starts with '0') |
|
91 | - * |
|
92 | - * @return mixed encrypted data |
|
93 | - * |
|
94 | - * @since 8.1.0 |
|
95 | - * @since 9.0.0 parameter $position added |
|
96 | - */ |
|
97 | - public function encrypt($data, $position); |
|
98 | - |
|
99 | - /** |
|
100 | - * decrypt data |
|
101 | - * |
|
102 | - * @param string $data you want to decrypt |
|
103 | - * @param string $position position of the block we want to decrypt |
|
104 | - * |
|
105 | - * @return mixed decrypted data |
|
106 | - * |
|
107 | - * @since 8.1.0 |
|
108 | - * @since 9.0.0 parameter $position added |
|
109 | - */ |
|
110 | - public function decrypt($data, $position); |
|
111 | - |
|
112 | - /** |
|
113 | - * update encrypted file, e.g. give additional users access to the file |
|
114 | - * |
|
115 | - * @param string $path path to the file which should be updated |
|
116 | - * @param string $uid of the user who performs the operation |
|
117 | - * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
118 | - * @return boolean |
|
119 | - * @since 8.1.0 |
|
120 | - */ |
|
121 | - public function update($path, $uid, array $accessList); |
|
122 | - |
|
123 | - /** |
|
124 | - * should the file be encrypted or not |
|
125 | - * |
|
126 | - * @param string $path |
|
127 | - * @return boolean |
|
128 | - * @since 8.1.0 |
|
129 | - */ |
|
130 | - public function shouldEncrypt($path); |
|
131 | - |
|
132 | - /** |
|
133 | - * get size of the unencrypted payload per block. |
|
134 | - * ownCloud read/write files with a block size of 8192 byte |
|
135 | - * |
|
136 | - * @param bool $signed |
|
137 | - * @return int |
|
138 | - * @since 8.1.0 optional parameter $signed was added in 9.0.0 |
|
139 | - */ |
|
140 | - public function getUnencryptedBlockSize($signed = false); |
|
141 | - |
|
142 | - /** |
|
143 | - * check if the encryption module is able to read the file, |
|
144 | - * e.g. if all encryption keys exists |
|
145 | - * |
|
146 | - * @param string $path |
|
147 | - * @param string $uid user for whom we want to check if he can read the file |
|
148 | - * @return boolean |
|
149 | - * @since 8.1.0 |
|
150 | - */ |
|
151 | - public function isReadable($path, $uid); |
|
152 | - |
|
153 | - /** |
|
154 | - * Initial encryption of all files |
|
155 | - * |
|
156 | - * @param InputInterface $input |
|
157 | - * @param OutputInterface $output write some status information to the terminal during encryption |
|
158 | - * @since 8.2.0 |
|
159 | - */ |
|
160 | - public function encryptAll(InputInterface $input, OutputInterface $output); |
|
161 | - |
|
162 | - /** |
|
163 | - * prepare encryption module to decrypt all files |
|
164 | - * |
|
165 | - * @param InputInterface $input |
|
166 | - * @param OutputInterface $output write some status information to the terminal during encryption |
|
167 | - * @param $user (optional) for which the files should be decrypted, default = all users |
|
168 | - * @return bool return false on failure or if it isn't supported by the module |
|
169 | - * @since 8.2.0 |
|
170 | - */ |
|
171 | - public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = ''); |
|
172 | - |
|
173 | - /** |
|
174 | - * Check if the module is ready to be used by that specific user. |
|
175 | - * In case a module is not ready - because e.g. key pairs have not been generated |
|
176 | - * upon login this method can return false before any operation starts and might |
|
177 | - * cause issues during operations. |
|
178 | - * |
|
179 | - * @param string $user |
|
180 | - * @return boolean |
|
181 | - * @since 9.1.0 |
|
182 | - */ |
|
183 | - public function isReadyForUser($user); |
|
184 | - |
|
185 | - /** |
|
186 | - * Does the encryption module needs a detailed list of users with access to the file? |
|
187 | - * For example if the encryption module uses per-user encryption keys and needs to know |
|
188 | - * the users with access to the file to encrypt/decrypt it. |
|
189 | - * |
|
190 | - * @since 13.0.0 |
|
191 | - * @return bool |
|
192 | - */ |
|
193 | - public function needDetailedAccessList(); |
|
38 | + /** |
|
39 | + * @return string defining the technical unique id |
|
40 | + * @since 8.1.0 |
|
41 | + */ |
|
42 | + public function getId(); |
|
43 | + |
|
44 | + /** |
|
45 | + * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
46 | + * |
|
47 | + * @return string |
|
48 | + * @since 8.1.0 |
|
49 | + */ |
|
50 | + public function getDisplayName(); |
|
51 | + |
|
52 | + /** |
|
53 | + * start receiving chunks from a file. This is the place where you can |
|
54 | + * perform some initial step before starting encrypting/decrypting the |
|
55 | + * chunks |
|
56 | + * |
|
57 | + * @param string $path to the file |
|
58 | + * @param string $user who read/write the file (null for public access) |
|
59 | + * @param string $mode php stream open mode |
|
60 | + * @param array $header contains the header data read from the file |
|
61 | + * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
62 | + * |
|
63 | + * $return array $header contain data as key-value pairs which should be |
|
64 | + * written to the header, in case of a write operation |
|
65 | + * or if no additional data is needed return a empty array |
|
66 | + * @since 8.1.0 |
|
67 | + */ |
|
68 | + public function begin($path, $user, $mode, array $header, array $accessList); |
|
69 | + |
|
70 | + /** |
|
71 | + * last chunk received. This is the place where you can perform some final |
|
72 | + * operation and return some remaining data if something is left in your |
|
73 | + * buffer. |
|
74 | + * |
|
75 | + * @param string $path to the file |
|
76 | + * @param string $position id of the last block (looks like "<Number>end") |
|
77 | + * |
|
78 | + * @return string remained data which should be written to the file in case |
|
79 | + * of a write operation |
|
80 | + * |
|
81 | + * @since 8.1.0 |
|
82 | + * @since 9.0.0 parameter $position added |
|
83 | + */ |
|
84 | + public function end($path, $position); |
|
85 | + |
|
86 | + /** |
|
87 | + * encrypt data |
|
88 | + * |
|
89 | + * @param string $data you want to encrypt |
|
90 | + * @param string $position position of the block we want to encrypt (starts with '0') |
|
91 | + * |
|
92 | + * @return mixed encrypted data |
|
93 | + * |
|
94 | + * @since 8.1.0 |
|
95 | + * @since 9.0.0 parameter $position added |
|
96 | + */ |
|
97 | + public function encrypt($data, $position); |
|
98 | + |
|
99 | + /** |
|
100 | + * decrypt data |
|
101 | + * |
|
102 | + * @param string $data you want to decrypt |
|
103 | + * @param string $position position of the block we want to decrypt |
|
104 | + * |
|
105 | + * @return mixed decrypted data |
|
106 | + * |
|
107 | + * @since 8.1.0 |
|
108 | + * @since 9.0.0 parameter $position added |
|
109 | + */ |
|
110 | + public function decrypt($data, $position); |
|
111 | + |
|
112 | + /** |
|
113 | + * update encrypted file, e.g. give additional users access to the file |
|
114 | + * |
|
115 | + * @param string $path path to the file which should be updated |
|
116 | + * @param string $uid of the user who performs the operation |
|
117 | + * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
118 | + * @return boolean |
|
119 | + * @since 8.1.0 |
|
120 | + */ |
|
121 | + public function update($path, $uid, array $accessList); |
|
122 | + |
|
123 | + /** |
|
124 | + * should the file be encrypted or not |
|
125 | + * |
|
126 | + * @param string $path |
|
127 | + * @return boolean |
|
128 | + * @since 8.1.0 |
|
129 | + */ |
|
130 | + public function shouldEncrypt($path); |
|
131 | + |
|
132 | + /** |
|
133 | + * get size of the unencrypted payload per block. |
|
134 | + * ownCloud read/write files with a block size of 8192 byte |
|
135 | + * |
|
136 | + * @param bool $signed |
|
137 | + * @return int |
|
138 | + * @since 8.1.0 optional parameter $signed was added in 9.0.0 |
|
139 | + */ |
|
140 | + public function getUnencryptedBlockSize($signed = false); |
|
141 | + |
|
142 | + /** |
|
143 | + * check if the encryption module is able to read the file, |
|
144 | + * e.g. if all encryption keys exists |
|
145 | + * |
|
146 | + * @param string $path |
|
147 | + * @param string $uid user for whom we want to check if he can read the file |
|
148 | + * @return boolean |
|
149 | + * @since 8.1.0 |
|
150 | + */ |
|
151 | + public function isReadable($path, $uid); |
|
152 | + |
|
153 | + /** |
|
154 | + * Initial encryption of all files |
|
155 | + * |
|
156 | + * @param InputInterface $input |
|
157 | + * @param OutputInterface $output write some status information to the terminal during encryption |
|
158 | + * @since 8.2.0 |
|
159 | + */ |
|
160 | + public function encryptAll(InputInterface $input, OutputInterface $output); |
|
161 | + |
|
162 | + /** |
|
163 | + * prepare encryption module to decrypt all files |
|
164 | + * |
|
165 | + * @param InputInterface $input |
|
166 | + * @param OutputInterface $output write some status information to the terminal during encryption |
|
167 | + * @param $user (optional) for which the files should be decrypted, default = all users |
|
168 | + * @return bool return false on failure or if it isn't supported by the module |
|
169 | + * @since 8.2.0 |
|
170 | + */ |
|
171 | + public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = ''); |
|
172 | + |
|
173 | + /** |
|
174 | + * Check if the module is ready to be used by that specific user. |
|
175 | + * In case a module is not ready - because e.g. key pairs have not been generated |
|
176 | + * upon login this method can return false before any operation starts and might |
|
177 | + * cause issues during operations. |
|
178 | + * |
|
179 | + * @param string $user |
|
180 | + * @return boolean |
|
181 | + * @since 9.1.0 |
|
182 | + */ |
|
183 | + public function isReadyForUser($user); |
|
184 | + |
|
185 | + /** |
|
186 | + * Does the encryption module needs a detailed list of users with access to the file? |
|
187 | + * For example if the encryption module uses per-user encryption keys and needs to know |
|
188 | + * the users with access to the file to encrypt/decrypt it. |
|
189 | + * |
|
190 | + * @since 13.0.0 |
|
191 | + * @return bool |
|
192 | + */ |
|
193 | + public function needDetailedAccessList(); |
|
194 | 194 | |
195 | 195 | } |
@@ -33,162 +33,162 @@ |
||
33 | 33 | */ |
34 | 34 | class Update { |
35 | 35 | |
36 | - /** @var \OC\Files\View */ |
|
37 | - protected $view; |
|
38 | - |
|
39 | - /** @var \OC\Encryption\Util */ |
|
40 | - protected $util; |
|
41 | - |
|
42 | - /** @var \OC\Files\Mount\Manager */ |
|
43 | - protected $mountManager; |
|
44 | - |
|
45 | - /** @var \OC\Encryption\Manager */ |
|
46 | - protected $encryptionManager; |
|
47 | - |
|
48 | - /** @var string */ |
|
49 | - protected $uid; |
|
50 | - |
|
51 | - /** @var \OC\Encryption\File */ |
|
52 | - protected $file; |
|
53 | - |
|
54 | - /** |
|
55 | - * |
|
56 | - * @param \OC\Files\View $view |
|
57 | - * @param \OC\Encryption\Util $util |
|
58 | - * @param \OC\Files\Mount\Manager $mountManager |
|
59 | - * @param \OC\Encryption\Manager $encryptionManager |
|
60 | - * @param \OC\Encryption\File $file |
|
61 | - * @param string $uid |
|
62 | - */ |
|
63 | - public function __construct( |
|
64 | - View $view, |
|
65 | - Util $util, |
|
66 | - Mount\Manager $mountManager, |
|
67 | - Manager $encryptionManager, |
|
68 | - File $file, |
|
69 | - $uid |
|
70 | - ) { |
|
71 | - |
|
72 | - $this->view = $view; |
|
73 | - $this->util = $util; |
|
74 | - $this->mountManager = $mountManager; |
|
75 | - $this->encryptionManager = $encryptionManager; |
|
76 | - $this->file = $file; |
|
77 | - $this->uid = $uid; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * hook after file was shared |
|
82 | - * |
|
83 | - * @param array $params |
|
84 | - */ |
|
85 | - public function postShared($params) { |
|
86 | - if ($this->encryptionManager->isEnabled()) { |
|
87 | - if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
|
88 | - $path = Filesystem::getPath($params['fileSource']); |
|
89 | - list($owner, $ownerPath) = $this->getOwnerPath($path); |
|
90 | - $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
91 | - $this->update($absPath); |
|
92 | - } |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * hook after file was unshared |
|
98 | - * |
|
99 | - * @param array $params |
|
100 | - */ |
|
101 | - public function postUnshared($params) { |
|
102 | - if ($this->encryptionManager->isEnabled()) { |
|
103 | - if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
|
104 | - $path = Filesystem::getPath($params['fileSource']); |
|
105 | - list($owner, $ownerPath) = $this->getOwnerPath($path); |
|
106 | - $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
107 | - $this->update($absPath); |
|
108 | - } |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * inform encryption module that a file was restored from the trash bin, |
|
114 | - * e.g. to update the encryption keys |
|
115 | - * |
|
116 | - * @param array $params |
|
117 | - */ |
|
118 | - public function postRestore($params) { |
|
119 | - if ($this->encryptionManager->isEnabled()) { |
|
120 | - $path = Filesystem::normalizePath('/' . $this->uid . '/files/' . $params['filePath']); |
|
121 | - $this->update($path); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * inform encryption module that a file was renamed, |
|
127 | - * e.g. to update the encryption keys |
|
128 | - * |
|
129 | - * @param array $params |
|
130 | - */ |
|
131 | - public function postRename($params) { |
|
132 | - $source = $params['oldpath']; |
|
133 | - $target = $params['newpath']; |
|
134 | - if( |
|
135 | - $this->encryptionManager->isEnabled() && |
|
136 | - dirname($source) !== dirname($target) |
|
137 | - ) { |
|
138 | - list($owner, $ownerPath) = $this->getOwnerPath($target); |
|
139 | - $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
140 | - $this->update($absPath); |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * get owner and path relative to data/<owner>/files |
|
146 | - * |
|
147 | - * @param string $path path to file for current user |
|
148 | - * @return array ['owner' => $owner, 'path' => $path] |
|
149 | - * @throw \InvalidArgumentException |
|
150 | - */ |
|
151 | - protected function getOwnerPath($path) { |
|
152 | - $info = Filesystem::getFileInfo($path); |
|
153 | - $owner = Filesystem::getOwner($path); |
|
154 | - $view = new View('/' . $owner . '/files'); |
|
155 | - $path = $view->getPath($info->getId()); |
|
156 | - if ($path === null) { |
|
157 | - throw new \InvalidArgumentException('No file found for ' . $info->getId()); |
|
158 | - } |
|
159 | - |
|
160 | - return array($owner, $path); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * notify encryption module about added/removed users from a file/folder |
|
165 | - * |
|
166 | - * @param string $path relative to data/ |
|
167 | - * @throws Exceptions\ModuleDoesNotExistsException |
|
168 | - */ |
|
169 | - public function update($path) { |
|
170 | - |
|
171 | - $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
172 | - |
|
173 | - // if the encryption module doesn't encrypt the files on a per-user basis |
|
174 | - // we have nothing to do here. |
|
175 | - if ($encryptionModule->needDetailedAccessList() === false) { |
|
176 | - return; |
|
177 | - } |
|
178 | - |
|
179 | - // if a folder was shared, get a list of all (sub-)folders |
|
180 | - if ($this->view->is_dir($path)) { |
|
181 | - $allFiles = $this->util->getAllFiles($path); |
|
182 | - } else { |
|
183 | - $allFiles = array($path); |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - |
|
188 | - foreach ($allFiles as $file) { |
|
189 | - $usersSharing = $this->file->getAccessList($file); |
|
190 | - $encryptionModule->update($file, $this->uid, $usersSharing); |
|
191 | - } |
|
192 | - } |
|
36 | + /** @var \OC\Files\View */ |
|
37 | + protected $view; |
|
38 | + |
|
39 | + /** @var \OC\Encryption\Util */ |
|
40 | + protected $util; |
|
41 | + |
|
42 | + /** @var \OC\Files\Mount\Manager */ |
|
43 | + protected $mountManager; |
|
44 | + |
|
45 | + /** @var \OC\Encryption\Manager */ |
|
46 | + protected $encryptionManager; |
|
47 | + |
|
48 | + /** @var string */ |
|
49 | + protected $uid; |
|
50 | + |
|
51 | + /** @var \OC\Encryption\File */ |
|
52 | + protected $file; |
|
53 | + |
|
54 | + /** |
|
55 | + * |
|
56 | + * @param \OC\Files\View $view |
|
57 | + * @param \OC\Encryption\Util $util |
|
58 | + * @param \OC\Files\Mount\Manager $mountManager |
|
59 | + * @param \OC\Encryption\Manager $encryptionManager |
|
60 | + * @param \OC\Encryption\File $file |
|
61 | + * @param string $uid |
|
62 | + */ |
|
63 | + public function __construct( |
|
64 | + View $view, |
|
65 | + Util $util, |
|
66 | + Mount\Manager $mountManager, |
|
67 | + Manager $encryptionManager, |
|
68 | + File $file, |
|
69 | + $uid |
|
70 | + ) { |
|
71 | + |
|
72 | + $this->view = $view; |
|
73 | + $this->util = $util; |
|
74 | + $this->mountManager = $mountManager; |
|
75 | + $this->encryptionManager = $encryptionManager; |
|
76 | + $this->file = $file; |
|
77 | + $this->uid = $uid; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * hook after file was shared |
|
82 | + * |
|
83 | + * @param array $params |
|
84 | + */ |
|
85 | + public function postShared($params) { |
|
86 | + if ($this->encryptionManager->isEnabled()) { |
|
87 | + if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
|
88 | + $path = Filesystem::getPath($params['fileSource']); |
|
89 | + list($owner, $ownerPath) = $this->getOwnerPath($path); |
|
90 | + $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
91 | + $this->update($absPath); |
|
92 | + } |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * hook after file was unshared |
|
98 | + * |
|
99 | + * @param array $params |
|
100 | + */ |
|
101 | + public function postUnshared($params) { |
|
102 | + if ($this->encryptionManager->isEnabled()) { |
|
103 | + if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
|
104 | + $path = Filesystem::getPath($params['fileSource']); |
|
105 | + list($owner, $ownerPath) = $this->getOwnerPath($path); |
|
106 | + $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
107 | + $this->update($absPath); |
|
108 | + } |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * inform encryption module that a file was restored from the trash bin, |
|
114 | + * e.g. to update the encryption keys |
|
115 | + * |
|
116 | + * @param array $params |
|
117 | + */ |
|
118 | + public function postRestore($params) { |
|
119 | + if ($this->encryptionManager->isEnabled()) { |
|
120 | + $path = Filesystem::normalizePath('/' . $this->uid . '/files/' . $params['filePath']); |
|
121 | + $this->update($path); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * inform encryption module that a file was renamed, |
|
127 | + * e.g. to update the encryption keys |
|
128 | + * |
|
129 | + * @param array $params |
|
130 | + */ |
|
131 | + public function postRename($params) { |
|
132 | + $source = $params['oldpath']; |
|
133 | + $target = $params['newpath']; |
|
134 | + if( |
|
135 | + $this->encryptionManager->isEnabled() && |
|
136 | + dirname($source) !== dirname($target) |
|
137 | + ) { |
|
138 | + list($owner, $ownerPath) = $this->getOwnerPath($target); |
|
139 | + $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
140 | + $this->update($absPath); |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * get owner and path relative to data/<owner>/files |
|
146 | + * |
|
147 | + * @param string $path path to file for current user |
|
148 | + * @return array ['owner' => $owner, 'path' => $path] |
|
149 | + * @throw \InvalidArgumentException |
|
150 | + */ |
|
151 | + protected function getOwnerPath($path) { |
|
152 | + $info = Filesystem::getFileInfo($path); |
|
153 | + $owner = Filesystem::getOwner($path); |
|
154 | + $view = new View('/' . $owner . '/files'); |
|
155 | + $path = $view->getPath($info->getId()); |
|
156 | + if ($path === null) { |
|
157 | + throw new \InvalidArgumentException('No file found for ' . $info->getId()); |
|
158 | + } |
|
159 | + |
|
160 | + return array($owner, $path); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * notify encryption module about added/removed users from a file/folder |
|
165 | + * |
|
166 | + * @param string $path relative to data/ |
|
167 | + * @throws Exceptions\ModuleDoesNotExistsException |
|
168 | + */ |
|
169 | + public function update($path) { |
|
170 | + |
|
171 | + $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
172 | + |
|
173 | + // if the encryption module doesn't encrypt the files on a per-user basis |
|
174 | + // we have nothing to do here. |
|
175 | + if ($encryptionModule->needDetailedAccessList() === false) { |
|
176 | + return; |
|
177 | + } |
|
178 | + |
|
179 | + // if a folder was shared, get a list of all (sub-)folders |
|
180 | + if ($this->view->is_dir($path)) { |
|
181 | + $allFiles = $this->util->getAllFiles($path); |
|
182 | + } else { |
|
183 | + $allFiles = array($path); |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + |
|
188 | + foreach ($allFiles as $file) { |
|
189 | + $usersSharing = $this->file->getAccessList($file); |
|
190 | + $encryptionModule->update($file, $this->uid, $usersSharing); |
|
191 | + } |
|
192 | + } |
|
193 | 193 | |
194 | 194 | } |
@@ -35,43 +35,43 @@ |
||
35 | 35 | class SetMasterKeyStatus implements IRepairStep { |
36 | 36 | |
37 | 37 | |
38 | - /** @var IConfig */ |
|
39 | - private $config; |
|
38 | + /** @var IConfig */ |
|
39 | + private $config; |
|
40 | 40 | |
41 | 41 | |
42 | - public function __construct(IConfig $config) { |
|
43 | - $this->config = $config; |
|
44 | - } |
|
42 | + public function __construct(IConfig $config) { |
|
43 | + $this->config = $config; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Returns the step's name |
|
48 | - * |
|
49 | - * @return string |
|
50 | - * @since 9.1.0 |
|
51 | - */ |
|
52 | - public function getName() { |
|
53 | - return 'Write default encryption module configuration to the database'; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Returns the step's name |
|
48 | + * |
|
49 | + * @return string |
|
50 | + * @since 9.1.0 |
|
51 | + */ |
|
52 | + public function getName() { |
|
53 | + return 'Write default encryption module configuration to the database'; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param IOutput $output |
|
58 | - */ |
|
59 | - public function run(IOutput $output) { |
|
60 | - if (!$this->shouldRun()) { |
|
61 | - return; |
|
62 | - } |
|
56 | + /** |
|
57 | + * @param IOutput $output |
|
58 | + */ |
|
59 | + public function run(IOutput $output) { |
|
60 | + if (!$this->shouldRun()) { |
|
61 | + return; |
|
62 | + } |
|
63 | 63 | |
64 | - // if no config for the master key is set we set it explicitly to '0' in |
|
65 | - // order not to break old installations because the default changed to '1'. |
|
66 | - $configAlreadySet = $this->config->getAppValue('encryption', 'useMasterKey', false); |
|
67 | - if ($configAlreadySet === false) { |
|
68 | - $this->config->setAppValue('encryption', 'useMasterKey', '0'); |
|
69 | - } |
|
70 | - } |
|
64 | + // if no config for the master key is set we set it explicitly to '0' in |
|
65 | + // order not to break old installations because the default changed to '1'. |
|
66 | + $configAlreadySet = $this->config->getAppValue('encryption', 'useMasterKey', false); |
|
67 | + if ($configAlreadySet === false) { |
|
68 | + $this->config->setAppValue('encryption', 'useMasterKey', '0'); |
|
69 | + } |
|
70 | + } |
|
71 | 71 | |
72 | - protected function shouldRun() { |
|
73 | - $appVersion = $this->config->getAppValue('encryption', 'installed_version', '0.0.0'); |
|
74 | - return version_compare($appVersion, '2.0.0', '<'); |
|
75 | - } |
|
72 | + protected function shouldRun() { |
|
73 | + $appVersion = $this->config->getAppValue('encryption', 'installed_version', '0.0.0'); |
|
74 | + return version_compare($appVersion, '2.0.0', '<'); |
|
75 | + } |
|
76 | 76 | |
77 | 77 | } |
@@ -33,57 +33,57 @@ |
||
33 | 33 | |
34 | 34 | class DisableMasterKey extends Command { |
35 | 35 | |
36 | - /** @var Util */ |
|
37 | - protected $util; |
|
38 | - |
|
39 | - /** @var IConfig */ |
|
40 | - protected $config; |
|
41 | - |
|
42 | - /** @var QuestionHelper */ |
|
43 | - protected $questionHelper; |
|
44 | - |
|
45 | - /** |
|
46 | - * @param Util $util |
|
47 | - * @param IConfig $config |
|
48 | - * @param QuestionHelper $questionHelper |
|
49 | - */ |
|
50 | - public function __construct(Util $util, |
|
51 | - IConfig $config, |
|
52 | - QuestionHelper $questionHelper) { |
|
53 | - |
|
54 | - $this->util = $util; |
|
55 | - $this->config = $config; |
|
56 | - $this->questionHelper = $questionHelper; |
|
57 | - parent::__construct(); |
|
58 | - } |
|
59 | - |
|
60 | - protected function configure() { |
|
61 | - $this |
|
62 | - ->setName('encryption:disable-master-key') |
|
63 | - ->setDescription('Disable the master key and use per-user keys instead. Only available for fresh installations with no existing encrypted data! There is no way to enable it again.'); |
|
64 | - } |
|
65 | - |
|
66 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
67 | - |
|
68 | - $isMasterKeyEnabled = $this->util->isMasterKeyEnabled(); |
|
69 | - |
|
70 | - if(!$isMasterKeyEnabled) { |
|
71 | - $output->writeln('Master key already disabled'); |
|
72 | - } else { |
|
73 | - $question = new ConfirmationQuestion( |
|
74 | - 'Warning: Only perform this operation for a fresh installations with no existing encrypted data! ' |
|
75 | - . 'There is no way to enable the master key again. ' |
|
76 | - . 'We strongly recommend to keep the master key, it provides significant performance improvements ' |
|
77 | - . 'and is easier to handle for both, users and administrators. ' |
|
78 | - . 'Do you really want to switch to per-user keys? (y/n) ', false); |
|
79 | - if ($this->questionHelper->ask($input, $output, $question)) { |
|
80 | - $this->config->setAppValue('encryption', 'useMasterKey', '0'); |
|
81 | - $output->writeln('Master key successfully disabled.'); |
|
82 | - } else { |
|
83 | - $output->writeln('aborted.'); |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - } |
|
36 | + /** @var Util */ |
|
37 | + protected $util; |
|
38 | + |
|
39 | + /** @var IConfig */ |
|
40 | + protected $config; |
|
41 | + |
|
42 | + /** @var QuestionHelper */ |
|
43 | + protected $questionHelper; |
|
44 | + |
|
45 | + /** |
|
46 | + * @param Util $util |
|
47 | + * @param IConfig $config |
|
48 | + * @param QuestionHelper $questionHelper |
|
49 | + */ |
|
50 | + public function __construct(Util $util, |
|
51 | + IConfig $config, |
|
52 | + QuestionHelper $questionHelper) { |
|
53 | + |
|
54 | + $this->util = $util; |
|
55 | + $this->config = $config; |
|
56 | + $this->questionHelper = $questionHelper; |
|
57 | + parent::__construct(); |
|
58 | + } |
|
59 | + |
|
60 | + protected function configure() { |
|
61 | + $this |
|
62 | + ->setName('encryption:disable-master-key') |
|
63 | + ->setDescription('Disable the master key and use per-user keys instead. Only available for fresh installations with no existing encrypted data! There is no way to enable it again.'); |
|
64 | + } |
|
65 | + |
|
66 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
67 | + |
|
68 | + $isMasterKeyEnabled = $this->util->isMasterKeyEnabled(); |
|
69 | + |
|
70 | + if(!$isMasterKeyEnabled) { |
|
71 | + $output->writeln('Master key already disabled'); |
|
72 | + } else { |
|
73 | + $question = new ConfirmationQuestion( |
|
74 | + 'Warning: Only perform this operation for a fresh installations with no existing encrypted data! ' |
|
75 | + . 'There is no way to enable the master key again. ' |
|
76 | + . 'We strongly recommend to keep the master key, it provides significant performance improvements ' |
|
77 | + . 'and is easier to handle for both, users and administrators. ' |
|
78 | + . 'Do you really want to switch to per-user keys? (y/n) ', false); |
|
79 | + if ($this->questionHelper->ask($input, $output, $question)) { |
|
80 | + $this->config->setAppValue('encryption', 'useMasterKey', '0'); |
|
81 | + $output->writeln('Master key successfully disabled.'); |
|
82 | + } else { |
|
83 | + $output->writeln('aborted.'); |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + } |
|
88 | 88 | |
89 | 89 | } |
@@ -32,72 +32,72 @@ |
||
32 | 32 | |
33 | 33 | class Personal implements ISettings { |
34 | 34 | |
35 | - /** @var IManager */ |
|
36 | - private $encryptionManager; |
|
35 | + /** @var IManager */ |
|
36 | + private $encryptionManager; |
|
37 | 37 | |
38 | - /** @var UserGlobalStoragesService */ |
|
39 | - private $userGlobalStoragesService; |
|
38 | + /** @var UserGlobalStoragesService */ |
|
39 | + private $userGlobalStoragesService; |
|
40 | 40 | |
41 | - /** @var BackendService */ |
|
42 | - private $backendService; |
|
41 | + /** @var BackendService */ |
|
42 | + private $backendService; |
|
43 | 43 | |
44 | - /** @var GlobalAuth */ |
|
45 | - private $globalAuth; |
|
44 | + /** @var GlobalAuth */ |
|
45 | + private $globalAuth; |
|
46 | 46 | |
47 | - /** @var IUserSession */ |
|
48 | - private $userSession; |
|
47 | + /** @var IUserSession */ |
|
48 | + private $userSession; |
|
49 | 49 | |
50 | - public function __construct( |
|
51 | - IManager $encryptionManager, |
|
52 | - UserGlobalStoragesService $userGlobalStoragesService, |
|
53 | - BackendService $backendService, |
|
54 | - GlobalAuth $globalAuth, |
|
55 | - IUserSession $userSession |
|
56 | - ) { |
|
57 | - $this->encryptionManager = $encryptionManager; |
|
58 | - $this->userGlobalStoragesService = $userGlobalStoragesService; |
|
59 | - $this->backendService = $backendService; |
|
60 | - $this->globalAuth = $globalAuth; |
|
61 | - $this->userSession = $userSession; |
|
62 | - } |
|
50 | + public function __construct( |
|
51 | + IManager $encryptionManager, |
|
52 | + UserGlobalStoragesService $userGlobalStoragesService, |
|
53 | + BackendService $backendService, |
|
54 | + GlobalAuth $globalAuth, |
|
55 | + IUserSession $userSession |
|
56 | + ) { |
|
57 | + $this->encryptionManager = $encryptionManager; |
|
58 | + $this->userGlobalStoragesService = $userGlobalStoragesService; |
|
59 | + $this->backendService = $backendService; |
|
60 | + $this->globalAuth = $globalAuth; |
|
61 | + $this->userSession = $userSession; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return TemplateResponse |
|
66 | - */ |
|
67 | - public function getForm() { |
|
68 | - $uid = $this->userSession->getUser()->getUID(); |
|
64 | + /** |
|
65 | + * @return TemplateResponse |
|
66 | + */ |
|
67 | + public function getForm() { |
|
68 | + $uid = $this->userSession->getUser()->getUID(); |
|
69 | 69 | |
70 | - $parameters = [ |
|
71 | - 'encryptionEnabled' => $this->encryptionManager->isEnabled(), |
|
72 | - 'visibilityType' => BackendService::VISIBILITY_PERSONAL, |
|
73 | - 'storages' => $this->userGlobalStoragesService->getStorages(), |
|
74 | - 'backends' => $this->backendService->getAvailableBackends(), |
|
75 | - 'authMechanisms' => $this->backendService->getAuthMechanisms(), |
|
76 | - 'dependencies' => \OC_Mount_Config::dependencyMessage($this->backendService->getBackends()), |
|
77 | - 'allowUserMounting' => $this->backendService->isUserMountingAllowed(), |
|
78 | - 'globalCredentials' => $this->globalAuth->getAuth($uid), |
|
79 | - 'globalCredentialsUid' => $uid, |
|
80 | - ]; |
|
70 | + $parameters = [ |
|
71 | + 'encryptionEnabled' => $this->encryptionManager->isEnabled(), |
|
72 | + 'visibilityType' => BackendService::VISIBILITY_PERSONAL, |
|
73 | + 'storages' => $this->userGlobalStoragesService->getStorages(), |
|
74 | + 'backends' => $this->backendService->getAvailableBackends(), |
|
75 | + 'authMechanisms' => $this->backendService->getAuthMechanisms(), |
|
76 | + 'dependencies' => \OC_Mount_Config::dependencyMessage($this->backendService->getBackends()), |
|
77 | + 'allowUserMounting' => $this->backendService->isUserMountingAllowed(), |
|
78 | + 'globalCredentials' => $this->globalAuth->getAuth($uid), |
|
79 | + 'globalCredentialsUid' => $uid, |
|
80 | + ]; |
|
81 | 81 | |
82 | - return new TemplateResponse('files_external', 'settings', $parameters, ''); |
|
83 | - } |
|
82 | + return new TemplateResponse('files_external', 'settings', $parameters, ''); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @return string the section ID, e.g. 'sharing' |
|
87 | - */ |
|
88 | - public function getSection() { |
|
89 | - return 'externalstorages'; |
|
90 | - } |
|
85 | + /** |
|
86 | + * @return string the section ID, e.g. 'sharing' |
|
87 | + */ |
|
88 | + public function getSection() { |
|
89 | + return 'externalstorages'; |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @return int whether the form should be rather on the top or bottom of |
|
94 | - * the admin section. The forms are arranged in ascending order of the |
|
95 | - * priority values. It is required to return a value between 0 and 100. |
|
96 | - * |
|
97 | - * E.g.: 70 |
|
98 | - */ |
|
99 | - public function getPriority() { |
|
100 | - return 40; |
|
101 | - } |
|
92 | + /** |
|
93 | + * @return int whether the form should be rather on the top or bottom of |
|
94 | + * the admin section. The forms are arranged in ascending order of the |
|
95 | + * priority values. It is required to return a value between 0 and 100. |
|
96 | + * |
|
97 | + * E.g.: 70 |
|
98 | + */ |
|
99 | + public function getPriority() { |
|
100 | + return 40; |
|
101 | + } |
|
102 | 102 | |
103 | 103 | } |
@@ -26,80 +26,80 @@ |
||
26 | 26 | namespace OC\DB; |
27 | 27 | |
28 | 28 | class OracleConnection extends Connection { |
29 | - /** |
|
30 | - * Quote the keys of the array |
|
31 | - */ |
|
32 | - private function quoteKeys(array $data) { |
|
33 | - $return = []; |
|
34 | - $c = $this->getDatabasePlatform()->getIdentifierQuoteCharacter(); |
|
35 | - foreach($data as $key => $value) { |
|
36 | - if ($key[0] !== $c) { |
|
37 | - $return[$this->quoteIdentifier($key)] = $value; |
|
38 | - } else { |
|
39 | - $return[$key] = $value; |
|
40 | - } |
|
41 | - } |
|
42 | - return $return; |
|
43 | - } |
|
29 | + /** |
|
30 | + * Quote the keys of the array |
|
31 | + */ |
|
32 | + private function quoteKeys(array $data) { |
|
33 | + $return = []; |
|
34 | + $c = $this->getDatabasePlatform()->getIdentifierQuoteCharacter(); |
|
35 | + foreach($data as $key => $value) { |
|
36 | + if ($key[0] !== $c) { |
|
37 | + $return[$this->quoteIdentifier($key)] = $value; |
|
38 | + } else { |
|
39 | + $return[$key] = $value; |
|
40 | + } |
|
41 | + } |
|
42 | + return $return; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * {@inheritDoc} |
|
47 | - */ |
|
48 | - public function insert($tableName, array $data, array $types = array()) { |
|
49 | - if ($tableName[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
50 | - $tableName = $this->quoteIdentifier($tableName); |
|
51 | - } |
|
52 | - $data = $this->quoteKeys($data); |
|
53 | - return parent::insert($tableName, $data, $types); |
|
54 | - } |
|
45 | + /** |
|
46 | + * {@inheritDoc} |
|
47 | + */ |
|
48 | + public function insert($tableName, array $data, array $types = array()) { |
|
49 | + if ($tableName[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
50 | + $tableName = $this->quoteIdentifier($tableName); |
|
51 | + } |
|
52 | + $data = $this->quoteKeys($data); |
|
53 | + return parent::insert($tableName, $data, $types); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * {@inheritDoc} |
|
58 | - */ |
|
59 | - public function update($tableName, array $data, array $identifier, array $types = array()) { |
|
60 | - if ($tableName[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
61 | - $tableName = $this->quoteIdentifier($tableName); |
|
62 | - } |
|
63 | - $data = $this->quoteKeys($data); |
|
64 | - $identifier = $this->quoteKeys($identifier); |
|
65 | - return parent::update($tableName, $data, $identifier, $types); |
|
66 | - } |
|
56 | + /** |
|
57 | + * {@inheritDoc} |
|
58 | + */ |
|
59 | + public function update($tableName, array $data, array $identifier, array $types = array()) { |
|
60 | + if ($tableName[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
61 | + $tableName = $this->quoteIdentifier($tableName); |
|
62 | + } |
|
63 | + $data = $this->quoteKeys($data); |
|
64 | + $identifier = $this->quoteKeys($identifier); |
|
65 | + return parent::update($tableName, $data, $identifier, $types); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * {@inheritDoc} |
|
70 | - */ |
|
71 | - public function delete($tableExpression, array $identifier, array $types = array()) { |
|
72 | - if ($tableExpression[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
73 | - $tableExpression = $this->quoteIdentifier($tableExpression); |
|
74 | - } |
|
75 | - $identifier = $this->quoteKeys($identifier); |
|
76 | - return parent::delete($tableExpression, $identifier); |
|
77 | - } |
|
68 | + /** |
|
69 | + * {@inheritDoc} |
|
70 | + */ |
|
71 | + public function delete($tableExpression, array $identifier, array $types = array()) { |
|
72 | + if ($tableExpression[0] !== $this->getDatabasePlatform()->getIdentifierQuoteCharacter()) { |
|
73 | + $tableExpression = $this->quoteIdentifier($tableExpression); |
|
74 | + } |
|
75 | + $identifier = $this->quoteKeys($identifier); |
|
76 | + return parent::delete($tableExpression, $identifier); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Drop a table from the database if it exists |
|
81 | - * |
|
82 | - * @param string $table table name without the prefix |
|
83 | - */ |
|
84 | - public function dropTable($table) { |
|
85 | - $table = $this->tablePrefix . trim($table); |
|
86 | - $table = $this->quoteIdentifier($table); |
|
87 | - $schema = $this->getSchemaManager(); |
|
88 | - if($schema->tablesExist(array($table))) { |
|
89 | - $schema->dropTable($table); |
|
90 | - } |
|
91 | - } |
|
79 | + /** |
|
80 | + * Drop a table from the database if it exists |
|
81 | + * |
|
82 | + * @param string $table table name without the prefix |
|
83 | + */ |
|
84 | + public function dropTable($table) { |
|
85 | + $table = $this->tablePrefix . trim($table); |
|
86 | + $table = $this->quoteIdentifier($table); |
|
87 | + $schema = $this->getSchemaManager(); |
|
88 | + if($schema->tablesExist(array($table))) { |
|
89 | + $schema->dropTable($table); |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Check if a table exists |
|
95 | - * |
|
96 | - * @param string $table table name without the prefix |
|
97 | - * @return bool |
|
98 | - */ |
|
99 | - public function tableExists($table){ |
|
100 | - $table = $this->tablePrefix . trim($table); |
|
101 | - $table = $this->quoteIdentifier($table); |
|
102 | - $schema = $this->getSchemaManager(); |
|
103 | - return $schema->tablesExist(array($table)); |
|
104 | - } |
|
93 | + /** |
|
94 | + * Check if a table exists |
|
95 | + * |
|
96 | + * @param string $table table name without the prefix |
|
97 | + * @return bool |
|
98 | + */ |
|
99 | + public function tableExists($table){ |
|
100 | + $table = $this->tablePrefix . trim($table); |
|
101 | + $table = $this->quoteIdentifier($table); |
|
102 | + $schema = $this->getSchemaManager(); |
|
103 | + return $schema->tablesExist(array($table)); |
|
104 | + } |
|
105 | 105 | } |
@@ -43,272 +43,272 @@ |
||
43 | 43 | |
44 | 44 | class Migrator { |
45 | 45 | |
46 | - /** @var \Doctrine\DBAL\Connection */ |
|
47 | - protected $connection; |
|
48 | - |
|
49 | - /** @var ISecureRandom */ |
|
50 | - private $random; |
|
51 | - |
|
52 | - /** @var IConfig */ |
|
53 | - protected $config; |
|
54 | - |
|
55 | - /** @var EventDispatcher */ |
|
56 | - private $dispatcher; |
|
57 | - |
|
58 | - /** @var bool */ |
|
59 | - private $noEmit = false; |
|
60 | - |
|
61 | - /** |
|
62 | - * @param \Doctrine\DBAL\Connection|Connection $connection |
|
63 | - * @param ISecureRandom $random |
|
64 | - * @param IConfig $config |
|
65 | - * @param EventDispatcher $dispatcher |
|
66 | - */ |
|
67 | - public function __construct(\Doctrine\DBAL\Connection $connection, |
|
68 | - ISecureRandom $random, |
|
69 | - IConfig $config, |
|
70 | - EventDispatcher $dispatcher = null) { |
|
71 | - $this->connection = $connection; |
|
72 | - $this->random = $random; |
|
73 | - $this->config = $config; |
|
74 | - $this->dispatcher = $dispatcher; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
79 | - */ |
|
80 | - public function migrate(Schema $targetSchema) { |
|
81 | - $this->noEmit = true; |
|
82 | - $this->applySchema($targetSchema); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - public function generateChangeScript(Schema $targetSchema) { |
|
90 | - $schemaDiff = $this->getDiff($targetSchema, $this->connection); |
|
91 | - |
|
92 | - $script = ''; |
|
93 | - $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform()); |
|
94 | - foreach ($sqls as $sql) { |
|
95 | - $script .= $this->convertStatementToScript($sql); |
|
96 | - } |
|
97 | - |
|
98 | - return $script; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param Schema $targetSchema |
|
103 | - * @throws \OC\DB\MigrationException |
|
104 | - */ |
|
105 | - public function checkMigrate(Schema $targetSchema) { |
|
106 | - $this->noEmit = true; |
|
107 | - /**@var \Doctrine\DBAL\Schema\Table[] $tables */ |
|
108 | - $tables = $targetSchema->getTables(); |
|
109 | - $filterExpression = $this->getFilterExpression(); |
|
110 | - $this->connection->getConfiguration()-> |
|
111 | - setFilterSchemaAssetsExpression($filterExpression); |
|
112 | - $existingTables = $this->connection->getSchemaManager()->listTableNames(); |
|
113 | - |
|
114 | - $step = 0; |
|
115 | - foreach ($tables as $table) { |
|
116 | - if (strpos($table->getName(), '.')) { |
|
117 | - list(, $tableName) = explode('.', $table->getName()); |
|
118 | - } else { |
|
119 | - $tableName = $table->getName(); |
|
120 | - } |
|
121 | - $this->emitCheckStep($tableName, $step++, count($tables)); |
|
122 | - // don't need to check for new tables |
|
123 | - if (array_search($tableName, $existingTables) !== false) { |
|
124 | - $this->checkTableMigrate($table); |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Create a unique name for the temporary table |
|
131 | - * |
|
132 | - * @param string $name |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - protected function generateTemporaryTableName($name) { |
|
136 | - return $this->config->getSystemValue('dbtableprefix', 'oc_') . $name . '_' . $this->random->generate(13, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Check the migration of a table on a copy so we can detect errors before messing with the real table |
|
141 | - * |
|
142 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
143 | - * @throws \OC\DB\MigrationException |
|
144 | - */ |
|
145 | - protected function checkTableMigrate(Table $table) { |
|
146 | - $name = $table->getName(); |
|
147 | - $tmpName = $this->generateTemporaryTableName($name); |
|
148 | - |
|
149 | - $this->copyTable($name, $tmpName); |
|
150 | - |
|
151 | - //create the migration schema for the temporary table |
|
152 | - $tmpTable = $this->renameTableSchema($table, $tmpName); |
|
153 | - $schemaConfig = new SchemaConfig(); |
|
154 | - $schemaConfig->setName($this->connection->getDatabase()); |
|
155 | - $schema = new Schema(array($tmpTable), array(), $schemaConfig); |
|
156 | - |
|
157 | - try { |
|
158 | - $this->applySchema($schema); |
|
159 | - $this->dropTable($tmpName); |
|
160 | - } catch (DBALException $e) { |
|
161 | - // pgsql needs to commit it's failed transaction before doing anything else |
|
162 | - if ($this->connection->isTransactionActive()) { |
|
163 | - $this->connection->commit(); |
|
164 | - } |
|
165 | - $this->dropTable($tmpName); |
|
166 | - throw new MigrationException($table->getName(), $e->getMessage()); |
|
167 | - } |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
172 | - * @param string $newName |
|
173 | - * @return \Doctrine\DBAL\Schema\Table |
|
174 | - */ |
|
175 | - protected function renameTableSchema(Table $table, $newName) { |
|
176 | - /** |
|
177 | - * @var \Doctrine\DBAL\Schema\Index[] $indexes |
|
178 | - */ |
|
179 | - $indexes = $table->getIndexes(); |
|
180 | - $newIndexes = array(); |
|
181 | - foreach ($indexes as $index) { |
|
182 | - if ($index->isPrimary()) { |
|
183 | - // do not rename primary key |
|
184 | - $indexName = $index->getName(); |
|
185 | - } else { |
|
186 | - // avoid conflicts in index names |
|
187 | - $indexName = $this->config->getSystemValue('dbtableprefix', 'oc_') . $this->random->generate(13, ISecureRandom::CHAR_LOWER); |
|
188 | - } |
|
189 | - $newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary()); |
|
190 | - } |
|
191 | - |
|
192 | - // foreign keys are not supported so we just set it to an empty array |
|
193 | - return new Table($newName, $table->getColumns(), $newIndexes, array(), 0, $table->getOptions()); |
|
194 | - } |
|
195 | - |
|
196 | - public function createSchema() { |
|
197 | - $filterExpression = $this->getFilterExpression(); |
|
198 | - $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
199 | - return $this->connection->getSchemaManager()->createSchema(); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @param Schema $targetSchema |
|
204 | - * @param \Doctrine\DBAL\Connection $connection |
|
205 | - * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
206 | - * @throws DBALException |
|
207 | - */ |
|
208 | - protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
209 | - // adjust varchar columns with a length higher then getVarcharMaxLength to clob |
|
210 | - foreach ($targetSchema->getTables() as $table) { |
|
211 | - foreach ($table->getColumns() as $column) { |
|
212 | - if ($column->getType() instanceof StringType) { |
|
213 | - if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) { |
|
214 | - $column->setType(Type::getType('text')); |
|
215 | - $column->setLength(null); |
|
216 | - } |
|
217 | - } |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - $filterExpression = $this->getFilterExpression(); |
|
222 | - $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
223 | - $sourceSchema = $connection->getSchemaManager()->createSchema(); |
|
224 | - |
|
225 | - // remove tables we don't know about |
|
226 | - /** @var $table \Doctrine\DBAL\Schema\Table */ |
|
227 | - foreach ($sourceSchema->getTables() as $table) { |
|
228 | - if (!$targetSchema->hasTable($table->getName())) { |
|
229 | - $sourceSchema->dropTable($table->getName()); |
|
230 | - } |
|
231 | - } |
|
232 | - // remove sequences we don't know about |
|
233 | - foreach ($sourceSchema->getSequences() as $table) { |
|
234 | - if (!$targetSchema->hasSequence($table->getName())) { |
|
235 | - $sourceSchema->dropSequence($table->getName()); |
|
236 | - } |
|
237 | - } |
|
238 | - |
|
239 | - $comparator = new Comparator(); |
|
240 | - return $comparator->compare($sourceSchema, $targetSchema); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
245 | - * @param \Doctrine\DBAL\Connection $connection |
|
246 | - */ |
|
247 | - protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { |
|
248 | - if (is_null($connection)) { |
|
249 | - $connection = $this->connection; |
|
250 | - } |
|
251 | - |
|
252 | - $schemaDiff = $this->getDiff($targetSchema, $connection); |
|
253 | - |
|
254 | - $connection->beginTransaction(); |
|
255 | - $sqls = $schemaDiff->toSql($connection->getDatabasePlatform()); |
|
256 | - $step = 0; |
|
257 | - foreach ($sqls as $sql) { |
|
258 | - $this->emit($sql, $step++, count($sqls)); |
|
259 | - $connection->query($sql); |
|
260 | - } |
|
261 | - $connection->commit(); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @param string $sourceName |
|
266 | - * @param string $targetName |
|
267 | - */ |
|
268 | - protected function copyTable($sourceName, $targetName) { |
|
269 | - $quotedSource = $this->connection->quoteIdentifier($sourceName); |
|
270 | - $quotedTarget = $this->connection->quoteIdentifier($targetName); |
|
271 | - |
|
272 | - $this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')'); |
|
273 | - $this->connection->exec('INSERT INTO ' . $quotedTarget . ' SELECT * FROM ' . $quotedSource); |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * @param string $name |
|
278 | - */ |
|
279 | - protected function dropTable($name) { |
|
280 | - $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($name)); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @param $statement |
|
285 | - * @return string |
|
286 | - */ |
|
287 | - protected function convertStatementToScript($statement) { |
|
288 | - $script = $statement . ';'; |
|
289 | - $script .= PHP_EOL; |
|
290 | - $script .= PHP_EOL; |
|
291 | - return $script; |
|
292 | - } |
|
293 | - |
|
294 | - protected function getFilterExpression() { |
|
295 | - return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
296 | - } |
|
297 | - |
|
298 | - protected function emit($sql, $step, $max) { |
|
299 | - if ($this->noEmit) { |
|
300 | - return; |
|
301 | - } |
|
302 | - if(is_null($this->dispatcher)) { |
|
303 | - return; |
|
304 | - } |
|
305 | - $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step+1, $max])); |
|
306 | - } |
|
307 | - |
|
308 | - private function emitCheckStep($tableName, $step, $max) { |
|
309 | - if(is_null($this->dispatcher)) { |
|
310 | - return; |
|
311 | - } |
|
312 | - $this->dispatcher->dispatch('\OC\DB\Migrator::checkTable', new GenericEvent($tableName, [$step+1, $max])); |
|
313 | - } |
|
46 | + /** @var \Doctrine\DBAL\Connection */ |
|
47 | + protected $connection; |
|
48 | + |
|
49 | + /** @var ISecureRandom */ |
|
50 | + private $random; |
|
51 | + |
|
52 | + /** @var IConfig */ |
|
53 | + protected $config; |
|
54 | + |
|
55 | + /** @var EventDispatcher */ |
|
56 | + private $dispatcher; |
|
57 | + |
|
58 | + /** @var bool */ |
|
59 | + private $noEmit = false; |
|
60 | + |
|
61 | + /** |
|
62 | + * @param \Doctrine\DBAL\Connection|Connection $connection |
|
63 | + * @param ISecureRandom $random |
|
64 | + * @param IConfig $config |
|
65 | + * @param EventDispatcher $dispatcher |
|
66 | + */ |
|
67 | + public function __construct(\Doctrine\DBAL\Connection $connection, |
|
68 | + ISecureRandom $random, |
|
69 | + IConfig $config, |
|
70 | + EventDispatcher $dispatcher = null) { |
|
71 | + $this->connection = $connection; |
|
72 | + $this->random = $random; |
|
73 | + $this->config = $config; |
|
74 | + $this->dispatcher = $dispatcher; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
79 | + */ |
|
80 | + public function migrate(Schema $targetSchema) { |
|
81 | + $this->noEmit = true; |
|
82 | + $this->applySchema($targetSchema); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + public function generateChangeScript(Schema $targetSchema) { |
|
90 | + $schemaDiff = $this->getDiff($targetSchema, $this->connection); |
|
91 | + |
|
92 | + $script = ''; |
|
93 | + $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform()); |
|
94 | + foreach ($sqls as $sql) { |
|
95 | + $script .= $this->convertStatementToScript($sql); |
|
96 | + } |
|
97 | + |
|
98 | + return $script; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param Schema $targetSchema |
|
103 | + * @throws \OC\DB\MigrationException |
|
104 | + */ |
|
105 | + public function checkMigrate(Schema $targetSchema) { |
|
106 | + $this->noEmit = true; |
|
107 | + /**@var \Doctrine\DBAL\Schema\Table[] $tables */ |
|
108 | + $tables = $targetSchema->getTables(); |
|
109 | + $filterExpression = $this->getFilterExpression(); |
|
110 | + $this->connection->getConfiguration()-> |
|
111 | + setFilterSchemaAssetsExpression($filterExpression); |
|
112 | + $existingTables = $this->connection->getSchemaManager()->listTableNames(); |
|
113 | + |
|
114 | + $step = 0; |
|
115 | + foreach ($tables as $table) { |
|
116 | + if (strpos($table->getName(), '.')) { |
|
117 | + list(, $tableName) = explode('.', $table->getName()); |
|
118 | + } else { |
|
119 | + $tableName = $table->getName(); |
|
120 | + } |
|
121 | + $this->emitCheckStep($tableName, $step++, count($tables)); |
|
122 | + // don't need to check for new tables |
|
123 | + if (array_search($tableName, $existingTables) !== false) { |
|
124 | + $this->checkTableMigrate($table); |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Create a unique name for the temporary table |
|
131 | + * |
|
132 | + * @param string $name |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + protected function generateTemporaryTableName($name) { |
|
136 | + return $this->config->getSystemValue('dbtableprefix', 'oc_') . $name . '_' . $this->random->generate(13, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Check the migration of a table on a copy so we can detect errors before messing with the real table |
|
141 | + * |
|
142 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
143 | + * @throws \OC\DB\MigrationException |
|
144 | + */ |
|
145 | + protected function checkTableMigrate(Table $table) { |
|
146 | + $name = $table->getName(); |
|
147 | + $tmpName = $this->generateTemporaryTableName($name); |
|
148 | + |
|
149 | + $this->copyTable($name, $tmpName); |
|
150 | + |
|
151 | + //create the migration schema for the temporary table |
|
152 | + $tmpTable = $this->renameTableSchema($table, $tmpName); |
|
153 | + $schemaConfig = new SchemaConfig(); |
|
154 | + $schemaConfig->setName($this->connection->getDatabase()); |
|
155 | + $schema = new Schema(array($tmpTable), array(), $schemaConfig); |
|
156 | + |
|
157 | + try { |
|
158 | + $this->applySchema($schema); |
|
159 | + $this->dropTable($tmpName); |
|
160 | + } catch (DBALException $e) { |
|
161 | + // pgsql needs to commit it's failed transaction before doing anything else |
|
162 | + if ($this->connection->isTransactionActive()) { |
|
163 | + $this->connection->commit(); |
|
164 | + } |
|
165 | + $this->dropTable($tmpName); |
|
166 | + throw new MigrationException($table->getName(), $e->getMessage()); |
|
167 | + } |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
172 | + * @param string $newName |
|
173 | + * @return \Doctrine\DBAL\Schema\Table |
|
174 | + */ |
|
175 | + protected function renameTableSchema(Table $table, $newName) { |
|
176 | + /** |
|
177 | + * @var \Doctrine\DBAL\Schema\Index[] $indexes |
|
178 | + */ |
|
179 | + $indexes = $table->getIndexes(); |
|
180 | + $newIndexes = array(); |
|
181 | + foreach ($indexes as $index) { |
|
182 | + if ($index->isPrimary()) { |
|
183 | + // do not rename primary key |
|
184 | + $indexName = $index->getName(); |
|
185 | + } else { |
|
186 | + // avoid conflicts in index names |
|
187 | + $indexName = $this->config->getSystemValue('dbtableprefix', 'oc_') . $this->random->generate(13, ISecureRandom::CHAR_LOWER); |
|
188 | + } |
|
189 | + $newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary()); |
|
190 | + } |
|
191 | + |
|
192 | + // foreign keys are not supported so we just set it to an empty array |
|
193 | + return new Table($newName, $table->getColumns(), $newIndexes, array(), 0, $table->getOptions()); |
|
194 | + } |
|
195 | + |
|
196 | + public function createSchema() { |
|
197 | + $filterExpression = $this->getFilterExpression(); |
|
198 | + $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
199 | + return $this->connection->getSchemaManager()->createSchema(); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @param Schema $targetSchema |
|
204 | + * @param \Doctrine\DBAL\Connection $connection |
|
205 | + * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
206 | + * @throws DBALException |
|
207 | + */ |
|
208 | + protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
209 | + // adjust varchar columns with a length higher then getVarcharMaxLength to clob |
|
210 | + foreach ($targetSchema->getTables() as $table) { |
|
211 | + foreach ($table->getColumns() as $column) { |
|
212 | + if ($column->getType() instanceof StringType) { |
|
213 | + if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) { |
|
214 | + $column->setType(Type::getType('text')); |
|
215 | + $column->setLength(null); |
|
216 | + } |
|
217 | + } |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + $filterExpression = $this->getFilterExpression(); |
|
222 | + $this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression); |
|
223 | + $sourceSchema = $connection->getSchemaManager()->createSchema(); |
|
224 | + |
|
225 | + // remove tables we don't know about |
|
226 | + /** @var $table \Doctrine\DBAL\Schema\Table */ |
|
227 | + foreach ($sourceSchema->getTables() as $table) { |
|
228 | + if (!$targetSchema->hasTable($table->getName())) { |
|
229 | + $sourceSchema->dropTable($table->getName()); |
|
230 | + } |
|
231 | + } |
|
232 | + // remove sequences we don't know about |
|
233 | + foreach ($sourceSchema->getSequences() as $table) { |
|
234 | + if (!$targetSchema->hasSequence($table->getName())) { |
|
235 | + $sourceSchema->dropSequence($table->getName()); |
|
236 | + } |
|
237 | + } |
|
238 | + |
|
239 | + $comparator = new Comparator(); |
|
240 | + return $comparator->compare($sourceSchema, $targetSchema); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
245 | + * @param \Doctrine\DBAL\Connection $connection |
|
246 | + */ |
|
247 | + protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { |
|
248 | + if (is_null($connection)) { |
|
249 | + $connection = $this->connection; |
|
250 | + } |
|
251 | + |
|
252 | + $schemaDiff = $this->getDiff($targetSchema, $connection); |
|
253 | + |
|
254 | + $connection->beginTransaction(); |
|
255 | + $sqls = $schemaDiff->toSql($connection->getDatabasePlatform()); |
|
256 | + $step = 0; |
|
257 | + foreach ($sqls as $sql) { |
|
258 | + $this->emit($sql, $step++, count($sqls)); |
|
259 | + $connection->query($sql); |
|
260 | + } |
|
261 | + $connection->commit(); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @param string $sourceName |
|
266 | + * @param string $targetName |
|
267 | + */ |
|
268 | + protected function copyTable($sourceName, $targetName) { |
|
269 | + $quotedSource = $this->connection->quoteIdentifier($sourceName); |
|
270 | + $quotedTarget = $this->connection->quoteIdentifier($targetName); |
|
271 | + |
|
272 | + $this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')'); |
|
273 | + $this->connection->exec('INSERT INTO ' . $quotedTarget . ' SELECT * FROM ' . $quotedSource); |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * @param string $name |
|
278 | + */ |
|
279 | + protected function dropTable($name) { |
|
280 | + $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($name)); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @param $statement |
|
285 | + * @return string |
|
286 | + */ |
|
287 | + protected function convertStatementToScript($statement) { |
|
288 | + $script = $statement . ';'; |
|
289 | + $script .= PHP_EOL; |
|
290 | + $script .= PHP_EOL; |
|
291 | + return $script; |
|
292 | + } |
|
293 | + |
|
294 | + protected function getFilterExpression() { |
|
295 | + return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
296 | + } |
|
297 | + |
|
298 | + protected function emit($sql, $step, $max) { |
|
299 | + if ($this->noEmit) { |
|
300 | + return; |
|
301 | + } |
|
302 | + if(is_null($this->dispatcher)) { |
|
303 | + return; |
|
304 | + } |
|
305 | + $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step+1, $max])); |
|
306 | + } |
|
307 | + |
|
308 | + private function emitCheckStep($tableName, $step, $max) { |
|
309 | + if(is_null($this->dispatcher)) { |
|
310 | + return; |
|
311 | + } |
|
312 | + $this->dispatcher->dispatch('\OC\DB\Migrator::checkTable', new GenericEvent($tableName, [$step+1, $max])); |
|
313 | + } |
|
314 | 314 | } |