Esempio n. 1
0
	/**
	 * Tests Remote::status
	 *
	 * @test
	 * @dataProvider provider_status
	 * @param boolean $input  Input for File::mime
	 * @param boolean $expected Output for File::mime
	 */
	public function test_status($input, $expected)
	{
		if ( ! $this->hasInternet())
			$this->markTestSkipped('An internet connection is required for this test');
		
		$this->assertSame($expected, Remote::status($input));
	}
Esempio n. 2
0
 private function rss_feed()
 {
     // RSS Feed parameters:
     $api_id = 2;
     // api_id = 2 for RSS Feeds
     // Perform gathering for each active RSS feed URL for given project_id
     $rss_feeds = $this->model_gather->get_active_rss_feeds($this->project_id);
     foreach ($rss_feeds as $rss_feed) {
         $total_results_gathered = 0;
         // If RSS feed was set searchable generate search query GET string
         $keyword_str = "";
         if ($rss_feed['searchable']) {
             $num_keywords = count($this->keywords_phrases);
             $i = 0;
             foreach ($this->keywords_phrases as $keyword_phrase) {
                 $i++;
                 $word_split = explode(" ", $keyword_phrase['keyword_phrase']);
                 if (count($word_split) > 1) {
                     // Is phrase (more than 1 word)
                     // Check if searching "exact phrase" -> if so add quotes
                     if ($keyword_phrase['exact_phrase']) {
                         $keyword_str .= '"' . urlencode($keyword_phrase['keyword_phrase']) . '"';
                     } else {
                         $keyword_str .= '(' . urlencode($keyword_phrase['keyword_phrase']) . ')';
                     }
                 } else {
                     // Is single keyword
                     $keyword_str .= urlencode($keyword_phrase['keyword_phrase']);
                 }
                 if ($i < $num_keywords) {
                     $keyword_str .= '+OR+';
                 }
             }
         }
         $connection_retries = 0;
         $this->api_connect_error = 0;
         while (TRUE) {
             // Compile request URL
             $request_url = $rss_feed['url'] . $keyword_str;
             print "Query: {$request_url}\n";
             // Check for connection errors
             try {
                 $status_code = Remote::status($request_url);
             } catch (Exception $e) {
                 $this->api_connect_error = "Error connecting to {$request_url}. Cannot locate host.";
             }
             if (!$this->api_connect_error and ($status_code < 200 or $status_code > 299)) {
                 $this->api_connect_error = "Error connecting to {$request_url}. Status code: {$status_code}";
             }
             if (!$this->api_connect_error) {
                 $rss_output = Feed::parse($request_url);
                 $num_results = count($rss_output);
                 if ($num_results > 0) {
                     // Loop through each result, parse, and store data
                     foreach ($rss_output as $item) {
                         $title = array_key_exists('title', $item) ? $item['title'] : '';
                         $text = array_key_exists('description', $item) ? $item['description'] : '';
                         $date_published_timestamp = array_key_exists('pubDate', $item) ? strtotime($item['pubDate']) : 0;
                         // Append title to text & strip all HTML tags except <br>'s
                         $text = "Title: {$title}<br>{$text}";
                         $text = strip_tags($text, "<br>");
                         // Determine unique identifier, if no URL -> use guid -> if no GUID give error
                         if (array_key_exists('link', $item) and $item['link'] != "") {
                             $url = $item['link'];
                         } else {
                             if (array_key_exists('guid', $item)) {
                                 $url = $item['guid'];
                             } else {
                                 print "Error: item has no URL or GUID. Cannot use.\n";
                                 continue;
                             }
                         }
                         // Add each result to database
                         $require_keywords = $rss_feed['searchable'] ? 0 : 1;
                         // Only require keywords if RSS feed is NOT searchable
                         $total_results_gathered += $this->add_metadata($url, $text, $require_keywords, array('project_id' => $this->project_id, 'api_id' => $api_id, 'date_published' => $date_published_timestamp, 'date_retrieved' => time()));
                     }
                 }
                 break;
             } else {
                 // Retry connecting to API
                 $connection_retries++;
                 // Connection error (only for non-searchable RSS feeds, it is assumed an error has occured if no item comes through the feed after multiple connection attempts)
                 if ($connection_retries > $this->connection_retries) {
                     if (!$rss_feed['searchable']) {
                         $this->model_gather->insert_gather_log(array('project_id' => $this->project_id, 'search_query' => $this->api_connect_error, 'date' => time(), 'results_gathered' => 0, 'error' => 1));
                         $this->api_connect_error = 1;
                     }
                     break;
                 }
             }
         }
         // Add entry to gather log (as long as no errors occurred)
         if (!$this->api_connect_error) {
             $this->model_gather->insert_gather_log(array('project_id' => $this->project_id, 'search_query' => $request_url, 'date' => time(), 'results_gathered' => $total_results_gathered));
         }
     }
 }
Esempio n. 3
0
 /**
  * Tests Remote::status
  *
  * @test
  * @dataProvider providerStatus
  * @param boolean $input  Input for File::mime
  * @param boolean $expected Output for File::mime
  */
 function testStatus($input, $expected)
 {
     $this->assertSame($expected, Remote::status($input));
 }