Skip to content

Commit

Permalink
fetch only first page for global search commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gharlan committed Dec 13, 2016
1 parent cd4ec9f commit d0cf1a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion action.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
case 'refresh-cache':
$curl = new Curl();
foreach (explode(',', $parts[2]) as $url) {
Workflow::requestCache($url, $curl, null, 0, false);
Workflow::requestCache($url, $curl, null, false, 0, false);
}
$curl->execute();
Workflow::cleanCache();
Expand Down
4 changes: 2 additions & 2 deletions search.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,15 @@ private static function addDefaultCommands($isSearch, $isUser, $isRepo, $queryUs
private static function addRepoSearchCommands()
{
$q = substr(self::$query, 2);
$repos = Workflow::requestApi('/search/repositories?q='.urlencode($q));
$repos = Workflow::requestApi('/search/repositories?q='.urlencode($q), null, null, true);

self::addRepos($repos, 's ');
}

private static function addUserSearchCommands()
{
$q = substr(self::$query, 3);
$users = Workflow::requestApi('/search/users?q='.urlencode($q));
$users = Workflow::requestApi('/search/users?q='.urlencode($q), null, null, true);

self::addUsers($users, 's @');
}
Expand Down
29 changes: 19 additions & 10 deletions workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,13 @@ public static function request($url, Curl $curl = null, $callback = null, $withA
* @param string $url
* @param Curl $curl
* @param callable $callback
* @param bool $firstPageOnly
* @param int $maxAge
* @param bool $refreshInBackground
*
* @return mixed
*/
public static function requestCache($url, Curl $curl = null, $callback = null, $maxAge = self::DEFAULT_CACHE_MAX_AGE, $refreshInBackground = true)
public static function requestCache($url, Curl $curl = null, $callback = null, $firstPageOnly = false, $maxAge = self::DEFAULT_CACHE_MAX_AGE, $refreshInBackground = true)
{
$return = false;
$returnValue = null;
Expand Down Expand Up @@ -209,20 +210,25 @@ public static function requestCache($url, Curl $curl = null, $callback = null, $
if (!$shouldRefresh || $refreshInBackground) {
self::log('using cached content for %s', $url);
$content = json_decode($content);
$stmt = self::getStatement('SELECT url, content FROM request_cache WHERE parent = ? ORDER BY `timestamp` DESC');
while ($stmt->execute(array($url)) && $data = $stmt->fetchObject()) {
$content = array_merge($content, json_decode($data->content));
$url = $data->url;

if (!$firstPageOnly) {
$stmt = self::getStatement('SELECT url, content FROM request_cache WHERE parent = ? ORDER BY `timestamp` DESC');
while ($stmt->execute(array($url)) && $data = $stmt->fetchObject()) {
$content = array_merge($content, json_decode($data->content));
$url = $data->url;
}
}

if (is_callable($callback)) {
$callback($content);
}

return $returnValue;
}

$responses = array();

$handleResponse = function (CurlResponse $response, $content, $parent = null) use (&$handleResponse, $curl, &$responses, $stmt, $callback) {
$handleResponse = function (CurlResponse $response, $content, $parent = null) use (&$handleResponse, $curl, &$responses, $stmt, $callback, $firstPageOnly) {
$url = $response->request->url;
if ($response && in_array($response->status, array(200, 304))) {
$checkNext = false;
Expand All @@ -239,7 +245,10 @@ public static function requestCache($url, Curl $curl = null, $callback = null, $
$responses[] = $response->content;
Workflow::getStatement('REPLACE INTO request_cache VALUES(?, ?, ?, ?, 0, ?)')
->execute(array($url, time(), $response->etag, json_encode($response->content), $parent));
if ($checkNext || $response->link && preg_match('/<(.+)>; rel="next"/U', $response->link, $match)) {

if ($firstPageOnly) {
// do nothing
} elseif ($checkNext || $response->link && preg_match('/<(.+)>; rel="next"/U', $response->link, $match)) {
$stmt = Workflow::getStatement('SELECT * FROM request_cache WHERE parent = ?');
$stmt->execute(array($url));
if ($checkNext) {
Expand Down Expand Up @@ -290,10 +299,10 @@ public static function requestCache($url, Curl $curl = null, $callback = null, $
return $returnValue;
}

public static function requestApi($url, Curl $curl = null, $callback = null, $maxAge = self::DEFAULT_CACHE_MAX_AGE)
public static function requestApi($url, Curl $curl = null, $callback = null, $firstPageOnly = false, $maxAge = self::DEFAULT_CACHE_MAX_AGE)
{
$url = self::getApiUrl($url);
return self::requestCache($url, $curl, $callback, $maxAge);
return self::requestCache($url, $curl, $callback, $firstPageOnly, $maxAge);
}

public static function cleanCache()
Expand Down Expand Up @@ -345,7 +354,7 @@ public static function checkUpdate()
if (!self::getConfig('autoupdate', 1)) {
return false;
}
$release = self::requestCache('https://api.github.com/repos/gharlan/alfred-github-workflow/releases/latest', null, null, 1440);
$release = self::requestCache('https://api.github.com/repos/gharlan/alfred-github-workflow/releases/latest', null, null, true, 1440);
if (!$release) {
return false;
}
Expand Down

0 comments on commit d0cf1a9

Please sign in to comment.