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