1
0

BuildSocialSearchIndexBackgroundJob.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @copyright 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
  4. *
  5. * @author call-me-matt <nextcloud@matthiasheinisch.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\Migration;
  24. use OC\BackgroundJob\QueuedJob;
  25. use OCA\DAV\CardDAV\CardDavBackend;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\IJobList;
  28. use OCP\IDBConnection;
  29. use OCP\ILogger;
  30. class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
  31. /** @var IDBConnection */
  32. private $db;
  33. /** @var CardDavBackend */
  34. private $davBackend;
  35. /** @var ILogger */
  36. private $logger;
  37. /** @var IJobList */
  38. private $jobList;
  39. /** @var ITimeFactory */
  40. private $timeFactory;
  41. /**
  42. * @param IDBConnection $db
  43. * @param CardDavBackend $davBackend
  44. * @param ILogger $logger
  45. * @param IJobList $jobList
  46. * @param ITimeFactory $timeFactory
  47. */
  48. public function __construct(IDBConnection $db,
  49. CardDavBackend $davBackend,
  50. ILogger $logger,
  51. IJobList $jobList,
  52. ITimeFactory $timeFactory) {
  53. $this->db = $db;
  54. $this->davBackend = $davBackend;
  55. $this->logger = $logger;
  56. $this->jobList = $jobList;
  57. $this->timeFactory = $timeFactory;
  58. }
  59. public function run($arguments) {
  60. $offset = $arguments['offset'];
  61. $stopAt = $arguments['stopAt'];
  62. $this->logger->info('Indexing social profile data (' . $offset .'/' . $stopAt . ')');
  63. $offset = $this->buildIndex($offset, $stopAt);
  64. if ($offset >= $stopAt) {
  65. $this->logger->info('All contacts with social profiles indexed');
  66. } else {
  67. $this->jobList->add(self::class, [
  68. 'offset' => $offset,
  69. 'stopAt' => $stopAt
  70. ]);
  71. $this->logger->info('New social profile indexing job scheduled with offset ' . $offset);
  72. }
  73. }
  74. /**
  75. * @param int $offset
  76. * @param int $stopAt
  77. * @return int
  78. */
  79. private function buildIndex($offset, $stopAt) {
  80. $startTime = $this->timeFactory->getTime();
  81. // get contacts with social profiles
  82. $query = $this->db->getQueryBuilder();
  83. $query->select('id', 'addressbookid', 'uri', 'carddata')
  84. ->from('cards', 'c')
  85. ->orderBy('id', 'ASC')
  86. ->where($query->expr()->like('carddata', $query->createNamedParameter('%SOCIALPROFILE%')))
  87. ->setMaxResults(100);
  88. $social_cards = $query->execute()->fetchAll();
  89. if (empty($social_cards)) {
  90. return $stopAt;
  91. }
  92. // refresh identified contacts in order to re-index
  93. foreach ($social_cards as $contact) {
  94. $offset = $contact['id'];
  95. $this->davBackend->updateCard($contact['addressbookid'], $contact['uri'], $contact['carddata']);
  96. // stop after 15sec (to be continued with next chunk)
  97. if (($this->timeFactory->getTime() - $startTime) > 15) {
  98. break;
  99. }
  100. }
  101. return $offset;
  102. }
  103. }