nextcloud/lib/files/cache/legacy.php

137 lines
3.2 KiB
PHP
Raw Normal View History

2013-01-03 03:26:13 +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;
/**
* Provide read only support for the old filecache
*/
class Legacy {
private $user;
2013-01-15 22:11:12 +04:00
private $cacheHasItems = null;
2013-01-03 03:26:13 +04:00
public function __construct($user) {
$this->user = $user;
}
2013-03-24 05:06:50 +04:00
/**
* get the numbers of items in the legacy cache
*
* @return int
*/
2013-01-03 03:26:13 +04:00
function getCount() {
2013-06-07 16:11:05 +04:00
$sql = 'SELECT COUNT(`id`) AS `count` FROM `*PREFIX*fscache` WHERE `user` = ?';
$result = \OC_DB::executeAudited($sql, array($this->user));
2013-01-03 03:26:13 +04:00
if ($row = $result->fetchRow()) {
return $row['count'];
} else {
return 0;
}
}
/**
* check if a legacy cache is present and holds items
*
* @return bool
*/
function hasItems() {
2013-01-15 22:11:12 +04:00
if (!is_null($this->cacheHasItems)) {
return $this->cacheHasItems;
}
2013-01-03 03:26:13 +04:00
try {
2013-06-10 12:04:52 +04:00
$query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*fscache` WHERE `user` = ?',1);
2013-01-03 03:26:13 +04:00
} catch (\Exception $e) {
2013-01-15 22:11:12 +04:00
$this->cacheHasItems = false;
2013-01-03 03:26:13 +04:00
return false;
}
try {
$result = $query->execute(array($this->user));
} catch (\Exception $e) {
2013-01-15 22:11:12 +04:00
$this->cacheHasItems = false;
2013-01-03 03:26:13 +04:00
return false;
}
2013-02-22 20:21:57 +04:00
if ($result === false || property_exists($result, 'error_message_prefix')) {
$this->cacheHasItems = false;
return false;
2013-02-22 20:21:57 +04:00
}
2013-01-15 22:11:12 +04:00
$this->cacheHasItems = (bool)$result->fetchRow();
return $this->cacheHasItems;
2013-01-03 03:26:13 +04:00
}
/**
2013-03-24 05:06:50 +04:00
* get an item from the legacy cache
*
2013-01-03 03:26:13 +04:00
* @param string|int $path
* @return array
*/
function get($path) {
if (is_numeric($path)) {
2013-06-07 16:11:05 +04:00
$sql = 'SELECT * FROM `*PREFIX*fscache` WHERE `id` = ?';
2013-01-03 03:26:13 +04:00
} else {
2013-06-07 16:11:05 +04:00
$sql = 'SELECT * FROM `*PREFIX*fscache` WHERE `path` = ?';
2013-01-03 03:26:13 +04:00
}
2013-06-07 16:11:05 +04:00
$result = \OC_DB::executeAudited($sql, array($path));
2013-03-24 19:20:59 +04:00
$data = $result->fetchRow();
$data['etag'] = $this->getEtag($data['path'], $data['user']);
2013-03-24 19:20:59 +04:00
return $data;
}
2013-03-26 00:45:55 +04:00
/**
* Get the ETag for the given path
*
* @param type $path
* @return string
*/
function getEtag($path, $user = null) {
2013-03-26 00:46:50 +04:00
static $query = null;
$pathDetails = explode('/', $path, 4);
if((!$user) && !isset($pathDetails[1])) {
//no user!? Too odd, return empty string.
return '';
} else if(!$user) {
//guess user from path, if no user passed.
$user = $pathDetails[1];
}
if(!isset($pathDetails[3]) || is_null($pathDetails[3])) {
2013-03-24 19:20:59 +04:00
$relativePath = '';
} else {
$relativePath = $pathDetails[3];
2013-03-24 19:20:59 +04:00
}
2013-03-26 00:46:50 +04:00
if(is_null($query)){
2013-04-03 15:04:38 +04:00
$query = \OC_DB::prepare('SELECT `propertyvalue` FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = \'{DAV:}getetag\'');
2013-03-26 00:46:50 +04:00
}
2013-06-07 16:11:05 +04:00
$result = \OC_DB::executeAudited($query,array($user, '/' . $relativePath));
2013-03-24 19:20:59 +04:00
if ($row = $result->fetchRow()) {
return trim($row['propertyvalue'], '"');
} else {
return '';
}
2013-01-03 03:26:13 +04:00
}
/**
2013-03-24 05:06:50 +04:00
* get all child items of an item from the legacy cache
*
2013-01-03 03:26:13 +04:00
* @param int $id
* @return array
*/
function getChildren($id) {
2013-06-07 16:11:05 +04:00
$result = \OC_DB::executeAudited('SELECT * FROM `*PREFIX*fscache` WHERE `parent` = ?', array($id));
2013-03-24 19:20:59 +04:00
$data = $result->fetchAll();
foreach ($data as $i => $item) {
$data[$i]['etag'] = $this->getEtag($item['path'], $item['user']);
2013-03-24 19:20:59 +04:00
}
return $data;
2013-01-03 03:26:13 +04:00
}
}