]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SearchSub/SearchSubPlugin.php
Merge branch 'testing' into 1.0.x
[quix0rs-gnu-social.git] / plugins / SearchSub / SearchSubPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * A plugin to enable local tab subscription
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  SearchSubPlugin
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * SearchSub plugin main class
37  *
38  * @category  SearchSubPlugin
39  * @package   StatusNet
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/
44  */
45 class SearchSubPlugin extends Plugin
46 {
47     const VERSION         = '0.1';
48
49     /**
50      * Database schema setup
51      *
52      * @see Schema
53      *
54      * @return boolean hook value; true means continue processing, false means stop.
55      */
56     function onCheckSchema()
57     {
58         $schema = Schema::get();
59         $schema->ensureTable('searchsub', SearchSub::schemaDef());
60         return true;
61     }
62
63     /**
64      * Load related modules when needed
65      *
66      * @param string $cls Name of the class to be loaded
67      *
68      * @return boolean hook value; true means continue processing, false means stop.
69      */
70     function onAutoload($cls)
71     {
72         $dir = dirname(__FILE__);
73
74         switch ($cls)
75         {
76         case 'SearchSub':
77             include_once $dir.'/'.$cls.'.php';
78             return false;
79         case 'SearchsubAction':
80         case 'SearchunsubAction':
81         case 'SearchsubsAction':
82         case 'SearchSubForm':
83         case 'SearchSubMenu':
84         case 'SearchUnsubForm':
85         case 'SearchSubTrackCommand':
86         case 'SearchSubTrackOffCommand':
87         case 'SearchSubTrackingCommand':
88         case 'SearchSubUntrackCommand':
89             include_once $dir.'/'.strtolower($cls).'.php';
90             return false;
91         default:
92             return true;
93         }
94     }
95
96     /**
97      * Map URLs to actions
98      *
99      * @param Net_URL_Mapper $m path-to-action mapper
100      *
101      * @return boolean hook value; true means continue processing, false means stop.
102      */
103     function onRouterInitialized($m)
104     {
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));
111
112         $m->connect(':nickname/search-subscriptions',
113                     array('action' => 'searchsubs'),
114                     array('nickname' => Nickname::DISPLAY_FMT));
115         return true;
116     }
117
118     /**
119      * Plugin version data
120      *
121      * @param array &$versions array of version data
122      *
123      * @return value
124      */
125     function onPluginVersion(&$versions)
126     {
127         $versions[] = array('name' => 'SearchSub',
128                             'version' => self::VERSION,
129                             'author' => 'Brion Vibber',
130                             'homepage' => 'http://status.net/wiki/Plugin:SearchSub',
131                             'rawdescription' =>
132                             // TRANS: Plugin description.
133                             _m('Plugin to allow following all messages with a given search.'));
134         return true;
135     }
136
137     /**
138      * Hook inbox delivery setup so search subscribers receive all
139      * notices with that search in their inbox.
140      *
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.
144      *
145      * @param Notice $notice
146      * @param array $ni in/out map of profile IDs to inbox constants
147      * @return boolean hook result
148      */
149     function onStartNoticeWhoGets(Notice $notice, array &$ni)
150     {
151         // Warning: this is potentially very slow
152         // with a lot of searches!
153         $sub = new SearchSub();
154         $sub->groupBy('search');
155         $sub->find();
156         while ($sub->fetch()) {
157             $search = $sub->search;
158
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;
164                 $searchsub->find();
165
166                 while ($searchsub->fetch()) {
167                     // These constants are currently not actually used, iirc
168                     $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
169                 }
170             }
171         }
172         return true;
173     }
174
175     /**
176      * Does the given notice match the given fulltext search query?
177      *
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.
182      *
183      * @param Notice $notice
184      * @param string $search
185      * @return boolean
186      */
187     function matchSearch(Notice $notice, $search)
188     {
189         return (mb_stripos($notice->content, $search) !== false);
190     }
191
192     /**
193      *
194      * @param NoticeSearchAction $action
195      * @param string $q
196      * @param Notice $notice
197      * @return boolean hook result
198      */
199     function onStartNoticeSearchShowResults($action, $q, $notice)
200     {
201         $user = common_current_user();
202         if ($user) {
203             $search = $q;
204             $searchsub = SearchSub::pkeyGet(array('search' => $search,
205                                                   'profile_id' => $user->id));
206             if ($searchsub) {
207                 $form = new SearchUnsubForm($action, $search);
208             } else {
209                 $form = new SearchSubForm($action, $search);
210             }
211             $action->elementStart('div', 'entity_actions');
212             $action->elementStart('ul');
213             $action->elementStart('li', 'entity_subscribe');
214             $form->show();
215             $action->elementEnd('li');
216             $action->elementEnd('ul');
217             $action->elementEnd('div');
218         }
219         return true;
220     }
221
222     /**
223      * Menu item for personal subscriptions/groups area
224      *
225      * @param Widget $widget Widget being executed
226      *
227      * @return boolean hook return
228      */
229     function onEndSubGroupNav($widget)
230     {
231         $action = $widget->out;
232         $action_name = $action->trimmed('action');
233
234         $action->menuItem(common_local_url('searchsubs', array('nickname' => $action->user->nickname)),
235                           // TRANS: SearchSub plugin menu item on user settings page.
236                           _m('MENU', 'Searches'),
237                           // TRANS: SearchSub plugin tooltip for user settings menu item.
238                           _m('Configure search subscriptions'),
239                           $action_name == 'searchsubs' && $action->arg('nickname') == $action->user->nickname);
240
241         return true;
242     }
243
244     /**
245      * Replace the built-in stub track commands with ones that control
246      * search subscriptions.
247      *
248      * @param CommandInterpreter $cmd
249      * @param string $arg
250      * @param User $user
251      * @param Command $result
252      * @return boolean hook result
253      */
254     function onEndInterpretCommand($cmd, $arg, $user, &$result)
255     {
256         if ($result instanceof TrackCommand) {
257             $result = new SearchSubTrackCommand($user, $arg);
258             return false;
259         } else if ($result instanceof TrackOffCommand) {
260             $result = new SearchSubTrackOffCommand($user);
261             return false;
262         } else if ($result instanceof TrackingCommand) {
263             $result = new SearchSubTrackingCommand($user);
264             return false;
265         } else if ($result instanceof UntrackCommand) {
266             $result = new SearchSubUntrackCommand($user, $arg);
267             return false;
268         } else {
269             return true;
270         }
271     }
272
273     function onHelpCommandMessages($cmd, &$commands)
274     {
275         // TRANS: Help message for IM/SMS command "track <word>"
276         $commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
277         // TRANS: Help message for IM/SMS command "untrack <word>"
278         $commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
279         // TRANS: Help message for IM/SMS command "track off"
280         $commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
281         // TRANS: Help message for IM/SMS command "untrack all"
282         $commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
283         // TRANS: Help message for IM/SMS command "tracks"
284         $commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
285         // TRANS: Help message for IM/SMS command "tracking"
286         $commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
287     }
288
289     function onEndDefaultLocalNav($menu, $user)
290     {
291         $user = common_current_user();
292
293         if (!empty($user)) {
294             $searches = SearchSub::forProfile($user->getProfile());
295
296             if (!empty($searches) && count($searches) > 0) {
297                 $searchSubMenu = new SearchSubMenu($menu->out, $user, $searches);
298                 // TRANS: Sub menu for searches.
299                 $menu->submenu(_m('MENU','Searches'), $searchSubMenu);
300             }
301         }
302
303         return true;
304     }
305 }