Introduction
Querying data for RSS feed in database is a heavy process. As an alternative, we can query data from SOLR server.
Background
When creating RSS feeds in website to display articles from a specific category, the drupal has to fetch a list of nodes related to specific taxonomy term. Usually, node table in drupal will have huge number of records, querying node table along with taxonomy term is a heavy task.
If website has configured SOLR search engine, it will be better to query data from SOLR instead of database.
Using the Code
The below code can be used to fetch articles from SOLR, this code will work on top of apachesolr module. If the website is already using SOLR, then this module should be available on the website.
Function to Generate RSS Feed for Specific term-id
function generateRssFeed($tid) {
$query = apachesolr_drupal_query('');
$params=array();
$params += apachesolr_search_basic_params($query);
$params['fq'] .= ' tid:' . $tid;
$params['fq'] .= ' type: "article" ';
$no_of_rows =10;
$page = 0;
$params['rows'] = $no_of_rows;
$params['start'] = $page * $no_of_rows;
$params['fl'] .= 'label, type, nid, ds_pubDate, teaser, tid, is_domain_source, changed';
$solr_query_sort = $query->get_available_sorts();
foreach ($solr_query_sort as $sort_key => $sort_element) {
$query->remove_available_sort($sort_key);
}
$params['sort'] = 'ds_pubDate desc';
$solr = apachesolr_get_solr();
$current_query = apachesolr_current_query($query);
$response = $solr->search($query->get_query_basic(), $params['start'],
(isset($params['rows']) ? $params['rows'] : 10), $params);
$result = apachesolr_process_response($response, $query, $params);
$numFound = $response->response->numFound;
$author = 'website name';
$title = 'Feed Title';
$url = url($_GET['q'], array('absolute' => TRUE));
$xml_feed = null;
$xml = new DOMDocument( "1.0", "utf-8" );
$xml_feed = $xml->createElement( "feed" );
$xml->appendChild( $xml_feed );
$xml_feed->setAttribute( "xmlns", "http://www.w3.org/2005/Atom" );
$xml_feed->appendChild( createElementWithText( $xml, "title", $title ));
$xml_link = $xml->createElement( "link" );
$xml_link->setAttribute( "href", $url );
$xml_feed->appendChild($xml_link);
$xml_feed->appendChild( $xml->createElement( "id", $url ));
$xml_feed->appendChild( $xml->createElement( "updated", date("c") ));
$xml_author = $xml->createElement( "author" );
$xml_feed->appendChild($xml_author);
$xml_author->appendChild( createElementWithText( $xml, "name", $author ));
$xml_link = $xml->createElement( "link" );
$xml_link->setAttribute( "rel", "self" );
$xml_link->setAttribute( "href", url($_GET['q'], array('absolute' => TRUE, 'query' => $query)) );
$xml_feed->appendChild( $xml_link );
foreach($result as $response) {
$article_data_obj = $response['fields'];
$xml_item = $xml->createElement( "entry" );
$xml_feed->appendChild($xml_item);
$xml_item->appendChild( createElementWithText( $xml, "title", $article_data_obj['title'] ) );
$link = check_url($article_data_obj['url']);
$xml_link = $xml->createElement( "link" );
$xml_link->setAttribute( "href", $link );
$xml_item->appendChild( $xml_link );
$xml_item->appendChild( $xml->createElement( "id", $link ) );
$xml_summary = rss_solr_createElementWithText( $xml, "summary", $article_data_obj['teaser']);
$xml_summary->setAttribute( "type", "html" );
$xml_item->appendChild( $xml_summary );
}
return $xml->saveXML();
}
Supporting Function to Create Element with Text
function createElementWithText($doc, $name, $child_text) {
$element = $doc->createElement($name);
$element_text = $doc->createTextNode($child_text);
$element->appendChild($element_text);
return $element;
}
Output RSS Feed
drupal_set_header('Content-Type: application/atom+xml');
print generateRssFeed(1234);