Skip to content

Commit d0cf1a9

Browse files
committed
fetch only first page for global search commands
1 parent cd4ec9f commit d0cf1a9

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
case 'refresh-cache':
7373
$curl = new Curl();
7474
foreach (explode(',', $parts[2]) as $url) {
75-
Workflow::requestCache($url, $curl, null, 0, false);
75+
Workflow::requestCache($url, $curl, null, false, 0, false);
7676
}
7777
$curl->execute();
7878
Workflow::cleanCache();

search.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,15 @@ private static function addDefaultCommands($isSearch, $isUser, $isRepo, $queryUs
297297
private static function addRepoSearchCommands()
298298
{
299299
$q = substr(self::$query, 2);
300-
$repos = Workflow::requestApi('/search/repositories?q='.urlencode($q));
300+
$repos = Workflow::requestApi('/search/repositories?q='.urlencode($q), null, null, true);
301301

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

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

310310
self::addUsers($users, 's @');
311311
}

workflow.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,13 @@ public static function request($url, Curl $curl = null, $callback = null, $withA
173173
* @param string $url
174174
* @param Curl $curl
175175
* @param callable $callback
176+
* @param bool $firstPageOnly
176177
* @param int $maxAge
177178
* @param bool $refreshInBackground
178179
*
179180
* @return mixed
180181
*/
181-
public static function requestCache($url, Curl $curl = null, $callback = null, $maxAge = self::DEFAULT_CACHE_MAX_AGE, $refreshInBackground = true)
182+
public static function requestCache($url, Curl $curl = null, $callback = null, $firstPageOnly = false, $maxAge = self::DEFAULT_CACHE_MAX_AGE, $refreshInBackground = true)
182183
{
183184
$return = false;
184185
$returnValue = null;
@@ -209,20 +210,25 @@ public static function requestCache($url, Curl $curl = null, $callback = null, $
209210
if (!$shouldRefresh || $refreshInBackground) {
210211
self::log('using cached content for %s', $url);
211212
$content = json_decode($content);
212-
$stmt = self::getStatement('SELECT url, content FROM request_cache WHERE parent = ? ORDER BY `timestamp` DESC');
213-
while ($stmt->execute(array($url)) && $data = $stmt->fetchObject()) {
214-
$content = array_merge($content, json_decode($data->content));
215-
$url = $data->url;
213+
214+
if (!$firstPageOnly) {
215+
$stmt = self::getStatement('SELECT url, content FROM request_cache WHERE parent = ? ORDER BY `timestamp` DESC');
216+
while ($stmt->execute(array($url)) && $data = $stmt->fetchObject()) {
217+
$content = array_merge($content, json_decode($data->content));
218+
$url = $data->url;
219+
}
216220
}
221+
217222
if (is_callable($callback)) {
218223
$callback($content);
219224
}
225+
220226
return $returnValue;
221227
}
222228

223229
$responses = array();
224230

225-
$handleResponse = function (CurlResponse $response, $content, $parent = null) use (&$handleResponse, $curl, &$responses, $stmt, $callback) {
231+
$handleResponse = function (CurlResponse $response, $content, $parent = null) use (&$handleResponse, $curl, &$responses, $stmt, $callback, $firstPageOnly) {
226232
$url = $response->request->url;
227233
if ($response && in_array($response->status, array(200, 304))) {
228234
$checkNext = false;
@@ -239,7 +245,10 @@ public static function requestCache($url, Curl $curl = null, $callback = null, $
239245
$responses[] = $response->content;
240246
Workflow::getStatement('REPLACE INTO request_cache VALUES(?, ?, ?, ?, 0, ?)')
241247
->execute(array($url, time(), $response->etag, json_encode($response->content), $parent));
242-
if ($checkNext || $response->link && preg_match('/<(.+)>; rel="next"/U', $response->link, $match)) {
248+
249+
if ($firstPageOnly) {
250+
// do nothing
251+
} elseif ($checkNext || $response->link && preg_match('/<(.+)>; rel="next"/U', $response->link, $match)) {
243252
$stmt = Workflow::getStatement('SELECT * FROM request_cache WHERE parent = ?');
244253
$stmt->execute(array($url));
245254
if ($checkNext) {
@@ -290,10 +299,10 @@ public static function requestCache($url, Curl $curl = null, $callback = null, $
290299
return $returnValue;
291300
}
292301

293-
public static function requestApi($url, Curl $curl = null, $callback = null, $maxAge = self::DEFAULT_CACHE_MAX_AGE)
302+
public static function requestApi($url, Curl $curl = null, $callback = null, $firstPageOnly = false, $maxAge = self::DEFAULT_CACHE_MAX_AGE)
294303
{
295304
$url = self::getApiUrl($url);
296-
return self::requestCache($url, $curl, $callback, $maxAge);
305+
return self::requestCache($url, $curl, $callback, $firstPageOnly, $maxAge);
297306
}
298307

299308
public static function cleanCache()
@@ -345,7 +354,7 @@ public static function checkUpdate()
345354
if (!self::getConfig('autoupdate', 1)) {
346355
return false;
347356
}
348-
$release = self::requestCache('https://api.github.com/repos/gharlan/alfred-github-workflow/releases/latest', null, null, 1440);
357+
$release = self::requestCache('https://api.github.com/repos/gharlan/alfred-github-workflow/releases/latest', null, null, true, 1440);
349358
if (!$release) {
350359
return false;
351360
}

0 commit comments

Comments
 (0)