Usage edit source
Lessons from User:RexxS/GCI, at least lesson 5-10 from Google Code-in 2019.
Code is called from [[1]], where the results per task can be found.
{{#invoke:Sandbox|function_name}}
--this is a comment
-- Task 2
p = {} -- an empty table
function p.CleverName(frame)
return "Hello world."
end
-- Task 6, mwLibraries
local function fallbacklangcount (langcode)
local langslist = mw.language.getFallbacksFor(langcode)
local count = 0
for k, v in pairs(langslist) do
count = count + 1
end
return count
end
function p.fallbackfindmost (frame)
local langslist = mw.language.fetchLanguageNames()
local langs = {}
local max = 0
for k, v in pairs(langslist) do
local count = fallbacklangcount (k)
if count > max then
max = count
langs = {}
table.insert(langs, k)
elseif count == max then
table.insert(langs, k)
end
end
out = table.getn(langs) .. " languages use " .. max .. " fallback languages:<br>"
for langCount = 1, table.getn(langs) do
out = out .. langs[langCount] .. ":"
local langslist2 = mw.language.getFallbacksFor(langs[langCount])
for k, v in pairs(langslist2) do
out = out .. " " .. v
end
out = out .. "<br>"
end
return out
end
function p.fallbacklangs (frame)
local langcode = frame.args.langcode or ""
local langslist = mw.language.getFallbacksFor(langcode)
local out = ""
local count = 0
for k, v in pairs(langslist) do
out = out .. k .. " - " .. v .. "<br>"
count = count + 1
end
return langcode .. " has " .. count .. " fallback languages:<br>" .. out
end
p.Hi = function(frame)
strName = frame.args.name or "Jimbo"
return "Hello from Lua to my friend " .. strName .. ".<br>"
end
-- Task 6, mwLibraries
function p.langs(frame)
local langslist = mw.language.fetchLanguageNames()
local out = ""
local count = 0
for k, v in pairs(langslist) do
out = out .. k .. " - " .. v .. "<br>"
count = count + 1
end
return out .. "<br>= " .. count .. " languages"
end
-- Task 6 - pginfo
p.pginfo = function( frame )
local title = frame.args.title or ""
if title == "" then
return "Please provide the title of the page to check as a named paramater '|title=<name>'."
end
local ttlobj = mw.title.new( title )
local txt = ttlobj.text
if ttlobj.exists then
txt = txt .. " exists"
else
txt = txt .. " does not exist"
end
if ttlobj.isRedirect then
txt = txt .. " and is a redirect."
else
txt = txt .. " and is not a redirect."
end
return txt
end
-- Task 6 - pgtitle
p.pgtitle = function( frame )
local title = frame.args.title
local ttlobj = mw.title.new( title )
local txt = ttlobj.text
return txt
end
-- Task 5 - Lua libraries
function p.sent(frame)
local txt = frame.args.text or ""
-- local out = txt
-- local out = string.sub(txt, 1, 1)
-- local out = string.sub(txt, 2)
local out = string.upper(string.sub(txt, 1, 1)) .. string.sub(txt, 2)
return out
end
function p.Temperature(frame)
cel = tonumber(frame.args.celsius) or 0
fah = cel * 9 / 5 + 32
if cel > 10 then
msg = ". It is warm."
else
msg = ". It is quit cold."
end
return "The temperature of " .. cel .. " degrees Celsius equals to " .. fah .. " degrees Fahrenheit." .. msg
end
-- Task 4 - Loops
p.Times = function(frame)
local num = tonumber( frame.args.num ) or 2
local out = num .. " times table<br>"
for i = 1, 10 do
out = out .. i .. " times " .. num .. " gives " .. i * num .. "<br>"
end
return out
end
-- Task 5 - Lua libraries
function p.unpack(frame)
local dmy = frame.args.dmydate or ""
local d, m, y = string.match(dmy, "(%d+) (%w+) (%d+)")
return "Year = " .. y .. "<br>Day = " .. d .. "<br>Month = " .. m
end
function p.unpackAmerican(frame)
local dmy = frame.args.dmydate or ""
local m, d, y = string.match(dmy, "(%w+) (%d+), (%d+)")
return "Year = " .. y .. "<br>Day = " .. d .. "<br>Month = " .. m
end
-- Get date
function p.getdate(frame)
local qid = frame.args.qid or ""
local prop = frame.args.prop or ""
if qid == "" or prop == "" then
return "Call this function with a qid and a prop, both being named parameters"
end
local valtbl = mw.wikibase.getBestStatements(qid, prop)
local timestamp = valtbl[1].mainsnak.datavalue.value.time
local y, m, d = string.match(timestamp, "(%d+)-(%d+)-(%d+)")
return "Year = " .. y .. "<br>Day = " .. d .. "<br>Month = " .. m
end
-- Get fulldate
function p.getfulldate(frame)
local qid = frame.args.qid or ""
local prop = frame.args.prop or ""
if qid == "" or prop == "" then
return "Call this function with a qid and a prop, both being named parameters"
end
local monthname = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }
local valtbl = mw.wikibase.getBestStatements(qid, prop)
local timestamp = valtbl[1].mainsnak.datavalue.value.time
local y, m, d = string.match(timestamp, "(%d+)-(%d+)-(%d+)")
out = d .. " " .. monthname[tonumber(m)] .. " " .. y
return out
end
function p.getitem(frame)
local qid = frame.args.qid or ""
local prop = frame.args.prop or ""
if qid == "" or prop == "" then
return "Call this function with a qid and a prop, both being named parameters"
end
local valtbl = mw.wikibase.getBestStatements(qid, prop)
local id = valtbl[1].mainsnak.datavalue.value.id
local label = mw.wikibase.getLabel(id)
return label
end
return p