「モジュール:SDC tracking」の版間の差分
ナビゲーションに移動
検索に移動
bad>Jarekt (SDC_statement_has_value should handle cases with multiple values) |
細 (1版 をインポートしました) |
||
(2人の利用者による、間の2版が非表示) | |||
15行目: | 15行目: | ||
]] | ]] | ||
-- ======================================= | |||
-- === Dependencies ====================== | |||
-- ======================================= | |||
require('strict') -- used for debugging purposes as it detects cases of unintended global variables | |||
-- ======================================= | |||
-- === Local Functions =================== | |||
-- ======================================= | |||
function | local function getArgs(frame) | ||
-- switch to lowercase parameters to make them case independent | -- switch to lowercase parameters to make them case independent | ||
local args = {} | local args = {} | ||
43行目: | 33行目: | ||
end | end | ||
end | end | ||
local page = mw.title.getCurrentTitle() | |||
if (page.namespace == 6) then | |||
args.entity = mw.wikibase.getEntity() | |||
elseif args.item then | |||
args.entity = mw.wikibase.getEntity(args.item) | |||
end | |||
return args | |||
end | |||
----------------------------------------------------------------------- | |||
local function Category(catName) | |||
if catName then | |||
return '[[Category:' .. catName .. ']]' | |||
else | |||
return '' | |||
end | |||
end | |||
-- ================================================== | |||
-- === External functions =========================== | |||
-- ================================================== | |||
local p = {} | |||
-- =========================================================================== | |||
-- === Version of the function to be called from other LUA codes | |||
-- =========================================================================== | |||
function p._statement_exist(entity, property, positive, negative) | |||
-- check if statement exist | -- check if statement exist | ||
if entity and entity.statements and entity.statements[property] then | |||
return positive | |||
if entity and entity.statements and entity.statements[ | |||
else | else | ||
return negative | |||
end | end | ||
end | end | ||
----------------------------------------------------------------------- | ----------------------------------------------------------------------- | ||
function p. | function p._statement_has_value(entity, property, value, matching, mismatching, missing) | ||
value = value or '' | |||
if entity and entity.statements and entity.statements[property] then | |||
for _, statement in pairs( entity:getBestStatements( property )) do | |||
if entity and entity.statements and entity.statements[ | |||
for _, statement in pairs( entity:getBestStatements( | |||
if (statement.mainsnak.snaktype == "value") then | if (statement.mainsnak.snaktype == "value") then | ||
local val = statement.mainsnak.datavalue.value | local val = statement.mainsnak.datavalue.value | ||
85行目: | 82行目: | ||
val = val.text | val = val.text | ||
end | end | ||
if val== | if val==value then | ||
return | return matching | ||
end | end | ||
end | end | ||
end | end | ||
return | return mismatching | ||
else | else | ||
return | return missing | ||
end | end | ||
end | |||
-- =========================================================================== | |||
-- === Versions of the function to be called from template namespace | |||
-- =========================================================================== | |||
function p.SDC_statement_exist(frame) | |||
local args = getArgs(frame) | |||
-- skip if we are not in file namespace | |||
local page = mw.title.getCurrentTitle() | |||
if not (page.namespace==6 or page.namespace==args.namespace or args.namespace=='all') then | |||
return '' | |||
end | |||
local output = p._statement_exist(args.entity, args.property, args.positive_category, args.negative_category) | |||
return Category(output) | |||
end | |||
----------------------------------------------------------------------- | |||
function p.SDC_statement_has_value (frame) | |||
local args = getArgs(frame) | |||
-- skip if we are not in file namespace | |||
local page = mw.title.getCurrentTitle() | |||
if not (page.namespace==6 or page.namespace==args.namespace or args.namespace=='all') then | |||
return '' | |||
end | |||
local output = p._statement_has_value(args.entity, args.property, args.value, | |||
args.matching_category, args.mismatching_category, args.missing_category) | |||
return Category(output) | |||
end | end | ||
return p | return p |
2023年8月6日 (日) 13:22時点における最新版
このモジュールについての説明文ページを モジュール:SDC tracking/doc に作成できます
--[[
__ __ _ _ ____ ____ ____ _ _ _
| \/ | ___ __| |_ _| | ___ _/ ___|| _ \ / ___| | |_ _ __ __ _ ___| | _(_)_ __ __ _
| |\/| |/ _ \ / _` | | | | |/ _ (_)___ \| | | | | | __| '__/ _` |/ __| |/ / | '_ \ / _` |
| | | | (_) | (_| | |_| | | __/_ ___) | |_| | |___ | |_| | | (_| | (__| <| | | | | (_| |
|_| |_|\___/ \__,_|\__,_|_|\___(_)____/|____/ \____|___\__|_| \__,_|\___|_|\_\_|_| |_|\__, |
|_____| |___/
This module is intended to be the engine behind "Template:SDC_tracking".
Please do not modify this code without applying the changes first at
"Module:SDC_tracking/sandbox" and testing at "Module:SDC_tracking/testcases".
Authors and maintainers:
* User:Jarekt - original version
]]
-- =======================================
-- === Dependencies ======================
-- =======================================
require('strict') -- used for debugging purposes as it detects cases of unintended global variables
-- =======================================
-- === Local Functions ===================
-- =======================================
local function getArgs(frame)
-- switch to lowercase parameters to make them case independent
local args = {}
for name, value in pairs( frame.args ) do
if value ~= '' then -- nuke empty strings
args[string.lower(name)] = value
end
end
local page = mw.title.getCurrentTitle()
if (page.namespace == 6) then
args.entity = mw.wikibase.getEntity()
elseif args.item then
args.entity = mw.wikibase.getEntity(args.item)
end
return args
end
-----------------------------------------------------------------------
local function Category(catName)
if catName then
return '[[Category:' .. catName .. ']]'
else
return ''
end
end
-- ==================================================
-- === External functions ===========================
-- ==================================================
local p = {}
-- ===========================================================================
-- === Version of the function to be called from other LUA codes
-- ===========================================================================
function p._statement_exist(entity, property, positive, negative)
-- check if statement exist
if entity and entity.statements and entity.statements[property] then
return positive
else
return negative
end
end
-----------------------------------------------------------------------
function p._statement_has_value(entity, property, value, matching, mismatching, missing)
value = value or ''
if entity and entity.statements and entity.statements[property] then
for _, statement in pairs( entity:getBestStatements( property )) do
if (statement.mainsnak.snaktype == "value") then
local val = statement.mainsnak.datavalue.value
if val.id then
val = val.id
elseif val.text then
val = val.text
end
if val==value then
return matching
end
end
end
return mismatching
else
return missing
end
end
-- ===========================================================================
-- === Versions of the function to be called from template namespace
-- ===========================================================================
function p.SDC_statement_exist(frame)
local args = getArgs(frame)
-- skip if we are not in file namespace
local page = mw.title.getCurrentTitle()
if not (page.namespace==6 or page.namespace==args.namespace or args.namespace=='all') then
return ''
end
local output = p._statement_exist(args.entity, args.property, args.positive_category, args.negative_category)
return Category(output)
end
-----------------------------------------------------------------------
function p.SDC_statement_has_value (frame)
local args = getArgs(frame)
-- skip if we are not in file namespace
local page = mw.title.getCurrentTitle()
if not (page.namespace==6 or page.namespace==args.namespace or args.namespace=='all') then
return ''
end
local output = p._statement_has_value(args.entity, args.property, args.value,
args.matching_category, args.mismatching_category, args.missing_category)
return Category(output)
end
return p