BuildCalendarSearchIndex.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Migration;
  7. use OCP\BackgroundJob\IJobList;
  8. use OCP\IConfig;
  9. use OCP\IDBConnection;
  10. use OCP\Migration\IOutput;
  11. use OCP\Migration\IRepairStep;
  12. class BuildCalendarSearchIndex implements IRepairStep {
  13. /** @var IDBConnection */
  14. private $db;
  15. /** @var IJobList */
  16. private $jobList;
  17. /** @var IConfig */
  18. private $config;
  19. /**
  20. * @param IDBConnection $db
  21. * @param IJobList $jobList
  22. * @param IConfig $config
  23. */
  24. public function __construct(IDBConnection $db,
  25. IJobList $jobList,
  26. IConfig $config) {
  27. $this->db = $db;
  28. $this->jobList = $jobList;
  29. $this->config = $config;
  30. }
  31. /**
  32. * @return string
  33. */
  34. public function getName() {
  35. return 'Registering building of calendar search index as background job';
  36. }
  37. /**
  38. * @param IOutput $output
  39. */
  40. public function run(IOutput $output) {
  41. // only run once
  42. if ($this->config->getAppValue('dav', 'buildCalendarSearchIndex') === 'yes') {
  43. $output->info('Repair step already executed');
  44. return;
  45. }
  46. $query = $this->db->getQueryBuilder();
  47. $query->select($query->createFunction('MAX(' . $query->getColumnName('id') . ')'))
  48. ->from('calendarobjects');
  49. $result = $query->execute();
  50. $maxId = (int) $result->fetchOne();
  51. $result->closeCursor();
  52. $output->info('Add background job');
  53. $this->jobList->add(BuildCalendarSearchIndexBackgroundJob::class, [
  54. 'offset' => 0,
  55. 'stopAt' => $maxId
  56. ]);
  57. // if all were done, no need to redo the repair during next upgrade
  58. $this->config->setAppValue('dav', 'buildCalendarSearchIndex', 'yes');
  59. }
  60. }