Модуль:Wikidata/Countries

Дакументацыю да гэтага модуля можна стварыць у Модуль:Wikidata/Countries/Дакументацыя

local p = {}local function calculateEndDateTimestamp( 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 ( statement.qualifiers and statement.qualifiers.P582 ) thenfor i, qualifier in ipairs(statement.qualifiers.P582 ) dolocal parsedTime = context.parseTimeFromSnak( qualifier );if ( parsedTime ) thenreturn parsedTime;endendend-- check death day... do we have it at all?for h, propertyId in pairs( { "P570", "P577", "P571" } ) dolocal dateClaims = context.selectClaims( options, propertyId );if ( dateClaims ) thenfor i, statement in ipairs( dateClaims ) dolocal parsedTime = context.parseTimeFromSnak( statement.mainsnak );if ( parsedTime ) thenreturn parsedTime;endendendend-- TODO: check other "end" properties-- no death dayreturn os.time() * 1000;endfunction getFlag( context, countryEntityId, actualDate )local countryEntity = mw.wikibase.getEntity( countryEntityId );if ( not countryEntity or not countryEntity.claims or not countryEntity.claims.P41 ) thenreturn nil;endlocal countryFlags = {};local flagImageStatements = countryEntity.claims.P41;for _, flagImageStatement in pairs( countryEntity.claims.P41 ) doif ( flagImageStatement.rank ~= 'deprecated' ) thenlocal flagImage;if ( flagImageStatement and flagImageStatement.mainsnak and flagImageStatement.mainsnak.datavalue and flagImageStatement.mainsnak.datavalue.value ) thenflagImage = flagImageStatement.mainsnak.datavalue.value;endlocal flagStartTime = -9223372036854775808;if ( flagImageStatement.qualifiersand flagImageStatement.qualifiers.P580and flagImageStatement.qualifiers.P580[1] ) then local parsedFlagStartTime = context.parseTimeFromSnak( flagImageStatement.qualifiers.P580[1] ); if ( parsedFlagStartTime ) then flagStartTime = parsedFlagStartTime; endendif ( flagImage ) thencountryFlags[ flagStartTime ] = flagImage;endendendlocal goodFlag = nil;if ( countryFlags ) thenlocal ordered_dates = {}for flagBeginDate in pairs(countryFlags) dotable.insert(ordered_dates, flagBeginDate)endtable.sort(ordered_dates)for i = 1, #ordered_dates dolocal flagBeginDate, flag = ordered_dates[i], countryFlags[ ordered_dates[i] ];if ( actualDate >= flagBeginDate ) thengoodFlag = flag;endendendif ( goodFlag ) thenreturn '[[File:' .. goodFlag .. '|20x15px|border]]';endreturn nil;endfunction p.formatCountryClaimWithFlag( 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 is missing') end;if (not statement) then error('statement not specified') end;local flag = nil;if ( statement.mainsnak and statement.mainsnak.datavalue and statement.mainsnak.datavalue.value and statement.mainsnak.datavalue.value["numeric-id"] ) thenlocal endDateTimestamp = calculateEndDateTimestamp( context, options, statement );flag = getFlag( context, 'Q' .. statement.mainsnak.datavalue.value["numeric-id"], endDateTimestamp );endif ( flag ) thenreturn flag .. '&nbsp;<span class="country-name">' .. context.formatStatementDefault( context, options, statement ) .. '</span>';endreturn '<span class="country-name">' .. context.formatStatementDefault( context, options, statement ) .. '</span>';endreturn p;