SimpleSitemapQueue.php in Simple XML sitemap 4.x
File
src/Queue/SimpleSitemapQueue.php
View source
<?php
namespace Drupal\simple_sitemap\Queue;
use Drupal\Core\Database\Connection;
use Drupal\Core\Queue\DatabaseQueue;
use Drupal\Component\Datetime\Time;
class SimpleSitemapQueue extends DatabaseQueue {
protected $time;
public function __construct($name, Connection $connection, Time $time) {
parent::__construct($name, $connection);
$this->time = $time;
}
public function claimItem($lease_time = 0) {
try {
$item = $this->connection
->queryRange('SELECT data, item_id FROM {queue} q WHERE name = :name ORDER BY item_id ASC', 0, 1, [
':name' => $this->name,
])
->fetchObject();
if ($item) {
$item->data = unserialize($item->data);
return $item;
}
} catch (\Exception $e) {
$this
->catchException($e);
}
return FALSE;
}
public function yieldItem() : \Generator {
try {
$query = $this->connection
->query('SELECT data, item_id FROM {queue} q WHERE name = :name ORDER BY item_id ASC', [
':name' => $this->name,
]);
while ($item = $query
->fetchObject()) {
$item->data = unserialize($item->data);
(yield $item);
}
} catch (\Exception $e) {
$this
->catchException($e);
}
}
public function createItems($data_sets) {
$try_again = FALSE;
try {
$id = $this
->doCreateItems($data_sets);
} catch (\Exception $e) {
if (!($try_again = $this
->ensureTableExists())) {
throw $e;
}
}
if ($try_again) {
$id = $this
->doCreateItems($data_sets);
}
return $id;
}
protected function doCreateItems($data_sets) {
$query = $this->connection
->insert(static::TABLE_NAME)
->fields([
'name',
'data',
'created',
]);
foreach ($data_sets as $i => $data) {
$query
->values([
$this->name,
serialize($data),
$this->time
->getRequestTime(),
]);
}
return $query
->execute();
}
public function deleteItems($item_ids) : void {
try {
$this->connection
->delete(static::TABLE_NAME)
->condition('item_id', $item_ids, 'IN')
->execute();
} catch (\Exception $e) {
$this
->catchException($e);
}
}
}