diff options
author | thomascube <thomas@roundcube.net> | 2011-02-14 20:46:48 +0000 |
---|---|---|
committer | thomascube <thomas@roundcube.net> | 2011-02-14 20:46:48 +0000 |
commit | 3e2637351da9559a4aa420004ac90e9fe30477ef (patch) | |
tree | ca7f99df5bba9fc2bf63ab3bb14566dfffa3b607 /program | |
parent | fc2d3de7691a9b1d083eed435c2c327c2b8edbaa (diff) |
Fulltext search over contact fields. Attention: DATABASE SCHEMA CHANGED\!
Diffstat (limited to 'program')
-rw-r--r-- | program/include/rcube_addressbook.php | 21 | ||||
-rw-r--r-- | program/include/rcube_contacts.php | 29 | ||||
-rw-r--r-- | program/include/rcube_ldap.php | 11 | ||||
-rw-r--r-- | program/include/rcube_vcard.php | 12 | ||||
-rw-r--r-- | program/steps/addressbook/search.inc | 2 |
5 files changed, 63 insertions, 12 deletions
diff --git a/program/include/rcube_addressbook.php b/program/include/rcube_addressbook.php index 648ef838e..d1b0f6281 100644 --- a/program/include/rcube_addressbook.php +++ b/program/include/rcube_addressbook.php @@ -32,6 +32,7 @@ abstract class rcube_addressbook const ERROR_NO_CONNECTION = 2; const ERROR_INCOMPLETE = 3; const ERROR_SAVING = 4; + const ERROR_SEARCH = 5; /** public properties (mandatory) */ public $primary_key; @@ -384,6 +385,26 @@ abstract class rcube_addressbook return $out; } + + + /** + * Normalize the given string for fulltext search. + * Currently only optimized for Latin-1 characters; to be extended + * + * @param string Input string (UTF-8) + * @return string Normalized string + */ + protected static function normalize_string($str) + { + $norm = strtolower(strtr(utf8_decode($str), + 'ÇçäâàåéêëèïîìÅÉöôòüûùÿøØáíóúñÑÁÂÀãÃÊËÈÍÎÏÓÔõÕÚÛÙýÝ', + 'ccaaaaeeeeiiiaeooouuuyooaiounnaaaaaeeeiiioooouuuyy')); + + return preg_replace( + array('/[\s;\+\-\/]+/i', '/(\d)\s+(\d)/', '/\s\w{1,3}\s/'), + array(' ', '\\1\\2', ' '), + $norm); + } } diff --git a/program/include/rcube_contacts.php b/program/include/rcube_contacts.php index 9ad4f17bb..8c9810fa9 100644 --- a/program/include/rcube_contacts.php +++ b/program/include/rcube_contacts.php @@ -5,7 +5,7 @@ | program/include/rcube_contacts.php | | | | This file is part of the Roundcube Webmail client | - | Copyright (C) 2006-2010, The Roundcube Dev Team | + | Copyright (C) 2006-2011, The Roundcube Dev Team | | Licensed under the GNU GPL | | | | PURPOSE: | @@ -41,10 +41,11 @@ class rcube_contacts extends rcube_addressbook private $user_id = 0; private $filter = null; private $result = null; - private $search_fields; - private $search_string; private $cache; - private $table_cols = array('name', 'email', 'firstname', 'surname', 'vcard'); + private $table_cols = array('name', 'email', 'firstname', 'surname'); + private $fulltext_cols = array('name', 'firstname', 'surname', 'middlename', 'nickname', + 'jobtitle', 'organization', 'department', 'maidenname', 'email', 'phone', + 'address', 'street', 'locality', 'zipcode', 'region', 'country', 'website', 'im', 'notes'); // public properties public $primary_key = 'contact_id'; @@ -115,8 +116,6 @@ class rcube_contacts extends rcube_addressbook { $this->result = null; $this->filter = null; - $this->search_fields = null; - $this->search_string = null; $this->cache = null; } @@ -253,8 +252,15 @@ class rcube_contacts extends rcube_addressbook $ids = $this->db->array2list($ids, 'integer'); $where[] = 'c.' . $this->primary_key.' IN ('.$ids.')'; } - else if ($strict) + else if ($strict) { $where[] = $this->db->quoteIdentifier($col).' = '.$this->db->quote($value); + } + else if ($col == '*') { + $words = array(); + foreach(explode(" ", self::normalize_string($value)) as $word) + $words[] = $this->db->ilike('words', '%'.$word.'%'); + $where[] = '(' . join(' AND ', $words) . ')'; + } else $where[] = $this->db->ilike($col, '%'.$value.'%'); } @@ -528,15 +534,21 @@ class rcube_contacts extends rcube_addressbook private function convert_save_data($save_data, $record = array()) { $out = array(); + $words = ''; // copy values into vcard object $vcard = new rcube_vcard($record['vcard'] ? $record['vcard'] : $save_data['vcard']); $vcard->reset(); foreach ($save_data as $key => $values) { list($field, $section) = explode(':', $key); + $fulltext = in_array($field, $this->fulltext_cols); foreach ((array)$values as $value) { if (isset($value)) $vcard->set($field, $value, $section); + if ($fulltext && is_array($value)) + $words .= ' ' . self::normalize_string(join(" ", $value)); + else if ($fulltext && strlen($value) >= 3) + $words .= ' ' . self::normalize_string($value); } } $out['vcard'] = $vcard->export(); @@ -552,6 +564,9 @@ class rcube_contacts extends rcube_addressbook // save all e-mails in database column $out['email'] = join(", ", $vcard->email); + // join words for fulltext search + $out['words'] = join(" ", array_unique(explode(" ", $words))); + return $out; } diff --git a/program/include/rcube_ldap.php b/program/include/rcube_ldap.php index 9c9973f25..3cb47480a 100644 --- a/program/include/rcube_ldap.php +++ b/program/include/rcube_ldap.php @@ -407,6 +407,17 @@ class rcube_ldap extends rcube_addressbook $filter = '(|'; $wc = !$strict && $this->prop['fuzzy_search'] ? '*' : ''; + if ($fields != '*') + { + // search_fields are required for fulltext search + if (!$this->prop['search_fields']) + { + $this->set_error(self::ERROR_SEARCH, 'nofulltextsearch'); + $this->result = new rcube_result_set(); + return $this->result; + } + } + if (is_array($this->prop['search_fields'])) { foreach ($this->prop['search_fields'] as $k => $field) diff --git a/program/include/rcube_vcard.php b/program/include/rcube_vcard.php index 82538379f..40544be90 100644 --- a/program/include/rcube_vcard.php +++ b/program/include/rcube_vcard.php @@ -135,11 +135,15 @@ class rcube_vcard $typemap = $this->typemap; // copy name fields to output array - foreach (array('firstname','surname','middlename','nickname','organization') as $col) - $out[$col] = $this->$col; + foreach (array('firstname','surname','middlename','nickname','organization') as $col) { + if (strlen($this->$col)) + $out[$col] = $this->$col; + } - $out['prefix'] = $this->raw['N'][0][3]; - $out['suffix'] = $this->raw['N'][0][4]; + if ($this->raw['N'][0][3]) + $out['prefix'] = $this->raw['N'][0][3]; + if ($this->raw['N'][0][4]) + $out['suffix'] = $this->raw['N'][0][4]; // convert from raw vcard data into associative data for Roundcube foreach (array_flip($this->fieldmap) as $tag => $col) { diff --git a/program/steps/addressbook/search.inc b/program/steps/addressbook/search.inc index 0b4397f6d..7d6775507 100644 --- a/program/steps/addressbook/search.inc +++ b/program/steps/addressbook/search.inc @@ -26,7 +26,7 @@ $search = trim(get_input_value('_q', RCUBE_INPUT_GET)); $search_request = md5('addr'.$search); // get contacts for this user -$result = $CONTACTS->search(array('name','email'), $search); +$result = $CONTACTS->search('*', $search); // save search settings in session $_SESSION['search'][$search_request] = $CONTACTS->get_search_set(); |