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