Merge pull request #23950 from nextcloud/backport/23928/stable20

[stable20] Fix repair mimetype step to not leave stray cursors
This commit is contained in:
Morris Jobke 2020-11-10 13:46:15 +01:00 committed by GitHub
commit 6a5b921284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 54 deletions

View File

@ -5585,6 +5585,13 @@
<code>array</code>
</InvalidReturnType>
</file>
<file src="lib/private/Share/Helper.php">
<ImplicitToStringCast occurrences="3">
<code>$query->createNamedParameter($parents, IQueryBuilder::PARAM_INT_ARRAY)</code>
<code>$query->createNamedParameter($changeParent, IQueryBuilder::PARAM_INT_ARRAY)</code>
<code>$query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)</code>
</ImplicitToStringCast>
</file>
<file src="lib/private/Share/Share.php">
<FalsableReturnStatement occurrences="1">
<code>false</code>

View File

@ -144,7 +144,7 @@ class Repair implements IOutput {
public static function getRepairSteps() {
return [
new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
new RepairMimeTypes(\OC::$server->getConfig()),
new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
new MoveUpdaterStepFile(\OC::$server->getConfig()),

View File

@ -33,84 +33,74 @@
namespace OC\Repair;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
class RepairMimeTypes implements IRepairStep {
/**
* @var \OCP\IConfig
*/
/** @var IConfig */
protected $config;
/** @var IDBConnection */
protected $connection;
/**
* @var int
*/
/** @var int */
protected $folderMimeTypeId;
/**
* @param \OCP\IConfig $config
*/
public function __construct($config) {
public function __construct(IConfig $config,
IDBConnection $connection) {
$this->config = $config;
$this->connection = $connection;
}
public function getName() {
return 'Repair mime types';
}
private static function existsStmt() {
return \OC_DB::prepare('
SELECT count(`mimetype`)
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
');
}
private static function getIdStmt() {
return \OC_DB::prepare('
SELECT `id`
FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
');
}
private static function insertStmt() {
return \OC_DB::prepare('
INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
VALUES ( ? )
');
}
private static function updateByNameStmt() {
return \OC_DB::prepare('
UPDATE `*PREFIX*filecache`
SET `mimetype` = ?
WHERE `mimetype` <> ? AND `mimetype` <> ? AND `name` ILIKE ?
');
}
private function updateMimetypes($updatedMimetypes) {
$query = $this->connection->getQueryBuilder();
$query->select('id')
->from('mimetypes')
->where($query->expr()->eq('mimetype', $query->createParameter('mimetype'), IQueryBuilder::PARAM_INT));
$insert = $this->connection->getQueryBuilder();
$insert->insert('mimetypes')
->setValue('mimetype', $insert->createParameter('mimetype'));
if (empty($this->folderMimeTypeId)) {
$result = \OC_DB::executeAudited(self::getIdStmt(), ['httpd/unix-directory']);
$this->folderMimeTypeId = (int)$result->fetchOne();
$query->setParameter('mimetype', 'httpd/unix-directory');
$result = $query->execute();
$this->folderMimeTypeId = (int)$result->fetchColumn();
$result->closeCursor();
}
$update = $this->connection->getQueryBuilder();
$update->update('filecache')
->set('mimetype', $update->createParameter('mimetype'))
->where($update->expr()->neq('mimetype', $update->createParameter('mimetype'), IQueryBuilder::PARAM_INT))
->andWhere($update->expr()->neq('mimetype', $update->createParameter('folder'), IQueryBuilder::PARAM_INT))
->andWhere($update->expr()->iLike('name', $update->createParameter('name')))
->setParameter('folder', $this->folderMimeTypeId);
$count = 0;
foreach ($updatedMimetypes as $extension => $mimetype) {
$result = \OC_DB::executeAudited(self::existsStmt(), [$mimetype]);
$exists = $result->fetchOne();
// get target mimetype id
$query->setParameter('mimetype', $mimetype);
$result = $query->execute();
$mimetypeId = (int)$result->fetchColumn();
$result->closeCursor();
if (!$exists) {
if (!$mimetypeId) {
// insert mimetype
\OC_DB::executeAudited(self::insertStmt(), [$mimetype]);
$insert->setParameter('mimetype', $mimetype);
$insert->execute();
$mimetypeId = $insert->getLastInsertId();
}
// get target mimetype id
$result = \OC_DB::executeAudited(self::getIdStmt(), [$mimetype]);
$mimetypeId = $result->fetchOne();
// change mimetype for files with x extension
$count += \OC_DB::executeAudited(self::updateByNameStmt(), [$mimetypeId, $this->folderMimeTypeId, $mimetypeId, '%.' . $extension]);
$update->setParameter('mimetype', $mimetypeId)
->setParameter('name', '%' . $this->connection->escapeLikeParameter('.' . $extension));
$count += $update->execute();
}
return $count;

View File

@ -50,7 +50,7 @@ class RepairMimeTypesTest extends \Test\TestCase {
$this->storage = new \OC\Files\Storage\Temporary([]);
$this->repair = new \OC\Repair\RepairMimeTypes($config);
$this->repair = new \OC\Repair\RepairMimeTypes($config, \OC::$server->getDatabaseConnection());
}
protected function tearDown(): void {