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