Commit 977d635b authored by Dries's avatar Dries

- 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;
}
......
This diff is collapsed.
......@@ -35,20 +35,20 @@ class PathTestCase extends DrupalWebTestCase {
// Create alias.
$edit = array();
$edit['src'] = 'node/' . $node1->nid;
$edit['dst'] = $this->randomName(8);
$edit['source'] = 'node/' . $node1->nid;
$edit['alias'] = $this->randomName(8);
$this->drupalPost('admin/config/search/path/add', $edit, t('Create new alias'));
// Visit the system path for the node and confirm a cache entry is
// created.
cache_clear_all('*', 'cache_path', TRUE);
$this->drupalGet($edit['src']);
$this->assertTrue(cache_get($edit['src'], 'cache_path'), t('Cache entry was created.'));
$this->drupalGet($edit['source']);
$this->assertTrue(cache_get($edit['source'], 'cache_path'), t('Cache entry was created.'));
// Visit the alias for the node and confirm a cache entry is created.
cache_clear_all('*', 'cache_path', TRUE);
$this->drupalGet($edit['dst']);
$this->assertTrue(cache_get($edit['src'], 'cache_path'), t('Cache entry was created.'));
$this->drupalGet($edit['alias']);
$this->assertTrue(cache_get($edit['source'], 'cache_path'), t('Cache entry was created.'));
}
/**
......@@ -60,25 +60,26 @@ class PathTestCase extends DrupalWebTestCase {
// Create alias.
$edit = array();
$edit['src'] = 'node/' . $node1->nid;
$edit['dst'] = $this->randomName(8);
$edit['source'] = 'node/' . $node1->nid;
$edit['alias'] = $this->randomName(8);
$this->drupalPost('admin/config/search/path/add', $edit, t('Create new alias'));
// Confirm that the alias works.
$this->drupalGet($edit['dst']);
$this->drupalGet($edit['alias']);
$this->assertText($node1->title[FIELD_LANGUAGE_NONE][0]['value'], 'Alias works.');
// Change alias.
$pid = $this->getPID($edit['dst']);
$pid = $this->getPID($edit['alias']);
$previous = $edit['dst'];
$edit['dst'] = $this->randomName(8);
$previous = $edit['alias'];
$edit['alias'] = $this->randomName(8);
$this->drupalPost('admin/config/search/path/edit/' . $pid, $edit, t('Update alias'));
// Confirm that the alias works.
$this->drupalGet($edit['dst']);
$this->drupalGet($edit['alias']);
$this->assertText($node1->title[FIELD_LANGUAGE_NONE][0]['value'], 'Changed alias works.');
drupal_static_reset('drupal_lookup_path');
// Confirm that previous alias no longer works.
$this->drupalGet($previous);
$this->assertNoText($node1->title, 'Previous alias no longer works.');
......@@ -88,18 +89,18 @@ class PathTestCase extends DrupalWebTestCase {
$node2 = $this->drupalCreateNode();
// Set alias to second test node.
$edit['src'] = 'node/' . $node2->nid;
// leave $edit['dst'] the same
$edit['source'] = 'node/' . $node2->nid;
// leave $edit['alias'] the same
$this->drupalPost('admin/config/search/path/add', $edit, t('Create new alias'));
// Confirm no duplicate was created.
$this->assertRaw(t('The alias %alias is already in use in this language.', array('%alias' => $edit['dst'])), 'Attempt to move alias was rejected.');
$this->assertRaw(t('The alias %alias is already in use in this language.', array('%alias' => $edit['alias'])), 'Attempt to move alias was rejected.');
// Delete alias.
$this->drupalPost('admin/config/search/path/delete/' . $pid, array(), t('Confirm'));
// Confirm that the alias no longer works.
$this->drupalGet($edit['dst']);
$this->drupalGet($edit['alias']);
$this->assertNoText($node1->title, 'Alias was successfully deleted.');
}
......@@ -151,8 +152,8 @@ class PathTestCase extends DrupalWebTestCase {
$this->assertNoText($node1->title, 'Alias was successfully deleted.');
}
function getPID($dst) {
return db_query("SELECT pid FROM {url_alias} WHERE dst = :dst", array(':dst' => $dst))->fetchField();
function getPID($alias) {
return db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", array(':alias' => $alias))->fetchField();
}
}
......
......@@ -1583,14 +1583,14 @@ function system_schema() {
'unsigned' => TRUE,
'not null' => TRUE,
),
'src' => array(
'source' => array(
'description' => 'The Drupal path this alias is for; e.g. node/12.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'dst' => array(
'alias' => array(
'description' => 'The alias for this path; e.g. title-of-the-story.',
'type' => 'varchar',
'length' => 255,
......@@ -1606,11 +1606,11 @@ function system_schema() {
),
),
'unique keys' => array(
'dst_language_pid' => array('dst', 'language', 'pid'),
'alias_language_pid' => array('alias', 'language', 'pid'),
),
'primary key' => array('pid'),
'indexes' => array(
'src_language_pid' => array('src', 'language', 'pid'),
'source_language_pid' => array('source', 'language', 'pid'),
),
);
......@@ -1944,7 +1944,6 @@ function system_update_7007() {
return implode(', ', $messages);
}
/**
* Use the poll_choice primary key to record votes in poll_votes rather than
* the choice order. Rename chorder to weight.
......@@ -2740,6 +2739,21 @@ function system_update_7041() {
db_add_field('menu_router', 'delivery_callback', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
}
/**
* Rename dst and src to source and alias.
*/
function system_update_7042() {
// Drop indexes.
db_drop_index('url_alias', 'src_language_pid');
db_drop_unique_key('url_alias', 'dst_language_pid');
// Rename fields.
db_change_field('url_alias', 'src', 'source', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
db_change_field('url_alias', 'dst', 'alias', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
// Add indexes back.
db_add_index('url_alias', 'source_language_pid', array('source', 'language', 'pid'));
db_add_unique_key('url_alias', 'alias_language_pid', array('alias', 'language', 'pid'));
}
/**
* @} End of "defgroup updates-6.x-to-7.x"
* The next series of updates should start at 8000.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment