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