123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace OCA\DAV\Migration;
- use OC\BackgroundJob\QueuedJob;
- use OCA\DAV\CalDAV\CalDavBackend;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\BackgroundJob\IJobList;
- use OCP\IDBConnection;
- use OCP\ILogger;
- class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
-
- private $db;
-
- private $calDavBackend;
-
- private $logger;
-
- private $jobList;
-
- private $timeFactory;
-
- public function __construct(IDBConnection $db,
- CalDavBackend $calDavBackend,
- ILogger $logger,
- IJobList $jobList,
- ITimeFactory $timeFactory) {
- $this->db = $db;
- $this->calDavBackend = $calDavBackend;
- $this->logger = $logger;
- $this->jobList = $jobList;
- $this->timeFactory = $timeFactory;
- }
- public function run($arguments) {
- $offset = (int) $arguments['offset'];
- $stopAt = (int) $arguments['stopAt'];
- $this->logger->info('Building calendar index (' . $offset .'/' . $stopAt . ')');
- $startTime = $this->timeFactory->getTime();
- while (($this->timeFactory->getTime() - $startTime) < 15) {
- $offset = $this->buildIndex($offset, $stopAt);
- if ($offset >= $stopAt) {
- break;
- }
- }
- if ($offset >= $stopAt) {
- $this->logger->info('Building calendar index done');
- } else {
- $this->jobList->add(self::class, [
- 'offset' => $offset,
- 'stopAt' => $stopAt
- ]);
- $this->logger->info('New building calendar index job scheduled with offset ' . $offset);
- }
- }
-
- private function buildIndex(int $offset, int $stopAt): int {
- $query = $this->db->getQueryBuilder();
- $query->select(['id', 'calendarid', 'uri', 'calendardata'])
- ->from('calendarobjects')
- ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
- ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
- ->orderBy('id', 'ASC')
- ->setMaxResults(500);
- $result = $query->execute();
- while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
- $offset = $row['id'];
- $calendarData = $row['calendardata'];
- if (is_resource($calendarData)) {
- $calendarData = stream_get_contents($calendarData);
- }
- $this->calDavBackend->updateProperties($row['calendarid'], $row['uri'], $calendarData);
- }
- return $offset;
- }
- }
|