]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/Irc_waiting_message.php
Update translator documentation, i18n and L10n.
[quix0rs-gnu-social.git] / plugins / Irc / Irc_waiting_message.php
1 <?php\r
2 /**\r
3  * Table Definition for irc_waiting_message\r
4  */\r
5 \r
6 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';\r
7 \r
8 class Irc_waiting_message extends Memcached_DataObject {\r
9 \r
10     public $__table = 'irc_waiting_message'; // table name\r
11     public $id;                              // int primary_key not_null auto_increment\r
12     public $data;                            // blob not_null\r
13     public $prioritise;                      // tinyint(1) not_null\r
14     public $attempts;                        // int not_null\r
15     public $created;                         // datetime() not_null\r
16     public $claimed;                         // datetime()\r
17 \r
18     /* Static get */\r
19     public function staticGet($k, $v = null) {\r
20         return Memcached_DataObject::staticGet('Irc_waiting_message', $k, $v);\r
21     }\r
22 \r
23     /**\r
24     * return table definition for DB_DataObject\r
25     *\r
26     * DB_DataObject needs to know something about the table to manipulate\r
27     * instances. This method provides all the DB_DataObject needs to know.\r
28     *\r
29     * @return array array of column definitions\r
30     */\r
31     public function table() {\r
32         return array('id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,\r
33                      'data' => DB_DATAOBJECT_BLOB + DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,\r
34                      'prioritise' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,\r
35                      'created' => DB_DATAOBJECT_TIME + DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,\r
36                      'claimed' => DB_DATAOBJECT_TIME + DB_DATAOBJECT_STR);\r
37     }\r
38 \r
39     /**\r
40     * return key definitions for DB_DataObject\r
41     *\r
42     * DB_DataObject needs to know about keys that the table has, since it\r
43     * won't appear in StatusNet's own keys list. In most cases, this will\r
44     * simply reference your keyTypes() function.\r
45     *\r
46     * @return array list of key field names\r
47     */\r
48     public function keys() {\r
49         return array_keys($this->keyTypes());\r
50     }\r
51 \r
52     /**\r
53     * return key definitions for Memcached_DataObject\r
54     *\r
55     * Our caching system uses the same key definitions, but uses a different\r
56     * method to get them. This key information is used to store and clear\r
57     * cached data, so be sure to list any key that will be used for static\r
58     * lookups.\r
59     *\r
60     * @return array associative array of key definitions, field name to type:\r
61     *         'K' for primary key: for compound keys, add an entry for each component;\r
62     *         'U' for unique keys: compound keys are not well supported here.\r
63     */\r
64     public function keyTypes() {\r
65         return array('id' => 'K');\r
66     }\r
67 \r
68     /**\r
69     * Magic formula for non-autoincrementing integer primary keys\r
70     *\r
71     * If a table has a single integer column as its primary key, DB_DataObject\r
72     * assumes that the column is auto-incrementing and makes a sequence table\r
73     * to do this incrementation. Since we don't need this for our class, we\r
74     * overload this method and return the magic formula that DB_DataObject needs.\r
75     *\r
76     * @return array magic three-false array that stops auto-incrementing.\r
77     */\r
78     public function sequenceKey() {\r
79         return array(false, false, false);\r
80     }\r
81 \r
82     /**\r
83      * Get the next item in the queue\r
84      *\r
85      * @return Irc_waiting_message Next message if there is one\r
86      */\r
87     public static function top() {\r
88         $wm = new Irc_waiting_message();\r
89 \r
90         $wm->orderBy('prioritise DESC, created');\r
91         $wm->whereAdd('claimed is null');\r
92 \r
93         $wm->limit(1);\r
94 \r
95         $cnt = $wm->find(true);\r
96 \r
97         if ($cnt) {\r
98             // XXX: potential race condition\r
99             // can we force it to only update if claimed is still null\r
100             // (or old)?\r
101             common_log(LOG_INFO, 'claiming IRC waiting message id = ' . $wm->id);\r
102             $orig = clone($wm);\r
103             $wm->claimed = common_sql_now();\r
104             $result = $wm->update($orig);\r
105             if ($result) {\r
106                 common_log(LOG_INFO, 'claim succeeded.');\r
107                 return $wm;\r
108             } else {\r
109                 common_log(LOG_INFO, 'claim failed.');\r
110             }\r
111         }\r
112         $wm = null;\r
113         return null;\r
114     }\r
115 \r
116     /**\r
117     * Increment the attempts count\r
118     *\r
119     * @return void\r
120     * @throws Exception\r
121     */\r
122     public function incAttempts() {\r
123         $orig = clone($this);\r
124         $this->attempts++;\r
125         $result = $this->update($orig);\r
126 \r
127         if (!$result) {\r
128             // TRANS: Exception thrown when an IRC attempts count could not be updated.\r
129             // TRANS: %d is the object ID for which the count could not be updated.\r
130             throw Exception(sprintf(_m('Could not increment attempts count for %d.'), $this->id));\r
131         }\r
132     }\r
133 \r
134     /**\r
135      * Release a claimed item.\r
136      */\r
137     public function releaseClaim() {\r
138         // DB_DataObject doesn't let us save nulls right now\r
139         $sql = sprintf("UPDATE irc_waiting_message SET claimed=NULL WHERE id=%d", $this->id);\r
140         $this->query($sql);\r
141 \r
142         $this->claimed = null;\r
143         $this->encache();\r
144     }\r
145 }\r