BuildCalendarSearchIndex.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * @copyright 2017 Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\DAV\Migration;
  26. use OCP\BackgroundJob\IJobList;
  27. use OCP\IConfig;
  28. use OCP\IDBConnection;
  29. use OCP\Migration\IOutput;
  30. use OCP\Migration\IRepairStep;
  31. class BuildCalendarSearchIndex implements IRepairStep {
  32. /** @var IDBConnection */
  33. private $db;
  34. /** @var IJobList */
  35. private $jobList;
  36. /** @var IConfig */
  37. private $config;
  38. /**
  39. * @param IDBConnection $db
  40. * @param IJobList $jobList
  41. * @param IConfig $config
  42. */
  43. public function __construct(IDBConnection $db,
  44. IJobList $jobList,
  45. IConfig $config) {
  46. $this->db = $db;
  47. $this->jobList = $jobList;
  48. $this->config = $config;
  49. }
  50. /**
  51. * @return string
  52. */
  53. public function getName() {
  54. return 'Registering building of calendar search index as background job';
  55. }
  56. /**
  57. * @param IOutput $output
  58. */
  59. public function run(IOutput $output) {
  60. // only run once
  61. if ($this->config->getAppValue('dav', 'buildCalendarSearchIndex') === 'yes') {
  62. $output->info('Repair step already executed');
  63. return;
  64. }
  65. $query = $this->db->getQueryBuilder();
  66. $query->select($query->createFunction('MAX(' . $query->getColumnName('id') . ')'))
  67. ->from('calendarobjects');
  68. $maxId = (int)$query->execute()->fetchColumn();
  69. $output->info('Add background job');
  70. $this->jobList->add(BuildCalendarSearchIndexBackgroundJob::class, [
  71. 'offset' => 0,
  72. 'stopAt' => $maxId
  73. ]);
  74. // if all were done, no need to redo the repair during next upgrade
  75. $this->config->setAppValue('dav', 'buildCalendarSearchIndex', 'yes');
  76. }
  77. }