1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Maps\DataAccess; |
6
|
|
|
|
7
|
|
|
use FileFetcher\FileFetcher; |
8
|
|
|
use FileFetcher\FileFetchingException; |
9
|
|
|
use MediaWiki\Storage\RevisionLookup; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Returns the content of the JSON file at the specified location as array. |
13
|
|
|
* Empty array is returned on failure. |
14
|
|
|
* |
15
|
|
|
* @licence GNU GPL v2+ |
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
17
|
|
|
*/ |
18
|
|
|
class GeoJsonFetcher { |
19
|
|
|
|
20
|
|
|
private $fileFetcher; |
21
|
|
|
private $titleParser; |
22
|
|
|
private $revisionLookup; |
23
|
|
|
|
24
|
11 |
|
public function __construct( FileFetcher $fileFetcher, \TitleParser $titleParser, RevisionLookup $revisionLookup ) { |
25
|
11 |
|
$this->fileFetcher = $fileFetcher; |
26
|
11 |
|
$this->titleParser = $titleParser; |
27
|
11 |
|
$this->revisionLookup = $revisionLookup; |
28
|
11 |
|
} |
29
|
|
|
|
30
|
7 |
|
public function parse( string $fileLocation ): array { |
31
|
7 |
|
return $this->fetch( $fileLocation )->getContent(); |
32
|
|
|
} |
33
|
|
|
|
34
|
11 |
|
public function fetch( string $fileLocation ) { |
35
|
|
|
try { |
36
|
11 |
|
$title = $this->titleParser->parseTitle( $fileLocation, NS_GEO_JSON ); |
37
|
|
|
|
38
|
10 |
|
$revision = $this->revisionLookup->getRevisionByTitle( $title ); |
39
|
|
|
|
40
|
10 |
|
if ( $revision !== null ) { |
41
|
5 |
|
$content = $revision->getContent( 'main' ); |
42
|
|
|
|
43
|
5 |
|
if ( $content instanceof \JsonContent ) { |
|
|
|
|
44
|
5 |
|
return new GeoJsonFetcherResult( |
45
|
10 |
|
$this->normalizeJson( $content->getNativeData() ), |
46
|
5 |
|
$revision->getId(), |
47
|
|
|
$title |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
1 |
|
catch ( \MalformedTitleException $e ) { |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Prevent reading JSON files on the server |
56
|
6 |
|
if( !filter_var( $fileLocation, FILTER_VALIDATE_URL) ) { |
57
|
3 |
|
return $this->newEmptyResult(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
try { |
61
|
3 |
|
return new GeoJsonFetcherResult( |
62
|
3 |
|
$this->normalizeJson( $this->fileFetcher->fetchFile( $fileLocation ) ), |
63
|
2 |
|
null, |
64
|
2 |
|
null |
65
|
|
|
); |
66
|
|
|
} |
67
|
1 |
|
catch ( FileFetchingException $ex ) { |
68
|
1 |
|
return $this->newEmptyResult(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
private function newEmptyResult(): GeoJsonFetcherResult { |
73
|
4 |
|
return new GeoJsonFetcherResult( |
74
|
4 |
|
[], |
75
|
4 |
|
null, |
76
|
4 |
|
null |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
7 |
|
private function normalizeJson( ?string $jsonString ): array { |
81
|
7 |
|
if ( $jsonString === null ) { |
82
|
|
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
7 |
|
$json = json_decode( $jsonString, true ); |
86
|
|
|
|
87
|
7 |
|
if ( $json === null ) { |
88
|
1 |
|
return []; |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
return $json; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.