3 * Data class to store local search subscriptions
7 * @category SearchSubPlugin
9 * @author Brion Vibber <brion@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('STATUSNET')) {
35 * For storing the search subscriptions
37 * @category PollPlugin
39 * @author Brion Vibber <brion@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41 * @link http://status.net/
46 class SearchSub extends Managed_DataObject
48 public $__table = 'searchsub'; // table name
49 public $search; // text
50 public $profile_id; // int -> profile.id
51 public $created; // datetime
54 * Get an instance by key
56 * This is a utility method to get a single instance with a given key value.
58 * @param string $k Key to use to lookup (usually 'user_id' for this class)
59 * @param mixed $v Value to lookup
61 * @return SearchSub object found, or null for no hits
64 function staticGet($k, $v=null)
66 return Memcached_DataObject::staticGet('SearchSub', $k, $v);
70 * Get an instance by compound key
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
76 * @param array $kv array of key-value mappings
78 * @return SearchSub object found, or null for no hits
83 return Memcached_DataObject::pkeyGet('SearchSub', $kv);
87 * The One True Thingy that must be defined and declared.
89 public static function schemaDef()
92 'description' => 'SearchSubPlugin search subscription records',
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'),
98 'primary key' => array('search', 'profile_id'),
99 'foreign keys' => array(
100 'searchsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
103 'searchsub_created_idx' => array('created'),
104 'searchsub_profile_id_tag_idx' => array('profile_id', 'search'),
110 * Start a search subscription!
112 * @param profile $profile subscriber
113 * @param string $search subscribee
116 static function start(Profile $profile, $search)
118 $ts = new SearchSub();
119 $ts->search = $search;
120 $ts->profile_id = $profile->id;
121 $ts->created = common_sql_now();
123 self::blow('searchsub:by_profile:%d', $profile->id);
128 * End a search subscription!
130 * @param profile $profile subscriber
131 * @param string $search subscribee
133 static function cancel(Profile $profile, $search)
135 $ts = SearchSub::pkeyGet(array('search' => $search,
136 'profile_id' => $profile->id));
139 self::blow('searchsub:by_profile:%d', $profile->id);
143 static function forProfile(Profile $profile)
147 $keypart = sprintf('searchsub:by_profile:%d', $profile->id);
148 $searchstring = self::cacheGet($keypart);
150 if ($searchstring !== false) {
151 if (!empty($searchstring)) {
152 $searches = explode(',', $searchstring);
155 $searchsub = new SearchSub();
156 $searchsub->profile_id = $profile->id;
157 $searchsub->selectAdd();
158 $searchsub->selectAdd('search');
160 if ($searchsub->find()) {
161 $searches = $searchsub->fetchAll('search');
164 self::cacheSet($keypart, implode(',', $searches));