BuildCalendarSearchIndex.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @copyright 2017 Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Migration;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\IConfig;
  30. use OCP\IDBConnection;
  31. use OCP\Migration\IOutput;
  32. use OCP\Migration\IRepairStep;
  33. class BuildCalendarSearchIndex implements IRepairStep {
  34. /** @var IDBConnection */
  35. private $db;
  36. /** @var IJobList */
  37. private $jobList;
  38. /** @var IConfig */
  39. private $config;
  40. /**
  41. * @param IDBConnection $db
  42. * @param IJobList $jobList
  43. * @param IConfig $config
  44. */
  45. public function __construct(IDBConnection $db,
  46. IJobList $jobList,
  47. IConfig $config) {
  48. $this->db = $db;
  49. $this->jobList = $jobList;
  50. $this->config = $config;
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function getName() {
  56. return 'Registering building of calendar search index as background job';
  57. }
  58. /**
  59. * @param IOutput $output
  60. */
  61. public function run(IOutput $output) {
  62. // only run once
  63. if ($this->config->getAppValue('dav', 'buildCalendarSearchIndex') === 'yes') {
  64. $output->info('Repair step already executed');
  65. return;
  66. }
  67. $query = $this->db->getQueryBuilder();
  68. $query->select($query->createFunction('MAX(' . $query->getColumnName('id') . ')'))
  69. ->from('calendarobjects');
  70. $result = $query->execute();
  71. $maxId = (int) $result->fetchOne();
  72. $result->closeCursor();
  73. $output->info('Add background job');
  74. $this->jobList->add(BuildCalendarSearchIndexBackgroundJob::class, [
  75. 'offset' => 0,
  76. 'stopAt' => $maxId
  77. ]);
  78. // if all were done, no need to redo the repair during next upgrade
  79. $this->config->setAppValue('dav', 'buildCalendarSearchIndex', 'yes');
  80. }
  81. }