Documentation icon دستاویز [تخلیق]
--[[
Cross validation with wikidata: parse the wikitext of a value related to wikidata and see if they are close enough.
]]

local CrossValidate = {}
CrossValidate.CrossValidateResult = {
	INVALID_WIKITEXT='לא ידוע', --wikitext is empty or too complex
	MISSING_WIKIDATA='חסר', -- data doesnt exist in wikidata (no entity/no claim)
	INCOMPATIBLE='לא מתאים',
	COMPATIBLE='מתאים',
	MISSING_WIKIDATA_LABEL='חסרה תווית עברית'
}

function CrossValidate.maintainceCategory(value, propertyName) 
	if value==CrossValidate.CrossValidateResult.INVALID_WIKITEXT then return '' end
	return '[[קטגוריה: ויקינתונים - השוואת ערכים: ' .. value.. ']]' ..  '[[קטגוריה: ויקינתונים - השוואת ערכים: ' .. value.. ': '..(mw.wikibase.label( propertyName) or propertyName)..']]'
end
--function CrossValidate.crossValidate( wikitext, propertyName, allowMulti, allowNA, entityId, multiSeperator, optionalQualifier, genderAware )
function CrossValidate.crossValidate( wikitext, propertyName, entityId )
	
	if wikitext == nil or wikitext == '-' or #wikitext ==0 then 
		return CrossValidate.CrossValidateResult.INVALID_WIKITEXT -- comparing to null wikitext is meaningless
	end
    
    if entityId == nil then
    	entityId = mw.wikibase.getEntityIdForCurrentPage()
    end
	if entityId==nil then
		if mw.title.getCurrentTitle().namespace==0 then
			return CrossValidate.CrossValidateResult.MISSING_WIKIDATA
		else
			return CrossValidate.CrossValidateResult.INVALID_WIKITEXT
		end
	end
    local propertyVals = mw.wikibase.getBestStatements(entityId, propertyName)
    if (not propertyVals) or (#propertyVals==0) then return CrossValidate.CrossValidateResult.MISSING_WIKIDATA end --no such property for this item
    local resTable = {}
    local missingTranslation = false
    local isEqual = true
    for i, property in ipairs(propertyVals) do
	    local propValue = property.mainsnak and property.mainsnak.datavalue
	    if not propValue then 
    		return CrossValidate.CrossValidateResult.MISSING_WIKIDATA -- no value/someone etc are considered as missing
		end
		
	    if propValue['type'] == 'wikibase-entityid' then
	    	local localLabel, langLabel = mw.wikibase.getLabelWithLang( "Q" .. propValue.value['numeric-id'] )
	    	local isLocalLabel = langLabel=='ur'
	    	if not isLocalLabel then
	    		return CrossValidate.CrossValidateResult.MISSING_WIKIDATA_LABEL
	    	end
	    	local isCompatible = mw.ustring.find(wikitext, localLabel, 1, true)
	    	if not isCompatible then
	    		local sitelink = mw.wikibase.sitelink( "Q" .. propValue.value['numeric-id'] )
	    		isCompatible = sitelink and mw.ustring.find(wikitext, sitelink, 1, true)
	    	end
			if not  isCompatible then
				local entityObject =  mw.wikibase.getEntity( "Q" .. propValue.value['numeric-id'] )
				local entityAliases = entityObject and entityObject.aliases and entityObject.aliases['ur']
				if entityAliases then
					for _, curAlias in ipairs(entityAliases) do
						isCompatible = isCompatible or mw.ustring.find(wikitext, curAlias.value, 1, true)
					end
				end
			end
			
        	if not isCompatible then
        		return CrossValidate.CrossValidateResult.INCOMPATIBLE
        	end
	    elseif propValue['type'] == 'string' then
	    	local isImage = (property.mainsnak.datatype=='commonsMedia')
	    	if isImage then
	    		return CrossValidate.CrossValidateResult.INVALID_WIKITEXT -- images are too complex
	    	else
	    		if not (mw.ustring.find(wikitext, propValue.value, 1, true)) then
	    			return CrossValidate.CrossValidateResult.INCOMPATIBLE
	    		end
	    	end
    	elseif propValue['type'] == 'monolingualtext' then
    		if not mw.ustring.find(wikitext, propValue.value.text, 1, true) then
    			return CrossValidate.CrossValidateResult.INCOMPATIBLE
    		end
	    elseif propValue['type'] == 'quantity' then
	    	return MISSING_WIKIDATA -- TODO: parse quantity and units
	    elseif propValue['type'] == 'time' then
	    	local timeValue = mw.wikibase.renderSnak( property.mainsnak )
	    	if timeValue ~= wikitext and mw.ustring.gsub(timeValue, '^(%d+ %a+) (%d+)$', '[[%1]] [[%2]]')  ~= wikitext then
    			-- TODO: once we trust date parsing this can return INCOMPATIBLE
    			return CrossValidate.CrossValidateResult.INVALID_WIKITEXT
	    	end
	    else
	    	return CrossValidate.CrossValidateResult.INVALID_WIKITEXT -- ignore other types
	    end
	end
	
	return CrossValidate.CrossValidateResult.COMPATIBLE
end

return CrossValidate