Skip to content

Commit 6a5b921

Browse files
authored
Merge pull request #23950 from nextcloud/backport/23928/stable20
[stable20] Fix repair mimetype step to not leave stray cursors
2 parents 73c2ad2 + cecf579 commit 6a5b921

File tree

4 files changed

+51
-54
lines changed

4 files changed

+51
-54
lines changed

build/psalm-baseline.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5585,6 +5585,13 @@
55855585
<code>array</code>
55865586
</InvalidReturnType>
55875587
</file>
5588+
<file src="lib/private/Share/Helper.php">
5589+
<ImplicitToStringCast occurrences="3">
5590+
<code>$query->createNamedParameter($parents, IQueryBuilder::PARAM_INT_ARRAY)</code>
5591+
<code>$query->createNamedParameter($changeParent, IQueryBuilder::PARAM_INT_ARRAY)</code>
5592+
<code>$query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)</code>
5593+
</ImplicitToStringCast>
5594+
</file>
55885595
<file src="lib/private/Share/Share.php">
55895596
<FalsableReturnStatement occurrences="1">
55905597
<code>false</code>

lib/private/Repair.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function addStep($repairStep) {
144144
public static function getRepairSteps() {
145145
return [
146146
new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
147-
new RepairMimeTypes(\OC::$server->getConfig()),
147+
new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
148148
new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
149149
new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
150150
new MoveUpdaterStepFile(\OC::$server->getConfig()),

lib/private/Repair/RepairMimeTypes.php

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,84 +33,74 @@
3333

3434
namespace OC\Repair;
3535

36+
use OCP\DB\QueryBuilder\IQueryBuilder;
37+
use OCP\IConfig;
38+
use OCP\IDBConnection;
3639
use OCP\Migration\IOutput;
3740
use OCP\Migration\IRepairStep;
3841

3942
class RepairMimeTypes implements IRepairStep {
40-
/**
41-
* @var \OCP\IConfig
42-
*/
43+
/** @var IConfig */
4344
protected $config;
45+
/** @var IDBConnection */
46+
protected $connection;
4447

45-
/**
46-
* @var int
47-
*/
48+
/** @var int */
4849
protected $folderMimeTypeId;
4950

50-
/**
51-
* @param \OCP\IConfig $config
52-
*/
53-
public function __construct($config) {
51+
public function __construct(IConfig $config,
52+
IDBConnection $connection) {
5453
$this->config = $config;
54+
$this->connection = $connection;
5555
}
5656

5757
public function getName() {
5858
return 'Repair mime types';
5959
}
6060

61-
private static function existsStmt() {
62-
return \OC_DB::prepare('
63-
SELECT count(`mimetype`)
64-
FROM `*PREFIX*mimetypes`
65-
WHERE `mimetype` = ?
66-
');
67-
}
68-
69-
private static function getIdStmt() {
70-
return \OC_DB::prepare('
71-
SELECT `id`
72-
FROM `*PREFIX*mimetypes`
73-
WHERE `mimetype` = ?
74-
');
75-
}
76-
77-
private static function insertStmt() {
78-
return \OC_DB::prepare('
79-
INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
80-
VALUES ( ? )
81-
');
82-
}
83-
84-
private static function updateByNameStmt() {
85-
return \OC_DB::prepare('
86-
UPDATE `*PREFIX*filecache`
87-
SET `mimetype` = ?
88-
WHERE `mimetype` <> ? AND `mimetype` <> ? AND `name` ILIKE ?
89-
');
90-
}
91-
9261
private function updateMimetypes($updatedMimetypes) {
62+
$query = $this->connection->getQueryBuilder();
63+
$query->select('id')
64+
->from('mimetypes')
65+
->where($query->expr()->eq('mimetype', $query->createParameter('mimetype'), IQueryBuilder::PARAM_INT));
66+
$insert = $this->connection->getQueryBuilder();
67+
$insert->insert('mimetypes')
68+
->setValue('mimetype', $insert->createParameter('mimetype'));
69+
9370
if (empty($this->folderMimeTypeId)) {
94-
$result = \OC_DB::executeAudited(self::getIdStmt(), ['httpd/unix-directory']);
95-
$this->folderMimeTypeId = (int)$result->fetchOne();
71+
$query->setParameter('mimetype', 'httpd/unix-directory');
72+
$result = $query->execute();
73+
$this->folderMimeTypeId = (int)$result->fetchColumn();
74+
$result->closeCursor();
9675
}
9776

77+
$update = $this->connection->getQueryBuilder();
78+
$update->update('filecache')
79+
->set('mimetype', $update->createParameter('mimetype'))
80+
->where($update->expr()->neq('mimetype', $update->createParameter('mimetype'), IQueryBuilder::PARAM_INT))
81+
->andWhere($update->expr()->neq('mimetype', $update->createParameter('folder'), IQueryBuilder::PARAM_INT))
82+
->andWhere($update->expr()->iLike('name', $update->createParameter('name')))
83+
->setParameter('folder', $this->folderMimeTypeId);
84+
9885
$count = 0;
9986
foreach ($updatedMimetypes as $extension => $mimetype) {
100-
$result = \OC_DB::executeAudited(self::existsStmt(), [$mimetype]);
101-
$exists = $result->fetchOne();
87+
// get target mimetype id
88+
$query->setParameter('mimetype', $mimetype);
89+
$result = $query->execute();
90+
$mimetypeId = (int)$result->fetchColumn();
91+
$result->closeCursor();
10292

103-
if (!$exists) {
93+
if (!$mimetypeId) {
10494
// insert mimetype
105-
\OC_DB::executeAudited(self::insertStmt(), [$mimetype]);
95+
$insert->setParameter('mimetype', $mimetype);
96+
$insert->execute();
97+
$mimetypeId = $insert->getLastInsertId();
10698
}
10799

108-
// get target mimetype id
109-
$result = \OC_DB::executeAudited(self::getIdStmt(), [$mimetype]);
110-
$mimetypeId = $result->fetchOne();
111-
112100
// change mimetype for files with x extension
113-
$count += \OC_DB::executeAudited(self::updateByNameStmt(), [$mimetypeId, $this->folderMimeTypeId, $mimetypeId, '%.' . $extension]);
101+
$update->setParameter('mimetype', $mimetypeId)
102+
->setParameter('name', '%' . $this->connection->escapeLikeParameter('.' . $extension));
103+
$count += $update->execute();
114104
}
115105

116106
return $count;

tests/lib/Repair/RepairMimeTypesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected function setUp(): void {
5050

5151
$this->storage = new \OC\Files\Storage\Temporary([]);
5252

53-
$this->repair = new \OC\Repair\RepairMimeTypes($config);
53+
$this->repair = new \OC\Repair\RepairMimeTypes($config, \OC::$server->getDatabaseConnection());
5454
}
5555

5656
protected function tearDown(): void {

0 commit comments

Comments
 (0)