وحدة:Wikidata2

local p = {}local Frame_args = {}local Moduletextlocal Modulecitelocal Moduleflagslocal InfoboxImagelocal ModuleGlobeslocal formateralocal weblinklocal citetitle = "Module:Cite Q"if nil ~= string.find(mw.getCurrentFrame():getTitle(), "ملعب", 1, true) thencitetitle = "Module:Cite Q/ملعب"endlocal i18n = {["errors"] = {["property-param-not-provided"] = "وسيط property غير متوفر.",["entity-not-found"] = "الكيان غير موجود.",["unknown-claim-type"] = "نوع claim غير معروف.",["unknown-snak-type"] = "نوع snak غير معروف.",["unknown-datatype"] = "نوع data غير معروف.",["unknown-entity-type"] = "نوع entity غير معروف.",["unknown-value-module"] = "يجب عليك تعيين كل من  value-module و value-function.",["unknown-claim-module"] = "يجب عليك تعيين كل من claim-module و claim-function.",["unknown-property-module"] = "يجب عليك تعيين كل من property-module و property-function.",["property-module-not-found"] = "الوحدة المستخدمة في وسيط property-module غير موجودة.",["property-function-not-found"] = "الوظيفة المستخدمة في وسيط property-function غير موجودة.",["value-module-not-found"] = "الوحدة المستخدمة في وسيط value-module غير موجودة.",["value-function-not-found"] = "الوظيفة المستخدمة في وسيط value-function غير موجودة.",["claim-module-not-found"] = "الوحدة المستخدمة في وسيط claim-module غير موجودة.",["claim-function-not-found"] = "الوظيفة المستخدمة في وسيط claim-function غير موجودة."},["noarabiclabel"] = "تصنيف:صفحات_ويكي_بيانات_بحاجة_لتسمية_عربية",["warnDump"] = "[[Category:Called function 'Dump' from module Wikidata]]",["somevalue"] = "", --'"غير محدد"'["novalue"] = "", --قيمة مجهولة["cateref"] = "[[" .. "تصنيف:صفحات بها مراجع ويكي بيانات" .. "]]",["to translate"] = "صفحات تستعمل معطيات من ويكي بيانات بحاجة لترجمة",["trackingcat"] = "صفحات تستخدم خاصية $1",["see-wikidata-value"] = "الاطلاع ومراجعة البيانات على ويكي داتا",["see-wikidata"] = "راجع العنصر من ويكي بيانات المقابل",["see-another-project"] = "مقالة على $1",["see-another-language"] = "مقالة على ويكيبيديا $1"}local sortingproperties = {"P585", "P571", "P580", "P569", "P582", "P570"}local sorting_methods = {["chronological"] = "chronological",["تصاعدي"] = "chronological",["asc"] = "chronological",["inverted"] = "inverted",["تنازلي"] = "inverted",["desc"] = "inverted"}local falsetitles = {"قالب:قيمة ويكي بيانات","وحدة:Wikidata2"}local skiip_items = {["P106"] = {"Q42857", -- prophet"Q14886050", -- terrorist"Q2159907" -- criminal}}function isvalid(x)if x and x ~= "" then return x endreturn nilendfunction isntvalid(x)if not x or x == "" or x == nil then return true endreturn falseendfunction No_Tracking_cat(options)if isvalid(options.formatting) == "raw" or isvalid(options.formatting) == "sitelink" thenreturn trueendlocal notracking = isvalid(options.nocate) or isvalid(options.notracking) or isvalid(Frame_args.notracking)local raw = isvalid(options.raw) or isvalid(Frame_args.raw) or isvalid(options.raw2) or isvalid(Frame_args.raw2)local nolink = isvalid(options.nolink) or isvalid(Frame_args.nolink)if notracking or raw or nolink thenreturn trueendlocal pagetitle = mw.title.getCurrentTitle().textfor _, title in pairs(falsetitles) doif string.find(pagetitle, title, 1, true) thenmw.log("notracking for title with: " .. title)return trueendendreturn falseendfunction addTrackingCategory(options)if No_Tracking_cat(options) thenreturn ""endlocal cat = require("Module:Wikidata/تتبع").makecategory1local category = cat(options)local nbsp = "&nbsp;"if isvalid(options.nbsp) thennbsp = ""endif isvalid(category) thenreturn nbsp .. categoryendreturn ""endfunction catewikidatainfo(options)--[[  function to add tracking category ]]if No_Tracking_cat(options) thenreturn ""endlocal cat = ""local prop = options.propertycat = cat .. " [[" .. "تصنيف:صفحات بها بيانات ويكي بيانات|" .. (prop or "wikidata") .. "]]"if isntvalid(options.nolink) thenreturn catelsereturn ""endendfunction getqualifierbysortingproperty(claim, sortingproperty)for k, v in pairs(sortingproperty) doif claim and claim.qualifiers and claim.qualifiers[v] and claim.qualifiers[v][1].snaktype == "value" thenlocal vali = claim.qualifiers[v][1].datavalue.value.time or claim.qualifiers[v][1].datavalue.value.amountif vali:sub(1, 1) == "+" thenvali = vali:sub(2)end--mw.log(vali)return valiendendreturn nilendfunction get_sorting_properties(options)if type(options.sortingproperty) == "table" thenreturn options.sortingpropertyelseif type(options.sortingproperty) == "string" and options.sortingproperty ~= "" thenreturn mw.text.split(options.sortingproperty, ",")elsereturn sortingpropertiesendendfunction getDate(claim, options)local sortingproperty = get_sorting_properties(options)return getqualifierbysortingproperty(claim, sortingproperty)endfunction get_entityId(options)local id = isvalid(options.entityId) or isvalid(options.id) or isvalid(options.qid)if id thenreturn idendif isvalid(options.page) thenid = mw.wikibase.getEntityIdForTitle(options.page)elseid = mw.wikibase.getEntityIdForCurrentPage()endreturn id or ""endfunction countSiteLinks(id)local numb = 0local entity = mw.wikibase.getEntityObject(id)if entity and entity.sitelinks thenfor i, v in pairs(entity.sitelinks) donumb = numb + 1endendreturn numbendfunction make_format_num(String)local line = Stringline = mw.getCurrentFrame():preprocess("{{ {{{|safesubst:}}}formatnum: " .. String .. " }}")line = mw.ustring.gsub(line, "٫", ".")line = mw.ustring.gsub(line, "٬", ",")return lineendfunction formatcharacters(label, options)local formatch = options.formatcharacters--if options.FormatfirstCharacter and options.num == 1 then--formatch = options.FormatfirstCharacter--endlocal String2 = mw.ustring.gsub(label, "–", "-")local march_y =mw.ustring.match(String2, "%d%d%d%d%-%d%d%d%d", 1) or mw.ustring.match(String2, "%d%d%-%d%d%d%d", 1) ormw.ustring.match(String2, "%d%d%d%d", 1) ormw.ustring.match(String2, "%d%d%d%d%-%d%d", 1) ormw.ustring.match(String2, "%d%d%d%d", 1)if isvalid(options.illwd2y) thenreturn march_y or labelendif isvalid(options.illwd2noy) and march_y thenlabel = mw.ustring.gsub(label, march_y, "")return labelendif isntvalid(formatch) thenreturn labelendlocal prepr = {["lcfirst"] = "{{lcfirst: " .. label .. " }}",["lc"] = "{{lc: " .. label .. " }}",["uc"] = "{{uc: " .. label .. " }}"}if prepr[formatch] thenreturn mw.getCurrentFrame():preprocess(prepr[formatch])elseif formatch == "ucfirst" thenreturn mw.language.getContentLanguage():ucfirst(label)elseif formatch == "formatnum" thenreturn make_format_num(label)endreturn labelendfunction descriptionIn(langcode, id) -- returns item description for a given languageif isntvalid(langcode) thenlangcode = "ar"endlangcode = mw.text.trim(langcode or "")id = mw.text.trim(id or "")if langcode == "ar" thenlocal description, lange = mw.wikibase.getDescriptionWithLang(id)if lange == langcode thenreturn descriptionelsereturn nilendelselocal entity = getEntityFromId(id)if entity and entity.descriptions thenlocal description = entity.descriptions[langcode]if description and description.value thenif description["language"] == langcode thenreturn description.valueendendendendreturn nilendfunction labelIn(langcode, id) -- returns item label for a given languageif isntvalid(langcode) thenlangcode = "ar"endif type(id) ~= "string" thenid = tostring(id)endlangcode = mw.text.trim(langcode or "")id = mw.text.trim(id or "")local label = mw.wikibase.getLabelByLang(id, langcode) or nilreturn labelendfunction get_snak_id(snak)ifsnak and snak.type and snak.type == "statement" and snak.mainsnak and snak.mainsnak.snaktype andsnak.mainsnak.snaktype == "value" andsnak.mainsnak.datavalue andsnak.mainsnak.datavalue.type andsnak.mainsnak.datavalue.type == "wikibase-entityid" andsnak.mainsnak.datavalue.value andsnak.mainsnak.datavalue.value.id thenID = snak.mainsnak.datavalue.value.idreturn IDendendfunction comparedates(a, b) -- returns true if a is earlier than B or if a has a date but not bif a and b thenreturn a > belseif a thenreturn trueendendfunction getDateArb(claim, sorting_properties)local sortingproperty = sorting_propertiesif claim.mainsnak.snaktype == "value" thenlocal item = claim.mainsnak.datavalue.value["numeric-id"]if claim.mainsnak.datavalue.value["entity-type"] == "item" thenitem = "Q" .. itemelseif claim.mainsnak.datavalue.value["entity-type"] == "property" thenitem = "P" .. itemendfor k, prop in pairs(sortingproperty) dolocal date =formatStatements({property = prop, entityId = item, firstvalue = "t", noref = "t", modifytime = "q"})if isvalid(date) then--mw.log("item:".. item .. ", prop:".. prop .. ", date:".. date)return dateendendendendfunction table_contains(table, element)for _, value in pairs(table) doif value == element thenreturn trueendendreturn falseendfunction dontget(claims, options)--[[options.dontgetoptions.dontgetproperty]]local claims2 = {}local dontget_values = mw.text.split(options.dontget, ",")for _, claim in pairs(claims) dolocal id = get_snak_id(claim)if id thenlocal valid = truelocal t2 =formatStatements({property = (options.dontgetproperty or "P31"), entityId = id, noref = "t", raw = "t"})if t2 and #t2 > 0 thenfor _, state in pairs(t2) doif table_contains(dontget_values, state.item) thenvalid = falsebreakendendendif valid thentable.insert(claims2, claim)endendendreturn claims2endfunction getonly(claims, options)--[[-- options.getonly-- options.getonlyproperty]]local claims2 = {}local getonly_values = mw.text.split(options.getonly, ",")for _, claim in pairs(claims) dolocal id = get_snak_id(claim)if id thenlocal t2 =formatStatements({property = (options.getonlyproperty or "P31"), entityId = id, noref = "t", raw = "t"})if t2 and #t2 > 0 thenfor _, state in pairs(t2) doif table_contains(getonly_values, state.item) thentable.insert(claims2, claim)breakendendendendendreturn claims2endfunction claimindex(claims, options)local claims2 = {}for j, index in pairs(mw.text.split(options.claimindex, ",")) doif tonumber(index) and #claims >= tonumber(index) thentable.insert(claims2, claims[tonumber(index)])endendreturn claims2endfunction avoidvalue(claims, options)local avoidvalue = options.avoidvalueif type(avoidvalue) == "string" thenavoidvalue = mw.text.split(avoidvalue, ",")elseif type(avoidvalue) ~= "table" thenreturn claimsendlocal claims4 = {}for i, j in pairs(claims) dolocal ID = get_snak_id(j)if ID and not table_contains(avoidvalue, ID) thentable.insert(claims4, j)endendreturn claims4endfunction prefervalue(claims, options)local prefervalue = options.prefervalueif type(prefervalue) == "string" thenprefervalue = mw.text.split(prefervalue, ",")elseif type(prefervalue) ~= "table" thenreturn claimsendlocal claims3 = {}for _, claim in pairs(claims) dolocal ID = get_snak_id(claim)if ID and table_contains(prefervalue, ID) thentable.insert(claims3, claim)endendreturn claims3endfunction preferqualifier(claims, options)--[[-- options.preferqualifier-- options.preferqualifiervalue]]local claims2 = {}local preferq_values = mw.text.split(options.preferqualifiervalue or "", ",")for _, statement in pairs(claims) doif statement.qualifiers and statement.qualifiers[options.preferqualifier:upper()] thenif isvalid(options.preferqualifiervalue) thenfor _, quall in pairs(statement.qualifiers[options.preferqualifier:upper()]) doif quall.snaktype == "value" and table_contains(preferq_values, quall.datavalue.value["id"]) thentable.insert(claims2, statement)breakendendelsetable.insert(claims2, statement)endendendreturn claims2endfunction avoidqualifier(claims, options)-- options.avoidqualifier-- options.avoidqualifiervalueif isntvalid(options.avoidqualifier) thenreturn claimsendlocal av = options.avoidqualifier:upper()local avoidqualifiervalue_values =type(options.avoidqualifiervalue) == "string" and mw.text.split(options.avoidqualifiervalue, ",") oroptions.avoidqualifiervaluelocal claims2 = {}for _, statement in pairs(claims) doif not statement.qualifiers or not statement.qualifiers[av] thentable.insert(claims2, statement)elseif isvalid(options.avoidqualifiervalue) thenlocal active = truefor _, quall in pairs(statement.qualifiers[av]) doifquall.snaktype == "value" and quall.datavalue and quall.datavalue.value andquall.datavalue.value["id"] andtable_contains(avoidqualifiervalue_values, quall.datavalue.value["id"]) thenactive = falsebreakendendif active thentable.insert(claims2, statement)endendendreturn claims2endfunction claims_limit(claims, limit)local newclaims = {}if type(limit) ~= "number" thenlimit = tonumber(limit)endif limit and type(limit) == "number" thenif limit < 1 or #claims < limit thenreturn claimsendfor i = 1, #claims doif i <= limit thennewclaims[i] = claims[i]endendreturn newclaimsendreturn claimsendfunction claims_offset(claims, offset)local offsetclaims = {}if type(offset) ~= "number" thenoffset = tonumber(offset)endif offset and type(offset) == "number" thenfor i = offset + 1, #claims dooffsetclaims[#offsetclaims + 1] = claims[i]endreturn offsetclaimsendreturn claimsendfunction sortbyarb(claims, sorting_properties, options)local sortingmethod = options.sortbyarbitrary or options.sortingmethod--mw.log("sortbyarb: " .. sortingmethod)table.sort(claims,function(a, b)local timeA = getDateArb(a, sorting_properties)local timeB = getDateArb(b, sorting_properties)if sortingmethod == "inverted" or sorting_methods[sortingmethod] == "inverted" thenreturn comparedates(timeB, timeA)elsereturn comparedates(timeA, timeB)endend)return claimsendfunction sort_langs(claims, options)local claims7 = {}local arabic_id = {["P407"] = 13955, ["P282"] = 8196} -- Q13955 = "العربية", Q8196 = "أبجدية عربية"for _, statement in pairs(claims) dofor prop, id in pairs(arabic_id) doif statement.qualifiers and statement.qualifiers[prop] thenfor _, v in pairs(statement.qualifiers[prop]) doif v.snaktype == "value" and v.datavalue.value["numeric-id"] == id thentable.insert(claims7, statement)breakendendendendendif #claims7 > 0 thenclaims = claims7endreturn claimsendfunction sortbyqualifier(claims, sorting_properties, options)if not sorting_properties or #sorting_properties == 0 thensorting_properties = get_sorting_properties(options)endlocal sort_by = sorting_methods[options.sortbytime] or options.sortbytimetable.sort(claims,function(a, b)local timeA = getqualifierbysortingproperty(a, sorting_properties)local timeB = getqualifierbysortingproperty(b, sorting_properties)if sort_by == "inverted" thenreturn comparedates(timeB, timeA)elsereturn comparedates(timeA, timeB)endend)return claimsendfunction sortbyqualifiernumber(claims, sorting_properties, options)if not sorting_properties or #sorting_properties == 0 thensorting_properties = get_sorting_properties(options)endlocal sort_by = sorting_methods[options.sortbynumber] or options.sortbynumbertable.sort(claims,function(a, b)local timeA = getqualifierbysortingproperty(a, sorting_properties)local timeB = getqualifierbysortingproperty(b, sorting_properties)if sort_by == "inverted" thenreturn comparedates(timeB, timeA)elsereturn comparedates(timeA, timeB)endend)return claimsendfunction sort_claims(claims, options)local sortingmethod = options.sortbyarbitrary or options.sortingmethodlocal sorting_properties = get_sorting_properties(options)if isvalid(options.sortbytime) and sorting_methods[options.sortbytime] thenif #sorting_properties == 0 thensorting_properties = sortingpropertiesendclaims = sortbyqualifier(claims, sorting_properties, options)elseif isvalid(options.sortbynumber) and sorting_methods[options.sortbynumber] thenclaims = sortbyqualifiernumber(claims, sorting_properties, options)elseif isvalid(sortingmethod) and sorting_methods[sortingmethod] thenclaims = sortbyarb(claims, sorting_properties, options)endreturn claimsendfunction filter_claims(claims, options)local claims = claimsif isntvalid(options.langpref) thenclaims = sort_langs(claims, options)endclaims = sort_claims(claims, options)local offset = options.offsetif isvalid(offset) thenclaims = claims_offset(claims, offset)endlocal limit = options.limitif isvalid(limit) thenclaims = claims_limit(claims, limit)endif isvalid(options.avoidqualifier) then -- to avoid value with a given qualifierclaims = avoidqualifier(claims, options)endif isvalid(options.preferqualifier) thenclaims = preferqualifier(claims, options)end-- options.avoidvalueif isvalid(options.avoidvalue) thenclaims = avoidvalue(claims, options)end-- options.prefervalueif isvalid(options.prefervalue) thenclaims = prefervalue(claims, options)end-- options.getonlyif isvalid(options.getonly) thenclaims = getonly(claims, options)end-- options.dontgetif isvalid(options.dontget) thenclaims = dontget(claims, options)endlocal firstvalue = options.enbarten or options.firstvaluelocal first = isvalid(tonumber(firstvalue))if isvalid(first) and #claims > 1 thenif #claims > 0 thenfirst = tonumber(first) or 1if first > 0 and first <= #claims thenclaims = {claims[first]}elseclaims = {claims[1]}endendelseif isvalid(firstvalue) and #claims > 0 thenclaims = {claims[1]}endlocal numval = options.numvalif numval and type(numval) ~= "number" thennumval = tonumber(numval)endif numval and type(numval) == "number" and #claims > 1 and #claims > numval thenlocal claimsnumval = {}local ic = 1while (numval >= ic) and (#claims >= ic) dotable.insert(claimsnumval, claims[ic])ic = ic + 1endclaims = claimsnumvalendreturn claimsendfunction getEntityFromId(id)return isvalid(id) and mw.wikibase.getEntityObject(id) or mw.wikibase.getEntityObject()endfunction formatError(key)return i18n.errors[key]endfunction formatOneStatement(statement, ref, options)local value = nillocal stat = formatStatement(statement, options)if not stat thenreturn {v = value, raw = stat}endlocal s = stat.valueif isntvalid(s) thenreturn {v = value, raw = stat}endif not stat.qualifiers thenstat.qualifiers = {}endlocal P585 = stat.P585 or ""local tf = stat.tifrlocal awardqual = stat.fotolocal ro = stat.rolocal qp1a = stat.qp1alocal onlyqualifier = stat.onlyqualifierlocal qp1 = stat.qp1local qp2 = stat.qp2local qp3 = stat.qp3local qp4 = stat.qp4local qp5 = stat.qp5local reff = stat.reffif reff and isvalid(options.reff) thens = s .. reffendif isvalid(options.template) thens =mw.getCurrentFrame():expandTemplate {title = options.template,args = {stat.QQ1,s,stat.QQ2,stat.QQ3,stat.QQ4,stat.QQ5,stat.QQ6,stat.QQ7,stat.QQ8,stat.QQ9,stat.QQ10,entityId = options.entityId,v1 = options.v1,id = stat.ID}}endif isvalid(options.football) thens =mw.getCurrentFrame():expandTemplate {title = "صندوق معلومات سيرة كرة قدم/سطر فريق",args = {stat.qualifiers.P580 or "",stat.qualifiers.P582 or "",s,stat.amatch,stat.goal}}endif isvalid(options.office) thens =mw.getCurrentFrame():expandTemplate {title = "معلومات صاحب منصب/منصب ويكي بيانات/نواة",args = {office = s,termstart = stat.qualifiers.P580 or "",termend = stat.qualifiers.P582 or "",constituency = stat.qualifiers.P768 or "",predecessor = stat.qualifiers.P1365 or "",successor = stat.qualifiers.P1366 or "",series = stat.qualifiers.P1545 or "",of = stat.qualifiers.P642 or "",electedin = stat.qualifiers.P2715 or "",jurisdiction = stat.qualifiers.P1001 or "",employer = stat.qualifiers.P108 or "",entityId = options.entityId}}endlocal function qoo(Prefix, qualpref, p, Suffix)if isvalid(p) thenlocal stri = (Prefix or " (") .. (qualpref or "") .. p .. (Suffix or ")")if isvalid(options.nosmall) thenreturn strielsereturn mw.text.tag("small", {}, stri)endendend  local QPrefix = isvalid(options.qualifierprefix)local QSuffix = isvalid(options.qualifiersuffix)if isvalid(qp1) and isvalid(options.qual1) and isvalid(qp1a) and isvalid(options.qual1a) thens = s .. qoo(QPrefix, options.qual1pref, qp1, QSuffix) .. qoo(QPrefix, options.qp1apref, qp1a, QSuffix)elseif isvalid(qp1) and isvalid(options.qual1) thens = s .. qoo(QPrefix, options.qual1pref, qp1, QSuffix)elseif isvalid(qp1a) and isvalid(options.qual1a) thens = s .. qoo(QPrefix, options.qp1apref, qp1a, QSuffix)endif isvalid(qp2) and isvalid(options.qual2) thens = s .. qoo(QPrefix, options.qual2pref, qp2, QSuffix)endif isvalid(qp3) and isvalid(options.qual3) thens = s .. qoo(QPrefix, options.qual3pref, qp3, QSuffix)endif isvalid(qp4) and isvalid(options.qual4) thens = s .. qoo(QPrefix, options.qual4pref, qp4, QSuffix)endif isvalid(qp5) and isvalid(options.qual5) thens = s .. qoo(QPrefix, options.qual5pref, qp5, QSuffix)endif isvalid(options.justthisqual) thenif onlyqualifier thens = onlyqualifierelses = nil -- We need only the qualifierendendif isvalid(ro) and isvalid(options.withro) thens = s .. qoo(QPrefix, "", ro, QSuffix)endif isvalid(P585) and isvalid(options.withdate) thenif options.withdate == "y" thens = s .. qoo(QPrefix, "سنة ", P585, QSuffix)elseif options.withdate == "before" thens = "*" .. P585 .. ":" .. s .. "\n"elses = s .. qoo(QPrefix, "", P585, QSuffix)endendif awardqual and isvalid(options.awardqua) thens = s .. qoo(QPrefix, "", awardqual, QSuffix)endlocal bothdates = options.withintervall or options.bothdatesif tf and isvalid(bothdates) thenif bothdates == "line" thens = s .. mw.text.tag("br") .. qoo(QPrefix, "", tf, QSuffix)elseif bothdates == "before" thens = qoo(QPrefix, "", tf, QSuffix) .. selses = s .. qoo(QPrefix, "", tf, QSuffix)endendif type(ref) == "table" or (isvalid(options.noref)) or (isvalid(options.justthisqual)) thenvalue = selselocal t = formatReferences(statement, options)stat.ref = tif isvalid(options.justref) thenvalue = telseif isvalid(options.onlyvaluewithref) thenif isvalid(t) thenvalue = s .. tendelsevalue = s .. tendendreturn {v = value, raw = stat}endfunction formatStatements(options, LuaClaims)local valuetable = {} -- formattedStatementslocal claims = {}if isntvalid(options.property) and isvalid(options.pid) thenoptions.property = options.pidendif isntvalid(options.property) thenreturn formatError("property-param-not-provided")endlocal option1 = options.option1if option1 and options.option1value thenoptions[option1] = options.option1valueoptions['"' .. option1 .. '"'] = options.option1value--mw.log( "option1: " .. option1 .. "value: " .. options.option1value  )endif type(LuaClaims) == "table" thenclaims = LuaClaims[options.property] or {}mw.log("module:wikidata2: claims = LuaClaims[options.property]")else--Get entitylocal entity = nilif options.entity and type(options.entity) == "table" thenentity = options.entityelselocal id = get_entityId(options)if isvalid(id) thenlocal check = mw.ustring.match(id, "Q%d+") or mw.ustring.match(id, "P%d+")if check == nil thenmw.addWarning(id .. " لا يمثل معرف ويكي بيانات صحيح")return ""elseoptions.entityId = idoptions.qid = idendendentity = getEntityFromId(id)end--local property = mw.wikibase.resolvePropertyId( options.property:upper() )local property = options.property:upper()if not entity thenreturn ""end --TODO error?if not entity.claims or not entity.claims[property] thenif isvalid(options.otherproperty) thenoptions.property = options.otherpropertyproperty = options.otherproperty:upper()endendif property == nil thenreturn ""endif not entity.claims or not entity.claims[property] thenreturn "" --TODO error?end--Format statement and concat them cleanlyif options.rank == "best" or isntvalid(options.rank) then--claims = entity:getAllStatements( property )claims = entity:getBestStatements(property)elseif options.rank == "valid" thenfor i, statement in pairs(entity.claims[property]) doif statement.rank == "preferred" or statement.rank == "normal" thentable.insert(claims, statement)endendelseif options.rank == "all" thenfor i, statement in pairs(entity.claims[property]) dotable.insert(claims, statement)endelsefor i, statement in pairs(entity.claims[property]) doif statement.rank == options.rank thentable.insert(claims, statement)endendendendclaims = filter_claims(claims, options)local statementsraw = {}local All_claims = claimsif claims thenif options["property-module"] or options["property-function"] thenif not options["property-module"] or not options["property-function"] thenreturn formatError("unknown-property-module")endlocal formatter = require("Module:" .. options["property-module"])if not formatter thenreturn formatError("property-module-not-found")endlocal fun = formatter[options["property-function"]]if not fun thenreturn formatError("property-function-not-found")endmw.log("work with property-module: " .. options["property-module"] .. "|" .. options["property-function"])return fun(claims, options)elsefor i, statement in pairs(claims) dooptions.num = ilocal va = formatOneStatement(statement, LuaClaims, options)if va.v thentable.insert(valuetable, va.v)endtable.insert(statementsraw, va.raw)endendendif isvalid(options.raw) thenif isvalid(options.rawtolua) thenreturn mw.getCurrentFrame():extensionTag("source", mw.dumpObject(statementsraw), {lang = "lua"})endreturn statementsrawendlocal priff = ""local Separator = options.separator -- or ""local Conjunction = options.conjunction -- or ""if Separator == "br" or Conjunction == "br" thenSeparator = mw.text.tag("br")endif Separator == "*" thenpriff = "\n*"Separator = "\n*"endif Separator == "#" thenpriff = "\n#"Separator = "\n#"endif isvalid(options.justref) thenpriff = ""Separator = ""endlocal tot = mw.text.listToText(valuetable, Separator, Separator)if #valuetable > 1 thentot = priff .. totendif isntvalid(tot) thentot = nilendif isvalid(options.returnnumberofvalues) or options.returnnumberofvalues == true thenreturn tot, #valuetableendif isvalid(options.numberofclaims) thenreturn #All_claimsendreturn totendfunction p.formatAndCat(args)if args == nil thenreturn nilendFrame_args = argsargs.linkback = args.linkback or trueargs.addcat = trueif isvalid(args.value) and args.value == "-" thenreturn nilendif isvalid(args.value) thenlocal val = args.value .. addTrackingCategory(args)val = p.addLinkBack(val, args.entity, args.property)return valendreturn p.formatStatementsFromLua(args)endfunction formatReferences(statement, options)local ic = 1local reference = {}local numberofref = tonumber(options.numberofreferences) or 7local qid = get_entityId(options)local statementreferences = statement.referencesif statementreferences thenif Modulecite == nil thenModulecite = require(citetitle)endfor i, ref in ipairs(statementreferences) doif ref.snaks and numberofref >= ic thenlocal s = Modulecite._cite_wikidata(ref, qid)if isvalid(s) thenic = ic + 1table.insert(reference, s)endendendendlocal final = table.concat(reference)if isvalid(final) thenfinal = final .. i18n.caterefendreturn final or ""endfunction formatqualifiers(statement, s, options)s.qualifiers = {}local function qua(p, firstvalue, modifytime)local vvvif isvalid(p) thenvvv =formatStatements({property = p,enlabelcate = "t",firstvalue = (firstvalue or ""),modifytime = (modifytime or "longdate"),noref = "t"},statement.qualifiers) or ""s.qualifiers[p] = vvvreturn vvvendendif isvalid(options.template) thens.ID = statement.mainsnak.datavalue.value.ids.QQ1 = qua(options.Q1)s.QQ2 = qua(options.Q2)s.QQ3 = qua(options.Q3)s.QQ4 = qua(options.Q4)s.QQ5 = qua(options.Q5)s.QQ6 = qua(options.Q6)s.QQ7 = qua(options.Q7)s.QQ8 = qua(options.Q8)s.QQ9 = qua(options.Q10)s.QQ10 = qua(options.Q10)endif isvalid(options.football) thenif statement.qualifiers.P1350 or statement.qualifiers.P1351 thens.amatch = qua("P1350", "true")s.goal = qua("P1351", "true")endendif (isvalid(options.football)) or (isvalid(options.office)) thens.start1 = qua("P580", "true")s.finish1 = qua("P582", "true")endif isvalid(options.office) thenifstatement.qualifiers.P580 or statement.qualifiers.P582 or statement.qualifiers.P1365 orstatement.qualifiers.P1366 thens.before1 = qua("P1365", "true")s.after1 = qua("P1366", "true")s.constituency1 = qua("P768")s.series1 = qua("P1545")s.electedin1 = qua("P2715", "")s.pp1001 = qua("P1001")s.pp108 = qua("P108")s.pp642 = qua("P642")endendif isvalid(options.withdate) then--if statement.qualifiers.P585 thens.P585 = qua("P585", "true", options.modifyqualifiertime)endlocal qwe = options.qwerif statement.qualifiers.qwe thens.ro = qua(qwe, "true")endlocal bothdates_option = options.withintervall or options.bothdatesif isvalid(bothdates_option) thenif statement.qualifiers.P580 or statement.qualifiers.P582 thenlocal f = qua("P580", "true", options.modifyqualifiertime)local t = qua("P582", "true", options.modifyqualifiertime)s.tifr = f .. "–" .. tendendif isvalid(options.awardqua) thenif statement.qualifiers.P585 or statement.qualifiers.P1346 thenlocal fo = qua("P585", "true", options.modifyqualifiertime)local to = qua("P1346", "true")s.foto = fo .. " " .. mw.text.tag("span", {}, " " .. to .. "")endendlocal function quaaal(opti, options)if isvalid(opti) and statement.qualifiers[opti] thenlocal kkk =formatStatements({property = opti,noref = "t",separator = options.qualifierseparator,conjunction = options.qualifierconjunction,size = options.size,image = options.image,modifytime = options.modifyqualifiertime,enlabelcate = "t",langpref = options.langpref,showlang = options.showlang},statement.qualifiers) or ""s.qualifiers[opti] = kkkreturn kkkendendif isvalid(options.justthisqual) and statement.qualifiers[options.justthisqual] thens.onlyqualifier = quaaal(options.justthisqual, options)endif isvalid(options.qual1) and statement.qualifiers[options.qual1] thens.qp1 = quaaal(options.qual1, options)endif isvalid(options.qual1a) and statement.qualifiers[options.qual1a] thens.qp1a = quaaal(options.qual1a, options)endif isvalid(options.qual2) and statement.qualifiers[options.qual2] thens.qp2 = quaaal(options.qual2, options)endif isvalid(options.qual3) and statement.qualifiers[options.qual3] thens.qp3 = quaaal(options.qual3, options)endif isvalid(options.qual4) and statement.qualifiers[options.qual4] thens.qp4 = quaaal(options.qual4, options)endif isvalid(options.qual5) and statement.qualifiers[options.qual5] thens.qp5 = quaaal(options.qual5, options)endendfunction formatSnak(snak, options)if snak.snaktype == "somevalue" thenlocal somevalue = options.somevalue or i18n["somevalue"]return {value = somevalue}elseif snak.snaktype == "novalue" thenlocal novalue = options.novalue or i18n["novalue"]return {value = novalue}elseif snak.snaktype == "value" thenlocal s = formatDatavalue(snak.datavalue, snak.datatype, options)if s and s.value and isvalid(s.value) thens.value = (options.prefix or "") .. s.value .. (options.suffix or "")endreturn selsereturn {value = formatError("unknown-snak-type")}endendfunction formatStatement(statement, options)if options["claim-module"] or options["claim-function"] thenif not options["claim-module"] or not options["claim-function"] thenreturn {value = formatError("unknown-claim-module")}endlocal formatter = require("Module:" .. options["claim-module"])if formatter == nil thenreturn {value = formatError("claim-module-not-found")}endlocal fun = formatter[options["claim-function"]]if fun == nil thenreturn {value = formatError("claim-function-not-found")}endreturn {value = fun(statement, options)}elseif statement.type == "statement" thenlocal s = formatSnak(statement.mainsnak, options)if isvalid(s) thens.qualifiers = {}if statement.qualifiers thenlocal qualu = formatqualifiers(statement, s, options)--if isvalid(qualu) then table.insert(qualu) endendif statement.references thenif isvalid(options.reff) thens.reff = formatReferences(statement, options)endendendreturn selseif not statement.type thenreturn formatSnak(statement, options)endreturn {value = formatError("unknown-claim-type")}endfunction get_property1(options, item)--[[ function to get countries flags without reload large countries items ]]local flagprop = {"p27", "p1532", "p17", "p495", "p1376"}local work_flag = falseif string.lower(options.property1) == "p41" thenfor k, l in pairs(flagprop) doif string.lower(options.property) == l thenwork_flag = trueendendendlocal caca = ""local size = options.size or ""if isntvalid(size) thensize = "20"endif work_flag thenif Moduleflags == nil thenModuleflags = require("Module:Wikidata2/Flags")endlocal flag = Moduleflags[item]if isntvalid(flag) thenflag =formatStatements({property = options.property1,otherproperty = options.otherproperty1,entityId = item,rank = options.property1rank,pattern = options.property1pattern,formatting = options.property1formatting,noref = "t",firstvalue = "t"})--mw.log("get flag2 :" .. flag .. ", for item ".. item )endif isvalid(flag) then -- return real imageif isvalid(options.image) then -- return real imagecaca = "[[file:" .. flag .. "|" .. size .. "px|" .. "border" .. "]]"--caca = Infobox_Image( {image=flag, size=size, maxsize = "280x330px", center=options.center} )--mw.log("get flag :" .. flag .. ", for item ".. item )endendendif isntvalid(caca) thencaca =formatStatements({property = options.property1,otherproperty = options.otherproperty1,entityId = item,rank = options.property1rank,pattern = options.property1pattern,formatting = options.property1formatting,size = options.size,image = options.image,noref = "t",firstvalue = "t"})endreturn cacaendfunction formatwikibaseitem(datavalue, datatype, options)--[[  datatypewikibase-item]]local valuelocal itemqid = datavalue.value.idlocal Skipped = skiip_items[options.property] or {}for k, v in pairs(Skipped) doif datavalue.value.id == v thenreturn {value = "", item = ""}endendif isvalid(options.formatting) thenif options.formatting == "raw" thenreturn {value = itemqid, item = itemqid}elseif options.formatting == "rawtotemplate" and isvalid(options.rawtotemplate) thenlocal args = {q = itemqid, no1 = options.no1 or "", no2 = options.no2 or ""}value = mw.getCurrentFrame():expandTemplate {title = options.rawtotemplate, args = args} .. "\n"return {value = value, item = itemqid}elseif options.formatting == "fu" thenvalue =mw.getCurrentFrame():expandTemplate {title = "Cycling race/stageclassification2",args = {itemqid, type = "1"}}return {value = value, item = itemqid}elseif options.formatting == "sitelink" thenvalue = formatsitelink(datavalue.value.id, options)return {value = value, item = itemqid}elsevalue = formatcharacters(datavalue.value, options)if isvalid(options.pattern) thenvalue = formatFromPattern(value, options)endreturn {value = value, item = itemqid}endendlocal itemValue = formatEntityId(itemqid, options).valueif isvalid(itemValue) thenif isvalid(options.property1) and options.property1:upper():sub(1, 1) == "P" thenlocal prop1value = get_property1(options, itemqid)if isvalid(prop1value) thenprop1value = (options.property1pref or "") .. "" .. prop1value .. "" .. (options.property1suff or "")value = prop1value .. " " .. itemValueif isvalid(options.property1after) thenvalue = itemValue .. prop1valueendelsevalue = itemValueendreturn {value = value, item = itemqid}elseif isvalid(options.propertyimage) thenlocal p_f = options.propertyimageformatting or options.formattingpropertyimagelocal vas =formatStatements({property = options.propertyimage,formatting = p_f,entityId = itemqid,rank = options.rank,pattern = options.pattern,size = options.size,image = options.image,noref = "t",avoidvalue = options.avoidvaluepropertyimage,firstvalue = "t",nolink = options.nolink})if isvalid(vas) thenreturn {value = vas}endelseif isvalid(options.property2) thenlocal caca =formatStatements({property = options.property2,entityId = itemqid,noref = options.noref,rank = options.rank,pattern = options.property2pattern,size = options.size,image = options.image,propertyimage = options.property3,firstvalue = "t"})if isvalid(caca) thenreturn {value = caca .. " " .. itemValue}endendendreturn {value = itemValue, item = itemqid}endfunction formatwikibaseproperty(datavalue, datatype, options)--[[  datatypewikibase-property]]local tid = ""if isvalid(options.formatting) thenif options.formatting == "raw" thentid = datavalue.value.idendelsetid = formatEntityId(datavalue.value.id, options).valueendreturn {value = tid}endfunction formattabulardata(datavalue, datatype, options)--[[  tabular-data ]]local data = "[[commons:" .. datavalue.value .. "|" .. datavalue.value .. "]]"return {value = data}endfunction formatgeoshape(datavalue, datatype, options)--[[  geo-shape ]]local shape = "[[commons:" .. datavalue.value .. "|" .. datavalue.value .. "]]"return {value = shape}endfunction formatcommonsMedia(datavalue, datatype, options)local tid--[[ commonsMedia ]]local size = options.size or ""if isvalid(options.image) then -- return real image--tid = "[[file:" .. datavalue.value  .. "|".. (options.size or "20").."px|border]]"--if isvalid(options.center) then--tid = "[[file:" .. datavalue.value  .. "|".. (options.size or "20").."px|border|center]]"--end--tid = "[[file:" .. datavalue.value  .. "|".. (options.size or "60").."px|".."border".."]]"local params = {image = datavalue.value, maxsize = "280x330px", center = (options.center or "")}if isvalid(options.size) thenparams.size = sizeendif InfoboxImage == nil thenInfoboxImage = require("Module:InfoboxImage")endtid = InfoboxImage.Infobox_Image(params)elsetid = formatcharacters(datavalue.value, options)endreturn {value = tid}endfunction formatmath(datavalue, datatype, options)--[[datatype math ]]--return{value=mw.text.tag("math", {}, "".. datavalue.value.."") } -- that doesn't work welllocal result = mw.getCurrentFrame():callParserFunction("#tag:math", datavalue.value)return {value = result}endfunction formatstring(datavalue, datatype, options)--[[  datatype string ]]local par = options.patternlocal result = formatcharacters(datavalue.value, options)local tid = resultif isvalid(options.stringpattern) thentid = mw.ustring.gsub(options.stringpattern, "$1", datavalue.value)elseif isvalid(par) thenif par ~= "autourl" and par ~= "autourl2" and par ~= "autourl3" and par ~= "autourl4" thentid = formatFromPattern(result, options)endendreturn {value = tid}endfunction formatexternalid(datavalue, datatype, options)local result = formatcharacters(datavalue.value, options)if isntvalid(options.pattern) thenreturn {value = result} --just return valueendlocal patter =formatStatements({property = "P1630", entityId = options.property, firstvalue = "t", noref = "t", rank = "all"}) -- get formatter URLlocal par = options.patternlocal tid = resultif isvalid(patter) then -- if P1630 are therelocal pp = formatFromPattern(datavalue.value, {pattern = patter})local plabel = mw.wikibase.label(options.property) or pplocal ppp = mw.ustring.gsub(pp, " ", "_")local results = {["autourl"] = ppp, -- like http://example.com/$1.html["autourl2"] = "[" .. ppp .. " " .. datavalue.value .. "]", -- like [http://example.com/$1.html $1]["autourl3"] = "[" .. ppp .. " " .. ppp .. "]", -- like [http://example.com/$1.html http://example.com/$1.html]["autourl4"] = "[" .. ppp .. " " .. plabel .. "]"}if results[par] thentid = results[par]elsetid = formatFromPattern(result, options)endelseif isvalid(par) thenif par ~= "autourl" and par ~= "autourl2" and par ~= "autourl3" and par ~= "autourl4" thentid = formatFromPattern(result, options)endendreturn {value = tid}endfunction formattime(datavalue, datatype, options)--[[  datatypetime  ]]local ModuleTime = require "Module:wikidata2/time"local timen = datavalue.valuelocal tid = ModuleTime.getdate(timen, options)-- local tid = mw.getCurrentFrame():preprocess(mall)if isvalid(options.modifytime) thenif options.modifytime == "q" thenlocal mall = datavalue.value.timetid = mw.getCurrentFrame():preprocess(mall)elseif options.modifytime == "precision" thenlocal mall = datavalue.value.precisiontid = mw.getCurrentFrame():preprocess(mall)endendreturn {value = tid}endfunction formatcoordinate(datavalue, datatype, options)--[[  datatypeglobe-coordinate  ]]--local GlobeCoordinate = require "Module:GlobeCoordinate"--return {value=GlobeCoordinate.newFromWikidataValue( datavalue.value ):toHtml()}if ModuleGlobes == nil thenModuleGlobes = require("Module:Wikidata2/Globes")endlocal coord = datavalue.valuelocal globe = datavalue.value.globelocal globe2 = ModuleGlobes[globe] or ""local results = {["latitude"] = coord.latitude,["longitude"] = coord.longitude,["dimension"] = coord.dimension,["precision"] = coord.precision,["globe"] = globe:match("Q%d+"),["globe2"] = globe2}local pro = options.formatting and results[options.formatting]if pro == nil thenpro =mw.getCurrentFrame():preprocess("{{ {{{|safesubst:}}}#invoke:Coordinates|coord" .."|" .. coord.latitude .."|" .. coord.longitude .."|display=inline" .."|globe:" .. globe2 .. "_type:landmark" .."|format=" .. (options.formatcoord or "") .. "}}") .. catewikidatainfo(options)endreturn {value = pro}endfunction formatquantity(datavalue, datatype, options)--[[  datatype quantity ]]local amount, unit = datavalue.value.amount, datavalue.value.unitamount = mw.ustring.gsub(amount, "+", "")if unit thenunit = unit:match("Q%d+")endif formatera == nil thenformatera = require("Module:Wikidata2/Math")endlocal number = formatera.newFromWikidataValue(datavalue.value)local unitraw = unitif unit then-- يتحقق اذا كان هناك اي اختصار لوحدة القياس--if lab and ( isntvalid(options.nounitshort) ) thenif isvalid(options.unitshort) thenlocal lab =options.label or formatStatements({property = "P498", entityId = unit, firstvalue = "t", noref = "t"}) orformatStatements({property = "P5061", entityId = unit, firstvalue = "t", langpref = options.langpref, noref = "t"}) or""local s =formatEntityId(unit, {label = lab, enlabelcate = "t", nolink = (options.nounitlink or options.nolink)})unit = s.valueelselocal s = formatEntityId(unit, {nolink = options.nounitlink, enlabelcate = "t"})unit = s.valueendendif options.formatcharacters and options.formatcharacters == "formatnum" thenamount = make_format_num(amount)endlocal Value = amount .. " " .. (unit or "")if isvalid(options.nounit) thenValue = amountendreturn {value = Value, amount = amount, unit = unit, unitraw = unitraw}endfunction formaturl(datavalue, datatype, options)--[[  datatypeurl ]]local label = options.labelif isvalid(options.urllabel) thenlabel = options.urllabelendlocal va = mw.ustring.gsub(datavalue.value, " ", "_")if label == nil and options.property == "P856" thenlabel = "الموقع الرسمي"endif isvalid(options.displayformat) == "weblink" thenif weblink == nil thenweblink = require("Module:Weblink")endreturn {value = weblink.makelink(va)}endif isvalid(options.formatting) == "raw" thenreturn {value = va}endlocal pro = vaif isvalid(label) thenpro = "[" .. va .. " " .. label .. "]"endreturn {value = pro}endfunction formatmonolingualtext(datavalue, datatype, options) -- showlanglocal text = datavalue.value.textif isvalid(options.textformat) == "text" thenreturn {value = text}endif Moduletext == nil thenModuletext = require "Module:wikidata2/monolingualtext"endlocal tid = Moduletext._main(datavalue, datatype, options)return {value = tid}endfunction formatDatavalue(datavalue, datatype, options)--Use the customize handler if providedif options["value-module"] or options["value-function"] thenif not options["value-module"] or not options["value-function"] thenreturn {value = formatError("unknown-value-module")}endlocal formatter = require("Module:" .. options["value-module"])if not formatter thenreturn {value = formatError("value-module-not-found")}endlocal fun = formatter[options["value-function"]]if not fun thenreturn {value = formatError("value-function-not-found")}endreturn {value = fun(datavalue, datatype, options)}end--Default dataformatterslocal dataformatters = {["wikibase-item"] = formatwikibaseitem,["wikibase-property"] = formatwikibaseproperty,["commonsMedia"] = formatcommonsMedia,["math"] = formatmath,["time"] = formattime,["external-id"] = formatexternalid,["string"] = formatstring,["globe-coordinate"] = formatcoordinate,["quantity"] = formatquantity,["url"] = formaturl,["monolingualtext"] = formatmonolingualtext,["geo-shape"] = formatgeoshape,["tabular-data"] = formattabulardata}local dataformatter = dataformatters[datatype]if not dataformatter thenreturn {value = formatError("unknown-data-type")}endreturn dataformatter(datavalue, datatype, options)endfunction Labelfunction(qid, arlabel, options) -- label with no arwiki sitelinklocal valuelocal cat = ""local use_en_label = isvalid(options.enlabelcate) or isvalid(options.use_en_labels)local Args = {fromlua = "t", ["المعرف"] = qid, nocat = "t"}if isvalid(options.illwd2noy) thenArgs.noy = "t"endif isvalid(options.illwd2y) thenArgs.y = "t"endif isvalid(arlabel) thenvalue = arlabelif isvalid(options.illwd2) thenArgs.label = arlabelvalue = mw.getCurrentFrame():expandTemplate {title = "Ill-WD2", args = Args}endelseif isntvalid(options.justarabic) then-- else--cat = " [[".. i18n.noarabiclabel .."|".. qid .."]]"local en_label = mw.wikibase.label(qid) or ""if isvalid(options.illwd2noarlabel) thenif isvalid(en_label) thenArgs.enlabel = en_labelendvalue = mw.getCurrentFrame():expandTemplate {title = "Ill-WD2", args = Args}elseif use_en_label and isvalid(en_label) thenvalue = en_labelendendreturn {value = value or "", cat = cat}endfunction formatEntityId(qid, options)local labeloption = options.labellocal label = ""local valuelocal arlabel = labelIn("ar", qid) or "" -- The arabic labellocal link = mw.wikibase.sitelink(qid)if isvalid(labeloption) thenlabel = labeloptionelseif isvalid(arlabel) then--mw.log("arlabel" .. arlabel)label = arlabelelseif isvalid(link) thenlabel = linkarlabel = linkendif isvalid(link) then-- elseif isvalid(arlabel) thenlocal linklabel = isvalid(label) or linkif (isntvalid(options.nolink)) thenvalue = "[[:" .. link .. "|" .. formatcharacters(linklabel, options) .. "]]"label = linklabelelsevalue = formatcharacters(linklabel, options)label = linklabelendelselocal va = Labelfunction(qid, arlabel, options)label = va.valuevalue = va.value -- .. va.catendreturn {value = value or "", label = label or ""}endfunction sitelink_g(id, wikisite)--[[ function to get any link from any sister project ]]local site = wikisite or "arwiki"local link = mw.wikibase.sitelink(id, site) or ""--mw.log("mw.wikibase.sitelink,site: " .. site.. ",link:" .. link )return linkendfunction sitelink(id, wikisite)local site = wikisite or "arwiki"local link = ""--local link = mw.wikibase.getSitelink( id, site ) or ""local entity = mw.wikibase.getEntityObject(id)ifentity and entity.sitelinks and entity.sitelinks["" .. site .. ""] and entity.sitelinks["" .. site .. ""].site andentity.sitelinks["" .. site .. ""].title thenif entity.sitelinks["" .. site .. ""].site == site thenlink = entity.sitelinks["" .. site .. ""].titleendendreturn linkendfunction formatsitelink(entityId, options)--[[ function to get only the value with link ]]local link = sitelink(entityId)if isvalid(link) and isntvalid(options.nolink) thenreturn "[[" .. link .. "]]" .. catewikidatainfo(options)endreturn linkendfunction formatFromPattern(str, options)-- [[  function to replace $1 with string  ]]if isvalid(options.pattern) thenstr = string.gsub(str, "%%", "%%%%")str = mw.ustring.gsub(options.pattern, "$1", str)endreturn strendfunction p.getEntity(id)if type(id) == "table" thenreturn idendreturn getEntityFromId(id)endfunction p.translate(str, rep1, rep2)str = i18n[str] or strif rep1 and (type(rep1) == "string") thenstr = str:gsub("$1", rep1)endif rep2 and (type(rep2) == "string") thenstr = str:gsub("$2", rep2)endreturn strendfunction p.getId(snak)if (snak.snaktype == "value") thenif snak.datavalue.type == "wikibase-entityid" thenreturn "Q" .. snak.datavalue.value["numeric-id"]endendendfunction p.addLinkBack(str, id, property)if not id thenid = p.getEntity()endif not id thenreturn strendif type(property) == "table" thenproperty = property[1]endif type(id) == "table" thenid = id.idendlocal class = ""if property thenclass = "wd_" .. string.lower(property)endlocal icon = "[[File:Blue pencil.svg|%s|10px|baseline|class=noviewer|link=%s]]"local title = i18n["see-wikidata-value"]local url = mw.uri.fullUrl("d:" .. id, "uselang=ar")url.fragment = propertyurl = tostring(url)local v =mw.html.create("span"):addClass(class):wikitext(str):tag("span"):addClass("noprint wikidata-linkback"):css("padding-left","0.5em"):wikitext(icon:format(title, url)):allDone()return tostring(v)end-- Function to check whether a certain item is a parent of a given item.-- If pExitItem is reached without finding the searched parent item, the search stops.-- A parent is connected via P31 or P279.-- Attention: very intensive function, use carefully!function isParent(property, pItem, pParent, pExitItem, pMaxDepth, pDepth)if not pDepth thenpDepth = 0endlocal entity = mw.wikibase.getEntity(pItem)if not entity thenreturn falseendlocal claims31local claims279if entity.claims thenif isvalid(property) thenclaims31 = entity.claims[property]elseclaims31 = entity.claims["P31"]claims279 = entity.claims["P279"]endelsereturn falseendif not claims31 and not claims279 thenreturn falseendlocal parentIds = {}if claims31 and #claims31 > 0 thenfor i, v in ipairs(claims31) doparentIds[#parentIds + 1] = p.getId(v.mainsnak)endendif claims279 and #claims279 > 0 thenfor i, v in ipairs(claims279) doparentIds[#parentIds + 1] = p.getId(v.mainsnak)endend-- check if searched parent or exit item is reached or do recursive callif not parentIds[1] or #parentIds == 0 thenreturn falseendlocal itemString = ""local result = nilfor i, qid in ipairs(parentIds) doif not qid thenreturn falseend--itemString = "Q" .. vitemString = qidif itemString == pParent then-- successful!return trueelseif itemString == pExitItem then --or itemString == "Q35120"-- exit if either "exit item" or node item (Q35120) is reachedreturn falseelseif pDepth + 1 < pMaxDepth thenresult = isParent(property, itemString, pParent, pExitItem, pMaxDepth, pDepth + 1)elsereturn falseendif result == true thenreturn resultendendenddoreturn falseendendfunction p.Subclass(options)if options thenFrame_args = optionsendlocal parent = options.parent or ""local id = options.id or ""local Entity = getEntityFromId(id)if Entity thenid = Entity.idendlocal property = options.property or "P31"if isntvalid(parent) or isntvalid(id) or isntvalid(property) thenreturn falseendlocal tab = mw.text.split(options.parent, ",")local result = mw.wikibase.getReferencedEntityId(id, property, tab) -- { "Q5", "Q2095" } )if result == nil and property == "P31" thenresult = mw.wikibase.getReferencedEntityId(id, "P279", tab)endif result thenreturn trueendendfunction old_isSubclass(options)if isntvalid(options.parent) thenreturn falseendlocal maxDepthmaxDepth = tonumber(options.maxDepth) or 10if not type(maxDepth) == "number" thenmaxDepth = 5endlocal property = options.propertylocal resultresult = isParent(property, options.id, options.parent, options.exitItem, maxDepth)if options.returnInt thenif result == true thenreturn 1elsereturn nilendelsereturn resultendendfunction p.formatSnak(snak, options)return formatSnak(snak, options)endfunction p.formatEntityId(entityId, options)return formatEntityId(entityId, (options or {}))endfunction p.formatStatements(frame, key)if frame.args thenif type(key) == "table" and key ~= {} thenelseFrame_args = frame.argsendend--[[ The main function ]]local args = frame.args--If a value if already set, use itif isvalid(args.value) thenreturn args.valueendlocal wd_arg = frame:getParent().args["ويكي بيانات"] or frame.args["ويكي بيانات"] -- arg used to ban wikidata valueif isvalid(wd_arg) == "لا" thenreturn ""endlocal valuesnumb = 0local slocal prop = formatStatements(args, key)if isvalid(args.returnnumberofvalues) thens, valuesnumb = formatStatements(frame.args, key)endif isvalid(prop) thenif isvalid(args.mainprefix) then -- mainprefixprop = args.mainprefix .. propendif isvalid(args.mainsuffix) then -- mainsuffixprop = prop .. args.mainsuffixendif isvalid(args.addTrackingCat) then -- add tracking catprop = prop .. addTrackingCategory(frame.args)endif isvalid(args.mainsuffixAfterIcon) then -- another suffix but after wikidata iconprop = prop .. args.mainsuffixAfterIconendelseif isvalid(args.NoPropValue) then -- value if no local value and no wikidata valueprop = args.NoPropValueendendif isvalid(args.returnnumberofvalues) thenmw.log("valuesnumb: " .. valuesnumb)return s, valuesnumbendreturn propendfunction p.formatStatementsFromLua(options, key) -- main function but to use from lua moduleif options thenif type(key) == "table" and key ~= {} thenelseFrame_args = optionsendend--If a value if already set, use itif isvalid(options.value) thenreturn options.valueendlocal valuesnumb = 0local s = formatStatements(options, key)if isvalid(options.returnnumberofvalues) thens, valuesnumb = formatStatements(options, key)endif isntvalid(s) thens = nilendif isvalid(s) thenif isvalid(options.mainprefix) then -- mainprefixs = options.mainprefix .. sendif isvalid(options.mainsuffix) then -- mainsuffixs = s .. options.mainsuffixendif isvalid(options.addTrackingCat) then -- add tracking cats = s .. addTrackingCategory(options)endif isvalid(options.mainsuffixAfterIcon) then -- another suffix but after wikidata icons = s .. options.mainsuffixAfterIconendelseif isvalid(options.NosValue) then -- value if no local value and no wikidata values = options.NosValueendendif isvalid(options.returnnumberofvalues) then--mw.log( "valuesnumb: " .. valuesnumb )return s, valuesnumbendreturn sendfunction p.isSubclass(frame)return p.Subclass(frame.args)end-- Return the site link for a given data item and a given site (the current site by default)function p.getSiteLink(frame)local site = frame.args[2] or frame.args.sitelocal id = frame.args[1] or frame.args.idlocal count = frame.args.countsitelinksif isntvalid(id) thenif isvalid(frame.args.page) thenid = mw.wikibase.getEntityIdForTitle(frame.args.page)endendif isvalid(count) thenreturn countSiteLinks(id)endlocal link = sitelink(id, site)if isvalid(link) thenreturn linkendendfunction p.getSiteLink1(frame)local project = frame.args[1]local id = frame.args[2]local link = sitelink(id, project)return linkend-- returns the page id (Q...) of the current page or nothing of the page is not connected to Wikidatafunction p.pageId(frame)return mw.wikibase.getEntityIdForCurrentPage()endfunction p.descriptionIn(frame)local langcode = frame.args[1] or frame.args["lang"]local id = frame.args[2] or frame.args["id"]return descriptionIn(langcode, id)endfunction p.labelIn(frame)local langcode = frame.args[1] or frame.args["lang"]local id = frame.args[2] or frame.args["id"]return labelIn(langcode, id)endfunction p.getLabel(entity, lang)return labelIn(lang, entity)endfunction p.ViewSomething(frame) -- from en:Module:Wikidatalocal f = (frame.args[1] or frame.args.id) and frame or frame:getParent()local aaif isvalid(f.args.id) thenaa = f.args.idendlocal data = mw.wikibase.getEntityObject(aa)if data == nil thenreturn nilendlocal i = 1while true dolocal index = f.args[i]if index == nil thenif type(data) == "table" thenreturn mw.text.jsonEncode(data, mw.text.JSON_PRESERVE_KEYS + mw.text.JSON_PRETTY)elsereturn tostring(data)endenddata = data[index] or data[tonumber(index)]if data == nil thenreturnendi = i + 1endendfunction p.Dump(frame)local f = (frame.args[1] or frame.args.id) and frame or frame:getParent()local aaif isvalid(f.args.id) thenaa = f.args.idendlocal data = mw.wikibase.getEntityObject(aa)if data == nil thenreturn i18n.warnDumpendlocal i = 1while true dolocal index = f.args[i]if index == nil thenreturn frame:extensionTag("source", mw.dumpObject(data), {lang = "lua"}) .. i18n.warnDumpenddata = data[index] or data[tonumber(index)]if data == nil thenreturn i18n.warnDumpendi = i + 1endendfunction p.countSiteLinks(id)return countSiteLinks(id)endfunction p.EntityIdForTitle(frame)local title = frame.args[1]local str = mw.wikibase.getEntityIdForTitle(title)--mw.log(str)return strendfunction p.Qidfortitleandwiki(frame)local title = frame.args[1]local wiki = frame.args[2]local str = mw.wikibase.getEntityIdForTitle(title, wiki)return strendreturn p