PageGetter::getFromRevision()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 18
cp 0
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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 View Code Duplication
	public function getFromRevisionId( $id, array $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...
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 7
	public function getFromTitle( $title, array $extraParams = [] ) {
51 7
		if ( $title instanceof Title ) {
52 7
			$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 7
		}
54
		$result =
55 7
			$this->api->getRequest(
56 7
				new SimpleRequest(
57 7
					'query',
58 7
					$this->getQuery( [ 'titles' => $title ], $extraParams )
59 7
				)
60 7
			);
61
62 7
		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 1 View Code Duplication
	public function getFromPageId( $id, array $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...
74
		$result =
75 1
			$this->api->getRequest(
76 1
				new SimpleRequest(
77 1
					'query',
78 1
					$this->getQuery( [ 'pageids' => $id ], $extraParams )
79 1
				)
80 1
			);
81
82 1
		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 5
	public function getFromPageIdentifier(
95
		PageIdentifier $pageIdentifier,
96
		array $extraParams = []
97
	) {
98 5
		if ( !$pageIdentifier->identifiesPage() ) {
99
			throw new RuntimeException( '$pageIdentifier does not identify a page' );
100
		}
101 5
		if ( $pageIdentifier->getId() !== null ) {
102
			return $this->getFromPageId( $pageIdentifier->getId(), $extraParams );
103
		} else {
104 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...
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 8
	private function getQuery( $additionalParams, array $extraParams = [] ) {
172
		$base = [
173 8
			'prop' => 'revisions|info|pageprops',
174 8
			'rvprop' => 'ids|flags|timestamp|user|size|sha1|comment|content|tags',
175 8
			'inprop' => 'protection',
176 8
		];
177
178 8
		return array_merge( $extraParams, $base, $additionalParams );
179
	}
180
181
	/**
182
	 * @param array $array
183
	 *
184
	 * @return Revisions
185
	 */
186 8
	private function getRevisionsFromResult( $array ) {
187 8
		$revisions = new Revisions();
188 8
		$pageid = $array['pageid'];
189 8
		foreach ( $array['revisions'] as $revision ) {
190 8
			$revision['comment'] = ( isset( $revision['comment'] ) ) ? $revision['comment'] : '';
191 8
			$revisions->addRevision(
192 8
				new Revision(
193 8
					$this->getContent( $array['contentmodel'], $revision['*'] ),
194 8
					new PageIdentifier( new Title( $array['title'], $array['ns'] ), $pageid ),
195 8
					$revision['revid'],
196 8
					new EditInfo(
197 8
						$revision['comment'],
198 8
						array_key_exists( 'minor', $revision ),
199 8
						array_key_exists( 'bot', $revision )
200 8
					),
201 8
					$revision['user'],
202 8
					$revision['timestamp']
203 8
				)
204 8
			);
205
		}
206 8
207
		return $revisions;
208
	}
209
210
	/**
211
	 * @param string $model
212
	 * @param string $content returned from the API
213
	 *
214
	 * @throws RuntimeException
215
	 * @return Content
216 8
	 */
217 8
	private function getContent( $model, $content ) {
218
		return new Content( $content, $model );
219
	}
220
221
	/**
222
	 * @param array $array
223
	 *
224
	 * @return Page
225 8
	 */
226 8
	private function newPageFromResult( $array ) {
227 8
		if ( array_key_exists( 'pageid', $array ) ) {
228 8
			$pageid = $array['pageid'];
229 8
			$revisions = $this->getRevisionsFromResult( $array );
230 1
		} else {
231 1
			$pageid = 0;
232
			$revisions = new Revisions();
233
		}
234 8
235 8
		return new Page(
236 8
			new PageIdentifier(
237 8
				new Title(
238 8
					$array['title'],
239 8
					$array['ns']
240
				),
241 8
				$pageid
242
			),
243 8
			$revisions
244
		);
245
	}
246
247
}
248