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());
64 * Load related modules when needed
66 * @param string $cls Name of the class to be loaded
68 * @return boolean hook value; true means continue processing, false means stop.
70 function onAutoload($cls)
72 $dir = dirname(__FILE__);
77 include_once $dir.'/'.$cls.'.php';
79 case 'SearchsubAction':
80 case 'SearchunsubAction':
81 case 'SearchsubsAction':
84 case 'SearchUnsubForm':
85 case 'SearchSubTrackCommand':
86 case 'SearchSubTrackOffCommand':
87 case 'SearchSubTrackingCommand':
88 case 'SearchSubUntrackCommand':
89 include_once $dir.'/'.strtolower($cls).'.php';
99 * @param Net_URL_Mapper $m path-to-action mapper
101 * @return boolean hook value; true means continue processing, false means stop.
103 function onRouterInitialized($m)
105 $m->connect('search/:search/subscribe',
106 array('action' => 'searchsub'),
107 array('search' => Router::REGEX_TAG));
108 $m->connect('search/:search/unsubscribe',
109 array('action' => 'searchunsub'),
110 array('search' => Router::REGEX_TAG));
112 $m->connect(':nickname/search-subscriptions',
113 array('action' => 'searchsubs'),
114 array('nickname' => Nickname::DISPLAY_FMT));
119 * Plugin version data
121 * @param array &$versions array of version data
125 function onPluginVersion(&$versions)
127 $versions[] = array('name' => 'SearchSub',
128 'version' => self::VERSION,
129 'author' => 'Brion Vibber',
130 'homepage' => 'http://status.net/wiki/Plugin:SearchSub',
132 // TRANS: Plugin description.
133 _m('Plugin to allow following all messages with a given search.'));
138 * Hook inbox delivery setup so search subscribers receive all
139 * notices with that search in their inbox.
141 * Currently makes no distinction between local messages and
142 * remote ones which happen to come in to the system. Remote
143 * notices that don't come in at all won't ever reach this.
145 * @param Notice $notice
146 * @param array $ni in/out map of profile IDs to inbox constants
147 * @return boolean hook result
149 function onStartNoticeWhoGets(Notice $notice, array &$ni)
151 // Warning: this is potentially very slow
152 // with a lot of searches!
153 $sub = new SearchSub();
154 $sub->groupBy('search');
156 while ($sub->fetch()) {
157 $search = $sub->search;
159 if ($this->matchSearch($notice, $search)) {
160 // Match? Find all those who subscribed to this
161 // search term and get our delivery on...
162 $searchsub = new SearchSub();
163 $searchsub->search = $search;
166 while ($searchsub->fetch()) {
167 // These constants are currently not actually used, iirc
168 $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
176 * Does the given notice match the given fulltext search query?
178 * Warning: not guaranteed to match other search engine behavior, etc.
179 * Currently using a basic case-insensitive substring match, which
180 * probably fits with the 'LIKE' search but not the default MySQL
181 * or Sphinx search backends.
183 * @param Notice $notice
184 * @param string $search
187 function matchSearch(Notice $notice, $search)
189 return (mb_stripos($notice->content, $search) !== false);
194 * @param NoticeSearchAction $action
196 * @param Notice $notice
197 * @return boolean hook result
199 function onStartNoticeSearchShowResults($action, $q, $notice)
201 $user = common_current_user();
204 $searchsub = SearchSub::pkeyGet(array('search' => $search,
205 'profile_id' => $user->id));
207 $form = new SearchUnsubForm($action, $search);
209 $form = new SearchSubForm($action, $search);
211 $action->elementStart('div', 'entity_actions');
212 $action->elementStart('ul');
213 $action->elementStart('li', 'entity_subscribe');
215 $action->elementEnd('li');
216 $action->elementEnd('ul');
217 $action->elementEnd('div');
223 * Menu item for personal subscriptions/groups area
225 * @param Widget $widget Widget being executed
227 * @return boolean hook return
230 function onEndSubGroupNav($widget)
232 $action = $widget->out;
233 $action_name = $action->trimmed('action');
235 $action->menuItem(common_local_url('searchsubs', array('nickname' => $action->user->nickname)),
236 // TRANS: SearchSub plugin menu item on user settings page.
237 _m('MENU', 'Searches'),
238 // TRANS: SearchSub plugin tooltip for user settings menu item.
239 _m('Configure search subscriptions'),
240 $action_name == 'searchsubs' && $action->arg('nickname') == $action->user->nickname);
246 * Replace the built-in stub track commands with ones that control
247 * search subscriptions.
249 * @param CommandInterpreter $cmd
252 * @param Command $result
253 * @return boolean hook result
255 function onEndInterpretCommand($cmd, $arg, $user, &$result)
257 if ($result instanceof TrackCommand) {
258 $result = new SearchSubTrackCommand($user, $arg);
260 } else if ($result instanceof TrackOffCommand) {
261 $result = new SearchSubTrackOffCommand($user);
263 } else if ($result instanceof TrackingCommand) {
264 $result = new SearchSubTrackingCommand($user);
266 } else if ($result instanceof UntrackCommand) {
267 $result = new SearchSubUntrackCommand($user, $arg);
274 function onHelpCommandMessages($cmd, &$commands)
276 // TRANS: Help message for IM/SMS command "track <word>"
277 $commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
278 // TRANS: Help message for IM/SMS command "untrack <word>"
279 $commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
280 // TRANS: Help message for IM/SMS command "track off"
281 $commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
282 // TRANS: Help message for IM/SMS command "untrack all"
283 $commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
284 // TRANS: Help message for IM/SMS command "tracks"
285 $commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
286 // TRANS: Help message for IM/SMS command "tracking"
287 $commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
290 function onEndDefaultLocalNav($menu, $user)
292 $user = common_current_user();
295 $searches = SearchSub::forProfile($user->getProfile());
297 if (!empty($searches) && count($searches) > 0) {
298 $searchSubMenu = new SearchSubMenu($menu->out, $user, $searches);
299 $menu->submenu(_m('Searches'), $searchSubMenu);