]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Sample/User_greeting_count.php
Merge branch 'inblob' of git@gitorious.org:~evan/statusnet/evans-mainline into inblob
[quix0rs-gnu-social.git] / plugins / Sample / User_greeting_count.php
1 <?php
2 /**
3  * Data class for counting greetings
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@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) 2009, 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 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Data class for counting greetings
38  *
39  * We use the DB_DataObject framework for data classes in StatusNet. Each
40  * table maps to a particular data class, making it easier to manipulate
41  * data.
42  *
43  * Data classes should extend Memcached_DataObject, the (slightly misnamed)
44  * extension of DB_DataObject that provides caching, internationalization,
45  * and other bits of good functionality to StatusNet-specific data classes.
46  *
47  * @category Action
48  * @package  StatusNet
49  * @author   Evan Prodromou <evan@status.net>
50  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
51  * @link     http://status.net/
52  *
53  * @see      DB_DataObject
54  */
55
56 class User_greeting_count extends Memcached_DataObject
57 {
58     public $__table = 'user_greeting_count'; // table name
59     public $user_id;                         // int(4)  primary_key not_null
60     public $greeting_count;                  // int(4)
61
62     /**
63      * Get an instance by key
64      *
65      * This is a utility method to get a single instance with a given key value.
66      *
67      * @param string $k Key to use to lookup (usually 'user_id' for this class)
68      * @param mixed  $v Value to lookup
69      *
70      * @return User_greeting_count object found, or null for no hits
71      *
72      */
73
74     function staticGet($k, $v=null)
75     {
76         return Memcached_DataObject::staticGet('User_greeting_count', $k, $v);
77     }
78
79     /**
80      * return table definition for DB_DataObject
81      *
82      * DB_DataObject needs to know something about the table to manipulate
83      * instances. This method provides all the DB_DataObject needs to know.
84      *
85      * @return array array of column definitions
86      */
87
88     function table()
89     {
90         return array('user_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
91                      'greeting_count' => DB_DATAOBJECT_INT);
92     }
93
94     /**
95      * return key definitions for DB_DataObject
96      *
97      * DB_DataObject needs to know about keys that the table has; this function
98      * defines them.
99      *
100      * @return array key definitions
101      */
102
103     function keys()
104     {
105         return array('user_id' => 'K');
106     }
107
108     /**
109      * return key definitions for Memcached_DataObject
110      *
111      * Our caching system uses the same key definitions, but uses a different
112      * method to get them.
113      *
114      * @return array key definitions
115      */
116
117     function keyTypes()
118     {
119         return $this->keys();
120     }
121
122     /**
123      * Magic formula for non-autoincrementing integer primary keys
124      *
125      * If a table has a single integer column as its primary key, DB_DataObject
126      * assumes that the column is auto-incrementing and makes a sequence table
127      * to do this incrementation. Since we don't need this for our class, we
128      * overload this method and return the magic formula that DB_DataObject needs.
129      *
130      * @return array magic three-false array that stops auto-incrementing.
131      */
132
133     function sequenceKey()
134     {
135         return array(false, false, false);
136     }
137
138     /**
139      * Increment a user's greeting count and return instance
140      *
141      * This method handles the ins and outs of creating a new greeting_count for a
142      * user or fetching the existing greeting count and incrementing its value.
143      *
144      * @param integer $user_id ID of the user to get a count for
145      *
146      * @return User_greeting_count instance for this user, with count already incremented.
147      */
148
149     static function inc($user_id)
150     {
151         $gc = User_greeting_count::staticGet('user_id', $user_id);
152
153         if (empty($gc)) {
154
155             $gc = new User_greeting_count();
156
157             $gc->user_id        = $user_id;
158             $gc->greeting_count = 1;
159
160             $result = $gc->insert();
161
162             if (!$result) {
163                 throw Exception(sprintf(_m("Could not save new greeting count for %d"),
164                                         $user_id));
165             }
166
167         } else {
168
169             $orig = clone($gc);
170
171             $gc->greeting_count++;
172
173             $result = $gc->update($orig);
174
175             if (!$result) {
176                 throw Exception(sprintf(_m("Could not increment greeting count for %d"),
177                                         $user_id));
178             }
179         }
180
181         return $gc;
182     }
183 }