Module:Constraints/search

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Constraints/search/doc

Code

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('[https://www.wikidata.org/wiki/Special:Search?search=%s %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 ~= "P1433" and property ~= "P2860")
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.buildDescriptionLanguage(property, languageCodes)
	if not (p.implemented(_id) and p.implemented(property)) then
		return nil
	end
	local query = "haswbstatement:" .. _id
	for _, languageCode in ipairs(languageCodes) do
		query = query .. " -hasdescription:" .. languageCode
	end
	return search_queryAsURL(query)	
end

function p.buildLabelLanguage(property, languageCodes)
	if not (p.implemented(_id) and p.implemented(property)) then
		return nil
	end
	local query = "haswbstatement:" .. _id
	for _, languageCode in ipairs(languageCodes) do
		query = query .. " -haslabel:" .. languageCode
	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