Skip to content

Commit

Permalink
Only deal with the 'strings' on the admin page.
Browse files Browse the repository at this point in the history
Build the facet/term/etc array, and store in their respective
Drupal variables, so instead of parsing all of them manually, we
can leave it up to variable_get to unserialize (or have it cached).
Should clean up code a little, and possibly be more efficient?
  • Loading branch information
adam-vessey committed Dec 2, 2011
1 parent 8698bd5 commit 07c2a74
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 65 deletions.
8 changes: 3 additions & 5 deletions IslandoraSolrQueryProcessor.inc
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,9 @@ class IslandoraSolrQueryProcessor {

$facetArray = array();
$facetFields = '';
$rawFacetVals = variable_get("islandora_solr_search_block_facets", 'dc.subject,dc.type');
$facetArray = islandora_build_substitution_list($rawFacetVals);
$facetFields = implode(",", array_keys($facetArray));
$facetFields = implode(",", variable_get("islandora_solr_search_block_facets", array('dc.subject', 'dc.type')));
$keys = array('');
$snippetArray = islandora_build_substitution_list(variable_get("islandora_solr_snippet_field", ""));
$snippetArray = variable_get("islandora_solr_snippet_field", "");
if (is_array($snippetArray)) {
$keys = array_keys($snippetArray);
}
Expand All @@ -122,7 +120,7 @@ class IslandoraSolrQueryProcessor {
'hl' => 'true',
'hl.fl' => isset($keys[0]) ? trim($keys[0]) : NULL,
'hl.fragsize' => 400,
'facet.field' => explode(',', $facetFields), //comma separated list configured in the block config
'facet.field' => array_keys($facetFields), //comma separated list configured in the block config
);


Expand Down
71 changes: 32 additions & 39 deletions IslandoraSolrResults.inc
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ class IslandoraSolrResults {
public $resultFieldArray = array();
public $allSubsArray = array();

static function makeArray($in) {
return is_array($in)?
/**
* @deprecated
* This should be factored out in the near future...
*/
static function makeArray(&$in) {
/*return is_array($in)?
$in:
(isset($in)?
array($in):
array()
)
;
;*/
return (array)$in;
}

/**
Expand Down Expand Up @@ -337,14 +342,17 @@ class IslandoraSolrResults {
* arrays for output substitution
*/
function prepFieldSubstitutions() {
$rawFacetVals = variable_get("islandora_solr_search_block_facets", 'dc.subject ~ Subject,dc.type ~ Type');
$this->facetFieldArray = islandora_build_substitution_list($rawFacetVals);

$rawSearchTerms = variable_get('islandora_solr_searchterms', 'dc.title ~ Title');
$this->searchFieldArray = islandora_build_substitution_list($rawSearchTerms);

$rawResultFields = variable_get('islandora_solr_search_result_fields', 'dc.subject ~ Subject,dc.type ~ Type');
$this->resultFieldArray = islandora_build_substitution_list($rawResultFields);
$this->facetFieldArray = variable_get("islandora_solr_search_block_facets", array(
'dc.subject' => 'Subject',
'dc.type' => 'Type'
));
$this->searchFieldArray = variable_get('islandora_solr_searchterms', array(
'dc.title' => 'Title'
));
$this->resultFieldArray = variable_get('islandora_solr_search_result_fields', array(
'dc.subject' => 'Subject',
'dc.type' => 'Type'
));

$this->allSubsArray = array_merge($this->facetFieldArray, $this->searchFieldArray, $this->resultFieldArray);
}
Expand All @@ -362,12 +370,8 @@ class IslandoraSolrResults {

$disable_link = (array_search($filter, $fq) !== FALSE) || $number == $solrQueryProcessor->solrResult->response->numFound;

//$disable_link = strpos($islandora_fq, $key . ':"' . . '"') !== FALSE; //we don't want a link for this facet as we already used it
if ($islandora_fq && $islandora_fq != '-') {//there are existing facets in the query
if ($disable_link) {
//don't show link to this facet but include a link to remove it
}
else {
if (!$disable_link) {
//FIXME: This instance of 't' should be changed to format_string for Drupal 7 (not really translateable)
$filter_include = t('!filter!sep!remaining', array(
'!filter' => $filter,
Expand All @@ -384,13 +388,10 @@ class IslandoraSolrResults {

$filter_include = replaceSlashes($filter_include); //replace the slash so url does not break
$filter_exclude = replaceSlashes($filter_exclude); //replace the slash so url does not break
if ($disable_link) {
// we don't want to create a link, because we're already filtering on this value.
// Links to remove enabled facet filters are created down below.
}
else {//normal link
//Doesn't seem to be used...
//$evenodd = ( $facet_count % 2 ? 'odd' : 'even' );

if (!$disable_link) {//normal link
//$evenodd = ( $facet_count % 2 ? 'odd' : 'even' ); //Doesn't seem to be used...

$lplus = 'islandora/solr/search/' . $solrQueryProcessor->solrQuery . '/' . $filter_include . '/' . $solrQueryProcessor->solrDefType;
$lminus = 'islandora/solr/search/' . $solrQueryProcessor->solrQuery . '/' . $filter_exclude . '/' . $solrQueryProcessor->solrDefType;
$text = $name;
Expand Down Expand Up @@ -430,14 +431,17 @@ class IslandoraSolrResults {
if (count($test) > 0) {
$primary = array();
$hidden = array();
$shown_limit = variable_get('islandora_solr_search_block_facet_shown_limit', 5);
foreach ($field as $name => $number) {
if (count($primary) < variable_get('islandora_solr_search_block_facet_shown_limit', 5)) {
if (count($primary) < $shown_limit) {
$this->_addFacets($key, $name, $number, $solrQueryProcessor, $facet_count, $primary);
}
else {
$this->_addFacets($key, $name, $number, $solrQueryProcessor, $facet_count, $hidden);
}
}


if (count($primary) > 0) {
$facet_output .='<div class="islandora_solr_search_facet">';
$list_title = $this->facetFieldArray[$key];
Expand All @@ -449,10 +453,10 @@ class IslandoraSolrResults {
if (count($hidden) > 0) {
$list_attributes['class'] = implode(' ', array('islandora_solr_search_facet_list', 'facet_list'));

$facet_output .= "<a class='islandora_solr_facet_subheader islandora_solr_more_facets' href='#'><h7>" . t("Show more") . "</h7></a>";
$facet_output .= "<a class='islandora_solr_facet_subheader islandora_solr_more_facets' href='#'>" . t("Show more") . "</a>";

$facet_output .= "<div style='display:none;'>";
$facet_output .= "<a class='islandora_solr_facet_subheader islandora_solr_less_facets' href='#'><h7>" . t("Show less") . "</h7></a>";
$facet_output .= "<a class='islandora_solr_facet_subheader islandora_solr_less_facets' href='#'>" . t("Show less") . "</a>";
$facet_output .= theme_item_list($hidden, '', $list_type, $list_attributes);
$facet_output .= "</div>";
}
Expand Down Expand Up @@ -536,17 +540,6 @@ EOT;
* @return string
*/
function build_solr_search_form($repeat = NULL, $pathToSearchTerms = NULL, $query = NULL) {
$types = array();
$terms = trim(variable_get('islandora_solr_searchterms', 'dc.title ~ Title,dc.subject ~ Subject'));
$termsArray = preg_split('/[\n]/', $terms);
foreach ($termsArray as $term) {
$vals = split('~', $term);
if (!$vals[1]) {
$vals[1] = $vals[0];
}
$types[trim($vals[0])] = trim($vals[1]);
}

$queryArray = NULL;
if (isset($query)) {
//A regex like '\\) (OR|AND) \\(' should work, right? Nope. Seems that
Expand Down Expand Up @@ -576,7 +569,7 @@ EOT;
$repeat = variable_get('islandora_solr_search_block_repeat', '3');
}
$i = 1;
$this->_add_field($form, $i, $queryArray[0], $types);
$this->_add_field($form, $i, $queryArray[0], $this->searchFieldArray);

if ($repeat > 1 && $repeat < 9) { //don't want less then 2 or more then 9
for ($i = 2; $i < $repeat + 1; $i++) {
Expand All @@ -591,7 +584,7 @@ EOT;
'#default_value' => $andorj,
'#options' => $andOrArray
);
$this->_add_field($form, $i, $queryArray[$t], $types);
$this->_add_field($form, $i, $queryArray[$t], $this->searchFieldArray);
}
}

Expand Down
42 changes: 29 additions & 13 deletions includes/common.inc
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,39 @@ function get_search_terms_array($path = NULL, $file = NULL) {
}

function islandora_build_substitution_list($rawFieldList) {
$facetArray = array();
$facetFields = '';
$fieldArray = array();
$lines = preg_split('/[,|\n|\r]/', $rawFieldList);
foreach ($lines as $line) {
if( $line ) {
if ($line) {
$lineArray = explode('~', $line);
$key = trim($lineArray[0]);
$value = trim($lineArray[1]);
if (!$value) {
$value = $key;
$field = trim($lineArray[0]);
$human_readable_name = trim($lineArray[1]);
if (!$human_readable_name) {
$human_readable_name = $field;
}
$facetArray[$key] = $value;
if ($facetFields) {
$facetFields .= ",";
}
$facetFields .= $key;
$fieldArray[$field] = $human_readable_name;
}
}
return $facetArray;
return $fieldArray;
}

function islandora_stringify_substitutions($subs) {
if (is_array($subs)) {
$parts = array();
foreach ($subs as $field => $name) {
if ($field !== $name) {
$parts[] = $field .' ~ '. $name;
}
else {
$parts[] = $field;
}
}

//Implode with a newline... \n doesn't work?
return implode('
', $parts);
}
else { //This might be gotten rid of in the future, when string versions are extremely unlikely to be encountered (and therefore clobbered)
return $subs;
}
}
4 changes: 2 additions & 2 deletions islandora_solr_config/IslandoraSolrResultsTable.inc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class IslandoraSolrResultsTable extends IslandoraSolrResults {
unset($rowclass);
$result_num = $recordStart + $num + 1;
$rows[$row][] = array(
'data' => (isset($val_array['PID']) ? l($result_num,'fedora/repository/'.$val_array['PID']) : $result_num),
'data' => (isset($val_array['PID']) ? l($result_num,'fedora/repository/'. $val_array['PID']) : $result_num),
'header' => TRUE,
);
foreach ($fields as $field) {
Expand All @@ -131,7 +131,7 @@ class IslandoraSolrResultsTable extends IslandoraSolrResults {
if( variable_get('islandora_solr_search_debug_mode', 0) ) { // debug dump
$results_r .= "<pre>Results: ".print_r($results,TRUE)."</pre>";
$fieldset_r = array(
'#title' => t("Raw Results"),
'#title' => t("Raw Results"),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#value' => $results_r,
Expand Down
22 changes: 17 additions & 5 deletions islandora_solr_search.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -143,34 +143,34 @@ function islandora_solr_admin_settings(&$form_state) {
Enter terms on separate lines using the following pattern: field [tilde] preferred label. ie <strong>dc.title ~ Title</strong><br />
Review the schema.xml to see what terms are available. A list of the fields available when using the schema.xml packaged with the Islandora Solr
module is available in Islandora's online documentation."),
'#default_value' => variable_get('islandora_solr_searchterms', 'dc.title ~ Title'),
'#default_value' => islandora_stringify_substitutions(variable_get('islandora_solr_searchterms', array('dc.title' => 'Title'))),
'#wysiwyg' => FALSE,
);
$form['islandora_solr_snippet_field'] = array(
'#type' => 'textfield',
'#title' => t('Snippet Field'),
'#default_value' => variable_get('islandora_solr_snippet_field', ''),
'#default_value' => islandora_stringify_substitutions(variable_get('islandora_solr_snippet_field', array())),
'#description' => t("If a match is found on this field, a snippet of text will be returned, with the search term highlighted.<br />
An optional friendly label may inserted using the following pattern <strong>dsm.Text ~ Full Text</strong><br />
<strong>Note:</strong><em> This feature is not supported by all display profiles.</em> "),
'#wysiwyg' => FALSE,
);

$form['islandora_solr_search_block_facets'] = array(
'#type' => 'textarea',
'#title' => t('Facet Fields'),
'#description' => t("Indicate which fields will appear in the <strong>Islandora Facet Block</strong>.<br />
Enter terms on separate lines using the following pattern: field [tilde] preferred label. ie <strong>dc.title ~ Title</strong> "),
'#default_value' => variable_get('islandora_solr_search_block_facets', 'dc.subject ~ Subject,dc.type ~ Type'),
'#default_value' => islandora_stringify_substitutions(variable_get('islandora_solr_search_block_facets', array('dc.subject' => 'Subject', 'dc.type' => 'Type'))),
'#wysiwyg' => FALSE,
'#default_value' => variable_get('islandora_solr_search_block_facets', t('dc.subject ~ Subject,dc.type ~ Type'))
);

$form['islandora_solr_search_result_fields'] = array(
'#type' => 'textarea',
'#title' => t('Labels for Returned Fields'),
'#description' => t('Set labels for fields returned from query.
Enter terms on separate lines using the following pattern: field [tilde] preferred label. ie <strong>dc.title ~ Title</strong>'),
'#default_value' => variable_get('islandora_solr_search_result_fields', 'dc.subject ~ Subject,dc.type ~ Type'),
'#default_value' => islandora_stringify_substitutions(variable_get('islandora_solr_search_result_fields', array('dc.subject' => 'Subject', 'dc.type' => 'Type'))),
'#wysiwyg' => FALSE,
);

Expand Down Expand Up @@ -349,6 +349,14 @@ function solr_settings_form_submit($form, &$form_state) {
// Exclude unnecessary elements.
unset($form_state['values']['submit'], $form_state['values']['reset'], $form_state['values']['form_id'], $form_state['values']['op'], $form_state['values']['form_token'], $form_state['values']['form_build_id']);

$toBlowUp = array(
'islandora_solr_search_result_fields',
'islandora_solr_search_block_facets',
'islandora_solr_searchterms',
'islandora_solr_search_sort',
"islandora_solr_snippet_field"
);

foreach ($form_state['values'] as $key => $value) {
if ($op == t('Reset to defaults')) {
variable_del($key);
Expand All @@ -357,6 +365,9 @@ function solr_settings_form_submit($form, &$form_state) {
if (is_array($value) && isset($form_state['values']['array_filter'])) {
$value = array_keys(array_filter($value));
}
elseif (array_search($key, $toBlowUp) !== FALSE) {
$value = islandora_build_substitution_list($value);
}
variable_set($key, $value);
}
}
Expand All @@ -367,6 +378,7 @@ function solr_settings_form_submit($form, &$form_state) {
drupal_set_message(t('The solr configuration options have been saved.'));
}

//This cache clearing and rebuilding really necessary? -avessey
cache_clear_all();
drupal_rebuild_theme_registry();
}
Expand Down
1 change: 1 addition & 0 deletions islandora_solr_search.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ form#islandora-solr-simple-search-form input#edit-islandora-simple-search-query
{
font-size:90%;
line-height:1.3em;
clear: both;
}

img.islandora_solr_add_remove_link
Expand Down
2 changes: 1 addition & 1 deletion islandora_solr_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ function theme_islandora_solr_search_block_form($form) {
* @param type $dismax
* @return type
*/
function islandora_solr_search($query, $fq=NULL, $dismax=NULL) {
function islandora_solr_search($query='', $fq=NULL, $dismax=NULL) {
global $queryClass;
islandora_solr_search_init();

Expand Down

0 comments on commit 07c2a74

Please sign in to comment.