diff --git a/README.md b/README.md index 57f1da8..d1a97e7 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ COMMONS_HOST="https://commons.wikimedia.org/" ### Wikimedia REST API * pageContent + * getPageHtml * getPageSummary * getPageTitle * mobile - @ToDo @@ -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 diff --git a/src/DTO/Requests/PageHtmlRequest.php b/src/DTO/Requests/PageHtmlRequest.php new file mode 100644 index 0000000..2983caa --- /dev/null +++ b/src/DTO/Requests/PageHtmlRequest.php @@ -0,0 +1,22 @@ +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(); + } } diff --git a/tests/Resources/PageContentResourceTest.php b/tests/Resources/PageContentResourceTest.php index cde26f6..c63991e 100644 --- a/tests/Resources/PageContentResourceTest.php +++ b/tests/Resources/PageContentResourceTest.php @@ -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); + } }