]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix repair mimetype step to not leave stray cursors
authorJoas Schilling <coding@schilljs.com>
Fri, 6 Nov 2020 09:26:41 +0000 (10:26 +0100)
committerMorris Jobke <hey@morrisjobke.de>
Tue, 10 Nov 2020 09:27:09 +0000 (10:27 +0100)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Repair.php
lib/private/Repair/RepairMimeTypes.php
tests/lib/Repair/RepairMimeTypesTest.php

index bba797a098ddd3c3d0c81507cd62852aa596916e..61857285f3962153aefff4b29d22225b3b4e03d3 100644 (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()),
index 60a7df25f63956ecdc490956a5fed3af7905783d..c5157f81612416713b87a5cbc6ad6f136ee234fc 100644 (file)
 
 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;
index 6f0791964b32e1d5108e36e58de48d5f0ecadbdb..26a52459c241f33f25d0dad09abbcba8eb9b4389 100644 (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 {