Completed
Push — master ( ddde46...ec2f81 )
by adam
12:43 queued 09:38
created

PageListGetter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 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 26 6
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 9
	public function __construct( MediawikiApi $api ) {
28 9
		$this->api = $api;
29 9
	}
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 5
	public function getPageListFromCategoryName( $name, array $extraParams = [] ) {
45 5
		$params = array_merge( $extraParams, [
46 5
			'list' => 'categorymembers',
47 5
			'cmtitle' => $name,
48 5
		] );
49 5
		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 1
	public function getPageListFromPageTransclusions( $pageName, array $extraParams = [] ) {
64 1
		$params = array_merge( $extraParams, [
65 1
			'list' => 'embeddedin',
66 1
			'eititle' => $pageName,
67 1
		] );
68 1
		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 1
	public function getFromWhatLinksHere( $pageName, $extraParams = [] ) {
84 1
		$params = array_merge( $extraParams, [
85 1
			'prop' => 'info',
86 1
			'generator' => 'linkshere',
87 1
			'titles' => $pageName,
88 1
		] );
89 1
		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 1
	public function getFromPrefix( $prefix ) {
102
		$params = [
103 1
			'list' => 'allpages',
104 1
			'apprefix' => $prefix,
105 1
		];
106 1
		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 1
	public function getRandom( array $extraParams = [] ) {
120 1
		$params = array_merge( $extraParams, [ 'list' => 'random' ] );
121 1
		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 9
	protected function runQuery( $params, $contName, $resName, $pageIdName = 'pageid', $cont = true ) {
135 9
		$pages = new Pages();
136
137
		do {
138
			// Set up continue parameter if it's been set already.
139 9
			if ( isset( $result['continue'][$contName] ) ) {
140 4
				$params[$contName] = $result['continue'][$contName];
141 4
			}
142
143
			// Run the actual query.
144 9
			$result = $this->api->getRequest( new SimpleRequest( 'query', $params ) );
145 9
			if ( !array_key_exists( 'query', $result ) ) {
146 1
				return $pages;
147
			}
148
149
			// Add the results to the output page list.
150 9
			foreach ( $result['query'][$resName] as $member ) {
151 9
				$pageTitle = new Title( $member['title'], $member['ns'] );
152 9
				$page = new Page( new PageIdentifier( $pageTitle, $member[$pageIdName] ) );
153 9
				$pages->addPage( $page );
154 9
			}
155
156 9
		} while ( $cont && isset( $result['continue'] ) );
157
158 9
		return $pages;
159
	}
160
}
161