Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ COMMONS_HOST="https://commons.wikimedia.org/"

### Wikimedia REST API
* pageContent
* getPageHtml
* getPageSummary
* getPageTitle
* mobile - @ToDo
Expand Down Expand Up @@ -271,6 +272,14 @@ You can use this action for a typeahead search that automatically suggests relev

### Page Content

#### getPageHtml

```php
$wiki = new MediaWiki();
$params = ['title' => 'Jupiter'];
$res = $wiki->pageContent()->getHtml($params);
```

#### getPageSummary

```php
Expand Down
22 changes: 22 additions & 0 deletions src/DTO/Requests/PageHtmlRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace MediawikiSdkPhp\DTO\Requests;

class PageHtmlRequest extends PageRequest
{
/**
* Requests for redirect pages return HTTP 302 with a redirect target in Location header and content in the body.
* To get a 200 response instead, supply false to the redirect parameter.
*
* @var bool|null
*/
public ?bool $redirect;

/**
* Whether to temporary stash data-parsoid in order to support transforming the modified content later.
* If this parameter is set, requests are rate-limited on a per-client basis (max 5 requests per second per client)
*
* @var bool|null
*/
public ?bool $stash;
}
35 changes: 35 additions & 0 deletions src/Resources/WikiMedia/PageContentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MediawikiSdkPhp\Resources\WikiMedia;

use JsonException;
use MediawikiSdkPhp\DTO\Requests\PageHtmlRequest;
use MediawikiSdkPhp\DTO\Requests\PageRequest;
use MediawikiSdkPhp\DTO\Responses\GetPageSummary;
use MediawikiSdkPhp\DTO\Responses\GetPageTitlesList;
Expand Down Expand Up @@ -39,4 +40,38 @@ public function getTitle(array $params): GetPageTitlesList
$url = "page/title/{$params['title']}";
return $this->adapter->handle('get', $url, GetPageTitlesList::class);
}

/**
* @param array $params
* @return string
* @throws MediaWikiException
*/
public function getHtml(array $params): string
{
$this->validateParams(PageHtmlRequest::class, $params);

$url = "page/html/{$params['title']}";
unset($params['title']);

if (!empty($params)){
$queryParams = http_build_query($params);
$url .= "?{$queryParams}";
}

$error = [];

$response = $this->adapter->get($url);
$data = $response->toArray();
$status = $response->getStatusCode();

if ($status !== 200){
$error = $this->adapter->generateError($data, $status);
}

if ($error) {
throw new MediaWikiException($error);
}

return $response->getContent();
}
}
24 changes: 24 additions & 0 deletions tests/Resources/PageContentResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ public function testGetTitleNotFound(): void

$this->wiki->pageContent()->getTitle($params);
}

public function testGetHtmlSuccess()
{
$params = [
'title' => 'Main_Page',
'redirect' => true,
'stash' => false,
];

$response = $this->wiki->pageContent()->getHtml($params);

$this->assertStringContainsString('html', $response);
}


public function testGetHtmlNotFound()
{
$params = ['title' => 'qwe123qwe123'];
$this->expectException(MediaWikiException::class);
$this->expectExceptionCode(404);
$this->expectExceptionMessage('Page or revision not found.');

$this->wiki->pageContent()->getHtml($params);
}
}


Expand Down