PageListGetter::getFromPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
	 *
64
	 * @param string $pageName The page name
65
	 * @param string[] $extraParams Any extra parameters to use
66
	 *                 glhprop, glhnamespace, glhshow, glhlimit
67
	 *
68
	 * @return Pages
69
	 */
70 1 View Code Duplication
	public function getFromWhatLinksHere( $pageName, $extraParams = [] ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
	 *
84
	 * @param string $pageName The page name
85
	 * @param string[] $extraParams Any extra parameters to use
86
	 *                 gpltitles, gplnamespace, gpldir, gpllimit
87
	 *
88
	 * @return Pages
89
	 */
90 View Code Duplication
	public function getLinksFromHere( $pageName, $extraParams = [] ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
	 *
121
	 * @param array $extraParams
122
	 *
123
	 * @return Pages
124
	 */
125
	public function getRandom( array $extraParams = [] ) {
126 1
		$params = array_merge( $extraParams, [ 'list' => 'random' ] );
127 1
		return $this->runQuery( $params, null, 'random', 'id', false );
128 1
	}
129
130
	/**
131
	 * Run a query to completion.
132
	 *
133
	 * @param string[] $params Query parameters
134
	 * @param string $contName Result subelement name for continue details
135
	 * @param string $resName Result element name for main results array
136
	 * @param string $pageIdName Result element name for page ID
137
	 * @param bool $cont Whether to continue the query, using multiple requests
138
	 * @return Pages
139
	 */
140
	protected function runQuery( $params, $contName, $resName, $pageIdName = 'pageid', $cont = true ) {
141 9
		$pages = new Pages();
142 9
		$negativeId = -1;
143 9
144
		do {
145
			// Set up continue parameter if it's been set already.
146
			if ( isset( $result['continue'][$contName] ) ) {
147 9
				$params[$contName] = $result['continue'][$contName];
148 4
			}
149 4
150
			// Run the actual query.
151
			$result = $this->api->getRequest( new SimpleRequest( 'query', $params ) );
152 9
			if ( !array_key_exists( 'query', $result ) ) {
153 9
				return $pages;
154 1
			}
155
156
			// Add the results to the output page list.
157
			foreach ( $result['query'][$resName] as $member ) {
158 9
				// Assign negative pageid if page is non-existent.
159
				if ( !array_key_exists( $pageIdName, $member ) ) {
160 9
					$member[$pageIdName] = $negativeId;
161
					$negativeId = $negativeId - 1;
162
				}
163
164
				$pageTitle = new Title( $member['title'], $member['ns'] );
165 9
				$page = new Page( new PageIdentifier( $pageTitle, $member[$pageIdName] ) );
166 9
				$pages->addPage( $page );
167 9
			}
168 9
169
		} while ( $cont && isset( $result['continue'] ) );
170 9
171
		return $pages;
172 9
	}
173
}
174