Completed
Push — master ( ec2f81...11966d )
by adam
03:28
created

PageListGetter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 12
c 7
b 1
f 0
lcom 1
cbo 6
dl 0
loc 144
ccs 0
cts 45
cp 0
rs 10

7 Methods

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