]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Msn/classes/msn_waiting_message.php
plugins onAutoload now only overloads if necessary (extlibs etc.)
[quix0rs-gnu-social.git] / plugins / Msn / classes / 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 Managed_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 $claimed;                         // datetime()\r
14     public $created;                         // datetime()   not_null\r
15     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP\r
16 \r
17     public static function schemaDef()\r
18     {\r
19         return array(\r
20             'fields' => array(\r
21                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique ID for entry'),\r
22                 'screenname' => array('type' => 'varchar', 'length' => 255, 'description' => 'from screenname'),\r
23                 'message' => array('type' => 'text', 'not null' => true, 'description' => 'MSN message text'),\r
24                 'claimed' => array('type' => 'datetime', 'description' => 'date this irc message was claimed'),\r
25                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),\r
26                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),\r
27             ),\r
28             'primary key' => array('id'),\r
29             'indexes' => array(\r
30                 'msn_waiting_message_prioritise_idx' => array('screenname'),\r
31             ),\r
32         );\r
33     }\r
34 \r
35     /**\r
36      * @param string $screenname screenname or array of screennames to pull from\r
37      *                          If not specified, checks all queues in the system.\r
38      */\r
39     public static function top($screenname = null) {\r
40         $wm = new Msn_waiting_message();\r
41         if ($screenname) {\r
42             if (is_array($screenname)) {\r
43                 // @fixme use safer escaping\r
44                 $list = implode("','", array_map('addslashes', $screenname));\r
45                 $wm->whereAdd("screenname in ('$list')");\r
46             } else {\r
47                 $wm->screenname = $screenname;\r
48             }\r
49         }\r
50         $wm->orderBy('created');\r
51         $wm->whereAdd('claimed is null');\r
52 \r
53         $wm->limit(1);\r
54 \r
55         $cnt = $wm->find(true);\r
56 \r
57         if ($cnt) {\r
58             // XXX: potential race condition\r
59             // can we force it to only update if claimed is still null\r
60             // (or old)?\r
61             common_log(LOG_INFO, 'claiming msn waiting message id = ' . $wm->id);\r
62             $orig = clone($wm);\r
63             $wm->claimed = common_sql_now();\r
64             $result = $wm->update($orig);\r
65             if ($result) {\r
66                 common_log(LOG_INFO, 'claim succeeded.');\r
67                 return $wm;\r
68             } else {\r
69                 common_log(LOG_INFO, 'claim failed.');\r
70             }\r
71         }\r
72         $wm = null;\r
73         return null;\r
74     }\r
75 \r
76     /**\r
77      * Release a claimed item.\r
78      */\r
79     public function releaseClaim() {\r
80         // DB_DataObject doesn't let us save nulls right now\r
81         $sql = sprintf("UPDATE msn_waiting_message SET claimed=NULL WHERE id=%d", $this->id);\r
82         $this->query($sql);\r
83 \r
84         $this->claimed = null;\r
85         $this->encache();\r
86     }\r
87 }\r