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

PageGetter::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 6
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\Content;
8
use Mediawiki\DataModel\EditInfo;
9
use Mediawiki\DataModel\Page;
10
use Mediawiki\DataModel\PageIdentifier;
11
use Mediawiki\DataModel\Revision;
12
use Mediawiki\DataModel\Revisions;
13
use Mediawiki\DataModel\Title;
14
use RuntimeException;
15
16
/**
17
 * @access private
18
 *
19
 * @author Addshore
20
 */
21
class PageGetter {
22
23
	/**
24
	 * @var MediawikiApi
25
	 */
26
	private $api;
27
28
	/**
29
	 * @param MediawikiApi $api
30
	 */
31 6
	public function __construct( MediawikiApi $api ) {
32 6
		$this->api = $api;
33 6
	}
34
35
	/**
36
	 * @since 0.2
37
	 *
38
	 * @param int $id
39
	 * @param array $extraParams
40
	 *
41
	 * @return Page
42
	 */
43
	public function getFromRevisionId( $id, array $extraParams = [] ) {
44
		$result =
45
			$this->api->getRequest(
46
				new SimpleRequest(
47
					'query',
48
					$this->getQuery( [ 'revids' => $id ], $extraParams )
49
				)
50
			);
51
52
		return $this->newPageFromResult( array_shift( $result['query']['pages'] ) );
53
	}
54
55
	/**
56
	 * @since 0.2
57
	 *
58
	 * @param string|Title $title
59
	 * @param array $extraParams
60
	 *
61
	 * @return Page
62
	 */
63 5
	public function getFromTitle( $title, array $extraParams = [] ) {
64 5
		if ( $title instanceof Title ) {
65 5
			$title = $title->getTitle();
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Title::getTitle() has been deprecated with message: in 0.6 use getText (makes things look cleaner)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
66 5
		}
67
		$result =
68 5
			$this->api->getRequest(
69 5
				new SimpleRequest(
70 5
					'query',
71 5
					$this->getQuery( [ 'titles' => $title ], $extraParams )
72 5
				)
73 5
			);
74
75 5
		return $this->newPageFromResult( array_shift( $result['query']['pages'] ) );
76
	}
77
78
	/**
79
	 * @since 0.2
80
	 *
81
	 * @param int $id
82
	 * @param array $extraParams
83
	 *
84
	 * @return Page
85
	 */
86 1
	public function getFromPageId( $id, array $extraParams = [] ) {
87
		$result =
88 1
			$this->api->getRequest(
89 1
				new SimpleRequest(
90 1
					'query',
91 1
					$this->getQuery( [ 'pageids' => $id ], $extraParams )
92 1
				)
93 1
			);
94
95 1
		return $this->newPageFromResult( array_shift( $result['query']['pages'] ) );
96
	}
97
98
	/**
99
	 * @since 0.4
100
	 *
101
	 * @param PageIdentifier $pageIdentifier
102
	 * @param array $extraParams
103
	 *
104
	 * @throws RuntimeException
105
	 * @return Page
106
	 */
107 5
	public function getFromPageIdentifier(
108
		PageIdentifier $pageIdentifier,
109
		array $extraParams = []
110
	) {
111 5
		if ( !$pageIdentifier->identifiesPage() ) {
112
			throw new RuntimeException( '$pageIdentifier does not identify a page' );
113
		}
114 5
		if ( !is_null( $pageIdentifier->getId() ) ) {
115
			return $this->getFromPageId( $pageIdentifier->getId(), $extraParams );
116
		} else {
117 5
			return $this->getFromTitle( $pageIdentifier->getTitle(), $extraParams );
0 ignored issues
show
Bug introduced by
It seems like $pageIdentifier->getTitle() can be null; however, getFromTitle() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
118
		}
119
	}
120
121
	/**
122
	 * @since 0.2
123
	 *
124
	 * @param Page $page
125
	 * @param array $extraParams
126
	 *
127
	 * @return Page
128
	 */
129
	public function getFromPage( Page $page, array $extraParams = [] ) {
130
		$result =
131
			$this->api->getRequest(
132
				new SimpleRequest(
133
					'query',
134
					$this->getQuery( [ 'pageids' => $page->getId() ], $extraParams )
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getId() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
135
				)
136
			);
137
		$revisions = $this->getRevisionsFromResult( array_shift( $result['query']['pages'] ) );
138
		$revisions->addRevisions( $page->getRevisions() );
139
140
		return new Page(
141
			$page->getPageIdentifier(),
142
			$revisions
143
		);
144
	}
145
146
	/**
147
	 * @since 0.2
148
	 *
149
	 * @param Revision $revision
150
	 * @param array $extraParams
151
	 *
152
	 * @return Page
153
	 */
154
	public function getFromRevision( Revision $revision, array $extraParams = [] ) {
155
		$result =
156
			$this->api->getRequest(
157
				new SimpleRequest(
158
					'query',
159
					$this->getQuery( [ 'revids' => $revision->getId() ], $extraParams )
160
				)
161
			);
162
		$revisions = $this->getRevisionsFromResult( array_shift( $result['query']['pages'] ) );
163
		$revisions->addRevision( $revision );
164
165
		return new Page(
166
			new PageIdentifier(
167
				new Title(
168
					$result['title'],
169
					$result['ns']
170
				),
171
				$result['pageid']
172
			),
173
			$revisions
174
		);
175
	}
176
177
	/**
178
	 * @param array $additionalParams
179
	 *
180
	 * @param array $extraParams
181
	 *
182
	 * @return array
183
	 */
184 6
	private function getQuery( $additionalParams, array $extraParams = [] ) {
185
		$base = [
186 6
			'prop' => 'revisions|info|pageprops',
187 6
			'rvprop' => 'ids|flags|timestamp|user|size|sha1|comment|content|tags',
188 6
			'inprop' => 'protection',
189 6
		];
190
191 6
		return array_merge( $extraParams, $base, $additionalParams );
192
	}
193
194
	/**
195
	 * @param array $array
196
	 *
197
	 * @return Revisions
198
	 */
199 6
	private function getRevisionsFromResult( $array ) {
200 6
		$revisions = new Revisions();
201 6
		$pageid = $array['pageid'];
202 6
		foreach ( $array['revisions'] as $revision ) {
203 6
			$revisions->addRevision(
204 6
				new Revision(
205 6
					$this->getContent( $array['contentmodel'], $revision['*'] ),
206 6
					new PageIdentifier( new Title( $array['title'], $array['ns'] ), $pageid ),
207 6
					$revision['revid'],
208 6
					new EditInfo(
209 6
						$revision['comment'],
210 6
						array_key_exists( 'minor', $revision ),
211 6
						array_key_exists( 'bot', $revision )
212 6
					),
213 6
					$revision['user'],
214 6
					$revision['timestamp']
215 6
				)
216 6
			);
217 6
		}
218
219 6
		return $revisions;
220
	}
221
222
	/**
223
	 * @param string $model
224
	 * @param string $content returned from the API
225
	 *
226
	 * @throws RuntimeException
227
	 * @return Content
228
	 */
229 6
	private function getContent( $model, $content ) {
230 6
		return new Content( $content, $model );
231
	}
232
233
	/**
234
	 * @param array $array
235
	 *
236
	 * @return Page
237
	 */
238 6
	private function newPageFromResult( $array ) {
239 6
		if ( array_key_exists( 'pageid', $array ) ) {
240 6
			$pageid = $array['pageid'];
241 6
			$revisions = $this->getRevisionsFromResult( $array );
242 6
		} else {
243
			$pageid = 0;
244
			$revisions = new Revisions();
245
		}
246
247 6
		return new Page(
248 6
			new PageIdentifier(
249 6
				new Title(
250 6
					$array['title'],
251 6
					$array['ns']
252 6
				),
253
				$pageid
254 6
			),
255
			$revisions
256 6
		);
257
	}
258
259
}
260