You are here

function _linkchecker_add_box_links in Link checker 6.2

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

Add block links to database.

Parameters

array $box: The fully populated block array.

integer $bid: Block id from table {blocks}.bid.

$skip_missing_links_detection: To prevent endless batch loops the value need to be TRUE. With FALSE the need for content re-scans is detected by the number of missing links.

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 includes/linkchecker.batch.inc
Batch operation: Scan ony by one block for links.
_linkchecker_batch_single_box_import_op in includes/linkchecker.batch.inc
_linkchecker_status_handling in ./linkchecker.module
Status code handling.

File

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

Code

function _linkchecker_add_box_links($box, $bid, $skip_missing_links_detection = FALSE) {

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

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

  // Box has 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);

    // Only add unique links to database that do not exist.
    $i = 0;
    foreach ($missing_links as $url) {
      $urlhash = md5($url);
      $link = db_fetch_object(db_query("SELECT lid FROM {linkchecker_links} WHERE urlhash = '%s'", $urlhash));
      if (!$link) {
        $link = new stdClass();
        $link->urlhash = $urlhash;
        $link->url = $url;
        $link->status = _linkchecker_link_check_status_filter($url);
        drupal_write_record('linkchecker_links', $link);
      }
      db_query("INSERT INTO {linkchecker_boxes} (bid, lid) VALUES (%d, %d)", $bid, $link->lid);

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

    // The first chunk of links not yet found in the {linkchecker_links} table
    // have now been imported by the above code. If the number of missing links
    // still exceeds the scan limit defined in LINKCHECKER_SCAN_MAX_LINKS_PER_RUN
    // the content need to be re-scanned until all links have been collected and
    // saved in {linkchecker_links} table.
    //
    // Above code has already scanned a number of LINKCHECKER_SCAN_MAX_LINKS_PER_RUN
    // links and need to be substracted from the number of missing links to
    // calculate the correct number of re-scan rounds.
    //
    // To prevent endless loops the $skip_missing_links_detection need to be TRUE.
    // This value will be set by the calling batch process that already knows
    // that it is running a batch job and the number of required re-scan rounds.
    $missing_links_count = count($missing_links) - LINKCHECKER_SCAN_MAX_LINKS_PER_RUN;
    if (!$skip_missing_links_detection && $missing_links_count > 0) {
      module_load_include('inc', 'linkchecker', '/includes/linkchecker.batch');
      batch_set(_linkchecker_batch_import_single_box($bid, $missing_links_count));

      // If batches were set in the submit handlers, we process them now,
      // possibly ending execution. We make sure we do not react to the batch
      // that is already being processed (if a batch operation performs a
      // drupal_execute).
      if (($batch =& batch_get()) && !isset($batch['current_set'])) {
        batch_process('admin/build/block');
      }
    }
  }

  // Remove dead link references for cleanup reasons as very last step.
  _linkchecker_cleanup_box_references($bid, $links);
}