]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SearchSub/SearchSubPlugin.php
Merge remote branch 'origin/1.0.x' 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 'SearchSubForm':
82         case 'SearchUnsubForm':
83             include_once $dir.'/'.strtolower($cls).'.php';
84             return false;
85         default:
86             return true;
87         }
88     }
89
90     /**
91      * Map URLs to actions
92      *
93      * @param Net_URL_Mapper $m path-to-action mapper
94      *
95      * @return boolean hook value; true means continue processing, false means stop.
96      */
97     function onRouterInitialized($m)
98     {
99         $m->connect('search/:search/subscribe',
100                     array('action' => 'searchsub'),
101                     array('search' => Router::REGEX_TAG));
102         $m->connect('search/:search/unsubscribe',
103                     array('action' => 'searchunsub'),
104                     array('search' => Router::REGEX_TAG));
105
106         return true;
107     }
108
109     /**
110      * Plugin version data
111      *
112      * @param array &$versions array of version data
113      *
114      * @return value
115      */
116     function onPluginVersion(&$versions)
117     {
118         $versions[] = array('name' => 'SearchSub',
119                             'version' => self::VERSION,
120                             'author' => 'Brion Vibber',
121                             'homepage' => 'http://status.net/wiki/Plugin:SearchSub',
122                             'rawdescription' =>
123                             // TRANS: Plugin description.
124                             _m('Plugin to allow following all messages with a given search.'));
125         return true;
126     }
127
128     /**
129      * Hook inbox delivery setup so search subscribers receive all
130      * notices with that search in their inbox.
131      *
132      * Currently makes no distinction between local messages and
133      * remote ones which happen to come in to the system. Remote
134      * notices that don't come in at all won't ever reach this.
135      *
136      * @param Notice $notice
137      * @param array $ni in/out map of profile IDs to inbox constants
138      * @return boolean hook result
139      */
140     function onStartNoticeWhoGets(Notice $notice, array &$ni)
141     {
142         // Warning: this is potentially very slow
143         // with a lot of searches!
144         $sub = new SearchSub();
145         $sub->groupBy('search');
146         $sub->find();
147         while ($sub->fetch()) {
148             $search = $sub->search;
149
150             if ($this->matchSearch($notice, $search)) {
151                 // Match? Find all those who subscribed to this
152                 // search term and get our delivery on...
153                 $searchsub = new SearchSub();
154                 $searchsub->search = $search;
155                 $searchsub->find();
156
157                 while ($searchsub->fetch()) {
158                     // These constants are currently not actually used, iirc
159                     $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
160                 }
161             }
162         }
163         return true;
164     }
165
166     /**
167      * Does the given notice match the given fulltext search query?
168      *
169      * Warning: not guaranteed to match other search engine behavior, etc.
170      * Currently using a basic case-insensitive substring match, which
171      * probably fits with the 'LIKE' search but not the default MySQL
172      * or Sphinx search backends.
173      *
174      * @param Notice $notice
175      * @param string $search 
176      * @return boolean
177      */
178     function matchSearch(Notice $notice, $search)
179     {
180         return (mb_stripos($notice->content, $search) !== false);
181     }
182
183     /**
184      *
185      * @param NoticeSearchAction $action
186      * @param string $q
187      * @param Notice $notice
188      * @return boolean hook result
189      */
190     function onStartNoticeSearchShowResults($action, $q, $notice)
191     {
192         $user = common_current_user();
193         if ($user) {
194             $search = $q;
195             $searchsub = SearchSub::pkeyGet(array('search' => $search,
196                                                   'profile_id' => $user->id));
197             if ($searchsub) {
198                 $form = new SearchUnsubForm($action, $search);
199             } else {
200                 $form = new SearchSubForm($action, $search);
201             }
202             $action->elementStart('div', 'entity_actions');
203             $action->elementStart('ul');
204             $action->elementStart('li', 'entity_subscribe');
205             $form->show();
206             $action->elementEnd('li');
207             $action->elementEnd('ul');
208             $action->elementEnd('div');
209         }
210         return true;
211     }
212 }