aboutsummaryrefslogtreecommitdiffstats
path: root/lib/files/cache/upgrade.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/files/cache/upgrade.php')
-rw-r--r--lib/files/cache/upgrade.php155
1 files changed, 114 insertions, 41 deletions
diff --git a/lib/files/cache/upgrade.php b/lib/files/cache/upgrade.php
index 77db4c2339e..1032e0a844c 100644
--- a/lib/files/cache/upgrade.php
+++ b/lib/files/cache/upgrade.php
@@ -9,62 +9,102 @@
namespace OC\Files\Cache;
class Upgrade {
- static $permissionsCaches = array();
+ /**
+ * @var Legacy $legacy
+ */
+ private $legacy;
- static $numericIds = array();
+ private $permissionsCaches = array();
- static function upgrade() {
- $insertQuery = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache`( `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted` )
- VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
+ private $numericIds = array();
- try {
- $oldEntriesQuery = \OC_DB::prepare('SELECT * FROM `*PREFIX*fscache` ORDER BY `id` ASC'); //sort ascending to ensure the parent gets inserted before a child
- } catch (\Exception $e) {
- return;
- }
- try {
- $oldEntriesResult = $oldEntriesQuery->execute();
- } catch (\Exception $e) {
- return;
- }
- if (!$oldEntriesResult) {
+ private $mimeTypeIds = array();
+
+ /**
+ * @param Legacy $legacy
+ */
+ public function __construct($legacy) {
+ $this->legacy = $legacy;
+ }
+
+ /**
+ * Preform a shallow upgrade
+ *
+ * @param string $path
+ * @param int $mode
+ */
+ function upgradePath($path, $mode = Scanner::SCAN_RECURSIVE) {
+ if (!$this->legacy->hasItems()) {
return;
}
+ \OC_Hook::emit('\OC\Files\Cache\Upgrade', 'migrate_path', $path);
- $checkExistingQuery = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `fileid` = ?');
+ if ($row = $this->legacy->get($path)) {
+ $data = $this->getNewData($row);
+ $this->insert($data);
- while ($row = $oldEntriesResult->fetchRow()) {
- if ($checkExistingQuery->execute(array($row['id']))->fetchRow()) {
- continue;
+ $children = $this->legacy->getChildren($data['id']);
+ foreach ($children as $child) {
+ if ($mode == Scanner::SCAN_SHALLOW) {
+ $childData = $this->getNewData($child);
+ \OC_Hook::emit('\OC\Files\Cache\Upgrade', 'migrate_path', $child['path']);
+ $this->insert($childData);
+ } else {
+ $this->upgradePath($child['path']);
+ }
}
+ }
+ }
- list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($row['path']);
- /**
- * @var \OC\Files\Storage\Storage $storage
- * @var string $internalPath;
- */
- $pathHash = md5($internalPath);
- $storageId = self::getNumericId($storage);
- $parentId = ($internalPath === '') ? -1 : $row['parent'];
+ /**
+ * @param array $data the data for the new cache
+ */
+ function insert($data) {
+ $insertQuery = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache`
+ ( `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted` )
+ VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
- $insertQuery->execute(array($row['id'], $storageId, $internalPath, $pathHash, $parentId, $row['name'], $row['mimetype'], $row['mimepart'], $row['size'], $row['mtime'], $row['encrypted']));
+ $insertQuery->execute(array($data['id'], $data['storage'], $data['path'], $data['path_hash'], $data['parent'], $data['name'],
+ $data['mimetype'], $data['mimepart'], $data['size'], $data['mtime'], $data['encrypted']));
- $permissions = ($row['writable']) ? \OCP\PERMISSION_ALL : \OCP\PERMISSION_READ;
- $permissionsCache = self::getPermissionsCache($storage);
- $permissionsCache->set($row['id'], $row['user'], $permissions);
- }
+ $permissionsCache = $this->getPermissionsCache($data['storage_object']);
+ $permissionsCache->set($data['id'], $data['user'], $data['permissions']);
+ }
+
+ /**
+ * get the new data array from the old one
+ *
+ * @param array $data the data from the old cache
+ * @return array
+ */
+ function getNewData($data) {
+ $newData = $data;
+ list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($data['path']);
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ * @var string $internalPath;
+ */
+ $newData['path_hash'] = md5($internalPath);
+ $newData['path'] = $internalPath;
+ $newData['storage'] = $this->getNumericId($storage);
+ $newData['parent'] = ($internalPath === '') ? -1 : $data['parent'];
+ $newData['permissions'] = ($data['writable']) ? \OCP\PERMISSION_ALL : \OCP\PERMISSION_READ;
+ $newData['storage_object'] = $storage;
+ $newData['mimetype'] = $this->getMimetypeId($newData['mimetype'], $storage);
+ $newData['mimepart'] = $this->getMimetypeId($newData['mimepart'], $storage);
+ return $newData;
}
/**
* @param \OC\Files\Storage\Storage $storage
* @return Permissions
*/
- static function getPermissionsCache($storage) {
+ function getPermissionsCache($storage) {
$storageId = $storage->getId();
- if (!isset(self::$permissionsCaches[$storageId])) {
- self::$permissionsCaches[$storageId] = $storage->getPermissionsCache();
+ if (!isset($this->permissionsCaches[$storageId])) {
+ $this->permissionsCaches[$storageId] = $storage->getPermissionsCache();
}
- return self::$permissionsCaches[$storageId];
+ return $this->permissionsCaches[$storageId];
}
/**
@@ -73,12 +113,45 @@ class Upgrade {
* @param \OC\Files\Storage\Storage $storage
* @return int
*/
- static function getNumericId($storage) {
+ function getNumericId($storage) {
$storageId = $storage->getId();
- if (!isset(self::$numericIds[$storageId])) {
+ if (!isset($this->numericIds[$storageId])) {
+ $cache = $storage->getCache();
+ $this->numericIds[$storageId] = $cache->getNumericStorageId();
+ }
+ return $this->numericIds[$storageId];
+ }
+
+ /**
+ * @param string $mimetype
+ * @param \OC\Files\Storage\Storage $storage
+ * @return int
+ */
+ function getMimetypeId($mimetype, $storage) {
+ if (!isset($this->mimeTypeIds[$mimetype])) {
$cache = new Cache($storage);
- self::$numericIds[$storageId] = $cache->getNumericStorageId();
+ $this->mimeTypeIds[$mimetype] = $cache->getMimetypeId($mimetype);
}
- return self::$numericIds[$storageId];
+ return $this->mimeTypeIds[$mimetype];
+ }
+
+ /**
+ * check if a cache upgrade is required for $user
+ *
+ * @param string $user
+ * @return bool
+ */
+ static function needUpgrade($user) {
+ $cacheVersion = (int)\OCP\Config::getUserValue($user, 'files', 'cache_version', 4);
+ return $cacheVersion < 5;
+ }
+
+ /**
+ * mark the filecache as upgrade
+ *
+ * @param string $user
+ */
+ static function upgradeDone($user) {
+ \OCP\Config::setUserValue($user, 'files', 'cache_version', 5);
}
}