summaryrefslogtreecommitdiffstats
path: root/lib/files/cache/upgrade.php
blob: 77db4c2339e4476fa575c9db1d2330a2936e360e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?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 {
	static $permissionsCaches = array();

	static $numericIds = 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(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');

		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) {
			return;
		}

		$checkExistingQuery = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `fileid` = ?');

		while ($row = $oldEntriesResult->fetchRow()) {
			if ($checkExistingQuery->execute(array($row['id']))->fetchRow()) {
				continue;
			}

			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'];

			$insertQuery->execute(array($row['id'], $storageId, $internalPath, $pathHash, $parentId, $row['name'], $row['mimetype'], $row['mimepart'], $row['size'], $row['mtime'], $row['encrypted']));

			$permissions = ($row['writable']) ? \OCP\PERMISSION_ALL : \OCP\PERMISSION_READ;
			$permissionsCache = self::getPermissionsCache($storage);
			$permissionsCache->set($row['id'], $row['user'], $permissions);
		}
	}

	/**
	 * @param \OC\Files\Storage\Storage $storage
	 * @return Permissions
	 */
	static function getPermissionsCache($storage) {
		$storageId = $storage->getId();
		if (!isset(self::$permissionsCaches[$storageId])) {
			self::$permissionsCaches[$storageId] = $storage->getPermissionsCache();
		}
		return self::$permissionsCaches[$storageId];
	}

	/**
	 * get the numeric storage id
	 *
	 * @param \OC\Files\Storage\Storage $storage
	 * @return int
	 */
	static function getNumericId($storage) {
		$storageId = $storage->getId();
		if (!isset(self::$numericIds[$storageId])) {
			$cache = new Cache($storage);
			self::$numericIds[$storageId] = $cache->getNumericStorageId();
		}
		return self::$numericIds[$storageId];
	}
}