1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediawiki\Api\Service; |
4
|
|
|
|
5
|
|
|
use Mediawiki\Api\SimpleRequest; |
6
|
|
|
use Mediawiki\DataModel\Page; |
7
|
|
|
use Mediawiki\DataModel\PageIdentifier; |
8
|
|
|
use Mediawiki\DataModel\Pages; |
9
|
|
|
use Mediawiki\DataModel\Title; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @access private |
13
|
|
|
* |
14
|
|
|
* @author Addshore |
15
|
|
|
*/ |
16
|
|
|
class PageListGetter extends Service { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get the set of pages in a given category. Extra parameters can include: |
20
|
|
|
* cmtype: default 'page|subcat|file' |
21
|
|
|
* cmlimit: default 10, maximum 500 (5000 for bots) |
22
|
|
|
* |
23
|
|
|
* @link https://www.mediawiki.org/wiki/API:Categorymembers |
24
|
|
|
* @since 0.3 |
25
|
|
|
* |
26
|
|
|
* @param string $name |
27
|
|
|
* @param array $extraParams |
28
|
|
|
* |
29
|
|
|
* @return Pages |
30
|
|
|
*/ |
31
|
|
|
public function getPageListFromCategoryName( $name, array $extraParams = [] ) { |
32
|
|
|
$params = array_merge( $extraParams, [ |
33
|
|
|
'list' => 'categorymembers', |
34
|
|
|
'cmtitle' => $name, |
35
|
|
|
] ); |
36
|
|
|
return $this->runQuery( $params, 'cmcontinue', 'categorymembers' ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* List pages that transclude a certain page. |
41
|
|
|
* |
42
|
|
|
* @link https://www.mediawiki.org/wiki/API:Embeddedin |
43
|
|
|
* @since 0.5 |
44
|
|
|
* |
45
|
|
|
* @param string $pageName |
46
|
|
|
* @param array $extraParams |
47
|
|
|
* |
48
|
|
|
* @return Pages |
49
|
|
|
*/ |
50
|
|
|
public function getPageListFromPageTransclusions( $pageName, array $extraParams = [] ) { |
51
|
|
|
$params = array_merge( $extraParams, [ |
52
|
|
|
'list' => 'embeddedin', |
53
|
|
|
'eititle' => $pageName, |
54
|
|
|
] ); |
55
|
|
|
return $this->runQuery( $params, 'eicontinue', 'embeddedin' ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get all pages that link to the given page. |
60
|
|
|
* |
61
|
|
|
* @link https://www.mediawiki.org/wiki/API:Linkshere |
62
|
|
|
* @since 0.5 |
63
|
|
|
* @uses PageListGetter::runQuery() |
64
|
|
|
* |
65
|
|
|
* @param string $pageName The page name |
66
|
|
|
* @param string[] Any extra parameters to use: glhprop, glhnamespace, glhshow, glhlimit |
67
|
|
|
* |
68
|
|
|
* @return Pages |
69
|
|
|
*/ |
70
|
|
|
public function getFromWhatLinksHere( $pageName, $extraParams = [] ) { |
71
|
|
|
$params = array_merge( $extraParams, [ |
72
|
|
|
'prop' => 'info', |
73
|
|
|
'generator' => 'linkshere', |
74
|
|
|
'titles' => $pageName, |
75
|
|
|
] ); |
76
|
|
|
return $this->runQuery( $params, 'glhcontinue', 'pages' ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get all pages that are linked to from the given page. |
81
|
|
|
* |
82
|
|
|
* @link https://www.mediawiki.org/wiki/API:Links |
83
|
|
|
* @uses PageListGetter::runQuery() |
84
|
|
|
* |
85
|
|
|
* @param string $pageName The page name |
86
|
|
|
* @param string[] Any extra parameters to use: gpltitles, gplnamespace, gpldir, gpllimit |
87
|
|
|
* |
88
|
|
|
* @return Pages |
89
|
|
|
*/ |
90
|
|
|
public function getLinksFromHere( $pageName, $extraParams = [] ) { |
91
|
|
|
$params = array_merge( $extraParams, [ |
92
|
|
|
'prop' => 'info', |
93
|
|
|
'generator' => 'links', |
94
|
|
|
'titles' => $pageName, |
95
|
|
|
] ); |
96
|
|
|
return $this->runQuery( $params, 'gplcontinue', 'pages' ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get all pages that have the given prefix. |
101
|
|
|
* |
102
|
|
|
* @link https://www.mediawiki.org/wiki/API:Allpages |
103
|
|
|
* |
104
|
|
|
* @param string $prefix The page title prefix. |
105
|
|
|
* |
106
|
|
|
* @return Pages |
107
|
|
|
*/ |
108
|
|
|
public function getFromPrefix( $prefix ) { |
109
|
|
|
$params = [ |
110
|
|
|
'list' => 'allpages', |
111
|
|
|
'apprefix' => $prefix, |
112
|
|
|
]; |
113
|
|
|
return $this->runQuery( $params, 'apcontinue', 'allpages' ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get up to 10 random pages. |
118
|
|
|
* |
119
|
|
|
* @link https://www.mediawiki.org/wiki/API:Random |
120
|
|
|
* @uses PageListGetter::runQuery() |
121
|
|
|
* |
122
|
|
|
* @param array $extraParams |
123
|
|
|
* |
124
|
|
|
* @return Pages |
125
|
|
|
*/ |
126
|
|
|
public function getRandom( array $extraParams = [] ) { |
127
|
|
|
$params = array_merge( $extraParams, [ 'list' => 'random' ] ); |
128
|
|
|
return $this->runQuery( $params, null, 'random', 'id', false ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Run a query to completion. |
133
|
|
|
* |
134
|
|
|
* @param string[] $params Query parameters |
135
|
|
|
* @param string $contName Result subelement name for continue details |
136
|
|
|
* @param string $resName Result element name for main results array |
137
|
|
|
* @param string $pageIdName Result element name for page ID |
138
|
|
|
* @param bool $cont Whether to continue the query, using multiple requests |
139
|
|
|
* @return Pages |
140
|
|
|
*/ |
141
|
|
|
protected function runQuery( $params, $contName, $resName, $pageIdName = 'pageid', $cont = true ) { |
142
|
|
|
$pages = new Pages(); |
143
|
|
|
$negativeId = -1; |
144
|
|
|
|
145
|
|
|
do { |
146
|
|
|
// Set up continue parameter if it's been set already. |
147
|
|
|
if ( isset( $result['continue'][$contName] ) ) { |
148
|
|
|
$params[$contName] = $result['continue'][$contName]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Run the actual query. |
152
|
|
|
$result = $this->api->getRequest( new SimpleRequest( 'query', $params ) ); |
153
|
|
|
if ( !array_key_exists( 'query', $result ) ) { |
154
|
|
|
return $pages; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// Add the results to the output page list. |
158
|
|
|
foreach ( $result['query'][$resName] as $member ) { |
159
|
|
|
// Assign negative pageid if page is non-existent. |
160
|
|
|
if ( !array_key_exists( $pageIdName, $member ) ) { |
161
|
|
|
$member[$pageIdName] = $negativeId; |
162
|
|
|
$negativeId = $negativeId - 1; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$pageTitle = new Title( $member['title'], $member['ns'] ); |
166
|
|
|
$page = new Page( new PageIdentifier( $pageTitle, $member[$pageIdName] ) ); |
167
|
|
|
$pages->addPage( $page ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} while ( $cont && isset( $result['continue'] ) ); |
171
|
|
|
|
172
|
|
|
return $pages; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|