1 | <?php |
||
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 ) { |
||
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 | public function getFromTitle( $title, array $extraParams = [] ) { |
||
64 | if ( $title instanceof Title ) { |
||
65 | $title = $title->getTitle(); |
||
|
|||
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 | * @return 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 | * @return 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 ); |
||
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 ) |
||
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 ) { |
||
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']; |
||
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.