Commit 2c71644d authored by Dries's avatar Dries

- Patch #394510 by Berdir, ksenzee: convert to new database abstraction layer.

parent 43ad4ad1
......@@ -15,19 +15,9 @@ function path_admin_overview($keys = NULL) {
// Add the filter form above the overview table.
$output = drupal_get_form('path_admin_filter_form', $keys);
// Enable language column if locale is enabled or if we have any alias with language
$count = db_result(db_query("SELECT COUNT(*) FROM {url_alias} WHERE language <> ''"));
$count = db_query("SELECT COUNT(*) FROM {url_alias} WHERE language <> ''")->fetchField();
$multilanguage = (module_exists('locale') || $count);
if ($keys) {
// Replace wildcards with PDO wildcards.
$keys = preg_replace('!\*+!', '%', $keys);
$sql = "SELECT * FROM {url_alias} WHERE dst LIKE :keys";
$args = array(':keys' => '%' . $keys . '%');
}
else {
$sql = 'SELECT * FROM {url_alias}';
$args = array();
}
$header = array(
array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
array('data' => t('System'), 'field' => 'src'),
......@@ -36,12 +26,21 @@ function path_admin_overview($keys = NULL) {
if ($multilanguage) {
array_splice($header, 2, 0, array(array('data' => t('Language'), 'field' => 'language')));
}
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50, 0 , NULL, $args);
$query = db_select('url_alias')->extend('PagerDefault')->extend('TableSort');
if ($keys) {
// Replace wildcards with PDO wildcards.
$query->condition('dst', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
}
$result = $query
->fields('url_alias')
->setHeader($header)
->limit(50)
->execute();
$rows = array();
$destination = drupal_get_destination();
while ($data = db_fetch_object($result)) {
foreach ($result as $data) {
$row = array(
// If the system path maps to a different URL alias, highlight this table
// row to let the user know of old aliases.
......@@ -144,7 +143,14 @@ function path_admin_form_validate($form, &$form_state) {
// Language is only set if locale module is enabled, otherwise save for all languages.
$language = isset($form_state['values']['language']) ? $form_state['values']['language'] : '';
if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid <> %d AND dst = '%s' AND language = '%s'", $pid, $dst, $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,
':language' => $language,
))
->fetchField();
if ($has_alias) {
form_set_error('dst', t('The alias %alias is already in use in this language.', array('%alias' => $dst)));
}
$item = menu_get_item($src);
......
......@@ -71,7 +71,9 @@ function path_menu() {
* Post-confirmation; delete an URL alias.
*/
function path_admin_delete($pid = 0) {
db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
db_delete('url_alias')
->condition('pid', $pid)
->execute();
drupal_set_message(t('The alias has been deleted.'));
}
......@@ -86,11 +88,19 @@ function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = ''
// An existing alias.
if (!$path || !$alias) {
// Delete the alias based on pid.
db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
db_delete('url_alias')
->condition('pid', $pid)
->execute();
}
else {
// Update the existing alias.
db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
db_update('url_alias')
->fields(array(
'src' => $path,
'dst' => $alias,
'language' => $language))
->condition('pid', $pid)
->execute();
}
}
elseif ($path && $alias) {
......@@ -98,20 +108,37 @@ function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = ''
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_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE dst = '%s'", $path, $alias, $language, $alias);
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_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
db_insert('url_alias')
->fields(array(
'src' => $path,
'dst' => $alias,
'language' => $language,
))
->execute();
}
}
else {
// Delete the alias.
if ($alias) {
db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
db_delete('url_alias')
->condition('dst', $alias)
->execute();
}
else {
db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
db_delete('url_alias')
->condition('src', $path)
->execute();
}
}
drupal_clear_path_cache();
......@@ -125,7 +152,14 @@ function path_node_validate($node, $form) {
if (isset($node->path)) {
$language = isset($node->language) ? $node->language : '';
$node->path = trim($node->path);
if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src <> '%s' AND language = '%s'", $node->path, "node/$node->nid", $language))) {
$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) {
form_set_error('path', t('The path is already in use.'));
}
}
......@@ -211,7 +245,11 @@ function path_form_alter(&$form, $form_state, $form_id) {
if ($path) {
$form['path']['pid'] = array(
'#type' => 'value',
'#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $path, $form['#node']->language))
'#value' => db_query("SELECT pid FROM {url_alias} WHERE dst = :dst AND language = :language", array(
':dst' => $path,
':language' => $form['#node']->language
))
->fetchField(),
);
}
}
......@@ -237,5 +275,5 @@ function path_perm() {
* Fetch a specific URL alias from the database.
*/
function path_load($pid) {
return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
return db_query('SELECT * FROM {url_alias} WHERE pid = :pid', array(':pid' => $pid))->fetchAssoc();
}
......@@ -127,7 +127,7 @@ class PathTestCase extends DrupalWebTestCase {
}
function getPID($dst) {
return db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $dst));
return db_query("SELECT pid FROM {url_alias} WHERE dst = :dst", array(':dst' => $dst))->fetchField();
}
}
......
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