You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

update.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. $installedVersion = OCP\Config::getAppValue('files_sharing', 'installed_version');
  3. if (version_compare($installedVersion, '0.4', '<')) {
  4. removeSharedFolder();
  5. }
  6. // clean up oc_share table from files which are no longer exists
  7. if (version_compare($installedVersion, '0.3.5.6', '<')) {
  8. \OC\Files\Cache\Shared_Updater::fixBrokenSharesOnAppUpdate();
  9. }
  10. /**
  11. * update script for the removal of the logical "Shared" folder, we create physical "Shared" folder and
  12. * update the users file_target so that it doesn't make any difference for the user
  13. * @note parameters are just for testing, please ignore them
  14. */
  15. function removeSharedFolder($mkdirs = true, $chunkSize = 99) {
  16. $query = OCP\DB::prepare('SELECT * FROM `*PREFIX*share`');
  17. $result = $query->execute();
  18. $view = new \OC\Files\View('/');
  19. $users = array();
  20. $shares = array();
  21. //we need to set up user backends
  22. OC_User::useBackend(new OC_User_Database());
  23. OC_Group::useBackend(new OC_Group_Database());
  24. OC_App::loadApps(array('authentication'));
  25. //we need to set up user backends, otherwise creating the shares will fail with "because user does not exist"
  26. while ($row = $result->fetchRow()) {
  27. //collect all user shares
  28. if ((int)$row['share_type'] === 0 && ($row['item_type'] === 'file' || $row['item_type'] === 'folder')) {
  29. $users[] = $row['share_with'];
  30. $shares[$row['id']] = $row['file_target'];
  31. } else if ((int)$row['share_type'] === 1 && ($row['item_type'] === 'file' || $row['item_type'] === 'folder')) {
  32. //collect all group shares
  33. $users = array_merge($users, \OC_group::usersInGroup($row['share_with']));
  34. $shares[$row['id']] = $row['file_target'];
  35. } else if ((int)$row['share_type'] === 2) {
  36. $shares[$row['id']] = $row['file_target'];
  37. }
  38. }
  39. $unique_users = array_unique($users);
  40. if (!empty($unique_users) && !empty($shares)) {
  41. // create folder Shared for each user
  42. if ($mkdirs) {
  43. foreach ($unique_users as $user) {
  44. \OC\Files\Filesystem::initMountPoints($user);
  45. if (!$view->file_exists('/' . $user . '/files/Shared')) {
  46. $view->mkdir('/' . $user . '/files/Shared');
  47. }
  48. }
  49. }
  50. $chunkedShareList = array_chunk($shares, $chunkSize, true);
  51. foreach ($chunkedShareList as $subList) {
  52. $statement = "UPDATE `*PREFIX*share` SET `file_target` = CASE `id` ";
  53. //update share table
  54. $ids = implode(',', array_keys($subList));
  55. foreach ($subList as $id => $target) {
  56. $statement .= "WHEN " . $id . " THEN '/Shared" . $target . "' ";
  57. }
  58. $statement .= ' END WHERE `id` IN (' . $ids . ')';
  59. $query = OCP\DB::prepare($statement);
  60. $query->execute(array());
  61. }
  62. }
  63. }