2015-01-12 19:04:19 +03:00
|
|
|
<?php
|
2015-03-26 13:44:34 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-02-26 13:37:37 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2015-01-12 19:04:19 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2015-01-12 19:04:19 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-01-12 19:04:19 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2015-01-12 19:04:19 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-12 19:04:19 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2016-01-12 18:28:47 +03:00
|
|
|
namespace OCA\Files_Sharing;
|
2015-01-12 19:04:19 +03:00
|
|
|
|
2016-01-12 18:28:47 +03:00
|
|
|
use Doctrine\DBAL\Connection;
|
2016-07-06 00:18:41 +03:00
|
|
|
use OCP\ICache;
|
2016-01-12 18:28:47 +03:00
|
|
|
use OCP\IDBConnection;
|
2016-01-25 19:57:41 +03:00
|
|
|
use OC\Cache\CappedMemoryCache;
|
2016-01-12 18:28:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Migration
|
|
|
|
*
|
|
|
|
* @package OCA\Files_Sharing
|
|
|
|
* @group DB
|
|
|
|
*/
|
2015-01-12 19:04:19 +03:00
|
|
|
class Migration {
|
|
|
|
|
2016-01-12 18:28:47 +03:00
|
|
|
/** @var IDBConnection */
|
|
|
|
private $connection;
|
|
|
|
|
2016-07-06 00:18:41 +03:00
|
|
|
/** @var ICache with all shares we already saw */
|
2016-01-12 18:28:47 +03:00
|
|
|
private $shareCache;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $table = 'share';
|
|
|
|
|
|
|
|
public function __construct(IDBConnection $connection) {
|
|
|
|
$this->connection = $connection;
|
2016-01-25 19:57:41 +03:00
|
|
|
|
|
|
|
// We cache up to 10k share items (~20MB)
|
|
|
|
$this->shareCache = new CappedMemoryCache(10000);
|
2016-01-12 18:28:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
|
2016-02-11 15:28:15 +03:00
|
|
|
$stmt = $this->getReShares();
|
2016-01-25 19:57:41 +03:00
|
|
|
|
2016-02-11 15:28:15 +03:00
|
|
|
$owners = [];
|
|
|
|
while($share = $stmt->fetch()) {
|
2016-01-25 19:57:41 +03:00
|
|
|
|
2016-02-11 15:28:15 +03:00
|
|
|
$this->shareCache[$share['id']] = $share;
|
2016-01-25 19:57:41 +03:00
|
|
|
|
2016-02-11 15:28:15 +03:00
|
|
|
$owners[$share['id']] = [
|
2016-01-25 19:57:41 +03:00
|
|
|
'owner' => $this->findOwner($share),
|
2016-02-11 15:28:15 +03:00
|
|
|
'initiator' => $share['uid_owner'],
|
|
|
|
'type' => $share['share_type'],
|
|
|
|
];
|
2016-01-25 19:57:41 +03:00
|
|
|
|
2016-02-11 15:28:15 +03:00
|
|
|
if (count($owners) === 1000) {
|
|
|
|
$this->updateOwners($owners);
|
|
|
|
$owners = [];
|
2016-01-25 19:57:41 +03:00
|
|
|
}
|
|
|
|
}
|
2016-02-11 15:28:15 +03:00
|
|
|
|
|
|
|
$stmt->closeCursor();
|
|
|
|
|
|
|
|
if (count($owners)) {
|
|
|
|
$this->updateOwners($owners);
|
|
|
|
}
|
2016-01-12 18:28:47 +03:00
|
|
|
}
|
|
|
|
|
2016-01-25 20:06:34 +03:00
|
|
|
/**
|
|
|
|
* update all owner information so that all shares have an owner
|
|
|
|
* and an initiator for the upgrade from oC 8.2 to 9.0 with the new sharing
|
|
|
|
*/
|
|
|
|
public function updateInitiatorInfo() {
|
|
|
|
while (true) {
|
|
|
|
$shares = $this->getMissingInitiator(1000);
|
|
|
|
|
|
|
|
if (empty($shares)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$owners = [];
|
|
|
|
foreach ($shares as $share) {
|
|
|
|
$owners[$share['id']] = [
|
|
|
|
'owner' => $share['uid_owner'],
|
2016-02-11 15:28:15 +03:00
|
|
|
'initiator' => $share['uid_owner'],
|
|
|
|
'type' => $share['share_type'],
|
2016-01-25 20:06:34 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
$this->updateOwners($owners);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:28:47 +03:00
|
|
|
/**
|
|
|
|
* 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'];
|
|
|
|
}
|
2015-01-12 19:04:19 +03:00
|
|
|
|
|
|
|
/**
|
2016-01-25 19:57:41 +03:00
|
|
|
* Get $n re-shares from the database
|
2016-01-12 18:28:47 +03:00
|
|
|
*
|
2016-01-25 19:57:41 +03:00
|
|
|
* @param int $n The max number of shares to fetch
|
2016-02-11 15:28:15 +03:00
|
|
|
* @return \Doctrine\DBAL\Driver\Statement
|
2015-01-12 19:04:19 +03:00
|
|
|
*/
|
2016-02-11 15:28:15 +03:00
|
|
|
private function getReShares() {
|
2016-01-12 18:28:47 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2016-02-11 15:28:15 +03:00
|
|
|
$query->select(['id', 'parent', 'uid_owner', 'share_type'])
|
2016-01-12 18:28:47 +03:00
|
|
|
->from($this->table)
|
|
|
|
->where($query->expr()->in(
|
|
|
|
'share_type',
|
|
|
|
$query->createNamedParameter(
|
|
|
|
[
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
2016-02-08 17:11:48 +03:00
|
|
|
\OCP\Share::SHARE_TYPE_LINK,
|
|
|
|
\OCP\Share::SHARE_TYPE_REMOTE,
|
2016-01-12 18:28:47 +03:00
|
|
|
],
|
|
|
|
Connection::PARAM_INT_ARRAY
|
|
|
|
)
|
|
|
|
))
|
|
|
|
->andWhere($query->expr()->in(
|
|
|
|
'item_type',
|
|
|
|
$query->createNamedParameter(
|
|
|
|
['file', 'folder'],
|
|
|
|
Connection::PARAM_STR_ARRAY
|
|
|
|
)
|
|
|
|
))
|
2016-01-25 19:57:41 +03:00
|
|
|
->andWhere($query->expr()->isNotNull('parent'))
|
2016-02-11 15:28:15 +03:00
|
|
|
->orderBy('id', 'asc');
|
|
|
|
return $query->execute();
|
|
|
|
|
|
|
|
|
2016-01-12 18:28:47 +03:00
|
|
|
$shares = $result->fetchAll();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
$ordered = [];
|
|
|
|
foreach ($shares as $share) {
|
|
|
|
$ordered[(int)$share['id']] = $share;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ordered;
|
2015-01-12 19:04:19 +03:00
|
|
|
}
|
|
|
|
|
2016-01-25 20:06:34 +03:00
|
|
|
/**
|
|
|
|
* Get $n re-shares from the database
|
|
|
|
*
|
|
|
|
* @param int $n The max number of shares to fetch
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getMissingInitiator($n = 1000) {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2016-02-11 15:28:15 +03:00
|
|
|
$query->select(['id', 'uid_owner', 'share_type'])
|
2016-01-25 20:06:34 +03:00
|
|
|
->from($this->table)
|
|
|
|
->where($query->expr()->in(
|
|
|
|
'share_type',
|
|
|
|
$query->createNamedParameter(
|
|
|
|
[
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
2016-02-08 17:11:48 +03:00
|
|
|
\OCP\Share::SHARE_TYPE_LINK,
|
|
|
|
\OCP\Share::SHARE_TYPE_REMOTE,
|
2016-01-25 20:06:34 +03:00
|
|
|
],
|
|
|
|
Connection::PARAM_INT_ARRAY
|
|
|
|
)
|
|
|
|
))
|
|
|
|
->andWhere($query->expr()->in(
|
|
|
|
'item_type',
|
|
|
|
$query->createNamedParameter(
|
|
|
|
['file', 'folder'],
|
|
|
|
Connection::PARAM_STR_ARRAY
|
|
|
|
)
|
|
|
|
))
|
|
|
|
->andWhere($query->expr()->isNull('uid_initiator'))
|
|
|
|
->orderBy('id', 'asc')
|
|
|
|
->setMaxResults($n);
|
|
|
|
$result = $query->execute();
|
|
|
|
$shares = $result->fetchAll();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
$ordered = [];
|
|
|
|
foreach ($shares as $share) {
|
|
|
|
$ordered[(int)$share['id']] = $share;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ordered;
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:28:47 +03:00
|
|
|
/**
|
|
|
|
* 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('uid_owner', $query->createNamedParameter($owner['owner']))
|
2016-02-11 15:28:15 +03:00
|
|
|
->set('uid_initiator', $query->createNamedParameter($owner['initiator']));
|
|
|
|
|
|
|
|
|
|
|
|
if ((int)$owner['type'] !== \OCP\Share::SHARE_TYPE_LINK) {
|
|
|
|
$query->set('parent', $query->createNamedParameter(null));
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->where($query->expr()->eq('id', $query->createNamedParameter($id)));
|
|
|
|
|
|
|
|
$query->execute();
|
2016-01-12 18:28:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->connection->commit();
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->connection->rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-01-12 19:04:19 +03:00
|
|
|
|
|
|
|
}
|