/** * Searches for multiple keywords * @param array keywords The keywords to search for * @param string since_id Limit results to entries starting after since_id * @param boolean search_or search using OR or AND */ static function search_multiple(&$oauthConnection, $keyword, $since_id = null, $search_or = true) { $entries = array(); $search_twitter_api = 'search/tweets.json'; // Filter: tweeds containing links only. // This Filter doesn't work at the moment, will produce an empty result! // It is not neccessary for us, only a hint for twitter. // rpp: results per page $filter = ''; // +filter:links if (!empty($since_id)) { $filter .= "&since_id={$since_id}"; } $query = $keyword; //urlencode($keyword); // Filter will be added to the query, so substract it. $max_query_len = 139 - strlen($filter); // Now execute the queries $api = new TwitterOAuthApi($oauthConnection); $newentries = $api->search($query . $filter, $entries); if ($newentries === false) { // Error occured, mostly resultet in an twitter overload! echo "<b>Search qry</b>: " . $search_twitter_api . ".?q={$query}{$filter}<br/>"; if (!empty($api->error_response)) { $response = json_decode($api->error_response); if (!empty($response->error)) { $errormsg = $response->error; } else { $errormsg = $api->error_response; } echo "<b>Error Resp</b>: {$errormsg}<br/>"; } $newentries = array(); } $entries = $newentries; return $entries; }
function check_tweetbacks_global($complete = false) { global $serendipity; $this->log("Check global"); if (!serendipity_db_bool($this->get_config('do_tweetbacks'))) { return false; } $lastcheck = $this->get_config('last_check_global'); $check_freq = $this->get_config('tweetback_check_freq', 30); if (!is_numeric($check_freq) || $check_freq < 5) { $check_freq = 5; } $check_freq = $check_freq * 60; // we need seconds if (!$complete && !empty($lastcheck) && time() - $lastcheck < $check_freq) { // Search already done. return $lastcheck + $check_freq; } // TODO: This file sema is blocking! wtf?! if (!($fp = TwitterPluginFileAccess::get_lock(0))) { $this->log("Sema is blocking."); return time() + $check_freq; // someone else ist processing the global check } $baseUrl = $serendipity['baseURL']; $permalinkRegex = '@(' . $baseUrl . '.*' . serendipity_makePermalinkRegex($serendipity['permalinkStructure'], 'entry') . ')/?@i'; if ($complete) { $old_since_id = '0'; } else { $old_since_id = $this->get_config('highest_id_global'); } $this->log("old_since_id [{$old_since_id}]"); $comment_type = $this->get_config('tweetback_type', 'TRACKBACK'); $ignore_names = explode(',', $this->get_config('ignore_tweetbacks_by_name', $this->get_config('twittername'))); $search_since_id = "0"; if (!empty($old_since_id)) { $search_since_id = $old_since_id; } // Start searching $idx = $this->get_config('twitter_generic_acc', '1'); if ($idx == '1') { $idx = ''; } $connection = $this->twitteroa_connect($idx); $twitterapi = new TwitterOAuthApi($connection); $twittersearch = $this->generate_domain_url(false) . "&since_id=" . $search_since_id; $entries = $twitterapi->search($twittersearch); if (is_array($entries) && !empty($entries)) { // reverse the entries to get oldest first! $entries = array_reverse($entries, true); $redirCheck = new RedirectCheck(); $validated_entries = array(); foreach ($entries as $entry) { $writer = $entry[TWITTER_SEARCHRESULT_LOGIN]; // First check if the tweets autor should be ignored: $ignore = false; foreach ($ignore_names as $ignore_name) { $ignore = strtoupper(trim($ignore_name)) == strtoupper($writer); if ($ignore) { break; } } if ($ignore) { continue; } // Check wether this tweet is blog article related: $tweetmatches = false; // Check all expanded urls: if (!empty($entry[TWITTER_SEARCHRESULT_URL_ARRAY])) { foreach ($entry[TWITTER_SEARCHRESULT_URL_ARRAY] as $url) { $url = $redirCheck->get_final_url($url); $tweetmatches = preg_match($permalinkRegex, $url, $matches); if ($tweetmatches) { break; } // Found it! } } // If no URL matches, try the tweet itself: if (!$tweetmatches) { $tweet = $entry[TWITTER_SEARCHRESULT_TWEET]; !($tweetmatches = preg_match($permalinkRegex, $tweet, $matches)); } // If we found a match, add it to the validated entries if ($tweetmatches) { $this->log("Tweet matches!"); $match = array('id' => $matches[2], 'entry' => $entry); $validated_entries[] = $match; } } } //is array $entries end // Save all comments and evaluate highest ids: $highest_ids = array(); if (is_array($validated_entries) && !empty($validated_entries)) { foreach ($validated_entries as $valid) { $comment_saved = false; $article_id = $valid['id']; $entry = $valid['entry']; $test_highest_id = null; if (!empty($highest_ids[$article_id])) { $test_highest_id = $highest_ids[$article_id]['high_id']; } if (empty($test_highest_id)) { // load old highest id of the article $last_info = TwitterPluginDbAccess::load_tweetback_info($article_id, $this); $test_highest_id = empty($last_info) ? null : $last_info['lasttweetid']; $highest_ids[$article_id] = array('high_id' => $test_highest_id, 'last_info' => $last_info); } if (empty($highest_ids[$article_id]['last_info']) || empty($highest_ids[$article_id]['last_info']['lasttweetid']) || "{$entry[TWITTER_SEARCHRESULT_ID]}" > $highest_ids[$article_id]['last_info']['lasttweetid']) { if ($complete) { // This is called from admin interface echo "<div class='serendipityAdminMsgSuccess'>Found new tweetback for article {$article_id}: tweetid: {$entry[TWITTER_SEARCHRESULT_ID]}</div><br/>"; } $this->check_tweetbacks_save_comment($article_id, $entry, $comment_type, true); $comment_saved = true; } // Remember highest id, if saved and highest id is higher than old one. if ($comment_saved && (empty($test_highest_id) || "{$entry[TWITTER_SEARCHRESULT_ID]}" > "{$test_highest_id}")) { $highest_ids[$article_id]['high_id'] = "{$entry[TWITTER_SEARCHRESULT_ID]}"; } } } // is array $validated_entries end $global_highest_id = $this->get_config('highest_id_global'); if (empty($global_highest_id)) { $global_highest_id = '0'; } if (is_array($highest_ids) && !empty($highest_ids)) { // Save highest ids: foreach ($highest_ids as $article_id => $highest_id_array) { // remember globaly if (!empty($highest_id_array['high_id']) && $highest_id_array['high_id'] > $global_highest_id) { $global_highest_id = $highest_id_array['high_id']; } // save per article if (empty($highest_id_array['last_info']) || $highest_id_array['high_id'] > $highest_id_array['last_info']['lasttweetid']) { TwitterPluginDbAccess::save_highest_id($article_id, $highest_id_array['high_id'], $highest_id_array['last_info']); } } } // is array $highest_ids end $lastcheck = time(); $this->set_config('last_check_global', $lastcheck); $this->set_config('highest_id_global', $global_highest_id); TwitterPluginFileAccess::free_lock(0); return $lastcheck + $check_freq; }