Skip to content

Commit

Permalink
Splits extensions and skins parser functions [WIK-1041] (#7)
Browse files Browse the repository at this point in the history
* Only return extensions via parser function

* Fixes indentation

* Adds `skins` and `skin-urls` flags, makes `extensions` and `extension-urls` to only return extensions data
  • Loading branch information
vedmaka authored Nov 5, 2023
1 parent 63d0441 commit 4b2aef3
Show file tree
Hide file tree
Showing 3 changed files with 599 additions and 428 deletions.
141 changes: 120 additions & 21 deletions src/RemoteWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace MediaWiki\Extension\RemoteWiki;

use Addwiki\Mediawiki\Api\Client\Action\Exception\UsageException;
use Addwiki\Mediawiki\Api\Client\Action\Request\ActionRequest;
use Addwiki\Mediawiki\Api\Client\Auth\UserAndPassword;
use Addwiki\Mediawiki\Api\Client\MediaWiki;
Expand Down Expand Up @@ -47,11 +46,12 @@ public function __construct(

/**
* @param Parser $parser
* @param null $endPoint
* @param null $type
*
* @return string
*/
public function remoteVersion( Parser $parser, $endPoint = null, $type = null ): string {

if ( !$this->validateEndpoint( $endPoint ) ) {
return '';
}
Expand All @@ -62,9 +62,15 @@ public function remoteVersion( Parser $parser, $endPoint = null, $type = null ):
case 'extensions':
$result = $this->getExtensionVersions( $api );
break;
case 'skins':
$result = $this->getSkinVersions( $api );
break;
case 'extension-urls':
$result = $this->getExtensionURLs( $api );
break;
case 'skin-urls':
$result = $this->getSkinURLs( $api );
break;
case 'version':
default:
$result = $this->getGenerator( $api );
Expand Down Expand Up @@ -183,6 +189,27 @@ private function getExtensionVersions( MediaWiki $api ): string {
}
}

/**
* Get skin versions
*
* @param MediaWiki $api
* @return string
*/
private function getSkinVersions( MediaWiki $api ): string {
$result = $this->getSkinsInfo( $api );
if ( is_string( $result ) ) {
// There was an error
return $result;
} else if (
is_array( $result )
&& array_key_exists( 'versions', $result )
) {
return $result['versions'];
} else {
throw new LogicException( 'Invalid getSkinsInfo() result' );
}
}

/**
* Get extensions urls
*
Expand All @@ -204,13 +231,99 @@ private function getExtensionURLs( MediaWiki $api ): string {
}
}

/**
* Get skin urls
*
* @param MediaWiki $api
* @return string
*/
private function getSkinURLs( MediaWiki $api ): string {
$result = $this->getSkinsInfo( $api );
if ( is_string( $result ) ) {
// There was an error
return $result;
} else if (
is_array( $result )
&& array_key_exists( 'urls', $result )
) {
return $result['urls'];
} else {
throw new LogicException( 'Invalid getSkinsInfo() result' );
}
}

/**
* Get extensions information (both versions and URLs)
*
* @param MediaWiki $api
* @return array|string
*/
private function getExtensionsInfo( MediaWiki $api ) {
$extensions = $this->getExtensionsData( $api );
if ( empty( $extensions ) ) {
return $this->config->get( 'RemoteWikiVerbose' ) ? 'ERROR: empty extensions response' : '';
}
// generate extension:version pairs and extension:URL pairs
$versions = [];
$urls = [];
foreach ( $extensions as $extension ) {
// filter out skins
if ( isset( $extension['type'] ) && $extension['type'] === 'skin' ) {
continue;
}
$versions[] = $extension['name'] . ':' . ( $extension['version'] ?? $extension['vcs-version'] ?? '?' );
$urls[] = $extension['name'] . ':' . ( $extension['url'] ?? '?' );
}
// URLs cannot be separated by a comma, that is a valid URL
// character, see
// https://datatracker.ietf.org/doc/html/rfc3986#section-2.2
// instead we will use a |, which isn't a valid part of a URL
$result = [
'versions' => implode( ',', $versions ),
'urls' => implode( '|', $urls ),
];
return $result;
}

/**
* Get skins information (both versions and URLs)
*
* @param MediaWiki $api
* @return array|string
*/
private function getSkinsInfo( MediaWiki $api ) {
$extensions = $this->getExtensionsData( $api );
if ( empty( $extensions ) ) {
return $this->config->get( 'RemoteWikiVerbose' ) ? 'ERROR: empty extensions response' : '';
}
// generate extension:version pairs and extension:URL pairs
$versions = [];
$urls = [];
foreach ( $extensions as $extension ) {
// filter out skins
if ( isset( $extension['type'] ) && $extension['type'] !== 'skin' ) {
continue;
}
$versions[] = $extension['name'] . ':' . ( $extension['version'] ?? $extension['vcs-version'] ?? '?' );
$urls[] = $extension['name'] . ':' . ( $extension['url'] ?? '?' );
}
// URLs cannot be separated by a comma, that is a valid URL
// character, see
// https://datatracker.ietf.org/doc/html/rfc3986#section-2.2
// instead we will use a |, which isn't a valid part of a URL
$result = [
'versions' => implode( ',', $versions ),
'urls' => implode( '|', $urls ),
];
return $result;
}

/**
* @param MediaWiki $api
*
* @return array
*/
private function getExtensionsData( MediaWiki $api ) {
$reqKey = $this->cache->makeKey(
$api->action()->getApiUrl(),
'extensions',
Expand All @@ -230,31 +343,17 @@ private function getExtensionsInfo( MediaWiki $api ) {
'siprop' => 'extensions'
]
);

try {
$result = $api->action()->request( $versionReq );
$extensions = $result['query']['extensions'];
if ( empty( $extensions ) ) {
return $this->config->get('RemoteWikiVerbose') ? 'ERROR: empty extensions response' : '';
return $extensions;
}
// generate extension:version pairs and extension:URL pairs
$versions = [];
$urls = [];
foreach ( $extensions as $extension ) {
$versions[] = $extension['name'] . ':' . ( $extension['version'] ?? $extension['vcs-version'] ?? '?' );
$urls[] = $extension['name'] . ':' . ( $extension['url'] ?? '?' );
}
// URLs cannot be separated by a comma, that is a valid URL
// character, see
// https://datatracker.ietf.org/doc/html/rfc3986#section-2.2
// instead we will use a |, which isn't a valid part of a URL
$result = [
'versions' => implode( ',', $versions ),
'urls' => implode( '|', $urls ),
];
$this->cache->set( $reqKey, $result, $cacheTTL );
return $result;
$this->cache->set( $reqKey, $extensions, $cacheTTL );
return $extensions;
} catch ( Exception $e ) {
return $this->config->get('RemoteWikiVerbose') ? $e->getMessage() : '';
return [];
}
}

Expand Down
Loading

0 comments on commit 4b2aef3

Please sign in to comment.