2012-11-30 00:41:30 +00: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-02 23:26:13 +00:00
|
|
|
/**
|
|
|
|
* @var Legacy $legacy
|
|
|
|
*/
|
|
|
|
private $legacy;
|
2012-11-30 00:41:30 +00:00
|
|
|
|
2013-01-02 23:26:13 +00:00
|
|
|
private $numericIds = array();
|
2012-11-30 00:41:30 +00:00
|
|
|
|
2013-01-02 23:26:13 +00:00
|
|
|
private $mimeTypeIds = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Legacy $legacy
|
|
|
|
*/
|
|
|
|
public function __construct($legacy) {
|
|
|
|
$this->legacy = $legacy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-24 01:06:50 +00:00
|
|
|
* Preform a upgrade a path and it's childs
|
2013-01-02 23:26:13 +00:00
|
|
|
*
|
|
|
|
* @param string $path
|
2013-03-24 01:06:50 +00:00
|
|
|
* @param bool $mode
|
2013-01-02 23:26:13 +00:00
|
|
|
*/
|
|
|
|
function upgradePath($path, $mode = Scanner::SCAN_RECURSIVE) {
|
|
|
|
if (!$this->legacy->hasItems()) {
|
2012-11-30 23:59:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-01-02 23:26:13 +00:00
|
|
|
\OC_Hook::emit('\OC\Files\Cache\Upgrade', 'migrate_path', $path);
|
|
|
|
if ($row = $this->legacy->get($path)) {
|
|
|
|
$data = $this->getNewData($row);
|
2013-03-16 18:28:42 +00:00
|
|
|
if ($data) {
|
|
|
|
$this->insert($data);
|
|
|
|
$this->upgradeChilds($data['id'], $mode);
|
|
|
|
}
|
2013-01-15 18:11:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-24 01:06:50 +00:00
|
|
|
* upgrade all child elements of an item
|
|
|
|
*
|
2013-01-15 18:11:12 +00:00
|
|
|
* @param int $id
|
2013-03-24 01:06:50 +00:00
|
|
|
* @param bool $mode
|
2013-01-15 18:11:12 +00:00
|
|
|
*/
|
|
|
|
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']);
|
2013-03-16 18:28:42 +00:00
|
|
|
if ($childData) {
|
|
|
|
$this->insert($childData);
|
|
|
|
if ($mode == Scanner::SCAN_RECURSIVE) {
|
|
|
|
$this->upgradeChilds($child['id']);
|
|
|
|
}
|
2012-12-15 01:29:34 +00:00
|
|
|
}
|
2013-01-02 23:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-15 01:29:34 +00:00
|
|
|
|
2013-01-02 23:26:13 +00:00
|
|
|
/**
|
2013-03-24 01:06:50 +00:00
|
|
|
* insert data into the new cache
|
|
|
|
*
|
2013-01-02 23:26:13 +00:00
|
|
|
* @param array $data the data for the new cache
|
|
|
|
*/
|
|
|
|
function insert($data) {
|
2013-03-22 12:36:31 +00:00
|
|
|
static $insertQuery = null;
|
|
|
|
if(is_null($insertQuery)) {
|
2013-01-30 17:17:56 +00:00
|
|
|
$insertQuery = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache`
|
2013-03-22 12:36:31 +00:00
|
|
|
( `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `etag` )
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
|
|
|
}
|
|
|
|
if (!$this->inCache($data['storage'], $data['path_hash'], $data['id'])) {
|
2013-06-07 12:11:05 +00:00
|
|
|
\OC_DB::executeAudited($insertQuery, array($data['id'], $data['storage'],
|
2013-02-11 16:44:02 +00:00
|
|
|
$data['path'], $data['path_hash'], $data['parent'], $data['name'],
|
2013-03-22 12:23:43 +00:00
|
|
|
$data['mimetype'], $data['mimepart'], $data['size'], $data['mtime'], $data['encrypted'], $data['etag']));
|
2013-01-30 17:17:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-24 01:06:50 +00:00
|
|
|
* check if an item is already in the new cache
|
|
|
|
*
|
2013-01-30 17:17:56 +00:00
|
|
|
* @param string $storage
|
|
|
|
* @param string $pathHash
|
2013-03-04 21:26:03 +00:00
|
|
|
* @param string $id
|
2013-01-30 17:17:56 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-03-04 21:26:03 +00:00
|
|
|
function inCache($storage, $pathHash, $id) {
|
2013-03-22 12:36:31 +00:00
|
|
|
static $query = null;
|
|
|
|
if(is_null($query)) {
|
|
|
|
$query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE (`storage` = ? AND `path_hash` = ?) OR `fileid` = ?');
|
|
|
|
}
|
2013-06-07 12:11:05 +00:00
|
|
|
$result = \OC_DB::executeAudited($query, array($storage, $pathHash, $id));
|
2013-03-04 22:19:55 +00:00
|
|
|
return (bool)$result->fetchRow();
|
2013-01-02 23:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the new data array from the old one
|
|
|
|
*
|
|
|
|
* @param array $data the data from the old cache
|
2013-03-22 12:23:43 +00:00
|
|
|
* Example data array
|
|
|
|
* Array
|
|
|
|
* (
|
|
|
|
* [id] => 418
|
|
|
|
* [path] => /tina/files/picture.jpg //relative to datadir
|
|
|
|
* [path_hash] => 66d4547e372888deed80b24fec9b192b
|
|
|
|
* [parent] => 234
|
|
|
|
* [name] => picture.jpg
|
|
|
|
* [user] => tina
|
|
|
|
* [size] => 1265283
|
|
|
|
* [ctime] => 1363909709
|
|
|
|
* [mtime] => 1363909709
|
|
|
|
* [mimetype] => image/jpeg
|
|
|
|
* [mimepart] => image
|
|
|
|
* [encrypted] => 0
|
|
|
|
* [versioned] => 0
|
|
|
|
* [writable] => 1
|
|
|
|
* )
|
|
|
|
*
|
2013-01-02 23:26:13 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getNewData($data) {
|
2013-04-05 09:27:08 +00:00
|
|
|
//Make sure there is a path, otherwise we can do nothing.
|
|
|
|
if(!isset($data['path'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-02 23:26:13 +00:00
|
|
|
$newData = $data;
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
* @var string $internalPath;
|
|
|
|
*/
|
2013-03-16 18:28:42 +00:00
|
|
|
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($data['path']);
|
|
|
|
if ($storage) {
|
2013-03-24 15:20:59 +00:00
|
|
|
$newData['etag'] = $data['etag'];
|
2013-03-16 18:28:42 +00:00
|
|
|
$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;
|
|
|
|
} else {
|
|
|
|
\OC_Log::write('core', 'Unable to migrate data from old cache for '.$data['path'].' because the storage was not found', \OC_Log::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-30 00:41:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-15 22:28:07 +00:00
|
|
|
/**
|
|
|
|
* get the numeric storage id
|
|
|
|
*
|
|
|
|
* @param \OC\Files\Storage\Storage $storage
|
|
|
|
* @return int
|
|
|
|
*/
|
2013-01-02 23:26:13 +00:00
|
|
|
function getNumericId($storage) {
|
2012-12-15 22:28:07 +00:00
|
|
|
$storageId = $storage->getId();
|
2013-01-02 23:26:13 +00:00
|
|
|
if (!isset($this->numericIds[$storageId])) {
|
|
|
|
$cache = $storage->getCache();
|
|
|
|
$this->numericIds[$storageId] = $cache->getNumericStorageId();
|
|
|
|
}
|
|
|
|
return $this->numericIds[$storageId];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-24 01:06:50 +00:00
|
|
|
* get the numeric id for a mimetype
|
|
|
|
*
|
2013-01-02 23:26:13 +00:00
|
|
|
* @param string $mimetype
|
|
|
|
* @param \OC\Files\Storage\Storage $storage
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function getMimetypeId($mimetype, $storage) {
|
|
|
|
if (!isset($this->mimeTypeIds[$mimetype])) {
|
2012-12-15 22:28:07 +00:00
|
|
|
$cache = new Cache($storage);
|
2013-01-02 23:26:13 +00:00
|
|
|
$this->mimeTypeIds[$mimetype] = $cache->getMimetypeId($mimetype);
|
2012-12-15 22:28:07 +00:00
|
|
|
}
|
2013-01-02 23:26:13 +00: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);
|
2013-10-02 11:00:05 +00:00
|
|
|
if ($cacheVersion < 5) {
|
|
|
|
$legacy = new \OC\Files\Cache\Legacy($user);
|
|
|
|
if ($legacy->hasItems()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
self::upgradeDone($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-01-02 23:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mark the filecache as upgrade
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
*/
|
|
|
|
static function upgradeDone($user) {
|
|
|
|
\OCP\Config::setUserValue($user, 'files', 'cache_version', 5);
|
2012-12-15 22:28:07 +00:00
|
|
|
}
|
2013-03-22 22:33:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does a "silent" upgrade, i.e. without an Event-Source as triggered
|
|
|
|
* on User-Login via Ajax. This method is called within the regular
|
|
|
|
* ownCloud upgrade.
|
|
|
|
*
|
|
|
|
* @param string $user a User ID
|
|
|
|
*/
|
|
|
|
public static function doSilentUpgrade($user) {
|
|
|
|
if(!self::needUpgrade($user)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$legacy = new \OC\Files\Cache\Legacy($user);
|
|
|
|
if ($legacy->hasItems()) {
|
|
|
|
\OC_DB::beginTransaction();
|
|
|
|
$upgrade = new \OC\Files\Cache\Upgrade($legacy);
|
|
|
|
$upgrade->upgradePath('/' . $user . '/files');
|
|
|
|
\OC_DB::commit();
|
|
|
|
}
|
|
|
|
\OC\Files\Cache\Upgrade::upgradeDone($user);
|
|
|
|
}
|
2012-11-30 00:41:30 +00:00
|
|
|
}
|