|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the PagesRepository class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Xtools; |
|
7
|
|
|
|
|
8
|
|
|
use Mediawiki\Api\SimpleRequest; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* A PagesRepository fetches data about Pages, either singularly or for multiple. |
|
12
|
|
|
*/ |
|
13
|
|
|
class PagesRepository extends Repository |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Get metadata about a single page from the API. |
|
18
|
|
|
* @param Project $project The project to which the page belongs. |
|
19
|
|
|
* @param string $pageTitle Page title. |
|
20
|
|
|
* @return string[] Array with some of the following keys: pageid, title, missing, displaytitle, |
|
21
|
|
|
* url. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getPageInfo(Project $project, $pageTitle) |
|
24
|
|
|
{ |
|
25
|
|
|
$info = $this->getPagesInfo($project, [$pageTitle]); |
|
26
|
|
|
return array_shift($info); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get metadata about a set of pages from the API. |
|
31
|
|
|
* @param Project $project The project to which the pages belong. |
|
32
|
|
|
* @param string[] $pageTitles Array of page titles. |
|
33
|
|
|
* @return string[] Array keyed by the page names, each element with some of the |
|
34
|
|
|
* following keys: pageid, title, missing, displaytitle, url. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getPagesInfo(Project $project, $pageTitles) |
|
37
|
|
|
{ |
|
38
|
|
|
// @TODO: Also include 'extlinks' prop when we start checking for dead external links. |
|
39
|
|
|
$params = [ |
|
40
|
|
|
'prop' => 'info|pageprops', |
|
41
|
|
|
'inprop' => 'protection|talkid|watched|watchers|notificationtimestamp|subjectid|url|readable|displaytitle', |
|
42
|
|
|
'converttitles' => '', |
|
43
|
|
|
// 'ellimit' => 20, |
|
|
|
|
|
|
44
|
|
|
// 'elexpandurl' => '', |
|
|
|
|
|
|
45
|
|
|
'titles' => join('|', $pageTitles), |
|
46
|
|
|
'formatversion' => 2 |
|
47
|
|
|
// 'pageids' => $pageIds // FIXME: allow page IDs |
|
|
|
|
|
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
$query = new SimpleRequest('query', $params); |
|
51
|
|
|
$api = $this->getMediawikiApi($project); |
|
52
|
|
|
$res = $api->getRequest($query); |
|
53
|
|
|
$result = []; |
|
54
|
|
|
if (isset($res['query']['pages'])) { |
|
55
|
|
|
foreach ($res['query']['pages'] as $pageInfo) { |
|
56
|
|
|
$result[$pageInfo['title']] = $pageInfo; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
return $result; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get revisions of a single page. |
|
64
|
|
|
* @param Page $page The page. |
|
65
|
|
|
* @param User|null $user Specify to get only revisions by the given user. |
|
66
|
|
|
* @return string[] Each member with keys: id, timestamp, length- |
|
67
|
|
|
*/ |
|
68
|
|
View Code Duplication |
public function getRevisions(Page $page, User $user = null) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$revTable = $this->getTableName($page->getProject()->getDatabaseName(), 'revision'); |
|
71
|
|
|
$userClause = $user ? "revs.rev_user_text in (:username) AND " : ""; |
|
72
|
|
|
|
|
73
|
|
|
$query = "SELECT |
|
74
|
|
|
revs.rev_id AS id, |
|
75
|
|
|
revs.rev_timestamp AS timestamp, |
|
76
|
|
|
revs.rev_minor_edit AS minor, |
|
77
|
|
|
revs.rev_len AS length, |
|
78
|
|
|
(CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS length_change, |
|
79
|
|
|
revs.rev_user AS user_id, |
|
80
|
|
|
revs.rev_user_text AS username, |
|
81
|
|
|
revs.rev_comment AS comment |
|
82
|
|
|
FROM $revTable AS revs |
|
83
|
|
|
LEFT JOIN $revTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id) |
|
84
|
|
|
WHERE $userClause revs.rev_page = :pageid |
|
85
|
|
|
ORDER BY revs.rev_timestamp ASC |
|
86
|
|
|
"; |
|
87
|
|
|
$params = ['pageid' => $page->getId()]; |
|
88
|
|
|
if ($user) { |
|
89
|
|
|
$params['username'] = $user->getUsername(); |
|
90
|
|
|
} |
|
91
|
|
|
$conn = $this->getProjectsConnection(); |
|
92
|
|
|
return $conn->executeQuery($query, $params)->fetchAll(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get a count of the number of revisions of a single page |
|
97
|
|
|
* @param Page $page The page. |
|
98
|
|
|
* @param User|null $user Specify to only count revisions by the given user. |
|
99
|
|
|
* @return int |
|
100
|
|
|
*/ |
|
101
|
|
View Code Duplication |
public function getNumRevisions(Page $page, User $user = null) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
$revTable = $this->getTableName($page->getProject()->getDatabaseName(), 'revision'); |
|
104
|
|
|
$userClause = $user ? "rev_user_text in (:username) AND " : ""; |
|
105
|
|
|
|
|
106
|
|
|
$query = "SELECT COUNT(*) |
|
107
|
|
|
FROM $revTable |
|
108
|
|
|
WHERE $userClause rev_page = :pageid |
|
109
|
|
|
"; |
|
110
|
|
|
$params = ['pageid' => $page->getId()]; |
|
111
|
|
|
if ($user) { |
|
112
|
|
|
$params['username'] = $user->getUsername(); |
|
113
|
|
|
} |
|
114
|
|
|
$conn = $this->getProjectsConnection(); |
|
115
|
|
|
return $conn->executeQuery($query, $params)->fetchColumn(0); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get assessment data for the given pages |
|
120
|
|
|
* @param Project $project The project to which the pages belong. |
|
121
|
|
|
* @param int[] $pageIds Page IDs |
|
122
|
|
|
* @return string[] Assessment data as retrieved from the database. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getAssessments(Project $project, $pageIds) |
|
125
|
|
|
{ |
|
126
|
|
|
if (!$project->hasPageAssessments()) { |
|
127
|
|
|
return []; |
|
128
|
|
|
} |
|
129
|
|
|
$pageAssessmentsTable = $this->getTableName($project->getDatabaseName(), 'page_assessments'); |
|
|
|
|
|
|
130
|
|
|
$pageIds = implode($pageIds, ','); |
|
131
|
|
|
|
|
132
|
|
|
$query = "SELECT pap_project_title AS wikiproject, pa_class AS class, pa_importance AS importance |
|
133
|
|
|
FROM page_assessments |
|
134
|
|
|
LEFT JOIN page_assessments_projects ON pa_project_id = pap_project_id |
|
135
|
|
|
WHERE pa_page_id IN ($pageIds)"; |
|
136
|
|
|
|
|
137
|
|
|
$conn = $this->getProjectsConnection(); |
|
138
|
|
|
return $conn->executeQuery($query)->fetchAll(); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.