Migration.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing;
  26. use Doctrine\DBAL\Connection;
  27. use OCP\ICache;
  28. use OCP\IDBConnection;
  29. use OC\Cache\CappedMemoryCache;
  30. /**
  31. * Class Migration
  32. *
  33. * @package OCA\Files_Sharing
  34. * @group DB
  35. */
  36. class Migration {
  37. /** @var IDBConnection */
  38. private $connection;
  39. /** @var ICache with all shares we already saw */
  40. private $shareCache;
  41. /** @var string */
  42. private $table = 'share';
  43. public function __construct(IDBConnection $connection) {
  44. $this->connection = $connection;
  45. // We cache up to 10k share items (~20MB)
  46. $this->shareCache = new CappedMemoryCache(10000);
  47. }
  48. /**
  49. * move all re-shares to the owner in order to have a flat list of shares
  50. * upgrade from oC 8.2 to 9.0 with the new sharing
  51. */
  52. public function removeReShares() {
  53. $stmt = $this->getReShares();
  54. $owners = [];
  55. while($share = $stmt->fetch()) {
  56. $this->shareCache[$share['id']] = $share;
  57. $owners[$share['id']] = [
  58. 'owner' => $this->findOwner($share),
  59. 'initiator' => $share['uid_owner'],
  60. 'type' => $share['share_type'],
  61. ];
  62. if (count($owners) === 1000) {
  63. $this->updateOwners($owners);
  64. $owners = [];
  65. }
  66. }
  67. $stmt->closeCursor();
  68. if (count($owners)) {
  69. $this->updateOwners($owners);
  70. }
  71. }
  72. /**
  73. * update all owner information so that all shares have an owner
  74. * and an initiator for the upgrade from oC 8.2 to 9.0 with the new sharing
  75. */
  76. public function updateInitiatorInfo() {
  77. while (true) {
  78. $shares = $this->getMissingInitiator(1000);
  79. if (empty($shares)) {
  80. break;
  81. }
  82. $owners = [];
  83. foreach ($shares as $share) {
  84. $owners[$share['id']] = [
  85. 'owner' => $share['uid_owner'],
  86. 'initiator' => $share['uid_owner'],
  87. 'type' => $share['share_type'],
  88. ];
  89. }
  90. $this->updateOwners($owners);
  91. }
  92. }
  93. /**
  94. * find the owner of a re-shared file/folder
  95. *
  96. * @param array $share
  97. * @return array
  98. */
  99. private function findOwner($share) {
  100. $currentShare = $share;
  101. while(!is_null($currentShare['parent'])) {
  102. if (isset($this->shareCache[$currentShare['parent']])) {
  103. $currentShare = $this->shareCache[$currentShare['parent']];
  104. } else {
  105. $currentShare = $this->getShare((int)$currentShare['parent']);
  106. $this->shareCache[$currentShare['id']] = $currentShare;
  107. }
  108. }
  109. return $currentShare['uid_owner'];
  110. }
  111. /**
  112. * Get $n re-shares from the database
  113. *
  114. * @param int $n The max number of shares to fetch
  115. * @return \Doctrine\DBAL\Driver\Statement
  116. */
  117. private function getReShares() {
  118. $query = $this->connection->getQueryBuilder();
  119. $query->select(['id', 'parent', 'uid_owner', 'share_type'])
  120. ->from($this->table)
  121. ->where($query->expr()->in(
  122. 'share_type',
  123. $query->createNamedParameter(
  124. [
  125. \OCP\Share::SHARE_TYPE_USER,
  126. \OCP\Share::SHARE_TYPE_GROUP,
  127. \OCP\Share::SHARE_TYPE_LINK,
  128. \OCP\Share::SHARE_TYPE_REMOTE,
  129. ],
  130. Connection::PARAM_INT_ARRAY
  131. )
  132. ))
  133. ->andWhere($query->expr()->in(
  134. 'item_type',
  135. $query->createNamedParameter(
  136. ['file', 'folder'],
  137. Connection::PARAM_STR_ARRAY
  138. )
  139. ))
  140. ->andWhere($query->expr()->isNotNull('parent'))
  141. ->orderBy('id', 'asc');
  142. return $query->execute();
  143. $shares = $result->fetchAll();
  144. $result->closeCursor();
  145. $ordered = [];
  146. foreach ($shares as $share) {
  147. $ordered[(int)$share['id']] = $share;
  148. }
  149. return $ordered;
  150. }
  151. /**
  152. * Get $n re-shares from the database
  153. *
  154. * @param int $n The max number of shares to fetch
  155. * @return array
  156. */
  157. private function getMissingInitiator($n = 1000) {
  158. $query = $this->connection->getQueryBuilder();
  159. $query->select(['id', 'uid_owner', 'share_type'])
  160. ->from($this->table)
  161. ->where($query->expr()->in(
  162. 'share_type',
  163. $query->createNamedParameter(
  164. [
  165. \OCP\Share::SHARE_TYPE_USER,
  166. \OCP\Share::SHARE_TYPE_GROUP,
  167. \OCP\Share::SHARE_TYPE_LINK,
  168. \OCP\Share::SHARE_TYPE_REMOTE,
  169. ],
  170. Connection::PARAM_INT_ARRAY
  171. )
  172. ))
  173. ->andWhere($query->expr()->in(
  174. 'item_type',
  175. $query->createNamedParameter(
  176. ['file', 'folder'],
  177. Connection::PARAM_STR_ARRAY
  178. )
  179. ))
  180. ->andWhere($query->expr()->isNull('uid_initiator'))
  181. ->orderBy('id', 'asc')
  182. ->setMaxResults($n);
  183. $result = $query->execute();
  184. $shares = $result->fetchAll();
  185. $result->closeCursor();
  186. $ordered = [];
  187. foreach ($shares as $share) {
  188. $ordered[(int)$share['id']] = $share;
  189. }
  190. return $ordered;
  191. }
  192. /**
  193. * get a specific share
  194. *
  195. * @param int $id
  196. * @return array
  197. */
  198. private function getShare($id) {
  199. $query = $this->connection->getQueryBuilder();
  200. $query->select(['id', 'parent', 'uid_owner'])
  201. ->from($this->table)
  202. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  203. $result = $query->execute();
  204. $share = $result->fetchAll();
  205. $result->closeCursor();
  206. return $share[0];
  207. }
  208. /**
  209. * update database with the new owners
  210. *
  211. * @param array $owners
  212. * @throws \Exception
  213. */
  214. private function updateOwners($owners) {
  215. $this->connection->beginTransaction();
  216. try {
  217. foreach ($owners as $id => $owner) {
  218. $query = $this->connection->getQueryBuilder();
  219. $query->update($this->table)
  220. ->set('uid_owner', $query->createNamedParameter($owner['owner']))
  221. ->set('uid_initiator', $query->createNamedParameter($owner['initiator']));
  222. if ((int)$owner['type'] !== \OCP\Share::SHARE_TYPE_LINK) {
  223. $query->set('parent', $query->createNamedParameter(null));
  224. }
  225. $query->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  226. $query->execute();
  227. }
  228. $this->connection->commit();
  229. } catch (\Exception $e) {
  230. $this->connection->rollBack();
  231. throw $e;
  232. }
  233. }
  234. }