You are here

function _linkchecker_add_node_links in Link checker 5.2

Same name and namespace in other branches
  1. 6.2 linkchecker.module \_linkchecker_add_node_links()
  2. 7 linkchecker.module \_linkchecker_add_node_links()

Add node links to database.

3 calls to _linkchecker_add_node_links()
linkchecker_nodeapi in ./linkchecker.module
_linkchecker_batch_node_import_op in ./linkchecker.module
Batch operation: Load all nodes, 100 by hundred.
_linkchecker_scan_node_links in ./linkchecker.module
Scan specified node for links. Helper function for job_queue scans.

File

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

Code

function _linkchecker_add_node_links($node) {

  // Create array of node fields to scan.
  $text_items = array();
  $text_items[] = _filter_url($node->title, $node->format);
  $text_items[] = _linkchecker_check_markup($node->body, $node->format, FALSE);
  $text_items[] = _linkchecker_check_markup($node->teaser, $node->format, FALSE);

  // Search for links in 'weblink' nodes from 'links' module package.
  if (module_exists('links_weblink') && $node->type == 'weblink' && isset($node->links_weblink_url)) {
    $text_items[] = _filter_url($node->links_weblink_url, $node->format);
  }

  // Search for links in 'weblinks' nodes from 'weblinks' module.
  if (module_exists('weblinks') && $node->type == 'weblinks' && isset($node->url)) {
    $text_items[] = _filter_url($node->url, $node->format);
  }

  // Search for CCK-fields of types 'link' and 'text'.
  if (module_exists('content')) {
    $fields = content_fields(NULL, $node->type);
    foreach ($fields as $field) {
      if (!empty($node->{$field['field_name']})) {
        if (module_exists('link') && $field['type'] == 'link') {
          foreach ($node->{$field}['field_name'] as $delta => $item) {
            $text_items[] = _filter_url($item['url'], $node->format);
          }
        }
        elseif (module_exists('text') && $field['type'] == 'text') {
          foreach ($node->{$field}['field_name'] as $delta => $item) {
            $text_items[] = _filter_url($item['value'], $node->format);
          }
        }
      }
    }
  }

  // Get the absolute node path for extraction of relative links.
  $path = url('node/' . $node->nid, NULL, NULL, TRUE);

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

  // Node have links.
  if (!empty($links)) {

    // Remove all links from the links array already in the database
    // and only add missing links to database.
    $missing_links = _linkchecker_node_links_missing($node->nid, $links);

    // Add a job for scanning the next LINKCHECKER_SCAN_MAX_LINKS_PER_RUN links via job_queue module.
    $missing_links_count = count($missing_links) - LINKCHECKER_SCAN_MAX_LINKS_PER_RUN;
    if (module_exists('job_queue') && $missing_links_count > 0) {
      job_queue_add('_linkchecker_scan_node_links', 'Scan node ' . $node->nid . ' having ' . $missing_links_count . ' links not yet added to linkchecker_links table.', array(
        $node->nid,
      ), '', FALSE);
    }

    // Only add links to database that do not exists.
    $i = 0;
    foreach ($missing_links as $link) {
      $lid = db_result(db_query("SELECT lid FROM {linkchecker_links} WHERE token = '%s'", md5($link)));
      if (!$lid) {
        $lid = db_next_id('linkchecker_links_lid');
        db_query("INSERT INTO {linkchecker_links} (lid, token, url, status) VALUES (%d, '%s', '%s', %d)", $lid, md5($link), $link, _linkchecker_link_check_status_filter($link));
      }
      db_query("INSERT INTO {linkchecker_nodes} (nid, lid) VALUES (%d, %d)", $node->nid, $lid);

      // Break processing if max links limit per run has been reached.
      $i++;
      if ($i >= LINKCHECKER_SCAN_MAX_LINKS_PER_RUN) {
        break;
      }
    }
  }

  // Remove dead link references for cleanup reasons as very last step.
  _linkchecker_cleanup_node_references($node->nid, $links);
}