模組:PJBSClass
外观
本模組為互助客棧提議設立通用評級的配套模組。為了實現「新的模版可以單獨給條目一個總體的品質評級,各個WikiProject可以直接繼承這個quality assessment,也可以搞自己的評級
」中的「繼承評級」項目,因此設計了用於讀取{{PJBS}}所輸入之評級的模組。
為了避免重複運算,使用時請使用Module:PJBSClass/main來讀取評級。Module:PJBSClass/main會透過使用mw.loadData("Module:PJBSClass/page")
來調用本模組,讓評級的計算在每個頁面只會計算一次。也因此,本模組只能讀取第一個{{PJBS}}所輸入之評級,如放置了多個{{PJBS}}模板,後面的{{PJBS}}評級內容將會被忽略。
用法
不建議直接調用本模組,應透過Module:PJBSClass/main中的getClass
函數來獲取評級值。下文僅簡介本模組中函數的功能。
本模組中的函數
- getAutoClass
-
- 模組調用:
p.getAutoClass(page_name, input_class)
- 模板調用:
{{#invoke:PJBSClass|getAutoClass| page_name | class = input_class }}
- 模組調用:
- 自動判斷頁面名稱為「page_name」之頁面的評級。如無法判斷則返回「input_class」評級值。
- 如果「page_name」為空或未輸入,則自動判斷當前頁面的評級。
- getClassByPage
-
- 模組調用:
p.getClassByPage(page_name)
- 模板調用:
{{#invoke:PJBSClass|getClassByPage| page_name }}
- 模組調用:
- 從頁面名稱為「page_name」之頁面的討論頁中{{PJBS}}或專題橫幅中讀取評級值。
- 如果「page_name」為空或未輸入則讀取當前頁面對應的討論頁。
- getWPBSTemplateContent
-
- 模組調用:
p.getWPBSTemplateContent(wikitext)
- 模板調用:(不支持)
- 模組調用:
- 從「wikitext」中擷取{{WikiProject banner shell}}代碼。
- getClassByWikitext
-
- 模組調用:
p.getClassByWikitext(wikitext, flag)
- 模板調用:
{{#invoke:PJBSClass|getClassByWikitext| wikitext }}
- 模組調用:
- 從未解析的維基代碼「wikitext」中讀取{{PJBS}}或專題橫幅中的評級值。「flag」為
true
時,會多返回「有無找到{{PJBS}}或專題橫幅」的布林值(限Lua調用)。
本模組的子模組可以透過{{Module:PJBSClass/page}}
直接調用。
- 用法
{{Module:PJBSClass/page}}
:回傳模板所在條目輸入於{{PJBS}}中的評級原始值或自動判斷的評級值。- 例如在本頁放置
{{Module:PJBSClass/page}}
→「module」
- 例如在本頁放置
- 用法
{{Module:PJBSClass/page|頁面名稱}}
:回傳輸入於指定頁面名稱{{PJBS}}中的評級原始值或自動判斷的評級值。- 例如
{{Module:PJBSClass/page|溫尼爾多面體模型列表}}
→「第37行Lua错误:bad argument #6 to 'inNamespaces' (unrecognized namespace number '2300')」
- 例如
- 用法
{{Module:PJBSClass/page|json=yes}}
:直接回傳所在條目mw.loadData("Module:PJBSClass/page")
的JSON結果。- 例如
{{Module:PJBSClass/page|json=yes}}
→「{"is_ga":false,"is_softredirect":false,"is_sia":false,"is_fa":false,"is_fm":false,"is_fl":false,"is_sandbox":false,"is_disambiguation":false,"class":"module"}
」{
}"is_ga": false, "is_softredirect": false, "is_sia": false, "is_fa": false, "is_fm": false, "is_fl": false, "class": "module", "is_disambiguation": false, "is_sandbox": false
- 例如
local p={}
function p.getAutoClass(input_data, class_input)
local page_name = input_data
local class_default = class_input or ''
if type((input_data or {}).args) ~= type(nil) then --input_data is a frame
page_name = input_data.args['1'] or input_data.args[1]
class_default = input_data.args.class or input_data.args.CLASS or ''
end
local page = (type(page_name) ~= type(nil)) and ({pcall(mw.title.new, page_name)})[2] or mw.title.getCurrentTitle()
local page_orig = (page or {}).subjectPageTitle
if page_orig then
if page_orig.isRedirect then
return 'redirect'
end
if page_orig:inNamespace(mw.title.new("Template:Ex").namespace) then
return 'template'
end
if page_orig:inNamespace(mw.title.new("Module:Ex").namespace) then
return 'module'
end
if page_orig:inNamespace(mw.title.new("Category:Ex").namespace) then
return 'category'
end
if page_orig:inNamespace(mw.title.new("File:Ex").namespace) then
return 'file'
end
if page_orig:inNamespace(mw.title.new("Draft:Ex").namespace) then
return 'draft'
end
if page_orig:inNamespace(mw.title.new("Portal:Ex").namespace) then
return 'portal'
end
if page_orig:inNamespace(mw.title.new("PJ:Ex").namespace) then
return '专题'
end
if page_orig:inNamespaces(
mw.title.new("MediaWiki:Ex").namespace,
mw.title.new("TimedText:Ex").namespace,
mw.title.new("Media:Ex").namespace,
mw.title.new("Special:Ex").namespace,2600,2300,2302)
then
return '非条目'
end
end
return class_default
end
function p.getClassByPage(input_data)
local page_name = input_data
if type((input_data or {}).args) ~= type(nil) then --input_data is a frame
page_name = input_data.args['1'] or input_data.args[1]
end
local page = (type(page_name) ~= type(nil)) and ({pcall(mw.title.new, page_name)})[2] or mw.title.getCurrentTitle()
local talk_page = (page or {}).talkPageTitle
if talk_page then
local class_result = p.getClassByWikitext(talk_page:getContent() or "")
return p.getAutoClass(talk_page.fullText, class_result)
end
return ''
end
local function processWPBSRedirect(input_text)
local text = input_text
local norm_name = "WPBS"
local match_list = { --Template:WikiProject banner shell的重定向匹配表
"wiki%s*project%s*banner%s*shell",
"w?pj?%s*banner%s*shell",
"wiki%s*project%s*banners",
"multiple%s*wikiprojects?",
"wiki%s*project%s*shell",
"[維维]基[专專][题題][橫横]幅",
"多?[個个]?[維维]?基?[专專][题題][橫横]幅",
"[維维]基[专專][题題]",
"多?[個个]?[維维]?基?[专專][题題]",
"[专專][题題][橫横]幅",
}
local lotext = mw.ustring.gsub(mw.ustring.lower(text), "_", " ")
local normtext = text
for i = 1,#match_list do
local j, k = mw.ustring.find(lotext, match_list[i])
while j ~= nil do
normtext = mw.ustring.sub(normtext, 1, j-1)..norm_name..mw.ustring.sub(normtext, k+1, -1)
lotext = mw.ustring.gsub(mw.ustring.lower(normtext), "_", " ")
j, k = mw.ustring.find(lotext, match_list[i])
end
end
normtext = mw.ustring.gsub(normtext, "[Ww][Pp][Bb][Ss]", "WikiProject banner shell")
text=normtext
return text
end
local function getTemplateSection(input_text)
local text = (input_text or '')..'\n'
local i,j = mw.ustring.find(text, "%=+[^\n]*%=+%s*\n")
if i == nil then
i,j = mw.ustring.find(text, "%{%{%s*[Dd][Yy][Kk][Ee]ntry/archive")
end
text = mw.ustring.sub(text, 1, (i or 0)-1)
return text
end
local function getWPBSTemplateContent(input_text)
local text = input_text
local re_WPBS_header = "%{%{%s*[Ww]iki[Pp]roject[%s_]*[Bb]anner[%s_]*[Ss]hell"
local it = text
local old_it = text.."-"
while it ~= old_it do
if not mw.ustring.match(it, re_WPBS_header) then break end
old_it = it
it = mw.ustring.gsub(it, "%{[^%{%}]*%}", "")
end
local WPBS = mw.ustring.match(old_it, re_WPBS_header.."[^%{%}]*%}%}") or ''
return WPBS
end
function p.getClassByWikitext(input_data)
local wikitext = input_data
if type((input_data or {}).args) ~= type(nil) then --input_data is a frame
wikitext = input_data.args['1'] or input_data.args[1] or ''
end
local text=wikitext or ''
text = getTemplateSection(text) --取得掛模板的段落
text = processWPBSRedirect(text) --處理WPBS的重定向頁
local WPBS = getWPBSTemplateContent(text) --讀取WPBS模板內容
--匹配 | class = XX
local select_class = mw.ustring.gsub(mw.ustring.match(WPBS, "%|%s*[Cc][Ll][Aa][Ss][Ss]%s*%=%s*([^%|%}]+)") or '', '\n', '')
return select_class
end
return p