Completed
Push — master ( 97b5b5...7e7f32 )
by Sam
10s
created

PageListGetter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 81.48%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 159
ccs 44
cts 54
cp 0.8148
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getLinksFromHere() 0 8 1
A getPageListFromCategoryName() 0 7 1
A getPageListFromPageTransclusions() 0 7 1
A getFromWhatLinksHere() 0 8 1
A getFromPrefix() 0 7 1
A getRandom() 0 4 1
B runQuery() 0 33 7
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 5
	public function getPageListFromCategoryName( $name, array $extraParams = [] ) {
32 5
		$params = array_merge( $extraParams, [
33 5
			'list' => 'categorymembers',
34 5
			'cmtitle' => $name,
35 5
		] );
36 5
		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 1
	public function getPageListFromPageTransclusions( $pageName, array $extraParams = [] ) {
51 1
		$params = array_merge( $extraParams, [
52 1
			'list' => 'embeddedin',
53 1
			'eititle' => $pageName,
54 1
		] );
55 1
		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 1
	public function getFromWhatLinksHere( $pageName, $extraParams = [] ) {
71 1
		$params = array_merge( $extraParams, [
72 1
			'prop' => 'info',
73 1
			'generator' => 'linkshere',
74 1
			'titles' => $pageName,
75 1
		] );
76 1
		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 1
	public function getFromPrefix( $prefix ) {
109
		$params = [
110 1
			'list' => 'allpages',
111 1
			'apprefix' => $prefix,
112 1
		];
113 1
		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 1
	public function getRandom( array $extraParams = [] ) {
127 1
		$params = array_merge( $extraParams, [ 'list' => 'random' ] );
128 1
		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 9
	protected function runQuery( $params, $contName, $resName, $pageIdName = 'pageid', $cont = true ) {
142 9
		$pages = new Pages();
143 9
		$negativeId = -1;
144
145
		do {
146
			// Set up continue parameter if it's been set already.
147 9
			if ( isset( $result['continue'][$contName] ) ) {
148 4
				$params[$contName] = $result['continue'][$contName];
149 4
			}
150
151
			// Run the actual query.
152 9
			$result = $this->api->getRequest( new SimpleRequest( 'query', $params ) );
153 9
			if ( !array_key_exists( 'query', $result ) ) {
154 1
				return $pages;
155
			}
156
157
			// Add the results to the output page list.
158 9
			foreach ( $result['query'][$resName] as $member ) {
159
				// Assign negative pageid if page is non-existent.
160 9
				if ( !array_key_exists( $pageIdName, $member ) ) {
161
					$member[$pageIdName] = $negativeId;
162
					$negativeId = $negativeId - 1;
163
				}
164
165 9
				$pageTitle = new Title( $member['title'], $member['ns'] );
166 9
				$page = new Page( new PageIdentifier( $pageTitle, $member[$pageIdName] ) );
167 9
				$pages->addPage( $page );
168 9
			}
169
170 9
		} while ( $cont && isset( $result['continue'] ) );
171
172 9
		return $pages;
173
	}
174
}
175