Completed
Push — master ( 59d864...868015 )
by adam
12s
created

PageListGetter::getFromPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 2
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
	 * @return Pages
44
	 */
45
	public function getPageListFromCategoryName( $name, array $extraParams = [] ) {
46
		$params = array_merge( $extraParams, [
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 = [] ) {
65
		$params = array_merge( $extraParams, [
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
	 * @return Pages
82
	 */
83
	public function getFromWhatLinksHere( $pageName ) {
84
		$params = [
85
			'prop' => 'info',
86
			'generator' => 'linkshere',
87
			'titles' => $pageName,
88
		];
89
		return $this->runQuery( $params, 'lhcontinue', '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 boolean $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