topical media & game development
[] readme course(s) preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthought(s) appendix reference(s) example(s) resource(s) _

talk show tell print

portal-classes-core.rss.php



  <?PHP
  error_reporting(E_ALL);
  
  //include('extern/class.rss20.php');
  
  class Rss {
  
          var core;
          //var Rss20;
          
          function Rss (&core) {
                  this->core = &core;
                  //this->Rss20 = new rss20();
          }
          
          function getFeeds () {
          
                  arrFeeds = array();
                  
                  sql = "SELECT *
                                  FROM rss_feeds";
                  rssQueryResults = this->core->Db->getQueryResults(sql);
                  
                  for (i = 0 ; i < count(rssQueryResults) ; i++) {
                          
                          arrFeeds[] = array("id"                => rssQueryResults[i]['id'],
                                                                  "title"                => rssQueryResults[i]['title'],
                                                                  "url"                => rssQueryResults[i]['url'] );                        
                          
                  }
                  
                  //arrFeeds[] = array("id" => 1 , "title" => 'Test' , "url" => 'news.nationalgeographic.com/rss/global_warming.rss');
                  
                  return arrFeeds;        
          }
          
          function getFeedList () {
                  
                  arrFeeds = this->getFeeds();
  
                  selected = '';
                  list = '';
                  
                  list .= '<option value="">Select feed</option>';
                  
                  foreach (arrFeeds as feed) {
                          list .= '<option value="index.php?FeedId='.feed['id'].'">'.feed['name'].'</option>';
                  }        
                  return list;
          }
          
          function ajaxGetFeedItem () {
          
                  contentId = _GET['ContentId'];
                  
                  sql = "SELECT content_title, content_binary
                                  FROM content
                                  WHERE content_id = ".contentId." ";
                  contentQueryResult = this->core->Db->getQueryResult(sql);
                                  
                  echo contentQueryResult['content_title'].'%^'.contentQueryResult['content_binary'].'%^'.contentId.'!@#';
          }
          
          function getFeedItems () {
          
                  arrFeedItems = array();
                  
                  sql = "SELECT content_id
                                  FROM content_type_article
                                  WHERE rss_article = 1
                                  ORDER BY content_type_article_id desc
                                  LIMIT 10";
                  articleQueryResults = this->core->Db->getQueryResults(sql);
                  
                  for (i = 0 ; i < count(articleQueryResults) ; i++) {
                          sql = "SELECT content_title
                                          FROM content
                                          WHERE content_id = ".articleQueryResults[i]['content_id']." ";
                          contentQueryResult = this->core->Db->getQueryResult(sql);
                          
                          arrFeedItems[] = array("content_id"        => articleQueryResults[i]['content_id'],
                                                                          "title"                        => this->core->Common->cutText(contentQueryResult['content_title'], 55) );
                  }
                  
                  return arrFeedItems;        
          }
          
          function putRssInDb () {
                  
                  arrFeeds = this->getFeeds();
                  arrFeedItems = array();
                  numberFeedItemsAdded = 0;
                  numberFeedItemsAlreadyInDb = 0;
                  
                  for (i = 0 ; i < count(arrFeeds) ; i++) {
                  
                          this->Rss20 = new rss20();
                          this->Rss20->newfile(arrFeeds[i]['url']); // lees een externe feed (zonder caching)
                          this->Rss20->parse(); // parse de XML-file
                          items = this->Rss20->get_items(0);
                          
                          foreach (items as item) {
                                  arrFeedItems[] = this->getModifiedRssFeed(arrFeeds[i]['url'], arrFeeds[i]['title'], item);
                          }
                          
                          shuffle(arrFeedItems);
                          this->Rss20->free_parser();
                          unset(items);
                          
                  }
                  
                  for (i = 0 ; i < count(arrFeedItems) ; i++) {
                  
                          sql = "SELECT *
                                          FROM content
                                          WHERE content_sourceurl = '".arrFeedItems[i]['link']."' ";
                          if (this->core->Db->getQueryNumRows(sql) == 1) {
                                  numberFeedItemsAlreadyInDb++;
                                  continue;
                          }
                          
                          sql = "INSERT INTO content (content_title , content_source_name , content_date , content_date_added , content_binary , content_state , content_type , content_sourceurl)
                                          VALUES ('".addslashes(arrFeedItems[i]['title'])."' , '".arrFeedItems[i]['sourcename']."' , '".arrFeedItems[i]['pub_date']."' , NOW() , '".addslashes(arrFeedItems[i]['description'])."' , '".arrFeedItems[i]['content_state']."' , 'article' , '".arrFeedItems[i]['link']."')";
                          this->core->Db->doQuery(sql);
                          contentId = this->core->Db->getLastInsertedId();
                          
                          sql = "INSERT INTO content_type_article (content_id , rss_article)
                                          VALUES (".contentId." , 1)";
                          this->core->Db->doQuery(sql);
                          
                          sql = "INSERT INTO content_matrix (content_id , health , politics , wildlife , energy , business , climate , pollution , technology , lifestyle , environment , region)
                                          VALUES (".contentId." , ".arrFeedItems[i]['arr_content_matrix']['health']." , ".arrFeedItems[i]['arr_content_matrix']['politics']." , ".arrFeedItems[i]['arr_content_matrix']['wildlife']." , 
                                          ".arrFeedItems[i]['arr_content_matrix']['energy']." , ".arrFeedItems[i]['arr_content_matrix']['business']." , ".arrFeedItems[i]['arr_content_matrix']['climate']." , 
                                          ".arrFeedItems[i]['arr_content_matrix']['pollution']." , ".arrFeedItems[i]['arr_content_matrix']['technology']." , ".arrFeedItems[i]['arr_content_matrix']['lifestyle']." , 
                                          ".arrFeedItems[i]['arr_content_matrix']['environment']." , '".arrFeedItems[i]['region']."' )";
                          this->core->Db->doQuery(sql);        
                          
                          numberFeedItemsAdded++;                        
                          
                  }
                  
                  echo 'Number feed items added: '.numberFeedItemsAdded;
                  echo '<br>Number feed items already in db: '.numberFeedItemsAlreadyInDb;
                  
          }
          
          function getModifiedRssFeed (feedUrl, feedTitle, item) {
                                                  
                  result = array();
                  
                  result['sourcename'] = feedTitle.'(feed)';
                  
                  switch (feedUrl) {
                          case 'www.climateark.org/rss/climate.xml':
                                  result['title'] = item['title'];
                                  result['description'] = trim(strip_tags(item['description']));
                                  result['link'] = item['link'];
                                  result['author'] = item['author'];
                                  result['pub_date'] = item['pubDate'];
                                  
                                  arrContentMatrix = this->getContentMatrix(item['title'].item['description']);
                                  result['arr_content_matrix'] = arrContentMatrix['arr_content_matrix'];
                                  result['content_state'] = arrContentMatrix['content_state'];
                                  
                                  preg_match("/.*\|(.*)\|(.*)/", item['category'], matches);
                                  result['region'] = matches[1].' - '.matches[2];
                                  
                                  break;
                          case 'www.globalwarming.org/news/all/feed':
                                  result['title'] = item['title'];
                                  result['link'] = item['link'];
                                  result['description'] = trim(strip_tags(item['description']));
                                  result['author'] = '';
                                  result['region'] = ''; // wordt niet gegeven
                                  result['pub_date'] = item['pubDate'];
                                  
                                  arrContentMatrix = this->getContentMatrix(item['title'].item['description']);
                                  result['arr_content_matrix'] = arrContentMatrix['arr_content_matrix'];
                                  result['content_state'] = arrContentMatrix['content_state'];                
                                                  
                                  break;
                          case 'newsrss.bbc.co.uk/rss/newsonline_world_edition/science/nature/rss.xml':
                                  result['title'] = item['title'];
                                  result['description'] = trim(strip_tags(item['description']));
                                  result['link'] = item['link'];
                                  result['author'] = '';
                                  result['region'] = '';        
                                  result['pub_date'] = item['pubDate'];
                                  
                                  arrContentMatrix = this->getContentMatrix(item['title'].item['description']);
                                  result['arr_content_matrix'] = arrContentMatrix['arr_content_matrix'];
                                  result['content_state'] = arrContentMatrix['content_state'];        
                                  
                                  break;
                          case 'dsc.discovery.com/news/subjects/earth/xml/top-stories.xml':
                                  result['title'] = item['title'];
                                  result['link'] = item['link'];
                                  result['description'] = trim(strip_tags(item['description']));
                                  result['author'] = '';
                                  result['region'] = '';
                                  result['pub_date'] = item['pubDate'];
                                  
                                  arrContentMatrix = this->getContentMatrix(item['title'].item['description']);
                                  result['arr_content_matrix'] = arrContentMatrix['arr_content_matrix'];
                                  result['content_state'] = arrContentMatrix['content_state'];        
                                          
                                  break;
                          case 'news.nationalgeographic.com/rss/global_warming.rss':
                                  result['title'] = item['title'];
                                  result['link'] = item['link'];
                                  result['description'] = trim(strip_tags(item['description']));
                                  result['author'] = '';
                                  result['region'] = '';        
                                  result['pub_date'] = item['pubDate'];
                                  
                                  arrContentMatrix = this->getContentMatrix(item['title'].item['description']);
                                  result['arr_content_matrix'] = arrContentMatrix['arr_content_matrix'];
                                  result['content_state'] = arrContentMatrix['content_state'];
                                          
                                  break;                
                          default:
                                  // wanneer de rss feed hier nog niet is opgenomen
                                  result['title'] = item['title'];
                                  result['link'] = item['link'];
                                  result['description'] = trim(strip_tags(item['description']));
                                  result['author'] = '';
                                  result['region'] = '';        
                                  result['pub_date'] = item['pubDate'];
                                  
                                  arrContentMatrix = this->getContentMatrix(item['title'].item['description']);
                                  result['arr_content_matrix'] = arrContentMatrix['arr_content_matrix'];
                                  result['content_state'] = arrContentMatrix['content_state'];
                                                                  
                  }
                  
                  return result;                
                  
          }
          
          function getContentMatrix (contentString) {
                  
                  arrContentMatrix = array();
                  contentState = 3;
                  
                  arrContentMatrix['health'] = 0;
                  arrContentMatrix['politics'] = 0;
                  arrContentMatrix['wildlife'] = 0;
                  arrContentMatrix['energy'] = 0;
                  arrContentMatrix['business'] = 0;
                  arrContentMatrix['climate'] = 0;
                  arrContentMatrix['pollution'] = 0;
                  arrContentMatrix['technology'] = 0;
                  arrContentMatrix['lifestyle'] = 0;
                  arrContentMatrix['environment'] = 0;
                  
                  // De title en descripstion van de feed item kunnen woorden bevatten waaruit de cotegorie afgeleid kan worden
                  healthRegex = "/health/";
                  politicsRegex = "/political|politics/";
                  wildlifeRegex = "/wildlife|animals|animal|beer/";
                  energyRegex = "/energy|solarpower|greenenergy|green\senergy/";
                  businessRegex = "/business|money|dollar/";
                  climateRegex = "/climate|weather/";
                  pollutionRegex = "/pollution|waste/";
                  technologyRegex = "/technology|technological/";
                  lifestyleRegex = "/lifestyle/";
                  environmentRegex = "/environment/";
                  
                  if (preg_match(politicsRegex, contentString)) {
                          arrContentMatrix['health'] = 1;
                          contentState = 1;
                  }
  
                  if (preg_match(healthRegex, contentString)) {
                          arrContentMatrix['politics'] = 1;
                          contentState = 1;
                  }
                  
                  if (preg_match(wildlifeRegex, contentString)) {
                          arrContentMatrix['wildlife'] = 1;
                          contentState = 1;
                  }
                  
                  if (preg_match(energyRegex, contentString)) {
                          arrContentMatrix['energy'] = 1;
                          contentState = 1;
                  }
                  
                  if (preg_match(businessRegex, contentString)) {
                          arrContentMatrix['business'] = 1;
                          contentState = 1;
                  }
          
                  if (preg_match(climateRegex, contentString)) {
                          arrContentMatrix['climate'] = 1;
                          contentState = 1;
                  }
                  
                  if (preg_match(pollutionRegex, contentString)) {
                          arrContentMatrix['pollution'] = 1;
                          contentState = 1;
                  }
                  
                  if (preg_match(technologyRegex, contentString)) {
                          arrContentMatrix['technology'] = 1;
                          contentState = 1;
                  }
                  
                  if (preg_match(lifestyleRegex, contentString)) {
                          arrContentMatrix['lifestyle'] = 1;
                          contentState = 1;
                  }
                  
                  if (preg_match(environmentRegex, contentString)) {
                          arrContentMatrix['environment'] = 1;
                          contentState = 1;
                  }
                  
                  result['arr_content_matrix'] = arrContentMatrix;
                  result['content_state'] = contentState;
                  
                  return result;                
                  
          }
  
  }
  ?>


(C) A. Eliëns 2/9/2007

You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.