View source
<?php
namespace Drupal\purge_queuer_url;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
class TrafficRegistry extends ServiceProviderBase implements TrafficRegistryInterface {
protected $connection;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
public function add($url_or_path, array $tags) {
if (!$this->connection
->schema()
->tableExists('purge_queuer_url')) {
return;
}
if (empty($tags)) {
throw new \LogicException('$tags cannot be empty!');
}
if (strlen($url_or_path) > 255) {
return;
}
$tag_ids = ';' . implode(';', array_keys($this
->getTagIds($tags)));
$fields = [
'url' => $url_or_path,
'tag_ids' => $tag_ids,
];
$this->connection
->merge('purge_queuer_url')
->insertFields($fields)
->updateFields($fields)
->key([
'url' => $url_or_path,
])
->execute();
}
public function remove($url_or_path) {
if (!$this->connection
->schema()
->tableExists('purge_queuer_url')) {
return;
}
if (empty(trim($url_or_path))) {
return;
}
$this->connection
->delete('purge_queuer_url')
->condition('url', $url_or_path)
->execute();
}
public function clear() {
if ($this->connection
->schema()
->tableExists('purge_queuer_url')) {
$this->connection
->delete('purge_queuer_url')
->execute();
}
if ($this->connection
->schema()
->tableExists('purge_queuer_url_tag')) {
$this->connection
->delete('purge_queuer_url_tag')
->execute();
}
}
public function countUrls() {
if (!$this->connection
->schema()
->tableExists('purge_queuer_url')) {
return 0;
}
return (int) $this->connection
->select('purge_queuer_url')
->fields(NULL, [
'field',
])
->countQuery()
->execute()
->fetchField();
}
public function getUrls(array $tags) {
if (!$this->connection
->schema()
->tableExists('purge_queuer_url')) {
return [];
}
if (empty($tags)) {
throw new \LogicException('$tags cannot be empty!');
}
$tag_ids = array_keys($this
->getTagIds($tags, FALSE));
if (empty($tag_ids)) {
return [];
}
$or = new Condition('OR');
foreach ($tag_ids as $tag_id) {
$syntax = '%;' . $this->connection
->escapeLike($tag_id) . '%';
$or
->condition('tag_ids', $syntax, 'LIKE');
}
$urls = [];
$results = $this->connection
->select('purge_queuer_url', 'u')
->fields('u', [
'url',
])
->condition($or)
->execute();
foreach ($results as $url) {
$urls[] = $url->url;
}
return $urls;
}
protected function getTagIds(array $tags, $add_missing = TRUE) {
if (empty($tags)) {
throw new \LogicException('$path cannot be empty!');
}
$load_from_db = function (&$tags, &$ids) {
$db_results = $this->connection
->select('purge_queuer_url_tag', 't')
->fields('t', [
'tagid',
'tag',
])
->condition('tag', $tags, 'IN')
->execute();
foreach ($db_results as $tag) {
$ids[intval($tag->tagid)] = $tag->tag;
unset($tags[array_search($tag->tag, $tags)]);
}
};
$ids = [];
$load_from_db($tags, $ids);
if (count($tags) && $add_missing) {
$q = $this->connection
->insert('purge_queuer_url_tag')
->fields([
'tag',
]);
foreach ($tags as $tag) {
$q
->values([
'tag' => $tag,
]);
}
$q
->execute();
$load_from_db($tags, $ids);
}
return $ids;
}
}