]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SearchSub/SearchSub.php
Localisation updates from http://translatewiki.net.
[quix0rs-gnu-social.git] / plugins / SearchSub / SearchSub.php
1 <?php
2 /**
3  * Data class to store local search subscriptions
4  *
5  * PHP version 5
6  *
7  * @category SearchSubPlugin
8  * @package  StatusNet
9  * @author   Brion Vibber <brion@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * For storing the search subscriptions
36  *
37  * @category PollPlugin
38  * @package  StatusNet
39  * @author   Brion Vibber <brion@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      DB_DataObject
44  */
45
46 class SearchSub extends Managed_DataObject
47 {
48     public $__table = 'searchsub'; // table name
49     public $search;         // text
50     public $profile_id;  // int -> profile.id
51     public $created;     // datetime
52
53     /**
54      * Get an instance by key
55      *
56      * This is a utility method to get a single instance with a given key value.
57      *
58      * @param string $k Key to use to lookup (usually 'user_id' for this class)
59      * @param mixed  $v Value to lookup
60      *
61      * @return SearchSub object found, or null for no hits
62      *
63      */
64     function staticGet($k, $v=null)
65     {
66         return Memcached_DataObject::staticGet('SearchSub', $k, $v);
67     }
68
69     /**
70      * Get an instance by compound key
71      *
72      * This is a utility method to get a single instance with a given set of
73      * key-value pairs. Usually used for the primary key for a compound key; thus
74      * the name.
75      *
76      * @param array $kv array of key-value mappings
77      *
78      * @return SearchSub object found, or null for no hits
79      *
80      */
81     function pkeyGet($kv)
82     {
83         return Memcached_DataObject::pkeyGet('SearchSub', $kv);
84     }
85
86     /**
87      * The One True Thingy that must be defined and declared.
88      */
89     public static function schemaDef()
90     {
91         return array(
92             'description' => 'SearchSubPlugin search subscription records',
93             'fields' => array(
94                 'search' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash search associated with this subscription'),
95                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile ID of subscribing user'),
96                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
97             ),
98             'primary key' => array('search', 'profile_id'),
99             'foreign keys' => array(
100                 'searchsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
101             ),
102             'indexes' => array(
103                 'searchsub_created_idx' => array('created'),
104                 'searchsub_profile_id_tag_idx' => array('profile_id', 'search'),
105             ),
106         );
107     }
108
109     /**
110      * Start a search subscription!
111      *
112      * @param profile $profile subscriber
113      * @param string $search subscribee
114      * @return SearchSub
115      */
116     static function start(Profile $profile, $search)
117     {
118         $ts = new SearchSub();
119         $ts->search = $search;
120         $ts->profile_id = $profile->id;
121         $ts->created = common_sql_now();
122         $ts->insert();
123         self::blow('searchsub:by_profile:%d', $profile->id);
124         return $ts;
125     }
126
127     /**
128      * End a search subscription!
129      *
130      * @param profile $profile subscriber
131      * @param string $search subscribee
132      */
133     static function cancel(Profile $profile, $search)
134     {
135         $ts = SearchSub::pkeyGet(array('search' => $search,
136                                     'profile_id' => $profile->id));
137         if ($ts) {
138             $ts->delete();
139             self::blow('searchsub:by_profile:%d', $profile->id);
140         }
141     }
142
143     static function forProfile(Profile $profile)
144     {
145         $searches = array();
146
147         $keypart = sprintf('searchsub:by_profile:%d', $profile->id);
148         $searchstring = self::cacheGet($keypart);
149         
150         if ($searchstring !== false) {
151                 if (!empty($searchstring)) {
152                 $searches = explode(',', $searchstring);
153                 }
154         } else {
155             $searchsub = new SearchSub();
156             $searchsub->profile_id = $profile->id;
157             $searchsub->selectAdd();
158             $searchsub->selectAdd('search');
159
160             if ($searchsub->find()) {
161                 $searches = $searchsub->fetchAll('search');
162             }
163
164             self::cacheSet($keypart, implode(',', $searches));
165         }
166
167         return $searches;
168     }
169 }