1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediawiki\Api\Service; |
4
|
|
|
|
5
|
|
|
use Mediawiki\Api\MediawikiApi; |
6
|
|
|
use Mediawiki\Api\SimpleRequest; |
7
|
|
|
use Mediawiki\DataModel\NamespaceInfo; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author gbirke |
11
|
|
|
*/ |
12
|
|
|
class NamespaceGetter |
13
|
|
|
{ |
14
|
|
|
private $api; |
15
|
|
|
|
16
|
|
|
public function __construct( MediawikiApi $api ) { |
17
|
|
|
|
18
|
|
|
$this->api = $api; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $canonicalName |
23
|
|
|
* @return NamespaceInfo|null |
24
|
|
|
*/ |
25
|
|
|
public function getNamespaceByCanonicalName( $canonicalName ) { |
26
|
|
|
foreach ( $this->getNamespaceResult()['query']['namespaces'] as $nsInfo ) { |
27
|
|
|
if ( !empty( $nsInfo['canonical'] ) && $nsInfo['canonical'] === $canonicalName ) { |
28
|
|
|
return $this->createNamespaceFromQuery( $nsInfo ); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
return null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $name |
36
|
|
|
* @return NamespaceInfo|null |
37
|
|
|
*/ |
38
|
|
|
public function getNamespaceByName( $name ) { |
39
|
|
|
foreach ( $this->getNamespaceResult()['query']['namespaces'] as $nsInfo ) { |
40
|
|
|
if ( ( !empty( $nsInfo['canonical'] ) && $nsInfo['canonical'] === $name ) || |
41
|
|
|
$nsInfo['*'] === $name ) { |
42
|
|
|
return $this->createNamespaceFromQuery( $nsInfo ); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return NamespaceInfo[] |
50
|
|
|
*/ |
51
|
|
|
public function getNamespaces() { |
52
|
|
|
$namespaces = []; |
53
|
|
|
foreach ( $this->getNamespaceResult()['query']['namespaces'] as $nsInfo ) { |
54
|
|
|
$namespaces[$nsInfo['id']] = $this->createNamespaceFromQuery( $nsInfo ); |
55
|
|
|
} |
56
|
|
|
return $namespaces; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function createNamespaceFromQuery( $nsInfo ) { |
60
|
|
|
return new NamespaceInfo( |
61
|
|
|
$nsInfo['id'], |
62
|
|
|
empty( $nsInfo['canonical'] ) ? '' : $nsInfo['canonical'], |
63
|
|
|
$nsInfo['*'], |
64
|
|
|
$nsInfo['case'], |
65
|
|
|
empty( $nsInfo['defaultcontentmodel'] ) ? null : $nsInfo['defaultcontentmodel'] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
private function getNamespaceResult() { |
73
|
|
|
return $this->api->getRequest( new SimpleRequest( |
74
|
|
|
'query', [ |
75
|
|
|
'meta' => 'siteinfo', |
76
|
|
|
'siprop' => 'namespaces|namespacealiases' |
77
|
|
|
] |
78
|
|
|
) ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|