Zeroclanzhang(讨论 | 贡献) 无编辑摘要 |
Zeroclanzhang(讨论 | 贡献) 无编辑摘要 |
||
第6行: | 第6行: | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
local category = args[1] | local category = args[1] | ||
local | local subcategory = args['Subcategory'] | ||
local namespace = args['namespace'] | |||
local text = args['text'] or args[3] | local text = args['text'] or args[3] | ||
local icon = args['icon'] | local icon = args['icon'] | ||
-- 使用缓存结果,减少查询 | -- 使用缓存结果,减少查询 | ||
local cacheKey = category .. ( | local cacheKey = category .. (subcategory or '') .. (namespace or '') .. (text or '') | ||
if p.cache[cacheKey] then | if p.cache[cacheKey] then | ||
return p.cache[cacheKey] | return p.cache[cacheKey] | ||
end | end | ||
-- | -- 构建查询字符串 | ||
local query = '[[Category:' .. category .. ']]' | local query = '[[Category:' .. category .. '|+depth=2]]' | ||
if | if subcategory then | ||
query = query .. '[[Category:' .. subcategory .. ']]' | |||
query = query .. '[[Category:' .. | end | ||
query = query .. '[[' .. | if namespace then | ||
query = query .. '[[Category:' .. namespace .. ':+]]' | |||
else | |||
query = query .. '[[:+]]' | |||
end | end | ||
-- 执行查询 | -- 执行查询 | ||
local results = frame:extensionTag('ask', query .. '| | local results = frame:extensionTag('ask', query .. '|sep=') | ||
-- 缓存查询结果 | -- 缓存查询结果 |
2024年1月24日 (三) 14:11的版本
可在模块:Navplate SMW row/doc创建此模块的帮助文档
-- Module:NavplateSMWRow
local p = {}
local mw = require('mw')
function p.navplateSmwRow(frame)
local args = frame:getParent().args
local category = args[1]
local subcategory = args['Subcategory']
local namespace = args['namespace']
local text = args['text'] or args[3]
local icon = args['icon']
-- 使用缓存结果,减少查询
local cacheKey = category .. (subcategory or '') .. (namespace or '') .. (text or '')
if p.cache[cacheKey] then
return p.cache[cacheKey]
end
-- 构建查询字符串
local query = '[[Category:' .. category .. '|+depth=2]]'
if subcategory then
query = query .. '[[Category:' .. subcategory .. ']]'
end
if namespace then
query = query .. '[[Category:' .. namespace .. ':+]]'
else
query = query .. '[[:+]]'
end
-- 执行查询
local results = frame:extensionTag('ask', query .. '|sep=')
-- 缓存查询结果
p.cache[cacheKey] = results
-- 格式化输出
return mw.html.create('div')
:addClass('template-navplate-item')
:node(mw.html.create('div')
:addClass('template-navplate-item__label')
:wikitext(icon and '[[File:' .. icon .. '|20px|link=]]' or '')
:wikitext(text)
)
:node(mw.html.create('div')
:addClass('template-navplate-item__list')
:wikitext(results)
)
:allDone()
end
p.cache = {}
return p