You are here

function _linkchecker_cleanup_links in Link checker 7

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

Run perodically via cron and delete all links without a references.

For speed reasons and check results we keep the links for some time as they may be reused by other new content.

1 call to _linkchecker_cleanup_links()
linkchecker_cron in ./linkchecker.module
Implements hook_cron().

File

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

Code

function _linkchecker_cleanup_links() {

  // Remove disabled node types no longer in use.
  $node_types = linkchecker_scan_node_types();
  if (!empty($node_types)) {
    $subquery1 = db_select('node', 'n')
      ->fields('n', array(
      'nid',
    ))
      ->condition('n.type', $node_types, 'NOT IN');
    db_delete('linkchecker_node')
      ->condition('nid', $subquery1, 'IN')
      ->execute();

    // @todo Remove comments link references from table.
    // db_query('DELETE FROM {linkchecker_comment} WHERE cid IN (SELECT nid FROM {node} n WHERE n.type NOT IN (' . db_placeholders($node_types, 'varchar') . '))', $node_types);
  }
  else {

    // No active node_type. Remove all items from table.
    db_truncate('linkchecker_node')
      ->execute();

    // @todo Remove comments link references from table.
  }

  // Remove comment link references if comment scanning is disabled.
  // @todo Remove comments of unpublished nodes.
  $comment_types = linkchecker_scan_comment_types();
  if (empty($comment_types)) {
    db_truncate('linkchecker_comment')
      ->execute();
  }

  // Remove block link references if block scanning is disabled.
  if (variable_get('linkchecker_scan_blocks', 0) == 0) {
    db_truncate('linkchecker_block_custom')
      ->execute();
  }

  // Remove dead links without references.
  $linkchecker_node = db_select('linkchecker_node', 'ln')
    ->distinct()
    ->fields('ln', array(
    'lid',
  ));
  $linkchecker_comment = db_select('linkchecker_comment', 'lc')
    ->distinct()
    ->fields('lc', array(
    'lid',
  ));
  $linkchecker_block_custom = db_select('linkchecker_block_custom', 'lb')
    ->distinct()
    ->fields('lb', array(
    'lid',
  ));

  // UNION all linkchecker type tables.
  $subquery2 = db_select($linkchecker_block_custom
    ->union($linkchecker_comment)
    ->union($linkchecker_node), 'q1')
    ->distinct()
    ->fields('q1', array(
    'lid',
  ));
  db_delete('linkchecker_link')
    ->condition('lid', $subquery2, 'NOT IN')
    ->execute();
}