@@ -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