Module:Sig2User
跳到导航
跳到搜索
这个模块可以从带有用户页、用户讨论页或用户贡献链接的签名中找出用户名。
为提高效率,仅在常见名字空间无法匹配时,才会获取mw.site.namespaces
表中对应名字空间所有的别名(例如繁体或简写)。
使用方式:
{{subst:#invoke:Sig2User|main|1=签名放在这里}}
如果不想出现错误大红字大红字爬或者不想被加入Category:有脚本错误的页面的情况下,可以指定suppress
参数。
{{subst:#invoke:Sig2User|main|1=签名放在这里|suppress=任意值}}
-- Module:Sig2User -- Made with ♥ by User:Leranjun -- This module takes a signature and returns the username found in the signature. -- For efficiency reasons, the module first looks for the "common" namespace aliases -- before obtaining all aliases from the mw.site.namespaces table. local p = {} local getArgs = require("Module:Arguments").getArgs local function isempty(s) return not s or s == "" end local function throw(e, suppress) return suppress and ("Error: " .. e) or error(e) end local function toComp(v) local r = mw.ustring.gsub( mw.ustring.gsub(v, "%s", "[ _]"), "(%%?)(.)", function(percent, letter) if percent ~= "" or not letter:match("%a") then -- do not use ustring! -- if the '%' matched, or `letter` is not a letter, return "as is" return percent .. letter else -- else, return a case-insensitive character class of the matched letter return mw.ustring.format("[%s%s]", mw.ustring.lower(letter), mw.ustring.upper(letter)) end end ) return r end local function findFromTable(s, prefixes, lang) local r = nil for _, v in pairs(prefixes) do pattern = "%[%[:?" .. toComp(v) .. "([^|/]+)|?.-/?.-%]%]" r = mw.ustring.match(s, pattern) if r then return lang:ucfirst(r) end end end local function patternExtend() local r = {} local NS_USER = mw.site.namespaces[2]["aliases"] local NS_USER_TALK = mw.site.namespaces[3]["aliases"] local NS_SPECIAL = mw.site.namespaces[-1]["aliases"] for _, v in pairs(NS_USER) do table.insert(r, v .. ":") end for _, v in pairs(NS_USER_TALK) do table.insert(r, v .. ":") end for _, v in pairs(NS_SPECIAL) do table.insert(r, v .. ":Contributions/") table.insert(r, v .. ":用[户戶][贡貢][献獻]/") end return r end function p._main(args) local s = args[1] local suppress = args["suppress"] if isempty(s) then return throw("Empty string", suppress) end -- Common namespace aliases local COMMON_PREFIX = { "User:", "U:", "User talk:", "Special:Contributions/", "Special:用[户戶][贡貢][献獻]/" } local lang = mw.language.getContentLanguage() local r = findFromTable(s, COMMON_PREFIX, lang) if r then return r end -- All namespace aliases r = findFromTable(s, patternExtend(), lang) if r then return r end -- Nothing found :( return throw("Invalid signature", suppress) end function p.main(frame) return p._main(getArgs(frame)) end return p