BuildSocialSearchIndexBackgroundJob.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\DB\QueryBuilder\IQueryBuilder;
  31. use OCP\IDBConnection;
  32. use Psr\Log\LoggerInterface;
  33. class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
  34. public function __construct(
  35. private IDBConnection $db,
  36. private CardDavBackend $davBackend,
  37. private LoggerInterface $logger,
  38. private IJobList $jobList,
  39. ITimeFactory $timeFactory,
  40. ) {
  41. parent::__construct($timeFactory);
  42. }
  43. public function run($arguments) {
  44. $offset = $arguments['offset'];
  45. $stopAt = $arguments['stopAt'];
  46. $this->logger->info('Indexing social profile data (' . $offset .'/' . $stopAt . ')');
  47. $offset = $this->buildIndex($offset, $stopAt);
  48. if ($offset >= $stopAt) {
  49. $this->logger->info('All contacts with social profiles indexed');
  50. } else {
  51. $this->jobList->add(self::class, [
  52. 'offset' => $offset,
  53. 'stopAt' => $stopAt
  54. ]);
  55. $this->logger->info('New social profile indexing job scheduled with offset ' . $offset);
  56. }
  57. }
  58. /**
  59. * @param int $offset
  60. * @param int $stopAt
  61. * @return int
  62. */
  63. private function buildIndex($offset, $stopAt) {
  64. $startTime = $this->time->getTime();
  65. // get contacts with social profiles
  66. $query = $this->db->getQueryBuilder();
  67. $query->select('id', 'addressbookid', 'uri', 'carddata')
  68. ->from('cards', 'c')
  69. ->orderBy('id', 'ASC')
  70. ->where($query->expr()->like('carddata', $query->createNamedParameter('%SOCIALPROFILE%')))
  71. ->andWhere($query->expr()->gt('id', $query->createNamedParameter((int)$offset, IQueryBuilder::PARAM_INT)))
  72. ->setMaxResults(100);
  73. $social_cards = $query->executeQuery()->fetchAll();
  74. if (empty($social_cards)) {
  75. return $stopAt;
  76. }
  77. // refresh identified contacts in order to re-index
  78. foreach ($social_cards as $contact) {
  79. $offset = $contact['id'];
  80. $cardData = $contact['carddata'];
  81. if (is_resource($cardData) && (get_resource_type($cardData) === 'stream')) {
  82. $cardData = stream_get_contents($cardData);
  83. }
  84. $this->davBackend->updateCard($contact['addressbookid'], $contact['uri'], $cardData);
  85. // stop after 15sec (to be continued with next chunk)
  86. if (($this->time->getTime() - $startTime) > 15) {
  87. break;
  88. }
  89. }
  90. return $offset;
  91. }
  92. }