diff --git globalredirect.info globalredirect.info index 0eeeae6..3ce5387 100644 --- globalredirect.info +++ globalredirect.info @@ -5,3 +5,4 @@ files[] = globalredirect.module files[] = globalredirect.install files[] = globalredirect.admin.inc files[] = globalredirect.test +configure = admin/config/system/globalredirect diff --git globalredirect.install globalredirect.install index 69ad73b..2569312 100644 --- globalredirect.install +++ globalredirect.install @@ -19,14 +19,12 @@ function globalredirect_update_6100() { $deslash = variable_get('globalredirect_deslah', variable_get('globalredirect_deslash', 1)); variable_set('globalredirect_deslash', $deslash); variable_del('globalredirect_deslah'); - return array(); } function globalredirect_update_6101() { - $ret = array(); - // Get the default settings + drupal_load('module', 'globalredirect'); $defaults = _globalredirect_get_settings(TRUE); // Define a settings array based on the variables already set (or their defaults) @@ -62,6 +60,4 @@ function globalredirect_update_6101() { variable_del('globalredirect_trailingzero'); variable_del('globalredirect_menu_check'); variable_del('globalredirect_case_sensitive_urls'); - - return array(); } diff --git globalredirect.module globalredirect.module index 4ab3739..97253db 100644 --- globalredirect.module +++ globalredirect.module @@ -6,27 +6,21 @@ * are not using the aliases which reduces the risk of duplicate content. */ - /** * Implements hook_help(). */ -function globalredirect_help($section) { - switch ($section) { - case 'admin/modules#description': +function globalredirect_help($path, $arg) { + switch ($path) { + case 'admin/help#globalredirect': return t('This module will do a 301 redirect for all nodes which have an alias but are not using that alias.'); } } - /** * Implements hook_init(). */ function globalredirect_init() { global $language; - #dpm(request_uri()); - #dpm($_GET['q']); - #dpm($_REQUEST['q']); - #return; /** * Get the settings. @@ -35,33 +29,35 @@ function globalredirect_init() { // If GlobalRedirect is inactive (for any of the reasons listed in // _globalredirect_is_active()) then skip the rest of the function. - if (!_globalredirect_is_active($settings)) return FALSE; - + if (!_globalredirect_is_active($settings)) { + return FALSE; + } // If menu checking is enabled, do the check. Note: Feature disabled by default. if ($settings['menu_check']) { // Check the access on the current path, return FALSE if access not // allowed. This stops redirection for paths without access permission. $item = menu_get_item(); - if (!$item['access']) return FALSE; + if (!$item['access']) { + return FALSE; + } } - - // Store the destination from the $_GET as, if we leave it in, drupal_goto will go to that instead. + // Store the destination from the $_REQUEST as it breaks things if we leave + // it in - restore it at the end... if (isset($_GET['destination'])) { $destination = $_GET['destination']; unset($_GET['destination']); } - - // Get the query string parameters. If none set, set to NULL + // Get the Query String (minus the 'q'). If none set, set to NULL $query_string = drupal_get_query_parameters(); if (empty($query_string)) { $query_string = NULL; } - - // Establish the language prefix that should be used, ie. the one that drupal_goto() would use + // Establish the language prefix that should be used, ie. the one that + // drupal_goto() would use $options = array( 'fragment' => '', 'query' => $query_string, @@ -76,18 +72,17 @@ function globalredirect_init() { $request_path = globalredirect_request_path(); - // Let the language_url_rewrite do it's magic, if preset. + // Let the language_url_rewrite_url do it's magic, if preset. // TODO: Is this needed anymore? - if (function_exists('language_url_rewrite')) { - // Note 1 : the language_url_rewrite() takes path (by reference) as the + if (function_exists('language_url_rewrite_url')) { + // Note 1 : the language_url_rewrite_url() takes path (by reference) as the // first argument but does not use it at all // Note 2 : We use $request_path here as we want the path in an untouched // form (current_path() gets modified by core) - language_url_rewrite($request_path, $options); + language_url_rewrite_url($request_path, $options); } $prefix = rtrim($options['prefix'], '/'); - // Do a check if this is a front page if (drupal_is_front_page()) { // Redirect if the current request does not refer to the front page in the @@ -138,7 +133,6 @@ function globalredirect_init() { } } - // If Content Translation module is enabled then check the path is correct. if ($settings['language_redirect'] && module_exists('translation') && (arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == '')) { switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) { @@ -185,13 +179,10 @@ function globalredirect_init() { } } - - // Find an alias (if any) for the request $langcode = isset($options['language']->language) ? $options['language']->language : ''; $alias = drupal_get_path_alias($current_path, $langcode); - // TODO: This looks wrong for D7... maybe a hook? if (function_exists('custom_url_rewrite_outbound')) { // Modules may alter outbound links by reference. @@ -201,19 +192,19 @@ function globalredirect_init() { $prefix .= '/'; } - // Alias case sensitivity check. - // NOTE: This has changed. In D6 the $alias matched the request (in terms of case), - // however in D7 $alias is already a true alias (accurate in case), and therefore not the "true" request... - // So, if the alias and the request path are case-insensitive the same then, if Case Senitive URL's are - // enabled, the alias SHOULD be the accurate $alias from above, otherwise it should be the request_path()... + // NOTE: This has changed. In D6 the $alias matched the request (in terms of + // case). However in D7 $alias is already a true alias (accurate in case), + // and therefore not the "true" request... So, if the alias and the request + // path are case-insensitive the same then, if Case Sensitive URL's are + // enabled, the alias SHOULD be the accurate $alias from above, otherwise it + // should be the request_path()... // TODO: Test if this breaks the language checking above! if (strcasecmp($alias, request_path()) == 0) { // The alias and the request are identical (case insensitive)... Therefore... $alias = $settings['case_sensitive_urls'] ? $alias : request_path(); } - // Compare the request to the alias. This also works as a 'deslashing' // agent. If we have a language prefix then prefix the alias if ($request_path != $prefix . $alias) { @@ -223,17 +214,16 @@ function globalredirect_init() { } } - // If no alias was returned, the final check is to direct non-clean to // clean - if clean is enabled if ($settings['nonclean_to_clean'] && ((bool)variable_get('clean_url', 0)) && strpos(request_uri(), '?q=')) { drupal_goto($current_path, $options, 301); } - // Restore the destination from earlier so its available in other places. - if (isset($destination)) $_GET['destination'] = $destination; - + if (isset($destination)) { + $_GET['destination'] = $destination; + } // Add the canonical link to the head of the document if desired. // TODO - The Canonical already gets set by Core for node page views... See http://api.drupal.org/api/function/node_page_view/7 @@ -244,14 +234,12 @@ function globalredirect_init() { )); } - // Add the Content-Location header to the page if ($settings['content_location_header']) { drupal_add_http_header('Content-Location', url(drupal_is_front_page() ? '' : $request_path, array('absolute' => TRUE, 'query' => $query_string))); } } - /** * Implements hook_menu(). */ @@ -268,15 +256,6 @@ function globalredirect_menu() { return $items; } - -/** - * Drupal 6 backport of drupal_is_cli(). - */ -function globalredirect_is_cli() { - return (!isset($_SERVER['SERVER_SOFTWARE']) && (PHP_SAPI == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0))); -} - - /** * Internal function to determine if GlobalRedirect is active. * Several rules have to be checked prior to execution, such as an empty post array, @@ -292,17 +271,17 @@ function _globalredirect_is_active($settings) { * @see http://drupal.org/node/205810 * @see http://drupal.org/node/278615 */ - if ($_SERVER['SCRIPT_NAME'] != $GLOBALS['base_path'] . 'index.php') return FALSE; - + if ($_SERVER['SCRIPT_NAME'] != $GLOBALS['base_path'] . 'index.php') { + return FALSE; + } /** * If this is a command line request (Drush, etc), skip processing. */ - if (globalredirect_is_cli()) { + if (drupal_is_cli()) { return FALSE; } - /** * Check if the request is an attempted url mask */ @@ -310,31 +289,35 @@ function _globalredirect_is_active($settings) { return FALSE; } - /** * If the site is in offline mode there is little point doing any of this as * you might end up redirecting to a 503. */ - if (variable_get('site_offline', 0) == 1) return FALSE; - + if (variable_get('site_offline', 0) == 1) { + return FALSE; + } /** * If there is something posted, GlobalRedirect is not active */ - if (!empty($_POST)) return FALSE; - + if (!empty($_POST)) { + return FALSE; + } /** * If drupal_get_path_alias isn't preset, GlobalRedirect is not active */ - if (!function_exists('drupal_get_path_alias')) return FALSE; - + if (!function_exists('drupal_get_path_alias')) { + return FALSE; + } /** - * If menu_check is enabled AND the menu_get_item function is missing, GlobalRedirect is disabled + * If menu_check is enabled AND the menu_get_item function is missing, + * GlobalRedirect is disabled. */ - if ($settings['menu_check'] && !function_exists('menu_get_item')) return FALSE; - + if ($settings['menu_check'] && !function_exists('menu_get_item')) { + return FALSE; + } /** * We seem to have passed all the tests - let say we're active @@ -342,7 +325,6 @@ function _globalredirect_is_active($settings) { return TRUE; } - /** * Return the settings with any defaults mapped over the top */ diff --git globalredirect.test globalredirect.test index 1caea07..c5262e6 100644 --- globalredirect.test +++ globalredirect.test @@ -239,13 +239,14 @@ class GlobalRedirectTestCase extends DrupalWebTestCase { class GlobalRedirectTestCaseDefault extends GlobalRedirectTestCase { - function getInfo() { + public static function getInfo() { return array( 'name' => '1. Global Redirect - Default Settings', 'description' => 'Ensure that Global Redirect functions correctly', 'group' => 'Global Redirect', ); } + function testGlobalRedirect() { variable_set('globalredirect_settings', array( 'deslash' => 1, @@ -260,13 +261,14 @@ class GlobalRedirectTestCaseDefault extends GlobalRedirectTestCase { class GlobalRedirectTestCaseConfigAlpha extends GlobalRedirectTestCase { - function getInfo() { + public static function getInfo() { return array( 'name' => '2. Global Redirect - Config Alpha', 'description' => 'Ensure that Global Redirect functions correctly', 'group' => 'Global Redirect', ); } + function testGlobalRedirect() { variable_set('globalredirect_settings', array( 'deslash' => 0,