Модул:Wikidata/Medals

Ҳуҷҷатгузорӣ
local WDS = require( 'Module:WikidataSelectors' )local moduleDate = require( 'Module:Wikidata/date' )local awardsOrder = mw.ext.data.get( "Wikidata/awards order.tab" )local p = {}local config = {--Hide award with key QID if there is a reward with value QIDabsorption = {Q16675272 = 'Q654471',Q16481808 = 'Q8706404',Q1948730 = 'Q178473',Q1980962 = 'Q208167',}}--Get string with dates from qualifiers tablelocal function datesFromQualifier( context, options, qualifierId )local dates = {}local qualifiers = options.qualifiers[ qualifierId ]if qualifiers thenfor _, qualifier in pairs( qualifiers ) doif qualifier.datavalue thenlocal dateValue = moduleDate.formatDate( context, options, qualifier.datavalue.value )if dateValue thentable.insert( dates, dateValue )endendendendreturn table.concat( dates, ', ' )end--Property function for [[d:Property:P166]]function p.formatProperty( 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;if options.property then -- TODO: Почему тут может не быть property?claims = context.selectClaims( options, options.property );endif claims == nil thenreturn '' --TODO error?end-- Обход всех заявлений утверждения и с накоплением оформленых-- предпочтительных заявлений в таблице.local formattedData = {}for i, claim in ipairs( claims ) doif ( claim.mainsnak andclaim.mainsnak andclaim.mainsnak.datavalue andclaim.mainsnak.datavalue.type == 'wikibase-entityid') thenlocal valueId = claim.mainsnak.datavalue.value.idlocal formattedStatement = context.formatStatement( options, claim )-- здесь может вернуться либо оформленный текст заявления, либо строка ошибки, либо nilif ( formattedStatement and formattedStatement ~= '' ) thenformattedStatement = '<span class="wikidata-claim" data-wikidata-property-id="' .. string.upper( options.property ) .. '" data-wikidata-claim-id="' .. claim.id .. '">' .. formattedStatement .. '</span>'table.insert( formattedData, {id = valueId,html = formattedStatement,} )endendend-- Удаление дублей (медаль + звание -> звание)for i, awardData in ipairs( formattedData ) dolocal isAbsorptionFound = falseif config.absorption[ awardData.id ] thenlocal absorptionAwardId = config.absorption[ awardData.id ]for _, absorptionAwardData in ipairs( formattedData ) doif absorptionAwardData.id == absorptionAwardId thenisAbsorptionFound = truebreakendendendif isAbsorptionFound thentable.remove( formattedData, i )endend-- Сортировка медалей по старшинствуlocal orderedData = {}local lastValue;if ( type (awardsOrder) == 'table' ) then-- Если не отсохла stuctured datafor i, awardFields in ipairs( awardsOrder.data ) dolocal awardOrder = awardFields[ 1 ]if awardOrder == '-' then-- separatorif lastValue ~= '-' thentable.insert( orderedData, '<br>' )lastHeight = nilendelsefor k, awardData in ipairs( formattedData ) doif awardOrder == awardData.id and not awardData.used thentable.insert( orderedData, awardData.html )formattedData[ k ].used = trueendendendendendfor i, awardData in ipairs( formattedData ) doif not awardData.used thentable.insert( orderedData, awardData.html )endendlocal lastHeightfor i, awardHtml in ipairs( orderedData ) dolocal height = mw.ustring.match( awardHtml, 'x%d+px' )if height and lastHeight and height ~= lastHeight thentable.insert( orderedData, i, '<br>' )endlastHeight = heightend-- создание текстовой строки со списком оформленых заявлений из таблицыlocal out = mw.text.listToText( orderedData, options.separator, options.conjunction )if out ~= '' thenif options.before thenout = options.before .. outendif options.after thenout = out .. options.afterendendreturn outendlocal function getImageFromClaims( claims, property )local imageClaims = WDS.filter( claims, property )if imageClaims and #imageClaims thenfor i, claim in pairs( imageClaims ) doif claim.type == 'statement' and claim.mainsnak.snaktype == 'value' thenreturn claim.mainsnak.datavalue.valueendendendreturn nilend-- Получение изображения (планки или иконки) и его размераfunction p.getImageFromEntity( entityOrId, actualDate )local entity = entityOrIdif type( entityOrId ) == "string" thenentity = mw.wikibase.getEntity( entityOrId )endlocal image = nillocal size = 'x17px'local border = false-- получение изображения планки из элементаimage = getImageFromClaims( entity.claims, 'P2425' )if image thenborder = trueend-- получение иконки из элементаif not image thenimage = getImageFromClaims( entity.claims, 'P2910' )if image thensize = '40x40px'endendreturn image, size, borderend-- Получение категории лауреатов из элементаfunction p.getRecipientCategoryFromEntity( entityOrId )local entity = entityOrIdif type( entityOrId ) == "string" thenentity = mw.wikibase.getEntity( entityOrId )endlocal category = ''local categoryClaims = WDS.filter( entity.claims, 'P2517' )if categoryClaims and #categoryClaims thenfor i, claim in pairs( categoryClaims ) doif claim.type == 'statement' and claim.mainsnak.snaktype == 'value' thenlocal categoryLink = mw.wikibase.getSitelink( claim.mainsnak.datavalue.value.id )if categoryLink thencategory = category .. '[[' .. categoryLink .. ']]'endendendendreturn categoryend--Value function for [[d:Property:P166]]function p.formatValue( context, options, statement )local entityId = statement.idif not entityId thenreturn statementendlocal entity = mw.wikibase.getEntity( entityId )local label = entity:getLabel()local image, size, border = p.getImageFromEntity( entity )local recipientCategory = ''if not options.nocat and options.nocat ~= '' thenrecipientCategory = p.getRecipientCategoryFromEntity( entity )endlocal dates = ''if options.qualifiers thenlocal startDates = {}dates = datesFromQualifier( context, options, 'P580' )if dates ~= '' thenlocal endDates = datesFromQualifier( context, options, 'P582' )if endDates and endDates ~= '' thendates = dates .. ' — ' .. endDatesendelsedates = datesFromQualifier( context, options, 'P585' )endend-- получение ссылки по идентификатору и вывод планкиif image thenlocal link = entity:getSitelink()local out = '[[File:' .. imageif border == true thenout = out .. '|border'endout = out .. '|' .. size .. '|link='-- получение ссылки из родительского элемента-- для степеней обычно только одна общая статьяif not link thenlocal partOfClaims = WDS.filter( entity.claims, 'P361' ) -- часть отif not partOfClaims or #partOfClaims == 0 thenpartOfClaims = WDS.filter( entity.claims, 'P279' ) -- подкласс отendif partOfClaims and #partOfClaims thenfor i, claim in pairs( partOfClaims ) doif claim.type == 'statement' and claim.mainsnak.snaktype == 'value' thenlink = mw.wikibase.getSitelink( claim.mainsnak.datavalue.value.id )if link thenbreakendendendendendif link thenout = out .. linkelseout = out .. 'd:' .. entityIdendif label thenout = out .. '|' .. labelendout = out .. ']]'out = out .. recipientCategoryreturn outendlocal out = context.formatValueDefault( context, options, statement )if out and out ~= '' thenif dates ~= '' thenout = out .. ' (' .. dates .. ')'endreturn '<p style="text-align:left>' .. out .. recipientCategory .. '</p>'endreturn ''end--Table for documentationfunction p.renderDoc()local out = {}for i, awardFields in ipairs( awardsOrder.data ) dolocal awardId = awardFields[ 1 ]local link = '[[d:' .. awardId .. '|' .. awardId .. ']]'if awardId == '-' then-- separatortable.insert( out, '|' .. i .. '|| colspan="3" | ----' )elseif i > 350 then-- limitstable.insert( out, '|' .. i .. '|| … || … || … || …' )breakelselocal awardEntity = mw.wikibase.getEntity( awardId )local image, size, border = p.getImageFromEntity( awardEntity )if image thenimage = '[[File:' .. imageif border == true thenimage = image .. '|border'endimage = image .. '|' .. size .. ']]'elseimage = ''endlocal label = awardEntity:getLabel() or ''local article = awardEntity:getSitelink()if article thenif label == '' thenlabel = articleendlabel = '[[' .. article .. '|' .. label .. ']]'endlocal country = awardEntity:formatPropertyValues( 'P17' )[ 'value' ]table.insert( out, '|' .. i .. '||' .. link .. '||' .. image ..'||' .. label .. '||' .. country  )endendreturn '{| class="standard"\n' ..'! # !! Унсур !! Қитъа !! Ном !! Кишвар\n|-\n' ..table.concat( out, '\n|-\n' ) ..'\n|}'endreturn p