You are here

function wikitools_handle_request in Wikitools 7

Same name and namespace in other branches
  1. 5 wikitools.module \wikitools_handle_request()
  2. 6 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 string reference to 'wikitools_handle_request'
wikitools_menu in ./wikitools.module
Implements hook_menu().

File

./wikitools.pages.inc, line 12
Page callbacks for wikitools pages.

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 = count(explode('/', wikitools_wiki_path()));
  }
  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) {
    $page_name = check_plain($page_name);

    // Try to find the current page with this name, limit the query by node types
    $result = db_query("SELECT nid, type FROM {node} WHERE type IN (:type) AND LOWER(title) = LOWER(:title)", array(
      ':type' => $node_types,
      'title' => $page_name,
    ));
    $found_nodes = array();
    foreach ($result as $node) {
      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,
        )), PASS_THROUGH);
        $output .= theme('wikitools_page_found', array(
          'page_name' => $page_name,
          'found_nodes' => $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_revision} r ON n.nid = r.nid WHERE LOWER(r.title) = LOWER(:title) ORDER BY n.vid DESC", array(
          ':title' => $page_name,
        ));
        $moved_nodes = array();
        foreach ($result as $node) {
          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,
            )), PASS_THROUGH);
            $output = theme('wikitools_page_moved', array(
              'page_name' => $page_name,
              'moved_nodes' => $moved_nodes,
            ));
          }
          else {
            drupal_set_title(t('Page does not exist: %page', array(
              '%page' => $page_name,
            )), PASS_THROUGH);
            $output = theme('wikitools_page_does_not_exist', array(
              'page_name' => $page_name,
            ));
          }
        }
      }
    }
  }
  if (empty($output)) {
    return MENU_NOT_FOUND;
  }
  return $output;
}