diff --git a/RELEASENOTES.md b/RELEASENOTES.md index b211693..e29d5d5 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -16,6 +16,7 @@ NOTE: [Token names have changed between versions](//www.mediawiki.org/wiki/API:T * If warnings are present in API results E_USER_WARNING errors are triggered * The Request interface and SimpleRequest class have been added * MediawikiApi now has a getRequest and postRequest method +* MediawikiApi now has a getVersion method * Unsuccessful logins now throw a UsageException with extra details ## Version 0.1.2 (25 May 2014) diff --git a/src/MediawikiApi.php b/src/MediawikiApi.php index dc3efec..199614e 100644 --- a/src/MediawikiApi.php +++ b/src/MediawikiApi.php @@ -24,6 +24,11 @@ class MediawikiApi { */ private $session; + /** + * @var string + */ + private $version; + /** * @param string|MediawikiApiClient $client either the url or the api or * @param MediawikiSession|null $session Inject a custom session here @@ -247,4 +252,23 @@ public function clearTokens() { $this->session->clearTokens(); } + /** + * @return string + */ + public function getVersion(){ + if( !isset( $this->version ) ) { + $result = $this->getRequest( new SimpleRequest( 'query', array( + 'meta' => 'siteinfo', + 'continue' => '', + ) ) ); + preg_match( + '/\d+(?:\.\d+)+/', + $result['query']['general']['generator'], + $versionParts + ); + $this->version = $versionParts[0]; + } + return $this->version; + } + } diff --git a/tests/unit/MediawikiApiTest.php b/tests/unit/MediawikiApiTest.php index 87efd68..6238101 100644 --- a/tests/unit/MediawikiApiTest.php +++ b/tests/unit/MediawikiApiTest.php @@ -255,4 +255,31 @@ public function testLogoutOnFailure() { $this->assertFalse( $api->logout( ) ); } + /** + * @dataProvider provideVersions + */ + public function testGetVersion( $apiValue, $expectedVersion ) { + $client = $this->getMockClient(); + $client->expects( $this->exactly( 1 ) ) + ->method( 'getAction' ) + ->with( array( 'action' => 'query', 'meta' => 'siteinfo', 'continue' => '' ) ) + ->will( $this->returnValue( array( + 'query' => array( + 'general' => array( + 'generator' => $apiValue, + ), + ), + ) ) ); + $api = new MediawikiApi( $client ); + $this->assertEquals( $expectedVersion, $api->getVersion() ); + } + + public function provideVersions() { + return array( + array( 'MediaWiki 1.25wmf13', '1.25' ), + array( 'MediaWiki 1.24.1', '1.24.1' ), + array( 'MediaWiki 1.19', '1.19' ), + array( 'MediaWiki 1.0.0', '1.0.0' ), + ); + } } \ No newline at end of file