“模块:SMWUtils”的版本间的差异
跳到导航
跳到搜索
第10行: | 第10行: | ||
for j, r in ipairs(res.results) do | for j, r in ipairs(res.results) do | ||
table.insert(ret, r.fulltext) | 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) | ||
+ | end | ||
+ | end | ||
+ | ans[prop] = p_values | ||
+ | end | ||
+ | table.insert(ret, {title=title, properties=ans}) | ||
end | end | ||
return ret | return ret |
2021年1月3日 (日) 00:42的版本
此模块的文档可以在模块: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) end end ans[prop] = p_values end table.insert(ret, {title=title, properties=ans}) end return ret end return p