1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xtools; |
4
|
|
|
|
5
|
|
|
use Mediawiki\Api\MediawikiApi; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A Project is a single wiki that Xtools is querying. |
9
|
|
|
*/ |
10
|
|
|
class Project extends Model |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** @var string The project name as supplied by the user. */ |
14
|
|
|
protected $nameUnnormalized; |
15
|
|
|
|
16
|
|
|
/** @var string[] Basic metadata about the project */ |
17
|
|
|
protected $metadata; |
18
|
|
|
|
19
|
|
|
public function __construct($nameOrUrl) |
20
|
|
|
{ |
21
|
|
|
$this->nameUnnormalized = $nameOrUrl; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function getMetadata() |
25
|
|
|
{ |
26
|
|
|
if (!$this->metadata) { |
|
|
|
|
27
|
|
|
$this->metadata = $this->getRepository()->getOne($this->nameUnnormalized); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
return $this->metadata; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Does this project exist? |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
public function exists() |
37
|
|
|
{ |
38
|
|
|
return !empty($this->getDomain()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The unique domain name of this project, without protocol or path components. |
43
|
|
|
* This should be used as the canonical project identifier. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getDomain() |
48
|
|
|
{ |
49
|
|
|
$url = isset($this->getMetadata()['url']) ? $this->getMetadata()['url'] : ''; |
50
|
|
|
return parse_url($url, PHP_URL_HOST); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The name of the database for this project. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getDatabaseName() |
59
|
|
|
{ |
60
|
|
|
return isset($this->getMetadata()['dbname']) ? $this->getMetadata()['dbname'] : ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The project URL is the fully-qualified domain name, with protocol and trailing slash. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getUrl() |
69
|
|
|
{ |
70
|
|
|
return rtrim($this->getMetadata()['url'], '/') . '/'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get a MediawikiApi object for this Project. |
75
|
|
|
* |
76
|
|
|
* @return MediawikiApi |
77
|
|
|
*/ |
78
|
|
|
public function getApi() |
79
|
|
|
{ |
80
|
|
|
return $this->getRepository()->getMediawikiApi($this); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The base URL path of this project (that page titles are appended to). |
85
|
|
|
* |
86
|
|
|
* @link https://www.mediawiki.org/wiki/Manual:$wgArticlePath |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function getArticlePath() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$metadata = $this->getRepository()->getMetadata($this->getUrl()); |
|
|
|
|
93
|
|
|
return isset($metadata['general']['articlePath']) |
94
|
|
|
? $metadata['general']['articlePath'] |
95
|
|
|
: '/wiki/'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* The URL path to index.php |
100
|
|
|
* |
101
|
|
|
* @link https://www.mediawiki.org/wiki/Manual:$wgScriptPath |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function getScriptPath() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$metadata = $this->getRepository()->getMetadata($this->getUrl()); |
|
|
|
|
108
|
|
|
return isset($metadata['general']['scriptPath']) |
109
|
|
|
? $metadata['general']['scriptPath'] |
110
|
|
|
: '/w/index.php'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get this project's title, the human-language full title of the wiki (e.g. "English |
115
|
|
|
* Wikipedia" or |
116
|
|
|
*/ |
117
|
|
|
public function getTitle() |
118
|
|
|
{ |
119
|
|
|
$metadata = $this->getRepository()->getMetadata($this->getUrl()); |
|
|
|
|
120
|
|
|
return $metadata['general']['wikiName'].' ('.$this->getDomain().')'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get an array of this project's namespaces and their IDs. |
125
|
|
|
* |
126
|
|
|
* @return string[] Keys are IDs, values are names. |
127
|
|
|
*/ |
128
|
|
|
public function getNamespaces() |
129
|
|
|
{ |
130
|
|
|
$metadata = $this->getRepository()->getMetadata($this->getUrl()); |
|
|
|
|
131
|
|
|
return $metadata['namespaces']; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.