]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Queue_item.php
Only serve tagprofile HTML if we aren't POSTing via ajax
[quix0rs-gnu-social.git] / classes / Queue_item.php
1 <?php
2 /**
3  * Table Definition for queue_item
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Queue_item extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'queue_item';                      // table name
13     public $id;                              // int(4)  primary_key not_null
14     public $frame;                           // blob not_null
15     public $transport;                       // varchar(32)
16     public $created;                         // datetime()   not_null
17     public $claimed;                         // datetime()
18
19     /* the code above is auto generated do not remove the tag below */
20     ###END_AUTOCODE
21
22     public static function schemaDef()
23     {
24         return array(
25             'fields' => array(
26                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
27                 'frame' => array('type' => 'blob', 'not null' => true, 'description' => 'data: object reference or opaque string'),
28                 'transport' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'queue for what? "email", "xmpp", "sms", "irc", ...'),
29                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
30                 'claimed' => array('type' => 'datetime', 'description' => 'date this item was claimed'),
31             ),
32             'primary key' => array('id'),
33             'indexes' => array(
34                 'queue_item_created_idx' => array('created'),
35             ),
36         );
37     }
38
39     /**
40      * @param mixed $transports name of a single queue or array of queues to pull from
41      *                          If not specified, checks all queues in the system.
42      */
43     static function top($transports=null) {
44
45         $qi = new Queue_item();
46         if ($transports) {
47             if (is_array($transports)) {
48                 // @fixme use safer escaping
49                 $list = implode("','", array_map(array($qi, 'escape'), $transports));
50                 $qi->whereAdd("transport in ('$list')");
51             } else {
52                 $qi->transport = $transports;
53             }
54         }
55         $qi->orderBy('created');
56         $qi->whereAdd('claimed is null');
57
58         $qi->limit(1);
59
60         $cnt = $qi->find(true);
61
62         if ($cnt) {
63             // XXX: potential race condition
64             // can we force it to only update if claimed is still null
65             // (or old)?
66             common_log(LOG_INFO, 'claiming queue item id = ' . $qi->id .
67                 ' for transport ' . $qi->transport);
68             $orig = clone($qi);
69             $qi->claimed = common_sql_now();
70             $result = $qi->update($orig);
71             if ($result) {
72                 common_log(LOG_INFO, 'claim succeeded.');
73                 return $qi;
74             } else {
75                 common_log(LOG_INFO, 'claim failed.');
76             }
77         }
78         $qi = null;
79         return null;
80     }
81
82     /**
83      * Release a claimed item.
84      */
85     function releaseClaim()
86     {
87         // DB_DataObject doesn't let us save nulls right now
88         $sql = sprintf("UPDATE queue_item SET claimed=NULL WHERE id=%d", $this->id);
89         $this->query($sql);
90
91         $this->claimed = null;
92         $this->encache();
93     }
94 }