--- path_redirect.module 2007-11-04 17:32:32.000000000 -0800
+++ path_redirect_new.module 2007-11-06 12:27:29.000000000 -0800
@@ -23,14 +23,44 @@ function path_redirect_help($section) {
*
*/
function path_redirect_init() {
- // see if this page has a redirect path
+ // first we build an array of possible path entries for the current path
$query = '';
if (isset($_GET['q'])) {
- $query = $_GET['q'];
+ $query = $_GET['q']; // the raw value
}
- $r = db_fetch_object(db_query("SELECT redirect, query, fragment, type FROM {path_redirect} WHERE path = '%s' OR path = '%s'", $query, utf8_encode($query)));
+ $paths[] = $query;
+ $utf = utf8_encode($query); // the utf8 version
+ if ($utf != $query) {
+ $paths[] = $utf;
+ }
+ // then we stick '<*>' in each position and add those to the possibilities
+ $args = explode('/', $query);
+ foreach($args as $key => $val) {
+ $temp = $args;
+ $temp[$key] = '<*>';
+ $temp_path = implode('/', $temp);
+ if (!in_array($temp_path, $paths)) {
+ $paths[] = implode('/', $temp);
+ }
+ }
+ // sanitize everything
+ foreach ($paths as $key => $val) {
+ $paths[$key] = db_escape_string($val);
+ }
+ $r = db_fetch_object(db_query("SELECT path, redirect, query, fragment, type FROM {path_redirect} WHERE path IN ('". implode("','", $paths) ."') LIMIT 1"));
if ($r) {
- // if there's a result found, do the redirect
+ if (strpos($r->path, '<*>') !== FALSE) {
+ //find the position of the wildcard
+ $rargs = explode('/', $r->path);
+ foreach ($rargs as $key => $val) {
+ if (strpos($val, '<*>') !== FALSE) {
+ $r->redirect = str_replace('<*>', $args[$key], $r->redirect);
+ $r->query = str_replace('<*>', $args[$key], $r->query);
+ $r->fragment = str_replace('<*>', $args[$key], $r->fragment);
+ break;
+ }
+ }
+ }
drupal_goto($r->redirect, ($r->query ? $r->query: NULL), ($r->fragment ? $r->fragment : NULL), $r->type);
}
}
@@ -124,7 +154,8 @@ function path_redirect_admin($rid = FALS
$count = db_num_rows($result);
$types = path_redirect_error_list();
while ($r = db_fetch_object($result)) {
- $path = drupal_get_path_alias($r->path);
+ $wildcard = strpos($r->path, '<*>') !== FALSE;
+ $path = $wildcard ? $r->path : drupal_get_path_alias($r->path);
$redirect = drupal_get_path_alias($r->redirect);
$query = $r->query ? "?$r->query" : '';
$fragment = $r->fragment ? "#$r->fragment" : '';
@@ -132,7 +163,7 @@ function path_redirect_admin($rid = FALS
$path,
check_url($redirect . $query . $fragment),
$types[$r->type]['title'],
- array('data' => l(t('test'), $r->path, array(), $r->query ? $r->query : NULL, $r->fragment ? $r->fragment : NULL)),
+ array('data' => $wildcard ? '' : l(t('test'), $r->path, array(), $r->query ? $r->query : NULL, $r->fragment ? $r->fragement : NULL)),
array('data' => l(t('edit'), 'admin/build/path_redirect/edit/'. $r->rid)),
array('data' => l(t('delete'), 'admin/build/path_redirect/edit/'. $r->rid .'/delete')),
);
@@ -172,7 +203,9 @@ function path_redirect_edit($edit = arra
$form['path'] = array(
'#type' => 'textfield',
'#title' => t('From'),
- '#description' => t('Enter a Drupal path or path alias to redirect'),
+ '#description' => t('Enter a Drupal path or path alias to redirect.
+ A single wildcard of <*> can be used as a distinct path segment
+ (separated by /).
Example: photos/<*>/large'),
'#default_value' => drupal_get_path_alias($edit['path']),
);
@@ -181,7 +214,12 @@ function path_redirect_edit($edit = arra
'#prefix' => '
<*>
+ in any of these fields to have the value of the wildcard appear in the redirected path.', array('%front' => '