diff --git a/README.md b/README.md index 57f1da8..51c8a3c 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,14 @@ COMMONS_HOST="https://commons.wikimedia.org/" * pageContent * getPageSummary * getPageTitle -* mobile - @ToDo +* mobile + * getSections + * getSectionsByRevision + * getSectionsLead + * getSectionsLeadByRevision + * getSectionsRemaining + * getSectionsRemainingByRevision + * getI18n * feed - @ToDo * transforms - @ToDo * math - @ToDo @@ -287,6 +294,64 @@ You can use this action for a typeahead search that automatically suggests relev $res = $wiki->pageContent()->getTitle($params); ``` +### Mobile + +#### getSections + +```php + $wiki = new MediaWiki(); + $params = ['title' => 'Jupiter']; + $res = $wiki->mobile()->getSections($params); +``` + +#### getSectionsByRevision + +```php + $wiki = new MediaWiki(); + $params = ['title' => 'Jupiter', 'revision' => 1124023924]; + $res = $wiki->mobile()->getSectionsByRevision($params); +``` + +#### getSectionsLead + +```php + $wiki = new MediaWiki(); + $params = ['title' => 'Jupiter']; + $res = $wiki->mobile()->getSectionsLead($params); +``` + +#### getSectionsLeadByRevision + +```php + $wiki = new MediaWiki(); + $params = ['title' => 'Jupiter', 'revision' => 1124023924]; + $res = $wiki->mobile()->getSectionsLeadByRevision($params); +``` + +#### getSectionsRemaining + +```php + $wiki = new MediaWiki(); + $params = ['title' => 'Jupiter']; + $res = $wiki->mobile()->getSectionsRemaining($params); +``` + +#### getSectionsRemainingByRevision + +```php + $wiki = new MediaWiki(); + $params = ['title' => 'Jupiter', 'revision' => 1124023924]; + $res = $wiki->mobile()->getSectionsRemainingByRevision($params); +``` + +#### getI18n + +```php + $wiki = new MediaWiki(); + $params = ['type' => 'pcs']; + $res = $wiki->mobile()->getI18n($params); +``` + ## Request parameters validation For validation the package use `spatie/data-transfer-object` and `ekut/spatie-dto-validators`. diff --git a/src/DTO/Requests/GetI18nRequest.php b/src/DTO/Requests/GetI18nRequest.php new file mode 100644 index 0000000..c4f6965 --- /dev/null +++ b/src/DTO/Requests/GetI18nRequest.php @@ -0,0 +1,12 @@ +search; } + public function mobile(): MobileResource + { + if (is_null($this->mobile)) { + $this->mobile = new MobileResource($this->lang); + } + + return $this->mobile; + } + /** * @throws MediaWikiException */ diff --git a/src/Resources/WikiMedia/MobileResource.php b/src/Resources/WikiMedia/MobileResource.php new file mode 100644 index 0000000..9d3d965 --- /dev/null +++ b/src/Resources/WikiMedia/MobileResource.php @@ -0,0 +1,108 @@ +validateParams(PageRequest::class, $params); + + $url = "page/mobile-sections/{$params['title']}"; + + return $this->adapter->handle('get', $url, GetMobile::class); + } + + /** + * @throws MediaWikiException + * @throws JsonException + */ + public function getSectionsByRevision(array $params): mixed + { + $this->validateParams(PageWithRevisionRequest::class, $params); + + $url = "page/mobile-sections/{$params['title']}/{$params['revision']}"; + + return $this->adapter->handle('get', $url, GetMobile::class); + } + + /** + * @throws MediaWikiException + * @throws JsonException + */ + public function getSectionsLead(array $params): mixed + { + $this->validateParams(PageRequest::class, $params); + + $url = "page/mobile-sections-lead/{$params['title']}"; + + return $this->adapter->handle('get', $url, GetMobileLead::class); + } + + /** + * @throws MediaWikiException + * @throws JsonException + */ + public function getSectionsLeadByRevision(array $params): mixed + { + $this->validateParams(PageWithRevisionRequest::class, $params); + + $url = "page/mobile-sections-lead/{$params['title']}/{$params['revision']}"; + + return $this->adapter->handle('get', $url, GetMobileLead::class); + } + + /** + * @throws MediaWikiException + * @throws JsonException + */ + public function getSectionsRemaining(array $params): mixed + { + $this->validateParams(PageRequest::class, $params); + + $url = "page/mobile-sections-remaining/{$params['title']}"; + + return $this->adapter->handle('get', $url, GetMobileRemaining::class); + } + + /** + * @throws MediaWikiException + * @throws JsonException + */ + public function getSectionsRemainingByRevision(array $params): mixed + { + $this->validateParams(PageWithRevisionRequest::class, $params); + + $url = "page/mobile-sections-remaining/{$params['title']}/{$params['revision']}"; + + return $this->adapter->handle('get', $url, GetMobileRemaining::class); + } + + /** + * @throws MediaWikiException + * @throws JsonException + */ + public function getI18n(array $params): mixed + { + $this->validateParams(GetI18nRequest::class, $params); + + $url = "data/i18n/{$params['type']}"; + + return $this->adapter->handle('get', $url, GetI18n::class); + } +} diff --git a/tests/Resources/MobileResourceTest.php b/tests/Resources/MobileResourceTest.php new file mode 100644 index 0000000..46f2158 --- /dev/null +++ b/tests/Resources/MobileResourceTest.php @@ -0,0 +1,141 @@ +wiki = new MediaWiki(); + + parent::setUp(); + } + + public function testGetSections(): void + { + $params = ['title' => 'Jupiter']; + + $response = $this->wiki->mobile()->getSections($params); + + $this->assertInstanceOf(GetMobile::class, $response); + } + + public function testGetSectionsNotFound(): void + { + $params = ['title' => 'hflk;aHF']; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Page or revision not found.'); + + $this->wiki->mobile()->getSections($params); + } + + public function testGetSectionsByRevision(): void + { + $params = ['title' => 'Jupiter', 'revision' => 1124023924]; + + $response = $this->wiki->mobile()->getSectionsByRevision($params); + + $this->assertInstanceOf(GetMobile::class, $response); + } + + public function testGetSectionsByRevisionNotFound(): void + { + $params = ['title' => 'hflk;aHF', 'revision' => 123]; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + + $this->wiki->mobile()->getSectionsByRevision($params); + } + + public function testGetSectionsLead(): void + { + $params = ['title' => 'Jupiter']; + + $response = $this->wiki->mobile()->getSectionsLead($params); + + $this->assertInstanceOf(GetMobileLead::class, $response); + } + + public function testGetSectionsLeadNotFound(): void + { + $params = ['title' => 'hflk;aHF']; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Page or revision not found.'); + + $this->wiki->mobile()->getSectionsLead($params); + } + + public function testGetSectionsLeadByRevision(): void + { + $params = ['title' => 'Jupiter', 'revision' => 1124023924]; + + $response = $this->wiki->mobile()->getSectionsLeadByRevision($params); + + $this->assertInstanceOf(GetMobileLead::class, $response); + } + + public function testGetSectionsLeadByRevisionNotFound(): void + { + $params = ['title' => 'hflk;aHF', 'revision' => 123]; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + + $this->wiki->mobile()->getSectionsLeadByRevision($params); + } + + public function testGetSectionsRemaining(): void + { + $params = ['title' => 'Jupiter']; + + $response = $this->wiki->mobile()->getSectionsRemaining($params); + + $this->assertInstanceOf(GetMobileRemaining::class, $response); + } + + public function testGetSectionsRemainingNotFound(): void + { + $params = ['title' => 'hflk;aHF']; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Page or revision not found.'); + + $this->wiki->mobile()->getSectionsRemaining($params); + } + + public function testGetSectionsRemainingByRevision(): void + { + $params = ['title' => 'Jupiter', 'revision' => 1124023924]; + + $response = $this->wiki->mobile()->getSectionsRemainingByRevision($params); + + $this->assertInstanceOf(GetMobileRemaining::class, $response); + } + + public function testGetSectionsRemainingByRevisionNotFound(): void + { + $params = ['title' => 'hflk;aHF', 'revision' => 123]; + $this->expectException(MediaWikiException::class); + $this->expectExceptionCode(404); + + $this->wiki->mobile()->getSectionsRemainingByRevision($params); + } + + public function testGetI18n(): void + { + $params = ['type' => 'pcs']; + + $response = $this->wiki->mobile()->getI18n($params); + + $this->assertInstanceOf(GetI18n::class, $response); + } +}