1
0

BuildCalendarSearchIndexBackgroundJob.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 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 = $arguments['offset'];
  63. $stopAt = $arguments['stopAt'];
  64. $this->logger->info('Building calendar index (' . $offset .'/' . $stopAt . ')');
  65. $offset = $this->buildIndex($offset, $stopAt);
  66. if ($offset >= $stopAt) {
  67. $this->logger->info('Building calendar index done');
  68. } else {
  69. $this->jobList->add(self::class, [
  70. 'offset' => $offset,
  71. 'stopAt' => $stopAt
  72. ]);
  73. $this->logger->info('New building calendar index job scheduled with offset ' . $offset);
  74. }
  75. }
  76. /**
  77. * @param int $offset
  78. * @param int $stopAt
  79. * @return int
  80. */
  81. private function buildIndex($offset, $stopAt) {
  82. $startTime = $this->timeFactory->getTime();
  83. $query = $this->db->getQueryBuilder();
  84. $query->select(['id', 'calendarid', 'uri', 'calendardata'])
  85. ->from('calendarobjects')
  86. ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
  87. ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
  88. ->orderBy('id', 'ASC');
  89. $stmt = $query->execute();
  90. while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  91. $offset = $row['id'];
  92. $calendarData = $row['calendardata'];
  93. if (is_resource($calendarData)) {
  94. $calendarData = stream_get_contents($calendarData);
  95. }
  96. $this->calDavBackend->updateProperties($row['calendarid'], $row['uri'], $calendarData);
  97. if (($this->timeFactory->getTime() - $startTime) > 15) {
  98. return $offset;
  99. }
  100. }
  101. return $stopAt;
  102. }
  103. }