「モジュール:SDC tracking」の版間の差分

ナビゲーションに移動 検索に移動
add namespace filter to allow the code to work in file namespace only, unless namespace name is listed
(1版 をインポートしました)
bsd>Jarekt
(add namespace filter to allow the code to work in file namespace only, unless namespace name is listed)
15行目: 15行目:
]]
]]


-- ==================================================
-- === External functions ===========================
-- ==================================================
local p = {}


-- =======================================
-- === Dependencies ======================
-- =======================================
require('strict') -- used for debugging purposes as it detects cases of unintended global variables


local function Category(catName)
-- =======================================
if catName then
-- === Local Functions ===================
return '[[Category:' .. catName .. ']]'
-- =======================================
else
return ''
end
end


function p.SDC_statement_exist(frame)
local function getArgs(frame)
-- skip if we are not in file namespace
local page = mw.title.getCurrentTitle()
if page.namespace~=6 then
return ''
end
-- 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
local output = ''
if entity and entity.statements and entity.statements[property] then
local entity = mw.wikibase.getEntity()
return positive
if entity and entity.statements and entity.statements[args.property] then
output = Category(args.positive_category)
else
else
output = Category(args.negative_category)
return negative
end
end
return output
end
end


-----------------------------------------------------------------------
-----------------------------------------------------------------------
function p.SDC_statement_has_value (frame)
function p._statement_has_value(entity, property, value, matching, mismatching, missing)
-- skip if we are not in file namespace
value = value or ''
local page = mw.title.getCurrentTitle()
if entity and entity.statements and entity.statements[property] then
if page.namespace~=6 then
for _, statement in pairs( entity:getBestStatements( property )) do
return
end
-- 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
if not args.value then
return
end
-- check if statement exist
local entity = mw.wikibase.getEntity()
if entity and entity.statements and entity.statements[args.property] then
for _, statement in pairs( entity:getBestStatements( args.property )) do
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==args.value then
if val==value then
return Category(args.matching_category)
return matching
end
end
end
end
end
end
return Category(args.mismatching_category)
return mismatching
else
else
return Category(args.missing_category)
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
匿名利用者

案内メニュー