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

/lib/files/cache/backgroundwatcher.php

https://github.com/sezuan/core
PHP | 104 lines | 80 code | 9 blank | 15 comment | 15 complexity | c66b09bfb2e1b70a33288ccaf9fe9dfa MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Copyright (c) 2013 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. use \OC\Files\Mount;
  10. use \OC\Files\Filesystem;
  11. class BackgroundWatcher {
  12. static $folderMimetype = null;
  13. static private function getFolderMimetype() {
  14. if (!is_null(self::$folderMimetype)) {
  15. return self::$folderMimetype;
  16. }
  17. $sql = 'SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?';
  18. $result = \OC_DB::executeAudited($sql, array('httpd/unix-directory'));
  19. $row = $result->fetchRow();
  20. return $row['id'];
  21. }
  22. static private function checkUpdate($id) {
  23. $cacheItem = Cache::getById($id);
  24. if (is_null($cacheItem)) {
  25. return;
  26. }
  27. list($storageId, $internalPath) = $cacheItem;
  28. $mounts = Filesystem::getMountByStorageId($storageId);
  29. if (count($mounts) === 0) {
  30. //if the storage we need isn't mounted on default, try to find a user that has access to the storage
  31. $permissionsCache = new Permissions($storageId);
  32. $users = $permissionsCache->getUsers($id);
  33. if (count($users) === 0) {
  34. return;
  35. }
  36. Filesystem::initMountPoints($users[0]);
  37. $mounts = Filesystem::getMountByStorageId($storageId);
  38. if (count($mounts) === 0) {
  39. return;
  40. }
  41. }
  42. $storage = $mounts[0]->getStorage();
  43. $watcher = new Watcher($storage);
  44. $watcher->checkUpdate($internalPath);
  45. }
  46. /**
  47. * get the next fileid in the cache
  48. *
  49. * @param int $previous
  50. * @param bool $folder
  51. * @return int
  52. */
  53. static private function getNextFileId($previous, $folder) {
  54. if ($folder) {
  55. $stmt = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `fileid` > ? AND `mimetype` = ? ORDER BY `fileid` ASC', 1);
  56. } else {
  57. $stmt = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `fileid` > ? AND `mimetype` != ? ORDER BY `fileid` ASC', 1);
  58. }
  59. $result = \OC_DB::executeAudited($stmt, array($previous,self::getFolderMimetype()));
  60. if ($row = $result->fetchRow()) {
  61. return $row['fileid'];
  62. } else {
  63. return 0;
  64. }
  65. }
  66. static public function checkNext() {
  67. // check both 1 file and 1 folder, this way new files are detected quicker because there are less folders than files usually
  68. $previousFile = \OC_Appconfig::getValue('files', 'backgroundwatcher_previous_file', 0);
  69. $previousFolder = \OC_Appconfig::getValue('files', 'backgroundwatcher_previous_folder', 0);
  70. $nextFile = self::getNextFileId($previousFile, false);
  71. $nextFolder = self::getNextFileId($previousFolder, true);
  72. \OC_Appconfig::setValue('files', 'backgroundwatcher_previous_file', $nextFile);
  73. \OC_Appconfig::setValue('files', 'backgroundwatcher_previous_folder', $nextFolder);
  74. if ($nextFile > 0) {
  75. self::checkUpdate($nextFile);
  76. }
  77. if ($nextFolder > 0) {
  78. self::checkUpdate($nextFolder);
  79. }
  80. }
  81. static public function checkAll() {
  82. $previous = 0;
  83. $next = 1;
  84. while ($next != 0) {
  85. $next = self::getNextFileId($previous, true);
  86. self::checkUpdate($next);
  87. }
  88. $previous = 0;
  89. $next = 1;
  90. while ($next != 0) {
  91. $next = self::getNextFileId($previous, false);
  92. self::checkUpdate($next);
  93. }
  94. }
  95. }