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