summaryrefslogtreecommitdiff
path: root/extensions/LocalisationUpdate/fetcher
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/LocalisationUpdate/fetcher')
-rw-r--r--extensions/LocalisationUpdate/fetcher/Fetcher.php26
-rw-r--r--extensions/LocalisationUpdate/fetcher/FetcherFactory.php24
-rw-r--r--extensions/LocalisationUpdate/fetcher/FileSystemFetcher.php35
-rw-r--r--extensions/LocalisationUpdate/fetcher/GitHubFetcher.php38
-rw-r--r--extensions/LocalisationUpdate/fetcher/HttpFetcher.php40
5 files changed, 163 insertions, 0 deletions
diff --git a/extensions/LocalisationUpdate/fetcher/Fetcher.php b/extensions/LocalisationUpdate/fetcher/Fetcher.php
new file mode 100644
index 00000000..9c06c105
--- /dev/null
+++ b/extensions/LocalisationUpdate/fetcher/Fetcher.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * Interface for classes which fetch files over different protocols and ways.
+ */
+interface LU_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/extensions/LocalisationUpdate/fetcher/FetcherFactory.php b/extensions/LocalisationUpdate/fetcher/FetcherFactory.php
new file mode 100644
index 00000000..4e26dc15
--- /dev/null
+++ b/extensions/LocalisationUpdate/fetcher/FetcherFactory.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * Constructs fetchers based on the repository urls.
+ */
+class LU_FetcherFactory {
+ public function getFetcher( $path ) {
+
+ if ( strpos( $path, 'https://raw.github.com/' ) === 0 ) {
+ return new LU_GitHubFetcher();
+ } elseif ( strpos( $path, 'http://' ) === 0 ) {
+ return new LU_HttpFetcher();
+ } elseif ( strpos( $path, 'https://' ) === 0 ) {
+ return new LU_HttpFetcher();
+ } else {
+ return new LU_FileSystemFetcher();
+ }
+ }
+}
diff --git a/extensions/LocalisationUpdate/fetcher/FileSystemFetcher.php b/extensions/LocalisationUpdate/fetcher/FileSystemFetcher.php
new file mode 100644
index 00000000..a463d6cd
--- /dev/null
+++ b/extensions/LocalisationUpdate/fetcher/FileSystemFetcher.php
@@ -0,0 +1,35 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * Accesses file system directly.
+ */
+class LU_FileSystemFetcher implements LU_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 = array();
+ foreach ( glob( $pattern ) as $file ) {
+ if ( is_readable( $file ) ) {
+ $data["file://$file"] = file_get_contents( $file );
+ }
+ }
+ return $data;
+ }
+}
diff --git a/extensions/LocalisationUpdate/fetcher/GitHubFetcher.php b/extensions/LocalisationUpdate/fetcher/GitHubFetcher.php
new file mode 100644
index 00000000..00c9b6ca
--- /dev/null
+++ b/extensions/LocalisationUpdate/fetcher/GitHubFetcher.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * 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 LU_GitHubFetcher extends LU_HttpFetcher {
+
+ public function fetchDirectory( $pattern ) {
+ $p = '~^https://raw.github\.com/(?P<org>[^/]+)/(?P<repo>[^/]+)/(?P<branch>[^/]+)/(?P<path>.+)/.+$~';
+ preg_match( $p, $pattern, $m );
+
+ $json = Http::get( "https://api.github.com/repos/{$m['org']}/{$m['repo']}/contents/{$m['path']}" );
+ if ( !$json ) {
+ throw new MWException( "Unable to get directory listing for {$m['org']}/{$m['repo']}" );
+ }
+
+ $files = array();
+ $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/extensions/LocalisationUpdate/fetcher/HttpFetcher.php b/extensions/LocalisationUpdate/fetcher/HttpFetcher.php
new file mode 100644
index 00000000..9dfed8db
--- /dev/null
+++ b/extensions/LocalisationUpdate/fetcher/HttpFetcher.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * Fetches files over HTTP(s).
+ */
+class LU_HttpFetcher implements LU_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 = array();
+
+ $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;
+ }
+}