Completed
Pull Request — master (#46)
by Sam
02:57
created

PageListGetter::getPageListFromPageTransclusions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 4
b 1
f 0
cc 1
eloc 5
nc 1
nop 2
crap 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\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 9
	public function __construct( MediawikiApi $api ) {
29 9
		$this->api = $api;
30 9
	}
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
	 * @return Pages
44
	 */
45 5
	public function getPageListFromCategoryName( $name, array $extraParams = [] ) {
46 5
		$params = array_merge( $extraParams, [
47 5
			'list' => 'categorymembers',
48 5
			'cmtitle' => $name,
49
		] );
50 5
		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 1
	public function getPageListFromPageTransclusions( $pageName, array $extraParams = [] ) {
65 1
		$params = array_merge( $extraParams, [
66 1
			'list' => 'embeddedin',
67 1
			'eititle' => $pageName,
68
		] );
69 1
		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
	 * @param string[] Any extra parameters to use: lhprop, lhnamespace, lhshow, lhlimit
81
	 *
82
	 * @return Pages
83
	 */
84 1
	public function getFromWhatLinksHere( $pageName, $extraParams = [] ) {
85 1
		$params = array_merge( $extraParams, [
86 1
			'prop' => 'info',
87 1
			'generator' => 'linkshere',
88 1
			'titles' => $pageName,
89
		] );
90 1
		return $this->runQuery( $params, 'glhcontinue', 'pages' );
91
	}
92
93
	/**
94
	 * Get all pages that have the given prefix.
95
	 *
96
	 * @link https://www.mediawiki.org/wiki/API:Allpages
97
	 *
98
	 * @param string $prefix The page title prefix.
99
	 *
100
	 * @return Pages
101
	 */
102 1
	public function getFromPrefix( $prefix ) {
103
		$params = [
104 1
			'list' => 'allpages',
105 1
		    'apprefix' => $prefix,
106
		];
107 1
		return $this->runQuery( $params, 'apcontinue', 'allpages' );
108
	}
109
110
	/**
111
	 * Get up to 10 random pages.
112
	 *
113
	 * @link https://www.mediawiki.org/wiki/API:Random
114
	 * @uses PageListGetter::runQuery()
115
	 *
116
	 * @param array $extraParams
117
	 *
118
	 * @return Pages
119
	 */
120 1
	public function getRandom( array $extraParams = [] ) {
121 1
		$params = array_merge( $extraParams, [ 'list' => 'random' ] );
122 1
		return $this->runQuery( $params, null, 'random', 'id', false );
123
	}
124
125
	/**
126
	 * Run a query to completion.
127
	 *
128
	 * @param string[] $params Query parameters
129
	 * @param string $contName Result subelement name for continue details
130
	 * @param string $resName Result element name for main results array
131
	 * @param string $pageIdName Result element name for page ID
132
	 * @param boolean $cont Whether to continue the query, using multiple requests
133
	 * @return Pages
134
	 */
135 9
	protected function runQuery( $params, $contName, $resName, $pageIdName = 'pageid', $cont = true ) {
136 9
		$pages = new Pages();
137
138
		do {
139
			// Set up continue parameter if it's been set already.
140 9
			if ( isset( $result['continue'][$contName] ) ) {
141 4
				$params[$contName] = $result['continue'][$contName];
142
			}
143
144
			// Run the actual query.
145 9
			$result = $this->api->getRequest( new SimpleRequest( 'query', $params ) );
146 9
			if ( !array_key_exists( 'query', $result ) ) {
147 1
				return $pages;
148
			}
149
150
			// Add the results to the output page list.
151 9
			foreach ( $result['query'][$resName] as $member ) {
152 9
				$pageTitle = new Title( $member['title'], $member['ns'] );
153 9
				$page = new Page( new PageIdentifier( $pageTitle, $member[$pageIdName] ) );
154 9
				$pages->addPage( $page );
155
			}
156
157 9
		} while ( $cont && isset( $result['continue'] ) );
158
159 9
		return $pages;
160
	}
161
}
162