]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/Fave_tally.php
Localisation updates from http://translatewiki.net.
[quix0rs-gnu-social.git] / plugins / AnonymousFave / Fave_tally.php
1 <?php
2 /**
3  * Data class for favorites talley
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Zach Copley <zach@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) 2010, 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 favorites tally
38  *
39  * A class representing a total number of times a notice has been favored
40  *
41  * @category Action
42  * @package  StatusNet
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45  * @link     http://status.net/
46  */
47 class Fave_tally extends Memcached_DataObject
48 {
49     ###START_AUTOCODE
50     /* the code below is auto generated do not remove the above tag */
51
52     public $__table = 'fave_tally';          // table name
53     public $notice_id;                       // int(4)  primary_key not_null
54     public $count;                           // int(4)  not_null
55     public $modified;                        // datetime   not_null default_0000-00-00%2000%3A00%3A00
56
57     /* Static get */
58     function staticGet($k, $v = NULL) { return Memcached_DataObject::staticGet('Fave_tally', $k, $v); }
59
60     /* the code above is auto generated do not remove the tag below */
61     ###END_AUTOCODE
62
63     /**
64      * return table definition for DB_DataObject
65      *
66      * @return array array of column definitions
67      */
68
69     function table()
70     {
71         return array(
72             'notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
73             'count'     => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
74             'modified'  => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL
75         );
76     }
77
78     /**
79      * return key definitions for DB_DataObject
80      *
81      * DB_DataObject needs to know about keys that the table has, since it
82      * won't appear in StatusNet's own keys list. In most cases, this will
83      * simply reference your keyTypes() function.
84      *
85      * @return array list of key field names
86      */
87     function keys()
88     {
89         return array_keys($this->keyTypes());
90     }
91
92     /**
93      * return key definitions for Memcached_DataObject
94      *
95      * Our caching system uses the same key definitions, but uses a different
96      * method to get them. This key information is used to store and clear
97      * cached data, so be sure to list any key that will be used for static
98      * lookups.
99      *
100      * @return array associative array of key definitions, field name to type:
101      *         'K' for primary key: for compound keys, add an entry for each component;
102      *         'U' for unique keys: compound keys are not well supported here.
103      */
104     function keyTypes()
105     {
106         return array('notice_id' => 'K');
107     }
108
109     /**
110      * Magic formula for non-autoincrementing integer primary keys
111      *
112      * If a table has a single integer column as its primary key, DB_DataObject
113      * assumes that the column is auto-incrementing and makes a sequence table
114      * to do this incrementation. Since we don't need this for our class, we
115      * overload this method and return the magic formula that DB_DataObject needs.
116      *
117      * @return array magic three-false array that stops auto-incrementing.
118      */
119     function sequenceKey()
120     {
121         return array(false, false, false);
122     }
123
124     /**
125      * Get a single object with multiple keys
126      *
127      * @param array $kv Map of key-value pairs
128      *
129      * @return User_flag_profile found object or null
130      */
131     function pkeyGet($kv)
132     {
133         return Memcached_DataObject::pkeyGet('Fave_tally', $kv);
134     }
135
136     /**
137      * Increment a notice's tally
138      *
139      * @param integer $noticeID ID of notice we're tallying
140      *
141      * @return Fave_tally $tally the tally data object
142      */
143     static function increment($noticeID)
144     {
145         $tally = Fave_tally::ensureTally($noticeID);
146
147         $orig = clone($tally);
148         $tally->count++;
149         $result = $tally->update($orig);
150
151         if (!$result) {
152             $msg = sprintf(
153                 // TRANS: Server exception.
154                 // TRANS: %d is the notice ID (number).
155                 _m("Could not update favorite tally for notice ID %d."),
156                 $noticeID
157             );
158             throw new ServerException($msg);
159         }
160
161         return $tally;
162     }
163
164     /**
165      * Decrement a notice's tally
166      *
167      * @param integer $noticeID ID of notice we're tallying
168      *
169      * @return Fave_tally $tally the tally data object
170      */
171     static function decrement($noticeID)
172     {
173         $tally = Fave_tally::ensureTally($noticeID);
174
175         if ($tally->count > 0) {
176             $orig = clone($tally);
177             $tally->count--;
178             $result = $tally->update($orig);
179
180             if (!$result) {
181                 $msg = sprintf(
182                     // TRANS: Server exception.
183                     // TRANS: %d is the notice ID (number).
184                     _m("Could not update favorite tally for notice ID %d."),
185                     $noticeID
186                 );
187                 throw new ServerException($msg);
188             }
189         }
190
191         return $tally;
192     }
193
194     /**
195      * Ensure a tally exists for a given notice. If we can't find
196      * one create one with the total number of existing faves
197      *
198      * @param integer $noticeID
199      *
200      * @return Fave_tally the tally data object
201      */
202     static function ensureTally($noticeID)
203     {
204         $tally = Fave_tally::staticGet('notice_id', $noticeID);
205
206         if (!$tally) {
207             $tally = new Fave_tally();
208             $tally->notice_id = $noticeID;
209             $tally->count = Fave_tally::countExistingFaves($noticeID);
210             $result = $tally->insert();
211             if (!$result) {
212                 $msg = sprintf(
213                     // TRANS: Server exception.
214                     // TRANS: %d is the notice ID (number).
215                     _m("Could not create favorite tally for notice ID %d."),
216                     $noticeID
217                 );
218                 throw new ServerException($msg);
219             }
220         }
221
222         return $tally;
223     }
224
225     /**
226      * Count the number of faves a notice already has. Used to initalize
227      * a tally for a notice.
228      *
229      * @param integer $noticeID ID of the notice to count faves for
230      *
231      * @return integer $total total number of time the notice has been favored
232      */
233     static function countExistingFaves($noticeID)
234     {
235         $fave = new Fave();
236         $fave->notice_id = $noticeID;
237         $total = $fave->count();
238         return $total;
239     }
240 }