Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adds $config param for MediaWiki class constructor
  • Loading branch information
vedmaka committed Nov 9, 2022
commit e5a6784c1697b1700450132575082e7a362d598b
9 changes: 7 additions & 2 deletions src/Client/Action/ActionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ class ActionApi implements Requester, LoggerAwareInterface {

private LoggerInterface $logger;

private array $config;

/**
* @param string $apiUrl The API Url
* @param AuthMethod|null $auth Auth method to use. null for NoAuth
* @param ClientInterface|null $client Guzzle Client
* @param Tokens|null $tokens Inject a custom tokens object here
* @param array $config ClientInterface compatible configuration array
*/
public function __construct(
string $apiUrl,
AuthMethod $auth = null,
?ClientInterface $client = null,
Tokens $tokens = null
Tokens $tokens = null,
array $config = []
) {
if ( $auth === null ) {
$auth = new NoAuth();
Expand All @@ -61,6 +65,7 @@ public function __construct(
$this->auth = $auth;
$this->client = $client;
$this->tokens = $tokens;
$this->config = $config;

$this->logger = new NullLogger();
}
Expand All @@ -71,7 +76,7 @@ public function getApiUrl(): string {

private function getClient(): ClientInterface {
if ( !$this->client instanceof ClientInterface ) {
$clientFactory = new ClientFactory();
$clientFactory = new ClientFactory( $this->config );
$clientFactory->setLogger( $this->logger );
$this->client = $clientFactory->getClient();
}
Expand Down
19 changes: 12 additions & 7 deletions src/Client/MediaWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,43 @@ class MediaWiki {

private RestApi $rest;

public function __construct( string $baseUrl, AuthMethod $auth = null ) {
private array $config;

public function __construct( string $baseUrl, AuthMethod $auth = null, array $config = [] ) {
if ( $auth === null ) {
$auth = new NoAuth();
}

$this->baseUrl = $baseUrl;
$this->auth = $auth;
$this->config = $config;
}

/**
* @param string $anApiEndpoint Either the REST or Action API endpoint e.g. https://en.wikipedia.org/w/api.php
* @param AuthMethod|null $auth
* @param array $config ClientInterface compatible configuration array
*/
public static function newFromEndpoint( string $anApiEndpoint, AuthMethod $auth = null ): self {
return new self( self::pruneActionOrRestPhp( $anApiEndpoint ), $auth );
public static function newFromEndpoint( string $anApiEndpoint, AuthMethod $auth = null, array $config = [] ): self {
return new self( self::pruneActionOrRestPhp( $anApiEndpoint ), $auth, $config );
}

private static function pruneActionOrRestPhp( string $url ): string {
return str_replace( 'rest.php', '', str_replace( self::ACTION_PHP, '', $url ) );
return str_replace( 'rest.php', '', str_replace( self::ACTION_PHP, '', $url ) );
}

/**
* @param string $anApiEndpoint A page on a MediaWiki site e.g. https://en.wikipedia.org/wiki/Main_Page
* @param AuthMethod|null $auth
* @param array $config ClientInterface compatible configuration array
*/
public static function newFromPage( string $pageUrl, AuthMethod $auth = null ): self {
return new self( ReallySimpleDiscovery::baseFromPage( $pageUrl ), $auth );
public static function newFromPage( string $pageUrl, AuthMethod $auth = null, array $config = [] ): self {
return new self( ReallySimpleDiscovery::baseFromPage( $pageUrl ), $auth, $config );
}

public function action(): ActionApi {
if ( !isset( $this->action ) ) {
$this->action = new ActionApi( $this->baseUrl . self::ACTION_PHP, $this->auth );
$this->action = new ActionApi( $this->baseUrl . self::ACTION_PHP, $this->auth, $this->config );
}

return $this->action;
Expand Down