|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OC\Repair; |
| 10 | + |
| 11 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 12 | +use OCP\IDBConnection; |
| 13 | +use OCP\Migration\IOutput; |
| 14 | +use OCP\Migration\IRepairStep; |
| 15 | + |
| 16 | +class RemoveBrokenProperties implements IRepairStep { |
| 17 | + /** |
| 18 | + * RemoveBrokenProperties constructor. |
| 19 | + * |
| 20 | + * @param IDBConnection $db |
| 21 | + */ |
| 22 | + public function __construct( |
| 23 | + private IDBConnection $db, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @inheritdoc |
| 29 | + */ |
| 30 | + public function getName() { |
| 31 | + return 'Remove broken DAV object properties'; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @inheritdoc |
| 36 | + */ |
| 37 | + public function run(IOutput $output) { |
| 38 | + // retrieve all object properties |
| 39 | + $qb = $this->db->getQueryBuilder(); |
| 40 | + $qb->select('id', 'propertyvalue') |
| 41 | + ->from('properties') |
| 42 | + ->where($qb->expr()->eq('valuetype', $qb->createNamedParameter('3', IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
| 43 | + $result = $qb->executeQuery(); |
| 44 | + // find broken object properties |
| 45 | + $brokenIds = []; |
| 46 | + while ($entry = $result->fetch()) { |
| 47 | + if (!empty($entry['propertyvalue'])) { |
| 48 | + $object = @unserialize(str_replace('\x00', chr(0), $entry['propertyvalue'])); |
| 49 | + if ($object === false) { |
| 50 | + $brokenIds[] = $entry['id']; |
| 51 | + } |
| 52 | + } else { |
| 53 | + $brokenIds[] = $entry['id']; |
| 54 | + } |
| 55 | + } |
| 56 | + $result->closeCursor(); |
| 57 | + // delete broken object properties |
| 58 | + $qb = $this->db->getQueryBuilder(); |
| 59 | + $qb->delete('properties') |
| 60 | + ->where($qb->expr()->in('id', $qb->createParameter('ids'), IQueryBuilder::PARAM_STR_ARRAY)); |
| 61 | + foreach (array_chunk($brokenIds, 1000) as $chunkIds) { |
| 62 | + $qb->setParameter('ids', $chunkIds, IQueryBuilder::PARAM_STR_ARRAY); |
| 63 | + $qb->executeStatement(); |
| 64 | + } |
| 65 | + $total = count($brokenIds); |
| 66 | + $output->info("$total broken object properties removed"); |
| 67 | + } |
| 68 | +} |
0 commit comments