|
33 | 33 |
|
34 | 34 | namespace OC\Repair; |
35 | 35 |
|
| 36 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 37 | +use OCP\IConfig; |
| 38 | +use OCP\IDBConnection; |
36 | 39 | use OCP\Migration\IOutput; |
37 | 40 | use OCP\Migration\IRepairStep; |
38 | 41 |
|
39 | 42 | class RepairMimeTypes implements IRepairStep { |
40 | | - /** |
41 | | - * @var \OCP\IConfig |
42 | | - */ |
| 43 | + /** @var IConfig */ |
43 | 44 | protected $config; |
| 45 | + /** @var IDBConnection */ |
| 46 | + protected $connection; |
44 | 47 |
|
45 | | - /** |
46 | | - * @var int |
47 | | - */ |
| 48 | + /** @var int */ |
48 | 49 | protected $folderMimeTypeId; |
49 | 50 |
|
50 | | - /** |
51 | | - * @param \OCP\IConfig $config |
52 | | - */ |
53 | | - public function __construct($config) { |
| 51 | + public function __construct(IConfig $config, |
| 52 | + IDBConnection $connection) { |
54 | 53 | $this->config = $config; |
| 54 | + $this->connection = $connection; |
55 | 55 | } |
56 | 56 |
|
57 | 57 | public function getName() { |
58 | 58 | return 'Repair mime types'; |
59 | 59 | } |
60 | 60 |
|
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 | | - |
92 | 61 | 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 | + |
93 | 70 | 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(); |
96 | 75 | } |
97 | 76 |
|
| 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 | + |
98 | 85 | $count = 0; |
99 | 86 | 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(); |
102 | 92 |
|
103 | | - if (!$exists) { |
| 93 | + if (!$mimetypeId) { |
104 | 94 | // insert mimetype |
105 | | - \OC_DB::executeAudited(self::insertStmt(), [$mimetype]); |
| 95 | + $insert->setParameter('mimetype', $mimetype); |
| 96 | + $insert->execute(); |
| 97 | + $mimetypeId = $insert->getLastInsertId(); |
106 | 98 | } |
107 | 99 |
|
108 | | - // get target mimetype id |
109 | | - $result = \OC_DB::executeAudited(self::getIdStmt(), [$mimetype]); |
110 | | - $mimetypeId = $result->fetchOne(); |
111 | | - |
112 | 100 | // 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(); |
114 | 104 | } |
115 | 105 |
|
116 | 106 | return $count; |
|
0 commit comments