You are here

function _linkchecker_extract_node_links in Link checker 7

Same name and namespace in other branches
  1. 6.2 linkchecker.module \_linkchecker_extract_node_links()

Extracts links from a node.

Parameters

object $node: The fully populated node object.

bool $return_field_names: If set to TRUE, the returned array will contain the link URLs as keys, and each element will be an array containing all field names in which the URL is found. Otherwise, a simple array of URLs will be returned.

Return value

array An array whose keys are fully qualified and unique URLs found in the node (as returned by _linkchecker_extract_links()), or a more complex structured array (see above) if $return_field_names is TRUE.

2 calls to _linkchecker_extract_node_links()
_linkchecker_add_node_links in ./linkchecker.module
Add node links to database.
_linkchecker_link_node_ids in ./linkchecker.module
Returns IDs of nodes that contain a link which the current user may be allowed to view.

File

./linkchecker.module, line 1184
This module periodically check links in given node types, blocks etc.

Code

function _linkchecker_extract_node_links($node, $return_field_names = FALSE) {
  $filter = new stdClass();
  $filter->settings['filter_url_length'] = 72;

  // Create array of node fields to scan.
  $text_items = array();
  $text_items_by_field = array();

  // Add fields typically not used for urls to the bottom. This way a link may
  // found earlier while looping over $text_items_by_field below.
  $text_items_by_field = array_merge($text_items_by_field, _linkchecker_parse_fields('node', $node->type, $node, TRUE));
  $text_items_by_field['title'][] = _filter_url($node->title, $filter);
  $text_items = _linkchecker_array_values_recursive($text_items_by_field);

  // Get the absolute node path for extraction of relative links.
  $languages = language_list();

  // Note: An "undefined language" (value: 'und') isn't listed in the available
  // languages variable $languages.
  $url_options = empty($node->language) || empty($languages[$node->language]) ? array(
    'absolute' => TRUE,
  ) : array(
    'language' => $languages[$node->language],
    'absolute' => TRUE,
  );
  $path = url('node/' . $node->nid, $url_options);

  // Extract all links in a node.
  $links = _linkchecker_extract_links(implode(' ', $text_items), $path);

  // Return either the array of links, or an array of field names containing
  // each link, depending on what was requested.
  if (!$return_field_names) {
    return $links;
  }
  else {
    $field_names = array();
    foreach ($text_items_by_field as $field_name => $items) {
      foreach ($items as $item) {
        foreach ($links as $uri => $link) {

          // We only need to do a quick check here to see if the URL appears
          // anywhere in the text; if so, that means users with access to this
          // field will be able to see the URL (and any private data such as
          // passwords contained in it). This is sufficient for the purposes of
          // _linkchecker_link_node_ids(), where this information is used.
          foreach ($link as $original_link) {
            if (strpos($item, $original_link) !== FALSE) {
              $field_names[$uri][$field_name] = $field_name;
            }
            elseif (strpos($item, str_replace('&', '&', $original_link)) !== FALSE) {
              $field_names[$uri][$field_name] = $field_name;
            }
          }
        }
      }
    }
    return $field_names;
  }
}