1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage MediaWiki
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * MediaWiki API Pages class for the Joomla Platform.
14 *
15 * @since 12.3
16 */
17 class JMediawikiPages extends JMediawikiObject
18 {
19 /**
20 * Method to edit a page.
21 *
22 * @param string $title Page title.
23 * @param int $section Section number.
24 * @param string $sectiontitle The title for a new section.
25 * @param string $text Page content.
26 * @param string $summary Title of the page you want to delete.
27 *
28 * @return object
29 *
30 * @since 12.3
31 */
32 public function editPage($title, $section = null, $sectiontitle = null, $text = null, $summary = null)
33 {
34 // Get the token.
35 $token = $this->getToken($title, 'edit');
36
37 // Build the request path.
38 $path = '?action=edit';
39
40 // Build the request data.
41 $data = array(
42 'title' => $title,
43 'token' => $token,
44 'section' => $section,
45 'sectiontitle' => $section,
46 'text' => $text,
47 'summary' => $summary,
48 );
49
50 // Send the request.
51 $response = $this->client->post($this->fetchUrl($path), $data);
52
53 return $this->validateResponse($response);
54 }
55
56 /**
57 * Method to delete a page.
58 *
59 * @param string $title Title of the page you want to delete.
60 * @param string $reason Reason for the deletion.
61 * @param string $watchlist Unconditionally add or remove the page from your watchlis.
62 * @param string $oldimage The name of the old image to delete.
63 *
64 * @return object
65 *
66 * @since 12.3
67 */
68 public function deletePageByName($title, $reason = null, $watchlist = null, $oldimage = null)
69 {
70 // Get the token.
71 $token = $this->getToken($title, 'delete');
72
73 // Build the request path.
74 $path = '?action=delete';
75
76 // Build the request data.
77 $data = array(
78 'title' => $title,
79 'token' => $token,
80 'reason' => $reason,
81 'watchlist' => $watchlist,
82 'oldimage' => $oldimage,
83 );
84
85 // Send the request.
86 $response = $this->client->post($this->fetchUrl($path), $data);
87
88 return $this->validateResponse($response);
89 }
90
91 /**
92 * Method to delete a page.
93 *
94 * @param string $pageid Page ID of the page you want to delete.
95 * @param string $reason Reason for the deletion.
96 * @param string $watchlist Unconditionally add or remove the page from your watchlis.
97 * @param string $oldimage The name of the old image to delete.
98 *
99 * @return object
100 *
101 * @since 12.3
102 */
103 public function deletePageById($pageid, $reason = null, $watchlist = null, $oldimage = null)
104 {
105 // Get the token.
106 $token = $this->getToken($pageid, 'delete');
107
108 // Build the request path.
109 $path = '?action=delete';
110
111 // Build the request data.
112 $data = array(
113 'pageid' => $pageid,
114 'token' => $token,
115 'reason' => $reason,
116 'watchlist' => $watchlist,
117 'oldimage' => $oldimage,
118 );
119
120 // Send the request.
121 $response = $this->client->post($this->fetchUrl($path), $data);
122
123 return $this->validateResponse($response);
124 }
125
126 /**
127 * Method to restore certain revisions of a deleted page.
128 *
129 * @param string $title Title of the page you want to restore.
130 * @param string $reason Reason for restoring (optional).
131 * @param string $timestamp Timestamps of the revisions to restore.
132 * @param string $watchlist Unconditionally add or remove the page from your watchlist.
133 *
134 * @return object
135 *
136 * @since 12.3
137 */
138 public function undeletePage($title, $reason = null, $timestamp = null, $watchlist = null)
139 {
140 // Get the token.
141 $token = $this->getToken($title, 'undelete');
142
143 // Build the request path.
144 $path = '?action=undelete';
145
146 // Build the request data.
147 $data = array(
148 'title' => $title,
149 'token' => $token,
150 'reason' => $reason,
151 'timestamp' => $timestamp,
152 'watchlist' => $watchlist,
153 );
154
155 // Send the request.
156 $response = $this->client->post($this->fetchUrl($path), $data);
157
158 return $this->validateResponse($response);
159 }
160
161 /**
162 * Method to move a page.
163 *
164 * @param string $from Title of the page you want to move.
165 * @param string $to Title you want to rename the page to.
166 * @param string $reason Reason for the move (optional).
167 * @param string $movetalk Move the talk page, if it exists.
168 * @param string $movesubpages Move subpages, if applicable.
169 * @param boolean $noredirect Don't create a redirect.
170 * @param string $watchlist Unconditionally add or remove the page from your watchlist.
171 * @param boolean $ignorewarnings Ignore any warnings.
172 *
173 * @return object
174 *
175 * @since 12.3
176 */
177 public function movePageByName($from, $to, $reason = null, $movetalk = null, $movesubpages = null, $noredirect = null,
178 $watchlist =null, $ignorewarnings = null)
179 {
180 // Get the token.
181 $token = $this->getToken($from, 'move');
182
183 // Build the request path.
184 $path = '?action=move';
185
186 // Build the request data.
187 $data = array(
188 'from' => $from,
189 'to' => $reason,
190 'token' => $token,
191 'reason' => $reason,
192 'movetalk' => $movetalk,
193 'movesubpages' => $movesubpages,
194 'noredirect' => $noredirect,
195 'watchlist' => $watchlist,
196 'ignorewarnings' => $ignorewarnings,
197 );
198
199 // Send the request.
200 $response = $this->client->post($this->fetchUrl($path), $data);
201
202 return $this->validateResponse($response);
203 }
204
205 /**
206 * Method to move a page.
207 *
208 * @param int $fromid Page ID of the page you want to move.
209 * @param string $to Title you want to rename the page to.
210 * @param string $reason Reason for the move (optional).
211 * @param string $movetalk Move the talk page, if it exists.
212 * @param string $movesubpages Move subpages, if applicable.
213 * @param boolean $noredirect Don't create a redirect.
214 * @param string $watchlist Unconditionally add or remove the page from your watchlist.
215 * @param boolean $ignorewarnings Ignore any warnings.
216 *
217 * @return object
218 *
219 * @since 12.3
220 */
221 public function movePageById($fromid, $to, $reason = null, $movetalk = null, $movesubpages = null, $noredirect = null,
222 $watchlist =null, $ignorewarnings = null)
223 {
224 // Get the token.
225 $token = $this->getToken($fromid, 'move');
226
227 // Build the request path.
228 $path = '?action=move';
229
230 // Build the request data.
231 $data = array(
232 'fromid' => $fromid,
233 'to' => $reason,
234 'token' => $token,
235 'reason' => $reason,
236 'movetalk' => $movetalk,
237 'movesubpages' => $movesubpages,
238 'noredirect' => $noredirect,
239 'watchlist' => $watchlist,
240 'ignorewarnings' => $ignorewarnings,
241 );
242
243 // Send the request.
244 $response = $this->client->post($this->fetchUrl($path), $data);
245
246 return $this->validateResponse($response);
247 }
248
249 /**
250 * Method to undo the last edit to the page.
251 *
252 * @param string $title Title of the page you want to rollback.
253 * @param string $user Name of the user whose edits are to be rolled back.
254 * @param string $summary Custom edit summary. If not set, default summary will be used.
255 * @param string $markbot Mark the reverted edits and the revert as bot edits.
256 * @param string $watchlist Unconditionally add or remove the page from your watchlist.
257 *
258 * @return object
259 *
260 * @since 12.3
261 */
262 public function rollback($title, $user, $summary = null, $markbot = null, $watchlist = null)
263 {
264 // Get the token.
265 $token = $this->getToken($title, 'rollback');
266
267 // Build the request path.
268 $path = '?action=rollback';
269
270 // Build the request data.
271 $data = array(
272 'title' => $title,
273 'token' => $token,
274 'user' => $user,
275 'expiry' => $summary,
276 'markbot' => $markbot,
277 'watchlist' => $watchlist,
278 );
279
280 // Send the request.
281 $response = $this->client->post($this->fetchUrl($path), $data);
282
283 return $this->validateResponse($response);
284 }
285
286 /**
287 * Method to change the protection level of a page.
288 *
289 * @param string $title Title of the page you want to (un)protect.
290 * @param string $protections Pipe-separated list of protection levels.
291 * @param string $expiry Expiry timestamps.
292 * @param string $reason Reason for (un)protecting (optional).
293 * @param string $cascade Enable cascading protection.
294 * @param string $watchlist Unconditionally add or remove the page from your watchlist.
295 *
296 * @return object
297 *
298 * @since 12.3
299 */
300 public function changeProtection($title, $protections, $expiry = null, $reason = null, $cascade = null, $watchlist = null)
301 {
302 // Get the token.
303 $token = $this->getToken($title, 'unblock');
304
305 // Build the request path.
306 $path = '?action=protect';
307
308 // Build the request data.
309 $data = array(
310 'title' => $title,
311 'token' => $token,
312 'protections' => $protections,
313 'expiry' => $expiry,
314 'reason' => $reason,
315 'cascade' => $cascade,
316 'watchlist' => $watchlist,
317 );
318
319 // Send the request.
320 $response = $this->client->post($this->fetchUrl($path), $data);
321
322 return $this->validateResponse($response);
323 }
324
325 /**
326 * Method to get basic page information.
327 *
328 * @param array $titles Page titles to retrieve info.
329 * @param array $inprop Which additional properties to get.
330 * @param array $intoken Request a token to perform a data-modifying action on a page
331 * @param boolean $incontinue When more results are available, use this to continue.
332 *
333 * @return object
334 *
335 * @since 12.3
336 */
337 public function getPageInfo(array $titles, array $inprop = null, array $intoken = null, $incontinue = null)
338 {
339 // Build the request
340 $path = '?action=query&prop=info';
341
342 // Append titles to the request.
343 $path .= '&titles=' . $this->buildParameter($titles);
344
345 if (isset($inprop))
346 {
347 $path .= '&inprop=' . $this->buildParameter($inprop);
348 }
349
350 if (isset($intoken))
351 {
352 $path .= '&intoken=' . $this->buildParameter($intoken);
353 }
354
355 if ($incontinue)
356 {
357 $path .= '&incontinue=';
358 }
359
360 // Send the request.
361 $response = $this->client->get($this->fetchUrl($path));
362
363 return $this->validateResponse($response);
364 }
365
366 /**
367 * Method to get various properties defined in the page content.
368 *
369 * @param array $titles Page titles to retrieve properties.
370 * @param boolean $ppcontinue When more results are available, use this to continue.
371 * @param string $ppprop Page prop to look on the page for.
372 *
373 * @return object
374 *
375 * @since 12.3
376 */
377 public function getPageProperties(array $titles, $ppcontinue = null, $ppprop = null)
378 {
379 // Build the request
380 $path = '?action=query&prop=pageprops';
381
382 // Append titles to the request.
383 $path .= '&titles=' . $this->buildParameter($titles);
384
385 if ($ppcontinue)
386 {
387 $path .= '&ppcontinue=';
388 }
389
390 if (isset($ppprop))
391 {
392 $path .= '&ppprop=' . $ppprop;
393 }
394
395 // Send the request.
396 $response = $this->client->get($this->fetchUrl($path));
397
398 return $this->validateResponse($response);
399 }
400
401 /**
402 * Method to get a list of revisions.
403 *
404 * @param array $titles Page titles to retrieve revisions.
405 * @param array $rvprop Which properties to get for each revision.
406 * @param boolean $rvparse Parse revision content.
407 * @param int $rvlimit Limit how many revisions will be returned.
408 *
409 * @return object
410 *
411 * @since 12.3
412 */
413 public function getRevisions(array $titles, array $rvprop = null, $rvparse = null, $rvlimit = null)
414 {
415 // Build the request
416 $path = '?action=query&prop=revisions';
417
418 // Append titles to the request.
419 $path .= '&titles=' . $this->buildParameter($titles);
420
421 if (isset($rvprop))
422 {
423 $path .= '&rvprop=' . $this->buildParameter($rvprop);
424 }
425
426 if ($rvparse)
427 {
428 $path .= '&rvparse=';
429 }
430
431 if (isset($rvlimit))
432 {
433 $path .= '&rvlimit=' . $rvlimit;
434 }
435
436 // Send the request.
437 $response = $this->client->get($this->fetchUrl($path));
438
439 return $this->validateResponse($response);
440 }
441
442 /**
443 * Method to get all page templates from the given page.
444 *
445 * @param array $titles Page titles to retrieve templates.
446 * @param array $tlnamespace Show templates in this namespace(s) only.
447 * @param integer $tllimit How many templates to return.
448 * @param boolean $tlcontinue When more results are available, use this to continue.
449 * @param string $tltemplates Only list these templates.
450 * @param string $tldir The direction in which to list.
451 *
452 * @return object
453 *
454 * @since 12.3
455 */
456 public function getPageTemplates(array $titles, array $tlnamespace = null, $tllimit = null, $tlcontinue = null, $tltemplates = null, $tldir = null)
457 {
458 // Build the request.
459 $path = '?action=query&prop=templates';
460
461 // Append titles to the request.
462 $path .= '&titles=' . $this->buildParameter($titles);
463
464 if (isset($tlnamespace))
465 {
466 $path .= '&tlnamespace=' . $this->buildParameter($tlnamespace);
467 }
468
469 if (isset($tllimit))
470 {
471 $path .= '&tllimit=' . $tllimit;
472 }
473
474 if ($tlcontinue)
475 {
476 $path .= '&tlcontinue=';
477 }
478
479 if (isset($tltemplates))
480 {
481 $path .= '&tltemplates=' . $tltemplates;
482 }
483
484 if (isset($tldir))
485 {
486 $path .= '&tldir=' . $tldir;
487 }
488
489 // Send the request.
490 $response = $this->client->get($this->fetchUrl($path));
491
492 return $this->validateResponse($response);
493 }
494
495 /**
496 * Method to get all pages that link to the given page.
497 *
498 * @param string $bltitle Title to search.
499 * @param integer $blpageid Pageid to search.
500 * @param boolean $blcontinue When more results are available, use this to continue.
501 * @param array $blnamespace The namespace to enumerate.
502 * @param string $blfilterredirect How to filter for redirects..
503 * @param integer $bllimit How many total pages to return.
504 * @param boolean $blredirect If linking page is a redirect, find all pages that link to that redirect as well.
505 *
506 * @return object
507 *
508 * @since 12.3
509 */
510 public function getBackLinks($bltitle, $blpageid = null, $blcontinue = null, array $blnamespace = null, $blfilterredirect = null,
511 $bllimit = null, $blredirect = null)
512 {
513 // Build the request.
514 $path = '?action=query&list=backlinks';
515
516 if (isset($bltitle))
517 {
518 $path .= '&bltitle=' . $bltitle;
519 }
520
521 if (isset($blpageid))
522 {
523 $path .= '&blpageid=' . $blpageid;
524 }
525
526 if ($blcontinue)
527 {
528 $path .= '&blcontinue=';
529 }
530
531 if (isset($blnamespace))
532 {
533 $path .= '&blnamespace=' . $this->buildParameter($blnamespace);
534 }
535
536 if (isset($blfilterredirect))
537 {
538 $path .= '&blfilterredirect=' . $blfilterredirect;
539 }
540
541 if (isset($bllimit))
542 {
543 $path .= '&bllimit=' . $bllimit;
544 }
545
546 if ($blredirect)
547 {
548 $path .= '&blredirect=';
549 }
550
551 // Send the request.
552 $response = $this->client->get($this->fetchUrl($path));
553
554 return $this->validateResponse($response);
555 }
556
557 /**
558 * Method to get all pages that link to the given interwiki link.
559 *
560 * @param string $iwbltitle Interwiki link to search for. Must be used with iwblprefix.
561 * @param string $iwblprefix Prefix for the interwiki.
562 * @param boolean $iwblcontinue When more results are available, use this to continue.
563 * @param integer $iwbllimit How many total pages to return.
564 * @param array $iwblprop Which properties to get.
565 *
566 * @return object
567 *
568 * @since 12.3
569 */
570 public function getIWBackLinks($iwbltitle, $iwblprefix = null, $iwblcontinue = null, $iwbllimit = null, array $iwblprop = null)
571 {
572 // Build the request
573 $path = '?action=query&list=iwbacklinks';
574
575 if (isset($iwbltitle))
576 {
577 $path .= '&iwbltitle=' . $iwbltitle;
578 }
579
580 if (isset($iwblprefix))
581 {
582 $path .= '&iwblprefix=' . $iwblprefix;
583 }
584
585 if ($iwblcontinue)
586 {
587 $path .= '&iwblcontinue=';
588 }
589
590 if (isset($iwbllimit))
591 {
592 $path .= '&bllimit=' . $iwbllimit;
593 }
594
595 if (isset($iwblprop))
596 {
597 $path .= '&iwblprop=' . $this->buildParameter($iwblprop);
598 }
599
600 // Send the request.
601 $response = $this->client->get($this->fetchUrl($path));
602
603 return $this->validateResponse($response);
604 }
605
606 /**
607 * Method to get access token.
608 *
609 * @param string $user The User to get token.
610 * @param string $intoken The type of token.
611 *
612 * @return object
613 *
614 * @since 12.1
615 */
616 public function getToken($user, $intoken)
617 {
618 // Build the request path.
619 $path = '?action=query&prop=info&intoken=' . $intoken . '&titles=User:' . $user;
620
621 // Send the request.
622 $response = $this->client->post($this->fetchUrl($path), null);
623
624 return (string) $this->validateResponse($response)->query->pages->page[$intoken . 'token'];
625 }
626 }
627