SimplesitemapQueue.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Drupal\simple_sitemap\Queue;
  3. use Drupal\Core\Queue\DatabaseQueue;
  4. /**
  5. * Class SimplesitemapQueue
  6. * @package Drupal\simple_sitemap\Queue
  7. */
  8. class SimplesitemapQueue extends DatabaseQueue {
  9. /**
  10. * Overrides \Drupal\Core\Queue\DatabaseQueue::claimItem().
  11. *
  12. * Unlike \Drupal\Core\Queue\DatabaseQueue::claimItem(), this method provides
  13. * a default lease time of 0 (no expiration) instead of 30. This allows the
  14. * item to be claimed repeatedly until it is deleted.
  15. */
  16. public function claimItem($lease_time = 0) {
  17. try {
  18. $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();
  19. if ($item) {
  20. $item->data = unserialize($item->data);
  21. return $item;
  22. }
  23. }
  24. catch (\Exception $e) {
  25. $this->catchException($e);
  26. }
  27. return FALSE;
  28. }
  29. public function createItems($data_sets) {
  30. $try_again = FALSE;
  31. try {
  32. $id = $this->doCreateItems($data_sets);
  33. }
  34. catch (\Exception $e) {
  35. // If there was an exception, try to create the table.
  36. if (!$try_again = $this->ensureTableExists()) {
  37. // If the exception happened for other reason than the missing table,
  38. // propagate the exception.
  39. throw $e;
  40. }
  41. }
  42. // Now that the table has been created, try again if necessary.
  43. if ($try_again) {
  44. $id = $this->doCreateItems($data_sets);
  45. }
  46. return $id;
  47. }
  48. protected function doCreateItems($data_sets) {
  49. $query = $this->connection->insert(static::TABLE_NAME)
  50. ->fields(['name', 'data', 'created']);
  51. foreach ($data_sets as $i => $data) {
  52. $query->values([
  53. $this->name,
  54. serialize($data),
  55. time(),
  56. ]);
  57. }
  58. return $query->execute();
  59. }
  60. public function deleteItems($item_ids) {
  61. try {
  62. $this->connection->delete(static::TABLE_NAME)
  63. ->condition('item_id', $item_ids, 'IN')
  64. ->execute();
  65. }
  66. catch (\Exception $e) {
  67. $this->catchException($e);
  68. }
  69. }
  70. }