Completed
Push — master ( 029219...af20f1 )
by Morris
43:36 queued 23:35
created
lib/private/Repair/RepairMimeTypes.php 1 patch
Indentation   +174 added lines, -174 removed lines patch added patch discarded remove patch
@@ -34,196 +34,196 @@
 block discarded – undo
34 34
 use OCP\Migration\IRepairStep;
35 35
 
36 36
 class RepairMimeTypes implements IRepairStep {
37
-	/**
38
-	 * @var \OCP\IConfig
39
-	 */
40
-	protected $config;
41
-
42
-	/**
43
-	 * @var int
44
-	 */
45
-	protected $folderMimeTypeId;
46
-
47
-	/**
48
-	 * @param \OCP\IConfig $config
49
-	 */
50
-	public function __construct($config) {
51
-		$this->config = $config;
52
-	}
53
-
54
-	public function getName() {
55
-		return 'Repair mime types';
56
-	}
57
-
58
-	private static function existsStmt() {
59
-		return \OC_DB::prepare('
37
+    /**
38
+     * @var \OCP\IConfig
39
+     */
40
+    protected $config;
41
+
42
+    /**
43
+     * @var int
44
+     */
45
+    protected $folderMimeTypeId;
46
+
47
+    /**
48
+     * @param \OCP\IConfig $config
49
+     */
50
+    public function __construct($config) {
51
+        $this->config = $config;
52
+    }
53
+
54
+    public function getName() {
55
+        return 'Repair mime types';
56
+    }
57
+
58
+    private static function existsStmt() {
59
+        return \OC_DB::prepare('
60 60
 			SELECT count(`mimetype`)
61 61
 			FROM   `*PREFIX*mimetypes`
62 62
 			WHERE  `mimetype` = ?
63 63
 		');
64
-	}
64
+    }
65 65
 
66
-	private static function getIdStmt() {
67
-		return \OC_DB::prepare('
66
+    private static function getIdStmt() {
67
+        return \OC_DB::prepare('
68 68
 			SELECT `id`
69 69
 			FROM   `*PREFIX*mimetypes`
70 70
 			WHERE  `mimetype` = ?
71 71
 		');
72
-	}
72
+    }
73 73
 
74
-	private static function insertStmt() {
75
-		return \OC_DB::prepare('
74
+    private static function insertStmt() {
75
+        return \OC_DB::prepare('
76 76
 			INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
77 77
 			VALUES ( ? )
78 78
 		');
79
-	}
79
+    }
80 80
 
81
-	private static function updateByNameStmt() {
82
-		return \OC_DB::prepare('
81
+    private static function updateByNameStmt() {
82
+        return \OC_DB::prepare('
83 83
 			UPDATE `*PREFIX*filecache`
84 84
 			SET `mimetype` = ?
85 85
 			WHERE `mimetype` <> ? AND `mimetype` <> ? AND `name` ILIKE ?
86 86
 		');
87
-	}
88
-
89
-	private function updateMimetypes($updatedMimetypes) {
90
-		if (empty($this->folderMimeTypeId)) {
91
-			$result = \OC_DB::executeAudited(self::getIdStmt(), array('httpd/unix-directory'));
92
-			$this->folderMimeTypeId = (int)$result->fetchOne();
93
-		}
94
-
95
-		$count = 0;
96
-		foreach ($updatedMimetypes as $extension => $mimetype) {
97
-			$result = \OC_DB::executeAudited(self::existsStmt(), array($mimetype));
98
-			$exists = $result->fetchOne();
99
-
100
-			if (!$exists) {
101
-				// insert mimetype
102
-				\OC_DB::executeAudited(self::insertStmt(), array($mimetype));
103
-			}
104
-
105
-			// get target mimetype id
106
-			$result = \OC_DB::executeAudited(self::getIdStmt(), array($mimetype));
107
-			$mimetypeId = $result->fetchOne();
108
-
109
-			// change mimetype for files with x extension
110
-			$count += \OC_DB::executeAudited(self::updateByNameStmt(), array($mimetypeId, $this->folderMimeTypeId, $mimetypeId, '%.' . $extension));
111
-		}
112
-
113
-		return $count;
114
-	}
115
-
116
-	private function introduceImageTypes() {
117
-		$updatedMimetypes = array(
118
-			'jp2' => 'image/jp2',
119
-			'webp' => 'image/webp',
120
-		);
121
-
122
-		return $this->updateMimetypes($updatedMimetypes);
123
-	}
124
-
125
-	private function introduceWindowsProgramTypes() {
126
-		$updatedMimetypes = array(
127
-			'htaccess' => 'text/plain',
128
-			'bat' => 'application/x-msdos-program',
129
-			'cmd' => 'application/cmd',
130
-		);
131
-
132
-		return $this->updateMimetypes($updatedMimetypes);
133
-	}
134
-
135
-	private function introduceLocationTypes() {
136
-		$updatedMimetypes = [
137
-			'gpx' => 'application/gpx+xml',
138
-			'kml' => 'application/vnd.google-earth.kml+xml',
139
-			'kmz' => 'application/vnd.google-earth.kmz',
140
-			'tcx' => 'application/vnd.garmin.tcx+xml',
141
-		];
142
-
143
-		return $this->updateMimetypes($updatedMimetypes);
144
-	}
145
-
146
-	private function introduceInternetShortcutTypes() {
147
-		$updatedMimetypes = [
148
-			'url' => 'application/internet-shortcut',
149
-			'webloc' => 'application/internet-shortcut'
150
-		];
151
-
152
-		return $this->updateMimetypes($updatedMimetypes);
153
-	}
154
-
155
-	private function introduceStreamingTypes() {
156
-		$updatedMimetypes = [
157
-			'm3u' => 'audio/mpegurl',
158
-			'm3u8' => 'audio/mpegurl',
159
-			'pls' => 'audio/x-scpls'
160
-		];
161
-
162
-		return $this->updateMimetypes($updatedMimetypes);
163
-	}
164
-
165
-	private function introduceVisioTypes() {
166
-		$updatedMimetypes = [
167
-			'vsdm' => 'application/vnd.visio',
168
-			'vsdx' => 'application/vnd.visio',
169
-			'vssm' => 'application/vnd.visio',
170
-			'vssx' => 'application/vnd.visio',
171
-			'vstm' => 'application/vnd.visio',
172
-			'vstx' => 'application/vnd.visio',
173
-		];
174
-
175
-		return $this->updateMimetypes($updatedMimetypes);
176
-	}
177
-
178
-	private function introduceComicbookTypes() {
179
-		$updatedMimetypes = [
180
-			'cb7' => 'application/comicbook+7z',
181
-			'cba' => 'application/comicbook+ace',
182
-			'cbr' => 'application/comicbook+rar',
183
-			'cbt' => 'application/comicbook+tar',
184
-			'cbtc' => 'application/comicbook+truecrypt',
185
-			'cbz' => 'application/comicbook+zip',
186
-		];
187
-
188
-		return $this->updateMimetypes($updatedMimetypes);
189
-	}
190
-
191
-	/**
192
-	 * Fix mime types
193
-	 */
194
-	public function run(IOutput $out) {
195
-
196
-		$ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
197
-
198
-		// NOTE TO DEVELOPERS: when adding new mime types, please make sure to
199
-		// add a version comparison to avoid doing it every time
200
-
201
-		if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.14', '<') && $this->introduceImageTypes()) {
202
-			$out->info('Fixed image mime types');
203
-		}
204
-
205
-		if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) {
206
-			$out->info('Fixed windows program mime types');
207
-		}
208
-
209
-		if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.0', '<') && $this->introduceLocationTypes()) {
210
-			$out->info('Fixed geospatial mime types');
211
-		}
212
-
213
-		if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) {
214
-			$out->info('Fixed internet-shortcut mime types');
215
-		}
216
-
217
-		if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.6', '<') && $this->introduceStreamingTypes()) {
218
-			$out->info('Fixed streaming mime types');
219
-		}
220
-
221
-		if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.8', '<') && $this->introduceVisioTypes()) {
222
-			$out->info('Fixed visio mime types');
223
-		}
224
-
225
-		if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.10', '<') && $this->introduceComicbookTypes()) {
226
-			$out->info('Fixed comicbook mime types');
227
-		}
228
-	}
87
+    }
88
+
89
+    private function updateMimetypes($updatedMimetypes) {
90
+        if (empty($this->folderMimeTypeId)) {
91
+            $result = \OC_DB::executeAudited(self::getIdStmt(), array('httpd/unix-directory'));
92
+            $this->folderMimeTypeId = (int)$result->fetchOne();
93
+        }
94
+
95
+        $count = 0;
96
+        foreach ($updatedMimetypes as $extension => $mimetype) {
97
+            $result = \OC_DB::executeAudited(self::existsStmt(), array($mimetype));
98
+            $exists = $result->fetchOne();
99
+
100
+            if (!$exists) {
101
+                // insert mimetype
102
+                \OC_DB::executeAudited(self::insertStmt(), array($mimetype));
103
+            }
104
+
105
+            // get target mimetype id
106
+            $result = \OC_DB::executeAudited(self::getIdStmt(), array($mimetype));
107
+            $mimetypeId = $result->fetchOne();
108
+
109
+            // change mimetype for files with x extension
110
+            $count += \OC_DB::executeAudited(self::updateByNameStmt(), array($mimetypeId, $this->folderMimeTypeId, $mimetypeId, '%.' . $extension));
111
+        }
112
+
113
+        return $count;
114
+    }
115
+
116
+    private function introduceImageTypes() {
117
+        $updatedMimetypes = array(
118
+            'jp2' => 'image/jp2',
119
+            'webp' => 'image/webp',
120
+        );
121
+
122
+        return $this->updateMimetypes($updatedMimetypes);
123
+    }
124
+
125
+    private function introduceWindowsProgramTypes() {
126
+        $updatedMimetypes = array(
127
+            'htaccess' => 'text/plain',
128
+            'bat' => 'application/x-msdos-program',
129
+            'cmd' => 'application/cmd',
130
+        );
131
+
132
+        return $this->updateMimetypes($updatedMimetypes);
133
+    }
134
+
135
+    private function introduceLocationTypes() {
136
+        $updatedMimetypes = [
137
+            'gpx' => 'application/gpx+xml',
138
+            'kml' => 'application/vnd.google-earth.kml+xml',
139
+            'kmz' => 'application/vnd.google-earth.kmz',
140
+            'tcx' => 'application/vnd.garmin.tcx+xml',
141
+        ];
142
+
143
+        return $this->updateMimetypes($updatedMimetypes);
144
+    }
145
+
146
+    private function introduceInternetShortcutTypes() {
147
+        $updatedMimetypes = [
148
+            'url' => 'application/internet-shortcut',
149
+            'webloc' => 'application/internet-shortcut'
150
+        ];
151
+
152
+        return $this->updateMimetypes($updatedMimetypes);
153
+    }
154
+
155
+    private function introduceStreamingTypes() {
156
+        $updatedMimetypes = [
157
+            'm3u' => 'audio/mpegurl',
158
+            'm3u8' => 'audio/mpegurl',
159
+            'pls' => 'audio/x-scpls'
160
+        ];
161
+
162
+        return $this->updateMimetypes($updatedMimetypes);
163
+    }
164
+
165
+    private function introduceVisioTypes() {
166
+        $updatedMimetypes = [
167
+            'vsdm' => 'application/vnd.visio',
168
+            'vsdx' => 'application/vnd.visio',
169
+            'vssm' => 'application/vnd.visio',
170
+            'vssx' => 'application/vnd.visio',
171
+            'vstm' => 'application/vnd.visio',
172
+            'vstx' => 'application/vnd.visio',
173
+        ];
174
+
175
+        return $this->updateMimetypes($updatedMimetypes);
176
+    }
177
+
178
+    private function introduceComicbookTypes() {
179
+        $updatedMimetypes = [
180
+            'cb7' => 'application/comicbook+7z',
181
+            'cba' => 'application/comicbook+ace',
182
+            'cbr' => 'application/comicbook+rar',
183
+            'cbt' => 'application/comicbook+tar',
184
+            'cbtc' => 'application/comicbook+truecrypt',
185
+            'cbz' => 'application/comicbook+zip',
186
+        ];
187
+
188
+        return $this->updateMimetypes($updatedMimetypes);
189
+    }
190
+
191
+    /**
192
+     * Fix mime types
193
+     */
194
+    public function run(IOutput $out) {
195
+
196
+        $ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
197
+
198
+        // NOTE TO DEVELOPERS: when adding new mime types, please make sure to
199
+        // add a version comparison to avoid doing it every time
200
+
201
+        if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.14', '<') && $this->introduceImageTypes()) {
202
+            $out->info('Fixed image mime types');
203
+        }
204
+
205
+        if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) {
206
+            $out->info('Fixed windows program mime types');
207
+        }
208
+
209
+        if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.0', '<') && $this->introduceLocationTypes()) {
210
+            $out->info('Fixed geospatial mime types');
211
+        }
212
+
213
+        if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) {
214
+            $out->info('Fixed internet-shortcut mime types');
215
+        }
216
+
217
+        if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.6', '<') && $this->introduceStreamingTypes()) {
218
+            $out->info('Fixed streaming mime types');
219
+        }
220
+
221
+        if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.8', '<') && $this->introduceVisioTypes()) {
222
+            $out->info('Fixed visio mime types');
223
+        }
224
+
225
+        if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.10', '<') && $this->introduceComicbookTypes()) {
226
+            $out->info('Fixed comicbook mime types');
227
+        }
228
+    }
229 229
 }
Please login to merge, or discard this patch.