Commit 977d635b authored by Dries's avatar Dries
Browse files

- Patch #332333 by dmitrig01, alexw: add a real API to path.module.

parent e1642603
......@@ -47,7 +47,7 @@ function drupal_lookup_path($action, $path = '', $path_language = '') {
global $language;
$cache = &drupal_static(__FUNCTION__, array(
'map' => array(),
'no_src' => array(),
'no_source' => array(),
'whitelist' => NULL,
'system_paths' => array(),
'no_aliases' => array(),
......@@ -83,7 +83,7 @@ function drupal_lookup_path($action, $path = '', $path_language = '') {
// Now fetch the aliases corresponding to these system paths.
// We order by ASC and overwrite array keys to ensure the correct
// alias is used when there are multiple aliases per path.
$cache['map'][$path_language] = db_query("SELECT src, dst FROM {url_alias} WHERE src IN (:system) AND language IN (:language, '') ORDER BY language ASC, pid ASC", array(
$cache['map'][$path_language] = db_query("SELECT source, alias FROM {url_alias} WHERE source IN (:system) AND language IN (:language, '') ORDER BY language ASC, pid ASC", array(
':system' => $cache['system_paths'],
':language' => $path_language
))->fetchAllKeyed();
......@@ -104,35 +104,35 @@ function drupal_lookup_path($action, $path = '', $path_language = '') {
// For system paths which were not cached, query aliases individually.
else if (!isset($cache['no_aliases'][$path_language][$path])) {
// Get the most fitting result falling back with alias without language
$alias = db_query("SELECT dst FROM {url_alias} WHERE src = :src AND language IN (:language, '') ORDER BY language DESC, pid DESC", array(
':src' => $path,
$alias = db_query("SELECT alias FROM {url_alias} WHERE source = :source AND language IN (:language, '') ORDER BY language DESC, pid DESC", array(
':source' => $path,
':language' => $path_language
))->fetchField();
$cache['map'][$path_language][$path] = $alias;
return $alias;
}
}
// Check $no_src for this $path in case we've already determined that there
// Check $no_source for this $path in case we've already determined that there
// isn't a path that has this alias
elseif ($action == 'source' && !isset($cache['no_src'][$path_language][$path])) {
elseif ($action == 'source' && !isset($cache['no_source'][$path_language][$path])) {
// Look for the value $path within the cached $map
$src = '';
if (!isset($cache['map'][$path_language]) || !($src = array_search($path, $cache['map'][$path_language]))) {
$source = '';
if (!isset($cache['map'][$path_language]) || !($source = array_search($path, $cache['map'][$path_language]))) {
// Get the most fitting result falling back with alias without language
if ($src = db_query("SELECT src FROM {url_alias} WHERE dst = :dst AND language IN (:language, '') ORDER BY language DESC, pid DESC", array(
':dst' => $path,
if ($source = db_query("SELECT source FROM {url_alias} WHERE alias = :alias AND language IN (:language, '') ORDER BY language DESC, pid DESC", array(
':alias' => $path,
':language' => $path_language))
->fetchField()) {
$cache['map'][$path_language][$src] = $path;
$cache['map'][$path_language][$source] = $path;
}
else {
// We can't record anything into $map because we do not have a valid
// index and there is no need because we have not learned anything
// about any Drupal path. Thus cache to $no_src.
$cache['no_src'][$path_language][$path] = TRUE;
// about any Drupal path. Thus cache to $no_source.
$cache['no_source'][$path_language][$path] = TRUE;
}
}
return $src;
return $source;
}
}
......@@ -205,8 +205,8 @@ function drupal_get_path_alias($path = NULL, $path_language = '') {
*/
function drupal_get_normal_path($path, $path_language = '') {
$result = $path;
if ($src = drupal_lookup_path('source', $path, $path_language)) {
$result = $src;
if ($source = drupal_lookup_path('source', $path, $path_language)) {
$result = $source;
}
if (function_exists('custom_url_rewrite_inbound')) {
// Modules may alter the inbound request path by reference.
......@@ -367,7 +367,7 @@ function drupal_path_alias_whitelist_rebuild() {
// path it corresponds to. This is the portion of the path before the first /
// if present, otherwise the whole path itself.
$whitelist = array();
$result = db_query("SELECT SUBSTRING_INDEX(src, '/', 1) AS path FROM {url_alias} GROUP BY path");
$result = db_query("SELECT SUBSTRING_INDEX(source, '/', 1) AS path FROM {url_alias} GROUP BY path");
foreach ($result as $row) {
$whitelist[$row->path] = TRUE;
}
......
......@@ -1268,8 +1268,8 @@ class LocalePathFunctionalTest extends DrupalWebTestCase {
$path = 'admin/config/search/path/add';
$english_path = $this->randomName(8);
$edit = array(
'src' => 'node/' . $node->nid,
'dst' => $english_path,
'source' => 'node/' . $node->nid,
'alias' => $english_path,
'language' => 'en',
);
$this->drupalPost($path, $edit, t('Create new alias'));
......@@ -1277,8 +1277,8 @@ class LocalePathFunctionalTest extends DrupalWebTestCase {
// Create a path alias in new custom language.
$custom_language_path = $this->randomName(8);
$edit = array(
'src' => 'node/' . $node->nid,
'dst' => $custom_language_path,
'source' => 'node/' . $node->nid,
'alias' => $custom_language_path,
'language' => $langcode,
);
$this->drupalPost($path, $edit, t('Create new alias'));
......
......@@ -19,8 +19,8 @@ function path_admin_overview($keys = NULL) {
$multilanguage = (module_exists('locale') || $alias_exists);
$header = array(
array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
array('data' => t('System'), 'field' => 'src'),
array('data' => t('Alias'), 'field' => 'alias', 'sort' => 'asc'),
array('data' => t('System'), 'field' => 'source'),
array('data' => t('Operations'), 'colspan' => '2')
);
if ($multilanguage) {
......@@ -30,7 +30,7 @@ function path_admin_overview($keys = NULL) {
$query = db_select('url_alias')->extend('PagerDefault')->extend('TableSort');
if ($keys) {
// Replace wildcards with PDO wildcards.
$query->condition('dst', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
$query->condition('alias', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
}
$result = $query
->fields('url_alias')
......@@ -43,15 +43,15 @@ function path_admin_overview($keys = NULL) {
foreach ($result as $data) {
$row = array(
'data' => array(
l($data->dst, $data->src),
l($data->src, $data->src, array('alias' => TRUE)),
l($data->alias, $data->source),
l($data->source, $data->source, array('alias' => TRUE)),
l(t('edit'), "admin/config/search/path/edit/$data->pid", array('query' => $destination)),
l(t('delete'), "admin/config/search/path/delete/$data->pid", array('query' => $destination)),
),
);
// If the system path maps to a different URL alias, highlight this table
// row to let the user know of old aliases.
if ($data->dst != drupal_get_path_alias($data->src, $data->language)) {
if ($data->alias != drupal_get_path_alias($data->source, $data->language)) {
$row['class'] = array('warning');
}
if ($multilanguage) {
......@@ -78,11 +78,10 @@ function path_admin_overview($keys = NULL) {
/**
* Menu callback; handles pages for creating and editing URL aliases.
*/
function path_admin_edit($pid = 0) {
if ($pid) {
$alias = path_load($pid);
drupal_set_title($alias['dst']);
$output = drupal_get_form('path_admin_form', $alias);
function path_admin_edit($path = array()) {
if ($path) {
drupal_set_title($path['alias']);
$output = drupal_get_form('path_admin_form', $path);
}
else {
$output = drupal_get_form('path_admin_form');
......@@ -98,41 +97,48 @@ function path_admin_edit($pid = 0) {
* @see path_admin_form_validate()
* @see path_admin_form_submit()
*/
function path_admin_form($form, &$form_state, $edit = array('src' => '', 'dst' => '', 'language' => '', 'pid' => NULL)) {
$form['#alias'] = $edit;
$form['src'] = array(
function path_admin_form($form, &$form_state, $path = array('source' => '', 'alias' => '', 'language' => '', 'pid' => NULL)) {
$form['source'] = array(
'#type' => 'textfield',
'#title' => t('Existing system path'),
'#default_value' => $edit['src'],
'#default_value' => $path['source'],
'#maxlength' => 255,
'#size' => 45,
'#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'),
'#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
'#required' => TRUE,
);
$form['dst'] = array(
$form['alias'] = array(
'#type' => 'textfield',
'#title' => t('Path alias'),
'#default_value' => $edit['dst'],
'#default_value' => $path['alias'],
'#maxlength' => 255,
'#size' => 45,
'#description' => t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
'#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
'#required' => TRUE,
);
// This will be a hidden value unless locale module is enabled
// This will be a hidden value unless locale module is enabled.
$form['language'] = array(
'#type' => 'value',
'#value' => $edit['language']
'#value' => $path['language']
);
if ($edit['pid']) {
$form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
$form['submit'] = array('#type' => 'submit', '#value' => t('Update alias'));
if ($path['pid']) {
$form['pid'] = array(
'#type' => 'hidden',
'#value' => $path['pid'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update alias'),
);
}
else {
$form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias'));
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create new alias'),
);
}
return $form;
......@@ -140,37 +146,42 @@ function path_admin_form($form, &$form_state, $edit = array('src' => '', 'dst' =
/**
* Verify that a new URL alias is valid
* Verify that a URL alias is valid
*/
function path_admin_form_validate($form, &$form_state) {
$src = $form_state['values']['src'];
$dst = $form_state['values']['dst'];
$source = $form_state['values']['source'];
$alias = $form_state['values']['alias'];
$pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
// Language is only set if locale module is enabled, otherwise save for all languages.
$language = isset($form_state['values']['language']) ? $form_state['values']['language'] : '';
$has_alias = db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid <> :pid AND dst = :dst AND language = :language", array(
':pid' => $pid,
':dst' => $dst,
$has_alias = db_query("SELECT COUNT(alias) FROM {url_alias} WHERE pid <> :pid AND alias = :alias AND language = :language", array(
':pid' => $pid,
':alias' => $alias,
':language' => $language,
))
->fetchField();
if ($has_alias) {
form_set_error('dst', t('The alias %alias is already in use in this language.', array('%alias' => $dst)));
form_set_error('alias', t('The alias %alias is already in use in this language.', array('%alias' => $alias)));
}
$item = menu_get_item($src);
$item = menu_get_item($source);
if (!$item || !$item['access']) {
form_set_error('src', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $src)));
form_set_error('source', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source)));
}
}
/**
* Save a new URL alias to the database.
* Save a URL alias to the database.
*/
function path_admin_form_submit($form, &$form_state) {
// Language is only set if locale module is enabled
path_set_alias($form_state['values']['src'], $form_state['values']['dst'], isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0, isset($form_state['values']['language']) ? $form_state['values']['language'] : '');
$path = array();
foreach (array('source', 'alias', 'pid', 'language') as $key) {
if (isset($form_state['values'][$key])) {
$path[$key] = $form_state['values'][$key];
}
}
path_save($path);
drupal_set_message(t('The alias has been saved.'));
$form_state['redirect'] = 'admin/config/search/path';
......@@ -180,15 +191,17 @@ function path_admin_form_submit($form, &$form_state) {
/**
* Menu callback; confirms deleting an URL alias
*/
function path_admin_delete_confirm($form, $form_state, $pid) {
$path = path_load($pid);
function path_admin_delete_confirm($form, &$form_state, $path) {
if (user_access('administer url aliases')) {
$form['pid'] = array('#type' => 'value', '#value' => $pid);
$output = confirm_form($form,
t('Are you sure you want to delete path alias %title?', array('%title' => $path['dst'])),
'admin/config/search/path');
$form_state['path'] = $path;
return confirm_form(
$form,
t('Are you sure you want to delete path alias %title?',
array('%title' => $path['alias'])),
'admin/config/search/path'
);
}
return $output;
return array();
}
/**
......@@ -196,7 +209,7 @@ function path_admin_delete_confirm($form, $form_state, $pid) {
*/
function path_admin_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
path_admin_delete($form_state['values']['pid']);
path_delete($form_state['path']['pid']);
$form_state['redirect'] = 'admin/config/search/path';
return;
}
......
......@@ -40,17 +40,18 @@ function path_menu() {
'access arguments' => array('administer url aliases'),
'file' => 'path.admin.inc',
);
$items['admin/config/search/path/edit'] = array(
$items['admin/config/search/path/edit/%path'] = array(
'title' => 'Edit alias',
'page callback' => 'path_admin_edit',
'page arguments' => array(5),
'access arguments' => array('administer url aliases'),
'type' => MENU_CALLBACK,
'file' => 'path.admin.inc',
);
$items['admin/config/search/path/delete'] = array(
$items['admin/config/search/path/delete/%path'] = array(
'title' => 'Delete alias',
'page callback' => 'drupal_get_form',
'page arguments' => array('path_admin_delete_confirm'),
'page arguments' => array('path_admin_delete_confirm', 5),
'access arguments' => array('administer url aliases'),
'type' => MENU_CALLBACK,
'file' => 'path.admin.inc',
......@@ -72,79 +73,87 @@ function path_menu() {
}
/**
* Post-confirmation; delete an URL alias.
* Fetch a specific URL alias from the database.
*
* @param $criteria
* A string representing the source, a number representing the pid, or an
* array of criteria.
*/
function path_admin_delete($pid = 0) {
db_delete('url_alias')
->condition('pid', $pid)
->execute();
drupal_set_message(t('The alias has been deleted.'));
function path_load($criteria) {
if (is_numeric($criteria)) {
$criteria = array('pid' => $criteria);
}
else if (is_string($criteria)) {
$criteria = array('source' => $criteria);
}
else if (!is_array($criteria)) {
return FALSE;
}
$select = db_select('url_alias');
foreach ($criteria as $field => $value) {
$select->condition($field, $value);
}
return $select
->fields('url_alias', array('source', 'alias', 'language', 'pid'))
->execute()
->fetchAssoc();
}
/**
* Set an aliased path for a given Drupal path, preventing duplicates.
* Save a path alias to the database.
*
* @param $path
* A path array containing the following keys:
* - source: the initial path.
* - alias: the aliased path.
* - language: the language of the alias.
* - pid: unique path alias identifier (optional).
*/
function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
$path = urldecode($path);
$alias = urldecode($alias);
// First we check if we deal with an existing alias and delete or modify it based on pid.
if ($pid) {
// An existing alias.
if (!$path || !$alias) {
// Delete the alias based on pid.
db_delete('url_alias')
->condition('pid', $pid)
->execute();
}
else {
// Update the existing alias.
db_update('url_alias')
->fields(array(
'src' => $path,
'dst' => $alias,
'language' => $language))
->condition('pid', $pid)
->execute();
}
function path_save($path) {
$path += array('language' => '', 'pid' => 0);
$pid = empty($path['pid']) ? 0 : $path['pid'];
$new = (bool) $pid;
unset($path['pid']);
// Make sure that this combination of source, alias, language wasn't save before.
$loaded_path = path_load($path);
if ($loaded_path) {
return $loaded_path;
}
elseif ($path && $alias) {
// Check for existing aliases.
if ($alias == drupal_get_path_alias($path, $language)) {
// There is already such an alias, neutral or in this language.
// Update the alias based on alias; setting the language if not yet done.
db_update('url_alias')
->fields(array(
'src' => $path,
'dst' => $alias,
'language' => $language
))
->condition('dst', $alias)
->execute();
}
else {
// A new alias. Add it to the database.
db_insert('url_alias')
->fields(array(
'src' => $path,
'dst' => $alias,
'language' => $language,
))
->execute();
}
if ($pid) {
db_update('url_alias')
->fields($path)
->condition('pid', $pid)
->execute();
}
else {
// Delete the alias.
if ($alias) {
db_delete('url_alias')
->condition('dst', $alias)
->execute();
}
else {
db_delete('url_alias')
->condition('src', $path)
->execute();
}
$pid = db_insert('url_alias')
->fields($path)
->execute();
}
$path['pid'] = $pid;
module_invoke_all('path_' . ($new ? 'insert' : 'update'), $path);
drupal_clear_path_cache();
return $path;
}
/**
* Delete a URL alias.
*
* @param $criteria
* A number representing the pid or an array of criteria.
*/
function path_delete($criteria) {
if (!is_array($criteria)) {
$criteria = array('pid' => $criteria);
}
$path = path_load($criteria);
$delete = db_delete('url_alias');
foreach ($criteria as $field => $value) {
$delete->condition($field, $value);
}
$delete->execute();
module_invoke_all('path_delete', $path);
drupal_clear_path_cache();
}
......@@ -154,16 +163,18 @@ function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = ''
function path_node_validate($node, $form) {
if (user_access('create url aliases') || user_access('administer url aliases')) {
if (isset($node->path)) {
$language = isset($node->language) ? $node->language : '';
$node->path = trim($node->path);
$has_alias = db_query("SELECT COUNT(dst) FROM {url_alias} WHERE src <> :src AND dst = :dst AND language = :language", array(
':src' => "node/$node->nid",
':dst' => $node->path,
':language' => $language,
))
->fetchField();
if ($has_alias) {
if (!is_array($node->path)) {
$node->path = array('alias' => $node->path);
}
$select = db_select('url_alias')->condition('alias', trim($node->path['alias']));
$select->addExpression('COUNT(alias)');
if ($node->nid) {
$select->condition('source', 'node/' . $node->nid, '<>');
}
if (isset($node->language)) {
$select->condition('language', $node->language);
}
if ($select->execute()->fetchField()) {
form_set_error('path', t('The path is already in use.'));
}
}
......@@ -175,10 +186,12 @@ function path_node_validate($node, $form) {
*/
function path_node_load($nodes, $types) {
foreach ($nodes as $node) {
$language = isset($node->language) ? $node->language : '';
$path = 'node/' . $node->nid;
$alias = drupal_get_path_alias($path, $language);
if ($path != $alias) {
$criteria = array('source' => 'node/' . $node->nid);
if (isset($node->language)) {
$criteria['language'] = $node->language;
}
$alias = path_load($criteria);
if ($alias) {
$node->path = $alias;
}
}
......@@ -188,13 +201,16 @@ function path_node_load($nodes, $types) {
* Implement hook_node_insert().
*/
function path_node_insert($node) {
if (user_access('create url aliases') || user_access('administer url aliases')) {
$language = isset($node->language) ? $node->language : '';
// Don't try to insert if path is NULL. We may have already set
// the alias ahead of time.
if (isset($node->path)) {
path_set_alias('node/' . $node->nid, $node->path, NULL, $language);
if ((user_access('create url aliases') || user_access('administer url aliases')) && isset($node->path)) {
if (!is_array($node->path)) {
$node->path = array('alias' => $node->path);
}
$node->path += array(
'source' => 'node/' . $node->nid,
'language' => isset($node->language) ? $node->language : '',
);
$node->path = path_save($node->path);
}
}
......@@ -202,9 +218,18 @@ function path_node_insert($node) {
* Implement hook_node_update().
*/
function path_node_update($node) {
if (user_access('create url aliases') || user_access('administer url aliases')) {
$language = isset($node->language) ? $node->language : '';
path_set_alias('node/' . $node->nid, isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language);
if ((user_access('create url aliases') || user_access('administer url aliases')) && isset($node->path)) {
if (!is_array($node->path)) {
$node->path = array('alias' => $node->path);
}
if (isset($node->pid)) {
$node->path['pid'] = $node->pid;
}
$node->path += array(
'source' => 'node/' . $node->nid,
'language' => isset($node->language) ? $node->language : '',
);
path_save($node->path);
}
}
......@@ -212,14 +237,20 @@ function path_node_update($node) {
* Implement hook_node_delete().
*/
function path_node_delete($node) {
path_set_alias('node/' . $node->nid);
if (isset($node->path)) {
if (!is_array($node->path)) {
$node->path = path_load(array('alias' => $node->path));
}
path_delete($node->path['pid']);
unset($node->path);
}
}
/**
* Implement hook_taxonomy_term_delete().
*/
function path_taxonomy_term_delete($term) {
path_set_alias('taxonomy/term/' . $term->tid);
path_delete(path_load('taxonomy/term/' . $term->tid));
}
/**
......@@ -227,7 +258,15 @@ function path_taxonomy_term_delete($term) {
*/
function path_form_alter(&$form, $form_state, $form_id) {
if (!empty($form['#node_edit_form'])) {
$path = isset($form['#node']->path) ? $form['#node']->path : NULL;
$path = NULL;
if (isset($form['#node']->path)) {
if (is_array($form['#node']->path)) {
$path = $form['#node']->path['alias'];
}
else {
$path = $form['#node']->path;
}
}