]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SearchSub/SearchSubPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[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      * Map URLs to actions
65      *
66      * @param URLMapper $m path-to-action mapper
67      *
68      * @return boolean hook value; true means continue processing, false means stop.
69      */
70     public function onRouterInitialized(URLMapper $m)
71     {
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));
81         return true;
82     }
83
84     /**
85      * Plugin version data
86      *
87      * @param array &$versions array of version data
88      *
89      * @return value
90      */
91     function onPluginVersion(array &$versions)
92     {
93         $versions[] = array('name' => 'SearchSub',
94                             'version' => self::VERSION,
95                             'author' => 'Brion Vibber',
96                             'homepage' => 'http://status.net/wiki/Plugin:SearchSub',
97                             'rawdescription' =>
98                             // TRANS: Plugin description.
99                             _m('Plugin to allow following all messages with a given search.'));
100         return true;
101     }
102
103     /**
104      * Hook inbox delivery setup so search subscribers receive all
105      * notices with that search in their inbox.
106      *
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.
110      *
111      * @param Notice $notice
112      * @param array $ni in/out map of profile IDs to inbox constants
113      * @return boolean hook result
114      */
115     function onStartNoticeWhoGets(Notice $notice, array &$ni)
116     {
117         // Warning: this is potentially very slow
118         // with a lot of searches!
119         $sub = new SearchSub();
120         $sub->groupBy('search');
121         $sub->find();
122         while ($sub->fetch()) {
123             $search = $sub->search;
124
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;
130                 $searchsub->find();
131
132                 while ($searchsub->fetch()) {
133                     // These constants are currently not actually used, iirc
134                     $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
135                 }
136             }
137         }
138         return true;
139     }
140
141     /**
142      * Does the given notice match the given fulltext search query?
143      *
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.
148      *
149      * @param Notice $notice
150      * @param string $search
151      * @return boolean
152      */
153     function matchSearch(Notice $notice, $search)
154     {
155         return (mb_stripos($notice->content, $search) !== false);
156     }
157
158     /**
159      *
160      * @param NoticeSearchAction $action
161      * @param string $q
162      * @param Notice $notice
163      * @return boolean hook result
164      */
165     function onStartNoticeSearchShowResults($action, $q, $notice)
166     {
167         $user = common_current_user();
168
169         if ($user instanceof User) {
170             $search = $q;
171             $searchsub = SearchSub::pkeyGet(array('search' => $search,
172                                                   'profile_id' => $user->id));
173             if ($searchsub) {
174                 $form = new SearchUnsubForm($action, $search);
175             } else {
176                 $form = new SearchSubForm($action, $search);
177             }
178
179             $action->elementStart('div', 'entity_actions');
180             $action->elementStart('ul');
181             $action->elementStart('li', 'entity_subscribe');
182             $form->show();
183             $action->elementEnd('li');
184             $action->elementEnd('ul');
185             $action->elementEnd('div');
186         }
187
188         return true;
189     }
190
191     /**
192      * Menu item for personal subscriptions/groups area
193      *
194      * @param Widget $widget Widget being executed
195      *
196      * @return boolean hook return
197      */
198     function onEndSubGroupNav($widget)
199     {
200         $action = $widget->out;
201         $action_name = $action->trimmed('action');
202
203         $action->menuItem(common_local_url('searchsubs', array('nickname' => $action->user->nickname)),
204                           // TRANS: SearchSub plugin menu item on user settings page.
205                           _m('MENU', 'Searches'),
206                           // TRANS: SearchSub plugin tooltip for user settings menu item.
207                           _m('Configure search subscriptions'),
208                           $action_name == 'searchsubs' && $action->arg('nickname') == $action->user->nickname);
209
210         return true;
211     }
212
213     /**
214      * Replace the built-in stub track commands with ones that control
215      * search subscriptions.
216      *
217      * @param CommandInterpreter $cmd
218      * @param string $arg
219      * @param User $user
220      * @param Command $result
221      * @return boolean hook result
222      */
223     function onEndInterpretCommand($cmd, $arg, $user, &$result)
224     {
225         if ($result instanceof TrackCommand) {
226             $result = new SearchSubTrackCommand($user, $arg);
227             return false;
228         } else if ($result instanceof TrackOffCommand) {
229             $result = new SearchSubTrackOffCommand($user);
230             return false;
231         } else if ($result instanceof TrackingCommand) {
232             $result = new SearchSubTrackingCommand($user);
233             return false;
234         } else if ($result instanceof UntrackCommand) {
235             $result = new SearchSubUntrackCommand($user, $arg);
236             return false;
237         } else {
238             return true;
239         }
240     }
241
242     function onHelpCommandMessages($cmd, &$commands)
243     {
244         // TRANS: Help message for IM/SMS command "track <word>"
245         $commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
246         // TRANS: Help message for IM/SMS command "untrack <word>"
247         $commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
248         // TRANS: Help message for IM/SMS command "track off"
249         $commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
250         // TRANS: Help message for IM/SMS command "untrack all"
251         $commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
252         // TRANS: Help message for IM/SMS command "tracks"
253         $commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
254         // TRANS: Help message for IM/SMS command "tracking"
255         $commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
256     }
257
258     function onEndDefaultLocalNav($menu, $user)
259     {
260         $user = common_current_user();
261
262         if (!empty($user)) {
263             $searches = SearchSub::forProfile($user->getProfile());
264
265             if (!empty($searches) && count($searches) > 0) {
266                 $searchSubMenu = new SearchSubMenu($menu->out, $user, $searches);
267                 // TRANS: Sub menu for searches.
268                 $menu->submenu(_m('MENU','Searches'), $searchSubMenu);
269             }
270         }
271
272         return true;
273     }
274 }