1
0

BuildCalendarSearchIndexBackgroundJob.php 3.2 KB

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