function _linkchecker_add_block_custom_links in Link checker 7
Add custom block links to database.
Parameters
array|object $block_custom: The fully populated custom block object.
int $bid: Block id from table {block}.bid.
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.
5 calls to _linkchecker_add_block_custom_links()
- linkchecker_block_custom_add_form_submit in ./
linkchecker.module - Custom submit handler for block add page.
- linkchecker_block_custom_configure_form_submit in ./
linkchecker.module - Custom submit handler for block configure page.
- _linkchecker_batch_import_block_custom_op in ./
linkchecker.batch.inc - Batch operation: Scan one by one block for links.
- _linkchecker_batch_single_block_custom_import_op in ./
linkchecker.batch.inc - Run single block link extraction.
- _linkchecker_status_handling in ./
linkchecker.module - Status code handling.
File
- ./
linkchecker.module, line 1422 - This module periodically check links in given node types, blocks etc.
Code
function _linkchecker_add_block_custom_links($block_custom, $bid, $skip_missing_links_detection = FALSE) {
// Convert custom block array to object.
// @todo: Are we able to remove this global conversion?
$block_custom = (object) $block_custom;
// Custom blocks really suxxx as it's very inconsistent core logic (values are
// integers or strings) and there are no usable hooks. Try to workaround this
// bad logic as good as possible to prevent warnings/errors.
// NOTE: Only custom blocks from block.module are supported. Skip all others.
if ($block_custom->module != 'block' || !is_numeric($block_custom->delta) || !is_numeric($bid) || $block_custom->delta != $bid) {
return;
}
$filter = new stdClass();
$filter->settings['filter_url_length'] = 72;
// Create array of custom block fields to scan. All fields cannot exists.
$text_items = array();
if (!empty($block_custom->info)) {
$text_items[] = _filter_url($block_custom->info, $filter);
}
// $block_custom from editing/scanning a block. See block_custom_block_save().
if (!empty($block_custom->body) && is_array($block_custom->body) && array_key_exists('value', $block_custom->body) && array_key_exists('format', $block_custom->body)) {
$text_items[] = _linkchecker_check_markup($block_custom->body['value'], $block_custom->body['format']);
}
// Extract all links in a custom block.
$links = array_keys(_linkchecker_extract_links(implode(' ', $text_items)));
// Custom block 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_block_custom_links_missing($bid, $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_block_custom')
->fields(array(
'bid' => $bid,
'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_block_custom($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/structure/block');
}
}
}
// Remove dead link references for cleanup reasons as very last step.
_linkchecker_cleanup_block_custom_references($bid, $links);
}