4 * @author Niklas Laxström
8 namespace LocalisationUpdate;
11 * This class uses GitHub api to obtain a list of files present in a directory
12 * to avoid fetching files that don't exist.
14 * @todo Could use file hashes to 1) avoid fetching files with same hash as
15 * the source. 2) avoid fetching files which haven't changed since last check
18 class GitHubFetcher extends HttpFetcher {
19 public function fetchDirectory( $pattern ) {
20 $domain = preg_quote( 'https://raw.github.com/', '~' );
21 $p = "~^$domain(?P<org>[^/]+)/(?P<repo>[^/]+)/(?P<branch>[^/]+)/(?P<path>.+)/.+$~";
22 preg_match( $p, $pattern, $m );
24 $apiURL = "https://api.github.com/repos/{$m['org']}/{$m['repo']}/contents/{$m['path']}";
25 $json = \Http::get( $apiURL );
27 throw new \Exception( "Unable to get directory listing for {$m['org']}/{$m['repo']}" );
31 $json = \FormatJson::decode( $json, true );
32 foreach ( $json as $fileinfo ) {
33 $fileurl = dirname( $pattern ) . '/' . $fileinfo['name'];
34 $file = $this->fetchFile( $fileurl );
36 $files[$fileurl] = $file;