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: lhprop, lhnamespace, lhshow, lhlimit |
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 have the given prefix. |
81
|
|
|
* |
82
|
|
|
* @link https://www.mediawiki.org/wiki/API:Allpages |
83
|
|
|
* |
84
|
|
|
* @param string $prefix The page title prefix. |
85
|
|
|
* |
86
|
|
|
* @return Pages |
87
|
|
|
*/ |
88
|
|
|
public function getFromPrefix( $prefix ) { |
89
|
|
|
$params = [ |
90
|
|
|
'list' => 'allpages', |
91
|
|
|
'apprefix' => $prefix, |
92
|
|
|
]; |
93
|
|
|
return $this->runQuery( $params, 'apcontinue', 'allpages' ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get up to 10 random pages. |
98
|
|
|
* |
99
|
|
|
* @link https://www.mediawiki.org/wiki/API:Random |
100
|
|
|
* @uses PageListGetter::runQuery() |
101
|
|
|
* |
102
|
|
|
* @param array $extraParams |
103
|
|
|
* |
104
|
|
|
* @return Pages |
105
|
|
|
*/ |
106
|
|
|
public function getRandom( array $extraParams = [] ) { |
107
|
|
|
$params = array_merge( $extraParams, [ 'list' => 'random' ] ); |
108
|
|
|
return $this->runQuery( $params, null, 'random', 'id', false ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Run a query to completion. |
113
|
|
|
* |
114
|
|
|
* @param string[] $params Query parameters |
115
|
|
|
* @param string $contName Result subelement name for continue details |
116
|
|
|
* @param string $resName Result element name for main results array |
117
|
|
|
* @param string $pageIdName Result element name for page ID |
118
|
|
|
* @param bool $cont Whether to continue the query, using multiple requests |
119
|
|
|
* @return Pages |
120
|
|
|
*/ |
121
|
|
|
protected function runQuery( $params, $contName, $resName, $pageIdName = 'pageid', $cont = true ) { |
122
|
|
|
$pages = new Pages(); |
123
|
|
|
|
124
|
|
|
do { |
125
|
|
|
// Set up continue parameter if it's been set already. |
126
|
|
|
if ( isset( $result['continue'][$contName] ) ) { |
127
|
|
|
$params[$contName] = $result['continue'][$contName]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Run the actual query. |
131
|
|
|
$result = $this->api->getRequest( new SimpleRequest( 'query', $params ) ); |
132
|
|
|
if ( !array_key_exists( 'query', $result ) ) { |
133
|
|
|
return $pages; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// Add the results to the output page list. |
137
|
|
|
foreach ( $result['query'][$resName] as $member ) { |
138
|
|
|
$pageTitle = new Title( $member['title'], $member['ns'] ); |
139
|
|
|
$page = new Page( new PageIdentifier( $pageTitle, $member[$pageIdName] ) ); |
140
|
|
|
$pages->addPage( $page ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} while ( $cont && isset( $result['continue'] ) ); |
144
|
|
|
|
145
|
|
|
return $pages; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|