BuildCalendarSearchIndexBackgroundJob.php 3.4 KB

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