Beispiel #1
0
 /**
  * parse feed and send feed items to database
  */
 public function index()
 {
     // Max number of feeds to keep
     $max_feeds = 100;
     // Today's Date
     $today = strtotime('now');
     // Get All Feeds From DB
     $feeds = ORM::factory('feed')->find_all();
     foreach ($feeds as $feed) {
         $last_update = $feed->feed_update;
         // Has it been more than 24 hours since the last update?
         if ((int) $today - (int) $last_update > 86400) {
             // Parse Feed URL using Feed Helper
             $feed_data = $this->_setup_simplepie($feed->feed_url);
             foreach ($feed_data->get_items(0, 50) as $feed_data_item) {
                 $title = $feed_data_item->get_title();
                 $link = $feed_data_item->get_link();
                 $description = $feed_data_item->get_description();
                 $date = $feed_data_item->get_date();
                 // Make Sure Title is Set (Atleast)
                 if (isset($title) && !empty($title)) {
                     // We need to check for duplicates!!!
                     // Maybe combination of Title + Date? (Kinda Heavy on the Server :-( )
                     $dupe_count = ORM::factory('feed_item')->where('item_title', $title)->where('item_date', date("Y-m-d H:i:s", strtotime($date)))->count_all();
                     if ($dupe_count == 0) {
                         $newitem = new Feed_Item_Model();
                         $newitem->feed_id = $feed->id;
                         $newitem->item_title = $title;
                         if (isset($description) && !empty($description)) {
                             $newitem->item_description = $description;
                         }
                         if (isset($link) && !empty($link)) {
                             $newitem->item_link = $link;
                         }
                         if (isset($date) && !empty($date)) {
                             $newitem->item_date = date("Y-m-d H:i:s", strtotime($date));
                         } else {
                             $newitem->item_date = date("Y-m-d H:i:s", time());
                         }
                         $newitem->save();
                     }
                 }
             }
             // Get Feed Item Count
             $feed_count = ORM::factory('feed_item')->where('feed_id', $feed->id)->count_all();
             if ($feed_count > $max_feeds) {
                 // Excess Feeds
                 $feed_excess = $feed_count - $max_feeds;
                 // Delete Excess Feeds
                 foreach (ORM::factory('feed_item')->where('feed_id', $feed->id)->orderby('id', 'ASC')->limit($feed_excess)->find_all() as $del_feed) {
                     $del_feed->delete($del_feed->id);
                 }
             }
             // Set feed update date
             $feed->feed_update = strtotime('now');
             $feed->save();
         }
     }
 }
Beispiel #2
0
 /**
  * parse feed and send feed items to database
  */
 private function _parse_feed()
 {
     // Max number of feeds to keep
     $max_feeds = 100;
     // Today's Date
     $today = strtotime('now');
     // Get All Feeds From DB
     $feeds = ORM::factory('feed')->find_all();
     foreach ($feeds as $feed) {
         $last_update = $feed->feed_update;
         // Has it been more than 24 hours since the last update?
         // Since its a manual refresh, we don't need to set a time
         if ((int) $today - (int) $last_update > 0) {
             // Parse Feed URL using Feed Helper
             $feed_data = feed::simplepie($feed->feed_url);
             foreach ($feed_data->get_items(0, 50) as $feed_data_item) {
                 $title = $feed_data_item->get_title();
                 $link = $feed_data_item->get_link();
                 $description = $feed_data_item->get_description();
                 $date = $feed_data_item->get_date();
                 $latitude = $feed_data_item->get_latitude();
                 $longitude = $feed_data_item->get_longitude();
                 // Make Sure Title is Set (Atleast)
                 if (isset($title) && !empty($title)) {
                     // We need to check for duplicates!!!
                     // Maybe combination of Title + Date? (Kinda Heavy on the Server :-( )
                     $dupe_count = ORM::factory('feed_item')->where('item_title', $title)->where('item_date', date("Y-m-d H:i:s", strtotime($date)))->count_all();
                     if ($dupe_count == 0) {
                         // Does this feed have a location??
                         $location_id = 0;
                         // STEP 1: SAVE LOCATION
                         if ($latitude && $longitude) {
                             $location = new Location_Model();
                             $location->location_name = Kohana::lang('ui_admin.unknown');
                             $location->latitude = $latitude;
                             $location->longitude = $longitude;
                             $location->location_date = date("Y-m-d H:i:s", time());
                             $location->save();
                             $location_id = $location->id;
                         }
                         $newitem = new Feed_Item_Model();
                         $newitem->feed_id = $feed->id;
                         $newitem->location_id = $location_id;
                         $newitem->item_title = $title;
                         if (isset($description) && !empty($description)) {
                             $newitem->item_description = $description;
                         }
                         if (isset($link) && !empty($link)) {
                             $newitem->item_link = $link;
                         }
                         if (isset($date) && !empty($date)) {
                             $newitem->item_date = date("Y-m-d H:i:s", strtotime($date));
                         } else {
                             $newitem->item_date = date("Y-m-d H:i:s", time());
                         }
                         if (isset($feed_type) && !empty($feed_type)) {
                             $newitem->feed_type = $feed_type;
                         }
                         $newitem->save();
                     }
                 }
             }
             // Get Feed Item Count
             $feed_count = ORM::factory('feed_item')->where('feed_id', $feed->id)->count_all();
             if ($feed_count > $max_feeds) {
                 // Excess Feeds
                 $feed_excess = $feed_count - $max_feeds;
                 // Delete Excess Feeds
                 foreach (ORM::factory('feed_item')->where('feed_id', $feed->id)->orderby('id', 'ASC')->limit($feed_excess)->find_all() as $del_feed) {
                     $del_feed->delete($del_feed->id);
                 }
             }
             // Set feed update date
             $feed->feed_update = strtotime('now');
             $feed->save();
         }
     }
 }
Beispiel #3
0
 /**
  * parse feed and send feed items to database
  */
 public function index()
 {
     // Max number of feeds to keep
     $max_feeds = 100;
     // Today's Date
     $today = strtotime('now');
     // Get All Feeds From DB
     $feeds = ORM::factory('feed')->find_all();
     foreach ($feeds as $feed) {
         $last_update = $feed->feed_update;
         // Parse Feed URL using Feed Helper
         $feed_data = feed::simplepie($feed->feed_url);
         foreach ($feed_data->get_items(0, 50) as $feed_data_item) {
             $title = $feed_data_item->get_title();
             $link = $feed_data_item->get_link();
             $description = $feed_data_item->get_description();
             $date = $feed_data_item->get_date();
             $latitude = $feed_data_item->get_latitude();
             $longitude = $feed_data_item->get_longitude();
             $categories = $feed_data_item->get_categories();
             // HT: new code
             $category_ids = new stdClass();
             // HT: new code
             // Make Sure Title is Set (Atleast)
             if (isset($title) && !empty($title)) {
                 // We need to check for duplicates!!!
                 // Maybe combination of Title + Date? (Kinda Heavy on the Server :-( )
                 $dupe_count = ORM::factory('feed_item')->where('item_title', $title)->where('item_date', date("Y-m-d H:i:s", strtotime($date)))->count_all();
                 if ($dupe_count == 0) {
                     // Does this feed have a location??
                     $location_id = 0;
                     // STEP 1: SAVE LOCATION
                     if ($latitude and $longitude) {
                         $location = new Location_Model();
                         $location->location_name = "Unknown";
                         $location->latitude = $latitude;
                         $location->longitude = $longitude;
                         $location->location_date = date("Y-m-d H:i:s", time());
                         $location->save();
                         $location_id = $location->id;
                     }
                     $newitem = new Feed_Item_Model();
                     $newitem->feed_id = $feed->id;
                     $newitem->location_id = $location_id;
                     $newitem->item_title = $title;
                     if (isset($description) and !empty($description)) {
                         $newitem->item_description = $description;
                     }
                     if (isset($link) and !empty($link)) {
                         $newitem->item_link = $link;
                     }
                     if (isset($date) and !empty($date)) {
                         $newitem->item_date = date("Y-m-d H:i:s", strtotime($date));
                     } else {
                         $newitem->item_date = date("Y-m-d H:i:s", time());
                     }
                     // HT: new code
                     if (!empty($categories)) {
                         foreach ($categories as $category) {
                             $categoryData = ORM::factory('category')->where('category_title', $category->term)->find();
                             if ($categoryData->loaded == TRUE) {
                                 $category_ids->feed_item_category[$categoryData->id] = $categoryData->id;
                             } elseif (Kohana::config('settings.allow_feed_category')) {
                                 $newcategory = new Category_Model();
                                 $newcategory->category_title = $category->term;
                                 $newcategory->parent_id = 0;
                                 $newcategory->category_description = $category->term;
                                 $newcategory->category_color = '000000';
                                 $newcategory->category_visible = 0;
                                 $newcategory->save();
                                 $category_ids->feed_item_category[$newcategory->id] = $newcategory->id;
                             }
                         }
                     }
                     // HT: End of new code
                     $newitem->save();
                     // HT: New code
                     if (!empty($category_ids->feed_item_category)) {
                         feed::save_category($category_ids, $newitem);
                     }
                     // HT: End of New code
                     // Action::feed_item_add - Feed Item Received!
                     Event::run('ushahidi_action.feed_item_add', $newitem);
                 }
             }
         }
         // Get Feed Item Count
         $feed_count = ORM::factory('feed_item')->where('feed_id', $feed->id)->count_all();
         if ($feed_count > $max_feeds) {
             // Excess Feeds
             $feed_excess = $feed_count - $max_feeds;
         }
         // Set feed update date
         $feed->feed_update = strtotime('now');
         $feed->save();
     }
 }
Beispiel #4
0
 /**
  *
  *   //get all the admin feeds in database.
  */
 private function get_new_feeds($category_id)
 {
     //get all the admin feeds in database.
     $dbfeeds = ORM::factory('feed')->select('id', 'feed_url', 'category_id')->where('category_id', $category_id)->find_all();
     if ($category_id == 0) {
         $dbfeeds = ORM::factory('feed')->select('id', 'feed_url', 'category_id')->find_all();
     }
     foreach ($dbfeeds as $dbfeed) {
         //Don't do anything about twitter categories.
         if ($dbfeed->category_id != 11) {
             $url = "";
             $feed = new SimplePie();
             $feed->enable_order_by_date(true);
             if ($dbfeed->category_id == 1) {
                 $url = "http://twitter.com/statuses/user_timeline/" . $dbfeed->feed_url . ".rss";
                 $feed->set_feed_url($url);
                 //	exit(0);
             } else {
                 $url = $dbfeed->feed_url;
                 $feed->set_feed_url($dbfeed->feed_url);
             }
             $feed->set_cache_location(APPPATH . 'cache');
             $feed->set_timeout(10);
             $feed->init();
             //		$channel = $feed->get_feed_tags('', 'channel');
             //		echo " tags=> ".$channel."<br/>";
             // echo "$url :<br/>";
             //	exit(0)
             $max_items = $feed->get_item_quantity();
             $require_new_items = 20;
             $new_item_counter = 0;
             $start = 0;
             for ($i = $start; $i < $max_items && $new_item_counter < $require_new_items; $i++) {
                 $item = $feed->get_item($i);
                 /*				//getting all the feed information.								 
                 						echo "$url:  latitude => ".$item->get_latitude();
                 						echo "   longitude => ".$item->get_longitude();
                 						echo '<a href="' . $feed->get_image_link() . '" title="' . $feed->get_image_title() . '">';
                 						echo '<img src="' . $feed->get_image_url() . '" width="' . $feed->get_image_width() . '" height="' . $feed->get_image_height() . '" />';
                 						echo '</a><br/>Title:'.$item->get_title();
                 						echo '<br/>Description:'.$item->get_description();
                 						echo '<hr/>';
                 								
                 						*/
                 $itemobj = new Feed_Item_Model();
                 $itemobj->feed_id = $dbfeed->id;
                 $itemobj->item_title = $item->get_title();
                 $itemobj->item_description = $item->get_description();
                 $itemobj->item_link = $item->get_permalink();
                 $itemobj->item_date = $item->get_date('Y-m-d h:m:s');
                 if ($author = $item->get_author()) {
                     $itemobj->item_source = $item->get_author()->get_name();
                     //temporary not working.
                 }
                 //echo "in Main Controller $dbfeed->feed_url =>  latitude =".$feed->get_latitude().", longitude =".$feed->get_longitude()."<br/>";
                 //echo "in Main Controller $dbfeed->feed_url =>   get_author() => ".$feed->get_author()."<br/>";
                 $linkCount = ORM::factory('feed_item')->where('item_link', $item->get_permalink())->count_all();
                 if ($linkCount == 0) {
                     $new_item_counter++;
                     //  echo "link:=> ".$item->get_permalink()." is new and has appear ".$linkCount." times <br/>";
                     $itemobj->save();
                 } else {
                     if ($linkCount > 0) {
                         //	echo "link:=> ".$item->get_permalink()." appears ".$linkCount." times <br/>";
                     }
                 }
             }
         }
     }
     //		exit(0);
 }
Beispiel #5
0
 /**
  * parse feed and send feed items to database
  */
 public function index()
 {
     // Max number of feeds to keep
     $max_feeds = 100;
     // Today's Date
     $today = strtotime('now');
     // Get All Feeds From DB
     $feeds = ORM::factory('feed')->find_all();
     foreach ($feeds as $feed) {
         $last_update = $feed->feed_update;
         // Parse Feed URL using Feed Helper
         $feed_data = feed::simplepie($feed->feed_url);
         foreach ($feed_data->get_items(0, 50) as $feed_data_item) {
             $title = $feed_data_item->get_title();
             $link = $feed_data_item->get_link();
             $description = $feed_data_item->get_description();
             $date = $feed_data_item->get_date();
             $latitude = $feed_data_item->get_latitude();
             $longitude = $feed_data_item->get_longitude();
             // Make Sure Title is Set (Atleast)
             if (isset($title) && !empty($title)) {
                 // We need to check for duplicates!!!
                 // Maybe combination of Title + Date? (Kinda Heavy on the Server :-( )
                 $dupe_count = ORM::factory('feed_item')->where('item_title', $title)->where('item_date', date("Y-m-d H:i:s", strtotime($date)))->count_all();
                 if ($dupe_count == 0) {
                     // Does this feed have a location??
                     $location_id = 0;
                     // STEP 1: SAVE LOCATION
                     if ($latitude && $longitude) {
                         $location = new Location_Model();
                         $location->location_name = "Unknown";
                         $location->latitude = $latitude;
                         $location->longitude = $longitude;
                         $location->location_date = date("Y-m-d H:i:s", time());
                         $location->save();
                         $location_id = $location->id;
                     }
                     $newitem = new Feed_Item_Model();
                     $newitem->feed_id = $feed->id;
                     $newitem->location_id = $location_id;
                     $newitem->item_title = $title;
                     if (isset($description) && !empty($description)) {
                         $newitem->item_description = $description;
                     }
                     if (isset($link) && !empty($link)) {
                         $newitem->item_link = $link;
                     }
                     if (isset($date) && !empty($date)) {
                         $newitem->item_date = date("Y-m-d H:i:s", strtotime($date));
                     } else {
                         $newitem->item_date = date("Y-m-d H:i:s", time());
                     }
                     $newitem->save();
                 }
             }
         }
         // Get Feed Item Count
         $feed_count = ORM::factory('feed_item')->where('feed_id', $feed->id)->count_all();
         if ($feed_count > $max_feeds) {
             // Excess Feeds
             $feed_excess = $feed_count - $max_feeds;
             // Delete Excess Feeds
             /**** DISABLED FOR NOW ****/
             //				foreach (ORM::factory('feed_item')
             //					->where('feed_id', $feed->id)
             //					->orderby('id', 'ASC')
             //					->limit($feed_excess)
             //					->find_all() as $del_feed)
             //				{
             //					$del_feed->delete($del_feed->id);
             //				}
         }
         // Set feed update date
         $feed->feed_update = strtotime('now');
         $feed->save();
     }
 }
Beispiel #6
0
 /**
  * Create a report and assign it to one or more categories and set verification
  */
 public function __response_create_report($vars)
 {
     $categories = array();
     if (isset($vars['add_category'])) {
         $categories = $vars['add_category'];
     }
     $verify = 0;
     if (isset($vars['verify'])) {
         $verify = (int) $vars['verify'];
     }
     $approve = 0;
     if (isset($vars['approve'])) {
         $approve = (int) $vars['approve'];
     }
     // Grab the location_id or create one if we can
     $location_id = 0;
     if (isset($this->data->location_id)) {
         $location_id = $this->data->location_id;
     } elseif (isset($this->data->latitude) and isset($this->data->longitude)) {
         $location_name = map::reverse_geocode($this->data->latitude, $this->data->longitude);
         // In case our location name is too long, chop off the end
         $location_name = substr_replace($location_name, '', 250);
         $location_data = (object) array('location_name' => $location_name, 'latitude' => $this->data->latitude, 'longitude' => $this->data->longitude);
         $location = new Location_Model();
         reports::save_location($location_data, $location);
         $location_id = $location->id;
     }
     // We can only create reports if we have location.
     if ($location_id == FALSE or $location_id == 0) {
         return false;
     }
     // Build title
     // Build title & description
     // If this is a message
     if (isset($this->data->message)) {
         $incident_title = $this->data->message;
         $incident_description = $this->data->message;
         $incident_date = $this->data->message_date;
         // If we're got more message detail, make that the description
         if (!empty($message->message_detail)) {
             $incident_description = $this->data->message_detail;
         }
     } elseif (isset($this->data->item_title)) {
         $incident_title = html::strip_tags(html_entity_decode(html_entity_decode($this->data->item_title, ENT_QUOTES)));
         $incident_description = html::clean(html_entity_decode($this->data->item_description, ENT_QUOTES));
         $incident_date = $this->data->item_date;
     }
     // Override title from action options
     if (!empty($vars['report_title'])) {
         $incident_title = $vars['report_title'];
     }
     // Save Incident
     $incident = new Incident_Model();
     $incident->location_id = $location_id;
     $incident->incident_title = $incident_title;
     $incident->incident_description = $incident_description;
     $incident->incident_date = $incident_date;
     $incident->incident_active = $approve;
     $incident->incident_verified = $verify;
     $incident->incident_dateadd = date("Y-m-d H:i:s", time());
     $incident->save();
     // Conflicted.. do I run report add here? Potential to create a mess with action triggers?
     //Event::run('ushahidi_action.report_add', $incident);
     // Save media
     if (isset($this->data->item_title)) {
         $news = new Media_Model();
         $news->location_id = $incident->location_id;
         $news->incident_id = $incident->id;
         $news->media_type = 4;
         // News
         $news->media_link = $this->data->item_link;
         $news->media_date = $this->data->item_date;
         $news->save();
     }
     $incident_id = $incident->id;
     foreach ($categories as $category_id) {
         // Assign Category
         Incident_Category_Model::assign_category_to_incident($incident_id, $category_id);
     }
     // Link message with incident?
     if (isset($this->data->message) and isset($this->data->id)) {
         $message = new Message_Model($this->data->id);
         $message->incident_id = $incident_id;
         $message->save();
     } elseif (isset($this->data->item_title) and isset($this->data->id)) {
         $item = new Feed_Item_Model($this->data->id);
         $item->incident_id = $incident_id;
         $item->save();
     }
     return TRUE;
 }