Skip to content

Commit

Permalink
Semi-backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout Gevaert voor persoonlijk gebruik authored and Wout Gevaert voor persoonlijk gebruik committed Sep 6, 2024
1 parent ea821ca commit aa8171d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
6 changes: 2 additions & 4 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
"rshiscores-error-empty-rsn": "No player name entered.",
"rshiscores-error-exceeded-limit": "Too many players requested. No more than $1 are allowed.",
"rshiscores-error-invalid-json": "The highscores endpoint returned unexpected results.",
"rshiscores-error-invalid-skill": "Skill parameter must not be a number, but a skill/activity name (e.g. 'cooking').",
"rshiscores-error-invalid-type": "Type parameter must not be a number, but a type name (xp/rank/score/level).",
"rshiscores-error-previous": "See previous error.",
"rshiscores-error-request-failed": "Failed to retrieve player data. Try again later.",
"rshiscores-error-unexpected-value": "The value for this skill could not be parsed.",
"rshiscores-error-unknown-api": "The API requested does not exist.",
"rshiscores-error-unknown-player": "Player '$1' does not exist.",
"rshiscores-error-unknown-skill": "The skill requested does not exist.",
"rshiscores-error-unknown-type": "The type requested does not exist."
"rshiscores-error-unknown-skill": "The skill or activity requested does not exist.",
"rshiscores-error-unknown-type": "The type requested does not exist for this skill or activity."
}
2 changes: 0 additions & 2 deletions i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"rshiscores-error-empty-rsn": "Error message for missing player name.",
"rshiscores-error-exceeded-limit": "Error message for when $wgRSHiScoresNameLimit is exceeded.",
"rshiscores-error-invalid-json": "Error message when Jagex endpoint returned unexpected json format.",
"rshiscores-error-invalid-skill": "Error message for when the skill parameter provided is numeric. That was old behaviour.",
"rshiscores-error-invalid-type": "Error message for when the type parameter provided is numeric. That was old behaviour.",
"rshiscores-error-previous": "Error message for when a previous error has occurred.",
"rshiscores-error-request-failed": "Error message for when the HTTP request failed.",
"rshiscores-error-unexpected-value": "Error message when value returned by the api was not string or integer.",
Expand Down
39 changes: 28 additions & 11 deletions src/RSHiScores.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,26 @@ private static function lookup( $api, $player, $skill, $type ) {

$player = trim( $player );

$skillIsInt = filter_var( $skill, FILTER_VALIDATE_INT ) !== false;
if ( $skillIsInt && $skill < 0 ) {
// Semi-backwards compatible: Previously, skill < 0 meant "get all data"
$skill = self::ALL_SKILLS;
$skillIsInt = false;
}

if ( filter_var( $type, FILTER_VALIDATE_INT ) !== false ) {
// Semi-backwards compatibility: If type is int, they used to refer to these values;
$type = [
'rank',
self::DEFAULT_TYPE,
'xp',
][$type];
}

if( $player === '' ) {
// Error: No player name was entered.
throw new Exception( wfMessage( 'rshiscores-error-empty-rsn' ) );

} elseif ( filter_var( $skill, FILTER_VALIDATE_INT ) !== false ) {
// Error: Skill parameter must not be a number; That was old behaviour
throw new Exception( wfMessage( 'rshiscores-error-invalid-skill' ) );

} elseif ( filter_var( $type, FILTER_VALIDATE_INT ) !== false ) {
// Error: Type parameter must be a number; That was old behaviour
throw new Exception( wfMessage( 'rshiscores-error-invalid-type' ) );

} elseif ( array_key_exists( $api, self::$cache ) && array_key_exists( $player, self::$cache[$api] ) ) {
// Get the HiScores data from the cache.
$data = self::$cache[$api][$player];
Expand Down Expand Up @@ -211,7 +219,7 @@ private static function lookup( $api, $player, $skill, $type ) {
if ( $skill === self::ALL_SKILLS ) {
return json_encode( $data );
} else {
return self::getSingleSkillData( $data, $skill, $type );
return self::getSingleSkillData( $data, $skill, $type, $skillIsInt );
}
}

Expand Down Expand Up @@ -270,14 +278,23 @@ private static function escapeStrings( $arrayOrString ) {
* @param array $data The data fetched from the endpoint, and processed by self::postFetch
* @param string $skill The skill to search for in the data.
* @param string $type The type (xp/rank/score/level/self::DEFAULT_TYPE) of data to get for the skill.
* @param bool $fromInt Should $skill be interpreted as an integer?
*
* @return string The requested data
*
* @throws Exception If $skill or $type could not be found, or if endpoint returned unexpected results
*/
private static function getSingleSkillData( $data, $skill, $type ) {
private static function getSingleSkillData( $data, $skill, $type, $fromInt ) {
// Case-insensitive, use same processing as self::postFetch did
$skill = self::escapeStrings( strtolower( $skill ) );
if ( $fromInt ) {
if ( (int) $skill >= count($data) ) {
throw new Exception( wfMessage( 'rshiscores-error-unknown-skill' ) );
}
// Semi-backwards compatibility: If skill is integer "n", take the "n"th element of the data.
$skill = array_keys( $data )[ $skill ];
} else {
$skill = self::escapeStrings( strtolower( $skill ) );
}
if ( !isset( $data[ $skill ] ) ) {
// Error: Skill/activity is unknown. Maybe they changed the spelling?
throw new Exception( wfMessage( 'rshiscores-error-unknown-skill' ) );
Expand Down

0 comments on commit aa8171d

Please sign in to comment.