Module:PermissionTicket

--[[ ___  ___          _       _       ______                   _         _           _____ _      _        _   \_|  |_/\___/ \__,_|\__,_|_|\___(_)_|  \___|_|  |_| |_| |_|_|___/___/_|\___/|_| |_\_/ |_|\___|_|\_\___|\__| This module is intended to be the engine behind "Template:PermissionTicket"
 * \/ |         | |     | |     _| ___ \                 (_)       (_)         |_   _(_)    | |      | |
 * . . | ___   __| |_   _| | ___(_) |_/ /__ _ __ _ __ ___  _ ___ ___ _  ___  _ __ | |  _  ___| | _____| |_
 * |\/| |/ _ \ / _` | | | | |/ _ \ | __/ _ \ '__| '_ ` _ \| / __/ __| |/ _ \| '_ \| | | |/ __| |/ / _ \ __|
 * | | | (_) | (_| | |_| | |  __/_| | |  __/ |  | | | | | | \__ \__ \ | (_) | | | | | | | (__|   <  __/ |_

Please do not modify this code without applying the changes first at "Module:PermissionTicket/sandbox" and testing at "Template:PermissionTicket/testcases".

Authors and maintainers: ]]
 * User:Jarekt - original version

-- ======================================= -- === Dependencies ====================== -- ======================================= local core = require('Module:Core')

-- ================================================== -- === Generic Local functions ====================== -- ================================================== --- local function getBestProperties(entity, prop) return core.parseStatements(entity:getBestStatements( prop ), nil) end

--- local function info_box(text) -- create warning infobox (borowed from Module:Coordinates) return string.format(''..		' '..		''..		' '..		'%s '..		'  ', text) end

--- local function autotranslate(base, lang, args) -- lua equivalent of 	-- get language fallback list local langList = mw.language.getFallbacksFor(lang) table.insert(langList,1,lang) -- find base template language subpage local page for _, language in ipairs(langList) do page = 'Template:' .. base .. '/' .. language -- returns only the page if mw.title.new(page).exists then break end end assert(page, string.format('No fallback page found for autotranslate (base=%s, lang=%s)', base, lang)) -- Transclude with template arguments the same as the ones passed to  template. return mw.getCurrentFrame:expandTemplate{ title = page, args = args} end

--- local function get_sdc_vrts(mid, namespace) -- find entity and look up all 'P6305' values with VRTS numbers local entity = nil if (namespace == 6) then entity = mw.wikibase.getEntity elseif mid and type(mid)=='string' and mid:match( '^[Mm]%d+$' ) then entity = mw.wikibase.getEntity(mid) end local vrts_array = {} if entity then vrts_array = getBestProperties(entity, 'P6305') end return vrts_array or {} end

--- local function check_template_parameters(args) -- code checking if the template has some unusual input parameters valid_args = {id=1, nocat=1, ['date']=1, comment=1, user=1, lang=1, mid=1, [1]=1, [2]=1, [3]=1, [4]=1 } bad_args = {} for field, _ in pairs( args ) do 		if not valid_args[field] then table.insert(bad_args, field) end end if #bad_args>0 then local cat = '\n' local msg = info_box('Error in template Template:PermissionTicket unknown parameter(s): ' .. table.concat(bad_args, ', ')) return cat .. msg end return nil end

--- local function render_template(vrts_str, lang, namespace) -- render the template based on vrts number, language and namespace local cat_lut = { -- 6 File, 10 template, 14 category bad = '', [06] = ,		[07] = ,		[10] = ,		[14] = ,	}

local tag, msg, args local vrts_num = tonumber(vrts_str) local cat = cat_lut.bad local url = 'https://ticket.wikimedia.org/otrs/index.pl?Action=AgentTicketZoom&TicketNumber=' .. vrts_str if vrts_num and vrts_num>1980000000000000 and vrts_num<3000000000000000 then -- CASE 1: parameter "id" or "1" which is numeric and in the correct range args = {url, vrts_str} cat = cat_lut[namespace]  or '' elseif vrts_num then -- CASE 2: parameter "id" or "1" which is likely not right args = {url, ' ' .. vrts_str .. ' '}		msg = "Warning: This file's Volunteer Response Team Software (VRTS) ticket ID seems to be invalid." else -- CASE 3: everything else which means no "id" or "1" parameters args = {, } msg = "Warning: This file is missing essential information needed to confirm validity of the Volunteer Response Team Software (VRTS) ticket." end tag = autotranslate('PermissionTicket', lang, args) if msg then tag = tag .. '\n' .. info_box(msg) end return tag, cat end

-- ================================================== -- === External functions =========================== -- ================================================== local p = {}

-- =========================================================================== -- === Version of the function to be called from other LUA codes -- ===========================================================================

function p.compare_ids(vrts, vrts_array) -- compare wikitext input "vrts" to SDC proterty array "vrts_array" if #vrts_array==0 then return 'missing' end for _, vrts_str in ipairs(vrts_array) do		if vrts_str==vrts then return 'matching' -- match was found end end return 'mismatch' end

-- =========================================================================== -- === Version of the functions to be called from template namespace -- ===========================================================================

function p.render(frame) -- debugging function for bare-bones teplate rendering local args = core.getArgs(frame) local tag, cat = render_template(args.id, args.lang, args.namespace or '') return tag .. '\n' .. cat end

--- function p.PermissionTicket(frame) local args = core.getArgs(frame) args.id = args.id or args[1] local cat_lut = { -- SDC related templates matching = '', -- was "" but no longer needed mismatch = '', missing = '', }	-- get entity and P6305 local page = mw.title.getCurrentTitle local vrts_array = get_sdc_vrts(args.mid, page.namespace) -- categorize local tags, cats = {}, {} if args.id and page.namespace==6 then local cat = p.compare_ids(args.id, vrts_array) table.insert(cats, cat_lut[cat]) end local cat = check_template_parameters(args) local allowed_namespaces = {[6]=1, [7]=1, [10]=1, [14]=1} if cat and allowed_namespaces[page.namespace] then table.insert(cats, cat) end -- render template if args.id then -- template wikitext vrts id has priority vrts_array = { args.id } elseif #vrts_array==0 then -- if no wikitext id and no SDC id ... vrts_array = { '' } end for _, vrts_str in ipairs(vrts_array) do -- render one or more templates local tag, cat = render_template(vrts_str, args.lang, page.namespace) table.insert(tags, tag) table.insert(cats, cat) end -- create final output local output = table.concat(tags, '\n') if not core.yesno(args.nocat, false) then -- if "nocat" then ignore categories output = output .. '\n' ..table.concat(cats, '\n') end return output end

return p