summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-06-27 11:23:19 +0200
committerVincent Petry <pvince81@owncloud.com>2014-06-27 11:23:19 +0200
commit4b5bf606cbb60ac4e57088aff518e1e0e745873f (patch)
tree86369a8f137221fd5461f6871527c2a095808608 /lib
parent36f771e9f08ed23c56129b3e47909aef8cda4ab4 (diff)
parent7ee90ddd595ab51d76bd95809dbb1bd997096e10 (diff)
downloadnextcloud-server-4b5bf606cbb60ac4e57088aff518e1e0e745873f.tar.gz
nextcloud-server-4b5bf606cbb60ac4e57088aff518e1e0e745873f.zip
Merge pull request #9215 from owncloud/officemimetypesupdatefix
Office mime types update fix
Diffstat (limited to 'lib')
-rw-r--r--lib/private/repair.php7
-rw-r--r--lib/repair/repairmimetypes.php125
2 files changed, 130 insertions, 2 deletions
diff --git a/lib/private/repair.php b/lib/private/repair.php
index db2a04433b0..14a917be32c 100644
--- a/lib/private/repair.php
+++ b/lib/private/repair.php
@@ -68,7 +68,9 @@ class Repair extends BasicEmitter {
* @return array of RepairStep instances
*/
public static function getRepairSteps() {
- return array();
+ return array(
+ new \OC\Repair\RepairMimeTypes()
+ );
}
/**
@@ -78,7 +80,8 @@ class Repair extends BasicEmitter {
* @return array of RepairStep instances
*/
public static function getBeforeUpgradeRepairSteps() {
- return array();
+ return array(
+ );
}
/**
diff --git a/lib/repair/repairmimetypes.php b/lib/repair/repairmimetypes.php
new file mode 100644
index 00000000000..f7618c6e060
--- /dev/null
+++ b/lib/repair/repairmimetypes.php
@@ -0,0 +1,125 @@
+<?php
+/**
+ * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
+ * Copyright (c) 2014 Jörn Dreyer jfd@owncloud.com
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Repair;
+
+use OC\Hooks\BasicEmitter;
+
+class RepairMimeTypes extends BasicEmitter implements \OC\RepairStep {
+
+ public function getName() {
+ return 'Repair mime types';
+ }
+
+ private function fixOfficeMimeTypes() {
+ // update wrong mimetypes
+ $wrongMimetypes = array(
+ 'application/mspowerpoint' => 'application/vnd.ms-powerpoint',
+ 'application/msexcel' => 'application/vnd.ms-excel',
+ );
+
+ $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) {
+
+
+ // 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.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($existsStmt, array($mimetype));
+ $exists = $result->fetchOne();
+
+ if ( ! $exists ) {
+ // insert mimetype
+ \OC_DB::executeAudited($insertStmt, array($mimetype));
+ }
+
+ // change mimetype for files with x extension
+ \OC_DB::executeAudited($updateByNameStmt, array($mimetype, '%.'.$extension));
+ }
+ }
+
+ /**
+ * Fix mime types
+ */
+ public function run() {
+ if ($this->fixOfficeMimeTypes()) {
+ $this->emit('\OC\Repair', 'info', array('Fixed office mime types'));
+ }
+ }
+}
+