These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | public function __construct( MediawikiApi $api ) { |
||
32 | $this->api = $api; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @since 0.2 |
||
37 | * |
||
38 | * @param int $id |
||
39 | * @param array $extraParams |
||
40 | * |
||
41 | * @returns 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 | * @returns Page |
||
62 | */ |
||
63 | public function getFromTitle( $title, array $extraParams = [] ) { |
||
64 | if ( $title instanceof Title ) { |
||
65 | $title = $title->getTitle(); |
||
0 ignored issues
–
show
|
|||
66 | } |
||
67 | $result = |
||
68 | $this->api->getRequest( |
||
69 | new SimpleRequest( |
||
70 | 'query', |
||
71 | $this->getQuery( [ 'titles' => $title ], $extraParams ) |
||
72 | ) |
||
73 | ); |
||
74 | |||
75 | 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 | * @returns Page |
||
85 | */ |
||
86 | public function getFromPageId( $id, array $extraParams = [] ) { |
||
87 | $result = |
||
88 | $this->api->getRequest( |
||
89 | new SimpleRequest( |
||
90 | 'query', |
||
91 | $this->getQuery( [ 'pageids' => $id ], $extraParams ) |
||
92 | ) |
||
93 | ); |
||
94 | |||
95 | 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 | * @returns Page |
||
106 | */ |
||
107 | public function getFromPageIdentifier( |
||
108 | PageIdentifier $pageIdentifier, |
||
109 | array $extraParams = [] |
||
110 | ) { |
||
111 | if ( !$pageIdentifier->identifiesPage() ) { |
||
112 | throw new RuntimeException( '$pageIdentifier does not identify a page' ); |
||
113 | } |
||
114 | if ( !is_null( $pageIdentifier->getId() ) ) { |
||
115 | return $this->getFromPageId( $pageIdentifier->getId(), $extraParams ); |
||
116 | } else { |
||
117 | return $this->getFromTitle( $pageIdentifier->getTitle(), $extraParams ); |
||
0 ignored issues
–
show
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
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 | private function getQuery( $additionalParams, array $extraParams = [] ) { |
||
185 | $base = [ |
||
186 | 'prop' => 'revisions|info|pageprops', |
||
187 | 'rvprop' => 'ids|flags|timestamp|user|size|sha1|comment|content|tags', |
||
188 | 'inprop' => 'protection', |
||
189 | ]; |
||
190 | |||
191 | return array_merge( $extraParams, $base, $additionalParams ); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param array $array |
||
196 | * |
||
197 | * @return Revisions |
||
198 | */ |
||
199 | private function getRevisionsFromResult( $array ) { |
||
200 | $revisions = new Revisions(); |
||
201 | $pageid = $array['pageid']; |
||
202 | foreach ( $array['revisions'] as $revision ) { |
||
203 | $revisions->addRevision( |
||
204 | new Revision( |
||
205 | $this->getContent( $array['contentmodel'], $revision['*'] ), |
||
206 | new PageIdentifier( new Title( $array['title'], $array['ns'] ), $pageid ), |
||
207 | $revision['revid'], |
||
208 | new EditInfo( |
||
209 | $revision['comment'], |
||
210 | array_key_exists( 'minor', $revision ), |
||
211 | array_key_exists( 'bot', $revision ) |
||
212 | ), |
||
213 | $revision['user'], |
||
214 | $revision['timestamp'] |
||
215 | ) |
||
216 | ); |
||
217 | } |
||
218 | |||
219 | 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 | private function getContent( $model, $content ) { |
||
230 | return new Content( $content, $model ); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param array $array |
||
235 | * |
||
236 | * @return Page |
||
237 | */ |
||
238 | private function newPageFromResult( $array ) { |
||
239 | if ( array_key_exists( 'pageid', $array ) ) { |
||
240 | $pageid = $array['pageid']; |
||
241 | $revisions = $this->getRevisionsFromResult( $array ); |
||
242 | } else { |
||
243 | $pageid = 0; |
||
244 | $revisions = new Revisions(); |
||
245 | } |
||
246 | |||
247 | return new Page( |
||
248 | new PageIdentifier( |
||
249 | new Title( |
||
250 | $array['title'], |
||
251 | $array['ns'] |
||
252 | ), |
||
253 | $pageid |
||
254 | ), |
||
255 | $revisions |
||
256 | ); |
||
257 | } |
||
258 | |||
259 | } |
||
260 |
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.