Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add paged Atom feed option to search results #365

Open
wants to merge 12 commits into
base: 7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions islandora_solr_config/includes/admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function islandora_solr_config_admin_rss_settings($form, &$form_state) {
'managingEditor' => '',
'webMaster' => '',
));
$rss_limit = variable_get('islandora_solr_config_rss_limit', 50);

$form = array(
'#tree' => TRUE,
Expand Down Expand Up @@ -110,6 +111,12 @@ function islandora_solr_config_admin_rss_settings($form, &$form_state) {
'#description' => t('Email address for person responsible for technical issues relating to channel.'),
'#default_value' => $rss_channel['webMaster'] ? $rss_channel['webMaster'] : '',
);
$form['rss_limit'] = array(
'#type' => 'textfield',
'#title' => 'RSS results limit',
'#description' => 'Maximum number of results via RSS.',
'#default_value' => $rss_limit,
);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
Expand All @@ -120,7 +127,6 @@ function islandora_solr_config_admin_rss_settings($form, &$form_state) {
'#value' => t('Reset to defaults'),
'#weight' => 51,
);

return $form;
}

Expand All @@ -135,10 +141,11 @@ function islandora_solr_config_admin_rss_settings_submit($form, &$form_state) {
// Get values.
$rss_item = $form_state['values']['rss_item'];
$rss_channel = $form_state['values']['rss_channel'];

$rss_limit = $form_state['values']['rss_limit'];
// Set variable.
variable_set('islandora_solr_config_rss_item', $rss_item);
variable_set('islandora_solr_config_rss_channel', $rss_channel);
variable_set('islandora_solr_config_rss_limit', $rss_limit);
}

// On reset.
Expand Down
248 changes: 248 additions & 0 deletions islandora_solr_config/includes/atom_results.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
<?php

/**
* @file
* Contains methods to search solr and display results. depends on
* Apache_Solr_Php client.
*/

/**
* Extention of IslandoraSolrResults for templating purposes.
* This overrides the displayResults function to provide an alternate display
* type.
*/

class IslandoraSolrResultsAtom extends IslandoraSolrResults {

/**
* Outputs results basically in the normal way.
*
* Thumbnails pulled from the Fedora repository.
*
* @param object $islandora_solr_query
* A solr query object.
*
* @return string
* html output for the resultset. Note: we currently create this
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved
* output manually, should refactor to use drupal forms api.
*/
public function printAtom($islandora_solr_query, $title = "Search Results") {

global $base_url;

drupal_add_http_header('Content-Type', 'application/atom+xml; charset=utf-8');
$islandora_solr_query->solrLimit = variable_get('islandora_solr_config_rss_limit', 50);
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved
$islandora_solr_query->executeQuery();

// Get raw results.
$solr_result = $islandora_solr_query->islandoraSolrResult;

// All results.
$docs = $solr_result['response']['objects'];

// Loop over results.
$items = NULL;
foreach ($docs as $doc) {
// Turn arrays into strings.
foreach ($doc['solr_doc'] as $key => $value) {
if (is_array($value)) {
// Turn array into comma separated string and trim.
$doc[$key] = trim(implode(', ', $value));
}
else {
// Give it a trim.
$doc[$key] = trim($value);
}
}
// Get the variables for the <item> element.
$item = $this->rssItem($doc);
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved

// Hook alter to change the rssItem before formatting.
drupal_alter('islandora_solr_search_rss_item', $item, $doc);
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved

// Render rss item.
$rendered_item = format_rss_item($item['title'], $item['link'], $item['description'], $item['items']);
$open_tag = "<item>";
$close_tag = "</item>";
$rendered_item = str_replace($open_tag, "<entry>", $rendered_item);
$rendered_item = str_replace($close_tag, "</entry>", $rendered_item);
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved

// ... allow it to be altered...
drupal_alter('islandora_solr_config_rss_item_post_render', $rendered_item, $doc);
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved

// ... and add to items string.
$items .= "$rendered_item\n";
}

// Query search terms:
$query = $islandora_solr_query->solrQuery;

// Get the variables for the <channel> element.
$channel = $this->rssChannel($query);
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved
drupal_alter('islandora_solr_config_rss_root_element_attributes', $channel, $items);
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved

// Give the results clean variable names.
$title = $channel['title'];
$url = $channel['url'];
$feed_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (strpos($feed_url, '&page') == FALSE) {
$page_number = '1';
}
else {
$arr = explode('&page=', $feed_url);
$page_number = $arr[1];
}

$link_self = $feed_url;
if (isset($arr)) {
$link_first = $arr[0];
}
else {
$link_first = $feed_url;
}
$page_next = $page_number + 1;
if ($page_number > 1) {
$page_prev = $page_number - 1;
}
$link_next = $link_first . '&page=' . $page_next;
if (isset($page_prev)) {
$link_prev = $link_first . '&page=' . $page_prev;
}
$description = $channel['description'];
$langcode = $channel['langcode'];
$args = $channel['args'];

$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$output .= '<feed xmlns="http://www.w3.org/2005/Atom">' . "\n";
$output .= ' <link rel="self" href="' . $link_self . '"/>' . "\n";
$output .= ' <link rel="first" href="' . $link_first . '"/>' . "\n";
if (isset($link_prev)) {
$output .= ' <link rel="previous" href="' . $link_prev . '"/>' . "\n";
}
$output .= ' <link rel="next" href="' . $link_next . '"/>';
$strip_channel = format_rss_channel($title, $url, $description, $items, $langcode, $args);
$tags = array(
"<channel>",
"</channel>",
);
$output .= str_replace($tags, "", $strip_channel);
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved
$output .= "</feed>\n";

print $output;
exit;
}


/**
* Function for setting the values of the <item> elements for the RSS display.
*
* @tutorial http://feed2.w3.org/docs/rss2.html#hrelementsOfLtitemgt
*
* @return array
* variable that holds all values to be rendered into <item> elements
*/
public function rssItem($doc) {
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved
// Set variables.
global $base_url;

// Get variables.
$rss_item = variable_get('islandora_solr_config_rss_item', array(
'title' => 'fgs_label_s',
'description' => '',
'author' => '',
'category' => '',
'pubDate' => 'fgs_createdDate_s',
'enclosure_dsid' => 'TN',
));

// Object url.
$object_url = url('islandora/object/' . htmlspecialchars($doc['PID'], ENT_QUOTES, 'utf-8'), array('absolute' => TRUE));
// Enclosure file url (thumbnail by default).
$dsid = ($rss_item['enclosure_dsid'] && isset($rss_item['enclosure_dsid'])) ? $rss_item['enclosure_dsid'] : 'TN';
$doc['datastreams'] = isset($doc['datastreams']) ? $doc['datastreams'] : array();
$is_datastream = in_array($dsid, $doc['datastreams']);

if ($is_datastream) {
$enclosure_url = $object_url . '/datastream/' . $dsid;
}

$rss_source = variable_get('site_name', "Default site name");

// Set the variables to be rendered as elements in the <item> element.
$result = array();
$result['title'] = ($rss_item['title'] && isset($doc[$rss_item['title']])) ? $doc[$rss_item['title']] : '';
$result['link'] = $object_url;
$result['description'] = ($rss_item['description'] && isset($doc[$rss_item['description']])) ? $doc[$rss_item['description']] : '';
$result['items'] = array(
array(
'key' => 'author',
'value' => ($rss_item['author'] && isset($doc[$rss_item['author']])) ? $doc[$rss_item['author']] : ''),
array(
'key' => 'guid',
'value' => $doc['PID'],
'attributes' => array('isPermaLink' => 'false')),
array(
'key' => 'id',
'value' => $doc['PID']),
array(
'key' => 'pubDate',
'value' => ($rss_item['pubDate'] && isset($doc[$rss_item['pubDate']])) ? $doc[$rss_item['pubDate']] : ''),
array(
'key' => 'category',
'value' => ($rss_item['category'] && isset($doc[$rss_item['category']])) ? $doc[$rss_item['category']] : ''),
array(
'key' => 'comments',
'value' => ''),
array(
'key' => 'source',
'value' => $rss_source, 'attributes' => array('url' => $base_url)),
);

if ($is_datastream) {
$result['items'][] = array(
'key' => 'enclosure',
'value' => '', 'attributes' => array(
'url' => $enclosure_url, 'length' => '',
'type' => ''));
}

return $result;
}

/**
* Function to set values of the <channel> elements for the RSS display.
*
* @tutorial http://feed2.w3.org/docs/rss2.html#requiredChannelElements
*
* @return array
* variable that holds all values to be rendered into <channel> elements
*/
public function rssChannel($query) {
bondjimbond marked this conversation as resolved.
Show resolved Hide resolved
// Set variables.
global $base_url;
$rss_channel = variable_get('islandora_solr_config_rss_channel', array(
'copyright' => '',
'managingEditor' => '',
'webMaster' => '',
));

// Set the variables to be rendered as elements in the <channel> element.
$result = array();
$result['title'] = t('@site_name aggregator', array('@site_name' => variable_get('site_name', 'Drupal')));
$result['url'] = $base_url;
$result['description'] = t('Aggregated search results of: @query', array('@query' => $query));
$result['langcode'] = NULL;
$result['args'] = array(
array(
'key' => 'copyright',
'value' => $rss_channel['copyright']),
array(
'key' => 'managingEditor',
'value' => $rss_channel['managingEditor']),
array(
'key' => 'webMaster',
'value' => $rss_channel['webMaster']),
);
return $result;
}
}
2 changes: 2 additions & 0 deletions islandora_solr_config/includes/rss_results.inc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class IslandoraSolrResultsRSS extends IslandoraSolrResults {
global $base_url;

drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
$islandora_solr_query->solrLimit = variable_get('islandora_solr_config_rss_limit', 50);
$islandora_solr_query->executeQuery();

// Get raw results.
$solr_result = $islandora_solr_query->islandoraSolrResult;
Expand Down
1 change: 1 addition & 0 deletions islandora_solr_config/islandora_solr_config.install
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function islandora_solr_config_uninstall() {
$variables = array(
'islandora_solr_table_profile_display_row_no',
'islandora_solr_table_profile_table_class',
'islandora_solr_config_rss_limit',
);
foreach ($variables as $variable) {
variable_del($variable);
Expand Down
9 changes: 9 additions & 0 deletions islandora_solr_config/islandora_solr_config.module
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ function islandora_solr_config_islandora_solr_secondary_display() {
'description' => t("Show search results as RSS feed"),
'logo' => '<img src="' . $path . '/images/rss.png" class="secondary-display-rss" alt="' . t("RSS Feed") . '">',
),
'atom' => array(
'name' => t('Atom'),
'module' => 'islandora_solr_config',
'file' => 'includes/atom_results.inc',
'class' => 'IslandoraSolrResultsAtom',
'function' => 'printAtom',
'description' => t("Show search results as an Atom feed"),
'logo' => '<img src="' . $path . '/images/rss.png" class="secondary-display-rss" alt="' . t("Atom Feed") . '">',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have its own image and class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions for an Atom image?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like in general, the same icon is used for both Atom and RSS.

Copy link

@DiegoPino DiegoPino Jun 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its the same, when Atom was invented bitmaps where just a dream.... I would either go with the same or duplicate the file and give it another name so people could eventually decide for a replacement?

),
);
}

Expand Down