From 4034b35874df478f837160d3b030f821c36e1328 Mon Sep 17 00:00:00 2001 From: call-me-matt Date: Tue, 11 Aug 2020 23:38:06 +0200 Subject: [PATCH] trigger re-index and optimize query by social field Signed-off-by: call-me-matt --- lib/Migration/BuildSocialSearchIndex.php | 91 ++++++++++++++++++++++++ lib/Service/SocialApiService.php | 6 +- 2 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 lib/Migration/BuildSocialSearchIndex.php diff --git a/lib/Migration/BuildSocialSearchIndex.php b/lib/Migration/BuildSocialSearchIndex.php new file mode 100644 index 000000000..a2bab0e01 --- /dev/null +++ b/lib/Migration/BuildSocialSearchIndex.php @@ -0,0 +1,91 @@ + + * + * @author Matthias Heinisch + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Contacts\Migration; + +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\IRepairStep; + +use OCA\DAV\CardDAV\CardDavBackend; + +class BuildSocialSearchIndex implements IRepairStep { + + /** @var IDBConnection */ + private $db; + /** @var IConfig */ + private $config; + /** @var CardDavBackend */ + private $davBackend; + + /** + * @param IDBConnection $db + * @param IConfig $config + * @param CardDavBackend $davBackend + */ + public function __construct(IDBConnection $db, + IConfig $config, + CardDavBackend $davBackend) { + $this->db = $db; + $this->config = $config; + $this->davBackend = $davBackend; + } + + /** + * @return string + */ + public function getName() { + return 'Adding social profile data to search index'; + } + + /** + * @param IOutput $output + */ + public function run(IOutput $output) { + // only run once + if ($this->config->getAppValue('contacts', 'builtSocialSearchIndex') === 'yes') { + $output->info('Repair step already executed'); + return; + } + + // get contacts with social profiles + $query = $this->db->getQueryBuilder(); + // TODO: return contacts with multiple social profiles only once + // FIXME: distinct seems only to return the first parameter? + // $query->selectDistinct('c.addressbookid', 'c.uri', 'c.carddata') + $query->select('c.addressbookid', 'c.uri', 'c.carddata') + ->from('cards_properties', 'p') + ->leftJoin('p', 'cards', 'c', 'c.id = p.cardid') + ->where('p.name=\'X-SOCIALPROFILE\''); + $social_cards = $query->execute(); + + // refresh identified contacts in order to re-index + while ($row = $social_cards->fetch(\PDO::FETCH_ASSOC)) { + $this->davBackend->updateCard($row['addressbookid'], $row['uri'], $row['carddata']); + } + + // no need to redo the repair during next upgrade + $this->config->setAppValue('contacts', 'builtSocialSearchIndex', 'yes'); + } +} diff --git a/lib/Service/SocialApiService.php b/lib/Service/SocialApiService.php index f0339c073..bf650f5d5 100644 --- a/lib/Service/SocialApiService.php +++ b/lib/Service/SocialApiService.php @@ -291,11 +291,7 @@ public function updateAddressbooks(string $network, string $userId) : JSONRespon } // get contacts in that addressbook - $contacts = $addressBook->search('', ['UID'], ['types' => true]); - // TODO: can be optimized by: - // $contacts = $addressBook->search('', ['X-SOCIALPROFILE'], ['types' => true]); - // but see https://github.com/nextcloud/contacts/pull/1722#discussion_r463782429 - // and the referenced PR before activating this (index has to be re-created!) + $contacts = $addressBook->search('', ['X-SOCIALPROFILE'], ['types' => true]); // update one contact after another foreach ($contacts as $contact) {