مۆدیوول:پێکهاتەی ویکیدراوە/ویکیدراوە

لە ئینسایکڵۆپیدیای ئازادی ویکیپیدیاوە
بەڵگەدارکردنی مۆدیوول[دروست بکە]
local wd = {}
local wd2formatStatements = require('مۆدیوول:Wikidata2').formatStatementsFromLua

local module_i18n = require 'Module:Wikidata/I18n'

function wd.translate(str, rep1, rep2)
	str = module_i18n[str] or str
	if rep1 and (type (rep1) == 'string') then
		str = str:gsub('$1', rep1)
	end
	if rep2 and (type (rep2) == 'string')then
		str = str:gsub('$2', rep2)
	end
	return str
end

local function addCat(cat, sortkey)
	if sortkey then
		return  '[[Category:' .. cat .. '|' .. sortkey .. ']]'
	end
	return '[[Category:' .. cat  .. ']]'
end

local function formatError( key , category, debug)
    if debug then
        return error(modules.i18n[key] or key)
    end
    if category then
        return addCat(category, key)
    else
        return addCat('cat-unsorted-issue', key)
    end
end

function wd.getEntityIdForCurrentPage()
	return mw.wikibase.getEntityIdForCurrentPage()
end

function wd.addLinkBack(str, id, property)
	if id == '-' then
		return str   -- تعطيل wikidata
	end
	if not id then
		id = mw.wikibase.getEntityObject() 
	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
function wd.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 formatError("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 addCat(cat )
		else return ''
	end
end

function wd.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 .. wd.addTrackingCat(args.property)
		val = wd.addLinkBack(val, args.entity, args.property)
		return val
	end 
	return wd2formatStatements( args )
end

function wd.getClaims(query)
	query.raw = 'true'
	local claims = wd2formatStatements(query)
	
	--mw.log("FgetClaims****"..mw.dumpObject( fwd.getClaims(query)))
	--mw.log("getClaims****"..mw.dumpObject( claims))
	
	return claims
end

return wd