function _linkchecker_add_comment_links in Link checker 7
Same name and namespace in other branches
- 5.2 linkchecker.module \_linkchecker_add_comment_links()
- 6.2 linkchecker.module \_linkchecker_add_comment_links()
Add comment links to database.
Parameters
object $comment: The fully populated comment object.
bool $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.
4 calls to _linkchecker_add_comment_links()
- linkchecker_comment_insert in ./
linkchecker.module - Implements hook_comment_insert().
- linkchecker_comment_update in ./
linkchecker.module - Implements hook_comment_update().
- _linkchecker_batch_comments_import_op in ./
linkchecker.batch.inc - Batch operation: Scan one by one comment for links.
- _linkchecker_batch_single_comment_import_op in ./
linkchecker.batch.inc - Run single comment link extraction.
File
- ./
linkchecker.module, line 1328 - This module periodically check links in given node types, blocks etc.
Code
function _linkchecker_add_comment_links($comment, $skip_missing_links_detection = FALSE) {
$filter = new stdClass();
$filter->settings['filter_url_length'] = 72;
// Create array of comment fields to scan.
$text_items = array();
$text_items[] = _filter_url($comment->subject, $filter);
$text_items = array_merge($text_items, _linkchecker_parse_fields('comment', $comment->node_type, $comment));
// Get the absolute node path for extraction of relative links.
$languages = language_list();
$node = node_load($comment->nid);
$url_options = empty($node->language) || empty($languages[$node->language]) ? array(
'absolute' => TRUE,
) : array(
'language' => $languages[$node->language],
'absolute' => TRUE,
);
$path = url('node/' . $comment->nid, $url_options);
// Extract all links in a comment.
$links = array_keys(_linkchecker_extract_links(implode(' ', $text_items), $path));
// Comment 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_comment_links_missing($comment->cid, $links);
// Only add unique links to database that do not exist.
$i = 0;
foreach ($missing_links as $url) {
$urlhash = drupal_hash_base64($url);
$link = db_query('SELECT lid FROM {linkchecker_link} WHERE urlhash = :urlhash', array(
':urlhash' => $urlhash,
))
->fetchObject();
if (!$link) {
$link = new stdClass();
$link->urlhash = $urlhash;
$link->url = $url;
$link->status = _linkchecker_link_check_status_filter($url);
drupal_write_record('linkchecker_link', $link);
}
db_insert('linkchecker_comment')
->fields(array(
'cid' => $comment->cid,
'lid' => $link->lid,
))
->execute();
// 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_link} 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_link} 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', 'linkchecker.batch');
batch_set(_linkchecker_batch_import_single_comment($comment->cid, $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('node/' . $comment->nid);
}
}
}
// Remove dead link references for cleanup reasons as very last step.
_linkchecker_cleanup_comment_references($comment->cid, $links);
}