模块:Navplate SMW row:修订间差异

来自决策链云智库
无编辑摘要
无编辑摘要
第7行: 第7行:
     local category = args[1]
     local category = args[1]
     local property = args[2]
     local property = args[2]
     local text = args[3]
    local subcategory = args[3]
     local icon = args[4]
     local text = args[4]
     local icon = args[5]


     -- 使用缓存结果,减少查询
     -- 使用缓存结果,减少查询
     local cacheKey = category .. (property or '') .. (text or '')
     local cacheKey = category .. (property or '') .. (subcategory or '') .. (text or '')
     if p.cache[cacheKey] then
     if p.cache[cacheKey] then
         return p.cache[cacheKey]
         return p.cache[cacheKey]
第17行: 第18行:


     -- 构建查询
     -- 构建查询
     local query = '[[Category:' .. category .. '|+depth=2]]'
     local query = '[[Category:' .. category .. ']]'
     if property and property:find("::") then
     if property and subcategory then
         local propertyParts = mw.text.split(property, "::")
         query = query .. '[[Category:' .. subcategory .. ']][[' .. property .. '::' .. subcategory .. ']]'
        -- 确保propertyParts有两个部分
        if #propertyParts == 2 then
            query = query .. '[[Category:' .. propertyParts[1] .. ']]'
            query = query .. '[[Property:' .. propertyParts[1] .. '::' .. propertyParts[2] .. ']]'
        end
     end
     end



2024年1月24日 (三) 15:36的版本

可在模块: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 property = args[2]
    local subcategory = args[3]
    local text = args[4]
    local icon = args[5]

    -- 使用缓存结果,减少查询
    local cacheKey = category .. (property or '') .. (subcategory or '') .. (text or '')
    if p.cache[cacheKey] then
        return p.cache[cacheKey]
    end

    -- 构建查询
    local query = '[[Category:' .. category .. ']]'
    if property and subcategory then
        query = query .. '[[Category:' .. subcategory .. ']][[' .. property .. '::' .. subcategory .. ']]'
    end

    -- 执行查询
    local results = frame:extensionTag('ask', query .. '|format=list')

    -- 缓存查询结果
    p.cache[cacheKey] = results

    -- 格式化输出
    local div = mw.html.create('div'):addClass('template-navplate-item')

    local labelDiv = mw.html.create('div'):addClass('template-navplate-item__label')
    if icon then
        labelDiv:wikitext('[[File:' .. icon .. '|20px|link=]]')
    end
    labelDiv:wikitext(text)
    div:node(labelDiv)

    local listDiv = mw.html.create('div'):addClass('template-navplate-item__list')
    listDiv:wikitext(results)
    div:node(listDiv)

    return div:allDone()
end

p.cache = {}
return p