From 0bd6f7645159095be3a39e01888eab116b397883 Mon Sep 17 00:00:00 2001 From: millette Date: Thu, 20 Nov 2008 16:13:47 -0500 Subject: sphinx search of people darcs-hash:20081120211347-099f7-d5588788dcd9a16cf72ece59da3d2bf9b8171b85.gz --- classes/Profile.php | 15 ++++++++++ classes/SearchEngines.php | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 classes/SearchEngines.php (limited to 'classes') diff --git a/classes/Profile.php b/classes/Profile.php index 794dc1de9..9de92ec4b 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -46,6 +46,21 @@ class Profile extends Memcached_DataObject /* the code above is auto generated do not remove the tag below */ ###END_AUTOCODE + function getSearchEngine() { + require_once INSTALLDIR.'/classes/SearchEngines.php'; + static $search_engine; + if (!isset($search_engine)) { + if (common_config('sphinx', 'enabled')) { + $search_engine = new SphinxSearch($this); + } elseif ('mysql' === common_config('db', 'type')) { + $search_engine = new MySQLSearch($this); + } else { + $search_engine = new PGSearch($this); + } + } + return $search_engine; + } + function getAvatar($width, $height=NULL) { if (is_null($height)) { $height = $width; diff --git a/classes/SearchEngines.php b/classes/SearchEngines.php new file mode 100644 index 000000000..e5f4e14ea --- /dev/null +++ b/classes/SearchEngines.php @@ -0,0 +1,71 @@ +. + */ + +if (!defined('LACONICA')) { exit(1); } + +class SearchEngine { + protected $profile; + + function __construct($profile) { + $this->profile = $profile; + } + + function query($q) { + } + + function limit($offset, $count) { + return $this->profile->limit($offset, $count); + } +} + +class SphinxSearch extends SearchEngine { + private $sphinx; + + function __construct($profile) { + parent::__construct($profile); + $this->sphinx = new SphinxClient; + $this->sphinx->setServer(common_config('sphinx', 'server'), common_config('sphinx', 'port')); + } + + function limit($offset, $count) { + $this->sphinx->setLimits($offset, $count); + $this->profile->limit($offset, $count); + } + + function query($q) { + $result = $this->sphinx->query($q); + if (!isset($result['matches'])) return false; + $id_set = join(', ', array_keys($result['matches'])); + return $this->profile->whereAdd("id in ($id_set)"); + } +} + +class MySQLSearch extends SearchEngine { + function query($q) { + return $this->profile->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' . + 'against (\''.addslashes($q).'\')'); + } +} + +class PGSearch extends SearchEngine { + function query($q) { + $this->profile->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')'); + } +} + -- cgit v1.2.3-54-g00ecf