1
0

UUIDFixInsert.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\User_LDAP\Migration;
  26. use OCA\User_LDAP\Mapping\GroupMapping;
  27. use OCA\User_LDAP\Mapping\UserMapping;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\IConfig;
  30. use OCP\Migration\IOutput;
  31. use OCP\Migration\IRepairStep;
  32. class UUIDFixInsert implements IRepairStep {
  33. /** @var IConfig */
  34. protected $config;
  35. /** @var UserMapping */
  36. protected $userMapper;
  37. /** @var GroupMapping */
  38. protected $groupMapper;
  39. /** @var IJobList */
  40. protected $jobList;
  41. public function __construct(IConfig $config, UserMapping $userMapper, GroupMapping $groupMapper, IJobList $jobList) {
  42. $this->config = $config;
  43. $this->userMapper = $userMapper;
  44. $this->groupMapper = $groupMapper;
  45. $this->jobList = $jobList;
  46. }
  47. /**
  48. * Returns the step's name
  49. *
  50. * @return string
  51. * @since 9.1.0
  52. */
  53. public function getName() {
  54. return 'Insert UUIDFix background job for user and group in batches';
  55. }
  56. /**
  57. * Run repair step.
  58. * Must throw exception on error.
  59. *
  60. * @param IOutput $output
  61. * @throws \Exception in case of failure
  62. * @since 9.1.0
  63. */
  64. public function run(IOutput $output) {
  65. $installedVersion = $this->config->getAppValue('user_ldap', 'installed_version', '1.2.1');
  66. if (version_compare($installedVersion, '1.2.1') !== -1) {
  67. return;
  68. }
  69. foreach ([$this->userMapper, $this->groupMapper] as $mapper) {
  70. $offset = 0;
  71. $batchSize = 50;
  72. $jobClass = $mapper instanceof UserMapping ? UUIDFixUser::class : UUIDFixGroup::class;
  73. do {
  74. $retry = false;
  75. $records = $mapper->getList($offset, $batchSize);
  76. if (count($records) === 0) {
  77. continue;
  78. }
  79. try {
  80. $this->jobList->add($jobClass, ['records' => $records]);
  81. $offset += $batchSize;
  82. } catch (\InvalidArgumentException $e) {
  83. if (str_contains($e->getMessage(), 'Background job arguments can\'t exceed 4000')) {
  84. $batchSize = (int)floor(count($records) * 0.8);
  85. $retry = true;
  86. }
  87. }
  88. } while (count($records) === $batchSize || $retry);
  89. }
  90. }
  91. }