2015-01-14 13:57:26 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2015-01-14 13:57:26 +03:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2015-01-14 13:57:26 +03:00
|
|
|
namespace OC\Repair;
|
|
|
|
|
2016-01-25 19:17:36 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2015-07-06 13:34:19 +03:00
|
|
|
use OCP\IDBConnection;
|
2016-06-14 13:30:22 +03:00
|
|
|
use OCP\IUserManager;
|
2016-04-22 16:35:39 +03:00
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
use OCP\Migration\IRepairStep;
|
2015-01-14 13:57:26 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class RepairConfig
|
|
|
|
*
|
|
|
|
* @package OC\Repair
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
class CleanTags implements IRepairStep {
|
2015-01-14 13:57:26 +03:00
|
|
|
|
2015-07-06 13:34:19 +03:00
|
|
|
/** @var IDBConnection */
|
2015-01-14 13:57:26 +03:00
|
|
|
protected $connection;
|
|
|
|
|
2016-06-14 13:30:22 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
|
|
|
|
|
|
|
protected $deletedTags = 0;
|
|
|
|
|
2015-01-14 13:57:26 +03:00
|
|
|
/**
|
2015-07-06 13:34:19 +03:00
|
|
|
* @param IDBConnection $connection
|
2016-06-14 13:30:22 +03:00
|
|
|
* @param IUserManager $userManager
|
2015-01-14 13:57:26 +03:00
|
|
|
*/
|
2016-06-14 13:30:22 +03:00
|
|
|
public function __construct(IDBConnection $connection, IUserManager $userManager) {
|
2015-01-14 13:57:26 +03:00
|
|
|
$this->connection = $connection;
|
2016-06-14 13:30:22 +03:00
|
|
|
$this->userManager = $userManager;
|
2015-01-14 13:57:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return 'Clean tags and favorites';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the configuration after running an update
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
public function run(IOutput $output) {
|
2016-06-14 13:30:22 +03:00
|
|
|
$this->deleteOrphanTags($output);
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->deleteOrphanFileEntries($output);
|
|
|
|
$this->deleteOrphanTagEntries($output);
|
|
|
|
$this->deleteOrphanCategoryEntries($output);
|
2015-01-15 12:25:54 +03:00
|
|
|
}
|
2015-01-14 13:57:26 +03:00
|
|
|
|
2016-06-14 13:30:22 +03:00
|
|
|
/**
|
|
|
|
* Delete tags for deleted users
|
|
|
|
*/
|
|
|
|
protected function deleteOrphanTags(IOutput $output) {
|
|
|
|
$offset = 0;
|
|
|
|
while ($this->checkTags($offset)) {
|
|
|
|
$offset += 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output->info(sprintf('%d tags of deleted users have been removed.', $this->deletedTags));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function checkTags($offset) {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('uid')
|
|
|
|
->from('vcategory')
|
|
|
|
->groupBy('uid')
|
|
|
|
->orderBy('uid')
|
|
|
|
->setMaxResults(50)
|
|
|
|
->setFirstResult($offset);
|
|
|
|
$result = $query->execute();
|
|
|
|
|
|
|
|
$users = [];
|
|
|
|
$hadResults = false;
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$hadResults = true;
|
|
|
|
if (!$this->userManager->userExists($row['uid'])) {
|
|
|
|
$users[] = $row['uid'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if (!$hadResults) {
|
|
|
|
// No more tags, stop looping
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($users)) {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete('vcategory')
|
|
|
|
->where($query->expr()->in('uid', $query->createNamedParameter($users, IQueryBuilder::PARAM_STR_ARRAY)));
|
|
|
|
$this->deletedTags += $query->execute();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-15 12:25:54 +03:00
|
|
|
/**
|
|
|
|
* Delete tag entries for deleted files
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
protected function deleteOrphanFileEntries(IOutput $output) {
|
2015-01-14 13:57:26 +03:00
|
|
|
$this->deleteOrphanEntries(
|
2016-04-22 16:35:39 +03:00
|
|
|
$output,
|
2015-01-14 13:57:26 +03:00
|
|
|
'%d tags for delete files have been removed.',
|
2015-08-10 17:21:41 +03:00
|
|
|
'vcategory_to_object', 'objid',
|
|
|
|
'filecache', 'fileid', 'path_hash'
|
2015-01-14 13:57:26 +03:00
|
|
|
);
|
2015-01-15 12:25:54 +03:00
|
|
|
}
|
2015-01-14 13:57:26 +03:00
|
|
|
|
2015-01-15 12:25:54 +03:00
|
|
|
/**
|
|
|
|
* Delete tag entries for deleted tags
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
protected function deleteOrphanTagEntries(IOutput $output) {
|
2015-01-14 13:57:26 +03:00
|
|
|
$this->deleteOrphanEntries(
|
2016-04-22 16:35:39 +03:00
|
|
|
$output,
|
2015-01-14 13:57:26 +03:00
|
|
|
'%d tag entries for deleted tags have been removed.',
|
2015-08-10 17:21:41 +03:00
|
|
|
'vcategory_to_object', 'categoryid',
|
|
|
|
'vcategory', 'id', 'uid'
|
2015-01-14 13:57:26 +03:00
|
|
|
);
|
2015-01-15 12:25:54 +03:00
|
|
|
}
|
2015-01-14 13:57:26 +03:00
|
|
|
|
2015-01-15 12:25:54 +03:00
|
|
|
/**
|
|
|
|
* Delete tags that have no entries
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
protected function deleteOrphanCategoryEntries(IOutput $output) {
|
2015-01-14 13:57:26 +03:00
|
|
|
$this->deleteOrphanEntries(
|
2016-04-22 16:35:39 +03:00
|
|
|
$output,
|
2015-01-14 13:57:26 +03:00
|
|
|
'%d tags with no entries have been removed.',
|
2015-08-10 17:21:41 +03:00
|
|
|
'vcategory', 'id',
|
|
|
|
'vcategory_to_object', 'categoryid', 'type'
|
2015-01-14 13:57:26 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes all entries from $deleteTable that do not have a matching entry in $sourceTable
|
|
|
|
*
|
|
|
|
* A query joins $deleteTable.$deleteId = $sourceTable.$sourceId and checks
|
|
|
|
* whether $sourceNullColumn is null. If it is null, the entry in $deleteTable
|
|
|
|
* is being deleted.
|
|
|
|
*
|
|
|
|
* @param string $repairInfo
|
|
|
|
* @param string $deleteTable
|
|
|
|
* @param string $deleteId
|
|
|
|
* @param string $sourceTable
|
|
|
|
* @param string $sourceId
|
|
|
|
* @param string $sourceNullColumn If this column is null in the source table,
|
|
|
|
* the entry is deleted in the $deleteTable
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
protected function deleteOrphanEntries(IOutput $output, $repairInfo, $deleteTable, $deleteId, $sourceTable, $sourceId, $sourceNullColumn) {
|
2015-07-06 13:34:19 +03:00
|
|
|
$qb = $this->connection->getQueryBuilder();
|
2015-01-14 13:57:26 +03:00
|
|
|
|
2015-07-07 23:08:21 +03:00
|
|
|
$qb->select('d.' . $deleteId)
|
|
|
|
->from($deleteTable, 'd')
|
2017-03-30 13:38:48 +03:00
|
|
|
->leftJoin('d', $sourceTable, 's', $qb->expr()->eq('d.' . $deleteId, 's.' . $sourceId))
|
2015-01-14 13:57:26 +03:00
|
|
|
->where(
|
2015-07-07 23:08:21 +03:00
|
|
|
$qb->expr()->eq('d.type', $qb->expr()->literal('files'))
|
2015-01-14 13:57:26 +03:00
|
|
|
)
|
|
|
|
->andWhere(
|
2015-07-07 23:08:21 +03:00
|
|
|
$qb->expr()->isNull('s.' . $sourceNullColumn)
|
2015-01-14 13:57:26 +03:00
|
|
|
);
|
|
|
|
$result = $qb->execute();
|
|
|
|
|
|
|
|
$orphanItems = array();
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$orphanItems[] = (int) $row[$deleteId];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($orphanItems)) {
|
|
|
|
$orphanItemsBatch = array_chunk($orphanItems, 200);
|
|
|
|
foreach ($orphanItemsBatch as $items) {
|
2015-07-07 23:08:21 +03:00
|
|
|
$qb->delete($deleteTable)
|
2015-01-15 12:25:54 +03:00
|
|
|
->where(
|
2015-07-07 23:08:21 +03:00
|
|
|
$qb->expr()->eq('type', $qb->expr()->literal('files'))
|
2015-01-15 12:25:54 +03:00
|
|
|
)
|
2015-07-07 23:08:21 +03:00
|
|
|
->andWhere($qb->expr()->in($deleteId, $qb->createParameter('ids')));
|
2016-01-25 19:17:36 +03:00
|
|
|
$qb->setParameter('ids', $items, IQueryBuilder::PARAM_INT_ARRAY);
|
2015-01-14 13:57:26 +03:00
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($repairInfo) {
|
2016-04-22 16:35:39 +03:00
|
|
|
$output->info(sprintf($repairInfo, sizeof($orphanItems)));
|
2015-01-14 13:57:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|