Modul:Wikidata/SisterCities: Versiyalar orasidagi farq
Oʻzbekcha Vikipediyadan import qilindi. | mw.wikibase stub: Ziyosferada Wikibase klienti yoʻq (Lua xatosini oldini olish) | ||
-- [zs-wikibase-guard] Ziyosfera has no Wikibase client; stub mw.wikibase so | |||
-- modules reading Wikidata degrade to nil instead of erroring. Auto-added on import. | |||
if not mw.wikibase then mw.wikibase = setmetatable({}, { __index = function() return function() return nil end end }) end | |||
local p = {}; | local p = {}; | ||
local flags = require( 'Module:Wikidata/Flags' ); | local flags = require( 'Module:Wikidata/Flags' ); | ||
$2, $3 dagi (joriy) koʻrinishi
-- [zs-wikibase-guard] Ziyosfera has no Wikibase client; stub mw.wikibase so
-- modules reading Wikidata degrade to nil instead of erroring. Auto-added on import.
if not mw.wikibase then mw.wikibase = setmetatable({}, { __index = function() return function() return nil end end }) end
local p = {};
local flags = require( 'Module:Wikidata/Flags' );
local delink
function p.formatSisterCitiesProperty( 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 ''; --TODO error?
end
local cityNames = {};
for i, claim in ipairs( claims ) do
if claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.value then
cityNames[ claim.id ] = mw.wikibase.label( claim.mainsnak.datavalue.value.id );
else
cityNames[ claim.id ] = '(none)';
end
end
local comparator = function( c1, c2 )
local n1 = cityNames[ c1.id ] or '';
local n2 = cityNames[ c2.id ] or '';
return n1 < n2;
end
table.sort( claims, comparator );
-- Barcha tasdiqlash arizalarini aylanib o‘tish va jadvalda rasmiylashtirilgan afzal ko‘rilgan
-- arizalarni to‘plash
local formattedClaims = {};
for i, claim in ipairs( claims ) do
local formattedStatement = context.formatStatement( options, claim );
-- bu yerda rasmiy ariza matni yoki xato satri qaytarilishi mumkin
-- xato satrini ko‘rsatuvchi nil qiymati hech qachon qaytarilmaydiganga o‘xshaydi
if formattedStatement then
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
-- jadvaldan rasmiy arizalar ro‘yxati bilan matnli satr hosil qilish
local out = mw.text.listToText( formattedClaims, '\n* ', '\n* ' );
if out ~= '' then
out = '* ' .. out;
if options.before then
out = options.before .. out;
end
if options.after then
out = out .. options.after;
end
end
return out
end
function findCountryStatements( placeId )
local countryStatements = {};
for _, countryStatement in pairs( mw.wikibase.getBestStatements( placeId, 'P17' ) ) do
if countryStatement and
countryStatement.mainsnak and
countryStatement.mainsnak.datavalue and
countryStatement.mainsnak.datavalue.value and
not ( countryStatement.qualifiers and countryStatement.qualifiers.P582 )
then
table.insert( countryStatements, countryStatement );
end
end
return countryStatements;
end
local flagsWikitextCache = {};
local countriesWikitextCache = {};
function p.formatSisterCityClaim( context, options, statement )
-- yoniq bo‘lsa, sanalar asosida filtrlash (masalan, through options.onlyCurrent)
local function getDate(snak)
if not snak or not snak.time then return nil end
return mw.text.split(snak.time, "T")[1] -- "2020-04-01" из "+2020-04-01T00:00:00Z"
end
local today = os.date("%Y-%m-%d")
if options.onlyCurrent == 'yes' then
local q = statement.qualifiers or {}
local start = q.P580 and q.P580[1] and getDate(q.P580[1])
local stop = q.P582 and q.P582[1] and getDate(q.P582[1])
-- start > today → aloqa tiklandi
if start and start > today then
return '' -- filtrlash
end
-- stop < today → yakunlangan
if stop and stop < today then
return '' -- filtrlash
end
end
local result = '';
if not statement or statement.mainsnak.snaktype ~= 'value' then
return result;
end
local countryStatements = findCountryStatements( statement.mainsnak.datavalue.value.id );
local flagImages = {};
for _, countryStatement in pairs( countryStatements ) do
local countryId = countryStatement.mainsnak.datavalue.value.id;
local flagImage = flagsWikitextCache[ countryId ];
if flagImage == nil then
flagImage = flags.getFlag( context, countryId, os.time() * 1000);
flagsWikitextCache[ countryId ] = flagImage;
end
if flagImage then
table.insert( flagImages, flagImage );
end
end
if #flagImages then
local flagText = mw.text.listToText( flagImages, ' / ', ' / ' )
result = result .. flagText .. ( flagText ~= '' and ' ' or '' );
end
result = result .. context.formatSnak( options, statement.mainsnak, {} );
local countryLinks = {};
for _, countryStatement in pairs( countryStatements ) do
local countryId = countryStatement.mainsnak.datavalue.value.id;
local countryText = countriesWikitextCache[ countryId ];
if countryText == nil then
countryText = context.formatSnak( {}, countryStatement.mainsnak, {} );
countriesWikitextCache[ countryId ] = countryText;
end
if countryText then
table.insert( countryLinks, countryText );
end
end
if #countryLinks then
result = result .. ', ' .. mw.text.listToText( countryLinks, ' / ', ' / ' );
end
if statement.qualifiers and statement.qualifiers.P580 then
if not delink then
-- [[Module:Delink]] contains require( 'strict' ), [[Module:Sources]] errors out with it
delink = function( text )
return mw.getCurrentFrame():expandTemplate{
title = 'Delink',
args = { text },
}
end
end
local snak = context.formatSnak( options, statement.qualifiers.P580[ 1 ] )
result = result .. ' (' .. delink( snak ) .. ')';
end
if options.references then
result = result .. context.formatRefs( options, statement );
end
return result;
end
return p;