Module:Constraints/search

local _lang = 'en' local _id local _datatype local _fallback

local function getDatatype if not _datatype then _datatype = mw.wikibase.getEntity(_id).datatype end return _datatype end

local function search_queryAsURL(query, text) if #query > 300 then return nil else return mw.ustring.format('%s', mw.uri.encode(query, 'PATH'), text or 'search') end end

local p = {}

function p.implemented(property) local curdatatype = mw.wikibase.getEntity(property).datatype return (curdatatype == "external-id") or (curdatatype == "string") or (curdatatype == "wikibase-property") or (curdatatype == "wikibase-lexeme") or (curdatatype == "wikibase-form") or (curdatatype == "wikibase-sense") or (curdatatype == "wikibase-item" and property ~= "P400" and property ~= "P486") end

function p.buildOneOf(values) if not p.implemented(_id) then return nil end local query = "haswbstatement:" .. _id for _, value in ipairs(values) do query = query .. " -haswbstatement:" .. _id .. "=" .. value end return search_queryAsURL(query) end

function p.buildRequiredClaim(property, values) if not (p.implemented(_id) and p.implemented(property)) then return nil end local query if #values == 0 then query = "haswbstatement:" .. _id .. " -haswbstatement:" .. property else query = "haswbstatement:" .. _id for _, value in ipairs(values) do query = query .. " -haswbstatement:" .. property .. "=" .. value end end return search_queryAsURL(query) end

function p.buildConflictsWith(property, values) if not (p.implemented(_id) and p.implemented(property)) then return nil end if #values > 1 then return nil end local query if #values == 0 then query = "haswbstatement:" .. _id .. " haswbstatement:" .. property else query = "haswbstatement:" .. _id .. " haswbstatement:" .. property .. "=" .. values[1] end return search_queryAsURL(query) end

function p.setLanguage(lang) _lang = lang _fallback = nil end

function p.setId(id) _id = id	_datatype = nil end

function p.setDatatype(datatype) _datatype = datatype end

return p