3 * Data class for counting greetings
9 * @author Evan Prodromou <evan@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) 2009, 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')) {
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
37 * Data class for counting greetings
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
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.
49 * @author Evan Prodromou <evan@status.net>
50 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
51 * @link http://status.net/
55 class User_greeting_count extends Managed_DataObject
57 public $__table = 'user_greeting_count'; // table name
58 public $user_id; // int(4) primary_key not_null
59 public $greeting_count; // int(4)
62 * return table definition for DB_DataObject
64 * DB_DataObject needs to know something about the table to manipulate
65 * instances. This method provides all the DB_DataObject needs to know.
67 * @return array array of column definitions
71 return array('user_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
72 'greeting_count' => DB_DATAOBJECT_INT);
76 * return key definitions for DB_DataObject
78 * DB_DataObject needs to know about keys that the table has, since it
79 * won't appear in StatusNet's own keys list. In most cases, this will
80 * simply reference your keyTypes() function.
82 * @return array list of key field names
86 return array_keys($this->keyTypes());
90 * return key definitions for Memcached_DataObject
92 * Our caching system uses the same key definitions, but uses a different
93 * method to get them. This key information is used to store and clear
94 * cached data, so be sure to list any key that will be used for static
97 * @return array associative array of key definitions, field name to type:
98 * 'K' for primary key: for compound keys, add an entry for each component;
99 * 'U' for unique keys: compound keys are not well supported here.
103 return array('user_id' => 'K');
107 * Magic formula for non-autoincrementing integer primary keys
109 * If a table has a single integer column as its primary key, DB_DataObject
110 * assumes that the column is auto-incrementing and makes a sequence table
111 * to do this incrementation. Since we don't need this for our class, we
112 * overload this method and return the magic formula that DB_DataObject needs.
114 * @return array magic three-false array that stops auto-incrementing.
116 function sequenceKey()
118 return array(false, false, false);
122 * Increment a user's greeting count and return instance
124 * This method handles the ins and outs of creating a new greeting_count for a
125 * user or fetching the existing greeting count and incrementing its value.
127 * @param integer $user_id ID of the user to get a count for
129 * @return User_greeting_count instance for this user, with count already incremented.
131 static function inc($user_id)
133 $gc = User_greeting_count::staticGet('user_id', $user_id);
136 $gc = new User_greeting_count();
138 $gc->user_id = $user_id;
139 $gc->greeting_count = 1;
141 $result = $gc->insert();
144 // TRANS: Exception thrown when the user greeting count could not be saved in the database.
145 // TRANS: %d is a user ID (number).
146 throw Exception(sprintf(_m('Could not save new greeting count for %d.'),
152 $gc->greeting_count++;
154 $result = $gc->update($orig);
157 // TRANS: Exception thrown when the user greeting count could not be saved in the database.
158 // TRANS: %d is a user ID (number).
159 throw Exception(sprintf(_m('Could not increment greeting count for %d.'),