123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class SearchSubPlugin extends Plugin
- {
- const VERSION = '0.1';
-
- function onCheckSchema()
- {
- $schema = Schema::get();
- $schema->ensureTable('searchsub', SearchSub::schemaDef());
- return true;
- }
-
- public function onRouterInitialized(URLMapper $m)
- {
- $m->connect('search/:search/subscribe',
- array('action' => 'searchsub'),
- array('search' => Router::REGEX_TAG));
- $m->connect('search/:search/unsubscribe',
- array('action' => 'searchunsub'),
- array('search' => Router::REGEX_TAG));
- $m->connect(':nickname/search-subscriptions',
- array('action' => 'searchsubs'),
- array('nickname' => Nickname::DISPLAY_FMT));
- return true;
- }
-
- function onPluginVersion(&$versions)
- {
- $versions[] = array('name' => 'SearchSub',
- 'version' => self::VERSION,
- 'author' => 'Brion Vibber',
- 'homepage' => 'http://status.net/wiki/Plugin:SearchSub',
- 'rawdescription' =>
-
- _m('Plugin to allow following all messages with a given search.'));
- return true;
- }
-
- function onStartNoticeWhoGets(Notice $notice, array &$ni)
- {
-
-
- $sub = new SearchSub();
- $sub->groupBy('search');
- $sub->find();
- while ($sub->fetch()) {
- $search = $sub->search;
- if ($this->matchSearch($notice, $search)) {
-
-
- $searchsub = new SearchSub();
- $searchsub->search = $search;
- $searchsub->find();
- while ($searchsub->fetch()) {
-
- $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
- }
- }
- }
- return true;
- }
-
- function matchSearch(Notice $notice, $search)
- {
- return (mb_stripos($notice->content, $search) !== false);
- }
-
- function onStartNoticeSearchShowResults($action, $q, $notice)
- {
- $user = common_current_user();
- if ($user) {
- $search = $q;
- $searchsub = SearchSub::pkeyGet(array('search' => $search,
- 'profile_id' => $user->id));
- if ($searchsub) {
- $form = new SearchUnsubForm($action, $search);
- } else {
- $form = new SearchSubForm($action, $search);
- }
- $action->elementStart('div', 'entity_actions');
- $action->elementStart('ul');
- $action->elementStart('li', 'entity_subscribe');
- $form->show();
- $action->elementEnd('li');
- $action->elementEnd('ul');
- $action->elementEnd('div');
- }
- return true;
- }
-
- function onEndSubGroupNav($widget)
- {
- $action = $widget->out;
- $action_name = $action->trimmed('action');
- $action->menuItem(common_local_url('searchsubs', array('nickname' => $action->user->nickname)),
-
- _m('MENU', 'Searches'),
-
- _m('Configure search subscriptions'),
- $action_name == 'searchsubs' && $action->arg('nickname') == $action->user->nickname);
- return true;
- }
-
- function onEndInterpretCommand($cmd, $arg, $user, &$result)
- {
- if ($result instanceof TrackCommand) {
- $result = new SearchSubTrackCommand($user, $arg);
- return false;
- } else if ($result instanceof TrackOffCommand) {
- $result = new SearchSubTrackOffCommand($user);
- return false;
- } else if ($result instanceof TrackingCommand) {
- $result = new SearchSubTrackingCommand($user);
- return false;
- } else if ($result instanceof UntrackCommand) {
- $result = new SearchSubUntrackCommand($user, $arg);
- return false;
- } else {
- return true;
- }
- }
- function onHelpCommandMessages($cmd, &$commands)
- {
-
- $commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
-
- $commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
-
- $commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
-
- $commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
-
- $commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
-
- $commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
- }
- function onEndDefaultLocalNav($menu, $user)
- {
- $user = common_current_user();
- if (!empty($user)) {
- $searches = SearchSub::forProfile($user->getProfile());
- if (!empty($searches) && count($searches) > 0) {
- $searchSubMenu = new SearchSubMenu($menu->out, $user, $searches);
-
- $menu->submenu(_m('MENU','Searches'), $searchSubMenu);
- }
- }
- return true;
- }
- }
|