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