Module:Sandbox/Tom.Reding/sandbox explained

--use "local arg1 = frame.args[1]" for sandboxing via #invoke--use "local args = frame:getParent.args" for use via template

local p =

function p.blind_epfc_test(frame) mw.incrementExpensiveFunctionCount --return mw.getExpensiveFunctionCount --returns nil --return mw.ExpensiveFunctionCount --returns nil return mw.expensiveFunctionCount --returns nilend

function p.select_test(frame) --not for table counting local t = local c = table.concat(t, ',') local c2 = table.concat(t, ', ') --local s = select('#', c) --return s --returns 1 --local s = select('#', t) --return s --returns 1 local s = select('#', c2) return s --returns 1end

function p.get_info(frame) local pn = frame.args[1] local wp = p.getSd(frame) local wd = p.getWdSd(frame) local dab_dne = 'dab_dne' local pn_dab = pn..' (disambiguation)' local pn_dab_result = dab_dne local pn_dab_content = mw.title.new(pn_dab):getContent or nil if pn_dab_content then pn_dab_result = ''..pn_dab..'' end local info = '# '..pn..' - '..pn_dab_result..' - '..wp..'; '..wd if wp

'wp_sd_dne' and wd

'wd_sd_dne' then local R = p.isR(frame) if R then local R_content = mw.title.new(pn):getContent or local R_target = mw.ustring.match(R_content, '^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*%[%[(.-)%]%]') or if R_target ~= then local R_pn = R_target frame.args[1] = R_pn local R_wp = p.getSd(frame) local R_wd = p.getWdSd(frame) local R_pn_dab = R_pn..' (disambiguation)' local R_pn_dab_result = dab_dne local R_pn_dab_content = mw.title.new(R_pn_dab):getContent or nil if R_pn_dab_content then R_pn_dab_result = ''..R_pn_dab..'' end local R_info = '## '..R_pn..' - '..R_pn_dab_result..' - '..R_wp..'; '..R_wd return info..'\n'..R_info else return info..' (malformed #R)' end end end return infoend

function p.getWdSd(frame) local pn = frame.args[1] local args = local sd = frame:expandTemplate if sd

then sd = 'wd_sd_dne' end return mw.text.trim(sd)end

function p.getSd(frame) local pn = frame.args[1] local content = mw.title.new(pn):getContent or local sd = mw.ustring.match(content, '') or mw.ustring.match(content, '') or mw.ustring.match(content, '') or 'wp_sd_dne' return mw.text.trim(sd)end

function p.sizeof(frame) local out = for _, pn in pairs(frame.args) do local content = mw.title.new(pn):getContent or out = out..'#'..#content..' '..pn..'\n' end return outend

function p.blt(frame) local myBlacklist = require('Module:Taxonbar/blacklist').blacklist

local selfdocout = 'myVariable = ' local args = out = frame:expandTemplate return outend

function p.isR(frame) local pn = frame.args[1] local title = mw.title.new(pn) return title.isRedirectend

function p.argt(frame) local list = 'strict' local doc = 'doc' local strict = p.whitelist(frame, list, doc) local tstrict = p.whitelist(frame, list) return 'doc:\n\n'..strict..'\n\ntable[Q16521]:'..tstrict[Q16521]end

function p.expandTemplate(frame) local Q = 'Q16521' local Q_expanded = frame:expandTemplate return Q_expandedend

function p.whitelist(frame) local listType = frame.args[1] local documentation = frame.args[2] local acceptableInstanceOf_Strict = local acceptableInstanceOf_Lax = local acceptableInstanceOf_All = --create IFF requested if listType

'all' then for k, v in pairs(acceptableInstanceOf_Strict) do acceptableInstanceOf_All[k] = v end for k, v in pairs(acceptableInstanceOf_Lax) do acceptableInstanceOf_All[k] = v end end if (documentation

nil) or --module-use only; order & format irrelevant (documentation and documentation

) then local out = if listType

'strict' then out = acceptableInstanceOf_Strict['Q16521'] elseif listType

'lax' then out = acceptableInstanceOf_Lax['Q42621'] elseif listType

'all' then out = acceptableInstanceOf_All['Q17487588'] end return out else local displayOrder_Strict = local displayOrder_Lax = local displayOrder_All = --create IFF requested if listType

'all' then --concatenate strict + lax display order tables local i = 0 for _, v in pairs(displayOrder_Strict) do i = i + 1 displayOrder_All[i] = v end for _, v in pairs(displayOrder_Lax) do i = i + 1 displayOrder_All[i] = v end end local outOrder = if listType

'strict' then outOrder = displayOrder_Strict elseif listType

'lax' then outOrder = displayOrder_Lax elseif listType

'all' then outOrder = displayOrder_All end local out = for _, q in pairs(outOrder) do local Q = frame:expandTemplate out = out..'# '..Q..'\n' end return out end end

function p.trr_test(frame) local getRegex = require('Module:Template redirect regex').main local tregex = getRegex(nil) return tregex or 'nil'end

function p.string_match(frame) local r = "2008–09 in Scottish women's football" local regex_hyph = '%d%d%d%d[–-]%d+' -- if string.match(r, regex_hyph) then --returns 'false'!-- if mw.ustring.match(r, regex_hyph) then --returns 'true' if string.match(r, 'ball$') then --returns 'true' return 'true' else return 'false' endend

function p.avoidself(frame) local currtitle = mw.title.getCurrentTitle local testcases = string.match(currtitle.subpageText, '^testcases') local avoidself = (currtitle.text ~= 'Navseasoncats' and not string.match(currtitle.text, 'Navseasoncats with') and not string.match(currtitle.text, 'Navseasoncats.*/doc') and not string.match(currtitle.text, 'Navseasoncats.*/sandbox') and currtitle.nsText:gsub('_', ' ') ~= 'User talk' and -- (currtitle.nsText ~= 'Template' or testcases)) --avoid nested transclusion errors (i.e.) return 'text: '..currtitle.text.. '
nsText: "'..currtitle.nsText..'"'.. '
testcases: '..(testcases or 'nil').. '
avoidself: '..tostring(avoidself)end

function p.ttest(frame) --#t local t0 = --0 local t1 = --0 local t2 = --1 local t31 = --2 local t32 = --0 local t33 = --0 local tneg = --1 --output return '#t0: ' ..#t0.. --0 '
#t1: ' ..#t1.. --0 '
#t2: ' ..#t2.. --1 '
#t31: '..#t31.. --2 '
#t32: '..#t32.. --0 '
#t33: '..#t33.. --0 '
#tneg: '..#tneg.. --1 '
mw.log'..(mw.log(#t31) or )end

function p.indexcalctest(frame) local t = t[1+3] = 5 return t[4] or 'nil' --returns 5 as expectedend

function p.fort(frame) local i = 0 for i=1, 10 do i = i + 1 end return i --returns '0' as expectedend

function p.whilet(frame) local i = 0 while i < 10 do i = i + 1 end return i --returns '10' as expectedend

function p.ifevaluationtest(frame) local cat1 = '2010s awards' local cat2 = '2020s awards' if mw.title.new(cat1, 'Category').exists and mw.title.new(cat2, 'Category').exists then return 1 end return 0end

function p.zerotest(frame) local neg = -0 --displays '-0' as expected local nneg = tonumber('-0') --displays '-0' as expected local pos = 0 --displays '-0' NOT as expected local ppos = tonumber('0') --displays '0' local pppos = tonumber('+0') --displays '0' as expected local s = tostring(pos) --displays '-0' NOT as expected local ss = tostring(ppos) --displays '0' local t = (neg < 0) --false local tt = (s

'-0') --true local u = (neg

0) --true local v = (neg

-0) --true return tostring(nneg)..' '..pppos..' '..s..' '..tostring(tt)..' '..tostring(v) --returns '-0 0 -0 true true'end

function p.test(frame) local conf = require("Module:Authority control").conf local out = for _, c in pairs(conf) do out = out..'
'..c[1] end return outend

function p.iffalsetest(frame) local f = false if f then return 'f' else return 'endd' --debugger thinks "'end'"

"end"... end --returns endd, as expectedend

function p.fortest(frame) local out = for gs = 0, 5 do out = out..gs end return out --returns 012345end

function p.regex_group_test(frame) local R = 'asdfasdf 2020–' local y, hyph, ending = mw.ustring.match(R, '^.-(%d+)([–-])(.*)$') return (y or 'nil')..', '..(hyph or 'nil')..', '..(ending or 'nil') --returns '2020, –,' as expectedend

function p.empty_regex_group_test(frame) local text = '2020–'-- return (mw.ustring.match(text, '%d+([–-])$') or 'nil') --returns –-- return (mw.ustring.match(text, '%d+[–-]$') or 'nil') --returns 6-- return (mw.ustring.match(text, '%d+[–-]($)') or 'nil') --returns nil return (mw.ustring.match(text, '%d+[–-](.*)$') or 'nil') --returns as expectedend

function p.regex_gsub_test(frame) local cat1 = '2006 Super 14 season' local base1 = '"'..mw.ustring.gsub(cat1, '%d+[–-]?%d?%d?%d?%d?', )..'"' --returns " Super season" local base1_1 = '"'..mw.ustring.gsub(cat1, '%d+[–-]?%d?%d?%d?%d?', , 1)..'"' --returns " Super 14 season" local cat2 = '14 Super 2006 season' local base2 = '"'..mw.ustring.gsub(cat2, '%d+[–-]?%d?%d?%d?%d?', )..'"' --returns " Super season" local base2_2 = '"'..mw.ustring.gsub(cat2, '%d+[–-]?%d?%d?%d?%d?', , 1)..'"' --returns " Super 2006 season" local base2_3 = '"'..mw.ustring.gsub(cat2, '%d%d?%d?%d?[–-]?%d?%d?%d?%d?', )..'"' --returns " Super season" local base2_4 = '"'..mw.ustring.gsub(cat2, '%d%d?%d?%d?[–-]?%d?%d?%d?%d?', , 1)..'"' --returns " Super 2006 season" return base2_4end

function p.regex_gsub_test2(frame) --return (mw.ustring.gsub('123', '%d', '$1')[1] or 'n')..(mw.ustring.gsub('123', '%d', )[2] or 'o')..(mw.ustring.gsub('123', '%d', '0') or 'p') --returns no000 --local one, two = mw.ustring.gsub('123', '%d', ) --return (one or '1')..(two or '2') --returns 3 --return (two or '2') --returns 3 local one, two = mw.ustring.gsub('123', '%D', ) return (two or '?') --returns 0end

function p.match_or_match_test(frame) local text = '2020–' -- local y, hyph = mw.ustring.match(text, '(%d+)([–-])present$') or mw.ustring.match(text, '(%d+)([–-])$')-- return (y or 'nil')..' & '..(hyph or 'nil') --returns 2020 & nil (NOT as expected) -- local y, hyph = mw.ustring.match(text, '(%d+)([–-])$') or mw.ustring.match(text, '(%d+)([–-])present$')-- return (y or 'nil')..' & '..(hyph or 'nil') --returns 2020 & nil (NOT as expected) -- local y, hyph = mw.ustring.match(text, '(%d+)([–-])$')-- return (y or 'nil')..' & '..(hyph or 'nil') --returns 2020 & – (as expected) local y, hyph = mw.ustring.match(text, '(%d+)([–-])present$') return (y or 'nil')..' & '..(hyph or 'nil') --returns nil & nil (as expected)end

function p.mw_title_new_exists_wstest(frame) local cat = 'UK MPs 2015–2017 '..' ' return tostring(mw.title.new(cat, 'Category').exists) --returns true (as expected)end

function p.tonumber_test(frame) local cat = 'Wikipedia articles with VIAF identifiers' --800,000 something local lang = mw.getContentLanguage local count = mw.site.stats.pagesInCategory(cat, 'pages') --800000 local count_lang = lang:formatNum(count) --800,000-- return (count or 'nil')..', '..(tonumber(count or '-1') or '-2') --returns 880843, 880843 as expected-- return (count_lang or 'nil')..', '..(tonumber(count_lang or '-1') or '-2') --returns 881,195, -2 --use lang:parseFormattedNumber instead return (count_lang or 'nil')..', '..(lang:parseFormattedNumber(count_lang or '-1') or '-2') --returns 880,358, 880358end

function p.lastedit_test(frame) return mw.title.new('Special:Contributions/Tom.Reding', 'Special'):getContent or 'nil'end

function p.split_test(frame)-- return table.concat(mw.text.split('test-1 two-', ' '), ',') or 'nil' --returns test-1,two- -- return table.concat(mw.text.split('test-1 two-', '%s'), ',') or 'nil' --returns test-1,two--- return table.concat(mw.text.split('test-1 two-', '-'), ',') or 'nil' --returns test,1 two,-- return table.concat(mw.text.split('test-1 two-', ' %-'), ',') or 'nil' --returns test-1 two--- return table.concat(mw.text.split('test-1 two-', ' -'), ',') or 'nil' --returns t,e,s,t,-,1,,t,w,o,--- return table.concat(mw.text.split('test-1 two-', '- '), ',') or 'nil' --returns test-1 two--- return table.concat(mw.text.split('test-1 two-', '%s%-'), ',') or 'nil' --returns test-1 two- return table.concat(mw.text.split('test-1 two-', '[%s%-]'), ',') or 'nil' --returns test,1,two,end

function p.regex_greed_and_nested_groups_test(frame) local t = '' local r = '' local g1, g2 = mw.ustring.match(t, r) return '"'..g1..'" "'..g2..'"' --returns "s in New Spain" "title year" as expectedend

function p.redirectTarget_test(frame)-- local title = mw.title.new('1670s in Mexico', 'Category') --#R --returns false-- local title2 = mw.title.new('1670s in New Spain', 'Category') --#R target --returns false -- local title = mw.title.makeTitle('Category', '1670s in Mexico') --#R --returns false-- local title2 = mw.title.makeTitle('Category', '1670s in New Spain') --#R target --returns false local title = mw.title.new('EL 61') --#R --returns "Haumea" local title2 = mw.title.new('Haumea') --#R target --returns "false" local rtitle = title.redirectTarget --only works in mainspace; ignorant of s local rtitle2 = title2.redirectTarget --only works in mainspace; ignorant of s if type(rtitle)

'boolean' then return tostring(rtitle)..' '..tostring(rtitle) else return '"'..title.redirectTarget.baseText..'" "'..tostring(rtitle2)..'"' endend

function p.frame_newChild_test(frame) local t = 'Category:1670s in Mexico' local child = frame:newChild-- return '"'..child:getTitle..'"' --returns "Category:1670s in Mexico" as expected-- return '"'..child:expandTemplate..'"' --returns "" NOT as expected...-- return '"'..child:expandTemplate..'"' --returns "1670" as expected return '"'..frame:expandTemplate..'"' --returns "1670" as expectedend

function p.ns_test(frame) local title = mw.title.getCurrentTitle local namespace = title.namespace return namespaceend

return p