|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the Page class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Xtools; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A Page is a single wiki page in one project. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Page extends Model |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** @var Project The project that this page belongs to. */ |
|
15
|
|
|
protected $project; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string The page name as provided at instantiation. */ |
|
18
|
|
|
protected $unnormalizedPageName; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string[] Metadata about this page. */ |
|
21
|
|
|
protected $pageInfo; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string[] Revision history of this page */ |
|
24
|
|
|
protected $revisions; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Page constructor. |
|
28
|
|
|
* @param Project $project |
|
29
|
|
|
* @param string $pageName |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Project $project, $pageName) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->project = $project; |
|
34
|
|
|
$this->unnormalizedPageName = $pageName; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get basic information about this page from the repository. |
|
39
|
|
|
* @return \string[] |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getPageInfo() |
|
42
|
|
|
{ |
|
43
|
|
|
if (!$this->pageInfo) { |
|
|
|
|
|
|
44
|
|
|
$this->pageInfo = $this->getRepository() |
|
|
|
|
|
|
45
|
|
|
->getPageInfo($this->project, $this->unnormalizedPageName); |
|
46
|
|
|
} |
|
47
|
|
|
return $this->pageInfo; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the page's title. |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getTitle() |
|
55
|
|
|
{ |
|
56
|
|
|
$info = $this->getPageInfo(); |
|
57
|
|
|
return isset($info['title']) ? $info['title'] : $this->unnormalizedPageName; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get this page's database ID. |
|
62
|
|
|
* @return int |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getId() |
|
65
|
|
|
{ |
|
66
|
|
|
$info = $this->getPageInfo(); |
|
67
|
|
|
return isset($info['pageid']) ? $info['pageid'] : null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get this page's length in bytes. |
|
72
|
|
|
* @return int |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getLength() |
|
75
|
|
|
{ |
|
76
|
|
|
$info = $this->getPageInfo(); |
|
77
|
|
|
return isset($info['length']) ? $info['length'] : null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get HTML for the stylized display of the title. |
|
82
|
|
|
* The text will be the same as Page::getTitle(). |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getDisplayTitle() |
|
86
|
|
|
{ |
|
87
|
|
|
$info = $this->getPageInfo(); |
|
88
|
|
|
if (isset($info['displaytitle'])) { |
|
89
|
|
|
return $info['displaytitle']; |
|
90
|
|
|
} |
|
91
|
|
|
return $this->getTitle(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get the full URL of this page. |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getUrl() |
|
99
|
|
|
{ |
|
100
|
|
|
$info = $this->getPageInfo(); |
|
101
|
|
|
return isset($info['fullurl']) ? $info['fullurl'] : null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the numerical ID of the namespace of this page. |
|
106
|
|
|
* @return int |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getNamespace() |
|
109
|
|
|
{ |
|
110
|
|
|
$info = $this->getPageInfo(); |
|
111
|
|
|
return isset($info['ns']) ? $info['ns'] : null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the number of page watchers. |
|
116
|
|
|
* @return int |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getWatchers() |
|
119
|
|
|
{ |
|
120
|
|
|
$info = $this->getPageInfo(); |
|
121
|
|
|
return isset($info['ns']) ? $info['ns'] : null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Whether or not this page exists. |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
public function exists() |
|
129
|
|
|
{ |
|
130
|
|
|
$info = $this->getPageInfo(); |
|
131
|
|
|
return !isset($info['missing']); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the Project to which this page belongs. |
|
136
|
|
|
* @return Project |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getProject() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->project; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get the Wikidata ID of this page. |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getWikidataId() |
|
148
|
|
|
{ |
|
149
|
|
|
$info = $this->getPageInfo(); |
|
150
|
|
|
if (isset($info['pageprops']['wikibase_item'])) { |
|
151
|
|
|
return $info['pageprops']['wikibase_item']; |
|
152
|
|
|
} else { |
|
153
|
|
|
return null; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Get the number of revisions the page has. |
|
159
|
|
|
* @param User $user Optionally limit to those of this user. |
|
160
|
|
|
* @return int |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getNumRevisions(User $user = null) |
|
163
|
|
|
{ |
|
164
|
|
|
// Return the count of revisions if already present |
|
165
|
|
|
if ($this->revisions) { |
|
|
|
|
|
|
166
|
|
|
return count($this->revisions); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// Otherwise do a COUNT in the event fetching |
|
170
|
|
|
// all revisions is not desired |
|
171
|
|
|
return $this->getRepository()->getNumRevisions($this, $user); |
|
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Get all edits made to this page. |
|
176
|
|
|
* @param User|null $user Specify to get only revisions by the given user. |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getRevisions(User $user = null) |
|
180
|
|
|
{ |
|
181
|
|
|
if ($this->revisions) { |
|
|
|
|
|
|
182
|
|
|
return $this->revisions; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$data = $this->getRepository()->getRevisions($this, $user); |
|
|
|
|
|
|
186
|
|
|
$totalAdded = 0; |
|
187
|
|
|
$totalRemoved = 0; |
|
188
|
|
|
$revisions = []; |
|
189
|
|
View Code Duplication |
foreach ($data as $revision) { |
|
|
|
|
|
|
190
|
|
|
if ($revision['length_change'] > 0) { |
|
191
|
|
|
$totalAdded += $revision['length_change']; |
|
192
|
|
|
} else { |
|
193
|
|
|
$totalRemoved += $revision['length_change']; |
|
194
|
|
|
} |
|
195
|
|
|
$revisions[] = $revision; |
|
196
|
|
|
} |
|
197
|
|
|
$this->revisions = $revisions; |
|
198
|
|
|
|
|
199
|
|
|
return $revisions; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get assessments of this page |
|
204
|
|
|
* @return string[]|false `false` if unsupported, or array in the format of: |
|
205
|
|
|
* [ |
|
206
|
|
|
* 'assessment' => 'C', // overall assessment |
|
207
|
|
|
* 'wikiprojects' => [ |
|
208
|
|
|
* 'Biography' => [ |
|
209
|
|
|
* 'assessment' => 'C', |
|
210
|
|
|
* 'badge' => 'url', |
|
211
|
|
|
* ], |
|
212
|
|
|
* ... |
|
213
|
|
|
* ], |
|
214
|
|
|
* 'wikiproject_prefix' => 'Wikipedia:WikiProject_', |
|
215
|
|
|
* ] |
|
216
|
|
|
*/ |
|
217
|
|
|
public function getAssessments() |
|
218
|
|
|
{ |
|
219
|
|
|
$projectDomain = $this->project->getDomain(); |
|
220
|
|
|
$config = $this->project->getRepository()->getAssessmentsConfig($projectDomain); |
|
|
|
|
|
|
221
|
|
|
$data = $this->getRepository()->getAssessments($this->project, [$this->getId()]); |
|
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
// Set the default decorations for the overall quality assessment |
|
224
|
|
|
// This will be replaced with the first valid class defined for any WikiProject |
|
225
|
|
|
$overallQuality = $config['class']['Unknown']; |
|
226
|
|
|
$overallQuality['value'] = '???'; |
|
227
|
|
|
|
|
228
|
|
|
$decoratedAssessments = []; |
|
229
|
|
|
|
|
230
|
|
|
foreach ($data as $assessment) { |
|
231
|
|
|
$classValue = $assessment['class']; |
|
232
|
|
|
|
|
233
|
|
|
// Use ??? as the presented value when the class is unknown or is not defined in the config |
|
234
|
|
|
if ($classValue === 'Unknown' || $classValue === '' || !isset($config['class'][$classValue])) { |
|
235
|
|
|
$classAttrs = $config['class']['Unknown']; |
|
236
|
|
|
$assessment['class']['value'] = '???'; |
|
237
|
|
|
$assessment['class']['category'] = $classAttrs['category']; |
|
238
|
|
|
$assessment['class']['badge'] = "https://upload.wikimedia.org/wikipedia/commons/". $classAttrs['badge']; |
|
239
|
|
|
} else { |
|
240
|
|
|
$classAttrs = $config['class'][$classValue]; |
|
241
|
|
|
$assessment['class'] = [ |
|
242
|
|
|
'value' => $classValue, |
|
243
|
|
|
'color' => $classAttrs['color'], |
|
244
|
|
|
'category' => $classAttrs['category'], |
|
245
|
|
|
]; |
|
246
|
|
|
|
|
247
|
|
|
// add full URL to badge icon |
|
248
|
|
|
if ($classAttrs['badge'] !== '') { |
|
249
|
|
|
$assessment['class']['badge'] = $this->project->getAssessmentBadgeURL($classValue); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
if ($overallQuality['value'] === '???') { |
|
253
|
|
|
$overallQuality = $assessment['class']; |
|
254
|
|
|
$overallQuality['category'] = $classAttrs['category']; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
$importanceValue = $assessment['importance']; |
|
259
|
|
|
$importanceUnknown = $importanceValue === 'Unknown' || $importanceValue === ''; |
|
260
|
|
|
|
|
261
|
|
|
if ($importanceUnknown || !isset($config['importance'][$importanceValue])) { |
|
262
|
|
|
$importanceAttrs = $config['importance']['Unknown']; |
|
263
|
|
|
$assessment['importance'] = $importanceAttrs; |
|
264
|
|
|
$assessment['importance']['value'] = '???'; |
|
265
|
|
|
$assessment['importance']['category'] = $importanceAttrs['category']; |
|
266
|
|
|
} else { |
|
267
|
|
|
$importanceAttrs = $config['importance'][$importanceValue]; |
|
268
|
|
|
$assessment['importance'] = [ |
|
269
|
|
|
'value' => $importanceValue, |
|
270
|
|
|
'color' => $importanceAttrs['color'], |
|
271
|
|
|
'weight' => $importanceAttrs['weight'], // numerical weight for sorting purposes |
|
272
|
|
|
'category' => $importanceAttrs['category'], |
|
273
|
|
|
]; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$decoratedAssessments[$assessment['wikiproject']] = $assessment; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
return [ |
|
280
|
|
|
'assessment' => $overallQuality, |
|
281
|
|
|
'wikiprojects' => $decoratedAssessments, |
|
282
|
|
|
'wikiproject_prefix' => $config['wikiproject_prefix'] |
|
283
|
|
|
]; |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
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.