summaryrefslogtreecommitdiffstats
path: root/lib/repair
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-06-26 16:40:12 +0200
committerVincent Petry <pvince81@owncloud.com>2014-06-26 16:40:12 +0200
commit67d0a3c15e52d865ebb5420e98b177a1bb9e42ba (patch)
tree9a872fe8aa35abaa3e83e786e18deb7e8942cb5c /lib/repair
parentb749292c18367a653d11d9d93e3320cf1dc9d330 (diff)
downloadnextcloud-server-67d0a3c15e52d865ebb5420e98b177a1bb9e42ba.tar.gz
nextcloud-server-67d0a3c15e52d865ebb5420e98b177a1bb9e42ba.zip
Fixed mime type repair step
Diffstat (limited to 'lib/repair')
-rw-r--r--lib/repair/repairmimetypes.php96
1 files changed, 67 insertions, 29 deletions
diff --git a/lib/repair/repairmimetypes.php b/lib/repair/repairmimetypes.php
index 397d18eb72d..f7618c6e060 100644
--- a/lib/repair/repairmimetypes.php
+++ b/lib/repair/repairmimetypes.php
@@ -24,61 +24,99 @@ class RepairMimeTypes extends BasicEmitter implements \OC\RepairStep {
'application/msexcel' => 'application/vnd.ms-excel',
);
- $stmt = \OC_DB::prepare('
- UPDATE `*PREFIX*mimetypes`
- SET `mimetype` = ?
+ $existsStmt = \OC_DB::prepare('
+ SELECT count(`mimetype`)
+ FROM `*PREFIX*mimetypes`
WHERE `mimetype` = ?
');
+ $getIdStmt = \OC_DB::prepare('
+ SELECT `id`
+ FROM `*PREFIX*mimetypes`
+ WHERE `mimetype` = ?
+ ');
+
+ $insertStmt = \OC_DB::prepare('
+ INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
+ VALUES ( ? )
+ ');
+
+ $updateWrongStmt = \OC_DB::prepare('
+ UPDATE `*PREFIX*filecache`
+ SET `mimetype` = (
+ SELECT `id`
+ FROM `*PREFIX*mimetypes`
+ WHERE `mimetype` = ?
+ ) WHERE `mimetype` = ?
+ ');
+
+ $deleteStmt = \OC_DB::prepare('
+ DELETE FROM `*PREFIX*mimetypes`
+ WHERE `id` = ?
+ ');
+
foreach ($wrongMimetypes as $wrong => $correct) {
- \OC_DB::executeAudited($stmt, array($wrong, $correct));
+
+
+ // do we need to remove a wrong mimetype?
+ $result = \OC_DB::executeAudited($getIdStmt, array($wrong));
+ $wrongId = $result->fetchOne();
+
+ if ($wrongId !== false) {
+
+ // do we need to insert the correct mimetype?
+ $result = \OC_DB::executeAudited($existsStmt, array($correct));
+ $exists = $result->fetchOne();
+
+ if ( ! $exists ) {
+ // insert mimetype
+ \OC_DB::executeAudited($insertStmt, array($correct));
+ }
+
+ // change wrong mimetype to correct mimetype in filecache
+ \OC_DB::executeAudited($updateWrongStmt, array($correct, $wrongId));
+
+ // delete wrong mimetype
+ \OC_DB::executeAudited($deleteStmt, array($wrongId));
+
+ }
+
}
$updatedMimetypes = array(
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
- 'xlsx' => 'application/vnd.ms-excel',
- 'pptx' => 'application/vnd.ms-powerpoint',
+ 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
);
+ $updateByNameStmt = \OC_DB::prepare('
+ UPDATE `*PREFIX*filecache`
+ SET `mimetype` = (
+ SELECT `id`
+ FROM `*PREFIX*mimetypes`
+ WHERE `mimetype` = ?
+ ) WHERE `name` LIKE ?
+ ');
+
// separate doc from docx etc
foreach ($updatedMimetypes as $extension => $mimetype ) {
- $result = \OC_DB::executeAudited('
- SELECT count(`mimetype`)
- FROM `*PREFIX*mimetypes`
- WHERE `mimetype` = ?
- ', array($mimetype)
- );
-
+ $result = \OC_DB::executeAudited($existsStmt, array($mimetype));
$exists = $result->fetchOne();
if ( ! $exists ) {
// insert mimetype
- \OC_DB::executeAudited('
- INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
- VALUES ( ? )
- ', array($mimetype)
- );
+ \OC_DB::executeAudited($insertStmt, array($mimetype));
}
// change mimetype for files with x extension
- \OC_DB::executeAudited('
- UPDATE `*PREFIX*filecache`
- SET `mimetype` = (
- SELECT `id`
- FROM `*PREFIX*mimetypes`
- WHERE `mimetype` = ?
- ) WHERE `name` LIKE ?
- ', array($mimetype, '%.'.$extension)
- );
+ \OC_DB::executeAudited($updateByNameStmt, array($mimetype, '%.'.$extension));
}
- return true;
}
/**
* Fix mime types
*/
public function run() {
- // TODO: check precondition to avoid running the fix every time
if ($this->fixOfficeMimeTypes()) {
$this->emit('\OC\Repair', 'info', array('Fixed office mime types'));
}