Skip to content

Commit

Permalink
Add getVersion method to MediawikiApi
Browse files Browse the repository at this point in the history
  • Loading branch information
addshore committed Jan 13, 2015
1 parent 1d7b0a7 commit 258ccf3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions src/MediawikiApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

}
27 changes: 27 additions & 0 deletions tests/unit/MediawikiApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
);
}
}

0 comments on commit 258ccf3

Please sign in to comment.