Module:Property documentation

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

Used by Template:Property documentation. Localized at Module:i18n/property documentation.

Code

local p = require 'Module:Property navbox'

local entity
local frame = p.getFrame()
local addcat = p.addCategory
local fb = p.fb
local highlightDollarInFormatter = p.highlightDollarInFormatter
local translate = p.translate

local onMedia
local onItems
local onProperties
local onLexemes
local onForms
local onSenses

local d = require 'Module:Wikidata'

local datatypeDisplay = (require 'Module:Datatype').display
local linguistic = require 'Module:Linguistic'
local formatNum = (require 'Module:Formatnum').formatNum

local function showFieldAsEntity(field)
	return function(data)
		return d.showentity(data[field], p.getLang())
	end
end

local function getWdqsLink(title, query)
	return 'https://query.wikidata.org/#%23title%3A' .. mw.uri.encode(title, 'PATH') .. '%0A' .. mw.uri.encode(query, 'PATH')
end

local function getWdqsEmbedLink(title, query)
	return 'https://query.wikidata.org/embed.html#%23title%3A' .. mw.uri.encode(title, 'PATH') .. '%0A' .. mw.uri.encode(query, 'PATH')
end

local function isForQualifierUse()
	-- todo: use property constraints
	if entity.claims and entity.claims.P31 then
		for _, statement in ipairs(entity.claims.P31) do
			if d.getmainid(statement) == 'Q15720608' then
				return true
			end
		end
	end
	return false
end

local set = (require 'Module:TableTools').listToSet

-- list from https://www.wikidata.org/wiki/Wikidata:Database_reports/List_of_properties/Top100
local freqproperties = set {
		"P2860", "P1545", "P2093", "P31", "P248", "P813", "P854", "P698", "P577", "P1476", "P1433",
		"P304", "P478", "P433", "P1215", "P1227", "P356", "P528", "P50", "P921", "P972", "P407", "P1932",
		"P17", "P585", "P131", "P932", "P106", "P459", "P625", "P21", "P580", "P3083", "P6257", "P6258",
		"P6259", "P2671", "P887", "P59", "P735", "P703", "P214", "P569", "P1810", "P2214", "P4656", "P143",
		"P10752", "P10751", "P27", "P373", "P18", "P582", "P361", "P2216", "P5875", "P646", "P734", "P684",
		"P1566", "P171", "P225", "P105", "P351", "P2583", "P1057", "P279", "P195", "P2888", "P19", "P570",
		"P1343", "P1087", "P2326", "P276", "P571", "P352", "P971", "P846", "P69", "P953", "P1412", "P1082",
		"P1001", "P227", "P6216", "P496", "P10585", "P155", "P156", "P1435", "P527", "P281", "P421", "P641",
		"P7859", "P54", "P1104", "P856", "P108"
	}
local prop31279 = set { "P31", "P279" }
-- https://gerrit.wikimedia.org/g/operations/mediawiki-config/+/69607ea194f6ea5406cd144dc99c1c8436e45ec6/wmf-config/InitialiseSettings.php#20671 (large page)
local searchPropertiesExclude = set { "P304", "P433", "P478", "P3903", "P3921", "P4316", "P1433", "P2860" }

p.rows = {
	--[[
		label = name of the message for the header in Module:i18n/property documentation,
		value = value of the line (by default, it gets the value of the parameter with the name of the label),
		query = property to be retrieved from item, or complex query from item,
		required = boolean,
		missingcat     = categorization in case some template data are missing,
		duplicatecat   = categorization in case some template data duplicate property statement data,
		conversioncat  = categorization in case some template data need to be moved to property statement data,
		converttoprop  = property to be used as replacement for template parameter value (defaults to param 'query' if it's a string),
		maintenancecat = categorization in case some other issue gets detected
]] --
	{
		label = 'description',
	},
	{
		label = 'subject item',
		value = showFieldAsEntity('subject item'),
		query = 'P1629',
		missingcat = 'Property documentation missing a subject item',
		duplicatecat = 'Property with duplicated subject item',
		conversioncat = 'Property with subject item to move to statement',
	},
	{
		label = 'associated item',
		query = function()
			return d.formatStatements {
					conjtype = 'comma',
					displayformat = 'wikidatastyle',
					entity = entity,
					lang = p.getLang(),
					property = { 'P2378', -- "issued by"
						'P1875', -- "represented by"
						'P137', -- "operator"
						'P126', -- "maintained by"
						'P127', -- "owned by"
						'P749' }, -- "parent organization"
					rank = 'valid',
				}
		end,
		maintenancecat = function(data)
			if data['associated item'] then
				return "Property documentation using 'associated item'"
			end
			return nil
		end,
	},
	{
		label = 'applicable "[[Property:P248|stated in]]" value',
		query = 'P9073',
	},
	{
		label = 'has quality',
		query = 'P1552',
	},
	{
		label = 'datatype',
		query = function() return datatypeDisplay(entity.datatype, p.getLang()) end,
	},
	{
		label = 'corresponding template',
		query = 'P2667',
	},
	{
		label = 'infobox parameter',
		maintenancecat = function(data)
			if data['infobox parameter'] then
				return "Property documentation using 'infobox parameter'"
			end
			return nil
		end,
	},
	{
		label = 'domain',
		query = function()
			return d.showQualifier {
					conjtype = 'or',
					displayformat = 'wikidatastyle',
					entity = entity,
					lang = p.getLang(),
					property = 'P2302',
					qualifier = 'P2308',
					targetvalue = 'Q21503250',
				}
		end
	},
	{
		label = 'allowed values',
		query = function()
			if (
				entity.datatype == 'commonsMedia' or
				entity.datatype == 'external-id' or
				entity.datatype == 'geo-shape' or
				entity.datatype == 'monolingualtext' or
				entity.datatype == 'string' or
				entity.datatype == 'tabular-data' or
				entity.datatype == 'url'
				) then
				--todo: local pattern = 'https://regex101.com/?regex=$1'
				return d.formatStatements {
						conjtype = '<br />',
						entity = entity,
						lang = p.getLang(),
						property = 'P1793', -- format as a regular expression
						showqualifiers = { 'P2916', 'P17' } -- syntax clarif, country
					} or d.showQualifier {
						entity = entity,
						lang = p.getLang(),
						property = 'P2302',
						qualifier = 'P1793',
						targetvalue = 'Q21502404',
					}
			elseif entity.datatype == 'quantity' then
				return d.showQualifier {
						conjtype = ' ≤ 𝓧 ≤ ',
						displayFormat = 'wikidatastyle',
						entity = entity,
						lang = p.getLang(),
						property = 'P2302',
						qualifiers = { 'P2313', 'P2312' }, -- minimum value, maximum value
						targetvalue = 'Q21510860',
					} -- todo: also display allowed units (Q21514353) constraint
			elseif entity.datatype == 'time' then
				return d.showQualifier {
						conjtype = ' ≤ 𝓧 ≤ ',
						displayFormat = 'wikidatastyle',
						entity = entity,
						lang = p.getLang(),
						property = 'P2302',
						qualifiers = { 'P2310', 'P2311' }, -- minimum date, maximum date
						targetvalue = 'Q21510860',
					}
			elseif entity.datatype == 'wikibase-item' then
				return d.showQualifier {
						conjtype = 'or',
						displayformat = 'wikidatastyle',
						entity = entity,
						lang = p.getLang(),
						property = 'P2302',
						qualifier = 'P2305',
						targetvalue = 'Q21510859',
					}
			end
			return nil
		end,
		duplicatecat = function()
			if entity.datatype == 'external-id' then
				return 'Property (external-id) with duplicated allowed values'
			else
				return 'Property with duplicated allowed values'
			end
		end,
		conversioncat = function()
			if entity.datatype == 'external-id' then
				return 'Property (external-id) with allowed values to move to statement'
			elseif (entity.datatype == 'string' or entity.datatype == 'url' or
				entity.datatype == 'monolingualtext' or entity.datatype == 'commonsMedia') then
				-- Everything else that uses regex P1793
				return 'Property (string) with allowed values to move to statement'
			else
				return 'Property with allowed values to move to statement'
			end
		end,
		converttoprop = function()
			if entity.datatype == 'external-id' then
				return 'P1793' -- format as a regular expression
			else
				return nil
			end
		end,
	},
	{
		label = 'allowed units',
		query = function()
			if entity.datatype ~= 'quantity' then
				return nil
			end
			local s = ''
			local lang = p.getLang()
			local units = d.showQualifier {
					conjtype = 'or',
					displayformat = 'wikidatastyle',
					entity = entity,
					lang = p.getLang(),
					property = 'P2302',
					qualifier = 'P2305',
					targetvalue = 'Q21514353',
				}
			local unittypes = d.formatStatements {
					entity = entity,
					lang = lang,
					property = 'P2876',
				}
			if unittypes then
				s = s .. translate('instances of') .. unittypes
				if units then
					s = s .. '<br />'
				end
			end
			if units then
				s = s .. units
			end
			if s == '' then
				return nil
			else
				return s
			end
		end,
		--converttoprop = {'P2237','P2876'},  -- units or types of units
	},
	{
		label = 'usage notes',
		query = function()
			for _, language in ipairs(fb.fblist(p.getLang(), true)) do
				local value = d.formatStatements {
						conjtype = '<br />',
						entity = entity,
						isinlanguage = language,
						property = 'P2559', -- Wikidata usage instructions
						rank = 'best',
					}
				if value then
					return value
				end
			end
			return d.formatStatements {
					conjtype = '<br />',
					entity = entity,
					rank = 'best',
					property = 'P2559',
				}
		end,
		converttoprop = 'P2559', -- Wikidata usage instructions
	},
	{
		label = 'preferred rank',
		value = showFieldAsEntity('preferred rank'),
		maintenancecat = function(data)
			if data['preferred rank'] then
				return "Property documentation using 'preferred rank'"
			end
			return nil
		end,
	},
	{
		-- fixme: skip P1855 with novalue and use only data in template
		label = 'example',
		query = function()
			local pattern, urlpattern
			if entity.datatype == 'commonsMedia' then -- they all have the same one
				urlpattern = 'https://commons.wikimedia.org/wiki/File:$1'
			elseif entity.datatype == 'tabular-data' or entity.datatype == 'geo-shape' then
				urlpattern = 'https://commons.wikimedia.org/wiki/$1' -- unlike for files, the prefix is part of the value
			elseif entity.datatype == 'string' or entity.datatype == 'external-id' then -- check first whether we need it
				urlpattern = d.formatStatements { entity = entity, property = 'P1630', rank = 'best', numval = 1, displayformat = 'raw' }
			end
			if urlpattern then
				local rdf = d.formatStatements { entity = entity, property = 'P1921', rank = 'best', numval = 1, displayformat = 'raw' }
				if rdf then
					pattern = mw.ustring.format('[%s $1] ([%s %s])', urlpattern, rdf, translate('rdf'))
					urlpattern = nil
				end
			end
			local delimiter = '&#32;' .. mw.getLanguage(p.getLang()):getArrow('forwards') .. '&#32;'
			if entity.id == 'P7383' then
				pattern = '<div style="display: inline;"> <hiero> $1 </hiero> </div>'
				urlpattern = nil
			end
			local stdsamples = d.formatStatements {
					conjtype = '<br />',
					delimiter = delimiter,
					displayformat = 'wikidatastyle',
					entity = entity,
					lang = p.getLang(),
					pattern = pattern,
					property = { 'P1855', 'P2271', 'P5192', 'P5193', 'P5977' },
					showqualifiers = { entity.id },
					urlpattern = urlpattern,
				}
			if entity.id == 'P7383' then
				return frame:preprocess(stdsamples)
			else
				return stdsamples
			end
		end,
		converttoprop = { 'P1855', 'P2271', 'P5192', 'P5193', 'P5977' }, -- property example
		required = false,
		missingcat = 'Property documentation missing an example',
		duplicatecat = 'Property with duplicated example',
		conversioncat = 'Property with example to move to statement',
	},
	{
		-- fixme: some value types may not work. Ok: string, quantity, item
		label = 'Commons example',
		query = function()
			local delimiter = '|' .. mw.getLanguage(p.getLang()):getArrow('forwards') .. '&nbsp;'
			local csamples = d.formatStatements {
					conjtype = '\n',
					delimiter = delimiter,
					displayformat = 'wikidatastyle',
					entity = entity,
					lang = p.getLang(),
					property = 'P6685',
					showqualifiers = { entity.id },
				}
			if csamples then
				return frame:preprocess('<gallery mode="packed" heights="150" style="text-align:left">\n' ..
					csamples .. '\n</gallery>')
			else
				return nil
			end
		end,
		converttoprop = { 'P6685' }, -- property example
		required = false,
	},
	{
		label = 'filter',
		value = function(data)
			local arg = data.filter
			if arg then
				local num = tonumber(arg)
				if num and num < 150 then
					return mw.ustring.format('[[Special:AbuseFilter/%d|%s]]', num,
							mw.ustring.format(translate('abuse filter'), formatNum(num, p.getLang())))
				else
					return arg
				end
			end
			return nil
		end,
		maintenancecat = function(data)
			if data.filter then
				if string.len(data.filter) <= 30 then
					return "Property documentation using 'filter'"
				else
					return "Property documentation using long 'filter'"
				end
			end
			return nil
		end,
	},
	{
		label = 'source',
		query = function()
			local claims = d.getClaims {
					entity = entity,
					property = 'P1896', -- source website
					rank = 'valid',
				}
			if not claims then
				return nil
			end
			for i, statement in ipairs(claims) do
				local showntext = d.getFormattedQualifiers(statement,
						{ 'P1476' }, -- title
						{ lang = p.getLang() })
				claims[i] = d.formatStatement(statement, {
						displayformat = 'weblink',
						lang = p.getLang(),
						showntext = showntext,
						showqualifiers = { 'P2315' } -- comment
					})
			end
			return linguistic.conj(claims, p.getLang(), '<br />')
		end,
		converttoprop = 'P1896', -- source website
		duplicatecat = 'Property with duplicated source',
		conversioncat = 'Property with source to move to statement',
	},
	{
		label = 'external links',
		value = function(data)
			if (entity.datatype == 'url' or entity.datatype == 'external-id') and data['external links'] then
				return frame:expandTemplate { title = 'External links', args = { data['external links'], data['external links 2'], data['external links 3'], data['external links 4'], data['external links 5'], data['external links 6'], data['external links 7'], data['external links 8'], data['external links 9'], data['external links 10'] } }
			end
			return nil
		end
	},
	{
		label = 'formatter URL',
		value = function(data)
			local val = data['formatter URL']
			if val then
				local append = ''
				if not mw.ustring.match(val, '^https?://') then
					append = append .. translate('wrong format')
				end
				if not mw.ustring.find(val, '$1', 7, true) then
					if append ~= '' then
						append = append .. ', '
					end
					append = append .. mw.ustring.format(translate('missing stuff'), '$1')
				end
				if append ~= '' then
					append = ' – <span style="color: red;">' .. append .. '</span>'
					addcat('Invalid formatter URL')
				end
				return highlightDollarInFormatter(val) .. append
			end
		end,
		query = function()
			if entity.datatype == 'commonsMedia' then
				return highlightDollarInFormatter('https://commons.wikimedia.org/wiki/File:$1')
			end
			if entity.datatype == 'tabular-data' or entity.datatype == 'geo-shape' then
				return highlightDollarInFormatter('https://commons.wikimedia.org/wiki/$1')
			end
			local claims = d.getClaims {
					entity = entity,
					property = 'P1630',
					rank = 'valid',
					sorttype = function(first, second)
						return first.rank == 'preferred' and second.rank ~= 'preferred'
					end
				}
			if not claims then
				return nil
			end
			for i, statement in ipairs(claims) do
				claims[i] = d.formatStatement(statement, { displayformat = 'raw' }) -- no escaping
				claims[i] = highlightDollarInFormatter(claims[i])
				local comment = d.getFormattedQualifiers(statement, { 'P2916' }, { lang = p.getLang() }) -- syntax clarif
				local regex = d.getFormattedQualifiers(statement, { 'P1793' }, {})
				if comment or regex then
					if regex then
						local colon = frame:expandTemplate { title = 'colon', args = { lang = p.getLang() } }
						regex = '[[Q185612|' .. translate('regex') .. ']]' .. colon .. '<code>' .. regex .. '</code>'
					end
					if comment and regex then
						claims[i] = claims[i] ..
							linguistic.inparentheses(comment .. translate('semicolon') .. regex, p.getLang())
					else
						claims[i] = claims[i] .. linguistic.inparentheses(comment or regex, p.getLang())
					end
				end
			end
			return linguistic.conj(claims, p.getLang(), '<br />')
		end,
		converttoprop = 'P1630', -- formatter URL
		duplicatecat = 'Property with duplicated formatter URL',
		conversioncat = 'Property with formatter URL to move to statement',
	},
	{
		label = 'embed URL',
		value = function(data)
			local val = data['embed URL']
			if val then
				local append = ''
				if not mw.ustring.match(val, '^https?://') then
					append = append .. translate('wrong format')
				end
				if not mw.ustring.find(val, '$1', 7, true) then
					if append ~= '' then
						append = append .. ', '
					end
					append = append .. mw.ustring.format(translate('missing stuff'), '$1')
				end
				if append ~= '' then
					append = ' – <span style="color: red;">' .. append .. '</span>'
					addcat('Invalid formatter URL')
				end
				return highlightDollarInFormatter(val) .. append
			end
		end,
		query = function()
			local lang = p.getLang()
			if entity.datatype == 'commonsMedia' then
				return highlightDollarInFormatter('https://commons.wikimedia.org/wiki/File:$1')
			end
			local claims = d.getClaims {
					entity = entity,
					property = 'P2720',
					rank = 'valid',
					sorttype = function(first, second)
						return first.rank == 'preferred' and second.rank ~= 'preferred'
					end
				}
			if not claims then
				return nil
			end
			for i, statement in ipairs(claims) do
				claims[i] = d.formatStatement(statement, { displayformat = 'raw' }) -- no escaping
				claims[i] = highlightDollarInFormatter(claims[i])
				local comment = d.getFormattedQualifiers(statement, { 'P2916' }, { lang = lang })
				local regex = d.getFormattedQualifiers(statement, { 'P1793' }, {})
				if comment or regex then
					if regex then
						regex = '[[Q185612|' ..
							translate('regex') .. ']]' .. linguistic.colon(lang) .. '<code>' .. regex .. '</code>'
					end
					if comment and regex then
						claims[i] = claims[i] ..
							linguistic.inparentheses(comment .. translate('semicolon') .. regex, lang)
					else
						claims[i] = claims[i] .. linguistic.inparentheses(comment or regex, lang)
					end
				end
			end
			return linguistic.conj(claims, lang, '<br />')
		end,
		converttoprop = 'P2720', -- embed URL
		duplicatecat = 'Property with duplicated formatter URL',
		conversioncat = 'Property with formatter URL to move to statement',
	},
	{
		label = 'URN',
		query = function()
			local lang = p.getLang()
			local claims = d.getClaims {
					entity = entity,
					property = 'P7470',
					rank = 'valid',
					sorttype = function(first, second)
						return first.rank == 'preferred' and second.rank ~= 'preferred'
					end
				}
			if not claims then
				return nil
			end
			for i, statement in ipairs(claims) do
				claims[i] = d.formatStatement(statement, { displayformat = 'raw' }) -- no escaping
				claims[i] = highlightDollarInFormatter(claims[i])
				local comment = d.getFormattedQualifiers(statement, { 'P2916' }, { lang = lang })
				if comment then
					claims[i] = claims[i] .. linguistic.inparentheses(comment, lang)
				end
			end
			return linguistic.conj(claims, lang, '<br />') ..
				" — [https://query.wikidata.org/embed.html#SELECT%20%3FUniform_Resource_Name_URN%20%3Fitem%20%3FitemLabel%20%3Fvalue%0A%7B%0A%20%20%20%20%7B%20BIND%28%20wdt%3A" ..
				entity.id ..
				"%20as%20%3Fwdt%29%20%3Fp%20wikibase%3AdirectClaim%20%3Fwdt%3B%20p%3AP7470%20%3Fst.%20%3Fst%20ps%3AP7470%20%3Ff.%20OPTIONAL%20%7B%3Fst%20pq%3AP1793%20%3Fr%7D%20BIND%28CONCAT%28%22%5E%22%2CCOALESCE%28%3Fr%2C%22%28%5E.%2a%29%22%29%2C%22%24%22%29%20as%20%3Fre%29%20%7D%0A%20%20%20%20%3Fitem%20%3Fwdt%20%3Fvalue.%20FILTER%28REGEX%28str%28%3Fvalue%29%2C%3Fre%29%29%20BIND%28URI%28REPLACE%28str%28%3Fvalue%29%2C%3Fre%2C%3Ff%29%29%20as%20%3FUniform_Resource_Name_URN%29%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7Bbd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%7D%0A%7D%0ALIMIT%20100 " ..
				translate('list of URNs') .. "]"
		end,
	},
	{
		label = 'robot and gadget jobs',
		value = function(data)
			local val = data['robot and gadget jobs']
			local json = mw.title.new('User:DeltaBot/fixClaims/jobs'):getContent()
			local allJobs = mw.text.jsonDecode(json)
			local jobs = {}
			for _, job in ipairs(allJobs) do
				if job.p == entity.id then
					table.insert(jobs, job)
				end
			end
			if #jobs > 0 then
				local rows = {}
				table.insert(rows, mw.ustring.format(translate('DeltaBot'), 'User:DeltaBot/fixClaims/jobs'))
				for _, job in ipairs(jobs) do
					local jobDesc
					if job.action == 'addClaim' then
						jobDesc = mw.ustring.format(translate('DeltaBot-' .. job.action), job.pNew, job.valNew or 'none')
					elseif job.action == 'changeProperty' then
						jobDesc = mw.ustring.format(translate('DeltaBot-' .. job.action), job.pOld or entity.id, job
							.pNew)
					elseif job.action == 'changeQualifierProperty' then
						jobDesc = mw.ustring.format(translate('DeltaBot-' .. job.action), job.pOld, job.pNew)
					elseif job.action == 'format' then
						jobDesc = translate('DeltaBot-' .. job.action .. '-' .. job.subaction)
					elseif job.action == 'inverse' then
						jobDesc = mw.ustring.format(translate('DeltaBot-' .. job.action), job.pNewT)
					elseif job.action == 'moveQualifierToStatement' then
						jobDesc = mw.ustring.format(translate('DeltaBot-' .. job.action), job.pQualifier, job.pNew)
					elseif job.action == 'moveStatementToQualifier' then
						jobDesc = mw.ustring.format(translate('DeltaBot-' .. job.action), job.pNew)
					else
						jobDesc = translate('DeltaBot-' .. job.action)
					end
					table.insert(rows,
						mw.ustring.format('* [%s %s]', getWdqsLink(jobDesc, job.query), jobDesc))
					if job.action == 'changeValue' then
						for old, new in pairs(job.map) do
							table.insert(rows, mw.ustring.format('** %s → %s',
								d.showentity('Q' .. old, p.getLang()), d.showentity('Q' .. new, p.getLang())))
						end
					end
				end
				if val then
					table.insert(rows, val)
				end
				return table.concat(rows, '\n')
			end
			return val
		end,
		maintenancecat = function(data)
			if data['robot and gadget jobs'] then
				if string.len(data['robot and gadget jobs']) <= 30 then
					return "Property documentation using 'robot and gadget jobs'"
				else
					return "Property documentation using long 'robot and gadget jobs'"
				end
			end
			return nil
		end,
	},
	{
		label = 'track same cat',
		value = showFieldAsEntity('track same cat'),
		query = 'P3734', -- category for value same as Wikidata
	},
	{
		label = 'track diff cat',
		value = showFieldAsEntity('track diff cat'),
		query = 'P3709', -- category for value different from Wikidata
	},
	{
		label = 'track usage cat',
		value = showFieldAsEntity('track usage cat'),
		query = 'P2875', -- property usage tracking category
	},
	{
		label = 'track local no-WD yes',
		value = showFieldAsEntity('track local no-WD yes'),
	},
	{
		label = 'track local yes-WD no',
		value = showFieldAsEntity('track local yes-WD no'),
		query = 'P3713', -- category for value not in Wikidata
	},
	{
		label = 'track local no-WD no',
		value = showFieldAsEntity('track local no-WD no'),
	},
	{
		label = 'track avail both cat',
		value = showFieldAsEntity('track avail both cat'),
	},
	{
		label = 'related2country',
		query = function()
			local lang = p.getLang()
			local function displayformat(snak, params)
				local id = d.getRawvalue(snak)
				local label = mw.wikibase.getLabelByLang(id, 'en')
				local formatted = d.showentity(id, lang)
				local flag, category, seeothers
				local image = d.formatStatements {
						entity = id,
						numval = 1,
						pattern = '[[File:$1|border|25px|link=' .. id .. ']]',
						property = 'P41',
						rank = 'best',
					}
				if image then
					flag = image .. ' '
				else
					flag = ''
				end
				if label then
					local cat_title = label .. '-related properties'
					local otherpropertynum = mw.site.stats.pagesInCategory(cat_title, 'pages') - 1
					if otherpropertynum > 0 then
						local msg = mw.message.newRawMessage(translate('see others'))
							:numParams(otherpropertynum)
							:plain()
						seeothers = linguistic.inparentheses(
								mw.ustring.format('[[:Category:%s|%s]]', cat_title, frame:preprocess(msg)), lang)
					else
						seeothers = ''
					end
					category = '[[Category:' .. cat_title .. ']]'
				else
					category = ''
					seeothers = ''
				end
				return flag .. formatted .. seeothers .. category
			end
			local country = d.formatStatements {
					entity = entity,
					property = 'P17',
					lang = lang,
					displayformat = displayformat,
					conjtype = '<br />'
				}
			if country then
				local jurisdiction = d.formatStatements { entity = entity, property = 'P1001', lang = lang, displayformat = 'wikidatastyle', conjtype = 'comma' }
				if jurisdiction then
					country = country .. linguistic.inparentheses(jurisdiction, lang)
				end
			end
			return country
		end,
		converttoprop = { 'P17', 'P1001' }, -- country, jurisdiction
	},
	{
		label = 'complementary property',
		query = 'P8882',
	},
	{
		label = 'seealso',
		query = 'P1659',
	},
	{
		label = 'lists',
		value = function(data)
			-- Use template field, if set
			local append = data.lists or ''
			local lang = p.getLang()
			local forQualifierUse = isForQualifierUse()
			local linkText, linkURL
			-- Get P2354 "has list" property, if any
			local val = d.formatStatements { entity = entity, lang = lang, displayformat = 'wikidatastyle', conjtype = '<br />', property = 'P2354' }
			if val then
				append = append .. '\n*' .. val
			end

			if entity.claims and entity.claims.P31 then
				for _, statement in ipairs(entity.claims.P31) do
					if d.getmainid(statement) == 'Q70564278' then
						-- https://commons.wikimedia.org/wiki/Commons_talk:Structured_data#Search_for_depicts_qualifier
						linkText = translate(
								"search Commons for files with depicts-statement and this property as qualifier")
						linkURL = 'c:Special:Search/haswbstatement:P180 +"' .. d._getLabel(entity, 'en') .. ':"'
						append = append .. '\n* [[' .. linkURL .. '|' .. linkText .. ']]'
						break
					end
				end
			end

			if entity.claims and entity.claims.P6685 then
				linkText = translate("search Commons for files with property")
				linkURL = "https://commons.wikimedia.org/w/index.php?ns6=1&search=haswbstatement%3A" .. entity.id
				append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'
			end

			if (onMedia > 0 and onItems == 0 and onProperties == 0 and onLexemes == 0 and onForms == 0 and onSenses == 0) then
				---	Commons only properties
			else
				if not forQualifierUse then
					if not freqproperties[entity.id] then
						-- statements count
						linkText = translate("items with the most statements of this property")
						linkURL = getWdqsLink("Items with most values of the property " .. entity.id, [[
SELECT ?item ?itemLabel ?cnt
{
  {
    SELECT ?item (COUNT(?value) AS ?cnt) {
      ?item wdt:]] .. entity.id .. [[ ?value
    }
    GROUP BY ?item ORDER BY DESC(?cnt) LIMIT 100
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,mul". }
}
ORDER BY DESC(?cnt)
]])
						append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

						linkText = translate("count of items by number of statements (chart)")
						linkURL = getWdqsLink("Count of items with " .. entity.id .. " by number of statements", [[
#defaultView:AreaChart
SELECT ?st ?ct {
  {
    BIND (0 AS ?ct)
    BIND (0 AS ?st)
  }
  UNION {
    SELECT ?st (COUNT(*) as ?ct) {
      ?item wdt:]] .. entity.id .. [[ ?value ; wikibase:statements ?st
    }
    GROUP BY ?st ORDER BY ?st
  }
}]])
						append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

						linkText = translate("items with the most statements")
						linkURL = getWdqsLink("Items with property " .. entity.id .. " and most statements", [[
SELECT ?item ?itemLabel ?value ?st {
  {
    SELECT * {
      ?item wdt:]] .. entity.id .. [[ ?value ; wikibase:statements ?st
    }
    ORDER BY DESC(?st)
    LIMIT 100
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,mul". }
}]])
						append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

						linkText = translate("items with the fewest statements")
						linkURL = getWdqsLink("Items with property " .. entity.id .. " and the lowest number of statements", [[
SELECT ?item ?itemLabel ?value ?valueLabel ?st
{
  {
    SELECT * {
      ?item wdt:]] .. entity.id .. [[ ?value ; wikibase:statements ?st
    }
    ORDER BY ASC(?st) ?item
    LIMIT 100
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,mul". }
}]])
						append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

						-- sitelinks count
						linkText = translate("count of items by number of sitelinks (chart)")
						linkURL =getWdqsLink("Count of items with " .. entity.id .. " statements by number of sitelinks", [[
#defaultView:AreaChart
SELECT ?st ?ct {
  {
    BIND (0 AS ?ct)
    BIND (-0.0001 AS ?st)
  }
  UNION {
    SELECT ?st (COUNT(*) as ?ct)
    {
      ?item wdt:]] .. entity.id .. [[ ?value ; wikibase:sitelinks ?st
    }
    GROUP BY ?st
    ORDER BY ?st
  }
}]])
						append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

						linkText = translate("items with the most sitelinks")
						linkURL =
							"https://query.wikidata.org/#SELECT%20%3Fsl%20%3Fitem%20%3FitemLabel%20%3FitemDescription%0AWHERE%0A%7B%0A%20%20%3Fitem%20wdt%3A" ..
							entity.id ..
							"%20%3Fvalue%20%3B%20wikibase%3Asitelinks%20%3Fsl%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fsl%29%0ALIMIT%2050"
						append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

						linkText = translate(
								"items with the most sitelinks, but no link to one Wikipedia (sample: Welsh Wikipedia, cywiki)")
						linkURL = "https://query.wikidata.org/#%23%20items%20without%20property%20" ..
							entity.id ..
							"%2C%20but%20no%20sitelink%20to%20cywiki%0A%23%20added%20by%20Jura1%2C%202017-11-27%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%3Fsl%0A%7B%0A%20%20%7B%0A%20%20%20%20%20%20%20SELECT%20%2a%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%3Fitem%20wdt%3A" ..
							entity.id ..
							"%20%3Fvalue%20%3B%20wikibase%3Asitelinks%20%3Fsl%0A%20%20%20%20%20%20%20%20%20%20FILTER%20NOT%20EXISTS%20%7B%20%5B%5D%20schema%3Aabout%20%3Fitem%20%3B%20schema%3AisPartOf%20%3Chttps%3A%2F%2Fcy.wikipedia.org%2F%3E%20%7D%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20ORDER%20BY%20DESC%28%3Fsl%29%20%3Fitem%0A%20%20%20%20%20%20%20LIMIT%201000%0A%20%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fsl%29%20%3Fitem"
						append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

						linkText = translate("items with the fewest sitelinks")
						linkURL =
							"https://query.wikidata.org/#SELECT%20%3Fsl%20%3Fitem%20%3FitemLabel%20%3FitemDescription%0AWHERE%0A%7B%0A%20%20%3Fitem%20wdt%3A" ..
							entity.id ..
							"%20%3Fvalue%20%3B%20wikibase%3Asitelinks%20%3Fsl%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0AORDER%20BY%20ASC%28%3Fsl%29%0ALIMIT%2050"
						append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

						linkText = translate("most linked wikis")
						linkURL =
							"https://query.wikidata.org/#SELECT%20%3Fwiki%20%28COUNT%28%20DISTINCT%20%3Fitem%20%29%20as%20%3Fcount%29%20%28SAMPLE%28%3Fitem%29%20as%20%3Fsample%29%0A%7B%0A%09%3Fitem%20wdt%3A" ..
							entity.id ..
							"%20%5B%20%5D%20.%20%0A%20%20%20%20%5B%5D%20schema%3Aabout%20%3Fitem%20%3B%20schema%3AisPartOf%20%3Fwiki%20.%20%0A%7D%0AGROUP%20BY%20%3Fwiki%20%0AORDER%20BY%20DESC%28%3Fcount%29%20%3Fwiki"
						append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

						linkText = translate("items with the most identifier properties")
						linkURL = "https://query.wikidata.org/#%23%20items%20with%20property%20" ..
							entity.id ..
							"%20and%20most%20identifiers%0A%23%20added%20by%20User%3AJura1%2C%202017-07-30%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%3Fids%0A%7B%0A%20%20%7B%0A%20%20%20%20%20%20%20SELECT%20%2a%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%3Fitem%20wdt%3A" ..
							entity.id ..
							"%20%3Fvalue%20%3B%20wikibase%3Aidentifiers%20%3Fids%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20ORDER%20BY%20DESC%28%3Fids%29%0A%20%20%20%20%20%20%20LIMIT%20100%0A%20%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fids%29%20%3Fitem"
						append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'


						if entity.datatype == 'wikibase-item' then
							linkText = translate("by value of property")
							append = append .. '\n* ' .. linkText .. ''

							local hasdistinct = d.formatStatements { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q21502410' }
							if hasdistinct == "Q21502410" then
								-- no grouping
								linkText = translate("most statements")
								linkURL = "https://query.wikidata.org/#%23title%3A%20values%20of%20property%20" ..
									entity.id ..
									"%20with%20most%20statements%0ASELECT%20%3Fitem%20%3FitemLabel%20%3FitemDescription%20%3Fsts%0AWHERE%0A%7B%0A%09%3Fp%20wdt%3A" ..
									entity.id ..
									"%20%3Fitem%20.%20%0A%20%20%20%20%3Fitem%20wikibase%3Astatements%20%3Fsts%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%20%20%20%20%20%20%0A%7D%0AORDER%20BY%20DESC%28%3Fsts%29%20%3Fitem%0ALIMIT%20100"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("fewest statements")
								linkURL = "https://query.wikidata.org/#%23title%3A%20values%20of%20property%20" ..
									entity.id ..
									"%20with%20fewest%20statements%0ASELECT%20%3Fitem%20%3FitemLabel%20%3FitemDescription%20%3Fsts%0AWHERE%0A%7B%0A%09%3Fp%20wdt%3A" ..
									entity.id ..
									"%20%3Fitem%20.%20%0A%20%20%20%20%3Fitem%20wikibase%3Astatements%20%3Fsts%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%20%20%20%20%20%20%0A%7D%0AORDER%20BY%20ASC%28%3Fsts%29%20%3Fitem%0ALIMIT%20100"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("most sitelinks")
								linkURL = "https://query.wikidata.org/#%23title%3A%20values%20of%20property%20" ..
									entity.id ..
									"%20with%20most%20sitelinks%0ASELECT%20%3Fitem%20%3FitemLabel%20%3FitemDescription%20%3Fsls%0AWHERE%0A%7B%0A%09%3Fp%20wdt%3A" ..
									entity.id ..
									"%20%3Fitem%20.%20%0A%20%20%20%20%3Fitem%20wikibase%3Asitelinks%20%3Fsls%20%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%20%20%20%20%20%20%0A%7D%0AORDER%20BY%20DESC%28%3Fsls%29%20%3Fitem%0ALIMIT%20100"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("most linked wikis")
								linkURL =
									"https://query.wikidata.org/#%23title%3A%20most%20linked%20wikis%20on%20values%20of%20property%20" ..
									entity.id ..
									"%0ASELECT%20%3Fwiki%20%28COUNT%28%20DISTINCT%20%3Fitem%20%29%20as%20%3Fcount%29%20%28SAMPLE%28%3Fitem%29%20as%20%3Fsample%29%0A%7B%0A%09%3Fp%20wdt%3A" ..
									entity.id ..
									"%20%3Fitem%20.%20%0A%20%20%20%20%5B%5D%20schema%3Aabout%20%3Fitem%20%3B%20schema%3AisPartOf%20%3Fwiki%20.%20%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%20%20%20%20%20%20%0A%7D%0AGROUP%20BY%20%3Fwiki%20%0AORDER%20BY%20DESC%28%3Fcount%29%20%3Fwiki"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("most identifiers")
								linkURL = "https://query.wikidata.org/#%23title%3A%20values%20of%20property%20" ..
									entity.id ..
									"%20with%20most%20identifiers%0ASELECT%20%3Fitem%20%3FitemLabel%20%3FitemDescription%20%3Fids%0AWHERE%0A%7B%0A%09%3Fp%20wdt%3A" ..
									entity.id ..
									"%20%3Fitem%20.%20%0A%20%20%20%20%3Fitem%20wikibase%3Aidentifiers%20%3Fids%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%20%20%20%20%20%20%0A%7D%0AORDER%20BY%20DESC%28%3Fids%29%20%3Fitem%0ALIMIT%20100"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'
							else
								-- with grouping by value
								linkText = translate("most statements")
								linkURL =
									"https://query.wikidata.org/#PREFIX%20wdtp%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F" ..
									entity.id ..
									"%3E%0A%23title%3Avalues%20with%20most%20statements%0ASELECT%20%3Fvalue%20%3FvalueLabel%20%3FvalueDescription%20%3Fsts%20%3Fuses_of_value%20%3Fsample_use%20%3Fsample_useLabel%0AWITH%0A%7B%0A%20%20%20%20SELECT%20%3Fvalue%20%28count%28%2a%29%20as%20%3Fuses_of_value%29%20%28SAMPLE%28%3Fi%29%20as%20%3Fsample_use%29%0A%20%20%20%20%7B%20%20%20%20%20%20%3Fi%20wdtp%3A%20%3Fvalue%20%20%20%20%7D%0A%20%20%20%20GROUP%20BY%20%3Fvalue%0A%20%20%20%20%23%20HAVING%28%20%3Fuses_of_value%20%3E%2010%20%29%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%7D%20as%20%25allvalues%0AWITH%0A%7B%0A%20%20SELECT%20%3Fvalue%20%3Fsts%20%3Fuses_of_value%20%3Fsample_use%0A%20%20%7B%20%20%20%20%0A%20%20%20%20%20%20INCLUDE%20%25allvalues%20%20%20%20%0A%20%20%20%20%20%20%3Fvalue%20wikibase%3Astatements%20%3Fsts%20%20%0A%20%20%7D%0A%20%20ORDER%20BY%20DESC%28%3Fsts%29%20%3Fvalue%0A%20%20LIMIT%20100%0A%7D%20as%20%25top100values%20%20%0AWHERE%0A%7B%0A%09INCLUDE%20%25top100values%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%20.%7D%20%20%20%20%20%20%0A%7D%0AORDER%20BY%20DESC%28%3Fsts%29%20%3Fvalue"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("fewest statements")
								linkURL =
									"https://query.wikidata.org/#PREFIX%20wdtp%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F" ..
									entity.id ..
									"%3E%0A%23title%3Avalues%20with%20fewest%20statements%0ASELECT%20%3Fvalue%20%3FvalueLabel%20%3FvalueDescription%20%3Fsts%20%3Fuses_of_value%20%3Fsample_use%20%3Fsample_useLabel%0AWITH%0A%7B%0A%20%20%20%20SELECT%20%3Fvalue%20%28count%28%2a%29%20as%20%3Fuses_of_value%29%20%28SAMPLE%28%3Fi%29%20as%20%3Fsample_use%29%0A%20%20%20%20%7B%20%20%20%20%20%20%3Fi%20wdtp%3A%20%3Fvalue%20%20%20%20%7D%0A%20%20%20%20GROUP%20BY%20%3Fvalue%0A%20%20%20%20%23%20HAVING%28%20%3Fuses_of_value%20%3E%2010%20%29%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%7D%20as%20%25allvalues%0AWITH%0A%7B%0A%20%20SELECT%20%3Fvalue%20%3Fsts%20%3Fuses_of_value%20%3Fsample_use%0A%20%20%7B%20%20%20%20%0A%20%20%20%20%20%20INCLUDE%20%25allvalues%20%20%20%20%0A%20%20%20%20%20%20%3Fvalue%20wikibase%3Astatements%20%3Fsts%20%20%0A%20%20%7D%0A%20%20ORDER%20BY%20ASC%28%3Fsts%29%20%3Fvalue%0A%20%20LIMIT%20100%0A%7D%20as%20%25top100values%20%20%0AWHERE%0A%7B%0A%09INCLUDE%20%25top100values%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%20.%7D%20%20%20%20%20%20%0A%7D%0AORDER%20BY%20ASC%28%3Fsts%29%20%3Fvalue"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("most sitelinks")
								linkURL =
									"https://query.wikidata.org/#PREFIX%20wdtp%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F" ..
									entity.id ..
									"%3E%0A%23title%3Avalues%20with%20most%20sitelinks%0ASELECT%20%3Fvalue%20%3FvalueLabel%20%3FvalueDescription%20%3Fsls%20%3Fuses_of_value%20%3Fsample_use%20%3Fsample_useLabel%0AWITH%0A%7B%0A%20%20%20%20SELECT%20%3Fvalue%20%28count%28%2a%29%20as%20%3Fuses_of_value%29%20%28SAMPLE%28%3Fi%29%20as%20%3Fsample_use%29%0A%20%20%20%20%7B%20%3Fi%20wdtp%3A%20%3Fvalue%20%7D%0A%20%20%20%20GROUP%20BY%20%3Fvalue%0A%20%20%20%20%23%20HAVING%28%20%3Fuses_of_value%20%3E%2010%20%29%20%0A%7D%20as%20%25allvalues%0AWITH%0A%7B%0A%20%20SELECT%20%3Fvalue%20%3Fsls%20%3Fuses_of_value%20%3Fsample_use%0A%20%20%7B%0A%20%20%20%20%20%20INCLUDE%20%25allvalues%0A%20%20%20%20%20%20%3Fvalue%20wikibase%3Asitelinks%20%3Fsls%0A%20%20%7D%0A%20%20ORDER%20BY%20DESC%28%3Fsls%29%20%3Fvalue%0A%20%20LIMIT%20100%0A%7D%20as%20%25top100values%0AWHERE%0A%7B%0A%09INCLUDE%20%25top100values%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%20.%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fsls%29%20%3Fvalue"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("most linked wikis")
								linkURL =
									"https://query.wikidata.org/#PREFIX%20wdtp%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F" ..
									entity.id ..
									"%3E%0A%23title%3A%20most%20linked%20wikis%20on%20values%20of%20property%0ASELECT%20%3Fwiki%20%28COUNT%28%20DISTINCT%20%3Fvalue%20%29%20as%20%3Fcount_values%29%20%28SUM%28%3Fuses_of_value%29%20as%20%3Fcount_statements%29%20%28SAMPLE%28%3Fi2%29%20as%20%3Fsample_use%29%20%28SAMPLE%28%3Fvalue%29%20as%20%3Fsample_value%29%0AWITH%0A%7B%0A%20%20%20%20SELECT%20%3Fvalue%20%28count%28%2a%29%20as%20%3Fuses_of_value%29%20%28SAMPLE%28%3Fi%29%20as%20%3Fi2%29%0A%20%20%20%20%7B%20%20%20%20%20%20%3Fi%20wdtp%3A%20%3Fvalue%20%20%20%20%7D%0A%20%20%20%20GROUP%20BY%20%3Fvalue%0A%7D%20as%20%25allvalues%0AWHERE%0A%7B%0A%20%20%20%20%20%20INCLUDE%20%25allvalues%20%20%20%20%0A%20%20%20%20%20%20%5B%5D%20schema%3Aabout%20%3Fvalue%20%3B%20schema%3AisPartOf%20%3Fwiki%20.%20%0A%7D%0AGROUP%20BY%20%3Fwiki%0AORDER%20BY%20DESC%28%3Fcount_values%29%20%3Fwiki%0ALIMIT%2050"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("total of sitelinks on items")
								linkURL =
									"https://query.wikidata.org/#PREFIX%20wdtp%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F" ..
									entity.id ..
									"%3E%0A%23title%3A%20values%20by%20total%20of%20sitelinks%20on%20items%20%28max.%201000000%20items%29%0ASELECT%20%3Fsls_on_items%20%3Fvalue%20%3FvalueLabel%20%3Fcount%0AWHERE%0A%7B%0A%20%20%7B%0A%20%20%20%20%7B%20%20SELECT%20%3Fvalue%20%28SUM%28%3Fsls_i%29%20AS%20%3Fsls_on_items%29%20%28COUNT%28%2a%29%20AS%20%3Fcount%29%0A%20%20%20%20%20%20%20WHERE%20%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20SELECT%20%2a%20WHERE%20%7B%0A%20%20%20%20%20%20%20%20%20%20%3Fi%20wdtp%3A%20%3Fvalue.%0A%20%20%20%20%20%20%20%20%20%20hint%3APrior%20hint%3ArangeSafe%20%22true%22%5E%5Exsd%3Aboolean.%0A%20%20%20%20%20%20%20%20%20%20%3Fi%20wikibase%3Asitelinks%20%3Fsls_i.%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20LIMIT%201000000%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20GROUP%20BY%20%3Fvalue%0A%20%20%20%20%20%20ORDER%20BY%20DESC%20%28%3Fsls_on_items%29%0A%20%20%20%20%20%20LIMIT%20100%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%0A%20%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0AORDER%20BY%20DESC%20%28%3Fsls_on_items%29"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("most identifiers")
								linkURL =
									"https://query.wikidata.org/#PREFIX%20wdtp%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F" ..
									entity.id ..
									"%3E%0A%23title%3Avalues%20with%20most%20identifiers%0ASELECT%20%3Fvalue%20%3FvalueLabel%20%3FvalueDescription%20%3Fids%20%3Fuses_of_value%20%3Fsample_use%20%3Fsample_useLabel%0AWITH%0A%7B%0A%20%20%20%20SELECT%20%3Fvalue%20%28count%28%2a%29%20as%20%3Fuses_of_value%29%20%28SAMPLE%28%3Fi%29%20as%20%3Fsample_use%29%0A%20%20%20%20%7B%20%3Fi%20wdtp%3A%20%3Fvalue%20%7D%0A%20%20%20%20GROUP%20BY%20%3Fvalue%0A%20%20%20%20%23%20HAVING%28%20%3Fuses_of_value%20%3E%2010%20%29%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%7D%20as%20%25allvalues%0AWITH%0A%7B%0A%20%20SELECT%20%3Fvalue%20%3Fids%20%3Fuses_of_value%20%3Fsample_use%0A%20%20%7B%20%20%20%0A%20%20%20%20%20%20INCLUDE%20%25allvalues%0A%20%20%20%20%20%20%3Fvalue%20wikibase%3Aidentifiers%20%3Fids%0A%20%20%7D%0A%20%20ORDER%20BY%20DESC%28%3Fids%29%20%3Fvalue%0A%20%20LIMIT%20100%0A%7D%20as%20%25top100values%0AWHERE%0A%7B%0A%09INCLUDE%20%25top100values%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%20.%7D%20%20%20%20%20%20%0A%7D%0AORDER%20BY%20DESC%28%3Fids%29%20%3Fvalue"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

								linkText = translate("fewest identifiers")
								linkURL =
									"https://query.wikidata.org/#PREFIX%20wdtp%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F" ..
									entity.id ..
									"%3E%0A%23title%3Avalues%20with%20fewest%20identifiers%0ASELECT%20%3Fvalue%20%3FvalueLabel%20%3FvalueDescription%20%3Fids%20%3Fuses_of_value%20%3Fsample_use%20%3Fsample_useLabel%0AWITH%0A%7B%0A%20%20%20%20SELECT%20%3Fvalue%20%28count%28%2a%29%20as%20%3Fuses_of_value%29%20%28SAMPLE%28%3Fi%29%20as%20%3Fsample_use%29%0A%20%20%20%20%7B%20%3Fi%20wdtp%3A%20%3Fvalue%20%7D%0A%20%20%20%20GROUP%20BY%20%3Fvalue%0A%20%20%20%20%23%20HAVING%28%20%3Fuses_of_value%20%3E%2010%20%29%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%7D%20as%20%25allvalues%0AWITH%0A%7B%0A%20%20SELECT%20%3Fvalue%20%3Fids%20%3Fuses_of_value%20%3Fsample_use%0A%20%20%7B%20%20%20%0A%20%20%20%20%20%20INCLUDE%20%25allvalues%0A%20%20%20%20%20%20%3Fvalue%20wikibase%3Aidentifiers%20%3Fids%0A%20%20%7D%0A%20%20ORDER%20BY%20ASC%28%3Fids%29%20%3Fvalue%0A%20%20LIMIT%20100%0A%7D%20as%20%25top100values%0AWHERE%0A%7B%0A%09INCLUDE%20%25top100values%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%20.%7D%20%20%20%20%20%20%0A%7D%0AORDER%20BY%20ASC%28%3Fids%29%20%3Fvalue"
								append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'
							end
						end
					end

					if entity.datatype == 'external-id' then
						linkText = translate("items with no other external identifier")
						linkURL = "https://query.wikidata.org/#%23title%3A%20Items%20with%20property%20" ..
							entity.id ..
							"%20and%20no%20other%20identifiers%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%7B%0A%20%20%3Fitem%20wdt%3A" ..
							entity.id ..
							"%20%3Fvalue%20%3B%20wikibase%3Aidentifiers%201%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%20LIMIT%201000"
						append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'
					end

					linkText = translate("items with no other statements")
					linkURL = "https://query.wikidata.org/#%23title%3A%20Items%20with%20property%20" ..
						entity.id ..
						"%20and%20no%20other%20statements%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%7B%0A%20%20%3Fitem%20wdt%3A" ..
						entity.id ..
						"%20%3Fvalue%20%3B%20wikibase%3Astatements%201%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D"
					append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'
				end

				linkText   = translate("most recently created items")
				linkURL    = "https://query.wikidata.org/#%23title%3A%20Most%20recently%20created%20items%20with%20" ..
					entity.id ..
					"%0ASELECT%20%3Fitem%20%3FitemLabel%20%7B%0A%20%20SERVICE%20wikibase%3Amwapi%20%7B%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Aendpoint%20%22www.wikidata.org%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wikibase%3Aapi%20%22Generator%22%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wikibase%3Alimit%20%22once%22%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mwapi%3Agenerator%20%22search%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mwapi%3Agsrsearch%20%22haswbstatement%3A" ..
					entity.id ..
					"%22%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mwapi%3Agsrsort%20%22create_timestamp_desc%22%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mwapi%3Agsrlimit%20%22100%22.%0A%20%20%20%20%3Fitem%20wikibase%3AapiOutputItem%20mwapi%3Atitle.%0A%20%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0AORDER%20BY%20DESC%28xsd%3Ainteger%28SUBSTR%28STR%28%3Fitem%29%2C%2033%29%29%29"
				append           = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

				linkText         = translate("items with novalue claims")
				linkURL          = "https://query.wikidata.org/#%23title%3A%20Items%20with%20statements%20where%20property%20" ..
					entity.id ..
					"%20uses%20the%20novalue%20special%20value%0ASELECT%20%3Fitem%20%3FitemLabel%20%28GROUP_CONCAT%28%3FinstanceLabel%3B%20separator%3D%22%2C%20%22%29%20as%20%3FinstanceLabels%29%0A%7B%0A%20%20%3Fitem%20p%3A" ..
					entity.id ..
					"%20%5Ba%20wdno%3A" ..
					entity.id ..
					"%3B%20a%20wikibase%3ABestRank%5D%20.%0A%0A%20%20OPTIONAL%20%7B%3Fitem%20wdt%3AP31%20%3Finstance%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
					lang ..
					"%2Cen%2Cru%2Cja%2Czh%22%20.%0A%20%20%20%20%3Fitem%20rdfs%3Alabel%20%3FitemLabel%20.%0A%20%20%20%20%3Finstance%20rdfs%3Alabel%20%3FinstanceLabel%0A%20%20%7D%0A%7D%0AGROUP%20BY%20%3Fitem%20%3FitemLabel%0ALIMIT%20100"
				append           = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

				linkText         = translate("items with somevalue claims")
				linkURL          = "https://query.wikidata.org/#%23title%3A%20Items%20with%20statements%20where%20property%20" ..
					entity.id ..
					"%20uses%20the%20somevalue%2Fanyvalue%20special%20value%0ASELECT%0A%20%20%3Fitem%20%3FitemLabel%0A%20%20%28GROUP_CONCAT%28%3FinstanceLabel%3B%20separator%3D%22%2C%20%22%29%20as%20%3FinstanceLabels%29%0AWHERE%0A{%0A%20%20%20%20%3Fitem%20wdt%3A" ..
					entity.id ..
					"%20%3Fvalue%20.%0A%20%20%20%20FILTER%20%28wikibase%3AisSomeValue%28%3Fvalue%29%29%0A%20%20OPTIONAL%20{%3Fitem%20wdt%3AP31%20%3Finstance}%0A%20%20SERVICE%20wikibase%3Alabel%20{%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
					lang ..
					"%2Cen%2Cru%2Cja%2Czh%22%20.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Fitem%20rdfs%3Alabel%20%3FitemLabel%20.%20%3Finstance%20rdfs%3Alabel%20%3FinstanceLabel%20}%0A}%0AGROUP%20BY%20%3Fitem%20%3FitemLabel%0ALIMIT%20100"
				append           = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

				linkText         = translate("usage history (total)") -- Use embed
				linkURL          = getWdqsEmbedLink("Chart of " .. entity.id .. " usage", [[
# This chart is based on https://www.wikidata.org/wiki/Template:Property_uses
# which is updated once a day by PLbot

#defaultView:LineChart
SELECT ?day ?count WITH { SELECT (".+\\|]] .. string.sub(entity.id, 2) .. [[=(\\d+).+" as ?r) ("|]] .. string.sub(entity.id, 2) .. [[=" as ?p)
  (IF(CONTAINS(?r1,?p),xsd:integer(REPLACE(?r1,?r,"$1","s")),-1) AS ?c1) (xsd:dateTime(?t1) AS ?d1)
  (IF(CONTAINS(?r2,?p),xsd:integer(REPLACE(?r2,?r,"$1","s")),-1) AS ?c2) (xsd:dateTime(?t2) AS ?d2)
  (IF(CONTAINS(?r3,?p),xsd:integer(REPLACE(?r3,?r,"$1","s")),-1) AS ?c3) (xsd:dateTime(?t3) AS ?d3)
  (IF(CONTAINS(?r4,?p),xsd:integer(REPLACE(?r4,?r,"$1","s")),-1) AS ?c4) (xsd:dateTime(?t4) AS ?d4)
  (IF(CONTAINS(?r5,?p),xsd:integer(REPLACE(?r5,?r,"$1","s")),-1) AS ?c5) (xsd:dateTime(?t5) AS ?d5)
  (IF(CONTAINS(?r6,?p),xsd:integer(REPLACE(?r6,?r,"$1","s")),-1) AS ?c6) (xsd:dateTime(?t6) AS ?d6)
  (IF(CONTAINS(?r7,?p),xsd:integer(REPLACE(?r7,?r,"$1","s")),-1) AS ?c7) (xsd:dateTime(?t7) AS ?d7)
  (IF(CONTAINS(?r8,?p),xsd:integer(REPLACE(?r8,?r,"$1","s")),-1) AS ?c8) (xsd:dateTime(?t8) AS ?d8)
  (IF(CONTAINS(?r9,?p),xsd:integer(REPLACE(?r9,?r,"$1","s")),-1) AS ?c9) (xsd:dateTime(?t9) AS ?d9)
  (IF(CONTAINS(?r10,?p),xsd:integer(REPLACE(?r10,?r,"$1","s")),-1) AS ?c10) (xsd:dateTime(?t10) AS ?d10)
  { SERVICE wikibase:mwapi {
      VALUES ?bot_user { "PLbot" "DeltaBot" }
      bd:serviceParam wikibase:api "Generator" ; wikibase:endpoint "www.wikidata.org" ; mwapi:generator "allpages" ;
                      mwapi:gapfrom "Property_uses" ; mwapi:gapto "Property_uses" ; mwapi:gapnamespace "10" ;
                      mwapi:prop "revisions" ; mwapi:rvprop "content|timestamp" ; mwapi:rvlimit "10" ; mwapi:rvuser ?bot_user ; wikibase:limit "10" .
      ?t1 wikibase:apiOutput "revisions/rev[1]/@timestamp" . ?r1 wikibase:apiOutput "revisions/rev[1]/text()" .
      ?t2 wikibase:apiOutput "revisions/rev[2]/@timestamp" . ?r2 wikibase:apiOutput "revisions/rev[2]/text()" .
      ?t3 wikibase:apiOutput "revisions/rev[3]/@timestamp" . ?r3 wikibase:apiOutput "revisions/rev[3]/text()" .
      ?t4 wikibase:apiOutput "revisions/rev[4]/@timestamp" . ?r4 wikibase:apiOutput "revisions/rev[4]/text()" .
      ?t5 wikibase:apiOutput "revisions/rev[5]/@timestamp" . ?r5 wikibase:apiOutput "revisions/rev[5]/text()" .
      ?t6 wikibase:apiOutput "revisions/rev[6]/@timestamp" . ?r6 wikibase:apiOutput "revisions/rev[6]/text()" .
      ?t7 wikibase:apiOutput "revisions/rev[7]/@timestamp" . ?r7 wikibase:apiOutput "revisions/rev[7]/text()" .
      ?t8 wikibase:apiOutput "revisions/rev[8]/@timestamp" . ?r8 wikibase:apiOutput "revisions/rev[8]/text()" .
      ?t9 wikibase:apiOutput "revisions/rev[9]/@timestamp" . ?r9 wikibase:apiOutput "revisions/rev[9]/text()" .
      ?t10 wikibase:apiOutput "revisions/rev[10]/@timestamp" . ?r10 wikibase:apiOutput "revisions/rev[10]/text()" .
    }
  }
} as %revs {
  {BIND(?c1 AS ?count) BIND(?d1 AS ?day) INCLUDE %revs} UNION
  {BIND(?c2 AS ?count) BIND(?d2 AS ?day) INCLUDE %revs} UNION
  {BIND(?c3 AS ?count) BIND(?d3 AS ?day) INCLUDE %revs} UNION
  {BIND(?c4 AS ?count) BIND(?d4 AS ?day) INCLUDE %revs} UNION
  {BIND(?c5 AS ?count) BIND(?d5 AS ?day) INCLUDE %revs} UNION
  {BIND(?c6 AS ?count) BIND(?d6 AS ?day) INCLUDE %revs} UNION
  {BIND(?c7 AS ?count) BIND(?d7 AS ?day) INCLUDE %revs} UNION
  {BIND(?c8 AS ?count) BIND(?d8 AS ?day) INCLUDE %revs} UNION
  {BIND(?c9 AS ?count) BIND(?d9 AS ?day) INCLUDE %revs} UNION
  {BIND(?c10 AS ?count) BIND(?d10 AS ?day) INCLUDE %revs}
  FILTER(?count != -1)
}]])
				append           = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

				linkText         = translate("usage history (main statement)") -- Use embed
				linkURL          = getWdqsEmbedLink("Chart of " .. entity.id .. " usage", [[
# Note: this chart is based on https://www.wikidata.org/wiki/Template:Number_of_main_statements_by_property
# which is updated once a day by PLbot

#defaultView:LineChart
SELECT ?day ?count WITH { SELECT (".+\\|]] .. string.sub(entity.id, 2) .. [[=(\\d+).+" as ?r) ("|]] .. string.sub(entity.id, 2) .. [[=" as ?p)
  (IF(CONTAINS(?r1,?p),xsd:integer(REPLACE(?r1,?r,"$1","s")),-1) AS ?c1) (xsd:dateTime(?t1) AS ?d1)
  (IF(CONTAINS(?r2,?p),xsd:integer(REPLACE(?r2,?r,"$1","s")),-1) AS ?c2) (xsd:dateTime(?t2) AS ?d2)
  (IF(CONTAINS(?r3,?p),xsd:integer(REPLACE(?r3,?r,"$1","s")),-1) AS ?c3) (xsd:dateTime(?t3) AS ?d3)
  (IF(CONTAINS(?r4,?p),xsd:integer(REPLACE(?r4,?r,"$1","s")),-1) AS ?c4) (xsd:dateTime(?t4) AS ?d4)
  (IF(CONTAINS(?r5,?p),xsd:integer(REPLACE(?r5,?r,"$1","s")),-1) AS ?c5) (xsd:dateTime(?t5) AS ?d5)
  (IF(CONTAINS(?r6,?p),xsd:integer(REPLACE(?r6,?r,"$1","s")),-1) AS ?c6) (xsd:dateTime(?t6) AS ?d6)
  (IF(CONTAINS(?r7,?p),xsd:integer(REPLACE(?r7,?r,"$1","s")),-1) AS ?c7) (xsd:dateTime(?t7) AS ?d7)
  (IF(CONTAINS(?r8,?p),xsd:integer(REPLACE(?r8,?r,"$1","s")),-1) AS ?c8) (xsd:dateTime(?t8) AS ?d8)
  (IF(CONTAINS(?r9,?p),xsd:integer(REPLACE(?r9,?r,"$1","s")),-1) AS ?c9) (xsd:dateTime(?t9) AS ?d9)
  (IF(CONTAINS(?r10,?p),xsd:integer(REPLACE(?r10,?r,"$1","s")),-1) AS ?c10) (xsd:dateTime(?t10) AS ?d10)
  { SERVICE wikibase:mwapi {
      VALUES ?bot_user { "PLbot" "DeltaBot" }
      bd:serviceParam wikibase:api "Generator" ; wikibase:endpoint "www.wikidata.org" ; mwapi:generator "allpages" ;
                      mwapi:gapfrom "Number_of_main_statements_by_property" ; mwapi:gapto "Number_of_main_statements_by_property" ; mwapi:gapnamespace "10" ;
                      mwapi:prop "revisions" ; mwapi:rvprop "content|timestamp" ; mwapi:rvlimit "10" ; mwapi:rvuser ?bot_user ; wikibase:limit "10" .
      ?t1 wikibase:apiOutput "revisions/rev[1]/@timestamp" . ?r1 wikibase:apiOutput "revisions/rev[1]/text()" .
      ?t2 wikibase:apiOutput "revisions/rev[2]/@timestamp" . ?r2 wikibase:apiOutput "revisions/rev[2]/text()" .
      ?t3 wikibase:apiOutput "revisions/rev[3]/@timestamp" . ?r3 wikibase:apiOutput "revisions/rev[3]/text()" .
      ?t4 wikibase:apiOutput "revisions/rev[4]/@timestamp" . ?r4 wikibase:apiOutput "revisions/rev[4]/text()" .
      ?t5 wikibase:apiOutput "revisions/rev[5]/@timestamp" . ?r5 wikibase:apiOutput "revisions/rev[5]/text()" .
      ?t6 wikibase:apiOutput "revisions/rev[6]/@timestamp" . ?r6 wikibase:apiOutput "revisions/rev[6]/text()" .
      ?t7 wikibase:apiOutput "revisions/rev[7]/@timestamp" . ?r7 wikibase:apiOutput "revisions/rev[7]/text()" .
      ?t8 wikibase:apiOutput "revisions/rev[8]/@timestamp" . ?r8 wikibase:apiOutput "revisions/rev[8]/text()" .
      ?t9 wikibase:apiOutput "revisions/rev[9]/@timestamp" . ?r9 wikibase:apiOutput "revisions/rev[9]/text()" .
      ?t10 wikibase:apiOutput "revisions/rev[10]/@timestamp" . ?r10 wikibase:apiOutput "revisions/rev[10]/text()" .
    }
  }
} as %revs {
  {BIND(?c1 AS ?count) BIND(?d1 AS ?day) INCLUDE %revs} UNION
  {BIND(?c2 AS ?count) BIND(?d2 AS ?day) INCLUDE %revs} UNION
  {BIND(?c3 AS ?count) BIND(?d3 AS ?day) INCLUDE %revs} UNION
  {BIND(?c4 AS ?count) BIND(?d4 AS ?day) INCLUDE %revs} UNION
  {BIND(?c5 AS ?count) BIND(?d5 AS ?day) INCLUDE %revs} UNION
  {BIND(?c6 AS ?count) BIND(?d6 AS ?day) INCLUDE %revs} UNION
  {BIND(?c7 AS ?count) BIND(?d7 AS ?day) INCLUDE %revs} UNION
  {BIND(?c8 AS ?count) BIND(?d8 AS ?day) INCLUDE %revs} UNION
  {BIND(?c9 AS ?count) BIND(?d9 AS ?day) INCLUDE %revs} UNION
  {BIND(?c10 AS ?count) BIND(?d10 AS ?day) INCLUDE %revs}
  FILTER(?count != -1)
}]])
				append           = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

				local usagecount = tonumber(frame:expandTemplate { title = 'Property uses', args = { string.sub(entity.id, 2) } }) or
					0
				if (usagecount < 400000) and (usagecount > 100) then
					linkText = translate("chart by item creation date")
					linkURL  =
						"https://query.wikidata.org/embed.html#%23defaultView%3AAreaChart%0ASELECT%20%3Fdate%20%3Fcumulativecount%0A%7B%0A%20%20%20%20hint%3AQuery%20hint%3Aoptimizer%20%22None%22.%0A%20%20%20%20%7B%20%20%20SELECT%20%3Fmilestonep%20%28COUNT%28%3Fitem%29%20as%20%3Fcumulativecount%29%0A%20%20%20%20%20%20%20%20WHERE%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%3Fitem%20wdt%3A" ..
						entity.id ..
						"%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20BIND%28%20xsd%3Ainteger%28%20substr%28str%28%3Fitem%29%2C%2033%29%29%20as%20%3Fqid%29%0A%20%20%20%20%20%20%20%20%20%20wd%3AQ38074555%20p%3AP1114%20%3Fmilestonep%20.%0A%20%20%20%20%20%20%20%20%20%20%3Fmilestonep%20ps%3AP1114%20%3Fmilestone%20.%0A%20%20%20%20%20%20%20%20%20%20FILTER%28%20%3Fmilestone%20%3E%20%3Fqid%20%29%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20GROUP%20BY%20%3Fmilestonep%0A%20%20%20%20%7D%0A%20%20%20%20%3Fmilestonep%20pq%3AP585%20%3Fdate%0A%7D"
					append   = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'
				end

				if entity.datatype == 'time' then
					linkText = translate("future dates")
					linkURL =
						"https://query.wikidata.org/#SELECT%20%3Fsubject%20%3FsubjectLabel%20%3Finstance%20%3FinstanceLabel%20%28year%28%3Fdate%29%20as%20%3Fyear%29%20%3Fdate%20%20WHERE%20{%0A%20%20%20%3Fsubject%20p%3A" ..
						entity.id ..
						"%2Fpsv%3A" ..
						entity.id ..
						"%20%3Fdate_node%20.%0A%20%20%20%3Fdate_node%20wikibase%3AtimeValue%20%3Fdate%20.%0A%20%20%20FILTER%20%28%28%3Fdate%29%20%3E%3D%20%28now%28%29%29%29%20.%0A%20%20%20%3Fsubject%20wdt%3AP31%20%3Finstance%20.%0A%20%20SERVICE%20wikibase%3Alabel%20{%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
						lang .. "%2Cen%22%20.%0A%20%20}%0A}%0AORDER%20BY%20DESC%28%3Fdate%29%0ALIMIT%20100"
					append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

					linkText = translate("dates in Gregorian calendar before 1582")
					linkURL =
						"https://query.wikidata.org/#%23Dates%20before%201582%20in%20Gregorian%20calendar%20%28date%20precision%20%3E%209%29%0A%23added%202016-02-26%2C%20based%20on%20a%20query%20by%20Mbch331%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fdate%0A{%0A%20%20{%20%20SELECT%20%3Fitem%20%3Fdate%0A%20%20%20%20{%0A%20%20%20%20%20%20%3Fitem%20p%3A" ..
						entity.id ..
						"%2Fpsv%3A" ..
						entity.id ..
						"%20%3Fdatevalue%20.%0A%20%20%20%20%20%20%20%20%3Fdatevalue%20wikibase%3AtimeValue%20%3Fdate%20.%0A%20%20%20%20%20%20FILTER%28%3Fdate%20%3C%20%22%2B1582-10-15T00%3A00%3A00Z%22^^xsd%3AdateTime%29%0A%20%20%20%20%20%20%3Fdatevalue%20wikibase%3AtimePrecision%20%3Fdateprecision%20.%0A%20%20%20%20%20%20FILTER%28%3Fdateprecision%20%3E%209%29%0A%20%20%20%20%20%20%3Fdatevalue%20wikibase%3AtimeCalendarModel%20wd%3AQ1985727%20.%0A%20%20%20%20}%0A%20%20%20%20%20%20%20%20LIMIT%205000%0A%20%20%20%20}%0A%20%20SERVICE%20wikibase%3Alabel%20{%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
						lang .. "%2Cen%22%20}%0A}"
					append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

					linkText = translate("dates before year 1")
					linkURL =
						"https://query.wikidata.org/#%23Dates%20before%20year%201%20with%20precision%20%3E8%3A%20year%2C%20month%2C%20day%0A%23added%202016-08-07%2C%20Jura1%0ASELECT%20%3Fitem%20%3FitemLabel%20%28%3Fdate%20as%20%3FGregorianDate%29%20%28CONCAT%28str%281%2BYEAR%28%3Fdate%29%2a-1%29%2C%20%22%20BC%22%29%20as%20%3FyearBC%29%20%3Fdateprec%20%3FdateprecisionLabel%20%3FinputcalendarmodelLabel%0A%7B%0A%20%20%7B%20%20SELECT%20%3Fitem%20%3Fdate%20%28xsd%3Adecimal%28%3Fprecision%29%20as%20%3Fdateprec%29%20%3Finputcalendarmodel%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%3Fitem%20p%3A" ..
						entity.id ..
						"%20%3Fd%20.%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Fd%20psv%3A" ..
						entity.id ..
						"%20%3Fdatevalue%20.%0A%20%20%20%20%20%20%20%20%3Fdatevalue%20wikibase%3AtimeValue%20%3Fdate%20.%20%20%20%20%20%20%20%20%20%20%20%20%20FILTER%28%3Fdate%20%3C%20%22%2B1-00-00T00%3A00%3A00Z%22%5E%5Exsd%3AdateTime%29%0A%20%20%20%20%20%20%3Fdatevalue%20wikibase%3AtimePrecision%20%3Fprecision%20.%20%20%20%20%20FILTER%28%3Fprecision%20%3E%208%29%0A%20%20%20%20%20%20%3Fd%20wikibase%3Arank%20%3Frank%20.%20%20%20%20%20%20%20%20%20%20%20%20%20%20FILTER%28%3Frank%20%21%3D%20wikibase%3ADeprecatedRank%29%0A%20%20%20%20%20%20%3Fdatevalue%20wikibase%3AtimeCalendarModel%20%3Finputcalendarmodel%20.%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20LIMIT%205000%0A%20%20%20%20%7D%0A%20%20%20%20%3Fdateprecision%20wdt%3AP2803%20%3Fdateprec%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
						lang .. "%2Cen%2Cla%22%20%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fdate%29"
					append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. '] ([[Help:Dates#Years BC]])'

					linkText = translate("date on January 1")
					linkURL =
						"https://query.wikidata.org/#%23title%3A%20date%20on%20January%201%20with%20best%20rank%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fdate%0A%7B%0A%20%20%3Fitem%20p%3A" ..
						entity.id ..
						"%20%5B%20a%20wikibase%3ABestRank%3B%20psv%3A" ..
						entity.id ..
						"%20%5B%20wikibase%3AtimeValue%20%3Fdate%3B%20wikibase%3AtimePrecision%2011%20%5D%5D%20.%20%0A%20%20FILTER%28%20MONTH%28%3Fdate%29%20%3D%201%20%26%26%20DAY%28%3Fdate%29%20%3D%201%20%29%20%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0ALIMIT%201000"
					append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. '] ([[Help:Dates#January 1 as date]])'
				end
				if entity.datatype == 'quantity' then
					linkText = translate("min&max")
					linkURL =
						"https://query.wikidata.org/embed.html#SELECT%20%2a%0AWITH%0A%7B%20SELECT%20%28%22qualifier%22%20as%20%3Ftype%29%20%20%20%20%20%20%20%20%20%28MIN%28%3Fq%29%20as%20%3Fmin%29%20%20%28MAX%28%3Fq%29%20as%20%3Fmax%29%20%20%20%20WHERE%20%20%7B%20%5B%5D%20pq%3A" ..
						entity.id ..
						"%20%3Fq%20%7D%20%7D%20as%20%25pq%0AWITH%0A%7B%20SELECT%20%28%22statement%20value%22%20as%20%3Ftype%29%20%20%20%28MIN%28%3Fq%29%20as%20%3Fmin%29%20%20%28MAX%28%3Fq%29%20as%20%3Fmax%29%20%20%20%20WHERE%20%20%7B%20%5B%5D%20wdt%3A" ..
						entity.id ..
						"%20%3Fq%20%7D%20%7D%20as%20%25wdt%0AWHERE%0A%7B%0A%20%20%7B%20INCLUDE%20%25pq%20%7D%20UNION%20%7B%20INCLUDE%20%25wdt%20%7D%0A%7D"
					append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'

					linkText = translate("Items with highest numeric value")
					linkURL =
						"https://query.wikidata.org/#SELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%0A%7B%0A%20%20%3Fitem%20wdt%3A" ..
						entity.id ..
						"%20%3Fvalue%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
						lang .. "%2Cen%22%20%20%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fvalue%29%0ALIMIT%2050"
					append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

					linkText = translate("Items with lowest numeric value")
					linkURL =
						"https://query.wikidata.org/#SELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%0A%7B%0A%20%20%3Fitem%20wdt%3A" ..
						entity.id ..
						"%20%3Fvalue%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
						lang .. "%2Cen%22%20%20%7D%0A%7D%0AORDER%20BY%20ASC%28%3Fvalue%29%0ALIMIT%2050"
					append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

					-- todo : queries with units/normalized unit value
				end

				-- type constraint including human or person
				local onQ5 = #(d.getClaims { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q21503250', qualifier = 'P2308', qualifiervalue = 'Q5' } or {})
				local onPerson = #(d.getClaims { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q21503250', qualifier = 'P2308', qualifiervalue = 'Q215627' } or {})
				-- item requires constraint human
				local forQ5 = #(d.getClaims { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q21503247', qualifier = 'P2305', qualifiervalue = 'Q5' } or {})

				if (onQ5 > 0 or forQ5 > 0 or onPerson > 0) then
					-- display queries on properties generally used (also) for humans
					linkText = translate("Map of people by place values:")
					append = append .. '\n* ' .. linkText

					local mappeople = { "P19", "P69", "P937", "P551", "P20", "P119" }
					local mappropertyname
					local propertyname = frame:preprocess('{{urlencode:"{{label|' ..
						entity.id .. '|' .. lang .. '}}" (' .. entity.id .. ')| PATH }}')
					for _, mapproperty in ipairs(mappeople) do
						linkText = frame:preprocess('{{label|' .. mapproperty .. '|' .. lang .. '}}')
						mappropertyname = frame:preprocess('{{urlencode:"' ..
							linkText .. '" (' .. mapproperty .. ')| PATH }}')
						linkURL = "https://query.wikidata.org/#%23title%3A" ..
							mappropertyname ..
							"%20of%20people%20with%20property%20" ..
							propertyname ..
							"%20statements%0A%23defaultView%3AMap%7B%22hide%22%3A%5B%22%3Fcoordinates%22%5D%7D%0ASELECT%20DISTINCT%20%3Fperson%20%3FpersonLabel%20%3FpersonDescription%20%3Fcoordinates%20%3Fimage%20%3Fvalue%0AWHERE%20%0A%7B%0A%20%20%3Fperson%20wdt%3A" ..
							entity.id ..
							"%20%3Fvalue%20%3B%20wdt%3A" ..
							mapproperty ..
							"%2Fwdt%3AP625%20%3Fcoordinates.%0A%20%20hint%3AQuery%20hint%3Aoptimizer%20%22None%22.%0A%20%20OPTIONAL%20%7B%20%3Fperson%20wdt%3AP18%20%3Fimage%20%7D%0A%20%20%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0ALIMIT%201000"
						append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'
					end

					linkText = translate("January 1st dates")
					append = append .. '\n* [[Help:Dates#January_1_as_date|' .. linkText .. ']]'

					linkText = translate("People with date of birth on January 1")
					linkURL =
						"https://query.wikidata.org/#%23title%3A%20people%20with%20property%20born%20on%20January%201%20%28best%20rank%2C%20day%20precision%20dates%20only%29%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%3Fdob%0A%7B%0A%20%20%3Fitem%20wdt%3A" ..
						entity.id ..
						"%20%3Fvalue%20.%0A%20%20%3Fitem%20p%3AP569%20%5B%20a%20wikibase%3ABestRank%3B%20psv%3AP569%20%5B%20wikibase%3AtimeValue%20%3Fdob%3B%20wikibase%3AtimePrecision%2011%20%5D%5D%20.%20%0A%20%20FILTER%28%20MONTH%28%3Fdob%29%20%3D%201%20%26%26%20DAY%28%3Fdob%29%20%3D%201%20%29%20%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0ALIMIT%201000"
					append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

					linkText = translate("People with date of death on January 1")
					linkURL =
						"https://query.wikidata.org/#%23title%3A%20people%20with%20property%20died%20on%20January%201%20%28best%20rank%2C%20day%20precision%20dates%20only%29%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%3Fdod%0A%7B%0A%20%20%3Fitem%20wdt%3A" ..
						entity.id ..
						"%20%3Fvalue%20.%0A%20%20%3Fitem%20p%3AP570%20%5B%20a%20wikibase%3ABestRank%3B%20psv%3AP570%20%5B%20wikibase%3AtimeValue%20%3Fdod%3B%20wikibase%3AtimePrecision%2011%20%5D%5D%20.%20%0A%20%20FILTER%28%20MONTH%28%3Fdod%29%20%3D%201%20%26%26%20DAY%28%3Fdod%29%20%3D%201%20%29%20%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0ALIMIT%201000"
					append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'

					linkText = translate("People with both dates on January 1")
					linkURL =
						"https://query.wikidata.org/#%23title%3A%20people%20with%20property%20and%20dates%20of%20birth%2Fdeath%20on%20January%201%20%28best%20rank%2C%20day%20precision%20dates%20only%29%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%3Fdob%20%3Fdod%0A%7B%0A%20%20%3Fitem%20wdt%3A" ..
						entity.id ..
						"%20%3Fvalue.%0A%20%20%3Fitem%20p%3AP569%20%5B%20a%20wikibase%3ABestRank%3B%20psv%3AP569%20%5B%20wikibase%3AtimeValue%20%3Fdob%3B%20wikibase%3AtimePrecision%2011%20%5D%5D.%0A%20%20FILTER%28%20MONTH%28%3Fdob%29%20%3D%201%20%26%26%20DAY%28%3Fdob%29%20%3D%201%20%29%0A%20%20%3Fitem%20p%3AP570%20%5B%20a%20wikibase%3ABestRank%3B%20psv%3AP570%20%5B%20wikibase%3AtimeValue%20%3Fdod%3B%20wikibase%3AtimePrecision%2011%20%5D%5D.%0A%20%20FILTER%28%20MONTH%28%3Fdod%29%20%3D%201%20%26%26%20DAY%28%3Fdod%29%20%3D%201%20%29%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%0ALIMIT%201000"
					append = append .. '\n** [' .. linkURL .. ' ' .. linkText .. ']'
				end

				linkURL = d.formatStatements {
						conjtype = '<br />',
						entity = entity,
						property = 'P2264',
						pattern = '[[mixnmatch:$1|' .. translate('mixnmatch') .. ']]'
						.. linguistic.inparentheses("[[User:Magnus Manske/Mix'n'match report/$1|" .. translate('mixnmatch-report') .. ']]', p.getLang()),
					}
				if linkURL then
					append = append .. '\n* ' .. linkURL
				end


				local quantity = entity.datatype == 'quantity'
				append = append
					..
					frame:preprocess(
						'{{#ifexist:Wikidata:Database_reports/Recent_deaths/missing/{{PAGENAME}}|* [[Wikidata:Database_reports/Recent_deaths/missing/{{PAGENAME}}|Database reports/Recent deaths/missing/{{PAGENAME}}]]}}')
					..
					(quantity and frame:preprocess('{{#ifexist:User:Laboramus/Units/{{PAGENAME}}|* [[User:Laboramus/Units/{{PAGENAME}}]]' .. linguistic.inparentheses(translate('units used'), lang) .. '}}') or '')
					..
					frame:preprocess(
						'{{#ifexist:Wikidata:Database_reports/Complex_constraint_violations/{{PAGENAME}}|* [[Wikidata:Database_reports/Complex_constraint_violations/{{PAGENAME}}|' ..
						translate('database-reports-complex-constraint-violations') .. '/{{PAGENAME}}]]}}')
					..
					frame:preprocess(
						'{{#ifexist:Wikidata:Database_reports/Humans_with_missing_claims/{{PAGENAME}}|* [[Wikidata:Database reports/Humans with missing claims/{{PAGENAME}}|Database reports/Humans with missing claims/{{PAGENAME}}]]}}')
					..
					frame:preprocess(
						'{{#ifexist:Wikidata:Database_reports/Constraint_violations/{{PAGENAME}}|* [[Wikidata:Database_reports/Constraint_violations/{{PAGENAME}}|' ..
						translate('database-reports-constraint-violations') .. '/{{PAGENAME}}]]}}')

				if not forQualifierUse then
					linkText = translate("map")
					linkURL =
						"https://query.wikidata.org/embed.html#%23title%3Alocation%20of%20items%20with%20property%20" ..
						entity.id ..
						"%20statements%0A%23defaultView%3AMap%7B%22hide%22%3A%5B%22%3Fcoordinates%22%5D%7D%0ASELECT%20DISTINCT%20%3Fsite%20%3FsiteLabel%20%3Fcoordinates%20%3Fimage%20%3Fvalue%0AWHERE%20%0A%7B%0A%20%20%3Fsite%20wdt%3A" ..
						entity.id ..
						"%20%3Fvalue%3B%20wdt%3AP625%20%3Fcoordinates.%0A%20%20OPTIONAL%20%7B%20%3Fsite%20wdt%3AP18%20%3Fimage%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D"
					append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'
				end

				if not forQualifierUse then
					linkText = translate("random list")
					linkURL =
						"https://query.wikidata.org/embed.html#SELECT%20%3Fitem%20%3FitemLabel%20%3FitemDescription%20%3Fvalue%20%3Fst%20%3Fids%20%3Fsl%0AWHERE%0A%7B%0A%20%20SERVICE%20bd%3Asample%20%7B%20%3Fitem%20wdt%3A" ..
						entity.id ..
						"%20%3Fvalue.%20bd%3AserviceParam%20bd%3Asample.limit%2042%20%7D%0A%20%20OPTIONAL%20%7B%20%3Fitem%20wikibase%3Astatements%20%3Fst%20%3B%20wikibase%3Aidentifiers%20%3Fids%20%3B%20wikibase%3Asitelinks%20%3Fsl%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D"
					append = append .. '\n* [' .. linkURL .. ' ' .. linkText .. ']'
				end
			end
			return append
		end,
		maintenancecat = function(data)
			if data.lists then
				return "Property documentation using 'lists'"
			end
			return nil
		end,
	},
	{
		label = 'living people protection class',
		query = 'P8274',
	},
	{
		label = 'proposal discussion',
		value = function(data)
			local arg = data['proposed by'] or data.subpage
			if arg then
				if string.lower(arg) == 'none' then
					return translate('no discussion')
				end
				local link = 'Property proposal/'
				if tonumber(arg) then
					link = link .. 'Archive/' .. arg .. '#' .. entity.id
				else
					link = link .. arg
				end
				if not mw.title.new(link, 'Wikidata') then
					return arg
				end
				return '[[Wikidata:' .. link .. '|' .. link .. ']]'
			end
			local discussion_link = d.formatStatements {
					entity = entity,
					property = 'P3254',
					numval = 1,
					pattern = '$1',
				}
			if not discussion_link then
				return translate('no discussion')
			end

			local discussion_page = discussion_link:gsub('https://www.wikidata.org/wiki/', '')
			if discussion_page == discussion_link then
				-- This should really not happen
				return '<span class="plainlinks">[' ..
					discussion_link .. ' ' .. translate('proposal discussion') .. ']</span>'
			end

			discussion_page = mw.uri.decode(discussion_page)
			return '[[' .. discussion_page .. '|' .. translate('proposal discussion') .. ']]'
		end,
		maintenancecat = function(data)
			if data['proposed by'] and data.subpage then
				return 'Property documentation with unclear link to its proposal discussion'
			end
			local arg = data['proposed by'] or data.subpage
			if arg then
				if string.lower(arg) == 'none' then
					return 'Property originally created without a formal discussion'
				end
				local link
				if tonumber(arg) then
					link = 'Property proposal/Archive/' .. arg
				else
					link = 'Property proposal/' .. arg
				end
				local title = mw.title.new(link, 'Wikidata')
				if not title or not title.exists then
					return 'Property documentation with malformed link to its proposal discussion'
				end
			end
			return nil
		end,
		required = true,
		missingcat = 'Property documentation missing a link to its proposal discussion',
	},
	{
		label = 'current uses',
		value = function()
			local total = tonumber(frame:expandTemplate { title = 'Property uses', args = { string.sub(entity.id, 2) } })
			local distinctuses = tonumber(frame:expandTemplate { title = 'Statcount', args = { string.sub(entity.id, 1) .. "distinct" } })
			if total and total > 0 then
				addcat(string.format('Properties used on %d+ items', 10 ^ math.floor(math.log10(total))))
				local usage_table = mw.html.create('table')
					:attr('class', 'wikitable')
					:css('font-size', 'smaller')
					:css('text-align', 'right')
					:done();
				local row = usage_table:tag('tr')
					:tag('td')
					:css('text-align', 'left')
					:wikitext("'''Total'''")
					:done()
					:tag('td')
					:wikitext(formatNum(total, p.getLang()))
					:done()
					:tag('td')
					:wikitext('') -- empty
					:done()
				if distinctuses and distinctuses > 0 then
					addcat('Properties with distinct value counts')
					row:tag('td')
						:wikitext("'''<span title='distinct values of property'>distinct values'''</span>")
						:done()
					row:tag('td')
						:wikitext("<span title='items with property / distinct values of property'>'''ratio'''</span>")
						:done()
				end

				local main_value = tonumber(frame:expandTemplate { title = 'Number of main statements by property', args = { string.sub(entity.id, 2) } })
				local qual_value = tonumber(frame:expandTemplate { title = 'Number of qualifiers by property', args = { string.sub(entity.id, 2) } })
				local ref_value = tonumber(frame:expandTemplate { title = 'Number of references by property', args = { string.sub(entity.id, 2) } })
				local makePercentage = function(number, total)
					if number == total then
						return formatNum('100', p.getLang())
					end
					local ratio = number / total
					if ratio < .001 then
						return '<' .. formatNum('0.1', p.getLang())
					elseif ratio > .999 then
						return '>' .. formatNum('99.9', p.getLang())
					else
						return formatNum(string.format('%.1f', 100 * ratio), p.getLang())
					end
				end

				if main_value and main_value > 0 then
					local row = usage_table:tag('tr')
						:tag('td')
						:css('text-align', 'left')
						:wikitext(translate('main-statement'))
						:done()
					local sourceIds = tonumber(d.formatStatements {
							entity = entity,
							numval = 1,
							property = 'P4876',
							rank = 'best',
						})

					local main_value_text
					if sourceIds and sourceIds > 0 then
						local percentage = string.format('%.0f', 100 * main_value / sourceIds)
						main_value_text = mw.ustring.format(translate('identifier coverage statistics'),
								formatNum(main_value, p.getLang()),
								formatNum(sourceIds, p.getLang()),
								formatNum(percentage, p.getLang()))
					else
						main_value_text = formatNum(main_value, p.getLang())
					end

					local linkURL =
						"https://query.wikidata.org/#%23%20This%20query%20finds%20all%20usages%20of%20property%20" ..
						entity.id ..
						"%20AS%20a%20statement.%0A%23%0A%23%20Important%20note%3A%20most%20SPARQL%20users%20don%27t%20need%20such%20level%20of%20complication%21%0A%23%20If%20you%20want%20to%20get%20only%20concrete%20values%20with%20best%20rank%2C%20replace%20body%20with%20something%20like%3A%0A%23%20%3Fitem%20wdt%3A" ..
						entity.id ..
						"%20%3Fvalue.%0A%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fstatement%20%3Frank%20%3Fsnaktype%20%3Fvalue%0A%7B%0A%20%20%3Fitem%20p%3A" ..
						entity.id ..
						"%20%3Fstatement%20.%0A%20%20%3Fstatement%20wikibase%3Arank%20%3Frank%20.%0A%20%20OPTIONAL%20%7B%20%3Fstatement%20ps%3A" ..
						entity.id ..
						"%20%3Fdbvalue%20%7D%0A%0A%20%20BIND%28COALESCE%28IF%28wikibase%3AisSomeValue%28%3Fdbvalue%29%2C%20%22unknown%22%2C%20%22concrete%22%29%2C%20%22novalue%22%29%20AS%20%3Fsnaktype%29%0A%20%20BIND%28IF%28wikibase%3AisSomeValue%28%3Fdbvalue%29%2C%20%22%22%2C%20%3Fdbvalue%29%20AS%20%3Fvalue%29%0A%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%20%7D%0A%7D%0ALIMIT%201000%0A"
					local linkText = "[" .. linkURL .. " " .. main_value_text .. "]"
					row:tag('td')
						:wikitext(linkText)
						:done()

					row:tag('td')
						:wikitext(mw.ustring.format(translate('percentage-of-uses'), makePercentage(main_value, total)))
						:done()
					if distinctuses and distinctuses > 0 then
						row:tag('td')
							:wikitext(formatNum(distinctuses, p.getLang()))
							:done()
						row:tag('td')
							:wikitext("<span title='one value is used on ~" ..
							string.format('%.1f', main_value / distinctuses) ..
							" items'>" .. string.format('%.1f', main_value / distinctuses) .. "</span>")
							:done()
					end
				end
				if qual_value and qual_value > 0 then
					local linkURL =
						"https://query.wikidata.org/#%23%20This%20query%20finds%20all%20usages%20of%20property%20" ..
						entity.id ..
						"%20as%20a%20qualifier.%0A%23%0A%23%20Important%20note%3A%20most%20SPARQL%20users%20don%27t%20need%20such%20level%20of%20complication%21%0A%23%20If%20you%20want%20to%20get%20only%20concrete%20values%20with%20best%20rank%2C%20replace%20body%20with%20something%20like%3A%0A%23%20%3Fitem%20%3Frelation%20%5Brdf%3Atype%20wikibase%3ABestRank%3B%20pq%3A" ..
						entity.id ..
						"%20%3Fvalue%5D%0A%23%0A%23%20Implementation%20note%3A%20it%20is%20not%20possible%20to%20distinguish%20between%20items%20where%20statement%20looks%20like%3A%0A%23%20" ..
						entity.id ..
						"%3A%20novalue%20with%20qualifier%20" ..
						entity.id ..
						"%3A%20novalue%2C%20AS%20both%20facts%20are%20stored%20in%20a%20form%20of%3A%0A%23%20%28p%3Axxxx%20rdf%3Atype%20wdno%3A" ..
						entity.id ..
						"%29%0A%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Frelitem%20%3FrelitemLabel%20%3Fstatement%20%3Frank%20%3Fsnaktype%20%3Fvalue%20%3Fqual_snaktype%20%3Fqual_value%0AWITH%20%7B%0A%20%20SELECT%20%3Fitem%20%3Frelitem%20%3Fstatement%20%3Fqual_snaktype%20%3Fqual_value%20%7B%0A%20%20%20%20hint%3AQuery%20hint%3Aoptimizer%20%22None%22%20.%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%3Fstatement%20pq%3A" ..
						entity.id ..
						"%20%3Fqual_dbvalue%20.%0A%20%20%20%20%20%20%3Fitem%20%3Frelation%20%3Fstatement%20.%0A%20%20%20%20%20%20%3Frelation%20rdf%3Atype%20owl%3AObjectProperty%20.%0A%20%20%20%20%20%20%3Frelitem%20wikibase%3Aclaim%20%3Frelation%20.%0A%20%20%20%20%7D%20UNION%20%7B%0A%20%20%20%20%20%20%3Fstatement%20rdf%3Atype%20wdno%3A" ..
						entity.id ..
						"%20.%0A%20%20%20%20%20%20%3Fitem%20%3Frelation%20%3Fstatement%20.%0A%20%20%20%20%20%20%3Frelation%20rdf%3Atype%20owl%3AObjectProperty%20.%0A%20%20%20%20%20%20%3Frelitem%20wikibase%3Aclaim%20%3Frelation%20.%0A%20%20%20%20%20%20FILTER%28%3Frelation%20%21%3D%20p%3A" ..
						entity.id ..
						"%29%0A%20%20%20%20%7D%20UNION%20%7B%0A%20%20%20%20%20%20%3Fstatement%20rdf%3Atype%20wdno%3A" ..
						entity.id ..
						"%20.%0A%20%20%20%20%20%20%3Fitem%20p%3A" ..
						entity.id ..
						"%20%3Fstatement%20.%0A%20%20%20%20%20%20%3Fstatement%20ps%3A" ..
						entity.id ..
						"%20%5B%5D%20.%0A%20%20%20%20%20%20BIND%28wd%3A" ..
						entity.id ..
						"%20AS%20%3Frelitem%29%0A%20%20%20%20%7D%0A%0A%20%20%20%20BIND%28COALESCE%28IF%28wikibase%3AisSomeValue%28%3Fqual_dbvalue%29%2C%20%22%22%2C%20%3Fqual_dbvalue%29%2C%20%22%22%29%20AS%20%3Fqual_value%29%0A%20%20%20%20BIND%28COALESCE%28IF%28wikibase%3AisSomeValue%28%3Fqual_dbvalue%29%2C%20%22unknown%22%2C%20%22concrete%22%29%2C%20%22novalue%22%29%20AS%20%3Fqual_snaktype%29%0A%20%20%7D%20LIMIT%201000%0A%7D%20AS%20%25statements%0A%7B%0A%20%20INCLUDE%20%25statements%0A%0A%20%20%3Fstatement%20wikibase%3Arank%20%3Frank%20.%0A%20%20%3Frelitem%20wikibase%3AstatementProperty%20%3Fps%20.%0A%0A%20%20OPTIONAL%20%7B%20%3Fstatement%20%3Fps%20%3Fdbvalue%20%7D%0A%0A%20%20BIND%28COALESCE%28IF%28wikibase%3AisSomeValue%28%3Fdbvalue%29%2C%20%22unknown%22%2C%20%22concrete%22%29%2C%20%22novalue%22%29%20AS%20%3Fsnaktype%29%0A%20%20BIND%28IF%28wikibase%3AisSomeValue%28%3Fdbvalue%29%2C%20%22%22%2C%20%3Fdbvalue%29%20AS%20%3Fvalue%29%0A%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%20%7D%0A%7D%0A"
					local linkText = "[" .. linkURL .. " " .. formatNum(qual_value, p.getLang()) .. "]"

					usage_table:tag('tr')
						:tag('td')
						:css('text-align', 'left')
						:wikitext(translate('qualifier'))
						:done()
						:tag('td')
						:wikitext(linkText)
						:done()
						:tag('td')
						:wikitext(mw.ustring.format(translate('percentage-of-uses'), makePercentage(qual_value, total)))
						:done()
				end
				if ref_value and ref_value > 0 then
					local linkURL =
						"https://query.wikidata.org/#%23%20This%20query%20finds%20all%20usages%20of%20property%20" ..
						entity.id ..
						"%20as%20a%20reference.%0A%23%0A%23%20Important%20note%3A%20most%20SPARQL%20users%20don%27t%20need%20such%20level%20of%20complication%21%0A%23%20If%20you%20want%20to%20get%20only%20concrete%20values%20with%20best%20rank%2C%20replace%20body%20with%20something%20like%3A%0A%23%20%3Fitem%20%3Frelation%20%5Brdf%3Atype%20wikibase%3ABestRank%3B%20prov%3AwasDerivedFrom%2Fpr%3A" ..
						entity.id ..
						"%20%3Fref_value%20%5D%0A%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Frelitem%20%3FrelitemLabel%20%3Fstatement%20%3Frank%20%3Fsnaktype%20%3Fvalue%20%3Fref_snaktype%20%3Fref_value%0AWITH%0A%7B%0A%20%20%23%20sometimes%20blank%20values%20are%20indexed%20multiple%20times%3B%20to%20fix%20the%20output%2C%20we%20select%20distinct%20values%20in%20a%20subquery%0A%20%20SELECT%20DISTINCT%20%3Fstatement%20%3Fprovenance%20%3Fref_snaktype%20%3Fref_value%20%7B%0A%20%20%20%20%7B%20%3Fprovenance%20pr%3A" ..
						entity.id ..
						"%20%3Fref_dbvalue%3B%20%5Eprov%3AwasDerivedFrom%20%3Fstatement%20%7D%20UNION%20%7B%3Fprovenance%20rdf%3Atype%20wdno%3A" ..
						entity.id ..
						"%3B%20%5Eprov%3AwasDerivedFrom%20%3Fstatement%20%7D%0A%0A%20%20%20%20BIND%28COALESCE%28IF%28wikibase%3AisSomeValue%28%3Fref_dbvalue%29%2C%20%22%22%2C%20%3Fref_dbvalue%29%2C%20%22%22%29%20AS%20%3Fref_value%29%0A%20%20%20%20BIND%28COALESCE%28IF%28wikibase%3AisSomeValue%28%3Fref_dbvalue%29%2C%20%22unknown%22%2C%20%22concrete%22%29%2C%20%22novalue%22%29%20AS%20%3Fref_snaktype%29%0A%20%20%7D%20LIMIT%201000%0A%7D%20AS%20%25prov%0A%7B%0A%20%20INCLUDE%20%25prov%0A%0A%20%20%3Fitem%20%3Frelation%20%3Fstatement%20.%0A%20%20%3Fstatement%20wikibase%3Arank%20%3Frank%20.%0A%0A%20%20%3Frelitem%20wikibase%3Aclaim%20%3Frelation%3B%20wikibase%3Anovalue%20%3Fwdno%3B%20wikibase%3AstatementProperty%20%3Fps%20.%0A%0A%20%20OPTIONAL%20%7B%20%3Fstatement%20%3Fps%20%3Fdbvalue%20%7D%0A%0A%20%20BIND%28COALESCE%28IF%28wikibase%3AisSomeValue%28%3Fdbvalue%29%2C%20%22unknown%22%2C%20%22concrete%22%29%2C%20%22novalue%22%29%20AS%20%3Fsnaktype%29%0A%20%20BIND%28IF%28wikibase%3AisSomeValue%28%3Fdbvalue%29%2C%20%22%22%2C%20%3Fdbvalue%29%20AS%20%3Fvalue%29%0A%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%20%7D%0A%7D%0A"
					local linkText = "[" .. linkURL .. " " .. formatNum(ref_value, p.getLang()) .. "]"

					usage_table:tag('tr')
						:tag('td')
						:css('text-align', 'left')
						:wikitext(translate('reference'))
						:done()
						:tag('td')
						:wikitext(linkText)
						:done()
						:tag('td')
						:wikitext(mw.ustring.format(translate('percentage-of-uses'), makePercentage(ref_value, total)))
						:done()
				end
				return tostring(usage_table)
			end
			addcat('Unused properties')
			return nil
		end,
	},
	{
		label = 'search for values',
		value = function()
			local value
			if (onMedia > 0 and onItems == 0 and onProperties == 0 and onLexemes == 0 and onForms == 0 and onSenses == 0) then
				---	Commons only properties
			else
				if (
					-- https://gerrit.wikimedia.org/r/plugins/gitiles/operations/mediawiki-config/+/master/wmf-config/SearchSettingsForWikibase.php#65
					entity.datatype == 'string' or
					entity.datatype == 'external-id' or
					entity.datatype == 'url' or
					entity.datatype == 'wikibase-item' or
					entity.datatype == 'wikibase-property' or
					entity.datatype == 'wikibase-lexeme' or
					entity.datatype == 'wikibase-form' or
					entity.datatype == 'wikibase-sense'
					) and not searchPropertiesExclude[entity.id] then
					-- todo: nearcoord for globe-coordinate
					-- todo: quantity (https://www.mediawiki.org/wiki/Help:Extension:WikibaseCirrusSearch#wbstatementquantity)
					local sample = d.showQualifier {
							displayformat = 'raw',
							entity = entity,
							numval = 1,
							property = { 'P1855', 'P2271', 'P5192', 'P5193', 'P5977' },
							rank = 'best',
							qualifier = entity.id,
						}
					local data = {
						'type=fulltext',
						'break=no',
						'inline=true',
						'width=80',
					}
					local search_term
					if sample and sample ~= '' then
						table.insert(data, 'searchbuttonlabel=' .. mw.ustring.format(translate('search for'), sample))
						sample = mw.ustring.gsub(sample, '"', '\\"')
						search_term = mw.ustring.format('"%s=%s"', entity.id, sample)
					else
						search_term = entity.id
					end
					table.insert(data, 'default=haswbstatement:' .. search_term)
					value = frame:extensionTag('inputbox', table.concat(data, '\n'), { align = 'left' })
				end
			end
			return value
		end,
	},
}

function p.getI18n()
	return mw.loadData('Module:i18n/property documentation')
end

function p.init(args)
	local id = args.id or frame:preprocess('{{BASEPAGENAME}}')
	if args.lang then
		p.setLang(args.lang)
	end
	entity = d.getEntityFromId(id)
	if not entity then
		return error('Entity not found')
	end
	addcat('All Properties')
	addcat('Properties with ' .. entity.datatype .. '-datatype')
end

function p.links(args)
	local forQualifierUse = isForQualifierUse()
	local id = entity.id
	local lang = p.getLang()
	local queries = {}
	local linkText, linkURL

	if not forQualifierUse then
		linkText = translate("basic item query")
		linkURL =
			"https://query.wikidata.org/#PREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0APREFIX%20p%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2F%3E%0APREFIX%20ps%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fstatement%2F%3E%0APREFIX%20pq%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fqualifier%2F%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0A%23Sample%20query%20to%20start%20a%20list%20of%20values%20for%20property%20" ..
			id ..
			"%0A%23Remove%20some%20of%20the%20%22%23%22%20below%20to%20see%20if%20it%20helps%20you%0A%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%3FvalueLabel%0A%23sample%20variables%20defined%20below%20%28but%20commented%20out%29%0A%3Fdob%20%3Fdod%20%3Fcountry%0AWHERE%0A{%0A%20%20%3Fitem%20wdt%3A" ..
			id ..
			"%20%3Fvalue%20.%0A%23%20%20%3Fitem%20wdt%3AP569%20%3Fdob%20.%0A%23%20%20OPTIONAL%20{%3Fitem%20wdt%3AP570%20%3Fdod%20}%20.%0A%23%20%20%3Fitem%20wdt%3AP17%20%3Fcountry%20.%0A%20%20SERVICE%20wikibase%3Alabel%20{%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
			lang .. "%2Cen%22%20%20}%0A}%0ALIMIT%2010"
		table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')

		linkText = translate("current uses")
		linkURL = "https://query.wikidata.org/#SELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue" ..
			(entity.datatype == "wikibase-item" and "%20%3FvalueLabel" or "") .. -- Only include "?valueLabel" if property has datatype wikibase-item
			"%0A{%0A%20%20%3Fitem%20wdt%3A" ..
			id ..
			"%20%3Fvalue%20.%0A%20%20SERVICE%20wikibase%3Alabel%20{%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
			lang .. "%2Cen%22%20%20}%0A}%0ALIMIT%201000"
		table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')

		if entity.datatype == 'time' then
			linkText = translate("with precision and calendarmodel")
			linkURL =
				"https://query.wikidata.org/#SELECT%20%3Fitem%20%3FitemLabel%20%3Fvalue%20%3Fprecision%20%3FtpLabel%20%3Fmodel%20%3FmodelLabel%0A%7B%0A%20%20%20%20%7B%20%20SELECT%20%2a%0A%20%20%20%20%20%20%20WHERE%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%3Fitem%20p%3A" ..
				id ..
				"%2Fpsv%3A" ..
				id ..
				"%20%5B%20wikibase%3AtimeValue%20%3Fvalue%20%3B%20wikibase%3AtimePrecision%20%3Fprecision%20%3B%20wikibase%3AtimeCalendarModel%20%3Fmodel%20%5D%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20LIMIT%20200%0A%20%20%20%20%7D%0A%0A%20%20%20%20%23%20for%20labels%0A%20%20%20%20%3Ftp%20wdt%3AP2803%20%3Fpr%20.%20FILTER%28%20str%28%3Fpr%29%20%3D%20str%28%3Fprecision%29%20%29%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		end


		if not prop31279[id] then
			linkText = translate("counts by class")
			linkURL = "https://query.wikidata.org/#%23top%2050%20for%20P31%20and%20P279%20of%20items%20using%20" ..
				id ..
				"%0ASELECT%20%3Fclass%20%3FclassLabel%20%3Fcount%20%3Fuse_as_Label%0A{%20%20{%20%20SELECT%20%3Fclass%20%28COUNT%28*%29%20AS%20%3Fcount%29%20%28wd%3AP31%20as%20%3Fuse_as_%29%0A%20%20%20%20{%20%20%3Fa%20%20wdt%3A" ..
				id ..
				"%20%20%3Fp%20%20%3B%20wdt%3AP31%20%20%3Fclass}%0A%20%20%20%20%20%20%20%20GROUP%20BY%20%3Fclass%20ORDER%20BY%20DESC%28%3Fcount%29%20LIMIT%2050%0A%20%20%20%20}%0A%20%20%20%20UNION%0A%20%20{%20%20SELECT%20%3Fclass%20%28COUNT%28*%29%20AS%20%3Fcount%29%20%28wd%3AP279%20as%20%3Fuse_as_%29%0A%20%20%20%20{%20%20%3Fa%20%20wdt%3A" ..
				id ..
				"%20%20%3Fp%20%20%3B%20wdt%3AP279%20%20%3Fclass}%0A%20%20%20%20%20%20%20%20GROUP%20BY%20%3Fclass%20ORDER%20BY%20DESC%28%3Fcount%29%20LIMIT%2050%0A%20%20%20%20}%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20{%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
				lang .. "%2Cen%22%20}%0A}%0AORDER%20BY%20DESC%28%3Fcount%29%20%3Fclass"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		end

		if entity.datatype == 'wikibase-item' then
			linkText = translate("value list")
			linkURL = "https://query.wikidata.org/#%23Values%20used%20by%20property%20" ..
				id ..
				"%0ASELECT%20%3Fvalue%20%3FvalueLabel%20%3Fct%20%3Fsampleitem%20%3FsampleitemLabel%0AWHERE%0A%7B%0A%20%20%7B%0A%20%20%20%20SELECT%20%3Fvalue%20%28count%28%2a%29%20as%20%3Fct%29%20%28SAMPLE%28%3Fitem%29%20as%20%3Fsampleitem%29%0A%20%20%20%20WHERE%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%3Fitem%20wdt%3A" ..
				id ..
				"%20%3Fvalue%0A%20%20%20%20%7D%0A%20%20%20%20GROUP%20BY%20%3Fvalue%0A%20%20%20%20ORDER%20BY%20DESC%28%3Fct%29%0A%20%20%20%20LIMIT%201000%0A%20%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%20%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fct%29%20ASC%28%3Fvalue%29"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		end

		if entity.datatype == 'time' then
			linkText = translate("by century")
			-- unresolved: FLOOR((0-1)/100)+1 = 0
			-- new problem: 20. century  => 2000-00-00 => 19th century
			linkURL =
				"https://query.wikidata.org/#%23Number%20of%20dates%20per%20century%0ASELECT%20%3Fcentury%20%28COUNT%28%3Fitem%29%20as%20%3Fcount%29%0AWHERE%0A%7B%0A%20%20%3Fitem%20wdt%3A" ..
				id ..
				"%20%3Fdate%20.%0A%20%20BIND%28FLOOR%28%28YEAR%28%3Fdate%29-1%29%2F100%29%2B1%20as%20%3Fcentury%29%0A%7D%0AGROUP%20BY%20%3Fcentury%0AORDER%20BY%20DESC%28%3Fcentury%29"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')

			-- access date (scope issue)
			if id ~= 'P813' then
				linkText = translate("date precisions")
				linkURL =
					"https://query.wikidata.org/#%23by%20Jura1%2C%202016-05-04%0ASELECT%20%3Fcount%20%3Fdatetime_precision_label%20%3Fdatetime_precision%0A{%0A%20%20{%0A%20%20%20%20SELECT%20%3Fdatetime_precision%20%28count%28DISTINCT%28%3Fnode%29%29%20as%20%3Fcount%29%0A%20%20%20%20{%0A%20%20%20%20%20%20%20%20%3Fnode%20psv%3A" ..
					id ..
					"%2Fwikibase%3AtimePrecision%20%3Fdatetime_precision%0A%20%20%20%20}%0A%20%20%20%20GROUP%20BY%20%3Fdatetime_precision%0A%20%20}%0A%20%20%3Fitem%20wdt%3AP2803%20%3Fvalue%0A%20%20FILTER%20%28%3Fdatetime_precision%20%3D%20%3Fvalue%29%0A%20%20SERVICE%20wikibase%3Alabel%20{%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
					lang ..
					"%2Cen%22%20.%20%3Fitem%20rdfs%3Alabel%20%3Fdatetime_precision_label%20%20}%0A}%0AORDER%20BY%20DESC%28%3Fcount%29%0ALIMIT%2010"
				table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
			end

			linkText = translate("calendars")
			linkURL =
				"https://query.wikidata.org/#SELECT%20%3Fcalendar%20%28count%28*%29%20as%20%3Fcount%29%0AWHERE%20{%0A%20%20%3Fdateproperty%20psv%3A" ..
				id ..
				"%2Fwikibase%3AtimeCalendarModel%20%3Fcalendar.%0A}%0AGROUP%20BY%20%3Fcalendar%0AORDER%20BY%20DESC%28%3Fcount%29"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		end

		if entity.datatype == 'external-id' or entity.datatype == 'string' or entity.datatype == 'monolingualtext' then
			linkText = translate("string lengths")
			linkURL =
				"https://query.wikidata.org/#%23String%20lenghts%20by%20frequency%20with%20sample%20value%0A%23by%20Jura1%2C%202016-01-24%2C%202017-07-28%0A%0ASELECT%20%3Fstringlength%20%28COUNT%28DISTINCT%28%3Fstringvalue%29%29%20AS%20%3Fcount%29%20%28SAMPLE%28%3Fstringvalue%29%20as%20%3Fsamplestringvalue%29%20%20%28SAMPLE%28%3Fitem%29%20as%20%3Fsampleitem%29%0A%28URI%28CONCAT%28%22https%3A%2F%2Fquery.wikidata.org%2F%23SELECT%2520%253Fitem%2520%253FitemLabel%2520%253FitemDescription%2520%253Fstringvalue%2520%253Fstringlength%250A%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22WHERE%2520%250A%257B%250A%2509%253Fitem%2520wdt%253A%22%2C%22" ..
				id ..
				"%22%2C%22%2520%253Fstringvalue%2520.%250A%2520%2520%2520%2520BIND%2528strlen%2528%253Fstringvalue%2529%2520as%2520%253F%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stringlength%2529%250A%2520%2520%2520%2520FILTER%2528%253Fstringlength%2520%253D%2520%22%2Cstr%28%3Fstringlength%29%2C%22%2529%250A%2520%2520%2520%2520SERVICE%2520wikibase%253Alabel%2520%257B%2520bd%253A%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22serviceParam%2520wikibase%253Alanguage%2520%2522%255BAUTO_LANGUAGE%255D%252Cen%2522.%2520%257D%250A%257D%250ALIMIT%2520100%250A%22%29%29%20as%20%3Flist%29%0AWHERE%0A%7B%0A%20%20%3Fitem%20wdt%3A" ..
				id ..
				"%20%3Fstringvalue%20.%0A%20%20%20%20BIND%28strlen%28%3Fstringvalue%29%20as%20%3Fstringlength%29%0A%7D%0AGROUP%20BY%20%3Fstringlength%0AORDER%20BY%20DESC%28%3Fcount%29%0ALIMIT%2010"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		end

		if entity.datatype == 'monolingualtext' then
			linkText = translate("languages of strings")
			linkURL =
				"https://query.wikidata.org/#%23by%20Jura1%2C%202016-01-24%0A%0ASELECT%20%3Flang%20%28COUNT%28DISTINCT%28%3Fmonolingualstring%29%29%20as%20%3Fcount%29%0AWHERE%0A{%0A%20%20%3Fitem%20wdt%3A" ..
				id ..
				"%20%3Fmonolingualstring%20.%0A%20%20BIND%28lang%28%3Fmonolingualstring%29%20as%20%3Flang%29%0A}%0AGROUP%20BY%20%3Flang%0AORDER%20BY%20DESC%28%3Fcount%29%0ALIMIT%2050"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		end

		if entity.datatype == 'quantity' then
			linkText = translate("units")
			linkURL = "https://query.wikidata.org/#%23Units%20used%20by%20property%20" ..
				id ..
				"%0A%23by%20Jura1%2C%202016-01-24%2C%20rev.%202020-10-05%2C%20%0A%0ASELECT%20DISTINCT%20%3Funit%20%3FunitLabel%20%3Fcount%20%3Fhas_SI_conversion%20%3Fhas_some_conversion%20%3Fallowed_unit%20%3Fsample%20%3FsampleLabel%0AWHERE%0A%7B%0A%20%20%7B%0A%20%20%20%20%20%20SELECT%20%3Funit%20%28count%28DISTINCT%28%3Fitem%29%29%20as%20%3Fcount%29%20%28SAMPLE%28%3Fitem%29%20as%20%3Fsamplest%29%0A%20%20%20%20WHERE%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%3Fitem%20psv%3A" ..
				id ..
				"%2Fwikibase%3AquantityUnit%20%3Funit%0A%20%20%20%20%7D%0A%20%20%20%20GROUP%20BY%20%3Funit%0A%20%20%7D%0A%20%20%20%20OPTIONAL%20%7B%20%3Funit%20wdt%3AP2370%20%5B%5D%20.%20BIND%28%22y%22%20as%20%3Fhas_SI_conversion%29%20%7D%0A%20%20%20%20OPTIONAL%20%7B%20%3Funit%20wdt%3AP2442%20%5B%5D%20.%20BIND%28%22y%22%20as%20%3Fhas_some_conversion%29%20%7D%0A%20%20%20%20%3Fsample%20p%3A" ..
				id ..
				"%20%3Fsamplest%20.%0A%20%20%20%20OPTIONAL%20%7B%20wd%3A" ..
				id ..
				"%20p%3AP2302%20%5B%20ps%3AP2302%20wd%3AQ21514353%20%3B%20pq%3AP2305%20%3Funit%20%5D%20.%20BIND%28%20%22y%22%20as%20%3Fallowed_unit%29%20%7D%0A%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fcount%29%20%3Funit%0ALIMIT%2010"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')

			linkText = translate("scale of values")
			linkURL =
				"https://query.wikidata.org/#%23by%20Jura1%2C%202015-01-25%0A%23scales%3A%20%3C1%3D%3C1%2C%201%3D1-9%2C%202%3D10-99%2C%203%3D100-999%2C%20etc.%0A%0ASELECT%20%3Fscale%20%28COUNT%28%3Fquantityvalue%29%20AS%20%3Fcount%29%0A{%0A%20%20%3Fitem%20wdt%3A" ..
				id ..
				"%20%3Fquantityvalue%20.%0A%20%20%20%20BIND%28strlen%28STR%28ROUND%28%3Fquantityvalue%29%29%29%20as%20%3Flength%29%0A%20%20%20%20BIND%28IF%28%3Fquantityvalue%3C1%2C%22%3C1%22%2C%3Flength%29%20%20%20as%20%3Fscale%29%0A}%0AGROUP%20BY%20%3Fscale%0AORDER%20BY%20%3Fscale%0ALIMIT%20100"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		end

		if entity.datatype == 'url' then
			linkText = translate("protocol")
			linkURL =
				"https://query.wikidata.org/#SELECT%20%3Fprotocol%20%28COUNT%28%3Fitem%29%20as%20%3Fcount%29%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20as%20%3Fcountdistinct%29%0AWHERE%0A{%0A%20%20%3Fitem%20wdt%3A" ..
				id ..
				"%20%3Fvalue%20.%0A%20%20%20%20BIND%28strbefore%28%20str%28%3Fvalue%29%2C%20%22%3A%22%29%20as%20%3Fprotocol%29%0A}%0AGROUP%20BY%20%3Fprotocol%0AORDER%20BY%20DESC%28%3Fcount%29%0ALIMIT%2010"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		end

		if entity.datatype == 'commonsMedia' then
			linkText = translate("file extensions")
			linkURL =
				"https://query.wikidata.org/#%23by%20Jura1%2C%202016-01-24%0A%0ASELECT%20%3Fextension%20%28COUNT%28DISTINCT%28%3Ffilename%29%29%20AS%20%3Fcount%29%20WHERE%0A{%0A%20%20%3Fitem%20wdt%3A" ..
				id ..
				"%20%3Ffilename%20.%0A%20%20%20%20BIND%28fn%3Alower-case%28REPLACE%28str%28%3Ffilename%29%2C%20%27%28.%2B%3F%29.%28\\u005B^.\\u005D*%24%29%27%2C%20%22%242%22%29%29%20AS%20%3Fextension%29%0A}%0AGROUP%20BY%20%3Fextension%0AORDER%20BY%20DESC%28%3Fcount%29%0ALIMIT%2010"
			table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		end

		linkText = translate("qualifiers")
		linkURL = getWdqsLink("Qualifiers for " .. id, [[
SELECT DISTINCT ?qual ?qualLabel ?count ?sample ?isAllowed ?isRequired {
  {
    SELECT ?qual (COUNT(DISTINCT ?item) AS ?count) (SAMPLE(?statement) AS ?sample) {
      hint:Query hint:optimizer "None" .
      ?item p:]] .. id .. [[ ?statement .
      ?statement ?pq_qual ?pq_obj .
      ?qual wikibase:qualifier ?pq_qual .
    } GROUP BY ?qual
  } .
  OPTIONAL { wd:]] .. id .. [[ p:P2302 [ pq:P2306 ?qual ; ps:P2302 wd:Q21510851 ] . BIND("y" as ?isAllowed) }
  OPTIONAL { wd:]] .. id .. [[ p:P2302 [ pq:P2306 ?qual ; ps:P2302 wd:Q21510856 ] . BIND("y" as ?isRequired ) }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,mul". }
}
ORDER BY DESC(?count) ASC(?qualLabel)]])

		-- avoid query timeout
		local total2 = tonumber(frame:expandTemplate { title = 'Property uses', args = { string.sub(entity.id, 2) } }) or
			0
		if total2 > 1000000 or total2 == 0 then
			linkURL = getWdqsLink("Count of qualifiers within a sample of statements (maybe the first 500,000 - 1 million)", [[
SELECT ?qual ?qualLabel ?countSample ?sample {
  {
    SELECT ?qual (COUNT(*) AS ?countSample) (SAMPLE(?st) AS ?sample) {
      { SELECT * { ?st ps:]] .. id .. [[ []; ?pq [] } LIMIT 4000000 } .
      ?qual wikibase:qualifier ?pq
    }
    GROUP BY ?qual
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,mul". }
} ORDER BY DESC(?countSample)]])
		end
		-- if not freqproperties[id] then
		table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
		-- end
	else
		--- for qualifiers

		linkText = translate("basic item query")
		linkURL =
			"https://query.wikidata.org/#PREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0APREFIX%20p%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2F%3E%0APREFIX%20ps%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fstatement%2F%3E%0APREFIX%20pq%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fqualifier%2F%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0A%23Sample%20query%20to%20start%20a%20list%20of%20statements%20using%20the%20qualifer%20" ..
			id ..
			"%0A%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Fproperty%20%3FpropertyLabel%20%3Fvalue%20%3FvalueLabel%20%3Fqualifier" ..
			id ..
			"value%0AWHERE%0A{%0A%20%20%20%20%3Fprop%20pq%3A" ..
			id ..
			"%20%3Fqualifier" ..
			id ..
			"value%20.%0A%20%20%20%20hint%3AQuery%20hint%3Aoptimizer%20%22None%22%20.%0A%20%20%3Fitem%20%3Fp%20%3Fprop%20.%0A%20%20%3Fproperty%20wikibase%3Aclaim%20%3Fp%20.%0A%20%20%20%20%3Fproperty%20wikibase%3AstatementProperty%20%3Fps%20.%0A%20%20%20%20%3Fprop%20%3Fps%20%3Fvalue%20.%0A%0A%20%20SERVICE%20wikibase%3Alabel%20{%20bd%3AserviceParam%20wikibase%3Alanguage%20%22" ..
			lang .. "%2Cen%22%20%20}%0A}%0ALIMIT%2020"
		table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')

		linkText = translate("properties using this qualifier")
		linkURL = "https://query.wikidata.org/#%23Number%20of%20items%2Fstatements%20using%20qualifer%20" ..
			id ..
			"%20for%20each%20property%0A%23added%202016-02-24%20by%20Jura1%0A%0ASELECT%20%3Fproperty%20%3FpropertyLabel%20%3Fitems%20%3Fstatements%0AWHERE%0A{%0A%20%20{%0A%20%20%20%20SELECT%20%3Fproperty%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20as%20%3Fitems%29%20%28COUNT%28%3Fvalue%29%20as%20%3Fstatements%29%0A%20%20%20%20WHERE%0A%20%20%20%20{%0A%20%20%20%20%20%20%20%20%3Fprop%20pq%3A" ..
			id ..
			"%20%3Fqualifier" ..
			id ..
			"value%20.%0A%20%20%20%20%20%20%20%20hint%3AQuery%20hint%3Aoptimizer%20%22None%22%20.%0A%20%20%20%20%20%20%3Fitem%20%3Fp%20%3Fprop%20.%0A%20%20%20%20%20%20%3Fproperty%20wikibase%3Aclaim%20%3Fp%20.%0A%20%20%20%20%20%20%20%20%3Fproperty%20wikibase%3AstatementProperty%20%3Fps%20.%0A%20%20%20%20%20%20%20%20%3Fprop%20%3Fps%20%3Fvalue%20.%0A%20%20%20%20}%0A%20%20%20%20GROUP%20BY%20%3Fproperty%0A%20%20%20%20ORDER%20BY%20DESC%28%3Fitems%29%20DESC%28%3Fstatements%29%0A%20%20%20%20LIMIT%2025%0A%20%20}%0A%20%20SERVICE%20wikibase%3Alabel%20{%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%2Cen%22%20%20}%0A}%0AORDER%20BY%20DESC%28%3Fitems%29%20DESC%28%3Fstatements%29%0A%0A%0A"
		table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')
	end

	linkText = translate("counts")

	if freqproperties[id] then
		linkURL = "https://query.wikidata.org/#%23%20Uses%20of%20" ..
			id ..
			"%3A%20%0A%23%20-%20as%20property%20%28any%20rank%2Fbest%20rank%29.%20For%20performance%20reasons%2C%20detailed%20counts%20by%20rank%20are%20not%20included.%0A%23%20-%20as%20qualifier%0A%23%20-%20in%20references%0A%23%20by%20Jura1%2C%20rev%202016-03-03%0ASELECT%20%3Fuse%20%3Frank%20%3Fitems_with_property%20%3Fuses_of_property%0AWHERE%20%7B%0A%20%20%7B%0A%20%20%20%20SELECT%20%28wikibase%3AstatementProperty%20as%20%3Fuse%29%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20as%20%3Fitems_with_property%29%20%28COUNT%28%3Fvalue%29%20as%20%3Fuses_of_property%29%0A%20%20%20%20WHERE%20%7B%3Fitem%20p%3A" ..
			id ..
			"%20%3Fvalue%7D%0A%20%20%7D%20UNION%20%7B%0A%20%20%20%20SELECT%20%28wikibase%3AstatementProperty%20as%20%3Fuse%29%20%28wikibase%3ABestRank%20as%20%3Frank%29%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20as%20%3Fitems_with_property%29%20%28COUNT%28%3Fbestvalue%29%20as%20%3Fuses_of_property%29%0A%20%20%20%20WHERE%20%7B%3Fitem%20wdt%3A" ..
			id ..
			"%20%3Fbestvalue%7D%0A%20%20%7D%20UNION%20%7B%0A%20%20%20%20SELECT%20%28wikibase%3Areference%20as%20%3Fuse%29%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20as%20%3Fitems_with_property%29%20%28COUNT%28%3Fvalue%29%20as%20%3Fuses_of_property%29%0A%20%20%20%20WHERE%20%7B%3Fitem%20pr%3A" ..
			id ..
			"%20%3Fvalue%7D%0A%20%20%7D%20UNION%20%7B%0A%20%20%20%20SELECT%20%28wikibase%3Aqualifier%20as%20%3Fuse%29%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20as%20%3Fitems_with_property%29%20%28COUNT%28%3Fvalue%29%20as%20%3Fuses_of_property%29%0A%20%20%20%20WHERE%20%7B%3Fitem%20pq%3A" ..
			id ..
			"%20%3Fvalue%7D%0A%20%20%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fuse%29%20DESC%28%3Fitems_with_property%29%20%3Frank%0A%0A%23%20PLEASE%20NOTE%3A%20Deleted%20items%20appear%20in%20counts%20for%20some%20ranks%2C%20but%20not%20in%20others."
	else
		linkURL = "https://query.wikidata.org/#%23%20Uses%20of%20" ..
			id ..
			"%3A%0A%23%20-%20as%20property%20%28any%20rank%2Fbest%20rank%2Fnormal%20rank%2Fpreferred%20rank%2Fdeprecated%20rank%29%0A%23%20-%20as%20qualifier%0A%23%20-%20in%20references%0A%23%20by%20Jura1%2C%20rev%202016-03-03%0ASELECT%20%3Fuse%20%3Frank%20%3Fitems_with_property%20%3Fuses_of_property%0AWHERE%20%7B%0A%20%20%7B%0A%20%20%20%20SELECT%20%28wikibase%3AstatementProperty%20AS%20%3Fuse%29%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20AS%20%3Fitems_with_property%29%20%28COUNT%28%3Fvalue%29%20AS%20%3Fuses_of_property%29%0A%20%20%20%20WHERE%20%7B%3Fitem%20p%3A" ..
			id ..
			"%20%3Fvalue%7D%0A%20%20%7D%20UNION%20%7B%0A%20%20%20%20SELECT%20%28wikibase%3AstatementProperty%20AS%20%3Fuse%29%20%28wikibase%3ABestRank%20AS%20%3Frank%29%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20AS%20%3Fitems_with_property%29%20%28COUNT%28%3Fbestvalue%29%20AS%20%3Fuses_of_property%29%0A%20%20%20%20WHERE%20%7B%3Fitem%20wdt%3A" ..
			id ..
			"%20%3Fbestvalue%7D%0A%20%20%7D%20UNION%20%7B%0A%20%20%20%20SELECT%20%28wikibase%3AstatementProperty%20AS%20%3Fuse%29%20%3Frank%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20AS%20%3Fitems_with_property%29%20%28COUNT%28%28%3Fstatement%29%29%20AS%20%3Fuses_of_property%29%0A%20%20%20%20WHERE%20%7B%3Fitem%20p%3A" ..
			id ..
			"%20%3Fstatement%20.%20%3Fstatement%20wikibase%3Arank%20%3Frank%7D%20GROUP%20BY%20%3Frank%0A%20%20%7D%20UNION%20%7B%0A%20%20%20%20SELECT%20%28wikibase%3Areference%20AS%20%3Fuse%29%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20AS%20%3Fitems_with_property%29%20%28COUNT%28%3Fvalue%29%20AS%20%3Fuses_of_property%29%0A%20%20%20%20WHERE%20%7B%3Fitem%20pr%3A" ..
			id ..
			"%20%3Fvalue%7D%0A%20%20%7D%20UNION%20%7B%0A%20%20%20%20SELECT%20%28wikibase%3Aqualifier%20AS%20%3Fuse%29%20%28COUNT%28DISTINCT%28%3Fitem%29%29%20AS%20%3Fitems_with_property%29%20%28COUNT%28%3Fvalue%29%20AS%20%3Fuses_of_property%29%0A%20%20%20%20WHERE%20%7B%3Fitem%20pq%3A" ..
			id ..
			"%20%3Fvalue%7D%0A%20%20%7D%0A%7D%0AORDER%20BY%20DESC%28%3Fuse%29%20DESC%28%3Fitems_with_property%29%20%3Frank%0A%0A%23PLEASE%20NOTE%3A%20Deleted%20items%20appear%20in%20counts%20for%20some%20ranks%2C%20but%20not%20in%20others."
	end
	table.insert(queries, '[' .. linkURL .. ' ' .. linkText .. ']')

	return queries
end

function p.before(args)
	local deprecated_params = { "status", "datatype", "topic", "subpage", "proposed by" }
	local used_deprecated_params = {}
	for _, v in ipairs(deprecated_params) do
		if args[v] ~= nil then
			table.insert(used_deprecated_params, v)
		end
	end
	if #used_deprecated_params > 0 then
		local result = '<span style="font-size: 200%; color: red;">Please remove the following deprecated parameters: '
		result = result .. table.concat(used_deprecated_params, ', ') .. '.</span>'
		return result
	end
	return ''
end

function p.header(args)
	-- Top-left: Label <br/> Description
	local label = d._getLabel(entity, p.getLang()) or ''
	local description = d._getDescription(entity, p.getLang()) or ''
	return '<b>' .. label .. '</b><br />' .. description
end

function p.makeRow(rowparams, data)
	-- get value from template
	local templateval
	local templatequery = rowparams.value
	if templatequery and type(templatequery) == 'function' then
		templateval = templatequery(data)
	else
		templateval = data[rowparams.label] -- by default parameter name = label name
	end

	-- get value from item properties
	local itemval, itemquery, _
	local lang = p.getLang()
	if entity then
		itemquery = rowparams.query
		if type(itemquery) == 'string' then
			itemval = d.formatStatements { entity = entity, property = itemquery, lang = lang, displayformat = 'wikidatastyle', conjtype = 'comma' }
		elseif type(itemquery) == 'function' then
			_, itemval = pcall(itemquery) -- prevent errors from propagation
		end
	end

	onMedia = #(d.getClaims { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q52004125', qualifier = 'P2305', qualifiervalue = 'Q59712033' } or {})
	onItems = #(d.getClaims { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q52004125', qualifier = 'P2305', qualifiervalue = 'Q29934200' } or {})
	onProperties = #(d.getClaims { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q52004125', qualifier = 'P2305', qualifiervalue = 'Q29934218' } or {})
	onLexemes = #(d.getClaims { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q52004125', qualifier = 'P2305', qualifiervalue = 'Q51885771' } or {})
	onForms = #(d.getClaims { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q52004125', qualifier = 'P2305', qualifiervalue = 'Q54285143' } or {})
	onSenses = #(d.getClaims { displayformat = 'raw', entity = entity, property = 'P2302', targetvalue = 'Q52004125', qualifier = 'P2305', qualifiervalue = 'Q54285715' } or {})

	local val = templateval or itemval
	if templateval then
		if itemval then
			-- Warn that info in the template parameter duplicates info from some property statement
			val = '<div>' .. translate('local data') .. '\n' .. templateval
				.. '</div>\n<div>' .. translate('property data') .. '<br />' .. itemval
				.. '</div>\n<b>' .. translate('notice') .. '</b>'
			local dupcat = 'Property with duplicated information'
			if rowparams.duplicatecat then
				if type(rowparams.duplicatecat) == 'function' then
					dupcat = rowparams.duplicatecat()
				else
					dupcat = rowparams.duplicatecat
				end
			end
			addcat(dupcat)
		elseif itemquery then
			-- Warn that template parameter needs to be ported to some statement of the property
			-- Look for a property to use as a destination for the info currently held by the template parameter
			local toprop
			if rowparams.converttoprop then
				if type(rowparams.converttoprop) == 'function' then
					toprop = rowparams.converttoprop(data)
				else
					toprop = rowparams.converttoprop
				end
			end
			if not toprop and type(itemquery) == 'string' then
				toprop = itemquery
			end
			if toprop then
				local topropstr
				if type(toprop) == 'table' then
					local toproptable = {}
					for i, j in ipairs(toprop) do
						toproptable[i] = d.showentity(j, lang)
					end
					topropstr = linguistic.conj(toproptable, lang, 'or')
				else
					topropstr = d.showentity(toprop, lang)
				end
				val = val .. '\n' .. mw.ustring.format(translate('to move with prop'), topropstr)
			else
				val = val .. '\n' .. translate('to move')
			end
			local convcat = 'Property documentation with data to be moved to statements'
			if rowparams.conversioncat then
				if type(rowparams.conversioncat) == 'function' then
					convcat = rowparams.conversioncat()
				else
					convcat = rowparams.conversioncat
				end
			end
			addcat(convcat)
		end
	end

	if not val or val == '-' then
		if not rowparams.required then
			return nil
		end
		val = '<span style="color: red;">' .. translate('missing') .. '</span>' -- maintenance for missing parameters
		addcat(rowparams.missingcat or 'Property documentation with missing information')
	end

	if rowparams.maintenancecat then
		addcat(rowparams.maintenancecat(data))
	end

	if string.find(val, '^[*#:;]') then
		val = '\n' .. val
	end

	return val
end

return p