3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * A plugin to enable local tab subscription
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @category SearchSubPlugin
25 * @author Brion Vibber <brion@status.net>
26 * @copyright 2011 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
36 * SearchSub plugin main class
38 * @category SearchSubPlugin
40 * @author Brion Vibber <brionv@status.net>
41 * @copyright 2011 StatusNet, Inc.
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
43 * @link http://status.net/
45 class SearchSubPlugin extends Plugin
47 const VERSION = '0.1';
50 * Database schema setup
54 * @return boolean hook value; true means continue processing, false means stop.
56 function onCheckSchema()
58 $schema = Schema::get();
59 $schema->ensureTable('searchsub', SearchSub::schemaDef());
66 * @param URLMapper $m path-to-action mapper
68 * @return boolean hook value; true means continue processing, false means stop.
70 public function onRouterInitialized(URLMapper $m)
72 $m->connect('search/:search/subscribe',
73 array('action' => 'searchsub'),
74 array('search' => Router::REGEX_TAG));
75 $m->connect('search/:search/unsubscribe',
76 array('action' => 'searchunsub'),
77 array('search' => Router::REGEX_TAG));
78 $m->connect(':nickname/search-subscriptions',
79 array('action' => 'searchsubs'),
80 array('nickname' => Nickname::DISPLAY_FMT));
87 * @param array &$versions array of version data
91 function onPluginVersion(array &$versions)
93 $versions[] = array('name' => 'SearchSub',
94 'version' => self::VERSION,
95 'author' => 'Brion Vibber',
96 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/SearchSub',
98 // TRANS: Plugin description.
99 _m('Plugin to allow following all messages with a given search.'));
104 * Hook inbox delivery setup so search subscribers receive all
105 * notices with that search in their inbox.
107 * Currently makes no distinction between local messages and
108 * remote ones which happen to come in to the system. Remote
109 * notices that don't come in at all won't ever reach this.
111 * @param Notice $notice
112 * @param array $ni in/out map of profile IDs to inbox constants
113 * @return boolean hook result
115 function onStartNoticeWhoGets(Notice $notice, array &$ni)
117 // Warning: this is potentially very slow
118 // with a lot of searches!
119 $sub = new SearchSub();
120 $sub->groupBy('search');
122 while ($sub->fetch()) {
123 $search = $sub->search;
125 if ($this->matchSearch($notice, $search)) {
126 // Match? Find all those who subscribed to this
127 // search term and get our delivery on...
128 $searchsub = new SearchSub();
129 $searchsub->search = $search;
132 while ($searchsub->fetch()) {
133 // These constants are currently not actually used, iirc
134 $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
142 * Does the given notice match the given fulltext search query?
144 * Warning: not guaranteed to match other search engine behavior, etc.
145 * Currently using a basic case-insensitive substring match, which
146 * probably fits with the 'LIKE' search but not the default MySQL
147 * or Sphinx search backends.
149 * @param Notice $notice
150 * @param string $search
153 function matchSearch(Notice $notice, $search)
155 return (mb_stripos($notice->content, $search) !== false);
160 * @param NoticeSearchAction $action
162 * @param Notice $notice
163 * @return boolean hook result
165 function onStartNoticeSearchShowResults($action, $q, $notice)
167 $user = common_current_user();
170 $searchsub = SearchSub::pkeyGet(array('search' => $search,
171 'profile_id' => $user->id));
173 $form = new SearchUnsubForm($action, $search);
175 $form = new SearchSubForm($action, $search);
177 $action->elementStart('div', 'entity_actions');
178 $action->elementStart('ul');
179 $action->elementStart('li', 'entity_subscribe');
181 $action->elementEnd('li');
182 $action->elementEnd('ul');
183 $action->elementEnd('div');
189 * Menu item for personal subscriptions/groups area
191 * @param Widget $widget Widget being executed
193 * @return boolean hook return
195 function onEndSubGroupNav($widget)
197 $action = $widget->out;
198 $action_name = $action->trimmed('action');
200 $action->menuItem(common_local_url('searchsubs', array('nickname' => $action->user->nickname)),
201 // TRANS: SearchSub plugin menu item on user settings page.
202 _m('MENU', 'Searches'),
203 // TRANS: SearchSub plugin tooltip for user settings menu item.
204 _m('Configure search subscriptions'),
205 $action_name == 'searchsubs' && $action->arg('nickname') == $action->user->nickname);
211 * Replace the built-in stub track commands with ones that control
212 * search subscriptions.
214 * @param CommandInterpreter $cmd
217 * @param Command $result
218 * @return boolean hook result
220 function onEndInterpretCommand($cmd, $arg, $user, &$result)
222 if ($result instanceof TrackCommand) {
223 $result = new SearchSubTrackCommand($user, $arg);
225 } else if ($result instanceof TrackOffCommand) {
226 $result = new SearchSubTrackOffCommand($user);
228 } else if ($result instanceof TrackingCommand) {
229 $result = new SearchSubTrackingCommand($user);
231 } else if ($result instanceof UntrackCommand) {
232 $result = new SearchSubUntrackCommand($user, $arg);
239 function onHelpCommandMessages($cmd, &$commands)
241 // TRANS: Help message for IM/SMS command "track <word>"
242 $commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
243 // TRANS: Help message for IM/SMS command "untrack <word>"
244 $commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
245 // TRANS: Help message for IM/SMS command "track off"
246 $commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
247 // TRANS: Help message for IM/SMS command "untrack all"
248 $commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
249 // TRANS: Help message for IM/SMS command "tracks"
250 $commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
251 // TRANS: Help message for IM/SMS command "tracking"
252 $commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
255 function onEndDefaultLocalNav($menu, $user)
257 $user = common_current_user();
260 $searches = SearchSub::forProfile($user->getProfile());
262 if (!empty($searches) && count($searches) > 0) {
263 $searchSubMenu = new SearchSubMenu($menu->out, $user, $searches);
264 // TRANS: Sub menu for searches.
265 $menu->submenu(_m('MENU','Searches'), $searchSubMenu);