=== modified file 'wikitools.module' --- wikitools.module 2008-02-25 09:22:43 +0000 +++ wikitools.module 2008-04-23 13:15:20 +0000 @@ -23,6 +23,60 @@ */ /** + * Implementation of hook_init + * + * Early checking of URL requested. + * If a wiki node is refered to by "node/$node->nid", the user is + * redirected using drupal_goto() + * + * This code was lifted graciously from the path_redirect module. + */ +function wikitools_init() { + // find the path (usually $_GET['q']) + $path = substr(request_uri(), strlen($GLOBALS['base_path'])); + if (preg_match('/^\?q=/', $path)) { + $path = preg_replace(array('/^\?q=/', '/&/'), array('', '?'), $path, 1); + } + if (preg_match('/^node\/(\d+)$/', $path, $matches)) { + $node = node_load($matches[1]); + if (wikitools_type_affected($node->type)) { + $redirect = wikitools_wikilink_drupal_path($node->title); + } + } + + // do the redirect if we've managed to locate a wikilink + if ($redirect) { + if (function_exists('drupal_goto')) { + // if there's a result found, do the redirect + unset($_REQUEST['destination']); + drupal_goto($redirect); + } + else { + // page caching is turned on so drupal_goto() (common.inc) hasn't been loaded + global $base_url; + $url = $base_url .'/'. $redirect; + + // Remove newlines from the URL to avoid header injection attacks. + $url = str_replace(array("\n", "\r"), '', $url); + + // Before the redirect, allow modules to react to the end of the page request. + bootstrap_invoke_all('exit'); + + // Even though session_write_close() is registered as a shutdown function, we + // need all session data written to the database before redirecting. + session_write_close(); + + header('Location: '. $url, TRUE, 302); + + // The "Location" header sends a REDIRECT status code to the http + // daemon. In some cases this can go wrong, so we make sure none + // of the code below the drupal_goto() call gets executed when we redirect. + exit(); + } + } +} + +/** * Implementation of hook_menu(). */ function wikitools_menu() { @@ -431,11 +485,15 @@ // Single match for title. $node = current($found_nodes); if ($subpage) { - drupal_goto("node/$node->nid/$subpage"); + $url = "node/$node->nid/$subpage"; } else { - drupal_goto("node/$node->nid"); + $url = "node/$node->nid"; } + // Set query path so that the tabs from the node get displayed. + menu_set_active_item($url); + // Generate the node. + $output = menu_execute_active_handler($url); } else if (count($found_nodes) > 1) { // Multiple match for title.