BuildCalendarSearchIndexBackgroundJob.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2017 Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Migration;
  27. use OCA\DAV\CalDAV\CalDavBackend;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\BackgroundJob\QueuedJob;
  31. use OCP\IDBConnection;
  32. use Psr\Log\LoggerInterface;
  33. class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
  34. public function __construct(
  35. private IDBConnection $db,
  36. private CalDavBackend $calDavBackend,
  37. private LoggerInterface $logger,
  38. private IJobList $jobList,
  39. ITimeFactory $timeFactory
  40. ) {
  41. parent::__construct($timeFactory);
  42. }
  43. public function run($arguments) {
  44. $offset = (int) $arguments['offset'];
  45. $stopAt = (int) $arguments['stopAt'];
  46. $this->logger->info('Building calendar index (' . $offset .'/' . $stopAt . ')');
  47. $startTime = $this->time->getTime();
  48. while (($this->time->getTime() - $startTime) < 15) {
  49. $offset = $this->buildIndex($offset, $stopAt);
  50. if ($offset >= $stopAt) {
  51. break;
  52. }
  53. }
  54. if ($offset >= $stopAt) {
  55. $this->logger->info('Building calendar index done');
  56. } else {
  57. $this->jobList->add(self::class, [
  58. 'offset' => $offset,
  59. 'stopAt' => $stopAt
  60. ]);
  61. $this->logger->info('New building calendar index job scheduled with offset ' . $offset);
  62. }
  63. }
  64. /**
  65. * @param int $offset
  66. * @param int $stopAt
  67. * @return int
  68. */
  69. private function buildIndex(int $offset, int $stopAt): int {
  70. $query = $this->db->getQueryBuilder();
  71. $query->select(['id', 'calendarid', 'uri', 'calendardata'])
  72. ->from('calendarobjects')
  73. ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
  74. ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
  75. ->orderBy('id', 'ASC')
  76. ->setMaxResults(500);
  77. $result = $query->executeQuery();
  78. while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
  79. $offset = $row['id'];
  80. $calendarData = $row['calendardata'];
  81. if (is_resource($calendarData)) {
  82. $calendarData = stream_get_contents($calendarData);
  83. }
  84. $this->calDavBackend->updateProperties($row['calendarid'], $row['uri'], $calendarData);
  85. }
  86. return $offset;
  87. }
  88. }