From 9584b2217b555047528e7ca2907a49c707842e97 Mon Sep 17 00:00:00 2001 From: erbolat_bit Date: Sat, 12 Nov 2022 17:30:17 +0500 Subject: [PATCH 01/11] LEARN-POEMS-T-35 add page html request --- src/DTO/Requests/PageHtmlRequest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/DTO/Requests/PageHtmlRequest.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 @@ + Date: Sat, 12 Nov 2022 17:31:11 +0500 Subject: [PATCH 02/11] LEARN-POEMS-T-35 add html method to PageContentResource --- .../WikiMedia/PageContentResource.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Resources/WikiMedia/PageContentResource.php b/src/Resources/WikiMedia/PageContentResource.php index 8819bbf..93b4b05 100644 --- a/src/Resources/WikiMedia/PageContentResource.php +++ b/src/Resources/WikiMedia/PageContentResource.php @@ -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; @@ -26,6 +27,36 @@ public function getSummary(array $params): mixed return $this->adapter->handle('get', $url, GetPageSummary::class); } + /** + * @param array $params + * @return string + * @throws MediaWikiException + */ + public function html(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}"; + } + + $response = $this->adapter->get($url); + + if ($response->getStatusCode() !== 200){ + throw new MediaWikiException([ + 'message' => 'Not found', + 'reason' => 'Nothing found by this name', + 'code' => 404, + ]); + } + + return $response->getContent(); + } + /** * @param array $params * @return GetPageTitlesList From 8d73f181b8b08d6a28918a42a2bd1566258d451b Mon Sep 17 00:00:00 2001 From: erbolat_bit Date: Sat, 12 Nov 2022 17:31:34 +0500 Subject: [PATCH 03/11] LEARN-POEMS-T-35 add test for page content html --- tests/Resources/PageContentHtmlTest.php | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/Resources/PageContentHtmlTest.php diff --git a/tests/Resources/PageContentHtmlTest.php b/tests/Resources/PageContentHtmlTest.php new file mode 100644 index 0000000..3f54a59 --- /dev/null +++ b/tests/Resources/PageContentHtmlTest.php @@ -0,0 +1,41 @@ +wiki = new MediaWiki(); + + parent::setUp(); + } + + public function testGetSuccess() + { + $params = [ + 'title' => 'Main_Page', + 'redirect' => true, + 'stash' => false, + ]; + + $response = $this->wiki->pageContent()->html($params); + + $this->assertStringContainsString('html', $response); + } + + + public function testGetNotFound() + { + $params = ['title' => 'qwe123qwe123']; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Not found'); + + $this->wiki->pageContent()->html($params); + } +} \ No newline at end of file From a7ca2f91613d2cc6904eaba76a5aca17f336d24a Mon Sep 17 00:00:00 2001 From: ekut Date: Tue, 15 Nov 2022 14:11:44 +0800 Subject: [PATCH 04/11] LEARN-POEMS-T-45. Reorganize tests for the PageContentResource. --- README.md | 9 ++++ .../WikiMedia/PageContentResource.php | 2 +- tests/Resources/PageContentHtmlTest.php | 41 ------------------- tests/Resources/PageContentResourceTest.php | 33 +++++++++++++++ 4 files changed, 43 insertions(+), 42 deletions(-) delete mode 100644 tests/Resources/PageContentHtmlTest.php 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/Resources/WikiMedia/PageContentResource.php b/src/Resources/WikiMedia/PageContentResource.php index 93b4b05..864b2d5 100644 --- a/src/Resources/WikiMedia/PageContentResource.php +++ b/src/Resources/WikiMedia/PageContentResource.php @@ -32,7 +32,7 @@ public function getSummary(array $params): mixed * @return string * @throws MediaWikiException */ - public function html(array $params): string + public function getHtml(array $params): string { $this->validateParams(PageHtmlRequest::class, $params); diff --git a/tests/Resources/PageContentHtmlTest.php b/tests/Resources/PageContentHtmlTest.php deleted file mode 100644 index 3f54a59..0000000 --- a/tests/Resources/PageContentHtmlTest.php +++ /dev/null @@ -1,41 +0,0 @@ -wiki = new MediaWiki(); - - parent::setUp(); - } - - public function testGetSuccess() - { - $params = [ - 'title' => 'Main_Page', - 'redirect' => true, - 'stash' => false, - ]; - - $response = $this->wiki->pageContent()->html($params); - - $this->assertStringContainsString('html', $response); - } - - - public function testGetNotFound() - { - $params = ['title' => 'qwe123qwe123']; - $this->expectException(MediaWikiException::class); - $this->expectExceptionCode(404); - $this->expectExceptionMessage('Not found'); - - $this->wiki->pageContent()->html($params); - } -} \ No newline at end of file diff --git a/tests/Resources/PageContentResourceTest.php b/tests/Resources/PageContentResourceTest.php index cde26f6..0613883 100644 --- a/tests/Resources/PageContentResourceTest.php +++ b/tests/Resources/PageContentResourceTest.php @@ -67,6 +67,39 @@ public function testGetTitleNotFound(): void $this->wiki->pageContent()->getTitle($params); } + + + /** + * @return void + * @throws MediaWikiException + */ + public function testGetHtmlSuccess(): void + { + $params = [ + 'title' => 'Jupiter', + 'redirect' => true, + 'stash' => false, + ]; + + $response = $this->wiki->pageContent()->getHtml($params); + + $this->assertStringContainsString('html', $response); + } + + + /** + * @return void + * @throws MediaWikiException + */ + public function testGetHtmlNotFound(): void + { + $params = ['title' => 'qwe123qwe123']; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Not found'); + + $this->wiki->pageContent()->getHtml($params); + } } From 9a5334adebaefa2f688d80b3ebb5e483c5874a47 Mon Sep 17 00:00:00 2001 From: erbolat_bit Date: Sat, 12 Nov 2022 17:30:17 +0500 Subject: [PATCH 05/11] LEARN-POEMS-T-35 add page html request --- src/DTO/Requests/PageHtmlRequest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/DTO/Requests/PageHtmlRequest.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 @@ + Date: Sat, 12 Nov 2022 17:31:11 +0500 Subject: [PATCH 06/11] LEARN-POEMS-T-35 add html method to PageContentResource --- .../WikiMedia/PageContentResource.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Resources/WikiMedia/PageContentResource.php b/src/Resources/WikiMedia/PageContentResource.php index 8819bbf..e5e8756 100644 --- a/src/Resources/WikiMedia/PageContentResource.php +++ b/src/Resources/WikiMedia/PageContentResource.php @@ -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; @@ -39,4 +40,34 @@ 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}"; + } + + $response = $this->adapter->get($url); + + if ($response->getStatusCode() !== 200){ + throw new MediaWikiException([ + 'message' => 'Not found', + 'reason' => 'Nothing found by this name', + 'code' => 404, + ]); + } + + return $response->getContent(); + } } From 2f6e871b67cac8c86be0f89373dce6d9c8e55530 Mon Sep 17 00:00:00 2001 From: erbolat_bit Date: Sat, 12 Nov 2022 17:31:34 +0500 Subject: [PATCH 07/11] LEARN-POEMS-T-35 add test for page content html --- tests/Resources/PageContentHtmlTest.php | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/Resources/PageContentHtmlTest.php diff --git a/tests/Resources/PageContentHtmlTest.php b/tests/Resources/PageContentHtmlTest.php new file mode 100644 index 0000000..3f54a59 --- /dev/null +++ b/tests/Resources/PageContentHtmlTest.php @@ -0,0 +1,41 @@ +wiki = new MediaWiki(); + + parent::setUp(); + } + + public function testGetSuccess() + { + $params = [ + 'title' => 'Main_Page', + 'redirect' => true, + 'stash' => false, + ]; + + $response = $this->wiki->pageContent()->html($params); + + $this->assertStringContainsString('html', $response); + } + + + public function testGetNotFound() + { + $params = ['title' => 'qwe123qwe123']; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Not found'); + + $this->wiki->pageContent()->html($params); + } +} \ No newline at end of file From ee66a3c864a06b60be16eb1a703eb83f694fd5ff Mon Sep 17 00:00:00 2001 From: erbolat_bit Date: Sun, 27 Nov 2022 10:03:37 +0500 Subject: [PATCH 08/11] LEARN-POEMS-T-35: change page content html response --- src/Resources/WikiMedia/PageContentResource.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Resources/WikiMedia/PageContentResource.php b/src/Resources/WikiMedia/PageContentResource.php index e5e8756..54a9b91 100644 --- a/src/Resources/WikiMedia/PageContentResource.php +++ b/src/Resources/WikiMedia/PageContentResource.php @@ -58,14 +58,18 @@ public function getHtml(array $params): string $url .= "?{$queryParams}"; } + $error = []; + $response = $this->adapter->get($url); + $data = $response->toArray(); + $status = $response->getStatusCode(); + + if ($status !== 200){ + $error = $this->adapter->generateError($data, $status); + } - if ($response->getStatusCode() !== 200){ - throw new MediaWikiException([ - 'message' => 'Not found', - 'reason' => 'Nothing found by this name', - 'code' => 404, - ]); + if ($error) { + throw new MediaWikiException($error); } return $response->getContent(); From 0e8aa31fb0e590b68bbd1b4a176996df11c96999 Mon Sep 17 00:00:00 2001 From: erbolat_bit Date: Sun, 27 Nov 2022 10:07:53 +0500 Subject: [PATCH 09/11] LEARN-POEMS-T-35: tests for page content html --- tests/Resources/PageContentHtmlTest.php | 41 --------------------- tests/Resources/PageContentResourceTest.php | 24 ++++++++++++ 2 files changed, 24 insertions(+), 41 deletions(-) delete mode 100644 tests/Resources/PageContentHtmlTest.php diff --git a/tests/Resources/PageContentHtmlTest.php b/tests/Resources/PageContentHtmlTest.php deleted file mode 100644 index 3f54a59..0000000 --- a/tests/Resources/PageContentHtmlTest.php +++ /dev/null @@ -1,41 +0,0 @@ -wiki = new MediaWiki(); - - parent::setUp(); - } - - public function testGetSuccess() - { - $params = [ - 'title' => 'Main_Page', - 'redirect' => true, - 'stash' => false, - ]; - - $response = $this->wiki->pageContent()->html($params); - - $this->assertStringContainsString('html', $response); - } - - - public function testGetNotFound() - { - $params = ['title' => 'qwe123qwe123']; - $this->expectException(MediaWikiException::class); - $this->expectExceptionCode(404); - $this->expectExceptionMessage('Not found'); - - $this->wiki->pageContent()->html($params); - } -} \ No newline at end of file diff --git a/tests/Resources/PageContentResourceTest.php b/tests/Resources/PageContentResourceTest.php index cde26f6..d3211e2 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()->html($params); + + $this->assertStringContainsString('html', $response); + } + + + public function testGetHtmlNotFound() + { + $params = ['title' => 'qwe123qwe123']; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Not found'); + + $this->wiki->pageContent()->html($params); + } } From d3ebee0ad74caef98dd786f2a4ca9442cd7c413e Mon Sep 17 00:00:00 2001 From: erbolat_bit Date: Sun, 27 Nov 2022 10:26:16 +0500 Subject: [PATCH 10/11] LEARN-POEMS-T-35: fix test bugs --- tests/Resources/PageContentResourceTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Resources/PageContentResourceTest.php b/tests/Resources/PageContentResourceTest.php index d3211e2..c63991e 100644 --- a/tests/Resources/PageContentResourceTest.php +++ b/tests/Resources/PageContentResourceTest.php @@ -76,7 +76,7 @@ public function testGetHtmlSuccess() 'stash' => false, ]; - $response = $this->wiki->pageContent()->html($params); + $response = $this->wiki->pageContent()->getHtml($params); $this->assertStringContainsString('html', $response); } @@ -87,9 +87,9 @@ public function testGetHtmlNotFound() $params = ['title' => 'qwe123qwe123']; $this->expectException(MediaWikiException::class); $this->expectExceptionCode(404); - $this->expectExceptionMessage('Not found'); + $this->expectExceptionMessage('Page or revision not found.'); - $this->wiki->pageContent()->html($params); + $this->wiki->pageContent()->getHtml($params); } } From d4e35c9646bd3fce38036c52b8f4cbe78920173e Mon Sep 17 00:00:00 2001 From: erbolat_bit Date: Sun, 27 Nov 2022 10:30:20 +0500 Subject: [PATCH 11/11] LEARN-POEMS-T-35: bugfix --- .../WikiMedia/PageContentResource.php | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/src/Resources/WikiMedia/PageContentResource.php b/src/Resources/WikiMedia/PageContentResource.php index 2d5974f..54a9b91 100644 --- a/src/Resources/WikiMedia/PageContentResource.php +++ b/src/Resources/WikiMedia/PageContentResource.php @@ -27,36 +27,6 @@ public function getSummary(array $params): mixed return $this->adapter->handle('get', $url, GetPageSummary::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}"; - } - - $response = $this->adapter->get($url); - - if ($response->getStatusCode() !== 200){ - throw new MediaWikiException([ - 'message' => 'Not found', - 'reason' => 'Nothing found by this name', - 'code' => 404, - ]); - } - - return $response->getContent(); - } - /** * @param array $params * @return GetPageTitlesList