summaryrefslogtreecommitdiff
path: root/bin/wiki/vendor/addwiki/mediawiki-api/src/Service/PageListGetter.php
blob: 6b6d00079515d7adc4604c2d258ca5f18b9d2a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

namespace Mediawiki\Api\Service;

use Mediawiki\Api\SimpleRequest;
use Mediawiki\DataModel\Page;
use Mediawiki\DataModel\PageIdentifier;
use Mediawiki\DataModel\Pages;
use Mediawiki\DataModel\Title;

/**
 * @access private
 *
 * @author Addshore
 */
class PageListGetter extends Service {

	/**
	 * Get the set of pages in a given category. Extra parameters can include:
	 *     cmtype: default 'page|subcat|file'
	 *     cmlimit: default 10, maximum 500 (5000 for bots)
	 *
	 * @link https://www.mediawiki.org/wiki/API:Categorymembers
	 * @since 0.3
	 *
	 * @param string $name
	 * @param array $extraParams
	 *
	 * @return Pages
	 */
	public function getPageListFromCategoryName( $name, array $extraParams = [] ) {
		$params = array_merge( $extraParams, [
			'list' => 'categorymembers',
			'cmtitle' => $name,
		] );
		return $this->runQuery( $params, 'cmcontinue', 'categorymembers' );
	}

	/**
	 * List pages that transclude a certain page.
	 *
	 * @link https://www.mediawiki.org/wiki/API:Embeddedin
	 * @since 0.5
	 *
	 * @param string $pageName
	 * @param array $extraParams
	 *
	 * @return Pages
	 */
	public function getPageListFromPageTransclusions( $pageName, array $extraParams = [] ) {
		$params = array_merge( $extraParams, [
			'list' => 'embeddedin',
			'eititle' => $pageName,
		] );
		return $this->runQuery( $params, 'eicontinue', 'embeddedin' );
	}

	/**
	 * Get all pages that link to the given page.
	 *
	 * @link https://www.mediawiki.org/wiki/API:Linkshere
	 * @since 0.5
	 * @uses PageListGetter::runQuery()
	 *
	 * @param string $pageName The page name
	 * @param string[] Any extra parameters to use: lhprop, lhnamespace, lhshow, lhlimit
	 *
	 * @return Pages
	 */
	public function getFromWhatLinksHere( $pageName, $extraParams = [] ) {
		$params = array_merge( $extraParams, [
			'prop' => 'info',
			'generator' => 'linkshere',
			'titles' => $pageName,
		] );
		return $this->runQuery( $params, 'glhcontinue', 'pages' );
	}

	/**
	 * Get all pages that have the given prefix.
	 *
	 * @link https://www.mediawiki.org/wiki/API:Allpages
	 *
	 * @param string $prefix The page title prefix.
	 *
	 * @return Pages
	 */
	public function getFromPrefix( $prefix ) {
		$params = [
			'list' => 'allpages',
			'apprefix' => $prefix,
		];
		return $this->runQuery( $params, 'apcontinue', 'allpages' );
	}

	/**
	 * Get up to 10 random pages.
	 *
	 * @link https://www.mediawiki.org/wiki/API:Random
	 * @uses PageListGetter::runQuery()
	 *
	 * @param array $extraParams
	 *
	 * @return Pages
	 */
	public function getRandom( array $extraParams = [] ) {
		$params = array_merge( $extraParams, [ 'list' => 'random' ] );
		return $this->runQuery( $params, null, 'random', 'id', false );
	}

	/**
	 * Run a query to completion.
	 *
	 * @param string[] $params Query parameters
	 * @param string $contName Result subelement name for continue details
	 * @param string $resName Result element name for main results array
	 * @param string $pageIdName Result element name for page ID
	 * @param bool $cont Whether to continue the query, using multiple requests
	 * @return Pages
	 */
	protected function runQuery( $params, $contName, $resName, $pageIdName = 'pageid', $cont = true ) {
		$pages = new Pages();

		do {
			// Set up continue parameter if it's been set already.
			if ( isset( $result['continue'][$contName] ) ) {
				$params[$contName] = $result['continue'][$contName];
			}

			// Run the actual query.
			$result = $this->api->getRequest( new SimpleRequest( 'query', $params ) );
			if ( !array_key_exists( 'query', $result ) ) {
				return $pages;
			}

			// Add the results to the output page list.
			foreach ( $result['query'][$resName] as $member ) {
				$pageTitle = new Title( $member['title'], $member['ns'] );
				$page = new Page( new PageIdentifier( $pageTitle, $member[$pageIdName] ) );
				$pages->addPage( $page );
			}

		} while ( $cont && isset( $result['continue'] ) );

		return $pages;
	}
}