diff options
Diffstat (limited to 'apps/files_versions/lib/Db')
-rw-r--r-- | apps/files_versions/lib/Db/VersionEntity.php | 45 | ||||
-rw-r--r-- | apps/files_versions/lib/Db/VersionsMapper.php | 82 |
2 files changed, 68 insertions, 59 deletions
diff --git a/apps/files_versions/lib/Db/VersionEntity.php b/apps/files_versions/lib/Db/VersionEntity.php index d5adbcfa104..10f1dc8cbba 100644 --- a/apps/files_versions/lib/Db/VersionEntity.php +++ b/apps/files_versions/lib/Db/VersionEntity.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me> - * - * @author Louis Chmn <louis@chmn.me> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Versions\Db; @@ -36,8 +19,8 @@ use OCP\DB\Types; * @method void setFileId(int $fileId) * @method int getTimestamp() * @method void setTimestamp(int $timestamp) - * @method int getSize() - * @method void setSize(int $size) + * @method int|float getSize() + * @method void setSize(int|float $size) * @method int getMimetype() * @method void setMimetype(int $mimetype) * @method array|null getMetadata() @@ -70,12 +53,22 @@ class VersionEntity extends Entity implements JsonSerializable { ]; } - public function getLabel(): string { - return $this->metadata['label'] ?? ''; + /** + * @abstract given a key, return the value associated with the key in the metadata column + * if nothing is found, we return an empty string + * @param string $key key associated with the value + */ + public function getMetadataValue(string $key): ?string { + return $this->metadata[$key] ?? null; } - public function setLabel(string $label): void { - $this->metadata['label'] = $label; + /** + * @abstract sets a key value pair in the metadata column + * @param string $key key associated with the value + * @param string $value value associated with the key + */ + public function setMetadataValue(string $key, string $value): void { + $this->metadata[$key] = $value; $this->markFieldUpdated('metadata'); } -}
\ No newline at end of file +} diff --git a/apps/files_versions/lib/Db/VersionsMapper.php b/apps/files_versions/lib/Db/VersionsMapper.php index 86a0be82668..318dd8f0d82 100644 --- a/apps/files_versions/lib/Db/VersionsMapper.php +++ b/apps/files_versions/lib/Db/VersionsMapper.php @@ -3,33 +3,15 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me> - * - * @author Louis Chmn <louis@chmn.me> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Versions\Db; -use OCA\Files_Versions\Db\VersionEntity; -use OCP\IDBConnection; use OCP\AppFramework\Db\QBMapper; -use OCP\DB\IResult; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; /** * @extends QBMapper<VersionEntity> @@ -46,8 +28,8 @@ class VersionsMapper extends QBMapper { $qb = $this->db->getQueryBuilder(); $qb->select('*') - ->from($this->getTableName()) - ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId))); + ->from($this->getTableName()) + ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId))); return $this->findEntities($qb); } @@ -59,10 +41,10 @@ class VersionsMapper extends QBMapper { $qb = $this->db->getQueryBuilder(); $qb->select('*') - ->from($this->getTableName()) - ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId))) - ->orderBy('timestamp', 'DESC') - ->setMaxResults(1); + ->from($this->getTableName()) + ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId))) + ->orderBy('timestamp', 'DESC') + ->setMaxResults(1); return $this->findEntity($qb); } @@ -71,9 +53,9 @@ class VersionsMapper extends QBMapper { $qb = $this->db->getQueryBuilder(); $qb->select('*') - ->from($this->getTableName()) - ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId))) - ->andWhere($qb->expr()->eq('timestamp', $qb->createNamedParameter($timestamp))); + ->from($this->getTableName()) + ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId))) + ->andWhere($qb->expr()->eq('timestamp', $qb->createNamedParameter($timestamp))); return $this->findEntity($qb); } @@ -82,7 +64,41 @@ class VersionsMapper extends QBMapper { $qb = $this->db->getQueryBuilder(); return $qb->delete($this->getTableName()) - ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId))) - ->executeStatement(); + ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId))) + ->executeStatement(); + } + + public function deleteAllVersionsForUser(int $storageId, ?string $path = null): void { + $fileIdsGenerator = $this->getFileIdsGenerator($storageId, $path); + + $versionEntitiesDeleteQuery = $this->db->getQueryBuilder(); + $versionEntitiesDeleteQuery->delete($this->getTableName()) + ->where($versionEntitiesDeleteQuery->expr()->in('file_id', $versionEntitiesDeleteQuery->createParameter('file_ids'))); + + foreach ($fileIdsGenerator as $fileIds) { + $versionEntitiesDeleteQuery->setParameter('file_ids', $fileIds, IQueryBuilder::PARAM_INT_ARRAY); + $versionEntitiesDeleteQuery->executeStatement(); + } + } + + private function getFileIdsGenerator(int $storageId, ?string $path): \Generator { + $offset = 0; + do { + $filesIdsSelect = $this->db->getQueryBuilder(); + $filesIdsSelect->select('fileid') + ->from('filecache') + ->where($filesIdsSelect->expr()->eq('storage', $filesIdsSelect->createNamedParameter($storageId, IQueryBuilder::PARAM_STR))) + ->andWhere($filesIdsSelect->expr()->like('path', $filesIdsSelect->createNamedParameter('files' . ($path ? '/' . $this->db->escapeLikeParameter($path) : '') . '/%', IQueryBuilder::PARAM_STR))) + ->andWhere($filesIdsSelect->expr()->gt('fileid', $filesIdsSelect->createParameter('offset'))) + ->setMaxResults(1000) + ->orderBy('fileid', 'ASC'); + + $filesIdsSelect->setParameter('offset', $offset, IQueryBuilder::PARAM_INT); + $result = $filesIdsSelect->executeQuery(); + $fileIds = $result->fetchAll(\PDO::FETCH_COLUMN); + $offset = end($fileIds); + + yield $fileIds; + } while (!empty($fileIds)); } } |