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