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