You are here

function wikitools_handle_request in Wikitools 5

Same name and namespace in other branches
  1. 6 wikitools.pages.inc \wikitools_handle_request()
  2. 7 wikitools.pages.inc \wikitools_handle_request()

Menu callback for wiki path. This function is called if a page without an alias is called below the wiki path.

1 call to wikitools_handle_request()
wikitools_menu in ./wikitools.module
Implementation of hook_menu().
1 string reference to 'wikitools_handle_request'
wikitools_menu in ./wikitools.module
Implementation of hook_menu().

File

./wikitools.module, line 483
A non-intrusive module to have some wiki-like behaviour.

Code

function wikitools_handle_request() {
  $output = '';

  // Create list of path parts.
  $args = explode('/', $_GET['q']);

  // Calculate index of first path argument after wiki path.
  if (arg(0) != 'freelinking') {
    $i = strlen(wikitools_wiki_path()) ? count(explode('/', wikitools_wiki_path())) : 0;
  }
  else {
    $i = 1;
  }

  // Determine subpage.
  $subpage = NULL;
  if (wikitools_subpages_handling() == 'query') {

    // Check if a query string is in the URL with a valid subpage
    if (isset($_GET[wikitools_subpages_query_string()])) {
      $subpage = $_GET[wikitools_subpages_query_string()];
      if (!in_array($subpage, wikitools_subpages())) {
        $subpage = NULL;
      }
    }
  }
  elseif (wikitools_subpages_handling() == 'url') {

    // Check if there are more than one part, and if the last one is a valid subpage.
    if (count($args) - $i > 1 && in_array(end($args), wikitools_subpages())) {
      $subpage = end($args);
      array_pop($args);
    }
  }

  // Determine page name.
  if (isset($args[$i])) {
    $page_name = wikitools_decode_page_name(implode('/', array_slice($args, $i)));
  }
  else {

    // Use default page title if only wiki path is entered.
    $page_name = wikitools_main_page_title();
  }

  // Don't do anything if no node types are active or no page name is available
  $node_types = wikitools_node_types();
  if (count($node_types) && $page_name) {

    // Try to find the current page with this name.
    $result = db_query("SELECT nid, type FROM {node} WHERE LOWER(title) = LOWER('%s')", $page_name);
    $found_nodes = array();
    while ($node = db_fetch_object($result)) {
      if (wikitools_type_affected($node->type)) {
        $found_nodes[] = $node;
      }
    }
    if (count($found_nodes) == 1) {

      // Single match for title.
      $node = current($found_nodes);
      if ($subpage) {
        drupal_goto("node/{$node->nid}/{$subpage}");
      }
      else {
        drupal_goto("node/{$node->nid}");
      }
    }
    else {
      if (count($found_nodes) > 1) {

        // Multiple match for title.
        drupal_set_title(t('Page found: %page', array(
          '%page' => $page_name,
        )));
        $output .= theme('wikitools_page_found', $page_name, $found_nodes);
      }
      else {

        // No match for title. Try to find an old page with this name
        $result = db_query("SELECT n.nid, n.type, n.title FROM {node} n LEFT JOIN {node_revisions} r ON n.nid = r.nid WHERE LOWER(r.title) = LOWER('%s') ORDER BY n.vid DESC", $page_name);
        $moved_nodes = array();
        while ($node = db_fetch_object($result)) {
          if (wikitools_type_affected($node->type)) {
            $moved_nodes[] = $node;
          }
        }
        if (count($moved_nodes) > 0 && wikitools_auto_redirect() && !isset($_REQUEST['noredirect'])) {
          $node = current($moved_nodes);
          drupal_set_message(t('Redirected from <a href="@url">%page</a>', array(
            '%page' => $page_name,
            '@url' => wikitools_wikilink_url($page_name, 'noredirect'),
          )));
          drupal_goto("node/{$node->nid}");
        }
        else {
          if (count($moved_nodes) > 0) {
            drupal_set_title(t('Page moved: %page', array(
              '%page' => $page_name,
            )));
            $output = theme('wikitools_page_moved', $page_name, $moved_nodes);
          }
          else {
            $wiki_path = wikitools_wiki_path();
            if (empty($wiki_path)) {
              return FALSE;
            }
            drupal_set_title(t('Page does not exist: %page', array(
              '%page' => $page_name,
            )));
            $output = theme('wikitools_page_does_not_exist', $page_name);
          }
        }
      }
    }
  }
  return $output;
}