View source
<?php
define('EXTRACTING_SERVLET', 'extract/tika');
function apachesolr_attachments_menu() {
$items = array();
$items['admin/settings/apachesolr/attachments'] = array(
'title' => 'File attachments',
'description' => 'Administer Apache Solr Attachments.',
'page callback' => 'apachesolr_attachments_admin_page',
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr_attachments.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/settings/apachesolr/attachments/confirm/reindex'] = array(
'title' => 'Reindex all files',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'apachesolr_attachments_confirm',
5,
),
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr_attachments.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/settings/apachesolr/attachments/confirm/delete'] = array(
'title' => 'Delete and reindex all files',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'apachesolr_attachments_confirm',
5,
),
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr_attachments.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/settings/apachesolr/attachments/confirm/clear-cache'] = array(
'title' => 'Delete the local cache of file text',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'apachesolr_attachments_confirm',
5,
),
'access arguments' => array(
'administer search',
),
'file' => 'apachesolr_attachments.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function apachesolr_attachments_help($section) {
switch ($section) {
case 'admin/settings/apachesolr/index':
if (!variable_get('apachesolr_read_only', 0)) {
$remaining = 0;
$total = 0;
$status = apachesolr_attachments_search('status');
$remaining += $status['remaining'];
$total += $status['total'];
return t('<br />There @items remaining to be examined for attachments out of @total total.', array(
'@items' => format_plural($remaining, t('is 1 post'), t('are @count posts')),
'@total' => $total,
));
}
break;
}
}
function apachesolr_attachments_search($op = 'search', $keys = NULL) {
switch ($op) {
case 'name':
return '';
case 'reset':
apachesolr_clear_last_index('apachesolr_attachments');
return;
case 'status':
return apachesolr_index_status('apachesolr_attachments');
case 'search':
return array();
}
}
function apachesolr_attachments_apachesolr_types_exclude($namespace) {
if ($namespace == 'apachesolr_attachments') {
if (variable_get('apachesolr_attachments_exclude_types', 1)) {
$excluded_types = variable_get('apachesolr_search_excluded_types', array());
return array_filter($excluded_types);
}
return array();
}
}
function apachesolr_attachments_update_index() {
if (!variable_get('apachesolr_attachments_tika_path', '') && variable_get('apachesolr_attachment_extract_using', 'tika') == 'tika') {
return;
}
$start = time();
$cron_try = variable_get('apachesolr_attachements_cron_try', 20);
$cron_limit = variable_get('apachesolr_attachments_cron_limit', 100);
$cron_time_limit = variable_get('apachesolr_attachements_cron_time_limit', 15);
$num_tried = 0;
module_load_include('inc', 'apachesolr_attachments', 'apachesolr_attachments.admin');
do {
$rows = apachesolr_get_nodes_to_index('apachesolr_attachments', $cron_try);
$success = apachesolr_index_nodes($rows, 'apachesolr_attachments', 'apachesolr_attachments_add_documents');
$num_tried += $cron_try;
} while ($success && $num_tried < $cron_limit && time() - $start < $cron_time_limit);
}
function apachesolr_attachments_nodeapi($node, $op) {
if ($op == 'delete' || $op == 'update' && !$node->status) {
apachesolr_attachments_remove_attachments_from_index($node->nid);
db_query("UPDATE {apachesolr_attachments_files} SET removed = 1 WHERE nid = %d", $node->nid);
}
}
function apachesolr_attachments_cron() {
try {
$solr = apachesolr_get_solr();
$result = db_query("SELECT fid, nid FROM {apachesolr_attachments_files} WHERE removed = 1");
$ids = array();
$fids = array();
while ($file = db_fetch_object($result)) {
$ids[] = apachesolr_document_id($file->fid . '-' . $file->nid, 'file');
$fids[] = $file->fid;
}
if ($ids) {
$solr
->deleteByMultipleIds($ids);
$solr
->commit();
db_query("DELETE FROM {apachesolr_attachments_files} WHERE fid IN (" . db_placeholders($fids) . ")", $fids);
}
} catch (Exception $e) {
watchdog('ApacheSolrAttach', nl2br(check_plain($e
->getMessage())) . ' in apachesolr_attachments_cron', NULL, WATCHDOG_ERROR);
}
}
function apachesolr_attachments_apachesolr_modify_query(&$query, &$params, $caller) {
if ($caller == 'apachesolr_search') {
$params['fl'] .= ',ss_filemime,ss_file_node_title,ss_file_node_url';
}
elseif ($caller == 'apachesolr_mlt') {
$query
->add_filter('entity', 'file', TRUE);
}
}
function apachesolr_attachments_apachesolr_process_results(&$results) {
foreach ($results as &$item) {
if (isset($item['node']->ss_filemime)) {
$nid = $item['node']->nid;
$item['link'] = file_create_url($item['node']->path);
$node_link = t('<em>attached to:</em> !node_link', array(
'!node_link' => l($item['node']->ss_file_node_title, 'node/' . $nid),
));
$icon = theme('filefield_icon', array(
'filemime' => $item['node']->ss_filemime,
));
$file_type = t('!icon @filemime', array(
'@filemime' => $item['node']->ss_filemime,
'!icon' => $icon,
));
$item['snippet'] .= '<span>' . $file_type . ' ' . $node_link . '</span>';
$item['extra'] = array();
$item['type'] = t('File attachment');
}
}
}
function apachesolr_attachments_remove_attachments_from_index($nid) {
try {
$solr = apachesolr_get_solr();
$solr
->deleteByQuery("nid:{$nid} AND entity:file AND hash:" . apachesolr_site_hash());
$solr
->commit();
} catch (Exception $e) {
watchdog('ApacheSolrAttach', nl2br(check_plain($e
->getMessage())), NULL, WATCHDOG_ERROR);
}
}