diff options
author | Björn Schießle <bjoern@schiessle.org> | 2016-01-12 16:28:47 +0100 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2016-01-28 19:24:23 +0100 |
commit | dc8d43575f71ee19e8c4ad90c3efd8bf9192d3fe (patch) | |
tree | 7a4d23ef44336436cd399467abb92a080cb35e65 /apps/files_sharing/lib | |
parent | 8b3d7d09d52ba169953d6a7d03ab570eb3ceed7a (diff) | |
download | nextcloud-server-dc8d43575f71ee19e8c4ad90c3efd8bf9192d3fe.tar.gz nextcloud-server-dc8d43575f71ee19e8c4ad90c3efd8bf9192d3fe.zip |
upgrade to sharing 2.0, remove hierarchical re-shares
Diffstat (limited to 'apps/files_sharing/lib')
-rw-r--r-- | apps/files_sharing/lib/migration.php | 153 |
1 files changed, 143 insertions, 10 deletions
diff --git a/apps/files_sharing/lib/migration.php b/apps/files_sharing/lib/migration.php index 0c5f46a5b3f..91e0c4c0996 100644 --- a/apps/files_sharing/lib/migration.php +++ b/apps/files_sharing/lib/migration.php @@ -1,7 +1,6 @@ <?php /** * @author Björn Schießle <schiessle@owncloud.com> - * @author Morris Jobke <hey@morrisjobke.de> * * @copyright Copyright (c) 2016, ownCloud, Inc. * @license AGPL-3.0 @@ -20,22 +19,156 @@ * */ - namespace OCA\Files_Sharing; +namespace OCA\Files_Sharing; +use Doctrine\DBAL\Connection; +use OCP\IDBConnection; + +/** + * Class Migration + * + * @package OCA\Files_Sharing + * @group DB + */ class Migration { + /** @var IDBConnection */ + private $connection; + + /** @var array with all shares we already saw */ + private $shareCache; + + /** @var string */ + private $table = 'share'; + + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + /** + * move all re-shares to the owner in order to have a flat list of shares + * upgrade from oC 8.2 to 9.0 with the new sharing + */ + public function removeReShares() { + $reShares = $this->getAllReShares(); + $this->shareCache = $reShares; + $owners = []; + foreach ($reShares as $share) { + $owners[$share['id']] = [ + 'owner' => $this->findOwner($share), + 'initiator' => $share['uid_owner'] + ]; + } + + $this->updateOwners($owners); + } + + /** + * find the owner of a re-shared file/folder + * + * @param array $share + * @return array + */ + private function findOwner($share) { + $currentShare = $share; + while(!is_null($currentShare['parent'])) { + if (isset($this->shareCache[$currentShare['parent']])) { + $currentShare = $this->shareCache[$currentShare['parent']]; + } else { + $currentShare = $this->getShare((int)$currentShare['parent']); + $this->shareCache[$currentShare['id']] = $currentShare; + } + } + + return $currentShare['uid_owner']; + } /** - * set accepted to 1 for all external shares. At this point in time we only - * have shares from the first version of server-to-server sharing so all should - * be accepted + * get all re-shares from the database + * + * @return array */ - public function addAcceptRow() { - $statement = 'UPDATE `*PREFIX*share_external` SET `accepted` = 1'; - $connection = \OC::$server->getDatabaseConnection(); - $query = $connection->prepare($statement); - $query->execute(); + private function getAllReShares() { + $query = $this->connection->getQueryBuilder(); + $query->select(['id', 'parent', 'uid_owner']) + ->from($this->table) + ->where($query->expr()->in( + 'share_type', + $query->createNamedParameter( + [ + \OCP\Share::SHARE_TYPE_USER, + \OCP\Share::SHARE_TYPE_GROUP, + \OCP\Share::SHARE_TYPE_LINK + ], + Connection::PARAM_INT_ARRAY + ) + )) + ->andWhere($query->expr()->in( + 'item_type', + $query->createNamedParameter( + ['file', 'folder'], + Connection::PARAM_STR_ARRAY + ) + )) + ->andWhere($query->expr()->isNotNull('parent')); + $result = $query->execute(); + $shares = $result->fetchAll(); + $result->closeCursor(); + + $ordered = []; + foreach ($shares as $share) { + $ordered[(int)$share['id']] = $share; + } + + return $ordered; } + /** + * get a specific share + * + * @param int $id + * @return array + */ + private function getShare($id) { + $query = $this->connection->getQueryBuilder(); + $query->select(['id', 'parent', 'uid_owner']) + ->from($this->table) + ->where($query->expr()->eq('id', $query->createNamedParameter($id))); + $result = $query->execute(); + $share = $result->fetchAll(); + $result->closeCursor(); + + return $share[0]; + } + + /** + * update database with the new owners + * + * @param array $owners + * @throws \Exception + */ + private function updateOwners($owners) { + + $this->connection->beginTransaction(); + + try { + + foreach ($owners as $id => $owner) { + $query = $this->connection->getQueryBuilder(); + $query->update($this->table) + ->set('parent', $query->createNamedParameter(null)) + ->set('uid_owner', $query->createNamedParameter($owner['owner'])) + ->set('uid_initiator', $query->createNamedParameter($owner['initiator'])) + ->where($query->expr()->eq('id', $query->createNamedParameter($id)))->execute(); + } + + $this->connection->commit(); + + } catch (\Exception $e) { + $this->connection->rollBack(); + throw $e; + } + + } } |