diff --git modules/path/path.admin.inc modules/path/path.admin.inc index dc39b15..ab9d3e5 100644 --- modules/path/path.admin.inc +++ modules/path/path.admin.inc @@ -15,7 +15,7 @@ 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) { @@ -41,7 +41,7 @@ function path_admin_overview($keys = NULL) { $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 +144,12 @@ 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))) { + $path_in_use = 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 ($path_in_use) { form_set_error('dst', t('The alias %alias is already in use in this language.', array('%alias' => $dst))); } $item = menu_get_item($src); diff --git modules/path/path.module modules/path/path.module index 9e28ba9..d2a14b1 100644 --- modules/path/path.module +++ modules/path/path.module @@ -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,18 @@ 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 +107,33 @@ 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 +147,12 @@ function path_nodeapi_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))) { + $path_in_use = db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = :dst AND src != :src AND language = :language", + array(':dst' => $node->path, + ':src' => "node/$node->nid", + ':language' => $language)) + ->fetchField(); + if ($path_in_use) { form_set_error('path', t('The path is already in use.')); } } @@ -208,7 +235,7 @@ 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(), ); } } @@ -234,5 +261,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(); } diff --git modules/path/path.test modules/path/path.test index c6781fc..ea9d09c 100644 --- modules/path/path.test +++ modules/path/path.test @@ -121,7 +121,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(); } }