Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trigger re-index and optimize query by social field #1744

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions lib/Migration/BuildSocialSearchIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* @copyright 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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');
}
}
6 changes: 1 addition & 5 deletions lib/Service/SocialApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down