]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/LocalisationUpdate/fetcher/GitHubFetcher.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / extensions / LocalisationUpdate / fetcher / GitHubFetcher.php
1 <?php
2 /**
3  * @file
4  * @author Niklas Laxström
5  * @license GPL-2.0+
6  */
7
8 namespace LocalisationUpdate;
9
10 /**
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.
13  *
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
16  * if we store them.
17  */
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 );
23
24                 $apiURL = "https://api.github.com/repos/{$m['org']}/{$m['repo']}/contents/{$m['path']}";
25                 $json = \Http::get( $apiURL );
26                 if ( !$json ) {
27                         throw new \Exception( "Unable to get directory listing for {$m['org']}/{$m['repo']}" );
28                 }
29
30                 $files = [];
31                 $json = \FormatJson::decode( $json, true );
32                 foreach ( $json as $fileinfo ) {
33                         $fileurl = dirname( $pattern ) . '/' . $fileinfo['name'];
34                         $file = $this->fetchFile( $fileurl );
35                         if ( $file ) {
36                                 $files[$fileurl] = $file;
37                         }
38                 }
39                 return $files;
40         }
41 }