tag -> search stuff: basic search subscription implementation in SearchSub

This commit is contained in:
Brion Vibber 2011-03-15 15:34:06 -07:00
parent 08b430a247
commit 341bef5e01

View File

@ -139,31 +139,61 @@ class SearchSubPlugin extends Plugin
*/ */
function onStartNoticeWhoGets(Notice $notice, array &$ni) function onStartNoticeWhoGets(Notice $notice, array &$ni)
{ {
foreach ($notice->getTags() as $search) { // Warning: this is potentially very slow
$searchsub = new SearchSub(); // with a lot of searches!
$searchsub->search = $search; $sub = new SearchSub();
$searchsub->find(); $sub->groupBy('search');
$sub->find();
while ($sub->fetch()) {
$search = $sub->search;
while ($searchsub->fetch()) { if ($this->matchSearch($notice, $search)) {
// These constants are currently not actually used, iirc // Match? Find all those who subscribed to this
$ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB; // search term and get our delivery on...
$searchsub = new SearchSub();
$searchsub->search = $search;
$searchsub->find();
while ($searchsub->fetch()) {
// These constants are currently not actually used, iirc
$ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
}
} }
} }
return true; return true;
} }
/** /**
* Does the given notice match the given fulltext search query?
* *
* @param SearchAction $action * Warning: not guaranteed to match other search engine behavior, etc.
* Currently using a basic case-insensitive substring match, which
* probably fits with the 'LIKE' search but not the default MySQL
* or Sphinx search backends.
*
* @param Notice $notice
* @param string $search
* @return boolean
*/
function matchSearch(Notice $notice, $search)
{
return (mb_stripos($notice->content, $search) !== false);
}
/**
*
* @param NoticeSearchAction $action
* @param string $q
* @param Notice $notice
* @return boolean hook result * @return boolean hook result
*/ */
function onStartTagShowContent(SearchAction $action) function onStartNoticeSearchShowResults($action, $q, $notice)
{ {
$user = common_current_user(); $user = common_current_user();
if ($user) { if ($user) {
$search = $action->trimmed('search'); $search = $q;
$searchsub = SearchSub::pkeyGet(array('search' => $search, $searchsub = SearchSub::pkeyGet(array('search' => $search,
'profile_id' => $user->id)); 'profile_id' => $user->id));
if ($searchsub) { if ($searchsub) {
$form = new SearchUnsubForm($action, $search); $form = new SearchUnsubForm($action, $search);
} else { } else {