وحدة:Wikidata/ru/نسخة

local i18n = {
    ["errors"] = {
        ["property-param-not-provided"] = "الخاصية غير موجودة.",
        ["entity-not-found"] = "المدخلة غير موجودة.",
         ["unknown-claim-type"] = "نوع الإدعاء غير معروف.",
         ["unknown-snak-type"] = "نوع الـ (snak)  غير معروف.",
         ["unknown-datavalue-type"] = "نوع الـ (dataValue) غير معروف.",
        ["unknown-entity-type"] = "نوع المدخلة غير معروفة.",
        ["unknown-property-module"] = "You must install and property-module, b property-function.",
        ["unknown-claim-module"] = "You must install and claim-module, and claim-function.",
        ["unknown-value-module"] = "You must install and value-module, b value-function.",
        ["property-module-not-found"] = "Module to display the properties can not be found",
        ["property-function-not-found"] = "Function to display properties not found",
        ["claim-module-not-found"] = "Module to display the statement could not be found.",
        ["claim-function-not-found"] = "Function to display the statement could not be found.",
        ["value-module-not-found"] = "Module to display the value is not found.",
        ["value-function-not-found"] = "Function to display the value is not found."
    },
    ["somevalue"] = "''غير معروف''",
    ["novalue"] = "",
    ["circa"] = '<span style="border-bottom: 1px dotted; cursor: help;" title="circa">حوالي. </span>',
    ["presumably"] = '<span style="border-bottom: 1px dotted; cursor: help;" title="presumably">محتمل. </span>',
}

-- settings, may differ from project to project
local categoryLinksToEntitiesWithMissingLabel = '[[تصنيف:صفحات على ويكي بيانات دون تسمية عربية]]';

local categoryLocalValuePresent = ''; --[[Категория:Википедия:Статьи с переопределением значения из Викиданных]]
local outputReferences = true;

--مصادر يجب تجاهلها في حالة وجود مصادر مفضلة
local deprecatedSources = {
	Q36578 = true, -- Gemeinsame Normdatei
	Q63056 = true, -- Find a Grave
	Q15222191 = true, -- BNF
};
-- المصادر المفضلة
local preferredSources = {
	Q5375741  = true, -- Encyclopædia Britannica Online
	Q17378135  = true, -- Great Soviet Encyclopedia (1969—1978)
};

-- Ссылки на используемые модули, которые потребуются в 99% случаев загрузки страниц (чтобы иметь на виду при переименовании)
local moduleSources = require('Module:Sources/نسخة')

local p = {}

local formatDatavalue, formatEntityId, formatRefs, formatSnak, formatStatement, formatStatementDefault, formatProperty, getSourcingCircumstances, loadCacheSafe, throwError, toBoolean;

local function copyTo( obj, target )
	for k, v in pairs( obj ) do
		target[k] = v
	end
	return target;
end

local function loadCacheSafe( entityId )
	local status, result = pcall( function() return mw.loadData( 'Module:WikidataCache/' .. entityId ) end );
	if ( status == true ) then
		return result;
	end
	return nil;
end
local function splitISO8601(str)
	if 'table' == type(str) then
		if str.args and str.args[1] then
			str = '' .. str.args[1]
		else
			return 'unknown argument type: ' .. type( str ) .. ': ' .. table.tostring( str )
		end
	end
	local Y, M, D = (function(str) 
		local pattern = "(%-?%d+)%-(%d+)%-(%d+)T"
		local Y, M, D = mw.ustring.match( str, pattern )
		return tonumber(Y), tonumber(M), tonumber(D)
	end) (str);
	local h, m, s = (function(str) 
		local pattern = "T(%d+):(%d+):(%d+)%Z";
		local H, M, S = mw.ustring.match( str, pattern);
		return tonumber(H), tonumber(M), tonumber(S);
	end) (str);
	local oh,om = ( function(str)
		if str:sub(-1)=="Z" then return 0,0 end; -- ends with Z, Zulu time
		-- matches ±hh:mm, ±hhmm or ±hh; else returns nils
		local pattern = "([-+])(%d%d):?(%d?%d?)$";
		local sign, oh, om = mw.ustring.match( str, pattern);
		sign, oh, om = sign or "+", oh or "00", om or "00";
		return tonumber(sign .. oh), tonumber(sign .. om);
	end )(str)
	return {year=Y, month=M, day=D, hour=(h+oh), min=(m+om), sec=s};
end

local function parseTimeBoundaries( time, precision )
	local s = splitISO8601( time );
	if (not s) then return nil; end

	if ( precision >= 0 and precision <= 8 ) then
		local powers = { 1000000000 , 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10 }
		local power = powers[ precision + 1 ];
		local left = s.year - ( s.year % power );
		return { tonumber(os.time( {year=left, month=1, day=1, hour=0, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=left + power - 1, month=12, day=31, hour=29, min=59, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 9 ) then
		return { tonumber(os.time( {year=s.year, month=1, day=1, hour=0, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=12, day=31, hour=23, min=59, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 10 ) then
		local lastDays = {31, 28.25, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		local lastDay = lastDays[s.month];
		return { tonumber(os.time( {year=s.year, month=s.month, day=1, hour=0, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=s.month, day=lastDay, hour=23, min=59, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 11 ) then
		return { tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=0, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=23, min=59, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 12 ) then
		return { tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=59, sec=58} )) * 1000 + 19991999 };
	end

	if ( precision == 13 ) then
		return { tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=s.min, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=s.min, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 14 ) then
		local t = tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=s.min, sec=0} ) );
		return { t * 1000, t * 1000 + 999 };
	end

	error('Unsupported precision: ' .. precision );
end

--[[ 
 تحويل سلسلة إلى قيمة منطقية

 Принимает: строковое значение (может отсутствовать)
 Возвращает: булевое значение true или false, если получается распознать значение, или defaultValue во всех остальных  случаях
]]
local function toBoolean( valueToParse, defaultValue )
    if ( valueToParse ) then
        if valueToParse == '' or valueToParse == 'false' or valueToParse == '0' then
            return false
        end
        return true
    end
    return defaultValue;
end

--[[ 
  Function to get the essence (entity) for the current page

  Accepts: a string identifier (type P18, Q42)
  Returns: an object table whose elements are indexed from zero
]]
local function getEntityFromId( id )
    if id then
    	local cached = loadCacheSafe( id );
    	if ( cached ) then
    		return cached;
    	end
        return mw.wikibase.getEntityObject( id )
    end
    local entity = mw.wikibase.getEntityObject();
    if ( entity ) then
    	local cached = loadCacheSafe( entity.id );
    	if ( cached ) then
    		return cached;
    	end
	end
    return entity;
end

--[[ 
وظيفة داخلية لإنشاء رسالة خطأ
 
  Accepts: key element in the i18n table (such as entity-not-found)
Returns: string message
]]
local function throwError( key )
    error( i18n.errors[key] );
end

--[[ 
The function for the entity identifier

Accepts: object table essentially 
Returns: string identifier (such as P18, Q42)
]]
local function getEntityIdFromValue( value )
    local prefix = ''
    if value['entity-type'] == 'item' then
        prefix = 'Q'
    elseif value['entity-type'] == 'property' then
        prefix = 'P'
    else
        throwError( 'unknown-entity-type' )
    end
    return prefix .. value['numeric-id']
end

-- التحقق من وجود وظيفة متخصصة في الخيارات
local function getUserFunction( options, prefix, defaultFunction )
    -- check for specialized processors in the parameters,
    -- transmitted by a call
    if options[ prefix .. '-module' ] or options[ prefix .. '-function' ] then
    	-- التحقق من الأسطر الفارغة في المعلمات أو عدمه
        if not options[ prefix .. '-module' ] or not options[ prefix .. '-function' ] then
            throwError( 'unknown-' .. prefix .. '-module' );
        end
        --dynamic load modules with the handler specified in the parameter
        local formatter = require ('Module:' .. options[ prefix .. '-module' ]);
        if formatter == nil then
            throwError( prefix .. '-module-not-found' )
        end
        local fun = formatter[ options[ prefix .. '-function' ] ]
        if fun == nil then
            throwError( prefix .. '-function-not-found' )
        end
        return fun;
    end

   	return defaultFunction;
end

-- Select the properties for property id, optional filtering them by rank
local function selectClaims( context, options, propertySelector )
	if ( not context ) then error( 'context not specified'); end;
	if ( not options ) then error( 'options not specified'); end;
	if ( not options.entity ) then error( 'options.entity is missing'); end;
	if ( not propertySelector ) then error( 'propertySelector not specified'); end;

	local WDS = require('Module:WikidataSelectors')
	result = WDS.filter(options.entity.claims, propertySelector)

    if ( not result or #result == 0 ) then
    	return nil;
    end

    return result;
end

--[[ 
  Function for registration statements (statement)
  لمزيد من المعلومات d:Wikidata:Glossary/ru

Accepts: table settings

 Returns: a string formatted text to be displayed in the article
]]
local function formatProperty( options )
    -- Receiving entity by ID
    local entity = getEntityFromId( options.entityId )
    if not entity then
        return -- throwError( 'entity-not-found' )
    end
	-- проверка на присутсвие у сущности заявлений (claim)
	-- подробнее о заявлениях см. d:Викиданные:Глоссарий
    if (entity.claims == nil) then
        return nil --'' --TODO error?
    end

	-- improve options
	options.frame = g_frame;
	options.entity = entity;
	options.extends = function( self, newOptions )
		return copyTo( newOptions, copyTo( self, {} ) )
	end

	if ( options.i18n ) then
		options.i18n = copyTo( options.i18n, copyTo( i18n, {} ) );
	else
		options.i18n = i18n;
	end

	-- create context
	local context = {
		entity = options.entity,
		formatSnak = formatSnak,
		formatPropertyDefault = formatPropertyDefault,
		formatStatementDefault = formatStatementDefault }
	context.formatProperty = function( options ) 
		local func = getUserFunction( options, 'property', context.formatPropertyDefault );
		return func( context, options )
	end;
	context.formatStatement = function( options, statement ) return formatStatement( context, options, statement ) end;
	context.formatSnak = function( options, snak, circumstances ) return formatSnak( context, options, snak, circumstances ) end;
	context.formatRefs = function( options, statement ) return formatRefs( context, options, statement ) end;
	
	context.parseTimeFromSnak = function( snak )
			if ( snak and snak.datavalue and snak.datavalue.value and snak.datavalue.value.time ) then
				return tonumber(os.time( splitISO8601( tostring( snak.datavalue.value.time ) ) ) ) * 1000;
			end
			return nil;
		end
	context.parseTimeBoundariesFromSnak = function( snak )
			if ( snak and snak.datavalue and snak.datavalue.value and snak.datavalue.value.time and snak.datavalue.value.precision ) then
				return parseTimeBoundaries( snak.datavalue.value.time, snak.datavalue.value.precision );
			end
			return nil;
		end
	context.getSourcingCircumstances = function( statement ) return getSourcingCircumstances( statement ) end;
	context.selectClaims = function( options, propertyId ) return selectClaims( context, options, propertyId ) end;

	return context.formatProperty( options );
end

function formatPropertyDefault( context, options )
	if ( not context ) then error( 'context not specified' ); end;
	if ( not options ) then error( 'options not specified' ); end;
	if ( not options.entity ) then error( 'options.entity missing' ); end;

    local claims = context.selectClaims( options, options.property );
    if (claims == nil) then
        return nil --'' --TODO error?
    end

    -- Обход всех заявлений утверждения и с накоплением оформленых предпочтительных 
    -- the statements in the table
    local formattedClaims = {}

    for i, claim in ipairs(claims) do
        local formattedStatement = context.formatStatement( options, claim )
        -- здесь может вернуться либо оформленный текст заявления
        -- or nil error string seems never returns
        if (formattedStatement) then
            --formattedStatement =''formattedStatement'' -- '<span class="wikidata-claim" data-wikidata-property-id="' .. string.upper( options.property ) .. '" data-wikidata-claim-id="' .. claim.id .. '">' .. formattedStatement .. '</span>'
            table.insert( formattedClaims, formattedStatement )
        end
    end

	-- إنشاء سلسلة نصية مع قائمة من طلبات التسجيل من الجدول
    return mw.text.listToText( formattedClaims, options.separator, options.conjunction )
end

--[[ 
 the function for processing one statement (statement)

Accepts: object-table statement and table settings


Returns: a string of text issued a statement (claim)
]]
function formatStatement( context, options, statement )
	if ( not statement ) then
		error( 'statement is not specified or nil' );
	end
    if not statement.type or statement.type ~= 'statement' then
        throwError( 'unknown-claim-type' )
    end

    local functionToCall = getUserFunction( options, 'claim', context.formatStatementDefault );
    return functionToCall( context, options, statement );
end

function getSourcingCircumstances( statement )
	if (not statement) then error('statement is not specified') end;

	local circumstances = {};
	if ( statement.qualifiers
			and statement.qualifiers.P1480 ) then
		for i, qualifier in pairs( statement.qualifiers.P1480 ) do
			if ( qualifier
					and qualifier.datavalue
					and qualifier.datavalue.type == 'wikibase-entityid'
					and qualifier.datavalue.value
					and qualifier.datavalue.value["entity-type"] == 'item' ) then
				local circumstance = 'Q' .. qualifier.datavalue.value["numeric-id"];
				if ( 'Q5727902' == circumstance ) then
					circumstances.circa = true;
				end
				if ( 'Q18122778' == circumstance ) then
					circumstances.presumably = true;
				end
			end
		end
	end
	return circumstances;
end

--[[ 
  The function for processing one statement (statement)

Accepts: object-table statement, the table settings,
	function object design approval of internal structures (snak) and
	function object design references to sources (reference)
	Returns: a string of text issued a statement (claim)
]]
function formatStatementDefault( context, options, statement )
	if (not context) then error('context is not specified') end;
	if (not options) then error('options is not specified') end;
	if (not statement) then error('statement is not specified') end;

	local circumstances = context.getSourcingCircumstances( statement );

	if ( options.references ) then
    	return context.formatSnak( options, statement.mainsnak, circumstances ) .. context.formatRefs( options, statement );
    else
    	return context.formatSnak( options, statement.mainsnak, circumstances );
    end
end

--[[ 
The function for registration of the approval (snak)

	Accepts: snak table object (main snak snak or from qualifier) and table options
	Returns: string issued wikitext
]]
function formatSnak( context, options, snak, circumstances )
	circumstances = circumstances or {};
	local hash = '';
	local mainSnakClass = '';
	if ( snak.hash ) then
		hash = ' data-wikidata-hash="' .. snak.hash .. '"';
	else
		mainSnakClass = ' wikidata-main-snak';
	end

	local before = ''--<span class="wikidata-snak ' .. mainSnakClass .. '"' .. hash .. '>
	local after = ''--</span>

    if snak.snaktype == 'somevalue' then
        if ( options['somevalue'] and options['somevalue'] ~= '' ) then
            return before .. options['somevalue'] .. after;
        end
        return before .. options.i18n['somevalue'] .. after;
    elseif snak.snaktype == 'novalue' then
        if ( options['novalue'] and options['novalue'] ~= '' ) then
            return before .. options['novalue'] .. after;
        end
        return before .. options.i18n['novalue'] .. after;
    elseif snak.snaktype == 'value' then
		if ( circumstances.presumably ) then
			before = before .. options.i18n.presumably;
		end
		if ( circumstances.circa ) then
			before = before .. options.i18n.circa;
		end

        if(formatDatavalue( context, options, snak.datavalue )) then 
            return before .. formatDatavalue( context, options, snak.datavalue ) .. after;
          else return '';
        end
    else
        throwError( 'unknown-snak-type' );
    end
end

--[[ 
 وظيفة لقيمة معالجة الكائنات مع الإحداثيات الجغرافية

Accepts: object value and the table settings,
	Returns: a string formatted text
]]
function formatGlobeCoordinate( value, options )
	-- проверка на требование в параметрах вызова на возврат сырого значения 
    if options['subvalue'] == 'latitude' then -- широты
        return value['latitude']
    elseif options['subvalue'] == 'longitude' then -- долготы
        return value['longitude']
    else
    	-- Otherwise generated parameters to call the template {{coord}}
		-- You need to add in the template documents that he called here, and that
		-- Any change it paramerov must be agreed with the code here
        local eps = 0.0000001 -- < 1/360000
        local globe = '' -- TODO
        local lat = {}
        lat['abs'] = math.abs(value['latitude'])
        lat['ns'] = value['latitude'] >= 0 and 'N' or 'S'
        lat['d'] = math.floor(lat['abs'] + eps)
        lat['m'] = math.floor((lat['abs'] - lat['d']) * 60 + eps)
        lat['s'] = math.max(0, ((lat['abs'] - lat['d']) * 60 - lat['m']) * 60)
        local lon = {}
        lon['abs'] = math.abs(value['longitude'])
        lon['ew'] = value['longitude'] >= 0 and 'E' or 'W'
        lon['d'] = math.floor(lon['abs'] + eps)
        lon['m'] = math.floor((lon['abs'] - lon['d']) * 60 + eps)
        lon['s'] = math.max(0, ((lon['abs'] - lon['d']) * 60 - lon['m']) * 60)
        local coord = '{{coord'
        if (value['precision'] == nil) or (value['precision'] < 1/60) then -- по умолчанию с точностью до секунды
            coord = coord .. '|' .. lat['d'] .. '|' .. lat['m'] .. '|' .. lat['s'] .. '|' .. lat['ns']
            coord = coord .. '|' .. lon['d'] .. '|' .. lon['m'] .. '|' .. lon['s'] .. '|' .. lon['ew']
        elseif value['precision'] < 1 then
            coord = coord .. '|' .. lat['d'] .. '|' .. lat['m'] .. '|' .. lat['ns']
            coord = coord .. '|' .. lon['d'] .. '|' .. lon['m'] .. '|' .. lon['ew']
        else
            coord = coord .. '|' .. lat['d'] .. '|' .. lat['ns']
            coord = coord .. '|' .. lon['d'] .. '|' .. lon['ew']
        end
        coord = coord .. '|globe:' .. globe
        if options['display'] then
            coord = coord .. '|display=' .. options.display
        else
            coord = coord .. '|display=title'
        end
        coord = coord .. '}}'

        return g_frame:preprocess(coord)
    end
end

local function getDefaultValueFunction( datavalue )
    -- استدعاء معالج الافتراضي لأنواع معروفة من القيم
    if datavalue.type == 'wikibase-entityid' then
    	-- entity identifier
        return function( context, options, value ) return formatEntityId( getEntityIdFromValue( value ), options ) end;
    elseif datavalue.type == 'string' then
    	-- line
        return function( context, options, value ) return value end;
    elseif datavalue.type == 'monolingualtext' then
    	--monolingual text (string indicating the language)
        return function( context, options, value )
        	if ( options.monolingualLangTemplate == 'lang' ) then
	        	return options.frame:expandTemplate{ title = 'lang-' .. value.language, args = { value.text } };
        	elseif ( options.monolingualLangTemplate == 'ref' ) then
	        	return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>' .. options.frame:expandTemplate{ title = 'ref-' .. value.language };
        	else
	        	return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>';
        	end
        end;
    elseif datavalue.type == 'globecoordinate' then
    	-- الإحداثيات الجغرافية
        return function( context, options, value ) return formatGlobeCoordinate( value, options )  end;
    elseif datavalue.type == 'quantity' then
        return function( context, options, value )
	    	--a range of values
	        local amount = string.gsub(value['amount'], '^%+', '')
	        local lang = mw.language.new( 'ar' )
	        return lang:formatNum( tonumber( amount ) )
        end;
    elseif datavalue.type == 'time' then
        return function( context, options, value )
			local moduleDate = require('Module:Wikidata/date')
	    	return moduleDate.formatDate( context, options, value );
        end;
    else
    	-- خطأ
        throwError( 'unknown-datavalue-type' )
    end
end

--[[ 
  وظيفة لمعالجة القيم (value)

Accepts: object value and the table settings,
	Returns: a string formatted text
]]
function formatDatavalue( context, options, datavalue )
	if ( not context ) then error( 'context not specified' ); end;
	if ( not options ) then error( 'options not specified' ); end;
	if ( not datavalue ) then error( 'datavalue not specified' ); end;
	if ( not datavalue.value ) then error( 'datavalue.value is missng' ); end;

    -- проверка на указание специализированных обработчиков в параметрах,
    -- переданных при вызове
    context.formatValueDefault = getDefaultValueFunction( datavalue );
    local functionToCall = getUserFunction( options, 'value', context.formatValueDefault );
    return functionToCall( context, options, datavalue.value );
end

-- Небольшой словарь упрощенного отображения (TODO: надо сделать расширенный с учётом даты)
local simpleReplaces = {
	Q300000 = 'Ars-en-Ré',
}

--[[ 
  Функция для оформления идентификатора сущности

	Accepts: identifier string (type Q42) and table settings,
		Returns: a string formatted text
]]
function formatEntityId( entityId, options )
	--  الحصول على الاسم المترجم 
    local label = nil;
    if ( options.text and options.text ~= '' ) then
        label = options.text
    else
    	if ( simpleReplaces[entityId] ) then
			return simpleReplaces[entityId];
		end
		label = mw.wikibase.label( entityId );
    end

	-- الحصول على الوصلة
    local link = mw.wikibase.sitelink( entityId )
    if link then
        if label then
            return '[[' .. link .. '|' .. label .. ']]'
        else
            return '[[' .. link .. ']]' 
        end
    end

    if label then
    	-- will be replaced by link to Reasonator by Gadget
        return label --nil
	else
		
    end
    -- رسالة حول عدم وجود وصلة محلية
    -- افتراضياً يظهر وصلة لويكي بيانات ولكن يفضل اخفاء هذه النتيجة ولا أدري ما الأفضل هل 
	-- return nil 
	-- أو نضع
	-- return '' 
    return '' --[ [:d:' .. entityId .. '|' .. entityId .. '] ]<span style="border-bottom: 1px dotted; cursor: help; white-space: nowrap" title="В صفحات على ويكي بيانات دون تسمية عربية">?</span>' ..categoryLinksToEntitiesWithMissingLabel; -- خاص بتصنيف محدد مسبقاً أعلاه
end

function p.labelIn(frame)
	local langcode = frame.args[1]
	local id = frame.args[2]	-- "id" must be nil, as access to other Wikidata objects is disabled in Mediawiki configuration
	-- return label of a Wikidata entity in the given language or the default language of this Wikipedia site
	return mw.wikibase.getEntityObject(id).labels[langcode or wiki.langcode].value
end

--[[ 
 The function for processing claims (statement)


Accepts: table settings
	Returns: a string formatted text to be displayed in the article
]]
-- устаревшее имя, не использовать
function p.formatStatements( frame )
	return p.formatProperty( frame );
end

function p.formatProperty( frame )
    local args = frame.args
    local plain = toBoolean( frame.args.plain, false );
    frame.args.nocat = toBoolean( frame.args.nocat, false );
    frame.args.references = toBoolean( frame.args.references, true );

    -- التحقق من الخاصية المطلوبة
    if not frame.args.property then
        throwError( 'property-param-not-provided' )
    end

-- if the value passed to the call options is only derive its
    if frame.args.value and frame.args.value ~= '' then
        -- special meaning to hide wikidata
        if frame.args.value == '-' then
            return ''
        end

        -- option, which prohibits registration of values, so does not touch
        if plain or frame.args.nocat or frame:callParserFunction( '#property', frame.args.property ) == '' then
            return frame.args.value
        end

        -- if the touch still can add the category marker
        return args.value .. categoryLocalValuePresent;
    end

    if ( plain ) then --Call the standard processor without clearance if transmitted plain option
    	return frame:callParserFunction( '#property', frame.args.property );
    end

	g_frame = frame
	-- after checking all the arguments - the call processing functions for the properties (a set of statements)
    return formatProperty( frame.args )
end

--[[
  وظيفة كل ما يشير إلى المصادر (reference) 

  Экспортируется в качестве зарезервированной точки для вызова из функций-расширения вида claim-module/claim-function через context
  Вызов из других модулей напрямую осуществляться не должен (используйте frame:expandTemplate вместе с одним из специлизированных шаблонов вывода значения свойства).

	Accepts: object-table statement

		Returns: a string designed to display links in the article
]]
function formatRefs( context, options, statement )
	if ( not context ) then error( 'context not specified' ); end;
	if ( not options ) then error( 'options not specified' ); end;
	if ( not options.entity ) then error( 'options.entity missing' ); end;
	if ( not statement ) then error( 'statement not specified' ); end;

	if ( not outputReferences ) then
		return '';
	end

	local result = '';
	if ( statement.references ) then

		local allReferences = statement.references;
		local hasPreferred = false;
		for _, reference in pairs( statement.references ) do
			if ( reference.snaks
					and reference.snaks.P248
					and reference.snaks.P248[1]
					and reference.snaks.P248[1].datavalue
					and reference.snaks.P248[1].datavalue.value["numeric-id"] ) then
				local entityId = "Q" .. reference.snaks.P248[1].datavalue.value["numeric-id"];
				if ( preferredSources[entityId] ) then
					hasPreferred = true;
				end
			end
		end

		for _, reference in pairs( statement.references ) do
			local display = true;
			if ( hasPreferred ) then
				if ( reference.snaks
						and reference.snaks.P248
						and reference.snaks.P248[1]
						and reference.snaks.P248[1].datavalue
						and reference.snaks.P248[1].datavalue.value["numeric-id"] ) then
					local entityId = "Q" .. reference.snaks.P248[1].datavalue.value["numeric-id"];
					if ( deprecatedSources[entityId] ) then
						display = false;
					end
				end
			end
			if ( display ) then
				result = result .. moduleSources.renderReference( g_frame, options.entity, reference );
			end
		end
	end
	return result
end

--Return the qualifiers with a format
function p.formatQualifiers(frame)
	local args = frame.args
	--If a value is already set, use it
	if args.value and args.value ~= '' then
		return args.value
	end
	--Required parameters
	local property = string.upper(args.property)
	local qualifierProperty = string.upper(args.qualifierProperty)
	if not property then
		return formatError( 'property-param-not-provided' )
	end
	if not qualifierProperty then
		return formatError( 'qualifier-property-param-not-provided' )
	end
	
	--Get statements
	local rawStatements = getClaims( args )
	if not rawStatements or numOfClaims(rawStatements) == 0 then
		return nil
	end
	
	--Format statement and concat them cleanly
	local formattedQualifiers = {}
	for i, statement in pairs( rawStatements ) do
		if not statement.qualifiers or not statement.qualifiers[qualifierProperty] then
			table.insert(formattedQualifiers, "")
		elseif args.rank == 'one' then
			return formatQualifier( statement.qualifiers, qualifierProperty, args )
		elseif args.rank == 'preferred' then --This should be set as the default case
			if #rawStatements == 1 then
				return formatQualifier( statement.qualifiers, qualifierProperty, args ) --Output the only value
			else
				if statement.rank == 'preferred' then
					table.insert( formattedQualifiers, formatQualifier( statement.qualifiers, qualifierProperty, args ) ) --Output only the preferred values
				end
			end
		else
			table.insert( formattedQualifiers, formatQualifier( statement.qualifiers, qualifierProperty, args ) )
		end
	end
	
	return mw.text.listToText( formattedQualifiers, args.separator, args.conjunction )
end

-- This is used to get the TA98 (Terminologia Anatomica first edition 1998) values like 'A01.1.00.005' (property P1323)
-- which are then linked to http://www.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/01.1.00.005%20Entity%20TA98%20EN.htm
-- uses the newer mw.wikibase calls instead of directly using the snaks
-- formatPropertyValues returns a table with the P1323 values concatenated with ", " so we have to split them out into a table in order to construct the return string
p.getTAValue = function(frame)
    local ent = mw.wikibase.getEntityObject()
    local props = ent:formatPropertyValues('P1323')
    local out = {}
    local t = {}
    for k, v in pairs(props) do
        if k == 'value' then
            t = mw.text.split( v, ", ")
            for k2, v2 in pairs(t) do
                out[#out + 1] = "[http://www.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/" .. string.sub(v2, 2) .. "%20Entity%20TA98%20EN.htm " .. v2 .. "]"
            end
        end
    end
    local ret = table.concat(out, "<br> ")
    if #ret == 0 then
        ret = "Invalid TA"
    end
    return ret
end


return p