Elgg  Version 1.9
DatabaseQueue.php
Go to the documentation of this file.
1 <?php
2 
15 
17  protected $name;
18 
20  protected $db;
21 
23  protected $workerId;
24 
31  public function __construct($name, Elgg_Database $db) {
32  $this->db = $db;
33  $this->name = $this->db->sanitizeString($name);
34  $this->workerId = $this->db->sanitizeString(md5(microtime() . getmypid()));
35  }
36 
40  public function enqueue($item) {
41  $prefix = $this->db->getTablePrefix();
42  $blob = $this->db->sanitizeString(serialize($item));
43  $time = time();
44  $query = "INSERT INTO {$prefix}queue
45  SET name = '$this->name', data = '$blob', timestamp = $time";
46  return $this->db->insertData($query) !== false;
47  }
48 
52  public function dequeue() {
53  $prefix = $this->db->getTablePrefix();
54  $update = "UPDATE {$prefix}queue
55  SET worker = '$this->workerId'
56  WHERE name = '$this->name' AND worker IS NULL
57  ORDER BY id ASC LIMIT 1";
58  $num = $this->db->updateData($update, true);
59  if ($num === 1) {
60  $select = "SELECT data FROM {$prefix}queue
61  WHERE worker = '$this->workerId'";
62  $obj = $this->db->getDataRow($select);
63  if ($obj) {
64  $data = unserialize($obj->data);
65  $delete = "DELETE FROM {$prefix}queue
66  WHERE name = '$this->name' AND worker = '$this->workerId'";
67  $this->db->deleteData($delete);
68  return $data;
69  }
70  }
71 
72  return null;
73  }
74 
78  public function clear() {
79  $prefix = $this->db->getTablePrefix();
80  $this->db->deleteData("DELETE FROM {$prefix}queue WHERE name = '$this->name'");
81  }
82 
86  public function size() {
87  $prefix = $this->db->getTablePrefix();
88  $result = $this->db->getDataRow("SELECT COUNT(id) AS total FROM {$prefix}queue WHERE name = '$this->name'");
89  return (int)$result->total;
90  }
91 }
$data
Definition: opendd.php:13
dequeue()
{Remove an item from the queue.mixed}
$item
Definition: item.php:12
enqueue($item)
{Add an item to the queue.Item to add to queue bool}
__construct($name, Elgg_Database $db)
Create a queue.
clear()
{Clear all items from the queue.void}
size()
{Get the size of the queue.int}