summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/LocalisationUpdate/fetcher
diff options
context:
space:
mode:
authorYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
committerYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
commitfc7369835258467bf97eb64f184b93691f9a9fd5 (patch)
treedaabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/LocalisationUpdate/fetcher
first commit
Diffstat (limited to 'www/wiki/extensions/LocalisationUpdate/fetcher')
-rw-r--r--www/wiki/extensions/LocalisationUpdate/fetcher/Fetcher.php28
-rw-r--r--www/wiki/extensions/LocalisationUpdate/fetcher/FetcherFactory.php25
-rw-r--r--www/wiki/extensions/LocalisationUpdate/fetcher/FileSystemFetcher.php37
-rw-r--r--www/wiki/extensions/LocalisationUpdate/fetcher/GitHubFetcher.php41
-rw-r--r--www/wiki/extensions/LocalisationUpdate/fetcher/HttpFetcher.php42
5 files changed, 173 insertions, 0 deletions
diff --git a/www/wiki/extensions/LocalisationUpdate/fetcher/Fetcher.php b/www/wiki/extensions/LocalisationUpdate/fetcher/Fetcher.php
new file mode 100644
index 00000000..9ff79584
--- /dev/null
+++ b/www/wiki/extensions/LocalisationUpdate/fetcher/Fetcher.php
@@ -0,0 +1,28 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+namespace LocalisationUpdate;
+
+/**
+ * Interface for classes which fetch files over different protocols and ways.
+ */
+interface Fetcher {
+ /**
+ * Fetches a single resource.
+ *
+ * @return bool|string False on failure.
+ */
+ public function fetchFile( $url );
+
+ /**
+ * Fetch a list of resources. This has the benefit of being able to pick up
+ * new languages as they appear if languages are stored in separate files.
+ *
+ * @return array
+ */
+ public function fetchDirectory( $pattern );
+}
diff --git a/www/wiki/extensions/LocalisationUpdate/fetcher/FetcherFactory.php b/www/wiki/extensions/LocalisationUpdate/fetcher/FetcherFactory.php
new file mode 100644
index 00000000..570dc7e0
--- /dev/null
+++ b/www/wiki/extensions/LocalisationUpdate/fetcher/FetcherFactory.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+namespace LocalisationUpdate;
+
+/**
+ * Constructs fetchers based on the repository urls.
+ */
+class FetcherFactory {
+ public function getFetcher( $path ) {
+ if ( strpos( $path, 'https://raw.github.com/' ) === 0 ) {
+ return new GitHubFetcher();
+ } elseif ( strpos( $path, 'http://' ) === 0 ) {
+ return new HttpFetcher();
+ } elseif ( strpos( $path, 'https://' ) === 0 ) {
+ return new HttpFetcher();
+ } else {
+ return new FileSystemFetcher();
+ }
+ }
+}
diff --git a/www/wiki/extensions/LocalisationUpdate/fetcher/FileSystemFetcher.php b/www/wiki/extensions/LocalisationUpdate/fetcher/FileSystemFetcher.php
new file mode 100644
index 00000000..6d215867
--- /dev/null
+++ b/www/wiki/extensions/LocalisationUpdate/fetcher/FileSystemFetcher.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+namespace LocalisationUpdate;
+
+/**
+ * Accesses file system directly.
+ */
+class FileSystemFetcher implements Fetcher {
+ public function fetchFile( $url ) {
+ // Remove the protocol prefix
+ $url = preg_replace( '~^file://~', '', $url );
+
+ if ( !is_readable( $url ) ) {
+ return false;
+ }
+
+ return file_get_contents( $url );
+ }
+
+ public function fetchDirectory( $pattern ) {
+ // Remove the protocol prefix
+ $pattern = preg_replace( '~^file://~', '', $pattern );
+
+ $data = [];
+ foreach ( glob( $pattern ) as $file ) {
+ if ( is_readable( $file ) ) {
+ $data["file://$file"] = file_get_contents( $file );
+ }
+ }
+ return $data;
+ }
+}
diff --git a/www/wiki/extensions/LocalisationUpdate/fetcher/GitHubFetcher.php b/www/wiki/extensions/LocalisationUpdate/fetcher/GitHubFetcher.php
new file mode 100644
index 00000000..5bc7d4f2
--- /dev/null
+++ b/www/wiki/extensions/LocalisationUpdate/fetcher/GitHubFetcher.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+namespace LocalisationUpdate;
+
+/**
+ * This class uses GitHub api to obtain a list of files present in a directory
+ * to avoid fetching files that don't exist.
+ *
+ * @todo Could use file hashes to 1) avoid fetching files with same hash as
+ * the source. 2) avoid fetching files which haven't changed since last check
+ * if we store them.
+ */
+class GitHubFetcher extends HttpFetcher {
+ public function fetchDirectory( $pattern ) {
+ $domain = preg_quote( 'https://raw.github.com/', '~' );
+ $p = "~^$domain(?P<org>[^/]+)/(?P<repo>[^/]+)/(?P<branch>[^/]+)/(?P<path>.+)/.+$~";
+ preg_match( $p, $pattern, $m );
+
+ $apiURL = "https://api.github.com/repos/{$m['org']}/{$m['repo']}/contents/{$m['path']}";
+ $json = \Http::get( $apiURL );
+ if ( !$json ) {
+ throw new \Exception( "Unable to get directory listing for {$m['org']}/{$m['repo']}" );
+ }
+
+ $files = [];
+ $json = \FormatJson::decode( $json, true );
+ foreach ( $json as $fileinfo ) {
+ $fileurl = dirname( $pattern ) . '/' . $fileinfo['name'];
+ $file = $this->fetchFile( $fileurl );
+ if ( $file ) {
+ $files[$fileurl] = $file;
+ }
+ }
+ return $files;
+ }
+}
diff --git a/www/wiki/extensions/LocalisationUpdate/fetcher/HttpFetcher.php b/www/wiki/extensions/LocalisationUpdate/fetcher/HttpFetcher.php
new file mode 100644
index 00000000..42b0f032
--- /dev/null
+++ b/www/wiki/extensions/LocalisationUpdate/fetcher/HttpFetcher.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+namespace LocalisationUpdate;
+
+/**
+ * Fetches files over HTTP(s).
+ */
+class HttpFetcher implements Fetcher {
+ public function fetchFile( $url ) {
+ return \Http::get( $url );
+ }
+
+ /**
+ * This is horribly inefficient. Subclasses have more efficient
+ * implementation of this.
+ */
+ public function fetchDirectory( $pattern ) {
+ $files = [];
+
+ $languages = \Language::fetchLanguageNames( null, 'mwfile' );
+
+ foreach ( array_keys( $languages ) as $code ) {
+ // Hack for core
+ if ( strpos( $pattern, 'Messages*.php' ) !== false ) {
+ $code = ucfirst( strtr( $code, '-', '_' ) );
+ }
+
+ $url = str_replace( '*', $code, $pattern );
+ $file = $this->fetchFile( $url );
+ if ( $file ) {
+ $files[$url] = $file;
+ }
+ }
+
+ return $files;
+ }
+}