Completed
Push — main ( 2daa48...b5d932 )
by
unknown
08:38
created

PageGetter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 228
Duplicated Lines 9.65 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 10
dl 22
loc 228
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Addwiki\Mediawiki\Api\Service;
4
5
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
6
use Addwiki\Mediawiki\DataModel\Content;
7
use Addwiki\Mediawiki\DataModel\EditInfo;
8
use Addwiki\Mediawiki\DataModel\Page;
9
use Addwiki\Mediawiki\DataModel\PageIdentifier;
10
use Addwiki\Mediawiki\DataModel\Revision;
11
use Addwiki\Mediawiki\DataModel\Revisions;
12
use Addwiki\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
	public function getFromRevisionId( int $id, array $extraParams = [] ): Page {
26
		$result =
27
			$this->api->getRequest(
28
				new SimpleRequest(
29
					'query',
30
					$this->getQuery( [ 'revids' => $id ], $extraParams )
31
				)
32
			);
33
34
		return $this->newPageFromResult( array_shift( $result['query']['pages'] ) );
35
	}
36
37
	/**
38
	 * @since 0.2
39
	 *
40
	 * @param string|Title $title
41
	 * @param array $extraParams
42
	 */
43
	public function getFromTitle( $title, array $extraParams = [] ): Page {
44
		if ( $title instanceof Title ) {
45
			$title = $title->getTitle();
46
		}
47
		$result =
48
			$this->api->getRequest(
49
				new SimpleRequest(
50
					'query',
51
					$this->getQuery( [ 'titles' => $title ], $extraParams )
52
				)
53
			);
54
55
		return $this->newPageFromResult( array_shift( $result['query']['pages'] ) );
56
	}
57
58
	/**
59
	 * @since 0.2
60
	 */
61
	public function getFromPageId( int $id, array $extraParams = [] ): Page {
62
		$result =
63
			$this->api->getRequest(
64
				new SimpleRequest(
65
					'query',
66
					$this->getQuery( [ 'pageids' => $id ], $extraParams )
67
				)
68
			);
69
70
		return $this->newPageFromResult( array_shift( $result['query']['pages'] ) );
71
	}
72
73
	/**
74
	 * @since 0.4
75
	 *
76
	 * @param PageIdentifier $pageIdentifier
77
	 * @param array $extraParams
78
	 *
79
	 * @throws RuntimeException
80
	 * @return Page|void
81
	 */
82
	public function getFromPageIdentifier(
83
		PageIdentifier $pageIdentifier,
84
		array $extraParams = []
85
	) {
86
		if ( !$pageIdentifier->identifiesPage() ) {
87
			throw new RuntimeException( '$pageIdentifier does not identify a page' );
88
		}
89
		if ( $pageIdentifier->getId() !== null ) {
90
			return $this->getFromPageId( $pageIdentifier->getId(), $extraParams );
91
		} else {
92
			return $this->getFromTitle( $pageIdentifier->getTitle(), $extraParams );
93
		}
94
	}
95
96
	/**
97
	 * @since 0.2
98
	 *
99
	 * @param Page $page
100
	 * @param array $extraParams
101
	 */
102
	public function getFromPage( Page $page, array $extraParams = [] ): Page {
103
		$result =
104
			$this->api->getRequest(
105
				new SimpleRequest(
106
					'query',
107
					$this->getQuery( [ 'pageids' => $page->getId() ], $extraParams )
108
				)
109
			);
110
		$revisions = $this->getRevisionsFromResult( array_shift( $result['query']['pages'] ) );
111
		$revisions->addRevisions( $page->getRevisions() );
112
113
		return new Page(
114
			$page->getPageIdentifier(),
115
			$revisions
116
		);
117
	}
118
119
	/**
120
	 * @since 0.2
121
	 *
122
	 * @param Revision $revision
123
	 * @param array $extraParams
124
	 */
125
	public function getFromRevision( Revision $revision, array $extraParams = [] ): Page {
126
		$result =
127
			$this->api->getRequest(
128
				new SimpleRequest(
129
					'query',
130
					$this->getQuery( [ 'revids' => $revision->getId() ], $extraParams )
131
				)
132
			);
133
		$revisions = $this->getRevisionsFromResult( array_shift( $result['query']['pages'] ) );
134
		$revisions->addRevision( $revision );
135
136
		return new Page(
137
			new PageIdentifier(
138
				new Title(
139
					$result['title'],
140
					$result['ns']
141
				),
142
				$result['pageid']
143
			),
144
			$revisions
145
		);
146
	}
147
148
	/**
149
	 *
150
	 *
151
	 * @return mixed[]
152
	 */
153
	private function getQuery( array $additionalParams, array $extraParams = [] ): array {
154
		$base = [
155
			'prop' => 'revisions|info|pageprops',
156
			'rvprop' => 'ids|flags|timestamp|user|size|sha1|comment|content|tags',
157
			'inprop' => 'protection',
158
		];
159
160
		return array_merge( $extraParams, $base, $additionalParams );
161
	}
162
163
	private function getRevisionsFromResult( array $array ): Revisions {
164
		$revisions = new Revisions();
165
		$pageid = $array['pageid'];
166
		foreach ( $array['revisions'] as $revision ) {
167
			$revision['comment'] ??= '';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '='
Loading history...
168
			$revisions->addRevision(
169
				new Revision(
170
					$this->getContent( $array['contentmodel'], $revision['*'] ),
171
					new PageIdentifier( new Title( $array['title'], $array['ns'] ), $pageid ),
172
					$revision['revid'],
173
					new EditInfo(
174
						$revision['comment'],
175
						array_key_exists( 'minor', $revision ),
176
						array_key_exists( 'bot', $revision )
177
					),
178
					$revision['user'],
179
					$revision['timestamp']
180
				)
181
			);
182
		}
183
184
		return $revisions;
185
	}
186
187
	/**
188
	 * @param string $content returned from the API
189
	 * @throws RuntimeException
190
	 */
191
	private function getContent( string $model, string $content ): Content {
192
		return new Content( $content, $model );
193
	}
194
195
	private function newPageFromResult( array $array ): Page {
196
		if ( array_key_exists( 'pageid', $array ) ) {
197
			$pageid = $array['pageid'];
198
			$revisions = $this->getRevisionsFromResult( $array );
199
		} else {
200
			$pageid = 0;
201
			$revisions = new Revisions();
202
		}
203
204
		return new Page(
205
			new PageIdentifier(
206
				new Title(
207
					$array['title'],
208
					$array['ns']
209
				),
210
				$pageid
211
			),
212
			$revisions
213
		);
214
	}
215
216
}
217