Module:Update

From RuneScape Classic Wiki
Jump to: navigation, search
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Update/doc. [edit]
Module:Update requires Module:Paramtest.
Module:Update requires Module:Top icons.

local p = {}

local paramTest = require('Module:Paramtest')
local topIcons = require('Module:Top icons')._main

local lang = mw.getContentLanguage()
local title = mw.title.getCurrentTitle()

-- Replacement before calling encode as lua patterns
local ReplaceBefore = {
	['%('] = '',
	['%)'] = '',
	['%!'] = '',
}

-- Replacements after calling encode as lua patterns
local ReplaceAfter = {
	['%+'] = '-',
	['%%2C'] = '',
	['%%2F'] = '',
	['%%3F'] = '',
	['%%26%%2338%%3B'] = '',
	['%%E2%%80%%93'] = '',
	['%.'] = '',
	['%%26quot%%3B'] = '',
	['%%E2%%80%%99'] = '',
	['%%26%%2339%%3B'] = '',
	['%%C3%%B6'] = 'o',
	['%%3A'] = '',
}

-- Convert month names to hex for sortkeys
local HexMonthConv = {
	January = '1',
	February = '2',
	March = '3',
	April = '4',
	May = '5',
	June = '6',
	July = '7',
	August = '8',
	September = '9',
	October = 'A',
	November = 'B',
	December = 'C',
}

-- category mapping: {category name, type for use on date pages (eg [[28 November]])
local CategoryInfo = {
	bts = {'Behind the Scenes updates', 'Behind the Scenes'},
	community = {'Community updates', 'Community'},
	devblog = {'Developer Blogs', 'Developer Blog'},
	event = {'Event updates', 'Event update'},
	game = {'Game updates', 'Game update'},
	future = {'Future Updates', 'Future update'},
	mobile = {'Mobile updates', 'Mobile update'},
	shop = {'Shop updates', 'Shop update'},
	support = {'Support updates', 'Support'},
	technical = {'Technical updates', 'Technical'},
	website = {'Website updates', 'Website update'},
	yourfeedback = {'Your Feedback updates', 'Your Feedback'},
	forum = {'Forum post updates', 'Forum post'},
	['#default'] = {'Missing update category', ''},
}
-- input mapping: accepted inputs -> keys for above table
local CategorySwitch = {
	bts = 'bts',
	['behind the scenes'] = 'bts',
	comm = 'community',
	community = 'community',
	dev = 'devblog',
	blog = 'devblog',
	['dev blog'] = 'devblog',
	devblog = 'devblog',
	event = 'event',
	events = 'event',
	game = 'game',
	feedback= 'yourfeedback',
	future = 'future',
	mobile = 'mobile',
	shop = 'shop',
	cs = 'support',
	customer = 'support',
	['customer support'] = 'support',
	support = 'support',
	tech = 'technical',
	technical = 'technical',
	th = 'th',
	site = 'website',
	ws = 'website',
	website = 'website',
	yourfeedback= 'yourfeedback',
	['your feedback'] = 'yourfeedback',
	['#default'] = '#default'
}

-- given a supported 'category' code, return the name of the category
function getUpdateCategory(catArg)
	if(catArg ~= nil) then
		catArg = string.gsub(string.lower(catArg), ' ?updates?', '')
		if CategorySwitch[catArg] and CategoryInfo[CategorySwitch[catArg]] then
			return CategoryInfo[CategorySwitch[catArg]][1]
		else
			return CategoryInfo[CategorySwitch['#default']][1]
		end
	else
		return CategoryInfo[CategorySwitch['#default']][1]
	end
end

-- given d,m,y, return the relevant time-based categories (with sorting)
function getDateCategories(day, month, year)
	local ret = {}
	local hexMonth, dayZero
		
	if((not day) or (not month) or (not HexMonthConv[month]) or (not year)) then
		table.insert(ret, '[[Category:Missing update date]]')
	else
		dayZero = ((tonumber(day) < 10 and '0' .. day) or day)
		hexMonth = HexMonthConv[month]
		-- [[Category:28 November updates]], sorted by year (eg 2015)
		table.insert(ret, string.format('[[Category:%s %s updates|%s%s%s]]', day, month, year, hexMonth, dayZero))
		
		-- [[Category:2015 updates]], sorted by monthday, months converted to hex, eg B28 (28 November)
		table.insert(ret, string.format('[[Category:%s updates|%s%s]]', year, hexMonth, dayZero))
	
		-- [[Category:Updates by date]], sorted year month day
		table.insert(ret, string.format('[[Category:Updates by date|%s%s%s]]', year, hexMonth, dayZero))
	end

	local indicators = {}
	hexMonthNum = tonumber(HexMonthConv[month], 16)
	-- If it is prior to OSRS's intial version date
	if(tonumber(year) < 2008 ) then
		table.insert(indicators, 'os')
	end
	table.insert(indicators, 'rs')
	
	table.insert(ret, topIcons(indicators))
	
	return table.concat(ret)
end

-- [[Template:Update]]
function p.update(frame)
	local args = frame:getParent().args

	local div = mw.html.create('div')
		:css({['display'] = 'table', ['max-width'] = '650px', ['border-radius'] = '3px', ['border'] = '1px solid grey', ['margin'] = 'auto', ['margin-bottom'] = '10px', ['margin-top'] = '10px', ['background'] = '#F5F5F5', ['text-align'] = 'center', ['padding'] = '5px', ['padding-left'] = '10px', ['padding-right'] = '10px' })
		--:addClass('official update')
		:done()
	
	local link
	
	if(args.link ~= nil) then
		if(args.link:lower() == 'no') then
			link = 'official news post'
		else
			link = '[' .. args.link .. ' official news post]'
		end
	else
		link = string.lower(title.baseText)
		for i,v in pairs(ReplaceBefore) do
			link = string.gsub(link, i, v)
		end
		link = mw.uri.encode(link)
		for i,v in pairs(ReplaceAfter) do
			link = string.gsub(link, i, v)
		end
		
		link = '[http://services.runescape.com/m=news/' .. link .. ' official news post]'
	end
	
	local day, month, year, dateLink
	if(args.date ~= nil) then
		day = lang:formatDate('j', args.date)
		month = lang:formatDate('F', args.date)
		year = lang:formatDate('Y', args.date)
		dateLink = '[[' .. day .. ' ' .. month .. ']] [[' .. year .. ']]'
	else
		dateLink = '(missing date)'
	end
	
	local message = 'This ' .. link ..' is copyrighted by [[Jagex]]. It is copied verbatim from the '
	local website = '[http://www.runescape.com/community \'\'RuneScape\'\' website]'
	
	div:wikitext(message)
		:tag('span')
			:addClass('plainlinks')
			:wikitext(website)
		:done()
		:tag('br'):done()
		:wikitext('This update dates to ' .. dateLink .. '.')
	:done()

	local cat = ''
	-- Add categories only in update namespace
	if(title.namespace == 112) then
		cat = '[[Category:Updates]]'
		cat = cat .. '[[Category:' .. getUpdateCategory(args.category) .. '|' .. title.text .. ']]'
		cat = cat .. getDateCategories(day, month, year)
	end
	
	return '__NOTOC__ __NOEDITSECTION__' .. tostring(div) .. cat
end


-- CATEGORY FORMATTERS

-- [[Category:28 November updates]] etc
function p.dateCat(frame)
	local frame = frame:getParent()
	local date = paramTest.has_content(frame.args[1]) and frame.args[1] or string.gsub(title.text, ' ?updates?', '') .. ' 2000'

	local ret = 'This category contains updates posted on ' .. lang:formatDate('[[j F]]', date) .. ', sorted chronologically.'
	-- cats only in category namespace
	if(title.namespace == 14) then
		--updates by day, sorted by hexmonth,day: B28 (28 November)
		ret = ret .. '[[Category:Updates by day|' .. HexMonthConv[lang:formatDate('F', date)] .. lang:formatDate('d', date) .. ']]'
		--updates by month, sorted by [space]day
		ret = ret .. '[[Category:' .. lang:formatDate('F', date) .. ' updates| ' .. lang:formatDate('d', date) .. ']]'
	end
	
	return ret
end

-- [[Category:2015 updates]] etc
function p.yearCat(frame)
	local frame = frame:getParent()
	local date = paramTest.has_content(frame.args[1]) and frame.args[1] or string.gsub(title.text, ' ?updates?', '')

	local ret = 'This category contains updates posted in [[' .. date .. ']], sorted chronologically.'
	-- cats only in category namespace
	if(title.namespace == 14) then
		--updates by year, sorted by [space]year
		ret = ret .. '[[Category:Updates by year| ' .. date .. ']]'
	end
	
	return ret
end

-- DPL HANDLERS
function p.datedpl(frame)
	local args = frame:getParent().args
	local year = (args.year ~= nil) and args.year or lang:formatDate('Y', args.date)
	-- if it has category, {{Update}}, missing both probably means {{Patch Notes}}
	local cat = paramTest.has_content(args.category) and category_info[cat_switch[string.gsub(string.lower(a.category), ' ?updates?', '')]][2] or 'Patch Notes'
	local page = args['%TITLE%']

	return string.format("'''%s''' – %s: [[Update:%s|%s]]", year, cat, page, page)
end

return p