BuildSocialSearchIndexBackgroundJob.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
  5. *
  6. * @author call-me-matt <nextcloud@matthiasheinisch.de>
  7. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  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 OCA\DAV\CardDAV\CardDavBackend;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\BackgroundJob\QueuedJob;
  30. use OCP\IDBConnection;
  31. use Psr\Log\LoggerInterface;
  32. class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
  33. public function __construct(
  34. private IDBConnection $db,
  35. private CardDavBackend $davBackend,
  36. private LoggerInterface $logger,
  37. private IJobList $jobList,
  38. ITimeFactory $timeFactory,
  39. ) {
  40. parent::__construct($timeFactory);
  41. }
  42. public function run($arguments) {
  43. $offset = $arguments['offset'];
  44. $stopAt = $arguments['stopAt'];
  45. $this->logger->info('Indexing social profile data (' . $offset .'/' . $stopAt . ')');
  46. $offset = $this->buildIndex($offset, $stopAt);
  47. if ($offset >= $stopAt) {
  48. $this->logger->info('All contacts with social profiles indexed');
  49. } else {
  50. $this->jobList->add(self::class, [
  51. 'offset' => $offset,
  52. 'stopAt' => $stopAt
  53. ]);
  54. $this->logger->info('New social profile indexing job scheduled with offset ' . $offset);
  55. }
  56. }
  57. /**
  58. * @param int $offset
  59. * @param int $stopAt
  60. * @return int
  61. */
  62. private function buildIndex($offset, $stopAt) {
  63. $startTime = $this->time->getTime();
  64. // get contacts with social profiles
  65. $query = $this->db->getQueryBuilder();
  66. $query->select('id', 'addressbookid', 'uri', 'carddata')
  67. ->from('cards', 'c')
  68. ->orderBy('id', 'ASC')
  69. ->where($query->expr()->like('carddata', $query->createNamedParameter('%SOCIALPROFILE%')))
  70. ->setMaxResults(100);
  71. $social_cards = $query->executeQuery()->fetchAll();
  72. if (empty($social_cards)) {
  73. return $stopAt;
  74. }
  75. // refresh identified contacts in order to re-index
  76. foreach ($social_cards as $contact) {
  77. $offset = $contact['id'];
  78. $this->davBackend->updateCard($contact['addressbookid'], $contact['uri'], $contact['carddata']);
  79. // stop after 15sec (to be continued with next chunk)
  80. if (($this->time->getTime() - $startTime) > 15) {
  81. break;
  82. }
  83. }
  84. return $offset;
  85. }
  86. }