=== modified file 'wikitools.module'
--- wikitools.module	2008-04-02 11:48:19 +0000
+++ wikitools.module	2008-04-23 13:34:17 +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($may_cache) {
@@ -122,6 +176,18 @@
       t('The options <em>Node Creation</em>, <em>Node Search</em> and <em>Automatic Redirect</em> work only if a wiki path is set or if freelinking hijacking is enabled. They take the page name from the path after the wiki path, i.e. <em>wikipath/Page Name</em>, or the page name of a freelinking link, i.e. <em>freelinking/Page Name</em>.') .'</li><li>'.
       t('The option <em>Automatic Redirect</em> works only if node revisions are created.') .'</li></ul>',
   );
+  $form['wikitools_404_type'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Wiki 404 type'),
+    '#description' => t('Select the 404 (page not found) type for all pages under the wiki path.'),
+    '#multiple' => TRUE,
+    '#options' => array(
+      'Link to search' => t('Link to search'),
+      'Link to creation' => t('Link to creation'),
+      'Creation form' => t('Creation form'),
+    ),
+    '#default_value' => wikitools_404(),
+  );
   $form['wikitools_disallowed_characters'] = array(
     '#type' => 'textfield',
     '#title' => t('Disallowed characters in titles'),
@@ -239,6 +305,16 @@
 }
 
 /**
+ * What 404 error settings are set?
+ */
+function wikitools_404($value = NULL) {
+  if (is_null($value)) {
+    return variable_get('wikitools_404_type', array('Link to search', 'Creation form'));
+  }
+  variable_set('wikitools_404_type', $values);
+}
+
+/**
  * String of characters which are not allowed in a wiki page title.
  */
 function wikitools_disallowed_characters($value = NULL) {
@@ -440,11 +516,19 @@
       // 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";
       }
+      // HACK: Force rebuilding of menu.  This is necessary because of the
+      // way menu entries are added in Drupal 5.x for specific nodes.
+      global $_menu;
+      $_menu = NULL;
+      // Set query path so that we look like we are coming to $url.
+      menu_set_active_item($url);
+      // Generate the node.
+      $output = menu_execute_active_handler();
     }
     else if (count($found_nodes) > 1) {
       // Multiple match for title.
@@ -587,7 +671,13 @@
  *   title of new node
  */
 function wikitools_create_url($type, $title) {
-  return url("node/add/$type", 'edit[title]='. urlencode($title));
+  if (is_object($type)) {
+    $type_url_str = str_replace('_', '-', $type->type);
+  }
+  else {
+    $type_url_str = str_replace('_', '-', $type);
+  }
+  return url("node/add/$type_url_str", 'edit[title]='. urlencode($title));
 }
 
 /**
@@ -656,7 +746,7 @@
     $node = node_load($info->nid);
     $output .= node_view($node, TRUE, FALSE, FALSE);
   }
-  $output .= theme('wikitools_search_notice' ,$page_name);
+  $output .= theme('wikitools_search_notice', $page_name);
   if (!wikitools_enforce_unique_titles()) {
     $output .= theme('wikitools_create_notice', $page_name);
   }
@@ -668,15 +758,23 @@
   $node = current($moved_nodes);
   $output .= '<p>'. t('The new page name is !new_name', array('!new_name' => l($node->title, "node/$node->nid"))) .'</p>';
   // Todo: show all moved pages
-  $output .= theme('wikitools_search_notice' ,$page_name);
+  $output .= theme('wikitools_search_notice', $page_name);
   $output .= theme('wikitools_create_notice', $page_name);
   return $output;
 }
 
 function theme_wikitools_page_does_not_exist($page_name) {
   $output = '<p>'. t('The page %page_name does not exist.', array('%page_name' => $page_name)) .'</p>';
-  $output .= theme('wikitools_search_notice' ,$page_name);
-  $output .= theme('wikitools_create_notice', $page_name);
+  $settings = wikitools_404();
+  if ($settings['Link to search']) {
+    $output .= theme('wikitools_search_notice', $page_name);
+  }
+  if ($settings['Link to creation']) {
+    $output .= theme('wikitools_create_notice', $page_name);
+  }
+  if ($settings['Creation form']) {
+    $output .= theme('wikitools_create', $page_name);
+  }
   return $output;
 }
 
@@ -707,3 +805,28 @@
   }
   return $output;
 }
+
+function theme_wikitools_create($page_name) {
+  $node_types = wikitools_node_types();
+  $form = array();
+  $output = '';
+  if (wikitools_node_creation() && count($node_types)) {
+    $output .= '<p>'. t('You can create the page as:') .'</p>';
+    // Collapse the forms initially if there are more than one.
+    $collapsed = count($node_types) > 1 ? ' collapsed' : '';
+    // The form_alter hooks excpects the preset title in the GET array, so we put it there.
+    $_GET['edit']['title'] = $page_name;
+    foreach ($node_types as $type) {
+      drupal_add_js('misc/collapse.js');
+      $type = node_get_types('type', $type);
+      if (node_access('create', $type->type)) {
+        $output .= '<fieldset class="collapsible'. $collapsed .'"><legend>'. $type->name .'</legend>';
+        $output .= node_add($type->type);
+        $output .= '</fieldset>';
+      }
+    }
+    // Some of the callbacks could have set the page title, so we reset it.
+    drupal_set_title('Page does not exist: '. $page_name);
+  }
+  return $output;
+}

