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