pages.php 16.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
<?php
/**
 * @package     Joomla.Platform
 * @subpackage  MediaWiki
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */

defined('JPATH_PLATFORM') or die;

/**
 * MediaWiki API Pages class for the Joomla Platform.
 *
 * @since  3.1.4
 */
class JMediawikiPages extends JMediawikiObject
{
	/**
	 * Method to edit a page.
	 *
	 * @param   string  $title         Page title.
	 * @param   int     $section       Section number.
	 * @param   string  $sectiontitle  The title for a new section.
	 * @param   string  $text          Page content.
	 * @param   string  $summary       Title of the page you want to delete.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function editPage($title, $section = null, $sectiontitle = null, $text = null, $summary = null)
	{
		// Get the token.
		$token = $this->getToken($title, 'edit');

		// Build the request path.
		$path = '?action=edit';

		// Build the request data.
		$data = array(
			'title' => $title,
			'token' => $token,
			'section' => $section,
			'sectiontitle' => $section,
			'text' => $text,
			'summary' => $summary,
		);

		// Send the request.
		$response = $this->client->post($this->fetchUrl($path), $data);

		return $this->validateResponse($response);
	}

	/**
	 * Method to delete a page.
	 *
	 * @param   string  $title      Title of the page you want to delete.
	 * @param   string  $reason     Reason for the deletion.
	 * @param   string  $watchlist  Unconditionally add or remove the page from your watchlis.
	 * @param   string  $oldimage   The name of the old image to delete.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function deletePageByName($title, $reason = null, $watchlist = null, $oldimage = null)
	{
		// Get the token.
		$token = $this->getToken($title, 'delete');

		// Build the request path.
		$path = '?action=delete';

		// Build the request data.
		$data = array(
			'title' => $title,
			'token' => $token,
			'reason' => $reason,
			'watchlist' => $watchlist,
			'oldimage' => $oldimage,
		);

		// Send the request.
		$response = $this->client->post($this->fetchUrl($path), $data);

		return $this->validateResponse($response);
	}

	/**
	 * Method to delete a page.
	 *
	 * @param   string  $pageid     Page ID of the page you want to delete.
	 * @param   string  $reason     Reason for the deletion.
	 * @param   string  $watchlist  Unconditionally add or remove the page from your watchlis.
	 * @param   string  $oldimage   The name of the old image to delete.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function deletePageById($pageid,  $reason = null, $watchlist = null, $oldimage = null)
	{
		// Get the token.
		$token = $this->getToken($pageid, 'delete');

		// Build the request path.
		$path = '?action=delete';

		// Build the request data.
		$data = array(
			'pageid' => $pageid,
			'token' => $token,
			'reason' => $reason,
			'watchlist' => $watchlist,
			'oldimage' => $oldimage,
		);

		// Send the request.
		$response = $this->client->post($this->fetchUrl($path), $data);

		return $this->validateResponse($response);
	}

	/**
	 * Method to restore certain revisions of a deleted page.
	 *
	 * @param   string  $title      Title of the page you want to restore.
	 * @param   string  $reason     Reason for restoring (optional).
	 * @param   string  $timestamp  Timestamps of the revisions to restore.
	 * @param   string  $watchlist  Unconditionally add or remove the page from your watchlist.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function undeletePage($title, $reason = null, $timestamp = null, $watchlist = null)
	{
		// Get the token.
		$token = $this->getToken($title, 'undelete');

		// Build the request path.
		$path = '?action=undelete';

		// Build the request data.
		$data = array(
			'title' => $title,
			'token' => $token,
			'reason' => $reason,
			'timestamp' => $timestamp,
			'watchlist' => $watchlist,
		);

		// Send the request.
		$response = $this->client->post($this->fetchUrl($path), $data);

		return $this->validateResponse($response);
	}

	/**
	 * Method to move a page.
	 *
	 * @param   string   $from            Title of the page you want to move.
	 * @param   string   $to              Title you want to rename the page to.
	 * @param   string   $reason          Reason for the move (optional).
	 * @param   string   $movetalk        Move the talk page, if it exists.
	 * @param   string   $movesubpages    Move subpages, if applicable.
	 * @param   boolean  $noredirect      Don't create a redirect.
	 * @param   string   $watchlist       Unconditionally add or remove the page from your watchlist.
	 * @param   boolean  $ignorewarnings  Ignore any warnings.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function movePageByName($from, $to, $reason = null, $movetalk = null, $movesubpages = null, $noredirect = null,
		$watchlist =null, $ignorewarnings = null)
	{
		// Get the token.
		$token = $this->getToken($from, 'move');

		// Build the request path.
		$path = '?action=move';

		// Build the request data.
		$data = array(
			'from' => $from,
			'to' => $reason,
			'token' => $token,
			'reason' => $reason,
			'movetalk' => $movetalk,
			'movesubpages' => $movesubpages,
			'noredirect' => $noredirect,
			'watchlist' => $watchlist,
			'ignorewarnings' => $ignorewarnings,
		);

		// Send the request.
		$response = $this->client->post($this->fetchUrl($path), $data);

		return $this->validateResponse($response);
	}

	/**
	 * Method to move a page.
	 *
	 * @param   int      $fromid          Page ID of the page you want to move.
	 * @param   string   $to              Title you want to rename the page to.
	 * @param   string   $reason          Reason for the move (optional).
	 * @param   string   $movetalk        Move the talk page, if it exists.
	 * @param   string   $movesubpages    Move subpages, if applicable.
	 * @param   boolean  $noredirect      Don't create a redirect.
	 * @param   string   $watchlist       Unconditionally add or remove the page from your watchlist.
	 * @param   boolean  $ignorewarnings  Ignore any warnings.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function movePageById($fromid, $to, $reason = null, $movetalk = null, $movesubpages = null, $noredirect = null,
		$watchlist =null, $ignorewarnings = null)
	{
		// Get the token.
		$token = $this->getToken($fromid, 'move');

		// Build the request path.
		$path = '?action=move';

		// Build the request data.
		$data = array(
			'fromid' => $fromid,
			'to' => $reason,
			'token' => $token,
			'reason' => $reason,
			'movetalk' => $movetalk,
			'movesubpages' => $movesubpages,
			'noredirect' => $noredirect,
			'watchlist' => $watchlist,
			'ignorewarnings' => $ignorewarnings,
		);

		// Send the request.
		$response = $this->client->post($this->fetchUrl($path), $data);

		return $this->validateResponse($response);
	}

	/**
	 * Method to undo the last edit to the page.
	 *
	 * @param   string  $title      Title of the page you want to rollback.
	 * @param   string  $user       Name of the user whose edits are to be rolled back.
	 * @param   string  $summary    Custom edit summary. If not set, default summary will be used.
	 * @param   string  $markbot    Mark the reverted edits and the revert as bot edits.
	 * @param   string  $watchlist  Unconditionally add or remove the page from your watchlist.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function rollback($title, $user, $summary = null, $markbot = null, $watchlist = null)
	{
		// Get the token.
		$token = $this->getToken($title, 'rollback');

		// Build the request path.
		$path = '?action=rollback';

		// Build the request data.
		$data = array(
			'title' => $title,
			'token' => $token,
			'user' => $user,
			'expiry' => $summary,
			'markbot' => $markbot,
			'watchlist' => $watchlist,
		);

		// Send the request.
		$response = $this->client->post($this->fetchUrl($path), $data);

		return $this->validateResponse($response);
	}

	/**
	 * Method to change the protection level of a page.
	 *
	 * @param   string  $title        Title of the page you want to (un)protect.
	 * @param   string  $protections  Pipe-separated list of protection levels.
	 * @param   string  $expiry       Expiry timestamps.
	 * @param   string  $reason       Reason for (un)protecting (optional).
	 * @param   string  $cascade      Enable cascading protection.
	 * @param   string  $watchlist    Unconditionally add or remove the page from your watchlist.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function changeProtection($title, $protections, $expiry = null, $reason = null, $cascade = null, $watchlist = null)
	{
		// Get the token.
		$token = $this->getToken($title, 'unblock');

		// Build the request path.
		$path = '?action=protect';

		// Build the request data.
		$data = array(
			'title' => $title,
			'token' => $token,
			'protections' => $protections,
			'expiry' => $expiry,
			'reason' => $reason,
			'cascade' => $cascade,
			'watchlist' => $watchlist,
		);

		// Send the request.
		$response = $this->client->post($this->fetchUrl($path), $data);

		return $this->validateResponse($response);
	}

	/**
	 * Method to get basic page information.
	 *
	 * @param   array    $titles      Page titles to retrieve info.
	 * @param   array    $inprop      Which additional properties to get.
	 * @param   array    $intoken     Request a token to perform a data-modifying action on a page
	 * @param   boolean  $incontinue  When more results are available, use this to continue.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function getPageInfo(array $titles, array $inprop = null, array $intoken = null, $incontinue = null)
	{
		// Build the request
		$path = '?action=query&prop=info';

		// Append titles to the request.
		$path .= '&titles=' . $this->buildParameter($titles);

		if (isset($inprop))
		{
			$path .= '&inprop=' . $this->buildParameter($inprop);
		}

		if (isset($intoken))
		{
			$path .= '&intoken=' . $this->buildParameter($intoken);
		}

		if ($incontinue)
		{
			$path .= '&incontinue=';
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to get various properties defined in the page content.
	 *
	 * @param   array    $titles      Page titles to retrieve properties.
	 * @param   boolean  $ppcontinue  When more results are available, use this to continue.
	 * @param   string   $ppprop      Page prop to look on the page for.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function getPageProperties(array $titles, $ppcontinue = null, $ppprop = null)
	{
		// Build the request
		$path = '?action=query&prop=pageprops';

		// Append titles to the request.
		$path .= '&titles=' . $this->buildParameter($titles);

		if ($ppcontinue)
		{
			$path .= '&ppcontinue=';
		}

		if (isset($ppprop))
		{
			$path .= '&ppprop=' . $ppprop;
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to get a list of revisions.
	 *
	 * @param   array    $titles   Page titles to retrieve revisions.
	 * @param   array    $rvprop   Which properties to get for each revision.
	 * @param   boolean  $rvparse  Parse revision content.
	 * @param   int      $rvlimit  Limit how many revisions will be returned.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function getRevisions(array $titles, array $rvprop = null, $rvparse = null, $rvlimit = null)
	{
		// Build the request
		$path = '?action=query&prop=revisions';

		// Append titles to the request.
		$path .= '&titles=' . $this->buildParameter($titles);

		if (isset($rvprop))
		{
			$path .= '&rvprop=' . $this->buildParameter($rvprop);
		}

		if ($rvparse)
		{
			$path .= '&rvparse=';
		}

		if (isset($rvlimit))
		{
			$path .= '&rvlimit=' . $rvlimit;
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to get all page templates from the given page.
	 *
	 * @param   array    $titles       Page titles to retrieve templates.
	 * @param   array    $tlnamespace  Show templates in this namespace(s) only.
	 * @param   integer  $tllimit      How many templates to return.
	 * @param   boolean  $tlcontinue   When more results are available, use this to continue.
	 * @param   string   $tltemplates  Only list these templates.
	 * @param   string   $tldir        The direction in which to list.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function getPageTemplates(array $titles, array $tlnamespace = null, $tllimit = null, $tlcontinue = null, $tltemplates = null, $tldir = null)
	{
		// Build the request.
		$path = '?action=query&prop=templates';

		// Append titles to the request.
		$path .= '&titles=' . $this->buildParameter($titles);

		if (isset($tlnamespace))
		{
			$path .= '&tlnamespace=' . $this->buildParameter($tlnamespace);
		}

		if (isset($tllimit))
		{
			$path .= '&tllimit=' . $tllimit;
		}

		if ($tlcontinue)
		{
			$path .= '&tlcontinue=';
		}

		if (isset($tltemplates))
		{
			$path .= '&tltemplates=' . $tltemplates;
		}

		if (isset($tldir))
		{
			$path .= '&tldir=' . $tldir;
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to get all pages that link to the given page.
	 *
	 * @param   string   $bltitle           Title to search.
	 * @param   integer  $blpageid          Pageid to search.
	 * @param   boolean  $blcontinue        When more results are available, use this to continue.
	 * @param   array    $blnamespace       The namespace to enumerate.
	 * @param   string   $blfilterredirect  How to filter for redirects..
	 * @param   integer  $bllimit           How many total pages to return.
	 * @param   boolean  $blredirect        If linking page is a redirect, find all pages that link to that redirect as well.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function getBackLinks($bltitle, $blpageid = null, $blcontinue = null, array $blnamespace = null, $blfilterredirect = null,
		$bllimit = null, $blredirect = null)
	{
		// Build the request.
		$path = '?action=query&list=backlinks';

		if (isset($bltitle))
		{
			$path .= '&bltitle=' . $bltitle;
		}

		if (isset($blpageid))
		{
			$path .= '&blpageid=' . $blpageid;
		}

		if ($blcontinue)
		{
			$path .= '&blcontinue=';
		}

		if (isset($blnamespace))
		{
			$path .= '&blnamespace=' . $this->buildParameter($blnamespace);
		}

		if (isset($blfilterredirect))
		{
			$path .= '&blfilterredirect=' . $blfilterredirect;
		}

		if (isset($bllimit))
		{
			$path .= '&bllimit=' . $bllimit;
		}

		if ($blredirect)
		{
			$path .= '&blredirect=';
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to get all pages that link to the given interwiki link.
	 *
	 * @param   string   $iwbltitle     Interwiki link to search for. Must be used with iwblprefix.
	 * @param   string   $iwblprefix    Prefix for the interwiki.
	 * @param   boolean  $iwblcontinue  When more results are available, use this to continue.
	 * @param   integer  $iwbllimit     How many total pages to return.
	 * @param   array    $iwblprop      Which properties to get.
	 *
	 * @return  object
	 *
	 * @since   3.1.4
	 */
	public function getIWBackLinks($iwbltitle, $iwblprefix = null, $iwblcontinue = null, $iwbllimit = null, array $iwblprop = null)
	{
		// Build the request
		$path = '?action=query&list=iwbacklinks';

		if (isset($iwbltitle))
		{
			$path .= '&iwbltitle=' . $iwbltitle;
		}

		if (isset($iwblprefix))
		{
			$path .= '&iwblprefix=' . $iwblprefix;
		}

		if ($iwblcontinue)
		{
			$path .= '&iwblcontinue=';
		}

		if (isset($iwbllimit))
		{
			$path .= '&bllimit=' . $iwbllimit;
		}

		if (isset($iwblprop))
		{
			$path .= '&iwblprop=' . $this->buildParameter($iwblprop);
		}

		// Send the request.
		$response = $this->client->get($this->fetchUrl($path));

		return $this->validateResponse($response);
	}

	/**
	 * Method to get access token.
	 *
	 * @param   string  $user     The User to get token.
	 * @param   string  $intoken  The type of token.
	 *
	 * @return  object
	 *
	 * @since   3.0.0
	 */
	public function getToken($user, $intoken)
	{
		// Build the request path.
		$path = '?action=query&prop=info&intoken=' . $intoken . '&titles=User:' . $user;

		// Send the request.
		$response = $this->client->post($this->fetchUrl($path), null);

		return (string) $this->validateResponse($response)->query->pages->page[$intoken . 'token'];
	}
}