summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/src/DataAccess
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Maps/src/DataAccess')
-rw-r--r--www/wiki/extensions/Maps/src/DataAccess/GeoJsonFetcher.php93
-rw-r--r--www/wiki/extensions/Maps/src/DataAccess/GeoJsonFetcherResult.php31
-rw-r--r--www/wiki/extensions/Maps/src/DataAccess/JsonFileParser.php79
3 files changed, 124 insertions, 79 deletions
diff --git a/www/wiki/extensions/Maps/src/DataAccess/GeoJsonFetcher.php b/www/wiki/extensions/Maps/src/DataAccess/GeoJsonFetcher.php
new file mode 100644
index 00000000..ea7724d5
--- /dev/null
+++ b/www/wiki/extensions/Maps/src/DataAccess/GeoJsonFetcher.php
@@ -0,0 +1,93 @@
+<?php
+
+declare( strict_types = 1 );
+
+namespace Maps\DataAccess;
+
+use FileFetcher\FileFetcher;
+use FileFetcher\FileFetchingException;
+use MediaWiki\Storage\RevisionLookup;
+
+/**
+ * Returns the content of the JSON file at the specified location as array.
+ * Empty array is returned on failure.
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
+ */
+class GeoJsonFetcher {
+
+ private $fileFetcher;
+ private $titleParser;
+ private $revisionLookup;
+
+ public function __construct( FileFetcher $fileFetcher, \TitleParser $titleParser, RevisionLookup $revisionLookup ) {
+ $this->fileFetcher = $fileFetcher;
+ $this->titleParser = $titleParser;
+ $this->revisionLookup = $revisionLookup;
+ }
+
+ public function parse( string $fileLocation ): array {
+ return $this->fetch( $fileLocation )->getContent();
+ }
+
+ public function fetch( string $fileLocation ) {
+ try {
+ $title = $this->titleParser->parseTitle( $fileLocation, NS_GEO_JSON );
+ $revision = $this->revisionLookup->getRevisionByTitle( $title );
+
+ if ( $revision !== null ) {
+ $content = $revision->getContent( 'main' );
+
+ if ( $content instanceof \JsonContent ) {
+ return new GeoJsonFetcherResult(
+ $this->normalizeJson( $content->getNativeData() ),
+ $revision->getId(),
+ $title
+ );
+ }
+ }
+ }
+ catch ( \MalformedTitleException $e ) {
+ }
+
+ // Prevent reading JSON files on the server
+ if( !filter_var( $fileLocation, FILTER_VALIDATE_URL) ) {
+ return $this->newEmptyResult();
+ }
+
+ try {
+ return new GeoJsonFetcherResult(
+ $this->normalizeJson( $this->fileFetcher->fetchFile( $fileLocation ) ),
+ null,
+ null
+ );
+ }
+ catch ( FileFetchingException $ex ) {
+ return $this->newEmptyResult();
+ }
+ }
+
+ private function newEmptyResult(): GeoJsonFetcherResult {
+ return new GeoJsonFetcherResult(
+ [],
+ null,
+ null
+ );
+ }
+
+ private function normalizeJson( ?string $jsonString ): array {
+ if ( $jsonString === null ) {
+ return [];
+ }
+
+ $json = json_decode( $jsonString, true );
+
+ if ( $json === null ) {
+ return [];
+ }
+
+ return $json;
+ }
+
+}
diff --git a/www/wiki/extensions/Maps/src/DataAccess/GeoJsonFetcherResult.php b/www/wiki/extensions/Maps/src/DataAccess/GeoJsonFetcherResult.php
new file mode 100644
index 00000000..7345b649
--- /dev/null
+++ b/www/wiki/extensions/Maps/src/DataAccess/GeoJsonFetcherResult.php
@@ -0,0 +1,31 @@
+<?php
+
+declare( strict_types = 1 );
+
+namespace Maps\DataAccess;
+
+class GeoJsonFetcherResult {
+
+ private $content;
+ private $revisionId;
+ private $source;
+
+ public function __construct( array $content, ?int $revisionId, ?\TitleValue $source ) {
+ $this->content = $content;
+ $this->revisionId = $revisionId;
+ $this->source = $source;
+ }
+
+ public function getContent(): array {
+ return $this->content;
+ }
+
+ public function getTitleValue(): ?\TitleValue {
+ return $this->source;
+ }
+
+ public function getRevisionId(): ?int {
+ return $this->revisionId;
+ }
+
+}
diff --git a/www/wiki/extensions/Maps/src/DataAccess/JsonFileParser.php b/www/wiki/extensions/Maps/src/DataAccess/JsonFileParser.php
deleted file mode 100644
index 81d6cfa0..00000000
--- a/www/wiki/extensions/Maps/src/DataAccess/JsonFileParser.php
+++ /dev/null
@@ -1,79 +0,0 @@
-<?php
-
-declare( strict_types = 1 );
-
-namespace Maps\DataAccess;
-
-use FileFetcher\FileFetcher;
-use FileFetcher\FileFetchingException;
-use Maps\MapsFactory;
-use ValueParsers\ParseException;
-use ValueParsers\ValueParser;
-
-/**
- * Returns the content of the JSON file at the specified location as array.
- * Empty array is returned on failure.
- *
- * @licence GNU GPL v2+
- * @author Jeroen De Dauw < jeroendedauw@gmail.com >
- */
-class JsonFileParser implements ValueParser {
-
- private $fileFetcher;
- private $pageContentFetcher;
- private $defaultNamespace;
-
- public function __construct( $fileFetcher = null, PageContentFetcher $pageContentFetcher = null ) {
- $this->fileFetcher = $fileFetcher instanceof FileFetcher
- ? $fileFetcher : MapsFactory::newDefault()->getGeoJsonFileFetcher();
-
- $this->pageContentFetcher = $pageContentFetcher instanceof PageContentFetcher
- ? $pageContentFetcher : MapsFactory::newDefault()->getPageContentFetcher();
-
- $this->defaultNamespace = NS_GEO_JSON;
- }
-
- /**
- * @param string $fileLocation
- *
- * @return array
- * @throws ParseException
- */
- public function parse( $fileLocation ) {
- $jsonString = $this->getJsonString( $fileLocation );
-
- if ( $jsonString === null ) {
- return [];
- }
-
- $json = json_decode( $jsonString, true );
-
- if ( $json === null ) {
- return [];
- }
-
- return $json;
- }
-
- private function getJsonString( string $fileLocation ): ?string {
- $content = $this->pageContentFetcher->getPageContent( $fileLocation, $this->defaultNamespace );
-
- if ( $content instanceof \JsonContent ) {
- return $content->getNativeData();
- }
-
- // Prevent reading JSON files on the server
- if( !filter_var( $fileLocation, FILTER_VALIDATE_URL) ) {
- return null;
- }
-
- try {
- return $this->fileFetcher->fetchFile( $fileLocation );
- }
- catch ( FileFetchingException $ex ) {
- return null;
- }
- }
-
-
-}