You are here

function _linkchecker_add_box_links in Link checker 5.2

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

Add box links to database.

5 calls to _linkchecker_add_box_links()
linkchecker_block_add_form_submit in ./linkchecker.module
Custom submit handler for block add page.
linkchecker_block_configure_form_submit in ./linkchecker.module
Custom submit handler for block configure page.
_linkchecker_batch_import_boxes_op in ./linkchecker.module
Batch operation: Load all boxes, 100 by 100.
_linkchecker_scan_box_links in ./linkchecker.module
Scan specified box for links. Helper function for job_queue scans.
_linkchecker_status_handling in ./linkchecker.module
Status code handling.

File

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

Code

function _linkchecker_add_box_links($box, $bid) {

  // Create array of box fields to scan.
  $text_items = array();
  $text_items[] = _filter_url($box['info'], $box['format']);
  $text_items[] = _filter_url($box['title'], $box['format']);
  $text_items[] = _linkchecker_check_markup($box['body'], $box['format'], FALSE);

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

  // Box 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_box_links_missing($bid, $links);

    // Add a job for scanning the next 100 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_box_links', 'Scan block ' . $bid . ' having ' . $missing_links_count . ' links not yet added to linkchecker_links table.', array(
        $bid,
      ), '', FALSE);
    }

    // Only add unique links to database that do not exist.
    $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_boxes} (bid, lid) VALUES (%d, %d)", $bid, $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_box_references($bid, $links);
}