2012-11-30 04:41:30 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Cache;
|
|
|
|
|
|
|
|
class Upgrade {
|
2013-01-03 03:26:13 +04:00
|
|
|
/**
|
|
|
|
* @var Legacy $legacy
|
|
|
|
*/
|
|
|
|
private $legacy;
|
2012-11-30 04:41:30 +04:00
|
|
|
|
2013-01-03 03:26:13 +04:00
|
|
|
private $numericIds = array();
|
2012-11-30 04:41:30 +04:00
|
|
|
|
2013-01-03 03:26:13 +04:00
|
|
|
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()) {
|
2012-12-01 03:59:49 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-01-03 03:26:13 +04:00
|
|
|
\OC_Hook::emit('\OC\Files\Cache\Upgrade', 'migrate_path', $path);
|
2012-11-30 04:41:30 +04:00
|
|
|
|
2013-01-03 03:26:13 +04:00
|
|
|
if ($row = $this->legacy->get($path)) {
|
|
|
|
$data = $this->getNewData($row);
|
|
|
|
$this->insert($data);
|
2012-12-15 05:29:34 +04:00
|
|
|
|
2013-01-15 22:11:12 +04:00
|
|
|
$this->upgradeChilds($data['id'], $mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
*/
|
|
|
|
function upgradeChilds($id, $mode = Scanner::SCAN_RECURSIVE) {
|
|
|
|
$children = $this->legacy->getChildren($id);
|
|
|
|
foreach ($children as $child) {
|
|
|
|
$childData = $this->getNewData($child);
|
|
|
|
\OC_Hook::emit('\OC\Files\Cache\Upgrade', 'migrate_path', $child['path']);
|
|
|
|
$this->insert($childData);
|
|
|
|
if ($mode == Scanner::SCAN_RECURSIVE) {
|
|
|
|
$this->upgradeChilds($child['id']);
|
2012-12-15 05:29:34 +04:00
|
|
|
}
|
2013-01-03 03:26:13 +04:00
|
|
|
}
|
|
|
|
}
|
2012-12-15 05:29:34 +04:00
|
|
|
|
2013-01-03 03:26:13 +04:00
|
|
|
/**
|
|
|
|
* @param array $data the data for the new cache
|
|
|
|
*/
|
|
|
|
function insert($data) {
|
2013-01-30 21:17:56 +04:00
|
|
|
if (!$this->inCache($data['storage'], $data['path_hash'])) {
|
|
|
|
$insertQuery = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache`
|
|
|
|
( `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted` )
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
2012-11-30 04:41:30 +04:00
|
|
|
|
2013-01-30 21:17:56 +04:00
|
|
|
$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']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $storage
|
|
|
|
* @param string $pathHash
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function inCache($storage, $pathHash) {
|
|
|
|
$query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
|
|
|
|
$result = $query->execute(array($storage, $pathHash));
|
|
|
|
return (bool)$result->fetchRow();
|
2013-01-03 03:26:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2012-11-30 04:41:30 +04:00
|
|
|
}
|
|
|
|
|
2012-12-16 02:28:07 +04:00
|
|
|
/**
|
|
|
|
* get the numeric storage id
|
|
|
|
*
|
|
|
|
* @param \OC\Files\Storage\Storage $storage
|
|
|
|
* @return int
|
|
|
|
*/
|
2013-01-03 03:26:13 +04:00
|
|
|
function getNumericId($storage) {
|
2012-12-16 02:28:07 +04:00
|
|
|
$storageId = $storage->getId();
|
2013-01-03 03:26:13 +04:00
|
|
|
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])) {
|
2012-12-16 02:28:07 +04:00
|
|
|
$cache = new Cache($storage);
|
2013-01-03 03:26:13 +04:00
|
|
|
$this->mimeTypeIds[$mimetype] = $cache->getMimetypeId($mimetype);
|
2012-12-16 02:28:07 +04:00
|
|
|
}
|
2013-01-03 03:26:13 +04:00
|
|
|
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);
|
2012-12-16 02:28:07 +04:00
|
|
|
}
|
2012-11-30 04:41:30 +04:00
|
|
|
}
|