Module:Test
La documentation pour ce module peut être créée à Module:Test/doc
-- Module:Test
-- Test date de creation d'une page depuis Lua (SMW _CDAT / PAGECREATIONDATE / REVISIONTIMESTAMP)
-- Usage:
-- {{#invoke:Test|show|page=Utilisateur:Wikinuma/Brouillon}}
-- ou sans parametre => page courante
local p = {}
local mw = mw
local F = mw.getCurrentFrame()
local t_trim = mw.text.trim
local t_jsonDecode = mw.text.jsonDecode
local function safeTrim(v)
return t_trim(tostring(v or ""))
end
local function getTitleText(page)
page = safeTrim(page)
if page ~= "" then
return page
end
return mw.title.getCurrentTitle().prefixedText
end
-- SMW _CDAT (si SMW dispo)
local function getSMW_CDAT(titleText)
if type(mw.smw) ~= "table" or type(mw.smw.ask) ~= "function" then
return "", "SMW_NO"
end
local res = nil
local ok = pcall(function()
res = mw.smw.ask({
"[[" .. titleText .. "]]",
"?_CDAT=cdat",
"limit=1",
"link=none"
})
end)
if not ok or type(res) ~= "table" or not res[1] then
return "", "SMW_ERR"
end
local cdat = res[1].cdat
if type(cdat) == "string" then
cdat = safeTrim(cdat)
return (cdat ~= "" and cdat or ""), (cdat ~= "" and "SMW_OK" or "SMW_EMPTY")
end
if type(cdat) == "table" then
for _, v in ipairs(cdat) do
if type(v) == "string" and safeTrim(v) ~= "" then
return safeTrim(v), "SMW_OK"
elseif type(v) == "table" then
local ft = v.fulltext or v.page or v[1]
if type(ft) == "string" and safeTrim(ft) ~= "" then
return safeTrim(ft), "SMW_OK"
end
end
end
end
return "", "SMW_EMPTY"
end
-- ParserFunction PAGECREATIONDATE (si existe sur le wiki)
local function getPAGECREATIONDATE(titleText)
-- Sur ton wiki, le PF n'existe pas => il faut pcall
local ok, v = pcall(function()
return F:callParserFunction("PAGECREATIONDATE", { titleText })
end)
if not ok then
return "", "PCD_NOT_FOUND"
end
v = safeTrim(v)
if v == "" then
return "", "PCD_EMPTY"
end
return v, "PCD_OK"
end
-- API 1re revision via mw.http.fetch (souvent indispo)
local function getApiFirstRevisionISO(titleText)
if type(mw.http) ~= "table" or type(mw.http.fetch) ~= "function" then
return "", "HTTP_NO_FETCH"
end
local server = (mw.site and mw.site.server) and tostring(mw.site.server) or ""
local scriptPath = (mw.site and mw.site.scriptPath) and tostring(mw.site.scriptPath) or ""
local api = server .. scriptPath .. "/api.php"
local params = {
action = "query",
prop = "revisions",
titles = titleText,
rvprop = "timestamp",
rvlimit = "1",
rvdir = "newer",
format = "json",
formatversion = "2"
}
local qs = (mw.uri and type(mw.uri.buildQueryString) == "function")
and mw.uri.buildQueryString(params)
or nil
local url = qs and (api .. "?" .. qs) or api
local r = mw.http.fetch(url)
if not r or r.status ~= 200 or type(r.body) ~= "string" then
return "", "HTTP_FAIL"
end
local ok, data = pcall(t_jsonDecode, r.body)
if not ok or not data or not data.query or not data.query.pages or not data.query.pages[1] then
return "", "API_BAD_JSON"
end
local page = data.query.pages[1]
local rev = page.revisions and page.revisions[1]
local ts = rev and rev.timestamp
ts = safeTrim(ts)
if ts == "" then
return "", "API_EMPTY"
end
return ts, "API_OK"
end
-- Derniere modif (toujours dispo)
local function getREVISIONTIMESTAMP_ISO(titleText)
local ts = F:callParserFunction("REVISIONTIMESTAMP", { titleText })
ts = safeTrim(ts)
-- REVISIONTIMESTAMP renvoie souvent YYYYMMDDHHMMSS
if ts ~= "" and #ts >= 14 and ts:match("^%d%d%d%d%d%d%d%d%d%d%d%d%d%d") then
local iso =
ts:sub(1, 4) .. "-" ..
ts:sub(5, 6) .. "-" ..
ts:sub(7, 8) .. "T" ..
ts:sub(9, 10) .. ":" ..
ts:sub(11, 12) .. ":" ..
ts:sub(13, 14) .. "Z"
return iso, "REV_OK"
end
-- si deja ISO ou autre
if ts ~= "" then
return ts, "REV_OK"
end
return "", "REV_EMPTY"
end
function p.show(frame)
frame = frame or F
-- page=... (arg nommé) ou 1=...
local args = frame.args or {}
local titleText = getTitleText(args.page or args[1])
local out = {}
out[#out + 1] = "Page : " .. titleText
local smwVal, smwSt = getSMW_CDAT(titleText)
out[#out + 1] = "SMW _CDAT : " .. (smwVal ~= "" and smwVal or "(vide)") .. " (" .. smwSt .. ")"
local pcdVal, pcdSt = getPAGECREATIONDATE(titleText)
out[#out + 1] = "PAGECREATIONDATE : " .. (pcdVal ~= "" and pcdVal or "(vide)") .. " (" .. pcdSt .. ")"
local apiVal, apiSt = getApiFirstRevisionISO(titleText)
out[#out + 1] = "API 1re revision : " .. (apiVal ~= "" and apiVal or "(vide)") .. " (" .. apiSt .. ")"
local revVal, revSt = getREVISIONTIMESTAMP_ISO(titleText)
out[#out + 1] = "REVISIONTIMESTAMP (derniere modif) : " .. (revVal ~= "" and revVal or "(vide)") .. " (" .. revSt .. ")"
return table.concat(out, "\n")
end
return p