[Pkg-owncloud-commits] [owncloud] 53/273: Moved office mimetype update to a repair step

David Prévot taffit at moszumanska.debian.org
Fri Jul 4 03:12:57 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit b1e778d5fd05ae7224e73dc2cdc72a94c4f8b55c
Author: Vincent Petry <pvince81 at owncloud.com>
Date:   Wed Jun 25 19:34:39 2014 +0200

    Moved office mimetype update to a repair step
---
 apps/files/appinfo/update.php        |  57 -------------------
 lib/private/repair.php               |   7 ++-
 lib/repair/repairmimetypes.php       |  87 ++++++++++++++++++++++++++++
 tests/lib/repair/repairmimetypes.php | 107 +++++++++++++++++++++++++++++++++++
 4 files changed, 199 insertions(+), 59 deletions(-)

diff --git a/apps/files/appinfo/update.php b/apps/files/appinfo/update.php
index a9b8ccb..de635e5 100644
--- a/apps/files/appinfo/update.php
+++ b/apps/files/appinfo/update.php
@@ -6,60 +6,3 @@ if (version_compare(\OCP\Config::getSystemValue('version', '0.0.0'), '7.0.0', '<
 	\OCP\Config::deleteSystemValue('allowZipDownload');
 	\OCP\Config::deleteSystemValue('maxZipInputSize');
 }
-
-if (version_compare(\OCP\Config::getAppValue('files', 'installed_version'), '1.1.9', '<')) {
-
-	// update wrong mimetypes
-	$wrongMimetypes = array(
-		'application/mspowerpoint' => 'application/vnd.ms-powerpoint',
-		'application/msexcel' => 'application/vnd.ms-excel',
-	);
-
-	$stmt = OC_DB::prepare('
-		UPDATE `*PREFIX*mimetypes`
-		SET    `mimetype` = ?
-		WHERE  `mimetype` = ?
-	');
-
-	foreach ($wrongMimetypes as $wrong => $correct) {
-		OC_DB::executeAudited($stmt, array($wrong, $correct));
-	}
-
-	$updatedMimetypes = array(
-		'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
-		'xlsx' => 'application/vnd.ms-excel',
-		'pptx' => 'application/vnd.ms-powerpoint',
-	);
-
-	// separate doc from docx etc
-	foreach ($updatedMimetypes as $extension => $mimetype ) {
-		$result = OC_DB::executeAudited('
-			SELECT count(`mimetype`)
-			FROM   `*PREFIX*mimetypes`
-			WHERE  `mimetype` = ?
-			', array($mimetype)
-		);
-
-		$exists = $result->fetchOne();
-
-		if ( ! $exists ) {
-			// insert mimetype
-			OC_DB::executeAudited('
-				INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
-				VALUES ( ? )
-				', 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)
-		);
-	}
-}
\ No newline at end of file
diff --git a/lib/private/repair.php b/lib/private/repair.php
index 23d1c2b..93547fd 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,6 +80,7 @@ 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 0000000..397d18e
--- /dev/null
+++ b/lib/repair/repairmimetypes.php
@@ -0,0 +1,87 @@
+<?php
+/**
+ * Copyright (c) 2014 Vincent Petry <pvince81 at owncloud.com>
+ * Copyright (c) 2014 Jörn Dreyer jfd at 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',
+		);
+
+		$stmt = \OC_DB::prepare('
+			UPDATE `*PREFIX*mimetypes`
+			SET    `mimetype` = ?
+			WHERE  `mimetype` = ?
+		');
+
+		foreach ($wrongMimetypes as $wrong => $correct) {
+			\OC_DB::executeAudited($stmt, array($wrong, $correct));
+		}
+
+		$updatedMimetypes = array(
+			'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+			'xlsx' => 'application/vnd.ms-excel',
+			'pptx' => 'application/vnd.ms-powerpoint',
+		);
+
+		// separate doc from docx etc
+		foreach ($updatedMimetypes as $extension => $mimetype ) {
+			$result = \OC_DB::executeAudited('
+				SELECT count(`mimetype`)
+				FROM   `*PREFIX*mimetypes`
+				WHERE  `mimetype` = ?
+				', array($mimetype)
+			);
+
+			$exists = $result->fetchOne();
+
+			if ( ! $exists ) {
+				// insert mimetype
+				\OC_DB::executeAudited('
+					INSERT INTO `*PREFIX*mimetypes` ( `mimetype` )
+					VALUES ( ? )
+					', 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)
+			);
+		}
+		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'));
+		}
+	}
+}
+
diff --git a/tests/lib/repair/repairmimetypes.php b/tests/lib/repair/repairmimetypes.php
new file mode 100644
index 0000000..42af116
--- /dev/null
+++ b/tests/lib/repair/repairmimetypes.php
@@ -0,0 +1,107 @@
+<?php
+/**
+ * Copyright (c) 2014 Vincent Petry <pvince81 at owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+/**
+ * Tests for the converting of legacy storages to home storages.
+ *
+ * @see \OC\Repair\RepairMimeTypes
+ */
+class TestRepairMimeTypes extends PHPUnit_Framework_TestCase {
+
+	/** @var \OC\RepairStep */
+	private $repair;
+
+	private $storage;
+
+	public function setUp() {
+		$this->storage = new \OC\Files\Storage\Temporary(array());
+
+		$this->repair = new \OC\Repair\RepairMimeTypes();
+	}
+
+	public function tearDown() {
+		$this->storage->getCache()->clear();
+		$sql = 'DELETE FROM `*PREFIX*storages` WHERE id = ?';
+		\OC_DB::executeAudited($sql, array($this->storage->getId()));
+		$this->clearMimeTypes();
+
+		DummyFileCache::clearCachedMimeTypes();
+	}
+
+	private function clearMimeTypes() {
+		$sql = 'DELETE FROM `*PREFIX*mimetypes`';
+		\OC_DB::executeAudited($sql);
+	}
+
+	private function addEntries($entries) {
+		// create files for the different extensions, this
+		// will also automatically create the corresponding mime types
+		foreach ($entries as $entry) {
+			$this->storage->getCache()->put(
+				$entry[0],
+				array(
+					'size' => 0,
+					'mtime' => 0,
+					'mimetype' => $entry[1]
+				)
+			);
+		}
+
+	}
+
+	private function checkEntries($entries) {
+		foreach ($entries as $entry) {
+			$data = $this->storage->getCache()->get($entry[0]);
+			$this->assertEquals($entry[1], $data['mimetype']);
+		}
+	}
+
+	/**
+	 * Test renaming and splitting old office mime types
+	 */
+	public function testRenameOfficeMimeTypes() {
+		$this->addEntries(
+			array(
+				array('test.doc', 'application/msword'),
+				array('test.docx', 'application/msword'),
+				array('test.xls', 'application/msexcel'),
+				array('test.xlsx', 'application/msexcel'),
+				array('test.ppt', 'application/mspowerpoint'),
+				array('test.pptx', 'application/mspowerpoint'),
+			)
+		);
+
+		$this->repair->run();
+
+		// force mimetype reload
+		$this->storage->getCache()->loadMimeTypes();
+
+		$this->checkEntries(
+			array(
+				array('test.doc', 'application/msword'),
+				array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
+				array('test.xls', 'application/msexcel'),
+				array('test.xlsx', 'application/vnd.ms-excel'),
+				array('test.ppt', 'application/mspowerpoint'),
+				array('test.pptx', 'application/vnd.ms-powerpoint'),
+			)
+		);
+	}
+}
+
+/**
+ * Dummy class to access protected members
+ */
+class DummyFileCache extends \OC\Files\Cache\Cache {
+
+	public static function clearCachedMimeTypes() {
+		self::$mimetypeIds = array();
+		self::$mimetypes = array();
+	}
+}
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list