Module:Constraints

From Gratisdata

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

local gd        = require('Module:Gratisdata')
local fb        = require('Module:Fallback')
local dtype     = require('Module:Datatype')
local TableTools= require('Module:TableTools')
local SPARQL    = require('Module:Constraints/SPARQL')
local search    = require('Module:Constraints/search')
local i18n_msgs = mw.loadData('Module:i18n/constraints')

local defaultlang = mw.getCurrentFrame():preprocess("{{int:lang}}")

local pConstraint          = "P142"
local pFormatPattern       = "P148"
local pProperty            = "P341"
local pItems               = "P340"
local pClasses             = "P237"
local pRelation            = "P238"
local pMinQuantity         = "P87"
local pMaxQuantity         = "P118"
local pMinDate             = "P120"
local pMaxDate             = "P123"
local pNamespace           = "P471"
local pMandatory           = "P286"
local pGroupBy             = "P485"
local pExceptions          = "P349"
local pInstanceOf          = "P3"
local pSubclassOf          = "P12"
local pComment             = "P347"
local pComment2            = "P346"
local pSeparator           = "P348"
local pConstraintScope     = "P339"
local pPropertyScope       = "P338"
local pLanguageCode        = "P159"

local pReplacementProperty = "P396"
local pReplacementValue = "P395"

local cMandatory           = "Q678"
local cSuggested           = "Q669"

local cInstanceOf          = "Q788"
local cSubclassOf          = "Q2157"
local cInstanceOfSubclassOf= "Q2159"

local cAllConstraints = {
	cSingleValue         = "Q794",
	cSingleBestValue     = "Q803",
	cFormat              = "Q673",
	cUniqueValue         = "Q776",
	cConflictsWith       = "Q798",
	cItem                = "Q668",
	cType                = "Q620",
	cQualifiers          = "Q795",
	cCommonsLink         = "Q1042",
	cDiffWithinRange     = "Q2150",
-- cDiffWithinRangeWithLink"] = "Q2150",
	cInverse             = "Q885",
	cMultiValue          = "Q797",
	cOneOf               = "Q782",
	cRange               = "Q2149",
	cSymmetric           = "Q814",
	cTargetRequiredClaim = "Q796",
	cValueType           = "Q789",
	cUnits               = "Q2154",
	cMandatoryQualifiers = "Q804",
	cContemporary        = "Q834",
	cNoBounds            = "Q2153",
	cInteger             = "Q2161",
	cNoneOf              = "Q942",
	cScope               = "Q779",
	cEntityType          = "Q781",
	cCitationNeeded      = "Q841",
	cOneofQualifierValue = "Q2183", -- test
	cLexemeCategory      = "Q2184",
	cLexemeValueCategory = "Q2185",
	cLexemeLanguage      = "Q2155",
	cLabelLanguage       = "Q2156",
	cDescriptionLanguage = "Q2950",
}

local cSomevalue           = 'somevalue'
local cNovalue             = 'novalue'

local cItemonlyConstraints     = { cContemporary }
local cItemOrPropertyonlyConstraints     = {cInverse, cOneOf, cSymmetric, cValueType, cTargetRequiredClaim, cNoneOf}
local cQuantityonlyConstraints = { cUnits, cNoBounds, cInteger }

local cFormatapplicable = {"math","commonsMedia","string","external-id","url","monolingualtext","tabular-data","geo-shape","musical-notation"} 
local cCommonsLinkapplicable = {"commonsMedia","geo-shape","string","tabular-data"}
local cRangeapplicable = {"quantity","time"}  -- also cDiffWithinRange


local function i18n(str)
	return fb._langSwitch(i18n_msgs[str], defaultlang)
end


local function throw(error_type, ...)
	error(string.format(i18n(error_type), ...), 0);
end


local function wikifyQKey(id)
	local label = gd._getLabel(id, defaultlang)
	return "[[" .. id .. "|" .. label .. " <small>(" .. id .. ")</small>]]"
end


local function wikifyPKey(id)
	local label = gd._getLabel(id, defaultlang)
	return "[[Property:" .. id .. "|" .. label .. " <small>(" .. id .. ")</small>]]"
end


local function wikifyQKeys(ids)
	local values = {}
	for _, id in pairs(ids) do
		if id == cSomevalue then
			table.insert(values, "somevalue")
		elseif id == cNovalue then
			table.insert(values, "novalue")
		elseif #ids > 100 then
			table.insert(values, "[[" .. id .. "]]")
		else
			table.insert(values, wikifyQKey(id))
		end
	end
	return table.concat(values, ", ");
end


local function wikifyPKeys(ids)
	local values = {}
	for _, id in pairs(ids) do
		table.insert(values, wikifyPKey(id))
	end
	return table.concat(values, ", ");
end


local function contains(array, value)
	for key, value in pairs(array) do
		if array[key] == value then
			return true
		end
	end

	return false
end


local function hasQualifier(statement, qualifier_id)
	return
		(statement ~= nil) and
		(statement.qualifiers ~= nil) and
		(statement.qualifiers[qualifier_id] ~= nil) and
		(#(statement.qualifiers[qualifier_id]) > 0)
end


local function getQualifierSingleValue( statement, qualifier_id )
	if hasQualifier(statement, qualifier_id) == false then
		throw("error_missing_qualifier", wikifyPKey(qualifier_id));
	end

	local qualifiers = statement.qualifiers[qualifier_id]
	if (#qualifiers > 1) then
		error("Too many qualifiers " .. wikifyPKey(qualifier_id), 0);
	end

	local qualifier = qualifiers[1]

	if (qualifier.snaktype == "somevalue") then
		return cSomevalue;
	elseif (qualifier.snaktype == "novalue") then
		return cNovalue;
	end

	if (qualifier.datavalue == nil
	    or qualifier.datavalue.type == nil
	    or qualifier.datavalue.value == nil) then
		error("Unexpected qualifier " .. wikifyPKey(qualifier_id) .. " value", 0);
	end

	if (qualifier.datavalue.type == "wikibase-entityid") then
		if (qualifier.datavalue.value["entity-type"] == "property") then
			return "P" .. qualifier.datavalue.value["numeric-id"];
		else						
			return "Q" .. qualifier.datavalue.value["numeric-id"];
		end
	else
		return gd.formatSnak(qualifier, { displayformat = 'raw' });
	end
end


local function getQualifierValues( statement, qualifier_id )
	local result = {}
	if hasQualifier(statement, qualifier_id) then
		for _, qualifier in pairs(statement.qualifiers[qualifier_id]) do
			if (qualifier.snaktype == "somevalue") then
				table.insert(result, cSomevalue)
			elseif (qualifier.snaktype == "novalue") then
				table.insert(result, cNovalue)
			elseif (qualifier.datavalue == nil
			    or qualifier.datavalue.type == nil
			    or qualifier.datavalue.value == nil) then
				table.insert(result, nil)
			else
				if ( qualifier.datavalue.type == "string" ) then
					table.insert(result, qualifier.datavalue.value);
				elseif ( qualifier.datavalue.type == "wikibase-entityid" ) then
					if (qualifier.datavalue.value["entity-type"] == "property") then
						table.insert(result, "P" .. qualifier.datavalue.value["numeric-id"]);
					else
						table.insert(result, "Q" .. qualifier.datavalue.value["numeric-id"]);
					end
				else 
					error("Unknown qualifier type: " .. qualifier.datavalue.type, 0);
				end
			end
		end
	end
	return result;
end


local function getQualifierAtLeastOneValue(statement, qualifier_id)
	local result = getQualifierValues(statement, qualifier_id)
	if #result == 0 then
		throw("error_missing_qualifier", wikifyPKey(qualifier_id));
	end
	return result
end


local function isMandatoryConstraint(constraint)
	if hasQualifier(constraint, pMandatory) then
		local value = getQualifierSingleValue(constraint, pMandatory)
		if value == cMandatory then
			return "true"
		elseif value ~= cSuggested then
			throw("error_unsupported_value", wikifyPKey(pMandatory), wikifyQKey(cMandatory))
		end
	end
	return ""
end


local function getOneOfQualifiers(constraint, qualifier_id1, qualifier_id2)
	if hasQualifier(constraint, qualifier_id1) then
		return getQualifierSingleValue(constraint, qualifier_id1)
	else
		return getQualifierSingleValue(constraint, qualifier_id2)
	end
end


local function getTypeRelation(constraint)
	local relation = getQualifierSingleValue(constraint, pRelation)

	if (relation == cInstanceOf) then
		return pInstanceOf
	elseif (relation == cSubclassOf) then
		return pSubclassOf
	elseif (relation == cInstanceOfSubclassOf) then
		return pInstanceOf,pSubclassOf
	else
		throw("error_unsupported_value", wikifyPKey(pRelation), wikifyQKeys({cInstanceOf, cSubclassOf}))
	end
end


local function getConstraintDescription(constraint, constraint_entity, property_id)
	--TODO: if somevalue or novalue is misused, it currently displays a module error, or unrelated number 0 or 1
	local type = constraint_entity.id
	
	local res = {}

	if (type == cAllConstraints.cSingleValue) then
		res.caption     = i18n("single_value_caption")
		res.description = i18n("single_value_description")
		res.query       = table.concat( {
			SPARQL.buildSingleValue(),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with single value constraints"

	elseif (type == cAllConstraints.cSingleBestValue) then
		res.caption     = i18n("single_best_value_caption")
		res.description = i18n("single_best_value_description")
		res.query       = table.concat( {
			SPARQL.buildSingleBestValue(),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with single best value constraints"

	elseif (type == cAllConstraints.cFormat) then
		res.caption     = string.format(i18n("format_caption"), 
				"<code><nowiki>" .. getQualifierSingleValue(constraint, pFormatPattern) .. "</nowiki></code>")
		res.description = i18n("format_description")
		res.query       = table.concat( {
			SPARQL.buildFormat(getQualifierSingleValue(constraint, pFormatPattern)),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with format constraints"

	elseif (type == cAllConstraints.cUniqueValue) then
		res.caption     = i18n("unique_value_caption")
		res.description = i18n("unique_value_description")
		res.query       = table.concat( {
			SPARQL.buildUniqueValue(),
			SPARQL.buildUniqueValueByValue(),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with unique value constraints"

	elseif (type == cAllConstraints.cConflictsWith) then
		local value = wikifyPKey(getQualifierSingleValue(constraint, pProperty))
		if hasQualifier(constraint, pItems) then
			value = value .. ": " .. wikifyQKeys(getQualifierValues(constraint, pItems))
		end
		res.caption     = string.format(i18n("conflicts_with_caption"), value)
		res.description = i18n("conflicts_with_description")
		res.query = table.concat( TableTools.compressSparseArray( {
			search.buildConflictsWith(
				getQualifierSingleValue(constraint, pProperty),
				getQualifierValues(constraint, pItems)
			),
			SPARQL.buildConflictsWith(
				getQualifierSingleValue(constraint, pProperty),
				getQualifierValues(constraint, pItems)
			),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		} ), ', ' )
		res.category    = "Properties with conflicts with constraints"

	elseif (type == cAllConstraints.cItem) then
		local value = wikifyPKey(getQualifierSingleValue(constraint, pProperty))
		if hasQualifier(constraint, pItems) then
			value = value .. ": " .. wikifyQKeys(getQualifierValues(constraint, pItems))
		end
		res.caption     = string.format(i18n("item_caption"),     value)
		res.description = string.format(i18n("item_description"), value)
		res.query = table.concat( TableTools.compressSparseArray( {
			search.buildRequiredClaim(
				getQualifierSingleValue(constraint, pProperty),
				getQualifierValues(constraint, pItems)
			),
			SPARQL.buildRequiredClaim(
				getQualifierSingleValue(constraint, pProperty),
				getQualifierValues(constraint, pItems)
			),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		} ), ', ' )
		res.category    = "Properties with constraints on items using them"

	elseif (type == cAllConstraints.cOneofQualifierValue) then
		local value = wikifyPKey(getQualifierSingleValue(constraint, pProperty))
		if hasQualifier(constraint, pItems) then
			value = value .. ": " .. wikifyQKeys(getQualifierValues(constraint, pItems))
		end
		res.caption     = "Qualifier:" .. string.format(i18n("item_caption"),     value)
		res.description = "test: qualifier should use one of the values listed" -- string.format(i18n("OneofQualifierValue_description"), value)
		res.category    = "Properties with constraints on for values of qualifiers being used"

	elseif (type == cAllConstraints.cType) then
		local types = wikifyQKeys(getQualifierAtLeastOneValue(constraint, pClasses))
		res.caption     = string.format(i18n("type_caption"), types)
		res.description = string.format(i18n("type_description"), wikifyPKey(getTypeRelation(constraint)), types)
		res.query       = table.concat( {
			SPARQL.buildType(
				getTypeRelation(constraint),
				getQualifierAtLeastOneValue(constraint, pClasses)
			),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with constraints on type"

	elseif (type == cAllConstraints.cQualifiers) then
		local qualifiers = getQualifierValues(constraint, pProperty)
		if qualifiers[1] == cNovalue then
			res.caption     = i18n("without_qualifier_caption")
			res.description = i18n("without_qualifier_description")
			--res.query       = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		else
			res.caption     = string.format(i18n("qualifiers_caption"), wikifyPKeys(getQualifierValues(constraint, pProperty)))
			res.description = i18n("qualifiers_description")
			res.query       = table.concat( {
				SPARQL.buildQualifiers(qualifiers),
				--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
			}, ', ' )
		end
		res.category    = "Properties with qualifiers constraints"
	elseif (type == cAllConstraints.cCommonsLink) then
		res.caption     = string.format(i18n("commons_caption"), hasQualifier(constraint, pNamespace) and getQualifierSingleValue(constraint, pNamespace) or "") --Better message needed for main namespace
		res.description = i18n("commons_description")
		--res.query       = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		res.category    = "Properties with Commons link constraints"

	elseif (type == cAllConstraints.cDiffWithinRange) then
		local prop = wikifyPKey(getQualifierSingleValue(constraint, pProperty))
		local min  = getQualifierSingleValue(constraint, pMinQuantity)
		if min == cNovalue then
			min = "−∞"
		end
		local max  = getQualifierSingleValue(constraint, pMaxQuantity)
		if max == cNovalue then
			max = "+∞"
		end
		res.caption     = string.format(i18n("difference_caption"),     prop, min, max)
		res.description = string.format(i18n("difference_description"), prop, min, max)
		--res.query       = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		res.category    = "Properties with difference within range constraints"

--	elseif (type == cDiffWithinRangeWithLink) then
--		local prop = wikifyPKey(getQualifierSingleValue(constraint, pProperty))
--		local min  = getQualifierSingleValue(constraint, pMinQuantity)
--		if min == cNovalue then
--			min = "−∞"
--		end
--		local max  = getQualifierSingleValue(constraint, pMaxQuantity)
--		if max == cNovalue then
--			max = "+∞"
--		end
--		local itemlinkprop = wikifyPKey(getQualifierSingleValue(constraint, pProperty))
--		res.caption     = string.format(i18n("difference_with_link_caption"),     prop, itemlinkprop, min, max)
--		res.description = string.format(i18n("difference_with_link_description"), prop, itemlinkprop, min, max)
--		res.query       = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982

	elseif (type == cAllConstraints.cInverse) then
		res.caption     = string.format(i18n("inverse_caption"),     wikifyPKey(getQualifierSingleValue(constraint, pProperty)))
		res.description = string.format(i18n("inverse_description"), wikifyPKey(property_id), wikifyPKey(getQualifierSingleValue(constraint, pProperty)))
		res.query       = table.concat( {
			SPARQL.buildInverse(getQualifierSingleValue(constraint, pProperty)),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with inverse constraints"

	elseif (type == cAllConstraints.cMultiValue) then
		res.caption     = i18n("multi_value_caption")
		res.description = i18n("multi_value_description")
		res.query       = table.concat( {
			SPARQL.buildMultiValue(),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with multi value constraints"

	elseif (type == cAllConstraints.cOneOf) then
		res.caption     = string.format(i18n("one_of_caption"), wikifyQKeys(getQualifierValues(constraint, pItems)))
		res.description = i18n("one_of_description")
		res.query       = table.concat( TableTools.compressSparseArray( {
			search.buildOneOf(getQualifierValues(constraint, pItems)),
			SPARQL.buildOneOf(getQualifierValues(constraint, pItems)),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		} ), ', ' )
		res.category    = "Properties with one-of constraints"

	elseif (type == cAllConstraints.cLexemeCategory)  then 
		res.caption     = string.format(i18n("lexeme_category_caption"), wikifyQKeys(getQualifierValues(constraint, pItems)))
		res.description = i18n("lexeme_category_description")
		--res.query       = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		res.category    = "Properties with lexical category constraints"

	elseif (type == cAllConstraints.cLexemeValueCategory)  then 
		res.caption     = string.format(i18n("lexeme_value_category_caption"), wikifyQKeys(getQualifierValues(constraint, pItems)))
		res.description = i18n("lexeme_value_category_description")
		res.query       = "" -- SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		res.category    = "Properties with lexical category constraints on value"

		
	elseif (type == cAllConstraints.cLexemeLanguage)  then 
		res.caption     = string.format(i18n("lexeme_language_caption"), wikifyQKeys(getQualifierValues(constraint, pItems)))
		res.description = i18n("lexeme_language_description")
		--res.query       = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		res.category    = "Properties with lexeme language constraints"

	elseif (type == cAllConstraints.cRange) then
		local min = getOneOfQualifiers(constraint, pMinQuantity, pMinDate)
		if min == cNovalue then
			min = "−∞"
		end
		if min == cSomevalue then
			min = i18n("now")
		end
		local max = getOneOfQualifiers(constraint, pMaxQuantity, pMaxDate)
		if max == cNovalue then
			max = "+∞"
		end
		if max == cSomevalue then
			max = i18n("now")
		end
		res.caption     = string.format(i18n("range_caption"),     min, max)
		res.description = string.format(i18n("range_description"), min, max)
		--res.query       = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		res.category    = "Properties with range constraints"

	elseif (type == cAllConstraints.cSymmetric) then
		res.caption     = i18n("symmetric_caption")
		res.description = i18n("symmetric_description")
		res.query       = table.concat( {
			SPARQL.buildSymmetric(),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with symmetric constraints"

	elseif (type == cAllConstraints.cTargetRequiredClaim) then
		local value = wikifyPKey(getQualifierSingleValue(constraint, pProperty))
		if hasQualifier(constraint, pItems) then
			value = value .. ": " .. wikifyQKeys(getQualifierValues(constraint, pItems))
		end
		res.caption     = string.format(i18n("target_item_caption"),     value)
		res.description = string.format(i18n("target_item_description"), value)
		res.query       = table.concat( TableTools.compressSparseArray( {
			SPARQL.buildTargetRequiredClaim(
				getQualifierSingleValue(constraint, pProperty),
				getQualifierValues(constraint, pItems)
			),
			SPARQL.buildTargetRequiredClaimByValue(
				getQualifierSingleValue(constraint, pProperty),
				getQualifierValues(constraint, pItems)
			),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		} ), ', ' )
		res.category    = "Properties with target required claim constraints"

	elseif (type == cAllConstraints.cValueType) then
		local types = wikifyQKeys(getQualifierAtLeastOneValue(constraint, pClasses))
		res.caption     = string.format(i18n("value_type_caption"), types)
		res.description = string.format(i18n("value_type_description"), wikifyPKey(getTypeRelation(constraint)), wikifyPKey(getTypeRelation(constraint)), types)
		res.query       = table.concat( {
			SPARQL.buildValueType(
				getTypeRelation(constraint),
				getQualifierAtLeastOneValue(constraint, pClasses)
			),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with constraints on type"

	elseif (type == cAllConstraints.cUnits) then
		if hasQualifier(constraint, pItems) then
			local units = wikifyQKeys(getQualifierValues(constraint, pItems))
			res.caption     = string.format(i18n("units_caption"),     units)
			res.description = string.format(i18n("units_description"), units)
			--res.query = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		else
			res.caption     = i18n("nounits_caption")
			res.description = i18n("nounits_description")
			res.query       = table.concat( {
				SPARQL.buildUnits(),
				--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
			}, ', ' )
		end
		res.category    = "Properties with units constraints"

	elseif (type == cAllConstraints.cMandatoryQualifiers) then
		res.caption     = string.format(i18n("mandatory_qualifier_caption"), wikifyPKey(getQualifierSingleValue(constraint, pProperty))) -- fixme: multiple?
		res.description = i18n("mandatory_qualifier_description")
		res.query       = table.concat( {
			SPARQL.buildMandatoryQualifiers(getQualifierValues(constraint, pProperty)),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with required qualifiers constraints"

	elseif (type == cAllConstraints.cContemporary) then
		res.caption     = string.format(i18n("contemporary_caption"))
		res.description = string.format(i18n("contemporary_description"), wikifyPKey(property_id))
		res.query       = table.concat( {
			SPARQL.buildContemporary(),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with contemporary constraints"

	elseif (type == cAllConstraints.cNoBounds) then
		res.caption     = string.format(i18n("nobounds_caption"))
		res.description = string.format(i18n("nobounds_description"))
		res.query       = table.concat( {
			SPARQL.buildNoBounds(),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with no bounds constraints"

	elseif (type == cAllConstraints.cInteger) then
		res.caption     = string.format(i18n("integer_caption"))
		res.description = string.format(i18n("integer_description"))
		res.query       = table.concat( {
			SPARQL.buildInteger(),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with integer constraints"

	elseif (type == cAllConstraints.cNoneOf) then
		res.caption     = string.format(i18n("none_of_caption"), wikifyQKeys(getQualifierValues(constraint, pItems)))
		res.description = i18n("none_of_description")
		res.description = res.description .. "<br>" .. string.format(i18n("none_of_description2"), wikifyPKeys(getQualifierValues(constraint, pReplacementProperty))) -- fixme: skip if no pReplacementProperty
		res.description = res.description .. "<br>" .. string.format(i18n("none_of_description3"), wikifyQKeys(getQualifierValues(constraint, pReplacementValue))) -- fixme: skip if no pReplacementValue
		res.query       = table.concat( {
			SPARQL.buildOneOf(getQualifierValues(constraint, pItems), true),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with none-of constraints"

	elseif (type == cAllConstraints.cScope) then
		res.caption     = string.format(i18n("scope_caption"), wikifyQKeys(getQualifierValues(constraint, pPropertyScope)))
		res.description = i18n("scope_description")
		res.query       = table.concat( {
			SPARQL.buildScope(getQualifierValues(constraint, pPropertyScope)),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		}, ', ' )
		res.category    = "Properties with scope constraints"

	elseif (type == cAllConstraints.cEntityType) then
		res.caption     = string.format(i18n("entity_type_caption"), wikifyQKeys(getQualifierValues(constraint, pItems)))
		res.description = i18n("entity_type_description")
		--res.query       = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		res.category    = "Properties with entity type constraints"
		
	elseif (type == cAllConstraints.cCitationNeeded) then
		res.caption     = string.format(i18n("citation_needed_caption"))
		res.description = i18n("citation_needed_description")
		--res.query       = SPARQL.buildNewSPARQL(constraint.id) disabled per T274982
		res.category    = "Properties with citation needed constraints"

	elseif (type == cAllConstraints.cDescriptionLanguage) then
		local languageCode = getQualifierSingleValue(constraint, pLanguageCode)
		res.caption     = string.format(i18n("description_language_caption"), languageCode)
		res.description = string.format(i18n("description_language_description"), languageCode)
		res.query       = table.concat( TableTools.compressSparseArray( {
			search.buildDescriptionLanguage(property_id, languageCode),
			SPARQL.buildDescriptionLanguage(property_id, languageCode),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		} ), ', ' )
		res.category    = "Properties with description language constraints"
		
	elseif (type == cAllConstraints.cLabelLanguage) then
		local languageCode = getQualifierSingleValue(constraint, pLanguageCode)
		res.caption     = string.format(i18n("label_language_caption"), languageCode)
		res.description = string.format(i18n("label_language_description"), languageCode)
		res.query       = table.concat( TableTools.compressSparseArray( {
			search.buildLabelLanguage(property_id, languageCode),
			SPARQL.buildLabelLanguage(property_id, languageCode),
			--SPARQL.buildNewSPARQL(constraint.id), disabled per T274982
		} ), ', ' )
		res.category    = "Properties with label language constraints"

	else
		throw("error_unsupported_value", wikifyPKey(pConstraint), wikifyQKeys(cAllConstraints));

	end
	
	
	return res
end


local function getConstraintAnchor(constraint, constraint_entity)
	-- "P152":[{"mainsnak":{"snaktype":"value","property":"P152","datavalue":{"value":{"text":"Format","language":"en"}

	local anchor = constraint_entity.claims.P152[1].mainsnak.datavalue.value.text
	for _, statement in ipairs(constraint_entity.claims.P152) do
		if statement.mainsnak.datavalue.value.language == "en" then
			anchor = statement.mainsnak.datavalue.value.text
			break
		end
	end

	local type = constraint_entity.id
	if (type == cItem) or (type == cTargetRequiredClaim) or (type == cConflictsWith) then
		anchor = anchor .. " " .. getQualifierSingleValue(constraint, pProperty)
	elseif (type == cType) or (type == cValueType) then
		anchor = anchor .. " " .. table.concat(getQualifierValues(constraint, pClasses), ", ")
	end

	return anchor
end


local function getConstraintImage(constraint_entity)
	return constraint_entity.claims.P386[1].mainsnak.datavalue.value
end


local function verifyConstraintType(type)
	if not contains(cAllConstraints, type) then
		throw("error_unsupported_value", wikifyPKey(pConstraint), wikifyQKeys(cAllConstraints));
	end
end


local function verifyPropertyDatatype(constraint_type, property_datatype)
	if (property_datatype ~= "wikibase-item") and contains(cItemonlyConstraints, constraint_type) then
		throw("error_only_datatype", dtype.display("wikibase-item", defaultlang));
	elseif (property_datatype ~= "wikibase-item") and (property_datatype ~= "wikibase-property") and contains(cItemOrPropertyonlyConstraints, constraint_type) then
		throw("error_invalid_datatype", dtype.display(property_datatype, defaultlang));
	elseif (property_datatype ~= "quantity") and contains(cQuantityonlyConstraints, constraint_type) then
		throw("error_only_datatype", dtype.display("quantity", defaultlang));
	elseif (constraint_type == cCommonsLink) and (not(contains(cCommonsLinkapplicable, property_datatype))) then
		throw("error_invalid_datatype", dtype.display(property_datatype, defaultlang));
	elseif (constraint_type == cFormat) and (not(contains(cFormatapplicable, property_datatype))) then
		throw("error_invalid_datatype", dtype.display(property_datatype, defaultlang));
	elseif (constraint_type == cRange) and (not(contains(cRangeapplicable, property_datatype))) then
		throw("error_invalid_datatype", dtype.display(property_datatype, defaultlang));
	elseif (constraint_type == cDiffWithinRange) and (not(contains(cRangeapplicable, property_datatype))) then
		throw("error_invalid_datatype", dtype.display(property_datatype, defaultlang));
	end
end


local function verifyConstraintQualifier(qualifier, constraint_entity)
	local accepted_properties = { pMandatory, pExceptions, pGroupBy, pComment, pComment2, pSeparator, pConstraintScope, pPropertyScope, pConstraintEntityType, pReplacementProperty, pReplacementValue, pLanguageCode }

	if contains(accepted_properties, qualifier) then
		return true;
	end

	if constraint_entity.claims.P167 ~= nil then
		for i, value in pairs(constraint_entity.claims.P167) do
			local accepted_prop = "P" .. value.mainsnak.datavalue.value["numeric-id"]
			if qualifier == accepted_prop then
				return true
			end
			table.insert(accepted_properties, accepted_prop)
		end
	end

	throw("error_unsupported_qualifier", wikifyPKey(qualifier), wikifyPKeys(accepted_properties));
end


local function verifyQualifiers(constraint, constraint_entity)
	if (constraint.qualifiers ~= nil) then
		for key, value in pairs(constraint.qualifiers) do
			verifyConstraintQualifier(key, constraint_entity)
		end
	end
end

local function getExceptions(constraint)
	local exceptions 
	-- todo: allow exception lists from several items
	if hasQualifier(constraint, pExceptions) then
		exceptions = getQualifierAtLeastOneValue(constraint, pExceptions)
	end
	local text = ""
	local sep = ""
	if exceptions ~= nil then
		if #exceptions<21 then
			for i, exception in pairs(exceptions) do
				text = text .. sep .. wikifyQKey(exception)
				if sep == "" then
					sep = ", "
				end
			end
		else
			for i, exception in pairs(exceptions) do
				text = text .. sep .. "[[" .. exception .. "]]"
				if sep == "" then
					sep = ", "
				end
			end
		end
	end
	return text
end

local function makeConstraintDocumentation(constraint, property_id, property_datatype, add_category_to_page)
	local constraint_type = "Q" .. constraint.mainsnak.datavalue.value["numeric-id"]
	local constraint_entity = mw.wikibase.getEntity(constraint_type)
	local constraint_help = constraint_entity:getSitelink("gratisdatawiki")
	
	local constraint_help_link
	if (constraint_help ~= nil) then
		constraint_help =  "Special:MyLanguage/" .. constraint_help
		constraint_help_link = "([[" .. constraint_help .. "|{{int:Help}}]])"
	else		
		constraint_help = ""
		constraint_help_link = ""
	end

	verifyConstraintType(constraint_type)
	verifyPropertyDatatype(constraint_type, property_datatype)
	verifyQualifiers(constraint, constraint_entity)
	
	local data = getConstraintDescription(constraint, constraint_entity, property_id)
	local category_text
	if add_category_to_page == true then
		category_text = "[[Category:" .. data.category .. "]]"
	else
		category_text = ""
	end
	
	return mw.getCurrentFrame():preprocess(
		"{{Constraint" ..
		"  |image       = [[File:" .. getConstraintImage(constraint_entity) .. "|40px|link=" .. constraint_help .. "|alt=]]" ..
		"  |name        = " .. data.caption ..
		"  |description = " .. data.description .. " " .. constraint_help_link ..
		"  |id          = " .. property_id ..
		"  |anchor      = " .. getConstraintAnchor(constraint, constraint_entity) .. 
		"  |exceptions  = " .. getExceptions(constraint) ..
		"  |mandatory   = " .. isMandatoryConstraint(constraint) ..
		"  |additional_report = " .. (data.query or '') ..
		"}}" .. category_text)
end


local function makeConstraintDocumentationSafe(constraint, property_id, property_datatype, add_category_to_page)
	local status, result = pcall(makeConstraintDocumentation, constraint, property_id, property_datatype, add_category_to_page)
	if status then
		return result
	else
		local constraint_type = "Q" .. constraint.mainsnak.datavalue.value["numeric-id"]
		return "<strong class=\"error\">" ..
		       string.format(i18n("error_in_constraint"), wikifyQKey(constraint_type), result) ..
		       "</strong><br />[[Category:Properties with constraint declaration errors]]"
	end
end


local p = {}


function p.extractIndividualConstraintTemplate(frame)
	local id = frame.args['id']
	local constraint_type = frame.args['constraint_type']
	local propertyentity = mw.wikibase.getEntity(id)
	
	if propertyentity == nil then
		return "<strong class=\"error\">" .. 
				"Error: " .. frame:preprocess("[[Property:" .. id .. "]]") .. " doesn't exist." ..
				"</strong>"
	end
	
	local property_datatype = propertyentity.datatype
	local constraints       = (propertyentity.claims or {})[pConstraint]
	local add_category_to_page = false

	SPARQL.setLanguage(defaultlang)
	SPARQL.setId(id)
	SPARQL.setDatatype(property_datatype)

	local text = ''
	if constraints ~= nil then
		for i, constraint in pairs(constraints) do
			if constraint.mainsnak.datavalue.value["id"] == constraint_type then
				text = text .. makeConstraintDocumentationSafe(constraint, id, property_datatype, add_category_to_page)
				return text
			end
		end
	end

	return "<strong class=\"error\">" .. 
			"Error: " .. frame:preprocess("{{Q|" .. constraint_type .. "}}")
			.. " is not defined at " .. frame:preprocess("{{P|" .. id .. "}}.") ..
			"</strong>"
end

function p.makedoc(frame)
	local id = frame:preprocess("{{BASEPAGENAME}}")

	local propertyentity = mw.wikibase.getEntity(id)
	local property_datatype = propertyentity.datatype
	local constraints       = (propertyentity.claims or {})[pConstraint]
	local add_category_to_page = true

	SPARQL.setLanguage(defaultlang)
	SPARQL.setId(id)
	SPARQL.setDatatype(property_datatype)

	search.setLanguage(defaultlang)
	search.setId(id)
	search.setDatatype(property_datatype)

	local text = ''
	if constraints ~= nil then
		for i, constraint in pairs(constraints) do
			if constraint.rank ~= 'deprecated' then
				text = text .. makeConstraintDocumentationSafe(constraint, id, property_datatype, add_category_to_page)
			end
		end
	end

	return text
end

-- temporary functions for migration period

function p.getCaption(frame)
	return string.format(i18n(frame.args[1] .. "_caption"), frame.args[2], frame.args[3], frame.args[4], frame.args[5])
end

function p.getDescription(frame)
	return string.format(i18n(frame.args[1] .. "_description"), frame.args[2], frame.args[3], frame.args[4], frame.args[5])
end

return p