@@ -164,74 +164,122 @@ public function getItems()
164164
165165 $ this ->ordering = $ this ->getState ('list.ordering ' , 'title ' );
166166 $ this ->orderDir = $ this ->getState ('list.direction ' , 'asc ' );
167- $ search = $ this ->getState ('filter.search ' );
168- $ searchId = $ this ->getState ('filter.searchid ' );
169-
170- $ page = $ this ->getPagination ()->pagesCurrent ;
171167
172168 try
173169 {
174- // If over the API limit, we can't build this list
175- // TODO - Cache the request data in case of API limiting
176- if ($ this ->rate ->remaining > 0 )
170+ $ cacheFile = JPATH_CACHE . '/patchtester.json ' ;
171+ $ params = $ this ->getState ('params ' );
172+
173+ // Check if caching is enabled
174+ if ($ params ->get ('cache ' , 1 ) == 1 )
177175 {
178- $ pulls = $ this ->github ->pulls ->getList ($ this ->getState ('github_user ' ), $ this ->getState ('github_repo ' ), 'open ' , $ page );
179- usort ($ pulls , array ($ this , 'sortItems ' ));
176+ // Fetch cache time from component parameters and convert to seconds
177+ $ cacheTime = $ params ->get ('cache_lifetime ' , 60 );
178+ $ cacheTime = $ cacheTime * 60 ;
180179
181- foreach ($ pulls as $ i => &$ pull )
180+ // Cache files expired?
181+ if (!file_exists ($ cacheFile ) || (time () - @filemtime ($ cacheFile ) > $ cacheTime ))
182182 {
183- if ($ search && false === strpos ($ pull ->title , $ search ))
184- {
185- unset($ pulls [$ i ]);
186- continue ;
187- }
183+ // Do a request to the GitHub API for new data
184+ $ pulls = $ this ->requestFromGithub ();
185+ }
186+ else
187+ {
188+ // Render from the cached data
189+ $ pulls = json_decode (file_get_contents ($ cacheFile ));
190+ }
191+ }
192+ else
193+ {
194+ // No caching, request from GitHub
195+ $ pulls = $ this ->requestFromGithub ();
196+ }
188197
189- if ( $ searchId && $ pull -> number != $ searchId )
190- {
191- unset( $ pulls [ $ i ]);
192- continue ;
193- }
198+ return $ pulls ;
199+ }
200+ catch ( Exception $ e )
201+ {
202+ JFactory:: getApplication ()-> enqueueMessage ( $ e -> getMessage (), ' error ' );
194203
195- // Try to find a Joomlacode issue number
196- $ pulls [$ i ]->joomlacode_issue = 0 ;
204+ return array ();
205+ }
206+ }
197207
198- $ matches = array ();
208+ /**
209+ * Method to request new data from GitHub
210+ *
211+ * @return array Pull request data
212+ *
213+ * @since 2.0
214+ */
215+ protected function requestFromGithub ()
216+ {
217+ // If over the API limit, we can't build this list
218+ if ($ this ->rate ->remaining > 0 )
219+ {
220+ $ page = $ this ->getPagination ()->pagesCurrent ;
221+ $ search = $ this ->getState ('filter.search ' );
222+ $ searchId = $ this ->getState ('filter.searchid ' );
199223
200- preg_match ('#\[\#([0-9]+)\]# ' , $ pull ->title , $ matches );
224+ $ pulls = $ this ->github ->pulls ->getList ($ this ->getState ('github_user ' ), $ this ->getState ('github_repo ' ), 'open ' , $ page );
225+ usort ($ pulls , array ($ this , 'sortItems ' ));
226+
227+ foreach ($ pulls as $ i => &$ pull )
228+ {
229+ if ($ search && false === strpos ($ pull ->title , $ search ))
230+ {
231+ unset($ pulls [$ i ]);
232+ continue ;
233+ }
234+
235+ if ($ searchId && $ pull ->number != $ searchId )
236+ {
237+ unset($ pulls [$ i ]);
238+ continue ;
239+ }
240+
241+ // Try to find a Joomlacode issue number
242+ $ pulls [$ i ]->joomlacode_issue = 0 ;
243+
244+ $ matches = array ();
245+
246+ preg_match ('#\[\#([0-9]+)\]# ' , $ pull ->title , $ matches );
247+
248+ if (isset ($ matches [1 ]))
249+ {
250+ $ pulls [$ i ]->joomlacode_issue = (int ) $ matches [1 ];
251+ }
252+ else
253+ {
254+ preg_match ('#(http://joomlacode[-\w\./\?\S]+)# ' , $ pull ->body , $ matches );
201255
202256 if (isset ($ matches [1 ]))
203257 {
204- $ pulls [$ i ]->joomlacode_issue = (int ) $ matches [1 ];
205- }
206- else
207- {
208- preg_match ('#(http://joomlacode[-\w\./\?\S]+)# ' , $ pull ->body , $ matches );
258+ preg_match ('#tracker_item_id=([0-9]+)# ' , $ matches [1 ], $ matches );
209259
210260 if (isset ($ matches [1 ]))
211261 {
212- preg_match ('#tracker_item_id=([0-9]+)# ' , $ matches [1 ], $ matches );
213-
214- if (isset ($ matches [1 ]))
215- {
216- $ pulls [$ i ]->joomlacode_issue = (int ) $ matches [1 ];
217- }
262+ $ pulls [$ i ]->joomlacode_issue = (int ) $ matches [1 ];
218263 }
219264 }
220265 }
221266 }
222- else
267+
268+ // If caching is enabled, save the request data
269+ $ params = $ this ->getState ('params ' );
270+
271+ if ($ params ->get ('cache ' ) == 1 )
223272 {
224- $ pulls = array ();
273+ $ data = json_encode ($ pulls );
274+ file_put_contents (JPATH_CACHE . '/patchtester.json ' , $ data );
225275 }
226-
227- return $ pulls ;
228276 }
229- catch ( Exception $ e )
277+ else
230278 {
231- JFactory::getApplication ()->enqueueMessage ($ e ->getMessage (), 'error ' );
232-
233- return array ();
279+ $ pulls = array ();
234280 }
281+
282+ return $ pulls ;
235283 }
236284
237285 /**
0 commit comments