وحدة:بطاقة/ويكي.بيانات

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث
-- author وهراني @arwiki
-- Version: 20230523 

local infowd = {}

local function WDCa_showDate(params)
       params = (type(params) == 'table' and params) or {}

       if type(params.property) ~= 'string' then
		 return nil,error('invalid-datatype property!! ' .. type(params.property) .. ' != string')
       elseif string.sub(params.property, 1, 1) ~= 'P'  then 
   		return nil,error('invalid-dataformat property!! ' .. params.property)
       end

       local i=1
       if(params.qualifier or params.qualifier1) then i=2 end

       while (params["qualifier" .. i]) do i = i + 1 end

       if(type(params.formatting) == 'string' and params.formatting ~='table')
       then params["colformat0"] = params.formatting
       end

       params.formatting='table'

       params["qualifier" .. (i+1)]='P580'
       params["qualifier" .. (i+2)]='P582'
       params["qualifier" .. (i+3)]='P585'

       params["colformat" .. (i+1)]='Y'
       params["colformat" .. (i+2)]='Y'
       params["colformat" .. (i+3)]='Y'

       params["qualifier" .. (i)] ='P580 or P582'
       params["rowsubformat" .. (i)]='<small class="nowrap">($'..(i+1)..' – $'..(i+2)..')</small>'
       params["rowsubformat" .. (i+3)]='<small>($'..(i+3)..')</small>'

       if( type(params.rowformat) == 'string') 
       then params.rowformat = params.rowformat ..'$'..(i)..'$'..(i+3)
       else params.rowformat = '$0$R0 '..'$'..(i)..'$'..(i+3) end
       
       if(type(params.sorttype) == 'string') then --chronological/inverted
       	 if(params.tablesort == nil) then params.tablesort = (i+1)..'/'..(i+3) end
       	 if(params.sorttype == 'inverted') then params.sorting=-1 end
       end

       --mw.logObject(params)
       return params
end

local function WikidataCa(id,query)
		local wd = require("Module:Wikidata.Ca")

		if not query.item then 	query.item = id end
		if type(query.func) == 'string' and query.func ~= 'claim' 
          then 
          	return wd[query.func](query) 
        end

		query.shownovalue = query.shownovalue or "false"
		if not query.conjtype and not query.conjunction 
		    then	query.conjunction = ' — '
			else 	query.conjunction = query.conjtype or query.conjunction
		end
		if query.conjunction == 'new line'
			then	query.conjunction = "<br />"
		elseif query.conjunction == 'comma'
			then	query.conjunction = "، "
		elseif query.conjunction == 'and'
			then	query.conjunction = " و"
		elseif query.conjunction == 'or'
			then	query.conjunction = " أو "
		end
		
		query.separator = query.separator or query.conjunction
		query.query='num'
		if type(query.property) == 'table' 
			then query.property = mw.text.listToText(query.property , ' OR ' ,' OR ') 
		end
		
		if query.references == nil  then query.references = 3 end

    	if ((type(query.showDate) =='string' or type(query.showDate) =='boolean') and query.showDate and query.formatting ~='raw') 
            then query = WDCa_showDate(query) end

		if (type(query.formatting) =='string' and query.formatting ~='raw') then
            local value, number = wd.claim(query)
            return value, number
		end
		return wd.claim(query)
end

local function WikidataFr(item,query)
		local wd  = require("Module:Wikidata/fr")
		local value, number -- valeur à retourner, nombre de valeurs pour accorder le libellé
		if not query.entity then
			query.entity = item
		end
		if not query.conjtype then
			query.conjtype = 'comma'
		end
		local claims = wd.getClaims(query)
			if (not claims) then
			return nil
		end
		return wd.formatAndCat(query), #claims
	end	
	
local function Wikidata2(item,query)	
    local wd   = require('وحدة:Wikidata2')
    local wd2formatStatements = wd.formatStatementsFromLua

	local function addLinkBack(str, id, property)
	
		if id == '-' then
			return str   -- تعطيل wikidata
		end
	
		if not id then
			id = mw.wikibase.getEntityIdForCurrentPage() 
		end
		if not id then
			return str
		end
		if type(property) == 'table' then
			property = property[1]
		end
	
		if type(id) == 'table' then
			id = id.id
		end
	
		local class = ''
	
		if property then
			class = 'wd_' .. string.lower(property)
		end
	
		local icon = '[[File:Blue pencil.svg|%s|10px|baseline|class=noviewer|link=%s]]'
		local title = wd.translate('see-wikidata-value')
		local url = mw.uri.fullUrl('d:' .. id, '')
		url.fragment = property -- ajoute une #ancre si paramètre "property" défini
	
		url = 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

	local function addTrackingCat(prop, cat) -- doit parfois être appelé par d'autres modules
	
		if type(prop) == 'table' then
			prop = prop[1] -- devrait logiquement toutes les ajouter
		end
	
		if not prop and not cat then
			return '[[Category:Error-property-param-not-provided]]'
		end
	
		if not cat then
			cat = wd.translate('trackingcat', prop or 'P??')
		end
		if mw.title.getCurrentTitle().namespace ==0 then
			return '[[Category:' .. cat  .. ']]'
			else return ''
		end
	end
	local function formatAndCat(args)
		if not args then
			return nil
		end
		args.linkback = args.linkback or true
		args.addcat = true
		if args.value then -- do not ignore linkback and addcat, as formatStatements do
			if args.value == '-' then
				return nil
			end
	
			local val = args.value  .. addTrackingCat(args.property)
			val = addLinkBack(val, args.entity, args.property)
			return val
		end 
		return wd2formatStatements( args )
	end 
	
	if not query.rank  then
		query.rank =  "best"
	end

	shownovalue=false
	
	if not query.entity then
		query.entityId = item.id
	end
	if not query.conjtype and not query.conjunction then
		query.conjunction = ' — '
		query.separator = query.conjunction
		else 
			query.conjunction = query.conjtype
			query.separator = query.conjunction
	end
	if query.conjunction == 'new line'
		then 
			query.conjunction = "\n"
			query.separator = query.conjunction
	elseif query.conjunction == 'comma'
		then 
			query.conjunction = "، "
			query.separator = query.conjunction
	elseif query.conjunction == 'and'
		then 
			query.conjunction = " و"
			query.separator = query.conjunction
	elseif query.conjunction == 'or'
		then 
			query.conjunction = " أو "
			query.separator = query.conjunction
	end
	if query.isinlang then 
		query.langpref = query.isinlang
	end
	
	local qualifiers
	if query.showqualifiers  then 
		if type(query.showqualifiers)=='table' then
			qualifiers = query.showqualifiers
		elseif type(query.showqualifiers)=='string' then
			qualifiers = mw.text.split(query.showqualifiers, ",")
		end
		if type(qualifiers)=='table' then
			if #qualifiers >=1 then query.qual1 = qualifiers[1] end
			if #qualifiers >=2 then query.qual2 = qualifiers[2] end
			if #qualifiers >=3 then query.qual3 = qualifiers[3] end
			if #qualifiers >=4 then query.qual4 = qualifiers[4] end
			if #qualifiers >=5 then query.qual5 = qualifiers[5] end
		else
			query.qual1 = query.qualifier
		end
	end
	query.preferqualifier =query.preferqualifier or  query.qualifier
	query.justthisqual = query.justthisqual or query.showonlyqualifier
	query.preferqualifiervalue = query.preferqualifiervalue or query.qualifiervalue
	query.avoidqualifier = query.avoidqualifier or query.excludequalifier
	--query. = query.excludequalifiervalue
	query.avoidvalue = query.avoidvalue or query.excludevalues

	if query.urlpattern then 
		query.pattern = query.urlpattern
	end
	if query.sorttype then 
		query.sortbytime = query.sorttype
		if not query.rank then query.rank=all end
	end
	if query.showdate then 
		query.withdate = query.showdate
		query.bothdates = query.showdate
	end
	if query.text and displayformat and displayformat=='weblink' then 
		query.urllabel = query.text
	end

	if not query.numberofreferences then 
		query.numberofreferences = 3
	end		
	if type(query.property) == 'table' then query.property = query.property[1] end
	
	local claims =wd2formatStatements(query) --wd.getClaims(query)
	if (not claims) then
		return nil
	end

	query.numberofclaims = 'y'
	local nbres = wd2formatStatements(query)
	return  formatAndCat({value=claims, property = query.property, entity = item.id}),nbres
	 
end

function infowd.expandQuery(item,wikimod,query)

	if(type(query) ~= 'table') then return nil end
	if(type(item) ~= 'table' or type(item.id)~= 'string') then return nil end
	
	local value, number -- نتيجة ، عدد النتائج للضبط الصرفي للعنوان
    local before = query.prefix or query.before or ''
    local after = query.suffix or query.after or ''
    
-- الوحدات المستعملة

    if wikimod == 'Wikidata.Ca' then 
			value, number =  WikidataCa(item.id,query)  
    elseif wikimod == 'Wikidata/fr' then
        	value, number = WikidataFr(item,query)
    else     --- else -- wikimod == 'Wikidata2' then 
		value, number =  Wikidata2(item,query)
	end
	if type(value) == 'string' 
		then return before..value..after, number
		else return nil
	end
end

return infowd