You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BuildCalendarSearchIndexBackgroundJob.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * @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 OC\BackgroundJob\QueuedJob;
  27. use OCA\DAV\CalDAV\CalDavBackend;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\IDBConnection;
  31. use OCP\ILogger;
  32. class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
  33. /** @var IDBConnection */
  34. private $db;
  35. /** @var CalDavBackend */
  36. private $calDavBackend;
  37. /** @var ILogger */
  38. private $logger;
  39. /** @var IJobList */
  40. private $jobList;
  41. /** @var ITimeFactory */
  42. private $timeFactory;
  43. /**
  44. * @param IDBConnection $db
  45. * @param CalDavBackend $calDavBackend
  46. * @param ILogger $logger
  47. * @param IJobList $jobList
  48. * @param ITimeFactory $timeFactory
  49. */
  50. public function __construct(IDBConnection $db,
  51. CalDavBackend $calDavBackend,
  52. ILogger $logger,
  53. IJobList $jobList,
  54. ITimeFactory $timeFactory) {
  55. $this->db = $db;
  56. $this->calDavBackend = $calDavBackend;
  57. $this->logger = $logger;
  58. $this->jobList = $jobList;
  59. $this->timeFactory = $timeFactory;
  60. }
  61. public function run($arguments) {
  62. $offset = (int) $arguments['offset'];
  63. $stopAt = (int) $arguments['stopAt'];
  64. $this->logger->info('Building calendar index (' . $offset .'/' . $stopAt . ')');
  65. $startTime = $this->timeFactory->getTime();
  66. while (($this->timeFactory->getTime() - $startTime) < 15) {
  67. $offset = $this->buildIndex($offset, $stopAt);
  68. if ($offset >= $stopAt) {
  69. break;
  70. }
  71. }
  72. if ($offset >= $stopAt) {
  73. $this->logger->info('Building calendar index done');
  74. } else {
  75. $this->jobList->add(self::class, [
  76. 'offset' => $offset,
  77. 'stopAt' => $stopAt
  78. ]);
  79. $this->logger->info('New building calendar index job scheduled with offset ' . $offset);
  80. }
  81. }
  82. /**
  83. * @param int $offset
  84. * @param int $stopAt
  85. * @return int
  86. */
  87. private function buildIndex(int $offset, int $stopAt): int {
  88. $query = $this->db->getQueryBuilder();
  89. $query->select(['id', 'calendarid', 'uri', 'calendardata'])
  90. ->from('calendarobjects')
  91. ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
  92. ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
  93. ->orderBy('id', 'ASC')
  94. ->setMaxResults(500);
  95. $result = $query->execute();
  96. while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
  97. $offset = $row['id'];
  98. $calendarData = $row['calendardata'];
  99. if (is_resource($calendarData)) {
  100. $calendarData = stream_get_contents($calendarData);
  101. }
  102. $this->calDavBackend->updateProperties($row['calendarid'], $row['uri'], $calendarData);
  103. }
  104. return $offset;
  105. }
  106. }