Zum Inhalt springen

Modul:Quickbar Land

Aus Wikivoyage
Dokumentation für das Modul Quickbar Land[Ansicht] [Bearbeiten] [Versionsgeschichte] [Aktualisieren]

Dieses Modul erzeugt eine Quickbar für Ortsartikel Es gibt nur eine Funktion, die Erzeugung einer Infobox (Quickbar) für Länder. Eine detaillierte Dokumentation findest du auf der Vorlage Quickbar Land. Daher sind die Parameter hier nicht noch mal aufgelistet.

Das Modul erzeugt indirekt auch eine Reihe von Wartungskategorien durch die verwendeten Wikidata-Module. Diese kannst du auf den Modulseiten der eingebundenen Getxxx-Module einsehen.

Verwandte Module

Verwendung

  • Quickbar Land – Die Vorlage dient zur Ausgabe von Kurzdaten für Länder. Die Anzeige erfolgt als umflossene Box am rechten Rand, die Box ist 300px breit. Alle angezeigten Infos sind optional und zum großen Teil in der Lage, ihre Werte auch von Wikidata zu beziehen.

Benötigte weitere Module

Dieses Modul benötigt folgende weitere Module: CountryData/Geography • GetNumber • GetImage • GetItem • Währung/Länder • Yesno
Hinweise
--[=[ Quickbar Land 2023-11-10
]=]

local yesno    = require( 'Modul:Yesno' )
local pnumber  = require( 'Module:GetNumber' )
local images   = require( 'Module:GetImage' )
local items    = require( 'Module:GetItem' )

local cfgCurrencies = mw.loadData('Modul:Währung/Länder')
local cfgCountries  = mw.loadData('Modul:CountryData/Geography')

local lang = mw.language.new( 'de' )


-- returns nil, if both values are equal, otherwise the value
-- similar to the SQL function nullif()
local function nilIf ( value, equalValue )

   if ( value == nil ) then
      return nil
   elseif ( tostring ( value ) == tostring ( equalValue ) ) then
      return nil
   else
      return value
   end

end


-- returns the first value that is not nil
-- similar to the SQL function coalesce()
local function coalesce ( value1, value2, value3 )
   return value1 or value2 or value3
end


-- round function, which is not available in Lua
local _round = function  ( value, precision )
   local rescale = math.pow(10, precision or 0);
   return math.floor(value * rescale + 0.5) / rescale;
end


-- returns a manually created quickbar row
local function createTr ( text, label, trClass )

   -- check for valid text
   -- if there is no text, then display nothing
   if text == '' then 

      return ''

   else

      -- displaying the given info
      local tr = mw.html.create ( 'tr' )
      tr:addClass('voy-qb-item ' .. trClass )
         :tag('th')
         :addClass('voy-qb-item-key')
         :wikitext( label )
      tr:tag('td')
         :addClass( 'voy-qb-item-value1' )
         :wikitext( text )
      return tostring ( tr )
   end

end


local function checkMapObject ( id )
   local region = require ( 'Modul:Location map data ' .. id )
   if ( region ~= nil ) and ( region.data ~= nil ) then
      region.data['id'] = id
      return region
   else
      return nil
   end
end


local qbLand = {}

function qbLand.qb_land ( frame )
  
   -- copying and lowering the given parameters
   local templateArgs = {}
   for key,value in pairs ( frame.args ) do
      templateArgs[string.lower(key)] = value
   end
   for key,value in pairs ( frame:getParent().args ) do
      templateArgs[string.lower(key)] = value
   end

   -- variables for the whole quickbar content and the categories
   local display = ''
   local categories = ''
   
   -- contains some site.infos
   -- needed as fallback for the  parameter "Namen" and the location map
   local page = {}
   page = mw.title.getCurrentTitle()   

   -- getting or determining (if needed) the wikidata-ID
   if templateArgs.id == '' then templateArgs.id = nil end
   local qbID = nilIf ( nilIf ( templateArgs.id, 'self' ), '' ) or mw.wikibase.getEntityIdForCurrentPage() or ''

   -- getting object name
   -- uses WD sitelink and the page name as fallback
   local destinationName
   if qbID ~= '' then 
      destinationName = coalesce ( mw.wikibase.getSitelink( qbID, 'dewikivoyage' ), page.text, 'Ort')
   else
      destinationName = coalesce ( page.text, 'Ort')
   end

   -- determining the country 
   -- (not urgently needed, but if someone wants to develop country- specific quickbars)
   -- getting from Wikidata, if not provided
   -- if you want to save processing time, you should provide it
   -- e.g. in country specific infobox tables
   local qbIso3166 = templateArgs["iso-3166"] or ''
   local qbIso3166Class = '' 
   if qbIso3166 == '' then
      if qbID ~= '' then
         local wdCountry = mw.wikibase.getBestStatements( qbID, 'P17' )
         local wdIso3166 = {}
         if #wdCountry > 0 then
            -- there where empty values
            if wdCountry[1].mainsnak.datavalue ~= nil then
               wdIso3166 = mw.wikibase.getBestStatements( wdCountry[1].mainsnak.datavalue.value["id"], 'P297' )
               if #wdIso3166 > 0 then
                  if wdIso3166[1].mainsnak.snaktype == 'value' then
                     qbIso3166 = string.lower(wdIso3166[1].mainsnak.datavalue.value)
                     qbIso3166Class =  ' voy-qb-' .. qbIso3166
                  else
                  	 qbIso3166Class = ''
                  end
               else
                  qbIso3166Class = ''
               end
            else 
               qbIso3166Class = ''
            end
         end
      end
   else
      qbIso3166Class = ' voy-qb-' .. qbIso3166
   end

   -- DEBUG: showing the parameters
   -- it was just for development
   -- display = display .. '<br />Parameter:<br />' ..  mw.dumpObject(templateArgs)

   -- starting the quickbar table
   display = display .. '<table cellspacing="0" class="voy-qb voy-qb-right voy-qb-country' .. qbIso3166Class .. '">'

   -- heading
   -- is mandatory, even if you do not provide it, its shown (with the sitename)
   -- initialising with given heading
   local qbNamen = coalesce ( templateArgs["namen"], '' )
   
   -- if no heading is provided, get the sitename
   if qbNamen == '' then
      qbNamen = destinationName
   end
   
   -- creating the row with the heading
   local trHeader = mw.html.create ( 'tr' )
   trHeader:addClass( 'voy-qb-header' )
   trHeader:tag('td')
      :attr('colspan', 2 )
      :wikitext(qbNamen)

   -- adding it to the quickbar
   display = display .. tostring ( trHeader )

   -- the position map
   -- taken from Wikidata, if not provided
   display = display .. images.GetImage().getPositionMapQuickbar ( qbID, nil, nil, coalesce ( templateArgs["lage"], '' ) )
   
   -- creating the row with the flag heading
   trHeader = mw.html.create ( 'tr' )
   trHeader:addClass( 'voy-qb-header voy-qb-header-flag' )
   trHeader:tag('td')
      :attr('colspan', 2 )
      :wikitext('Flagge')

   -- adding it to the quickbar
   display = display .. tostring ( trHeader )

   -- flag
   -- always taken from Wikidata
   display = display .. images.GetImage().getEmblemsQuickbar ( qbID, 'F' )
   
   -- creating the row with the info heading
   trHeader = mw.html.create ( 'tr' )
   trHeader:addClass( 'voy-qb-header voy-qb-header-info' )
   trHeader:tag('td')
      :attr('colspan', 2 )
      :wikitext('Kurzdaten')

   -- adding it to the quickbar
   display = display .. tostring ( trHeader )

   -- capital
   display = display .. items.GetItem().getItemsQuickbar ( qbID, 'P36', coalesce ( templateArgs["hauptstadt"], '') )

   -- basic form of government
   display = display .. items.GetItem().getItemsQuickbar ( qbID, 'P122', coalesce ( templateArgs["staatsform"], '') )

   -- currency
   if cfgCurrencies[qbID] ~= nil then

      -- getting exchange rate
      local exchangeRate = 0
      local exchangeDate = ''
      local exchangeSource = ''
      local exchangeTable = {}

      -- trying Commons: Data:ECB euro foreign exchange reference rates.tab
      exchangeTable = mw.ext.data.get( 'ECB euro foreign exchange reference rates.tab' )

      for i, entry in pairs ( exchangeTable.data ) do
         if entry[1] == cfgCurrencies[qbID][4] then
            exchangeRate = _round ( tonumber ( entry[2] ), 3 )
            exchangeDate = entry[3]
            exchangeSource = exchangeTable.sources
            break
         end
      end

      -- trying Commons: Data:Xe.com exchange rates.tab
      if exchangeRate == 0 then

         exchangeTable = mw.ext.data.get( 'Xe.com exchange rates.tab' )

         for i, entry in pairs ( exchangeTable.data ) do
            if entry[1] == cfgCurrencies[qbID][4] then
               exchangeRate = _round ( tonumber ( entry[3] ), 3 )
               exchangeDate = entry[6]
               exchangeSource = exchangeTable.sources
               break
            end
         end

      end

      if exchangeRate > 0 and cfgCurrencies[qbID][4] ~= 'EUR' then

         -- formatting exchange rate
         local exchangeRateText = '<br />'

         -- first row (EUR > local)
         exchangeRateText = exchangeRateText .. '1&#x202F;EUR&#x202F;=&#x202F;' .. lang:formatNum ( exchangeRate ) ..  '&#x202F;' .. cfgCurrencies[qbID][4]

         -- second row (local > EUR)
         exchangeRateText = exchangeRateText .. '<br />'

         if exchangeRate > 10000 then
            exchangeRateText = exchangeRateText .. '100.000&#x202F;' .. cfgCurrencies[qbID][4] .. '&#x202F;=&#x202F;' .. lang:formatNum ( _round ( 100000 / exchangeRate, 3 ) ) ..  '&#x202F;EUR'
         elseif exchangeRate > 1000 then
            exchangeRateText = exchangeRateText .. '10.000&#x202F;' .. cfgCurrencies[qbID][4] .. '&#x202F;=&#x202F;' .. lang:formatNum ( _round ( 10000 / exchangeRate, 3 ) ) ..  '&#x202F;EUR'
         elseif exchangeRate > 100 then
            exchangeRateText = exchangeRateText .. '1.000&#x202F;' .. cfgCurrencies[qbID][4] .. '&#x202F;=&#x202F;' .. lang:formatNum ( _round ( 1000 / exchangeRate, 3 ) ) ..  '&#x202F;EUR'
         elseif exchangeRate > 10 then
            exchangeRateText = exchangeRateText .. '100&#x202F;' .. cfgCurrencies[qbID][4] .. '&#x202F;=&#x202F;' .. lang:formatNum ( _round ( 100 / exchangeRate, 3 ) ) ..  '&#x202F;EUR'
         elseif exchangeRate > 1 then
            exchangeRateText = exchangeRateText .. '10&#x202F;' .. cfgCurrencies[qbID][4] .. '&#x202F;=&#x202F;' .. lang:formatNum ( _round ( 10 / exchangeRate, 3 ) ) ..  '&#x202F;EUR'
         else
            exchangeRateText = exchangeRateText .. '1&#x202F;' .. cfgCurrencies[qbID][4] .. '&#x202F;=&#x202F;' .. lang:formatNum ( _round ( 1 / exchangeRate, 3 ) ) ..  '&#x202F;EUR'
         end

         -- adding the ref-Tag
         exchangeSource = frame:extensionTag{ name = 'ref', content = '[' .. exchangeSource .. ']' }

         display = display .. createTr ( cfgCurrencies[qbID][3] .. exchangeRateText, 'Währung<br />Kurs&#x202F;<span class="voy-small">(' .. lang:formatDate ( 'd.m.Y', exchangeDate ) .. ')' .. exchangeSource .. '</span>', 'voy-commons-content voy-qb-item-currency' )

      else

         display = display .. createTr ( cfgCurrencies[qbID][3] .. ' (' .. cfgCurrencies[qbID][4] .. ')', 'Währung', 'voy-qb-item-currency' )

      end
   end

   -- area
   display = display .. pnumber.GetNumber().getNumbersWithUnitQuickbar ( qbID, 'P2046', nil, 0, coalesce ( templateArgs["fläche"], '' ) )

   -- population
   display = display .. pnumber.GetNumber().getNumbersWithDateQuickbar ( qbID, 'P1082', coalesce ( templateArgs["bevölkerung"], '' ) )

   -- official language
   display = display .. items.GetItem().getItemsQuickbar ( qbID, 'P37', coalesce ( templateArgs["amtssprache"], ''), nil, ', ' )

   -- religion (no wikidata)
   if coalesce ( templateArgs["religion"], '' ) ~= '' then
      display = display .. createTr ( templateArgs["religion"], 'Religion', 'voy-qb-item-religion' )
   end

   -- electricity
   -- getting the Voltage
   local voltage = coalesce ( nilIf ( templateArgs["stromnetz"], '' ), pnumber.GetNumber().getNumbersWithUnit( qbID, 'P2884' ) , '' )

   -- getting the plugs
   -- when fetching the items as "label only", no categories are provided, they are saved in the second result
   -- "label only" has zo beused, because the result is used in a link and the categories destroy the link 
   -- normally getItems delivers complete links (if available) with the maintenance categories
   local plugs, plugsaCat = coalesce ( nilIf ( templateArgs["stecksysteme"], '' ), mw.ustring.gsub ( items.GetItem().getItems( qbID, 'P2853', nil, 'label', '/&#8203;' ), 'Stecker%-Typ ', '' ), '' )

   if voltage ~= '' or plugs ~= '' then

      local electricity = voltage
      local electricityLabel = ''
      if plugs ~= '' then
         if voltage ~= '' then
            electricity = electricity .. ';<br />[[Stromsysteme#Steckertypen|' .. plugs .. ']]'
            electricityLabel = '[[Stromsysteme|Strom/Stecker]]'
            categories = categories .. coalesce ( plugsaCat, '' )
         else
            electricity = '[[Stromsysteme#Steckertypen|' .. plugs .. ']]'
            electricityLabel = '[[Stromsysteme|Stecker]]'
         end
      else
         electricityLabel = '[[Stromsysteme|Stromnetz]]'
      end
      display = display .. createTr ( electricity, electricityLabel, 'voy-qb-item-electricity' )
   end

   -- calling code
   if cfgCountries.countries[qbID] ~= nil then
         display = display .. createTr ( cfgCountries.countries[qbID].cc, 'Telefonvorwahl', 'voy-qb-item-calling-code' )
   end
   
   if cfgCountries.adminEntities[qbID] ~= nil then
         display = display .. createTr ( cfgCountries.adminEntities[qbID].cc, 'Telefonvorwahl', 'voy-qb-item-calling-code' )
   end

   -- top level domain
   display = display .. items.GetItem().getItemsQuickbar ( qbID, 'P78', nil, nil, ', ' )

   -- timezone (no wikidata, for several reasons discussed in community)
   if coalesce ( templateArgs["zeitzone"], '' ) ~= '' then
      display = display .. createTr ( templateArgs["zeitzone"], 'Zeitzone', 'voy-qb-item-time-zone' )
   end

   -- finishing the HTML table
   display = display .. '</table>'

   return display .. categories
   
end

return qbLand