--require "Module:No globals"local p = {}local lib = require 'Module:Wikidata/lib'local function in_array(value, array)for _, val in ipairs(array) doif val == value thenreturn trueendendreturn falseendlocal function checkLimit(array, limit)local limit = limit and tonumber(limit)if limit thenreturn #array >= limitendreturn trueendlocal function applyLimit(array, limit)local limit = limit and tonumber(limit)while limit and #array > limit dotable.remove(array)endendlocal function IsInLanguage(snak, langs)local langs = lib.textToTable(langs)if snak.datatype ~= 'monolingualtext' thenreturn error(lib.raiseInvalidDatatype('IsInLanguage', snak.datatype, 'monolingualtext'))elsereturn lib.IsSnakValue(snak) and in_array(snak.datavalue.value.language, langs)endendlocal function filterSnaktype(snak, somevalue, novalue)local snaktype = snak.snaktypeif snaktype == 'somevalue' thenreturn somevalue or falseelseif snaktype == 'novalue' thenreturn novalue or falseendreturn lib.IsSnakValue(snak)endlocal function filterBySnaktype(statement, somevalue, novalue)return filterSnaktype(statement.mainsnak, somevalue, novalue)endlocal function hasSnakTarget(snak, target)local Formatters = require 'Module:Wikidata/Formatters'return tostring(Formatters.getRawValue(snak)) == targetendlocal function filterHasTarget(statement, target)return hasSnakTarget(statement.mainsnak, target)endlocal function filterHasQualifier(statement, prop, value)if statement.qualifiers thenprop = prop:upper()for _, snak in ipairs(statement.qualifiers[prop] or {}) doif not value or hasSnakTarget(snak, value) thenreturn trueendendendreturn falseendlocal function filterHasRanks(statement, ranks)return in_array(statement.rank, ranks)endlocal function filterHasReferences(statement, options)if statement.references thenif #p.filterReferences(statement.references, options) > 0 thenreturn trueendendreturn falseendlocal function filterSnakHasLabel(snak)local datatype = snak.datatypeif datatype ~= 'wikibase-item' and datatype ~= 'wikibase-property' thenreturn error(lib.raiseInvalidDatatype('filterSnakHasLabel', datatype, { 'wikibase-item', 'wikibase-property' }))endif lib.IsSnakValue(snak) thenlocal langs = { 'cs', 'el', 'en' }local Formatters = require 'Module:Wikidata/Formatters'if lib.getLabelInLanguage(Formatters.getRawValue(snak), langs) thenreturn trueendendreturn falseendlocal function filterHasLabel(statement)return filterSnakHasLabel(statement.mainsnak)endlocal function filterItemIsInstance(statement, options)local datatype = statement.mainsnak.datatypeif datatype ~= 'wikibase-item' and datatype ~= 'wikibase-property' thenreturn error(lib.raiseInvalidDatatype('filterItemIsInstance', datatype, { 'wikibase-item', 'wikibase-property' }))endif lib.IsSnakValue(statement.mainsnak) thenlocal Module = require 'Module:Wikidata/Tree'local Formatters = require 'Module:Wikidata/Formatters'local item = Formatters.getRawValue(statement.mainsnak)if Module.IsInstance(item, options) thenreturn trueendendreturn falseendlocal function filterMainsnakInLanguage(statement, withlang)return IsInLanguage(statement.mainsnak, withlang)endlocal function filterMainsnakHasUnit(statement, unit)local datatype = statement.mainsnak.datatypeif datatype ~= 'quantity' thenreturn error(lib.raiseInvalidDatatype('filterItemIsInstance', datatype, { 'quantity' }))endreturn lib.IsSnakValue(statement.mainsnak) and lib.getItemIdFromURI(statement.mainsnak.datavalue.value.unit) == unitendlocal function filter(array, callback, ...)local i = #arraywhile i > 0 doif not callback(array[i], ...) thentable.remove(array, i)endi = i - 1endendlocal function getValuesFromQualifiers(qualifiers)local Values = {}local Formatters = require 'Module:Wikidata/Formatters'for key, array in pairs(lib.props) dofor _, prop in ipairs(array) dofor _, snak in ipairs(qualifiers[prop] or {}) doif lib.IsSnakValue(snak) thenValues[key] = Formatters.getRawValue(snak)breakendendendendreturn Valuesendfunction p.filterStatementsFromEntity(entity, options)if not options.property or options.property == '' thenreturn error(lib.formatError('param-not-provided', 'property'))endif not entity or not entity.claims thenreturn {}endlocal property = mw.ustring.upper(options.property)local statements = mw.clone(entity.claims[property])if not statements thenreturn {}endp.filterStatements(statements, options)return statementsendfunction p.filterStatements(statements, options)local options = lib.common.cleanArgs(options)-- apply filter by ranklocal rank = options.rank or "valid"if rank ~= "all" thenif rank == "valid" or rank == "best" thenfilter(statements, filterHasRanks, { "normal", "preferred" })if rank == "best" and #statements > 0 thenfor _, statement in ipairs(statements) doif statement.rank == "preferred" thenfilter(statements, filterHasRanks, { "preferred" })breakendendendelsefilter(statements, filterHasRanks, { rank })endif #statements == 0 then return endend-- apply filter by sourceif options.ref thenfilter(statements, filterHasReferences, options)if #statements == 0 then return endend-- apply filter by snak typefilter(statements, filterBySnaktype, options.somevalue and true, options.novalue and true)if #statements == 0 then return end-- apply filter by target valueif options.withtarget thenfilter(statements, filterHasTarget, options.withtarget)if #statements == 0 then return endend-- apply filter by qualifier propertyif options.withqualifier thenfilter(statements, filterHasQualifier, options.withqualifier, options.withqualifiervalue)if #statements == 0 then return endend-- apply filter by languageif options.withlang thenfilter(statements, filterMainsnakInLanguage, options.withlang)if #statements == 0 then return endend-- apply filter by unitif options.withunit thenfilter(statements, filterMainsnakHasUnit, options.withunit)if #statements == 0 then return endend-- apply filter by timeif options.date thenlocal datelocal Time = require 'Module:Time'if type(options.date) == 'table' thendate = options.dateelseif options.date == '#now' thendate = Time.new(os.date('!*t'))elsedate = Time.newFromIso8601(options.date)endif not date thenreturn error(lib.formatError('invalid-date', tostring(options.date)))endlocal oldStatements = mw.clone(statements)while #statements > 0 do table.remove(statements) endfor _, statement in ipairs(oldStatements) dolocal Values = getValuesFromQualifiers(statement.qualifiers or {})if Values.point thenif date == Values.point thenfilter(statements, function(st)local val = getValuesFromQualifiers(st.qualifiers).pointif val thenreturn val == Values.pointendreturn trueend)table.insert(statements, statement)elseif Values.point < date thenif #statements == 0 thentable.insert(statements, statement)elselocal same, insfor _, st in ipairs(statements) dolocal val = getValuesFromQualifiers(st.qualifiers).pointif val thenif date == Values.point thensame = truebreakendif val == Values.point or val < Values.point thenins = trueendendendif ins and not same thenfilter(statements, function(st)local val = getValuesFromQualifiers(st.qualifiers).pointreturn not val or val == Values.pointend)table.insert(statements, statement)endendendelseif Values.begin thenif Values.begin < date thenif not Values.ending thentable.insert(statements, statement)elseif date < Values.ending thentable.insert(statements, statement)endendelseif Values.ending thenif date < Values.ending thenif not Values.begin thentable.insert(statements, statement)elseif Values.begin < date thentable.insert(statements, statement)endendendendendif #statements == 0 then return endendif lib.IsOptionTrue(options, 'withlabel') thenfilter(statements, filterHasLabel)if #statements == 0 then return endend-- apply filter by classif options.instance thenfilter(statements, filterItemIsInstance, options)if #statements == 0 then return endend-- sort statements if neededif options.sort then -- patří to sem?local Sorters = require 'Module:Wikidata/Sorters'Sorters.sortStatements(statements, options)end-- apply filter by limitapplyLimit(statements, options.limit)endfunction p.filterQualifiers(qualifiers, options)local options = lib.common.cleanArgs(options)filter(qualifiers, filterSnaktype, options['qualifiers somevalue'] and true, options['qualifiers novalue'] and true)if #qualifiers == 0 then return endif options['qualifiers withlang'] thenfilter(qualifiers, IsInLanguage, options['qualifiers withlang'])if #qualifiers == 0 then return endendif lib.IsOptionTrue(options, 'qualifiers withlabel') thenfilter(qualifiers, filterSnakHasLabel)if #qualifiers == 0 then return endendif options['qualifiers class'] then--[[ todolocal datatype = Qualifiers[math.random(#Qualifiers)].datatypeif datatype == 'wikibase-item' or datatype == 'wikibase-property' thenoldQualifiers, Qualifiers = Qualifiers, {}local Module = require 'Module:Wikidata/Tree'local Formatters = require 'Module:Wikidata/Formatters'for _, snak in ipairs(oldQualifiers) doif lib.IsSnakValue(snak) thenlocal item = Formatters.getRawValue(snak)if Module.IsInTree(item, options['qualifiers class'], 'P279', 20, {}) thentable.insert(Qualifiers, snak)endendendif #Qualifiers == 0 then return {} endelsereturn error(lib.raiseInvalidDatatype('inClass', datatype, { 'wikibase-item', 'wikibase-property' }))end]]--endif options['qualifiers sort'] thenlocal Sorters = require 'Module:Wikidata/Sorters'Sorters.sortQualifiers(qualifiers, options)endapplyLimit(qualifiers, options['qualifiers limit'])endfunction p.filterReferences(references, options)local options = lib.common.cleanArgs(options)if options.ref == '#any' then-- @deprecatedreturn referencesendlocal oldReferences, References = references, {}if options.ref == 'valid' thenlocal map = (require 'Module:Wikidata/cite').propsfor _, ref in ipairs(oldReferences) dofor _, props in pairs(map) dofor _, prop in ipairs(props) doif ref.snaks[prop] thentable.insert(References, ref)endendendendendif options.min_ref and not checkLimit(References, options.min_ref) thenreturn {}end-- @deprecatedreturn Referencesendreturn p