BuildSocialSearchIndexBackgroundJob.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Migration;
  8. use OCA\DAV\CardDAV\CardDavBackend;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\BackgroundJob\IJobList;
  11. use OCP\BackgroundJob\QueuedJob;
  12. use OCP\DB\QueryBuilder\IQueryBuilder;
  13. use OCP\IDBConnection;
  14. use Psr\Log\LoggerInterface;
  15. class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
  16. public function __construct(
  17. private IDBConnection $db,
  18. private CardDavBackend $davBackend,
  19. private LoggerInterface $logger,
  20. private IJobList $jobList,
  21. ITimeFactory $timeFactory,
  22. ) {
  23. parent::__construct($timeFactory);
  24. }
  25. public function run($arguments) {
  26. $offset = $arguments['offset'];
  27. $stopAt = $arguments['stopAt'];
  28. $this->logger->info('Indexing social profile data (' . $offset . '/' . $stopAt . ')');
  29. $offset = $this->buildIndex($offset, $stopAt);
  30. if ($offset >= $stopAt) {
  31. $this->logger->info('All contacts with social profiles indexed');
  32. } else {
  33. $this->jobList->add(self::class, [
  34. 'offset' => $offset,
  35. 'stopAt' => $stopAt
  36. ]);
  37. $this->logger->info('New social profile indexing job scheduled with offset ' . $offset);
  38. }
  39. }
  40. /**
  41. * @param int $offset
  42. * @param int $stopAt
  43. * @return int
  44. */
  45. private function buildIndex($offset, $stopAt) {
  46. $startTime = $this->time->getTime();
  47. // get contacts with social profiles
  48. $query = $this->db->getQueryBuilder();
  49. $query->select('id', 'addressbookid', 'uri', 'carddata')
  50. ->from('cards', 'c')
  51. ->orderBy('id', 'ASC')
  52. ->where($query->expr()->like('carddata', $query->createNamedParameter('%SOCIALPROFILE%')))
  53. ->andWhere($query->expr()->gt('id', $query->createNamedParameter((int)$offset, IQueryBuilder::PARAM_INT)))
  54. ->setMaxResults(100);
  55. $social_cards = $query->executeQuery()->fetchAll();
  56. if (empty($social_cards)) {
  57. return $stopAt;
  58. }
  59. // refresh identified contacts in order to re-index
  60. foreach ($social_cards as $contact) {
  61. $offset = $contact['id'];
  62. $this->davBackend->updateCard($contact['addressbookid'], $contact['uri'], $contact['carddata']);
  63. // stop after 15sec (to be continued with next chunk)
  64. if (($this->time->getTime() - $startTime) > 15) {
  65. break;
  66. }
  67. }
  68. return $offset;
  69. }
  70. }