FANDOM


-- <pre>
 
local p = {}
 
-- Some of these are guest-imates. Update them if a more specific version min/max is known
local _versionToYear = {
    { year=0, min="0.0", max="99.99" },
};
 
-- http://stackoverflow.com/a/1579673/1411473
local function split(pString, sep)
        local sep, fields = sep or ":", {}
        local pattern = string.format("([^%s]+)", sep)
        pString:gsub(pattern, function(c) fields[#fields+1] = c end)
        return fields
end
 
-- http://phpjs.org/functions/version_compare/
-- Simulate PHP version_compare
function p.version_compare (v1, v2, operator)
	--       discuss at: http://phpjs.org/functions/version_compare/
	--      original by: Philippe Jausions (http://pear.php.net/user/jausions)
	--      original by: Aidan Lister (http://aidanlister.com/)
	--      reimplemented by: Kankrelune (http://www.webfaktory.info/)
	--      improved by: Brett Zamir (http://brett-zamir.me)
	--      improved by: Scott Baker
	--      improved by: Theriault
	--        example 1: version_compare('8.2.5rc', '8.2.5a');
	--        returns 1: 1
	--        example 2: version_compare('8.2.50', '8.2.52', '<');
	--        returns 2: true
	--        example 3: version_compare('5.3.0-dev', '5.3.0');
	--        returns 3: -1
	--        example 4: version_compare('4.1.0.52','4.01.0.51');
	--        returns 4: 1
	local i = 0
	local x = 0
	local compare = 0
	-- Leave as negatives so they can come before numerical versions
	local vm = { ['a']= 1, ['b']= 2, ['c']= 3, ['d']= 4, ['e']= 5 }
	--local vm = { ['dev']= -6, ['alpha']= -5, ['a']= -5, ['beta']= -4, ['b']= -4, ['RC']= -3, ['rc']= -3, ['#']= -2, ['p']= 1, ['pl']= 1 }
 
	-- Format version string to remove oddities.
	local function prepVersion (v)
		v = string.gsub( (''..v), '[_\-+]', '.')
	    v = string.gsub(v, "([^.%d]+)", function(a) return '.'..a..'.' end)
		v = string.gsub(v, "\.{2,}", '.')
		return (string.len(v) <= 0 and {8} or split(v, '.'))
	end
 
	-- This converts a version component to a number.
	local function numVersion (v)
		return not v and 0 or (tonumber(v)==nil and (vm[v] or 7) or tonumber(v))
	end
	local v1 = prepVersion(v1)
	local v2 = prepVersion(v2)
	local x = math.max(#v1, #v2)
	for i = 1, x do
		if v1[i] ~= v2[i] then
    		mw.log(v1[i], operator, v2[i]);
    		v1[i] = numVersion(v1[i]);
    		v2[i] = numVersion(v2[i]);
    		mw.log(v1[i], operator, v2[i]);
    		if v1[i] < v2[i] then
    		    compare = -1
    		    break
    		elseif v1[i] > v2[i] then
    		    compare = 1
    		    break
            end
		end
	end
	if not operator then return compare < 0 end
 
	if operator == '>' then         return (compare > 0)
	elseif operator == '>=' then    return (compare >= 0)
	elseif operator == '<' then     return (compare < 0)
	elseif operator == '<=' then    return (compare <= 0)
	elseif operator == '=' or operator == '==' then    return (compare == 0)
	elseif operator == '~=' then    return (compare ~= 0)
	else return nil
	end
end
 
function p._versionLink(version)
    -- If the number returns as less than zero, there is some kinda error.
    if p.version_compare(version, 0, "<") then
        return version
    end
 
    local link = ""
    for i, obj in ipairs(_versionToYear) do
        if p.version_compare(version, obj.min, ">=") and p.version_compare(version, obj.max, "<=") then
            link = "Game_updates/"..obj.year
            break
        end
    end
    return link == "" and "V"..version or "[["..link.."#"..version.."|V"..version.."]]"
end
 
function p.versionLink(frame)
    local pArgs = frame:getParent().args
    local v = pArgs[1]
 
    return p._versionLink(v)
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.