Wikia

RuneScape Wiki

Catcrewser/RSHiscores

Discuss3
22,490pages
on this wiki

< User:Catcrewser

RSHiscores returns the hiscore result from RuneScape's Hiscores Lite preformatted to return the level, experience or rank of any skill/activity for use by templates, StringFunctions, bots, or JavaScript.

This extension was written by TehKittyCat, but with much review and feedback by Quarenon and is released under GPLv3+.

The code is now also located in Wikia's source code at http://trac.wikia-code.com/browser/wikia/trunk/extensions/3rdparty/RSHighscores.

Note: Depending on how soon Wikia responds to my updates, this page will have the most up-to-date code, but will not necessarily be what is running on the wiki.

Contents

Changes Edit

2.0.7 - 2011/04/02 Edit

  • Added a debug mode activated with '!' as final parameter, this is for testing of connectivity of Wikia to the outside and to RuneScape. This should allow for determining the issues with RSHiscores that only happen on Wikia.
  • If H<*> is returned, then this is debug mode, read the source to figure out the meaning.

2.0.6 - 2011/02/07 Edit

  • If an error occurs for a user it is now consistently propagated instead of wasting an explode and key exists and then returning a different error. In other words, if a player doesn't exist, it always returns doesn't exist instead of a doesn't exist, then for the rest invalid skill/type.

2.0.5 - 2011/02/06 Edit

  • Quick release to add better handling of HTTP status codes, also since 2.0.4 never went live redid the status codes again to maintain consistency with the new error code.
  • If A is returned, then no name was entered.
  • If B is returned, then the player could not be found.
  • If C is returned, then a curl error occurred, if it in form of C<#>, check the number here for the cause.
  • If D<#> is returned, then an unexpected HTTP status was returned, check the number here for the cause.
  • If E is returned, then the hiscores parser function limit was reached.
  • If F is returned, then the skill does not exist.
  • If G is returned, then the type does not exist.

2.0.4 - 2011/02/06 Edit

  • Redid error codes as letters to prevent confusion that they are the desired result and to make curl error codes consistent.
  • If A is returned, then no name was entered.
  • If B is returned, then the player could not be found.
  • If C is returned, then a curl error occurred, if it in form of C<#>, check the number here for the cause.
  • If D is returned, then the hiscores parser function limit was reached.
  • If E is returned, then the skill does not exist.
  • If F is returned, then the type does not exist.
  • Added curl error codes, so wikia-side problems can be easily found.

2.0.3 - 2010/04/08 Edit

  • Fixed edge case where if the key right after the last skill was chosen, then nothing would be returned.

2.0.2 - 2010/04/06 Edit

  • Fixed three potential PHP warnings.
  • Added two more error codes related to the PHP warnings fixed.
  • If 4 is returned, then the skill does not exist.
  • If 5 is returned, then the type does not exist.

2.0.1 - 2010/04/03 Edit

  • Renamed parser function for final time to {{#hs}} as this is easier to remember, faster to write, and most other parser functions are 2-3 characters.
  • Moved functionality of ParseHiscoreData to the extension, however, a Template:Hs will be created to convert skill and type names to integers; this allows the extension to be forward compatible with new skills and fields immediately rather than waiting for new versions of the extension.
  • Actually fixed the problems with whitespace; this problem was caused by newline characters and was fixed by the ParseHiscoreData integration.
  • Renamed most remaining references to RSHighscores to RSHiscores.

2.0 - 2010/03/30 Edit

  • The limit has been bumped to two by default, which will allow comparing two players.
  • An cache has been added, so repeated calls can be used.
  • The broken response codes have been replaced by:
  • If 0 is returned, then no name was entered.
  • If 1 is returned, then the player could not be found.
  • If 2 is returned, then an error occurred.
  • If 3 is returned, then the highscores parser function limit was reached.
  • Changed naming from RSHighscores to RSHiscores to be consistent with Jagex.
  • Removes the extra whitespace on the end of the raw string now.
  • Due to the amount of incompatible changes and for naming consistency with Jagex, the parser function is renamed {{#hiscore}}.

Example Edit

  • {{#hs:TehKitty-Cat|1|1}} will return a integer similar to 75, which is the integer returned for my attack level by RuneScape's Hiscores Lite.
  • If A is returned, then no name was entered.
  • If B is returned, then the player could not be found.
  • If C is returned, then a curl error occurred, if it in form of C<#>, check the number here for the cause.
  • If D<#> is returned, then an unexpected HTTP status was returned, check the number here for the cause.
  • If E is returned, then the hiscores parser function limit was reached.
  • If F is returned, then the skill does not exist.
  • If G is returned, then the type does not exist.
  • If H<*> is returned, then this is debug mode, read the source to figure out the meaning.

Configuration/Installation Edit

  1. Save the code below as RSHiscores.php to the extensions directory of your MediaWiki install.
  2. Add the following code to LocalSettings.php:
// RSHiscores
require_once('extensions/RSHiscores.php');
// You may set $wgRSLimit in LocalSettings.php to adjust the max number of calls allowed. Setting to 0 allows unlimited calls.
// If more than $wgRSLimit calls are made, then 3 is returned for the calls over the limit.
$wgRSLimit = 2; // Only allow 2 calls to {{#hs}}
3. Test out the example above on your wiki, optionally you can also run the tests below.

Code Edit

<?php
/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
# Only execute extension through MediaWiki
if ( !defined( 'MEDIAWIKI' ) ) die();
 
# Define a setup function
$wgHooks['ParserFirstCallInit'][] = 'wfHiscores';
$wgExtensionCredits['parserhook'][] = array(
    'name' => 'RSHiscores',
    'version' => '2.0.7',
    'description' => 'A parser function returning raw player data from RuneScape\'s Hiscores Lite',
    'url' => 'http://runescape.wikia.com/wiki/User:Catcrewser/RSHiscores',
    'author' => '[http://runescape.wikia.com/wiki/User_talk:Catcrewser TehKittyCat]'
);
 
# Set limit to prevent abuse, defaults to two, which allows for comparison of hiscore data
if( !isset( $wgRSLimit ) ) $wgRSLimit = 2;
$wgRSTimes = 0;
 
# Cache of hiscore fetches
$wgRSHiscoreCache = array();
 
# Initialise the parser function
$wgHooks['LanguageGetMagic'][] = 'wfHiscores_Magic';
 
# Setup parser function 
function wfHiscores( &$parser ) {
    $parser->setFunctionHook( 'hs', 'wfHiscores_Render' );
     return true;
}
 
# Parser function
function wfHiscores_Magic( &$magicWords ) {
    $magicWords['hs'] = array( 0, 'hs' );
    return true;
}
 
#Skills: 0-Overall(Default), 1-Attack, 2-Defence, 3-Strength, 4-Constitution(formerly Hitpoints), 5-Ranged, 6-Prayer, 7-Magic, 8-Cooking, 9-Woodcutting, 10-Fletching, 11-Fishing,
# 12-Firemaking, 13-Crafting, 14-Smithing, 15-Mining, 16-Herblore, 17-Agility, 18-Thieving, 19-Slayer, 20-Farming, 21-Runecrafting, 22-Hunter,
# 23-Construction, 24-Summoning, 25-Dungeoneering, 26-Duel Tournament, 27-Bounty Hunter, 28-Bounty Hunter Rogue, 29-Fist of Guthix, 30-Mobilising Armies,
# 31-B.A. Attacker, 32-B.A. Defender, 33-B.A. Collector, 34-B.A. Healer, 35-Castle Wars, 36-Conquest
#Types: 0-Rank, 1-Level(Default), 2-Experience

# Function for the parser function
function wfHiscores_Render( &$parser, $player = '', $skill = 0, $type = 1, $debug = false ) {
    global $wgRSch, $wgRSHiscoreCache, $wgRSLimit, $wgRSTimes, $wgHTTPTimeout;
    if( $debug != '!' ) {
	    $player = trim( $player );
	    if( $player == '' ) {
	        return 'A'; # No (display)name entered
	    } elseif( array_key_exists( $player, $wgRSHiscoreCache ) ) {
	        $data = $wgRSHiscoreCache[$player];
	        # Check to see if an error has already occurred, if so then return the error, otherwise will return wrong error and waste a bit of resource.
	        # Checks first char as some errors have integer statuses.
	        if( ctype_alpha ( $data{0} ) ) return $data;
	        $data = explode( "\n", rtrim($data), $skill+2 );
	        if( !array_key_exists( $skill, $data ) ) return 'F'; # Non-existant skill
	        $data = explode( ',', $data[$skill], $type+2 );
	        if( !array_key_exists( $type, $data ) ) return 'G'; # Non-existant type
	        return $data[$type];
	    } elseif( $wgRSTimes < $wgRSLimit || $wgRSLimit == 0 ) {
	        $wgRSTimes++;
	        if( !isset( $wgRSch ) ) {
	            # Setup cURL
	            $wgRSch = curl_init();
	            curl_setopt( $wgRSch, CURLOPT_TIMEOUT, $wgHTTPTimeout );
	            curl_setopt( $wgRSch, CURLOPT_RETURNTRANSFER, TRUE );
	        }
	        # Other known working URL: 'http://hiscore.runescape.com/index_lite.ws?player='
	        curl_setopt( $wgRSch, CURLOPT_URL, 'http://services.runescape.com/m=hiscore/index_lite.ws?player='.urlencode( $player ) );
	        if( $data = curl_exec( $wgRSch ) ) {
	            $wgRSHiscoreCache[$player] = $data;
	            $status = curl_getinfo( $wgRSch, CURLINFO_HTTP_CODE );
	            if( $status == 200 ) {
	                $data = $wgRSHiscoreCache[$player];
	                $data = explode( "\n", $data, $skill+2 );
	                if( !array_key_exists( $skill, $data ) ) return 'F'; # Non-existant skill
	                $data = explode( ',', $data[$skill], $type+2 );
	                if( !array_key_exists( $type, $data ) ) return 'G'; # Non-existant type
	                return $data[$type];
	            } elseif( $status == 404 ) {
	                return $wgRSHiscoreCache[$player] = 'B'; # Non-existant player
	            }
	            # Unexpected HTTP status code
	            return $wgRSHiscoreCache[$player] = 'D'.$status;
	        }
	        # An unhandled curl error occurred, report it.
	        $errno = curl_errno ( $wgRSch );
	        if( $errno ) {
	            return $wgRSHiscoreCache[$player] = 'C'.$errno;
	        }
	        # Should be impossible, but odd things happen, so handle it.
	        return $wgRSHiscoreCache[$player] = 'C';
	    } else {
	        return 'E'; # Parser function limit reached.
	    }
    } else {
    	curl_setopt( $wgRSch, CURLOPT_URL, 'http://services.runescape.com/m=hiscore/index_lite.ws?player='.urlencode( $player ) );
    	if( $data = curl_exec( $wgRSch ) ) {
    		$ret = 'H'.$data.'D,'.$wgRSLimit.','.$wgHTTPTimeout.','.curl_getinfo( $wgRSch, CURLINFO_HTTP_CODE ).','.curl_getinfo( $wgRSch, CURLINFO_TOTAL_TIME ).
    		','.curl_getinfo( $wgRSch, CURLINFO_NAMELOOKUP_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_CONNECT_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_PRETRANSFER_TIME ).
    		','.curl_getinfo( $wgRSch, CURLINFO_STARTTRANSFER_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_SPEED_DOWNLOAD ).','.curl_getinfo( $wgRSch, CURLINFO_SPEED_UPLOAD );
    	} else {
    		$ret = 'H'.curl_errno ( $wgRSch ).'E,'.$wgRSLimit.','.$wgHTTPTimeout.','.curl_getinfo( $wgRSch, CURLINFO_HTTP_CODE ).','.curl_getinfo( $wgRSch, CURLINFO_TOTAL_TIME ).
    		','.curl_getinfo( $wgRSch, CURLINFO_NAMELOOKUP_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_CONNECT_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_PRETRANSFER_TIME ).
    		','.curl_getinfo( $wgRSch, CURLINFO_STARTTRANSFER_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_SPEED_DOWNLOAD ).','.curl_getinfo( $wgRSch, CURLINFO_SPEED_UPLOAD );
    	}
    	curl_setopt( $wgRSch, CURLOPT_URL, 'http://www.google.com/' );
    	if( curl_exec( $wgRSch ) ) {
    		$ret .= 'G'.curl_getinfo( $wgRSch, CURLINFO_HTTP_CODE ).','.curl_getinfo( $wgRSch, CURLINFO_TOTAL_TIME ).
    		','.curl_getinfo( $wgRSch, CURLINFO_NAMELOOKUP_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_CONNECT_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_PRETRANSFER_TIME ).
    		','.curl_getinfo( $wgRSch, CURLINFO_STARTTRANSFER_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_SPEED_DOWNLOAD ).','.curl_getinfo( $wgRSch, CURLINFO_SPEED_UPLOAD );
    	} else {
    		$ret .= 'G'.curl_errno ( $wgRSch ).'E,'.curl_getinfo( $wgRSch, CURLINFO_HTTP_CODE ).','.curl_getinfo( $wgRSch, CURLINFO_TOTAL_TIME ).
    		','.curl_getinfo( $wgRSch, CURLINFO_NAMELOOKUP_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_CONNECT_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_PRETRANSFER_TIME ).
    		','.curl_getinfo( $wgRSch, CURLINFO_STARTTRANSFER_TIME ).','.curl_getinfo( $wgRSch, CURLINFO_SPEED_DOWNLOAD ).','.curl_getinfo( $wgRSch, CURLINFO_SPEED_UPLOAD );
    	}
    	return $ret;
    }
}
## If A is returned, then no (display)name was entered.(Enter a username!)
## If B is returned, then the player could not be found.(HTTP 404)
## If C is returned, then an unknown error occurred.(Any response or lack there of HTTP 200/404)
## If C<#> is returned, then an unexpected error occurred, see the curl error codes for more information.(http://curl.haxx.se/libcurl/c/libcurl-errors.html)
## If D<#> is returned, then an unexpected HTTP status was returned, see the HTTP status codes for more information.(http://en.wikipedia.org/wiki/List_of_HTTP_status_codes)
## If E is returned, then the hiscores parser function limit was reached.(By default one, configurable with $wgRSLimit, limit is not affected by same username used repeatedly)
## If F is returned, then the skill does not exist.
## If G is returned, then the type does not exist.
## If H<*> is returned, then this is debug mode.
## If anything else if returned, then it worked and that is the hiscores data.(Yay!)

Tests Edit

Note: Each test must be ran under the defaults individually.

The limit - returns two integers(overall levels) and E(parser limit reached):

{{#hs:Catcrewser07}}
{{#hs:Zezima}}
{{#hs:TehKitty-Cat}}

Empty name handling - returns A(no player entered) twice:

{{#hs:}}
{{#hs: }}

Whitespace handling - returns two integers(overall levels):

{{#hs:TehKitty-Cat }}
{{#hs: Zezima}}

Non-existent user handling - returns B(non-existent player):

{{#hs:TehKitty-Cat123456}}

Template:Hs Edit

{{#hs:{{{1}}}|{{#switch:{{lc:{{{2}}}}} | overall=0 | attack=1 | defence=2 | strength=3 | constitution=4 | hitpoints=4 | ranged=5 | prayer=6 | magic=7 | cooking=8 | woodcutting=9 | fletching=10 | fishing=11 | firemaking=12 | crafting=13 | smithing=14 | mining=15 | herblore=16 | agility=17 | thieving=18 | slayer=19 | farming=20 | runecrafting=21 | hunter=22 | construction=23 | summoning=24 | dungeoneering=25 | duel=26 | bh=27 | bhr=28 | fog=29 | ma=30 | baa=31 | bad=32 | bac=33 | bah=34 | cw=35 | conquest=36 | 0 }}|{{#switch:{{lc:{{{3}}}}} | rank=0 | lvl=1 | exp=2 | 1 }}}}

Files

Add a File
52,806files on the wiki
See all files >

Recent Activity

See more >

Around Wikia's network

Random Wiki