PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/files/cache/legacy.php

https://github.com/sezuan/core
PHP | 136 lines | 83 code | 14 blank | 39 comment | 17 complexity | 894bad7cd1718671e1b482223585be9e MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Cache;
  9. /**
  10. * Provide read only support for the old filecache
  11. */
  12. class Legacy {
  13. private $user;
  14. private $cacheHasItems = null;
  15. public function __construct($user) {
  16. $this->user = $user;
  17. }
  18. /**
  19. * get the numbers of items in the legacy cache
  20. *
  21. * @return int
  22. */
  23. function getCount() {
  24. $sql = 'SELECT COUNT(`id`) AS `count` FROM `*PREFIX*fscache` WHERE `user` = ?';
  25. $result = \OC_DB::executeAudited($sql, array($this->user));
  26. if ($row = $result->fetchRow()) {
  27. return $row['count'];
  28. } else {
  29. return 0;
  30. }
  31. }
  32. /**
  33. * check if a legacy cache is present and holds items
  34. *
  35. * @return bool
  36. */
  37. function hasItems() {
  38. if (!is_null($this->cacheHasItems)) {
  39. return $this->cacheHasItems;
  40. }
  41. try {
  42. $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*fscache` WHERE `user` = ?',1);
  43. } catch (\Exception $e) {
  44. $this->cacheHasItems = false;
  45. return false;
  46. }
  47. try {
  48. $result = $query->execute(array($this->user));
  49. } catch (\Exception $e) {
  50. $this->cacheHasItems = false;
  51. return false;
  52. }
  53. if ($result === false || property_exists($result, 'error_message_prefix')) {
  54. $this->cacheHasItems = false;
  55. return false;
  56. }
  57. $this->cacheHasItems = (bool)$result->fetchRow();
  58. return $this->cacheHasItems;
  59. }
  60. /**
  61. * get an item from the legacy cache
  62. *
  63. * @param string|int $path
  64. * @return array
  65. */
  66. function get($path) {
  67. if (is_numeric($path)) {
  68. $sql = 'SELECT * FROM `*PREFIX*fscache` WHERE `id` = ?';
  69. } else {
  70. $sql = 'SELECT * FROM `*PREFIX*fscache` WHERE `path` = ?';
  71. }
  72. $result = \OC_DB::executeAudited($sql, array($path));
  73. $data = $result->fetchRow();
  74. $data['etag'] = $this->getEtag($data['path'], $data['user']);
  75. return $data;
  76. }
  77. /**
  78. * Get the ETag for the given path
  79. *
  80. * @param type $path
  81. * @return string
  82. */
  83. function getEtag($path, $user = null) {
  84. static $query = null;
  85. $pathDetails = explode('/', $path, 4);
  86. if((!$user) && !isset($pathDetails[1])) {
  87. //no user!? Too odd, return empty string.
  88. return '';
  89. } else if(!$user) {
  90. //guess user from path, if no user passed.
  91. $user = $pathDetails[1];
  92. }
  93. if(!isset($pathDetails[3]) || is_null($pathDetails[3])) {
  94. $relativePath = '';
  95. } else {
  96. $relativePath = $pathDetails[3];
  97. }
  98. if(is_null($query)){
  99. $query = \OC_DB::prepare('SELECT `propertyvalue` FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = \'{DAV:}getetag\'');
  100. }
  101. $result = \OC_DB::executeAudited($query,array($user, '/' . $relativePath));
  102. if ($row = $result->fetchRow()) {
  103. return trim($row['propertyvalue'], '"');
  104. } else {
  105. return '';
  106. }
  107. }
  108. /**
  109. * get all child items of an item from the legacy cache
  110. *
  111. * @param int $id
  112. * @return array
  113. */
  114. function getChildren($id) {
  115. $result = \OC_DB::executeAudited('SELECT * FROM `*PREFIX*fscache` WHERE `parent` = ?', array($id));
  116. $data = $result->fetchAll();
  117. foreach ($data as $i => $item) {
  118. $data[$i]['etag'] = $this->getEtag($item['path'], $item['user']);
  119. }
  120. return $data;
  121. }
  122. }