Module:PermissionTicket
Jump to navigation
Jump to search
Lua
English: Code behind {{PermissionTicket}} template.
Usage
{{#invoke:PermissionTicket|function_name}}
Examples
{{#invoke:PermissionTicket|PermissionTicket|id=2022011910011071|nocat=1}}
->
{{#invoke:PermissionTicket|PermissionTicket|id=202201191001107|nocat=1}}
->
{{#invoke:PermissionTicket|PermissionTicket|nocat=1}}
->
Code
--[[
___ ___ _ _ ______ _ _ _____ _ _ _
| \/ | | | | | _| ___ \ (_) (_) |_ _(_) | | | |
| . . | ___ __| |_ _| | ___(_) |_/ /__ _ __ _ __ ___ _ ___ ___ _ ___ _ __ | | _ ___| | _____| |_
| |\/| |/ _ \ / _` | | | | |/ _ \ | __/ _ \ '__| '_ ` _ \| / __/ __| |/ _ \| '_ \| | | |/ __| |/ / _ \ __|
| | | | (_) | (_| | |_| | | __/_| | | __/ | | | | | | | \__ \__ \ | (_) | | | | | | | (__| < __/ |_
\_| |_/\___/ \__,_|\__,_|_|\___(_)_| \___|_| |_| |_| |_|_|___/___/_|\___/|_| |_\_/ |_|\___|_|\_\___|\__|
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('<table class="messagebox plainlinks layouttemplate" style="border-collapse:collapse; border-width:2px; border-style:solid; width:100%%; clear: both; '..
'border-color:#f28500; background:#ffe;direction:ltr; border-left-width: 8px; ">'..
'<tr>'..
'<td class="mbox-image" style="padding-left:.9em;">'..
' [[File:Commons-emblem-issue.svg|class=noviewer|45px]]</td>'..
'<td class="mbox-text" style="">%s</td>'..
'</tr></table>', text)
end
-------------------------------------------------------------------------------
local function autotranslate(base, lang, args)
-- lua equivalent of {{Autotranslate}}
-- 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 {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} 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 = '[[Category:Pages using PermissionTicket template with incorrect parameter]]\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 = '[[Category:Items with incorrect VRTS ticket ID]]',
[06] = '[[Category:Items with VRTS permission confirmed]]',
[07] = '[[Category:Items with VRTS permission confirmed]]',
[10] = '[[Category:Custom VRTS permission template]]',
[14] = '[[Category:Categories with VRTS permission template]]',
}
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, '<span style="color:red">' .. vrts_str .. '</span>'}
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 "[[Category:P6305 SDC]]" but no longer needed
mismatch = '[[Category:Files with PermissionTicket ID different from SDC]]',
missing = '[[Category:Files with PermissionTicket template but without P6305 SDC statement]]',
}
-- 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