“模块:SMWUtils”的版本间的差异
跳到导航
跳到搜索
第28行: | 第28行: | ||
if r.printouts[prop] then | if r.printouts[prop] then | ||
for k, t in ipairs(r.printouts[prop]) do | for k, t in ipairs(r.printouts[prop]) do | ||
− | table.insert(p_values, t.fulltext) | + | table.insert(p_values, t.fulltext or t) |
end | end | ||
end | end |
2021年7月4日 (日) 14:32的最新版本
此模块的文档可以在模块:SMWUtils/doc创建
local p = {} function p.exists(query) return mw.smw.ask(query.."|limit=1") end function p.query_pages(query) local res = mw.smw.getQueryResult(query) local ret = {} for j, r in ipairs(res.results) do table.insert(ret, r.fulltext) end return ret end function p.query_page_properties(query, properties) local real_query = query for i, p in ipairs(properties) do real_query = real_query .. "|?" .. p end local res = mw.smw.getQueryResult(real_query) local ret = {} for i, r in ipairs(res.results) do local ans = {} local title = r.fulltext for j, prop in ipairs(properties) do local p_values = {} if r.printouts[prop] then for k, t in ipairs(r.printouts[prop]) do table.insert(p_values, t.fulltext or t) end end ans[prop] = p_values end table.insert(ret, {title=title, properties=ans}) end return ret end local QueryBuilder = {content = ""} function QueryBuilder:new() local n = {} setmetatable(n, QueryBuilder) self.__index = self return n end function QueryBuilder:cat(v) local n = self:new() n.content = self.content .. v return n end function QueryBuilder:bracket(t) return self:cat('[['..t..']] ') end function QueryBuilder:or_() return self:cat(' OR ') end function QueryBuilder:done() return self.content end function QueryBuilder:page(name) return self:bracket(name) end function QueryBuilder:category(name) return self:bracket("Category:"..name) end function QueryBuilder:exists(prop) return self:bracket(prop..'::+') end function QueryBuilder:property(prop, value) return self:bracket(prop..'::'..value) end function QueryBuilder:get_pages() return p.query_pages(self:done()) end function QueryBuilder:get_properties(properties) return p.query_properties(self:done(), properties) end p.QueryBuilder = QueryBuilder function p.query() return QueryBuilder:new() end return p