diff options
author | Joas Schilling <coding@schilljs.com> | 2020-06-04 10:23:23 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-06-15 08:47:30 +0200 |
commit | 00e7b2b956e2901a9c3baa13af3948f1cd67378e (patch) | |
tree | ca354b9dd5ea7eb22ec9b457d8d23501e5305224 /apps/dav/lib | |
parent | 839f27b50185847056f23d1ab2b081a70e142f7f (diff) | |
download | nextcloud-server-00e7b2b956e2901a9c3baa13af3948f1cd67378e.tar.gz nextcloud-server-00e7b2b956e2901a9c3baa13af3948f1cd67378e.zip |
Reduce load of the contacts search when we know it can't match
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/CardDAV/CardDavBackend.php | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/apps/dav/lib/CardDAV/CardDavBackend.php b/apps/dav/lib/CardDAV/CardDavBackend.php index 40b48f69ae0..8fb48d062d0 100644 --- a/apps/dav/lib/CardDAV/CardDavBackend.php +++ b/apps/dav/lib/CardDAV/CardDavBackend.php @@ -949,20 +949,38 @@ class CardDavBackend implements BackendInterface, SyncSupport { * @return array an array of contacts which are arrays of key-value-pairs */ public function search($addressBookId, $pattern, $searchProperties, $options = []) { - $query2 = $this->db->getQueryBuilder(); + $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; - $query2->selectDistinct('cp.cardid') - ->from($this->dbCardsPropertiesTable, 'cp') - ->andWhere($query2->expr()->eq('cp.addressbookid', $query2->createNamedParameter($addressBookId))); + $query2 = $this->db->getQueryBuilder(); $or = $query2->expr()->orX(); foreach ($searchProperties as $property) { + if ($escapePattern) { + if ($property === 'EMAIL' && strpos($pattern, ' ') !== false) { + // There can be no spaces in emails + continue; + } + + if ($property === 'CLOUD' && preg_match('/[^a-zA-Z0-9 _.@\-\']/', $pattern) === 1) { + // There can be no chars in cloud ids which are not valid for user ids + continue; + } + } + $or->add($query2->expr()->eq('cp.name', $query2->createNamedParameter($property))); } - $query2->andWhere($or); + + if ($or->count() === 0) { + return []; + } + + $query2->selectDistinct('cp.cardid') + ->from($this->dbCardsPropertiesTable, 'cp') + ->andWhere($query2->expr()->eq('cp.addressbookid', $query2->createNamedParameter($addressBookId))) + ->andWhere($or); // No need for like when the pattern is empty if ('' !== $pattern) { - if (\array_key_exists('escape_like_param', $options) && $options['escape_like_param'] === false) { + if (!$escapePattern) { $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter($pattern))); } else { $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |