]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SearchSub/SearchSubPlugin.php
SearchSub plugin: add management UI for subscribed searches
[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             include_once $dir.'/'.strtolower($cls).'.php';
85             return false;
86         default:
87             return true;
88         }
89     }
90
91     /**
92      * Map URLs to actions
93      *
94      * @param Net_URL_Mapper $m path-to-action mapper
95      *
96      * @return boolean hook value; true means continue processing, false means stop.
97      */
98     function onRouterInitialized($m)
99     {
100         $m->connect('search/:search/subscribe',
101                     array('action' => 'searchsub'),
102                     array('search' => Router::REGEX_TAG));
103         $m->connect('search/:search/unsubscribe',
104                     array('action' => 'searchunsub'),
105                     array('search' => Router::REGEX_TAG));
106
107         $m->connect(':nickname/search-subscriptions',
108                     array('action' => 'searchsubs'),
109                     array('nickname' => Nickname::DISPLAY_FMT));
110         return true;
111     }
112
113     /**
114      * Plugin version data
115      *
116      * @param array &$versions array of version data
117      *
118      * @return value
119      */
120     function onPluginVersion(&$versions)
121     {
122         $versions[] = array('name' => 'SearchSub',
123                             'version' => self::VERSION,
124                             'author' => 'Brion Vibber',
125                             'homepage' => 'http://status.net/wiki/Plugin:SearchSub',
126                             'rawdescription' =>
127                             // TRANS: Plugin description.
128                             _m('Plugin to allow following all messages with a given search.'));
129         return true;
130     }
131
132     /**
133      * Hook inbox delivery setup so search subscribers receive all
134      * notices with that search in their inbox.
135      *
136      * Currently makes no distinction between local messages and
137      * remote ones which happen to come in to the system. Remote
138      * notices that don't come in at all won't ever reach this.
139      *
140      * @param Notice $notice
141      * @param array $ni in/out map of profile IDs to inbox constants
142      * @return boolean hook result
143      */
144     function onStartNoticeWhoGets(Notice $notice, array &$ni)
145     {
146         // Warning: this is potentially very slow
147         // with a lot of searches!
148         $sub = new SearchSub();
149         $sub->groupBy('search');
150         $sub->find();
151         while ($sub->fetch()) {
152             $search = $sub->search;
153
154             if ($this->matchSearch($notice, $search)) {
155                 // Match? Find all those who subscribed to this
156                 // search term and get our delivery on...
157                 $searchsub = new SearchSub();
158                 $searchsub->search = $search;
159                 $searchsub->find();
160
161                 while ($searchsub->fetch()) {
162                     // These constants are currently not actually used, iirc
163                     $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
164                 }
165             }
166         }
167         return true;
168     }
169
170     /**
171      * Does the given notice match the given fulltext search query?
172      *
173      * Warning: not guaranteed to match other search engine behavior, etc.
174      * Currently using a basic case-insensitive substring match, which
175      * probably fits with the 'LIKE' search but not the default MySQL
176      * or Sphinx search backends.
177      *
178      * @param Notice $notice
179      * @param string $search 
180      * @return boolean
181      */
182     function matchSearch(Notice $notice, $search)
183     {
184         return (mb_stripos($notice->content, $search) !== false);
185     }
186
187     /**
188      *
189      * @param NoticeSearchAction $action
190      * @param string $q
191      * @param Notice $notice
192      * @return boolean hook result
193      */
194     function onStartNoticeSearchShowResults($action, $q, $notice)
195     {
196         $user = common_current_user();
197         if ($user) {
198             $search = $q;
199             $searchsub = SearchSub::pkeyGet(array('search' => $search,
200                                                   'profile_id' => $user->id));
201             if ($searchsub) {
202                 $form = new SearchUnsubForm($action, $search);
203             } else {
204                 $form = new SearchSubForm($action, $search);
205             }
206             $action->elementStart('div', 'entity_actions');
207             $action->elementStart('ul');
208             $action->elementStart('li', 'entity_subscribe');
209             $form->show();
210             $action->elementEnd('li');
211             $action->elementEnd('ul');
212             $action->elementEnd('div');
213         }
214         return true;
215     }
216
217     /**
218      * Menu item for personal subscriptions/groups area
219      *
220      * @param Widget $widget Widget being executed
221      *
222      * @return boolean hook return
223      */
224
225     function onEndSubGroupNav($widget)
226     {
227         $action = $widget->out;
228         $action_name = $action->trimmed('action');
229
230         $action->menuItem(common_local_url('searchsubs', array('nickname' => $action->user->nickname)),
231                           // TRANS: SearchSub plugin menu item on user settings page.
232                           _m('MENU', 'Searches'),
233                           // TRANS: SearchSub plugin tooltip for user settings menu item.
234                           _m('Configure search subscriptions'),
235                           $action_name == 'searchsubs' && $action->arg('nickname') == $action->user->nickname);
236
237         return true;
238     }
239
240     /**
241      * Add a count of mirrored feeds into a user's profile sidebar stats.
242      *
243      * @param Profile $profile
244      * @param array $stats
245      * @return boolean hook return value
246      */
247     function onProfileStats($profile, &$stats)
248     {
249         $cur = common_current_user();
250         if (!empty($cur) && $cur->id == $profile->id) {
251             $searchsub = new SearchSub();
252             $searchsub ->profile_id = $profile->id;
253             $entry = array(
254                 'id' => 'searchsubs',
255                 'label' => _m('Search subscriptions'),
256                 'link' => common_local_url('searchsubs', array('nickname' => $profile->nickname)),
257                 'value' => $searchsub->count(),
258             );
259
260             $insertAt = count($stats);
261             foreach ($stats as $i => $row) {
262                 if ($row['id'] == 'groups') {
263                     // Slip us in after them.
264                     $insertAt = $i + 1;
265                     break;
266                 }
267             }
268             array_splice($stats, $insertAt, 0, array($entry));
269         }
270         return true;
271     }
272 }