Click here to Skip to main content
14,734,298 members
Articles » Web Development » PHP » General
Tip/Trick
Posted 9 May 2017

Tagged as

Stats

7.4K views

RSS Feed from SOLR in Drupal

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
9 May 2017CPOL
Fetching articles from SOLR for RSS feed instead of querying in database

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 to generate rss feed document
function generateRssFeed($tid) {
  // Prepare SOLR query
  $query = apachesolr_drupal_query('');
  $params=array();
  $params += apachesolr_search_basic_params($query);
 
  //Apply filter in query to fetch article from specific term-id
  $params['fq'] .= ' tid:' . $tid; // $tid is term id
  $params['fq'] .= ' type: "article" ';
  
  // other attributes
  $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';
  
  // Add sort by publish date
  // remove available sorts
  $solr_query_sort = $query->get_available_sorts();
  foreach ($solr_query_sort as $sort_key => $sort_element) {
    $query->remove_available_sort($sort_key);
  }
  // add new sort condition
  $params['sort'] = 'ds_pubDate desc';
  
  // Fetch data from SOLR
  $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));
    

  // Prepare RSS document
  $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 );
  
  // generate feed item for each node in result
  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

// supporting function to append element in xml document
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

// Generate and return rss feed for term-id 1234
drupal_set_header('Content-Type: application/atom+xml');
print generateRssFeed(1234);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Visveswaraiah
Technical Lead HCL Technologies
India India
No Biography provided

Comments and Discussions

 
QuestionSuperb article Pin
msnathan11-May-17 21:23
Membermsnathan11-May-17 21:23 
GeneralGreat article for Drupal and SOLR integration Pin
magendirans11-May-17 1:29
professionalmagendirans11-May-17 1:29 
QuestionGreat Article Pin
Sorna Kumar10-May-17 23:49
MemberSorna Kumar10-May-17 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.