]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SearchSub/SearchSubPlugin.php
Merge branch '1.0.x' into testing
[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 'SearchUnsubForm':
84         case 'SearchSubTrackCommand':
85         case 'SearchSubTrackOffCommand':
86         case 'SearchSubTrackingCommand':
87         case 'SearchSubUntrackCommand':
88             include_once $dir.'/'.strtolower($cls).'.php';
89             return false;
90         default:
91             return true;
92         }
93     }
94
95     /**
96      * Map URLs to actions
97      *
98      * @param Net_URL_Mapper $m path-to-action mapper
99      *
100      * @return boolean hook value; true means continue processing, false means stop.
101      */
102     function onRouterInitialized($m)
103     {
104         $m->connect('search/:search/subscribe',
105                     array('action' => 'searchsub'),
106                     array('search' => Router::REGEX_TAG));
107         $m->connect('search/:search/unsubscribe',
108                     array('action' => 'searchunsub'),
109                     array('search' => Router::REGEX_TAG));
110
111         $m->connect(':nickname/search-subscriptions',
112                     array('action' => 'searchsubs'),
113                     array('nickname' => Nickname::DISPLAY_FMT));
114         return true;
115     }
116
117     /**
118      * Plugin version data
119      *
120      * @param array &$versions array of version data
121      *
122      * @return value
123      */
124     function onPluginVersion(&$versions)
125     {
126         $versions[] = array('name' => 'SearchSub',
127                             'version' => self::VERSION,
128                             'author' => 'Brion Vibber',
129                             'homepage' => 'http://status.net/wiki/Plugin:SearchSub',
130                             'rawdescription' =>
131                             // TRANS: Plugin description.
132                             _m('Plugin to allow following all messages with a given search.'));
133         return true;
134     }
135
136     /**
137      * Hook inbox delivery setup so search subscribers receive all
138      * notices with that search in their inbox.
139      *
140      * Currently makes no distinction between local messages and
141      * remote ones which happen to come in to the system. Remote
142      * notices that don't come in at all won't ever reach this.
143      *
144      * @param Notice $notice
145      * @param array $ni in/out map of profile IDs to inbox constants
146      * @return boolean hook result
147      */
148     function onStartNoticeWhoGets(Notice $notice, array &$ni)
149     {
150         // Warning: this is potentially very slow
151         // with a lot of searches!
152         $sub = new SearchSub();
153         $sub->groupBy('search');
154         $sub->find();
155         while ($sub->fetch()) {
156             $search = $sub->search;
157
158             if ($this->matchSearch($notice, $search)) {
159                 // Match? Find all those who subscribed to this
160                 // search term and get our delivery on...
161                 $searchsub = new SearchSub();
162                 $searchsub->search = $search;
163                 $searchsub->find();
164
165                 while ($searchsub->fetch()) {
166                     // These constants are currently not actually used, iirc
167                     $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
168                 }
169             }
170         }
171         return true;
172     }
173
174     /**
175      * Does the given notice match the given fulltext search query?
176      *
177      * Warning: not guaranteed to match other search engine behavior, etc.
178      * Currently using a basic case-insensitive substring match, which
179      * probably fits with the 'LIKE' search but not the default MySQL
180      * or Sphinx search backends.
181      *
182      * @param Notice $notice
183      * @param string $search 
184      * @return boolean
185      */
186     function matchSearch(Notice $notice, $search)
187     {
188         return (mb_stripos($notice->content, $search) !== false);
189     }
190
191     /**
192      *
193      * @param NoticeSearchAction $action
194      * @param string $q
195      * @param Notice $notice
196      * @return boolean hook result
197      */
198     function onStartNoticeSearchShowResults($action, $q, $notice)
199     {
200         $user = common_current_user();
201         if ($user) {
202             $search = $q;
203             $searchsub = SearchSub::pkeyGet(array('search' => $search,
204                                                   'profile_id' => $user->id));
205             if ($searchsub) {
206                 $form = new SearchUnsubForm($action, $search);
207             } else {
208                 $form = new SearchSubForm($action, $search);
209             }
210             $action->elementStart('div', 'entity_actions');
211             $action->elementStart('ul');
212             $action->elementStart('li', 'entity_subscribe');
213             $form->show();
214             $action->elementEnd('li');
215             $action->elementEnd('ul');
216             $action->elementEnd('div');
217         }
218         return true;
219     }
220
221     /**
222      * Menu item for personal subscriptions/groups area
223      *
224      * @param Widget $widget Widget being executed
225      *
226      * @return boolean hook return
227      */
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      * Add a count of mirrored feeds into a user's profile sidebar stats.
246      *
247      * @param Profile $profile
248      * @param array $stats
249      * @return boolean hook return value
250      */
251     function onProfileStats($profile, &$stats)
252     {
253         $cur = common_current_user();
254         if (!empty($cur) && $cur->id == $profile->id) {
255             $searchsub = new SearchSub();
256             $searchsub ->profile_id = $profile->id;
257             $entry = array(
258                 'id' => 'searchsubs',
259                 'label' => _m('Search subscriptions'),
260                 'link' => common_local_url('searchsubs', array('nickname' => $profile->nickname)),
261                 'value' => $searchsub->count(),
262             );
263
264             $insertAt = count($stats);
265             foreach ($stats as $i => $row) {
266                 if ($row['id'] == 'groups') {
267                     // Slip us in after them.
268                     $insertAt = $i + 1;
269                     break;
270                 }
271             }
272             array_splice($stats, $insertAt, 0, array($entry));
273         }
274         return true;
275     }
276
277     /**
278      * Replace the built-in stub track commands with ones that control
279      * search subscriptions.
280      *
281      * @param CommandInterpreter $cmd
282      * @param string $arg
283      * @param User $user
284      * @param Command $result
285      * @return boolean hook result
286      */
287     function onEndInterpretCommand($cmd, $arg, $user, &$result)
288     {
289         if ($result instanceof TrackCommand) {
290             $result = new SearchSubTrackCommand($user, $arg);
291             return false;
292         } else if ($result instanceof TrackOffCommand) {
293             $result = new SearchSubTrackOffCommand($user);
294             return false;
295         } else if ($result instanceof TrackingCommand) {
296             $result = new SearchSubTrackingCommand($user);
297             return false;
298         } else if ($result instanceof UntrackCommand) {
299             $result = new SearchSubUntrackCommand($user, $arg);
300             return false;
301         } else {
302             return true;
303         }
304     }
305
306     function onHelpCommandMessages($cmd, &$commands)
307     {
308         // TRANS: Help message for IM/SMS command "track <word>"
309         $commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
310         // TRANS: Help message for IM/SMS command "untrack <word>"
311         $commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
312         // TRANS: Help message for IM/SMS command "track off"
313         $commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
314         // TRANS: Help message for IM/SMS command "untrack all"
315         $commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
316         // TRANS: Help message for IM/SMS command "tracks"
317         $commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
318         // TRANS: Help message for IM/SMS command "tracking"
319         $commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
320     }
321 }