|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the Project class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Xtools; |
|
7
|
|
|
|
|
8
|
|
|
use Mediawiki\Api\MediawikiApi; |
|
9
|
|
|
use Symfony\Component\VarDumper\VarDumper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* A Project is a single wiki that XTools is querying. |
|
13
|
|
|
*/ |
|
14
|
|
|
class Project extends Model |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** @var string The project name as supplied by the user. */ |
|
18
|
|
|
protected $nameUnnormalized; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string[] Basic metadata about the project */ |
|
21
|
|
|
protected $metadata; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Create a new Project. |
|
25
|
|
|
* @param string $nameOrUrl The project's database name or URL. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($nameOrUrl) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->nameUnnormalized = $nameOrUrl; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get basic metadata about the project. |
|
34
|
|
|
* @return \string[] |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getMetadata() |
|
37
|
|
|
{ |
|
38
|
|
|
if (!$this->metadata) { |
|
|
|
|
|
|
39
|
|
|
$this->metadata = $this->getRepository()->getOne($this->nameUnnormalized); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
return $this->metadata; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Does this project exist? |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
public function exists() |
|
49
|
|
|
{ |
|
50
|
|
|
return !empty($this->getDomain()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The unique domain name of this project, without protocol or path components. |
|
55
|
|
|
* This should be used as the canonical project identifier. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getDomain() |
|
60
|
|
|
{ |
|
61
|
|
|
$url = isset($this->getMetadata()['url']) ? $this->getMetadata()['url'] : ''; |
|
62
|
|
|
return parse_url($url, PHP_URL_HOST); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The name of the database for this project. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getDatabaseName() |
|
71
|
|
|
{ |
|
72
|
|
|
return isset($this->getMetadata()['dbname']) ? $this->getMetadata()['dbname'] : ''; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The language for this project. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getLang() |
|
81
|
|
|
{ |
|
82
|
|
|
return isset($this->getMetadata()['lang']) ? $this->getMetadata()['lang'] : ''; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* The project URL is the fully-qualified domain name, with protocol and trailing slash. |
|
87
|
|
|
* |
|
88
|
|
|
* @param bool $withTrailingSlash Whether to append a slash. |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getUrl($withTrailingSlash = true) |
|
92
|
|
|
{ |
|
93
|
|
|
return rtrim($this->getMetadata()['url'], '/') . ($withTrailingSlash ? '/' : ''); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get a MediawikiApi object for this Project. |
|
98
|
|
|
* |
|
99
|
|
|
* @return MediawikiApi |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getApi() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->getRepository()->getMediawikiApi($this); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* The base URL path of this project (that page titles are appended to). |
|
108
|
|
|
* For some wikis the title (apparently) may not be at the end. |
|
109
|
|
|
* Replace $1 with the article name. |
|
110
|
|
|
* |
|
111
|
|
|
* @link https://www.mediawiki.org/wiki/Manual:$wgArticlePath |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function getArticlePath() |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$metadata = $this->getRepository()->getMetadata($this->getUrl()); |
|
|
|
|
|
|
118
|
|
|
return isset($metadata['general']['articlePath']) |
|
119
|
|
|
? $metadata['general']['articlePath'] |
|
120
|
|
|
: '/wiki/$1'; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* The URL path of the directory that contains index.php, with no trailing slash. |
|
125
|
|
|
* Defaults to '/w' which is the same as the normal WMF set-up. |
|
126
|
|
|
* |
|
127
|
|
|
* @link https://www.mediawiki.org/wiki/Manual:$wgScriptPath |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
View Code Duplication |
public function getScriptPath() |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
$metadata = $this->getRepository()->getMetadata($this->getUrl()); |
|
|
|
|
|
|
134
|
|
|
return isset($metadata['general']['scriptPath']) |
|
135
|
|
|
? $metadata['general']['scriptPath'] |
|
136
|
|
|
: '/w'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* The URL path to index.php |
|
141
|
|
|
* Defaults to '/w/index.php' which is the same as the normal WMF set-up. |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
View Code Duplication |
public function getScript() |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
$metadata = $this->getRepository()->getMetadata($this->getUrl()); |
|
|
|
|
|
|
148
|
|
|
return isset($metadata['general']['script']) |
|
149
|
|
|
? $metadata['general']['script'] |
|
150
|
|
|
: $this->getScriptPath() . '/index.php'; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* The full URL to api.php |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getApiUrl() |
|
159
|
|
|
{ |
|
160
|
|
|
return rtrim($this->getUrl(), '/') . $this->getRepository()->getApiPath(); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get this project's title, the human-language full title of the wiki (e.g. "English |
|
165
|
|
|
* Wikipedia" or |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getTitle() |
|
168
|
|
|
{ |
|
169
|
|
|
$metadata = $this->getRepository()->getMetadata($this->getUrl()); |
|
|
|
|
|
|
170
|
|
|
return $metadata['general']['wikiName'].' ('.$this->getDomain().')'; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get an array of this project's namespaces and their IDs. |
|
175
|
|
|
* |
|
176
|
|
|
* @return string[] Keys are IDs, values are names. |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getNamespaces() |
|
179
|
|
|
{ |
|
180
|
|
|
$metadata = $this->getRepository()->getMetadata($this->getUrl()); |
|
|
|
|
|
|
181
|
|
|
return $metadata['namespaces']; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get the name of the page on this project that the user must create in order to opt in for |
|
186
|
|
|
* restricted statistics display. |
|
187
|
|
|
* @param User $user |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function userOptInPage(User $user) |
|
191
|
|
|
{ |
|
192
|
|
|
$localPageName = 'User:' . $user->getUsername() . '/EditCounterOptIn.js'; |
|
193
|
|
|
return $localPageName; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Has a user opted in to having their restricted statistics displayed to anyone? |
|
198
|
|
|
* @param User $user |
|
199
|
|
|
* @return bool |
|
200
|
|
|
*/ |
|
201
|
|
|
public function userHasOptedIn(User $user) |
|
202
|
|
|
{ |
|
203
|
|
|
// 1. First check to see if the whole project has opted in. |
|
204
|
|
|
if (!isset($this->metadata['opted_in'])) { |
|
205
|
|
|
$optedInProjects = $this->getRepository()->optedIn(); |
|
|
|
|
|
|
206
|
|
|
$this->metadata['opted_in'] = in_array($this->getDatabaseName(), $optedInProjects); |
|
207
|
|
|
} |
|
208
|
|
|
if ($this->metadata['opted_in']) { |
|
209
|
|
|
return true; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
// 2. Then see if the currently-logged-in user is requesting their own statistics. |
|
213
|
|
|
if ($user->isCurrentlyLoggedIn()) { |
|
214
|
|
|
return true; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
// 3. Then see if the user has opted in on this project. |
|
218
|
|
|
$userNsId = 2; |
|
219
|
|
|
$localExists = $this->getRepository() |
|
|
|
|
|
|
220
|
|
|
->pageHasContent($this, $userNsId, $this->userOptInPage($user)); |
|
221
|
|
|
if ($localExists) { |
|
222
|
|
|
return true; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// 4. Lastly, see if they've opted in globally on the default project or Meta. |
|
226
|
|
|
$globalPageName = $user->getUsername() . '/EditCounterGlobalOptIn.js'; |
|
227
|
|
|
$globalProject = $this->getRepository()->getGlobalProject(); |
|
|
|
|
|
|
228
|
|
|
if ($globalProject instanceof Project) { |
|
229
|
|
|
$globalExists = $globalProject->getRepository() |
|
|
|
|
|
|
230
|
|
|
->pageHasContent($globalProject, $userNsId, $globalPageName); |
|
231
|
|
|
if ($globalExists) { |
|
232
|
|
|
return true; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return false; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
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.