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

/lib/private/files/cache/changepropagator.php

https://gitlab.com/Red54/core
PHP | 117 lines | 52 code | 12 blank | 53 comment | 3 complexity | ba4055421bfe1d07e43a358d7384905b MD5 | raw file
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Files\Cache;
  23. use OC\Hooks\BasicEmitter;
  24. /**
  25. * Propagates changes in etag and mtime up the filesystem tree
  26. *
  27. * @package OC\Files\Cache
  28. */
  29. class ChangePropagator extends BasicEmitter {
  30. /**
  31. * @var string[]
  32. */
  33. protected $changedFiles = array();
  34. /**
  35. * @var \OC\Files\View
  36. */
  37. protected $view;
  38. /**
  39. * @param \OC\Files\View $view
  40. */
  41. public function __construct(\OC\Files\View $view) {
  42. $this->view = $view;
  43. }
  44. public function addChange($path) {
  45. $this->changedFiles[] = $path;
  46. }
  47. public function getChanges() {
  48. return $this->changedFiles;
  49. }
  50. /**
  51. * propagate the registered changes to their parent folders
  52. *
  53. * @param int $time (optional) the mtime to set for the folders, if not set the current time is used
  54. */
  55. public function propagateChanges($time = null) {
  56. $parents = $this->getAllParents();
  57. $this->changedFiles = array();
  58. if (!$time) {
  59. $time = time();
  60. }
  61. foreach ($parents as $parent) {
  62. /**
  63. * @var \OC\Files\Storage\Storage $storage
  64. * @var string $internalPath
  65. */
  66. list($storage, $internalPath) = $this->view->resolvePath($parent);
  67. if ($storage) {
  68. $cache = $storage->getCache();
  69. $entry = $cache->get($internalPath);
  70. $cache->update($entry['fileid'], array('mtime' => max($time, $entry['mtime']), 'etag' => $storage->getETag($internalPath)));
  71. $this->emit('\OC\Files', 'propagate', [$parent, $entry]);
  72. }
  73. }
  74. }
  75. /**
  76. * @return string[]
  77. */
  78. public function getAllParents() {
  79. $parents = array();
  80. foreach ($this->getChanges() as $path) {
  81. $parents = array_values(array_unique(array_merge($parents, $this->getParents($path))));
  82. }
  83. return $parents;
  84. }
  85. /**
  86. * get all parent folders of $path
  87. *
  88. * @param string $path
  89. * @return string[]
  90. */
  91. protected function getParents($path) {
  92. $parts = explode('/', $path);
  93. // remove the singe file
  94. array_pop($parts);
  95. $result = array('/');
  96. $resultPath = '';
  97. foreach ($parts as $part) {
  98. if ($part) {
  99. $resultPath .= '/' . $part;
  100. $result[] = $resultPath;
  101. }
  102. }
  103. return $result;
  104. }
  105. }