]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/classes/Irc_waiting_message.php
plugins onAutoload now only overloads if necessary (extlibs etc.)
[quix0rs-gnu-social.git] / plugins / Irc / classes / 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 $claimed;                         // datetime()\r
16     public $created;                         // datetime()   not_null\r
17     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP\r
18 \r
19     public static function schemaDef()\r
20     {\r
21         return array(\r
22             'fields' => array(\r
23                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique ID for entry'),\r
24                 'data' => array('type' => 'blob', 'not null' => true, 'description' => 'data blob'),\r
25                 'prioritise' => array('type' => 'int', 'size' => 'tiny', 'description' => 'tinyint priority value'),\r
26                 'attempts' => array('type' => 'int', 'not null' => true, 'description' => 'attempts count'),\r
27                 'claimed' => array('type' => 'datetime', 'description' => 'date this irc message was claimed'),\r
28                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),\r
29                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),\r
30             ),\r
31             'primary key' => array('id'),\r
32             'indexes' => array(\r
33                 'irc_waiting_message_prioritise_idx' => array('prioritise'),\r
34             ),\r
35         );\r
36     }\r
37 \r
38     /**\r
39      * Get the next item in the queue\r
40      *\r
41      * @return Irc_waiting_message Next message if there is one\r
42      */\r
43     public static function top() {\r
44         $wm = new Irc_waiting_message();\r
45 \r
46         $wm->orderBy('prioritise DESC, created');\r
47         $wm->whereAdd('claimed is null');\r
48 \r
49         $wm->limit(1);\r
50 \r
51         $cnt = $wm->find(true);\r
52 \r
53         if ($cnt) {\r
54             // XXX: potential race condition\r
55             // can we force it to only update if claimed is still null\r
56             // (or old)?\r
57             common_log(LOG_INFO, 'claiming IRC waiting message id = ' . $wm->id);\r
58             $orig = clone($wm);\r
59             $wm->claimed = common_sql_now();\r
60             $result = $wm->update($orig);\r
61             if ($result) {\r
62                 common_log(LOG_INFO, 'claim succeeded.');\r
63                 return $wm;\r
64             } else {\r
65                 common_log(LOG_INFO, 'claim failed.');\r
66             }\r
67         }\r
68         $wm = null;\r
69         return null;\r
70     }\r
71 \r
72     /**\r
73     * Increment the attempts count\r
74     *\r
75     * @return void\r
76     * @throws Exception\r
77     */\r
78     public function incAttempts() {\r
79         $orig = clone($this);\r
80         $this->attempts++;\r
81         $result = $this->update($orig);\r
82 \r
83         if (!$result) {\r
84             // TRANS: Exception thrown when an IRC attempts count could not be updated.\r
85             // TRANS: %d is the object ID for which the count could not be updated.\r
86             throw Exception(sprintf(_m('Could not increment attempts count for %d.'), $this->id));\r
87         }\r
88     }\r
89 \r
90     /**\r
91      * Release a claimed item.\r
92      */\r
93     public function releaseClaim() {\r
94         // DB_DataObject doesn't let us save nulls right now\r
95         $sql = sprintf("UPDATE irc_waiting_message SET claimed=NULL WHERE id=%d", $this->id);\r
96         $this->query($sql);\r
97 \r
98         $this->claimed = null;\r
99         $this->encache();\r
100     }\r
101 }\r