Completed
Push — master ( 11966d...5d848f )
by Sam
13s
created

PageGetter::getFromPageIdentifier()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Mediawiki\Api\Service;
4
5
use Mediawiki\Api\SimpleRequest;
6
use Mediawiki\DataModel\Content;
7
use Mediawiki\DataModel\EditInfo;
8
use Mediawiki\DataModel\Page;
9
use Mediawiki\DataModel\PageIdentifier;
10
use Mediawiki\DataModel\Revision;
11
use Mediawiki\DataModel\Revisions;
12
use Mediawiki\DataModel\Title;
13
use RuntimeException;
14
15
/**
16
 * @access private
17
 *
18
 * @author Addshore
19
 */
20
class PageGetter extends Service {
21
22
	/**
23
	 * @since 0.2
24
	 *
25
	 * @param int $id
26
	 * @param array $extraParams
27
	 *
28
	 * @return Page
29
	 */
30
	public function getFromRevisionId( $id, array $extraParams = [] ) {
31
		$result =
32
			$this->api->getRequest(
33
				new SimpleRequest(
34
					'query',
35
					$this->getQuery( [ 'revids' => $id ], $extraParams )
36
				)
37
			);
38
39
		return $this->newPageFromResult( array_shift( $result['query']['pages'] ) );
40
	}
41
42
	/**
43
	 * @since 0.2
44
	 *
45
	 * @param string|Title $title
46
	 * @param array $extraParams
47
	 *
48
	 * @return Page
49
	 */
50
	public function getFromTitle( $title, array $extraParams = [] ) {
51
		if ( $title instanceof Title ) {
52
			$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...
53
		}
54
		$result =
55
			$this->api->getRequest(
56
				new SimpleRequest(
57
					'query',
58
					$this->getQuery( [ 'titles' => $title ], $extraParams )
59
				)
60
			);
61
62
		return $this->newPageFromResult( array_shift( $result['query']['pages'] ) );
63
	}
64
65
	/**
66
	 * @since 0.2
67
	 *
68
	 * @param int $id
69
	 * @param array $extraParams
70
	 *
71
	 * @return Page
72
	 */
73
	public function getFromPageId( $id, array $extraParams = [] ) {
74
		$result =
75
			$this->api->getRequest(
76
				new SimpleRequest(
77
					'query',
78
					$this->getQuery( [ 'pageids' => $id ], $extraParams )
79
				)
80
			);
81
82
		return $this->newPageFromResult( array_shift( $result['query']['pages'] ) );
83
	}
84
85
	/**
86
	 * @since 0.4
87
	 *
88
	 * @param PageIdentifier $pageIdentifier
89
	 * @param array $extraParams
90
	 *
91
	 * @throws RuntimeException
92
	 * @return Page
93
	 */
94
	public function getFromPageIdentifier(
95
		PageIdentifier $pageIdentifier,
96
		array $extraParams = []
97
	) {
98
		if ( !$pageIdentifier->identifiesPage() ) {
99
			throw new RuntimeException( '$pageIdentifier does not identify a page' );
100
		}
101
		if ( !is_null( $pageIdentifier->getId() ) ) {
102
			return $this->getFromPageId( $pageIdentifier->getId(), $extraParams );
103
		} else {
104
			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...
105
		}
106
	}
107
108
	/**
109
	 * @since 0.2
110
	 *
111
	 * @param Page $page
112
	 * @param array $extraParams
113
	 *
114
	 * @return Page
115
	 */
116
	public function getFromPage( Page $page, array $extraParams = [] ) {
117
		$result =
118
			$this->api->getRequest(
119
				new SimpleRequest(
120
					'query',
121
					$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...
122
				)
123
			);
124
		$revisions = $this->getRevisionsFromResult( array_shift( $result['query']['pages'] ) );
125
		$revisions->addRevisions( $page->getRevisions() );
126
127
		return new Page(
128
			$page->getPageIdentifier(),
129
			$revisions
130
		);
131
	}
132
133
	/**
134
	 * @since 0.2
135
	 *
136
	 * @param Revision $revision
137
	 * @param array $extraParams
138
	 *
139
	 * @return Page
140
	 */
141
	public function getFromRevision( Revision $revision, array $extraParams = [] ) {
142
		$result =
143
			$this->api->getRequest(
144
				new SimpleRequest(
145
					'query',
146
					$this->getQuery( [ 'revids' => $revision->getId() ], $extraParams )
147
				)
148
			);
149
		$revisions = $this->getRevisionsFromResult( array_shift( $result['query']['pages'] ) );
150
		$revisions->addRevision( $revision );
151
152
		return new Page(
153
			new PageIdentifier(
154
				new Title(
155
					$result['title'],
156
					$result['ns']
157
				),
158
				$result['pageid']
159
			),
160
			$revisions
161
		);
162
	}
163
164
	/**
165
	 * @param array $additionalParams
166
	 *
167
	 * @param array $extraParams
168
	 *
169
	 * @return array
170
	 */
171
	private function getQuery( $additionalParams, array $extraParams = [] ) {
172
		$base = [
173
			'prop' => 'revisions|info|pageprops',
174
			'rvprop' => 'ids|flags|timestamp|user|size|sha1|comment|content|tags',
175
			'inprop' => 'protection',
176
		];
177
178
		return array_merge( $extraParams, $base, $additionalParams );
179
	}
180
181
	/**
182
	 * @param array $array
183
	 *
184
	 * @return Revisions
185
	 */
186
	private function getRevisionsFromResult( $array ) {
187
		$revisions = new Revisions();
188
		$pageid = $array['pageid'];
189
		foreach ( $array['revisions'] as $revision ) {
190
			$revisions->addRevision(
191
				new Revision(
192
					$this->getContent( $array['contentmodel'], $revision['*'] ),
193
					new PageIdentifier( new Title( $array['title'], $array['ns'] ), $pageid ),
194
					$revision['revid'],
195
					new EditInfo(
196
						$revision['comment'],
197
						array_key_exists( 'minor', $revision ),
198
						array_key_exists( 'bot', $revision )
199
					),
200
					$revision['user'],
201
					$revision['timestamp']
202
				)
203
			);
204
		}
205
206
		return $revisions;
207
	}
208
209
	/**
210
	 * @param string $model
211
	 * @param string $content returned from the API
212
	 *
213
	 * @throws RuntimeException
214
	 * @return Content
215
	 */
216
	private function getContent( $model, $content ) {
217
		return new Content( $content, $model );
218
	}
219
220
	/**
221
	 * @param array $array
222
	 *
223
	 * @return Page
224
	 */
225
	private function newPageFromResult( $array ) {
226
		if ( array_key_exists( 'pageid', $array ) ) {
227
			$pageid = $array['pageid'];
228
			$revisions = $this->getRevisionsFromResult( $array );
229
		} else {
230
			$pageid = 0;
231
			$revisions = new Revisions();
232
		}
233
234
		return new Page(
235
			new PageIdentifier(
236
				new Title(
237
					$array['title'],
238
					$array['ns']
239
				),
240
				$pageid
241
			),
242
			$revisions
243
		);
244
	}
245
246
}
247