]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TagSub/TagSub.php
c0c4c1ec86f4c2233f31fbf743563019a0d21605
[quix0rs-gnu-social.git] / plugins / TagSub / TagSub.php
1 <?php
2 /**
3  * Data class to store local tag subscriptions
4  *
5  * PHP version 5
6  *
7  * @category TagSubPlugin
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 tag 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 class TagSub extends Managed_DataObject
46 {
47     public $__table = 'tagsub'; // table name
48     public $tag;         // text
49     public $profile_id;  // int -> profile.id
50     public $created;     // datetime
51
52     /**
53      * Get an instance by key
54      *
55      * This is a utility method to get a single instance with a given key value.
56      *
57      * @param string $k Key to use to lookup (usually 'user_id' for this class)
58      * @param mixed  $v Value to lookup
59      *
60      * @return TagSub object found, or null for no hits
61      *
62      */
63     function staticGet($k, $v=null)
64     {
65         return Memcached_DataObject::staticGet('TagSub', $k, $v);
66     }
67
68     /**
69      * Get an instance by compound key
70      *
71      * This is a utility method to get a single instance with a given set of
72      * key-value pairs. Usually used for the primary key for a compound key; thus
73      * the name.
74      *
75      * @param array $kv array of key-value mappings
76      *
77      * @return TagSub object found, or null for no hits
78      *
79      */
80     function pkeyGet($kv)
81     {
82         return Memcached_DataObject::pkeyGet('TagSub', $kv);
83     }
84
85     /**
86      * The One True Thingy that must be defined and declared.
87      */
88     public static function schemaDef()
89     {
90         return array(
91             'description' => 'TagSubPlugin tag subscription records',
92             'fields' => array(
93                 'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash tag associated with this subscription'),
94                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile ID of subscribing user'),
95                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
96             ),
97             'primary key' => array('tag', 'profile_id'),
98             'foreign keys' => array(
99                 'tagsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
100             ),
101             'indexes' => array(
102                 'tagsub_created_idx' => array('created'),
103                 'tagsub_profile_id_tag_idx' => array('profile_id', 'tag'),
104             ),
105         );
106     }
107
108     /**
109      * Start a tag subscription!
110      *
111      * @param profile $profile subscriber
112      * @param string $tag subscribee
113      * @return TagSub
114      */
115     static function start(Profile $profile, $tag)
116     {
117         $ts = new TagSub();
118         $ts->tag = $tag;
119         $ts->profile_id = $profile->id;
120         $ts->created = common_sql_now();
121         $ts->insert();
122         self::blow('tagsub:by_profile:%d', $profile->id);
123         return $ts;
124     }
125
126     /**
127      * End a tag subscription!
128      *
129      * @param profile $profile subscriber
130      * @param string $tag subscribee
131      */
132     static function cancel(Profile $profile, $tag)
133     {
134         $ts = TagSub::pkeyGet(array('tag' => $tag,
135                                     'profile_id' => $profile->id));
136         if ($ts) {
137             $ts->delete();
138             self::blow('tagsub:by_profile:%d', $profile->id);
139         }
140     }
141
142     static function forProfile(Profile $profile)
143     {
144         $tags = array();
145
146         $keypart = sprintf('tagsub:by_profile:%d', $profile->id);
147         $tagstring = self::cacheGet($keypart);
148         
149         if ($tagstring !== false) { // cache hit
150                 if (!empty($tagstring)) {
151                 $tags = explode(',', $tagstring);
152                 }
153         } else {
154             $tagsub             = new TagSub();
155             $tagsub->profile_id = $profile->id;
156             $tagsub->selectAdd();
157             $tagsub->selectAdd('tag');
158
159             if ($tagsub->find()) {
160                                 $tags = $tagsub->fetchAll('tag');
161             }
162
163             self::cacheSet($keypart, implode(',', $tags));
164         }
165
166         return $tags;
167     }
168 }