|
|
|
@ -1,14 +1,10 @@
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* Contains \Drupal\Core\Path\AliasStorage.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Drupal\Core\Path;
|
|
|
|
|
|
|
|
|
|
use Drupal\Core\Cache\Cache;
|
|
|
|
|
use Drupal\Core\Database\Connection;
|
|
|
|
|
use Drupal\Core\Database\SchemaObjectExistsException;
|
|
|
|
|
use Drupal\Core\Extension\ModuleHandlerInterface;
|
|
|
|
|
use Drupal\Core\Language\LanguageInterface;
|
|
|
|
|
use Drupal\Core\Database\Query\Condition;
|
|
|
|
@ -21,6 +17,12 @@ use Drupal\Core\Database\Query\Condition;
|
|
|
|
|
* the same, and will both refer to the same internal system path.
|
|
|
|
|
*/
|
|
|
|
|
class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The table for the url_alias storage.
|
|
|
|
|
*/
|
|
|
|
|
const TABLE = 'url_alias';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The database connection.
|
|
|
|
|
*
|
|
|
|
@ -70,18 +72,43 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
|
|
|
|
|
// Insert or update the alias.
|
|
|
|
|
if (empty($pid)) {
|
|
|
|
|
$query = $this->connection->insert('url_alias')
|
|
|
|
|
->fields($fields);
|
|
|
|
|
$pid = $query->execute();
|
|
|
|
|
$try_again = FALSE;
|
|
|
|
|
try {
|
|
|
|
|
$query = $this->connection->insert(static::TABLE)
|
|
|
|
|
->fields($fields);
|
|
|
|
|
$pid = $query->execute();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
// If there was an exception, try to create the table.
|
|
|
|
|
if (!$try_again = $this->ensureTableExists()) {
|
|
|
|
|
// If the exception happened for other reason than the missing table,
|
|
|
|
|
// propagate the exception.
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Now that the table has been created, try again if necessary.
|
|
|
|
|
if ($try_again) {
|
|
|
|
|
$query = $this->connection->insert(static::TABLE)
|
|
|
|
|
->fields($fields);
|
|
|
|
|
$pid = $query->execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fields['pid'] = $pid;
|
|
|
|
|
$operation = 'insert';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Fetch the current values so that an update hook can identify what
|
|
|
|
|
// exactly changed.
|
|
|
|
|
$original = $this->connection->query('SELECT source, alias, langcode FROM {url_alias} WHERE pid = :pid', array(':pid' => $pid))->fetchAssoc();
|
|
|
|
|
try {
|
|
|
|
|
$original = $this->connection->query('SELECT source, alias, langcode FROM {url_alias} WHERE pid = :pid', array(':pid' => $pid))
|
|
|
|
|
->fetchAssoc();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
$original = FALSE;
|
|
|
|
|
}
|
|
|
|
|
$fields['pid'] = $pid;
|
|
|
|
|
$query = $this->connection->update('url_alias')
|
|
|
|
|
$query = $this->connection->update(static::TABLE)
|
|
|
|
|
->fields($fields)
|
|
|
|
|
->condition('pid', $pid);
|
|
|
|
|
$pid = $query->execute();
|
|
|
|
@ -101,7 +128,7 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function load($conditions) {
|
|
|
|
|
$select = $this->connection->select('url_alias');
|
|
|
|
|
$select = $this->connection->select(static::TABLE);
|
|
|
|
|
foreach ($conditions as $field => $value) {
|
|
|
|
|
if ($field == 'source' || $field == 'alias') {
|
|
|
|
|
// Use LIKE for case-insensitive matching.
|
|
|
|
@ -111,12 +138,18 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
$select->condition($field, $value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $select
|
|
|
|
|
->fields('url_alias')
|
|
|
|
|
->orderBy('pid', 'DESC')
|
|
|
|
|
->range(0, 1)
|
|
|
|
|
->execute()
|
|
|
|
|
->fetchAssoc();
|
|
|
|
|
try {
|
|
|
|
|
return $select
|
|
|
|
|
->fields(static::TABLE)
|
|
|
|
|
->orderBy('pid', 'DESC')
|
|
|
|
|
->range(0, 1)
|
|
|
|
|
->execute()
|
|
|
|
|
->fetchAssoc();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -124,7 +157,7 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
*/
|
|
|
|
|
public function delete($conditions) {
|
|
|
|
|
$path = $this->load($conditions);
|
|
|
|
|
$query = $this->connection->delete('url_alias');
|
|
|
|
|
$query = $this->connection->delete(static::TABLE);
|
|
|
|
|
foreach ($conditions as $field => $value) {
|
|
|
|
|
if ($field == 'source' || $field == 'alias') {
|
|
|
|
|
// Use LIKE for case-insensitive matching.
|
|
|
|
@ -134,7 +167,13 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
$query->condition($field, $value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$deleted = $query->execute();
|
|
|
|
|
try {
|
|
|
|
|
$deleted = $query->execute();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
$deleted = FALSE;
|
|
|
|
|
}
|
|
|
|
|
// @todo Switch to using an event for this instead of a hook.
|
|
|
|
|
$this->moduleHandler->invokeAll('path_delete', array($path));
|
|
|
|
|
Cache::invalidateTags(['route_match']);
|
|
|
|
@ -146,8 +185,8 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
*/
|
|
|
|
|
public function preloadPathAlias($preloaded, $langcode) {
|
|
|
|
|
$langcode_list = [$langcode, LanguageInterface::LANGCODE_NOT_SPECIFIED];
|
|
|
|
|
$select = $this->connection->select('url_alias')
|
|
|
|
|
->fields('url_alias', ['source', 'alias']);
|
|
|
|
|
$select = $this->connection->select(static::TABLE)
|
|
|
|
|
->fields(static::TABLE, ['source', 'alias']);
|
|
|
|
|
|
|
|
|
|
if (!empty($preloaded)) {
|
|
|
|
|
$conditions = new Condition('OR');
|
|
|
|
@ -175,7 +214,13 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
|
|
|
|
|
$select->orderBy('pid', 'ASC');
|
|
|
|
|
$select->condition('langcode', $langcode_list, 'IN');
|
|
|
|
|
return $select->execute()->fetchAllKeyed();
|
|
|
|
|
try {
|
|
|
|
|
return $select->execute()->fetchAllKeyed();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -186,8 +231,8 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
$langcode_list = [$langcode, LanguageInterface::LANGCODE_NOT_SPECIFIED];
|
|
|
|
|
|
|
|
|
|
// See the queries above. Use LIKE for case-insensitive matching.
|
|
|
|
|
$select = $this->connection->select('url_alias')
|
|
|
|
|
->fields('url_alias', ['alias'])
|
|
|
|
|
$select = $this->connection->select(static::TABLE)
|
|
|
|
|
->fields(static::TABLE, ['alias'])
|
|
|
|
|
->condition('source', $source, 'LIKE');
|
|
|
|
|
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
|
|
|
|
|
array_pop($langcode_list);
|
|
|
|
@ -201,7 +246,13 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
|
|
|
|
|
$select->orderBy('pid', 'DESC');
|
|
|
|
|
$select->condition('langcode', $langcode_list, 'IN');
|
|
|
|
|
return $select->execute()->fetchField();
|
|
|
|
|
try {
|
|
|
|
|
return $select->execute()->fetchField();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -212,8 +263,8 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
$langcode_list = [$langcode, LanguageInterface::LANGCODE_NOT_SPECIFIED];
|
|
|
|
|
|
|
|
|
|
// See the queries above. Use LIKE for case-insensitive matching.
|
|
|
|
|
$select = $this->connection->select('url_alias')
|
|
|
|
|
->fields('url_alias', ['source'])
|
|
|
|
|
$select = $this->connection->select(static::TABLE)
|
|
|
|
|
->fields(static::TABLE, ['source'])
|
|
|
|
|
->condition('alias', $alias, 'LIKE');
|
|
|
|
|
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
|
|
|
|
|
array_pop($langcode_list);
|
|
|
|
@ -227,7 +278,13 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
|
|
|
|
|
$select->orderBy('pid', 'DESC');
|
|
|
|
|
$select->condition('langcode', $langcode_list, 'IN');
|
|
|
|
|
return $select->execute()->fetchField();
|
|
|
|
|
try {
|
|
|
|
|
return $select->execute()->fetchField();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -235,7 +292,7 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
*/
|
|
|
|
|
public function aliasExists($alias, $langcode, $source = NULL) {
|
|
|
|
|
// Use LIKE and NOT LIKE for case-insensitive matching.
|
|
|
|
|
$query = $this->connection->select('url_alias')
|
|
|
|
|
$query = $this->connection->select(static::TABLE)
|
|
|
|
|
->condition('alias', $this->connection->escapeLike($alias), 'LIKE')
|
|
|
|
|
->condition('langcode', $langcode);
|
|
|
|
|
if (!empty($source)) {
|
|
|
|
@ -243,45 +300,152 @@ class AliasStorage implements AliasStorageInterface {
|
|
|
|
|
}
|
|
|
|
|
$query->addExpression('1');
|
|
|
|
|
$query->range(0, 1);
|
|
|
|
|
return (bool) $query->execute()->fetchField();
|
|
|
|
|
try {
|
|
|
|
|
return (bool) $query->execute()->fetchField();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function languageAliasExists() {
|
|
|
|
|
return (bool) $this->connection->queryRange('SELECT 1 FROM {url_alias} WHERE langcode <> :langcode', 0, 1, array(':langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED))->fetchField();
|
|
|
|
|
try {
|
|
|
|
|
return (bool) $this->connection->queryRange('SELECT 1 FROM {url_alias} WHERE langcode <> :langcode', 0, 1, array(':langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED))->fetchField();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function getAliasesForAdminListing($header, $keys = NULL) {
|
|
|
|
|
$query = $this->connection->select('url_alias')
|
|
|
|
|
$query = $this->connection->select(static::TABLE)
|
|
|
|
|
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
|
|
|
|
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
|
|
|
|
if ($keys) {
|
|
|
|
|
// Replace wildcards with PDO wildcards.
|
|
|
|
|
$query->condition('alias', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
|
|
|
|
|
}
|
|
|
|
|
return $query
|
|
|
|
|
->fields('url_alias')
|
|
|
|
|
->orderByHeader($header)
|
|
|
|
|
->limit(50)
|
|
|
|
|
->execute()
|
|
|
|
|
->fetchAll();
|
|
|
|
|
try {
|
|
|
|
|
return $query
|
|
|
|
|
->fields(static::TABLE)
|
|
|
|
|
->orderByHeader($header)
|
|
|
|
|
->limit(50)
|
|
|
|
|
->execute()
|
|
|
|
|
->fetchAll();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function pathHasMatchingAlias($initial_substring) {
|
|
|
|
|
$query = $this->connection->select('url_alias', 'u');
|
|
|
|
|
$query = $this->connection->select(static::TABLE, 'u');
|
|
|
|
|
$query->addExpression(1);
|
|
|
|
|
return (bool) $query
|
|
|
|
|
->condition('u.source', $this->connection->escapeLike($initial_substring) . '%', 'LIKE')
|
|
|
|
|
->range(0, 1)
|
|
|
|
|
->execute()
|
|
|
|
|
->fetchField();
|
|
|
|
|
try {
|
|
|
|
|
return (bool) $query
|
|
|
|
|
->condition('u.source', $this->connection->escapeLike($initial_substring) . '%', 'LIKE')
|
|
|
|
|
->range(0, 1)
|
|
|
|
|
->execute()
|
|
|
|
|
->fetchField();
|
|
|
|
|
}
|
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
$this->catchException($e);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the table exists and create it if not.
|
|
|
|
|
*/
|
|
|
|
|
protected function ensureTableExists() {
|
|
|
|
|
try {
|
|
|
|
|
$database_schema = $this->connection->schema();
|
|
|
|
|
if (!$database_schema->tableExists(static::TABLE)) {
|
|
|
|
|
$schema_definition = $this->schemaDefinition();
|
|
|
|
|
$database_schema->createTable(static::TABLE, $schema_definition);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If another process has already created the table, attempting to recreate
|
|
|
|
|
// it will throw an exception. In this case just catch the exception and do
|
|
|
|
|
// nothing.
|
|
|
|
|
catch (SchemaObjectExistsException $e) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Act on an exception when url_alias might be stale.
|
|
|
|
|
*
|
|
|
|
|
* If the table does not yet exist, that's fine, but if the table exists and
|
|
|
|
|
* yet the query failed, then the url_alias is stale and the exception needs
|
|
|
|
|
* to propagate.
|
|
|
|
|
*
|
|
|
|
|
* @param $e
|
|
|
|
|
* The exception.
|
|
|
|
|
*
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function catchException(\Exception $e) {
|
|
|
|
|
if ($this->connection->schema()->tableExists(static::TABLE)) {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the schema for the {url_alias} table.
|
|
|
|
|
*/
|
|
|
|
|
public static function schemaDefinition() {
|
|
|
|
|
return [
|
|
|
|
|
'description' => 'A list of URL aliases for Drupal paths; a user may visit either the source or destination path.',
|
|
|
|
|
'fields' => [
|
|
|
|
|
'pid' => [
|
|
|
|
|
'description' => 'A unique path alias identifier.',
|
|
|
|
|
'type' => 'serial',
|
|
|
|
|
'unsigned' => TRUE,
|
|
|
|
|
'not null' => TRUE,
|
|
|
|
|
],
|
|
|
|
|
'source' => [
|
|
|
|
|
'description' => 'The Drupal path this alias is for; e.g. node/12.',
|
|
|
|
|
'type' => 'varchar',
|
|
|
|
|
'length' => 255,
|
|
|
|
|
'not null' => TRUE,
|
|
|
|
|
'default' => '',
|
|
|
|
|
],
|
|
|
|
|
'alias' => [
|
|
|
|
|
'description' => 'The alias for this path; e.g. title-of-the-story.',
|
|
|
|
|
'type' => 'varchar',
|
|
|
|
|
'length' => 255,
|
|
|
|
|
'not null' => TRUE,
|
|
|
|
|
'default' => '',
|
|
|
|
|
],
|
|
|
|
|
'langcode' => [
|
|
|
|
|
'description' => "The language code this alias is for; if 'und', the alias will be used for unknown languages. Each Drupal path can have an alias for each supported language.",
|
|
|
|
|
'type' => 'varchar_ascii',
|
|
|
|
|
'length' => 12,
|
|
|
|
|
'not null' => TRUE,
|
|
|
|
|
'default' => '',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'primary key' => ['pid'],
|
|
|
|
|
'indexes' => [
|
|
|
|
|
'alias_langcode_pid' => ['alias', 'langcode', 'pid'],
|
|
|
|
|
'source_langcode_pid' => ['source', 'langcode', 'pid'],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|