'admin/settings/swish/main', 'title' => 'swish settings',
'callback' => 'swish_settings',
'access' => user_access('administer swish-e'),
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10
);
$items[] = array(
'path' => 'admin/settings/swish/index', 'title' => 'swish indexer',
'callback' => 'swish_index',
'access' => user_access('administer swish-e'),
'type' => MENU_LOCAL_TASK
);
return $items;
}
/**
* swish_settings callback function
*/
function swish_settings(){
swish_validate_paths();
$form['swish_path'] = array(
'#type' => 'textfield',
'#title' => t('Swish-E Path'),
'#default_value' => variable_get('swish_path', '/usr/local/bin/swish-e'),
'#size' => 60,
'#maxlength' => 120,
'#description' => t('The absolute path of the swish-e binary.'),
);
$form['catdoc_path'] = array(
'#type' => 'textfield',
'#title' => t('M$ Word Filter Path'),
'#default_value' => variable_get('catdoc_path', '/usr/local/bin/catdoc'),
'#size' => 60,
'#maxlength' => 120,
'#description' => t('The absolute path of the M$ Word filter.'),
);
$form['xls2csv_path'] = array(
'#type' => 'textfield',
'#title' => t('M$ Excel Filter Path'),
'#default_value' => variable_get('xls2csv_path', '/usr/local/bin/xls2csv'),
'#size' => 60,
'#maxlength' => 120,
'#description' => t('The absolute path of the M$ Excel filter'),
);
$form['ppthtml_path'] = array(
'#type' => 'textfield',
'#title' => t('M$ Powerpoint Filter Path'),
'#default_value' => variable_get('ppthtml_path', '/usr/local/bin/ppthtml'),
'#size' => 60,
'#maxlength' => 120,
'#description' => t('The absolute path of the M$ Excel filter'),
);
$form['pdftotext_path'] = array(
'#type' => 'textfield',
'#title' => t('PDF Filter Path'),
'#default_value' => variable_get('pdftotext_path', '/usr/local/bin/pdftotext'),
'#size' => 60,
'#maxlength' => 120,
'#description' => t('The absolute path of the PDF filter.'),
);
$form['unrtf_path'] = array(
'#type' => 'textfield',
'#title' => t('RTF Filter Path'),
'#default_value' => variable_get('unrtf_path', '/usr/bin/unrtf'),
'#size' => 60,
'#maxlength' => 120,
'#description' => t('The absolute path of the Rich Text Format filter.'),
);
return $form;
}
/**
* swish_settings callback function
*/
function swish_index() {
if (swish_validate_paths()) {
drupal_set_message('Swish module settings errors exist. Fix it '. l('here.', 'admin/settings/swish'), 'error');
$disable = 'disabled';
}
else {
$op = $_POST['op'];
if ($op) {
$result = _swish_do_index();
if (substr($result[0], 0, 5) == 'err: ' ){
drupal_set_message('Indexing aborted. '.$result[0], 'error');
}
else {
$length = count($result);
//include only the last 3 lines
for ($i = $length - 1 ; $i >= ($length - 3) ; --$i)
$msg .= $result[$i]."
";
drupal_set_message($msg);
}
}
}
$form['index'] = array(
'#type' => 'submit',
'#value' => 'Index Now',
'#attributes' => array('disable' => $disable),
);
$output = drupal_get_form('Swish Indexer', $form);
return print theme('page', $output, 'Swish Indexer');
}
/**
* This will be invoked by the search module.
* Returns the Header, and array of items found, if any.
*/
function swish_search ($op = 'search', $keys = null){
switch ($op) {
case 'name':
return t('uploaded files');
case 'reset':
return;
case 'search' :
$find = array();
$swish_bin = variable_get('swish_path', '/usr/local/bin/swish-e');
$swish_index = getcwd().'\\files\\my_swish_index';
//check if swish-e binary is executable and index file is readable.
//if not, put an entry in the watchdog and just exit quitely.
//we dont want to disturb the end user.
if (!is_executable($swish_bin) && !is_readable($swish_index) && !is_readable($swish_index.'.prop')) {
watchdog('swish', 'Search: Swish binary is not executable or doesnt exist. Or, index files are not readable or do not exist.
Fix it '. l('here.', 'admin/settings/swish'));
return array();
}
$words = '"'.chop(str_replace('\(', '(', str_replace('\)', ')', str_replace('\*','*', escapeshellcmd($keys).' ')))).'"';
$swish_command = variable_get("swish_path","/usr/local/bin/swish-e") . ' -m 50 -f '. getcwd() . '\\files\\my_swish_index -w '.$words;
exec ($swish_command, $results, $return_val);
foreach ($results as $result){
if (substr($result,0,1) == '#' || substr($result,0,1) == '.' ){
//TODO: parse swish result comments
//for now, just continue;
continue;
}
else if (substr($result, 0, 5) == 'err: ' ){
//if swish-e coughs error msgs, just exit and display no
// document found.
watchdog('swish', 'Swish-e Barfed: '.$result.'
Command: '.$swish_command);
return array();
}
else { //if there are results.
$k = strpos($result,' '); //first space
$i = strpos($result,' "');
$j = strpos ($result, '" ');
$rank = substr($result, 0, $k);
$basename = basename(substr($result, $k+1, $i-$k-1));
$l = strlen($basename);
$filetype = substr($basename, $l-3, 3 );
$title = substr($result, $i + 2, $j -$i -2 );
$file_size = substr($result, $j+2);
$extra = array(
'rank' => $rank,
'file_size' => $file_size,
'file_type' => $filetype );
$link = file_create_url ("files/".$basename);
$find[] = array('link' => $link, 'title' => $title, 'extra' => $extra);
}
}
return $find;
}
}
/**
* Overrides the _search_item hook.
* Returns a nice html output of each found item.
*/
function swish_search_item($item){
global $base_url;
$extra = $item['extra'];
$output = '