Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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 = $name;
35  $this->workerId = md5(microtime() . getmypid());
36  }
37 
41  public function enqueue($item) {
42  $prefix = $this->db->getTablePrefix();
43  $name = $this->db->sanitizeString($this->name);
44  $blob = $this->db->sanitizeString(serialize($item));
45  $time = time();
46 
47  $query = "INSERT INTO {$prefix}queue
48  SET name = '$name', data = '$blob', timestamp = $time";
49  return $this->db->insertData($query) !== false;
50  }
51 
55  public function dequeue() {
56  $prefix = $this->db->getTablePrefix();
57  $name = $this->db->sanitizeString($this->name);
58  $worker_id = $this->db->sanitizeString($this->workerId);
59 
60  $update = "UPDATE {$prefix}queue
61  SET worker = '$worker_id'
62  WHERE name = '$name' AND worker IS NULL
63  ORDER BY id ASC LIMIT 1";
64  $num = $this->db->updateData($update, true);
65  if ($num === 1) {
66  $select = "SELECT data FROM {$prefix}queue
67  WHERE worker = '$worker_id'";
68  $obj = $this->db->getDataRow($select);
69  if ($obj) {
70  $data = unserialize($obj->data);
71  $delete = "DELETE FROM {$prefix}queue
72  WHERE name = '$name' AND worker = '$worker_id'";
73  $this->db->deleteData($delete);
74  return $data;
75  }
76  }
77 
78  return null;
79  }
80 
84  public function clear() {
85  $prefix = $this->db->getTablePrefix();
86  $name = $this->db->sanitizeString($this->name);
87 
88  $this->db->deleteData("DELETE FROM {$prefix}queue WHERE name = '$name'");
89  }
90 
94  public function size() {
95  $prefix = $this->db->getTablePrefix();
96  $name = $this->db->sanitizeString($this->name);
97 
98  $result = $this->db->getDataRow("SELECT COUNT(id) AS total FROM {$prefix}queue WHERE name = '$name'");
99  return (int)$result->total;
100  }
101 }
102 
__construct($name,\Elgg\Database $db)
Create a queue.
clear()
{Clear all items from the queue.void}
if($selector) $select
Definition: filter.php:36
$data
Definition: opendd.php:13
$site name
dequeue()
{Remove an item from the queue.mixed}
$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}