Modul:GetImage/Test

Aus Wikivoyage
Dokumentation für das Modul GetImage/Test[Ansicht] [Bearbeiten] [Versionsgeschichte] [Aktualisieren]

Verwendung

Das Submodul ist eine Testversion des Moduls GetImage und nicht für den Produktivbetrieb geeignet. Es dient der Weiterentwicklung des Moduls GetImage, damit sich Änderungen nicht auf alle Artikel auswirken, die das Modul benutzen.

Das Modul ist eine Spielwiese und nicht zum produktiven Einsatz vorgesehen. Der derzeitige Code ist eine finale Testversion zur Inbetriebnahme. Er enthält für Analysezwecke zusätzliche Wikidata-Abfragen und Ausgaben.

Einfache Abfragen

  • Cottbus: Bild: Cottbus 07-2017 img10 aerial.jpg
  • Cottbus: Flagge: Hissflagge der Stadt Cottbus.svg
  • Cottbus: Wappen: Wappen Cottbus.svg
  • Tong Lang Beach: Bild: Tong Lang Beach, Ko Lan 0001.jpg
  • Tong Lang Beach: Panoramaansicht: Tong Lang Beach, Ko Lan 0002.jpg


Hauptbild

Cottbus

{{#invoke:GetImage/Test|getMainImageQuickbar|Q3214}}


Cottbus

{{#invoke:GetImage/Test|getMainImageQuickbar|Q3214}}


Cottbus

Hauptbild unterdrückt
{{#invoke:GetImage/Test|getMainImageQuickbar|Q3214|no}}

AGC 748828

Kein Hauptbild vorhanden
{{#invoke:GetImage/Test|getMainImageQuickbar|Q92377276}}

Hoheitszeichen

Cottbus

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q3214|F}}

Cottbus

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q3214|C}}

Cottbus

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q3214|CF}}

Cottbus

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q3214|FCSL}}

Sabah

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q179029|CFSL}}

Sabah

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q179029|CFSL}}

Krabi

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q236769|FS}}

Krabi

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q236769|S}}

Flughafen Frankfurt

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q46033|L}}
{{#invoke:GetImage/Test|getMainImageQuickbar|Q46033}}

Nepal

{{#invoke:GetImage/Test|getEmblemsQuickbar|Q837|F}}

Lagekarte

Orthografische grün-graue Karte als Standard dargestellt, rote Sammlung als Fallback.

Benutzung voreingestellte Standardsammlung

Deutschland
Lagekarte

{{#invoke:GetImage/Test|getPositionMapQuickbar|Q183}}

voreingestellte Standardsammlung nocht vorhanden -> benutzung des Fallbacks (rot)

Polen
Lagekarte

{{#invoke:GetImage/Test|getPositionMapQuickbar|Q36}}

Anforderung des roten Sammlung

Indonesien
Lagekarte

{{#invoke:GetImage/Test|getPositionMapQuickbar|Q252|Q40426579}}

nichts angefordert, Standards beide nicht vorhanden

Brandenburg
Lagekarte

{{#invoke:GetImage/Test|getPositionMapQuickbar|Q1208}}

Manuell angegebene Datei

Brandenburg
Lagekarte

{{#invoke:GetImage/Test|getPositionMapQuickbar|Q1208|||File:132.Französisches Rondell Große Fontäne - Sanssouci Steffen Heilfort.JPG|300px]]}}

Imagemap

Sabah ohne Imagemap

Sabah
Lagekarte

{{#invoke:GetImage/Test|getPositionMapQuickbar|Q179029|||File:Sabah in Malaysia.svg}}

Sabah mit Imagemap

Sabah
SarawakLabuanSabahJohorMalakkaNegeri SembilanPutrajayaKuala LumpurSelangorPahangTerengganuKelantanPerakPerlisKedahThailandSingapurBruneiIndonesienIndonesienIndonesienIndonesienIndonesienIndonesienIndonesienPhilippinenPhilippinen

{{#invoke:GetImage/Test|getPositionMapQuickbar|Q179029|||File:Sabah in Malaysia.svg|my}}

Sabah mit Imagemap und individueller Breite

Sabah
SarawakLabuanSabahJohorMalakkaNegeri SembilanPutrajayaKuala LumpurSelangorPahangTerengganuKelantanPerakPerlisKedahThailandSingapurBruneiIndonesienIndonesienIndonesienIndonesienIndonesienIndonesienIndonesienPhilippinenPhilippinen

{{#invoke:GetImage/Test|getPositionMapQuickbar|Q179029||200px|File:Sabah in Malaysia.svg|my}}


Hinweise
--[=[ getImage 2022-11-10
*
* getImagesAsTable (internal use)
* 
* getMainImageQuickbar ( id, localImage )
* getEmblemsQuickbar ( id, emblems )
* getPositionMapQuickbar ( id, collectionCategory, widthPositionMap, localPositionMap, imageMap, qbFrame )
* 
]=]
local GetImage = {}


-- categories and properties for data evaluation
local gip = require ( 'Modul:GetImage/Properties' )

-- local tables > maybe moving into a separate module later
-- categories for data evaluation
local wikidataCat = {
   -- common wikidata categories
   noProperty      = '[[Kategorie:Wikidata:Fehlende Eigenschaft]]',
   unknownProperty = '[[Kategorie:Wikidata:Unbekannte Eigenschaft]]'
}

-- 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

-- determines the length of a key-value table
-- the # operator does not work here
local function getTableLength(tbl)
  local getN = 0
  for n in pairs(tbl) do 
    getN = getN + 1 
  end
  return getN
end


--[[ 
- getImagesAsTable() -> just for internal use. 
- it delivers the images including qualifiers
- images are saved into two subtables
-    - "images" contains the images including the folowing qualifiers
-       - P585 (point of time)
-       - P195 (ID of the collection)
-    - "collections" contains the images with the Wikidata-ID of the collection as key
- ranking: options
-   - best (standard) : uses getBestStatement
-   - all: uses getStatementAll (is used for locator maps only, 
              because the wanted collection scheme is not alwas available
              with the bestStatemenmt function, e. g. Germany Q183)
--]]
local getImagesAsTable = function ( id, property, ranking )

   -- local variables
   -- ID of the item
   -- Determined, if not provided
   -- compatibility to existing modules and templates:
   -- some use the keyword "self" for using the own entity-ID
   local localID = coalesce ( nilIf ( nilIf ( id, 'self' ), '' ), mw.wikibase.getEntityIdForCurrentPage(), '' )

   -- initialising returning table
   local wdValues = {}
   wdValues.images = {}
   wdValues.collections = {}

   -- no Wikidata object
   if localID == '' then
      return wdValues, '', ''
   end
   
   -- maintenance categories
   local maintenanceCategory = ''

   -- property
   local requestedProperty = property or 'none';
   if string.sub(requestedProperty,1,1) ~= 'P' then
      requestedProperty = 'none'
   end

   -- no property given: category and exit
   if requestedProperty == 'none' then
      maintenanceCategory = maintenanceCategory .. gip.noProperty
      return {}, localID, maintenanceCategory
   end

   local wdStatements = {}

   -- getting the values
   if coalesce ( ranking, '' ) == 'all' then
      wdStatements = mw.wikibase.getAllStatements( localID, requestedProperty )
   else
      wdStatements = mw.wikibase.getBestStatements( localID, requestedProperty )
   end

   -- running through the array and store it in a simple table

   local entryValueImage
   for i, entry in ipairs ( wdStatements ) do

      -- check for number
      if entry.mainsnak.datatype == 'commonsMedia' then

         -- new data set
         entryValue = {}

         -- getting the image
         entryValue.image = entry.mainsnak.datavalue.value
         
         -- initialising qualifiers
         entryValue.year = 0
         
         -- read qualifiers
         if entry.qualifiers ~= nil then

            -- read P585-Qualifiers (date/time of validity)
            if entry.qualifiers.P585 ~= nil then

               if entry.qualifiers.P585[1].datavalue.value.precision == 9 then
                  entryValue.year = tonumber ( entry.qualifiers.P585[1].datavalue.value.time:sub( 2, 5 ) )
               end
               if entry.qualifiers.P585[1].datavalue.value.precision == 10 then
                  entryValue.year = tonumber ( entry.qualifiers.P585[1].datavalue.value.time:sub( 2, 5 ) )
               end
               if entry.qualifiers.P585[1].datavalue.value.precision == 11 then
                  entryValue.year = tonumber ( entry.qualifiers.P585[1].datavalue.value.time:sub( 2, 5 ) )
               end

               -- maintenance category
               maintenanceCategory = maintenanceCategory .. '[[Kategorie:Seiten, die die Wikidata-Eigenschaft P585 benutzen]]' 
            end

            -- read the P195-Qualifiers (collection)
            if entry.qualifiers.P195 ~= nil then

               -- adding to the images-subtable
               entryValue.collectionID = entry.qualifiers.P195[1].datavalue.value.id

               -- adding to the collection.subtable
               wdValues.collections[entry.qualifiers.P195[1].datavalue.value.id] = entry.mainsnak.datavalue.value

               -- maintenance category
               maintenanceCategory = maintenanceCategory .. '[[Kategorie:Seiten, die die Wikidata-Eigenschaft P195 benutzen]]'
            end

         end

         -- adding to the list
         table.insert( wdValues.images, entryValue )

      end -- if entry.mainsnak.datatype == 'commonsMedia'

   end -- for i, entry in ipairs ( wdStatements )

   -- adding usage category
   if #wdStatements > 0 then
      maintenanceCategory = maintenanceCategory .. '[[Kategorie:Seiten, die die Wikidata-Eigenschaft ' .. requestedProperty .. ' benutzen]]'
   end 

   -- adding missing data categories
   if #wdStatements == 0 then
      if gip[requestedProperty] ~= nil then
         maintenanceCategory = maintenanceCategory .. gip[requestedProperty].noData
      end
   end


   return wdValues, localID, maintenanceCategory
 
end


-- getImage() 
-- gets the images (filenames)
-- it fetches only a single entry, because its provided without wiki markup
-- it does not provide the wikidata usage category for the same reason
GetImage.getImage = function ( id, property )

   -- returning String
   local imageString = ''

   -- making empty parameters to nil
   if unit == '' then unit = nil end

   -- WD-Values
   local imageList = {}
   local wikidataID = ''
   local categories = ''
   imageList, wikidataID, categories = getImagesAsTable ( id, property )

   -- getting the filename, if at least one is returned
   if #imageList.images > 0 then
      imageString = imageList.images[1].image
   end 
   
   -- providing the image, not followed by categories, 
   -- because its just the filename which has to be packed into wiki markup later
   return imageString

end


-- provides the main image for quickbars
-- Only P18 is used for the main image
GetImage.getMainImageQuickbar = function ( id, localImage )
   
   -- making nil to empty string
   if localImage == nil then localImage = '' end

   -- getting the specific class for the item
   local trClass = gip.P18.cssClass or ''

   -- image from Wikidata
   if localImage == '' then

      -- getting the link with the base function above
      local wikidataImages = {}
      local wikidataID = ''
      local wikidataCategories = ''
      wikidataImages, wikidataID, wikidataCategories = getImagesAsTable ( id, 'P18' )

      local tr = mw.html.create ( 'tr' )

      -- no image? -> invisible row row
      if #wikidataImages.images == 0 then

         trClass = trClass .. ' voy-qb-empty'
         tr:addClass('voy-qb-item ' .. trClass )
            :tag('td')
               :attr('colspan','2')
               :attr('data-wikidata-id',wikidataID)
            :tag('div')
               :addClass('voy-qb-item-image')
               :wikitext('')

         -- returning the row
         return tostring ( tr ) .. wikidataCategories

      end

      -- creating the table row with the image
      tr:addClass('voy-qb-item ' .. trClass )
         :tag('td')
            :attr('colspan','2')
            :attr('data-wikidata-id',wikidataID)
         :tag('div')
            :addClass('voy-qb-item-image')
            :wikitext('[[File:' .. wikidataImages.images[1].image .. '|' .. gip.quickbarSettings.widthP18 .. ']]')

      -- returning the row
      return tostring ( tr ) .. wikidataCategories

   -- or local image
   else

      -- wikivalues, that can be interpreted as "FALSE" suppress the whole row
      if localImage =='no'
         or localImage == 'n'
         or localImage == 'false'
         or localImage == 'falsch'
         or localImage == 'nein'
         or localImage == '0'
      then
         return ''
      end

      -- creating the table row with the image
      local tr = mw.html.create ( 'tr' )
      tr:addClass('voy-qb-item ' .. trClass )
         :tag('td')
            :attr('colspan','2')
         :tag('div')
            :addClass('voy-qb-item-image')
            :wikitext(localImage)

      return tostring ( tr )

   end

end


-- provides flag, coat of arms, seal or/and logo in one row for quickbars
-- the emblems parameter describes selection and order of the elements
--   F: flag (P41)
--   C: coat of arms (P94)
--   S: seal (P158)
--   L: logo (P154)
GetImage.getEmblemsQuickbar = function ( id, emblems )

   -- set to empty string, if nil
   if emblems == nil then emblems = '' end

   -- break, if empty
   if emblems == '' then
      return ''
   end

   -- initialising the temporary variables for getting the images from Wikidata
   local wikidataImages = {}
   local wikidataID = ''
   local wikidataCategories = ''
   -- single category to collect categories on every loop run
   local wikidataCategory = ''

   -- contains the wikitext to be inserted into the TD-Tag
   local imageString = ''

   -- contains the width of an item, 
   -- Modul:GetImage/Properties 
   -- dependet from amount of items in a row
   local imageWidth = ''

   -- contains options as like the border incase of flags
   local imageOptions = ''

   -- contains a list of image specific classes for the row
   local trClass = ''

   -- contains error categories
   local errorCategory = ''

   -- running through the emblems parameter
   for c in emblems:gmatch"." do

      imageOptions = ''

      if c:upper() == 'F' then

         -- getting the link with the base function above
         wikidataImages, wikidataID, wikidataCategory = getImagesAsTable ( id, 'P41' )

         -- setting the border
         imageOptions = '|border'

         -- determining the width
         if string.upper(emblems) == 'F' then 
            imageWidth = gip.quickbarSettings.widthP41.single
         else
            imageWidth = gip.quickbarSettings.widthP41.twoItems 
         end

         -- setting the css class
         trClass = trClass .. ' ' .. coalesce ( gip.P41.cssClass, '' )

      elseif c:upper() == 'C' then

         -- getting the link with the base function above
         wikidataImages, wikidataID, wikidataCategory = getImagesAsTable ( id, 'P94' )

         -- determining the width
         if string.upper(emblems) == 'C' then 
            imageWidth = gip.quickbarSettings.widthP94.single
         else
            imageWidth = gip.quickbarSettings.widthP94.twoItems 
         end

         -- setting the css class
         trClass = trClass .. ' ' .. coalesce ( gip.P94.cssClass, '' )

      elseif c:upper() == 'S' then

         -- getting the link with the base function above
         wikidataImages, wikidataID, wikidataCategory = getImagesAsTable ( id, 'P158' )

         -- determining the width
         if string.upper(emblems) == 'S' then 
            imageWidth = gip.quickbarSettings.widthP158.single
         else
            imageWidth = gip.quickbarSettings.widthP158.twoItems 
         end

         -- setting the css class
         trClass = trClass .. ' ' .. coalesce ( gip.P158.cssClass, '' )

      elseif c:upper() == 'L' then

         -- getting the link with the base function above
         wikidataImages, wikidataID, wikidataCategory = getImagesAsTable ( id, 'P154' )

         -- determining the width
         if string.upper(emblems) == 'L' then 
            imageWidth = gip.quickbarSettings.widthP154.single
         else
            imageWidth = gip.quickbarSettings.widthP154.twoItems 
         end

         -- setting the css class
         trClass = trClass .. ' ' .. coalesce ( gip.P154.cssClass, '' )

      else

         -- error handling for unknown chracters
         errorCategory = '[[Kategorie:Quickbar:Unbekannter Parameter für Hoheitszeichen]]'

      end

      -- got image? -> add it
      if #wikidataImages.images > 0 then
         imageString = imageString .. '[[File:' .. wikidataImages.images[1].image .. '|' .. imageWidth .. imageOptions .. '|class=notpageimage]]'
         wikidataCategories = wikidataCategories .. wikidataCategory
      end

   end

   -- if not any item available, doing nothing
   if imageString == '' then
      return ''
   end

   -- creating the table row with the image(s)
   local tr = mw.html.create ( 'tr' )
   tr:addClass('voy-qb-item' .. trClass )
      :tag('td')
         :attr('colspan','2')
         :attr('data-wikidata-id',wikidataID)
      :tag('div')
         :addClass('voy-qb-item-image')
         :wikitext(imageString)

   -- returning the row
   return tostring ( tr ) .. wikidataCategories

end



-- provides the position map in quickbars
-- it's the map with a highlighted region, not the map with a pin
-- it can fetch a map thats a part of a requested collection
-- an imagemap can be added to the map image
GetImage.getPositionMapQuickbar = function ( id, collectionCategory, widthPositionMap, localPositionMap, imageMap, qbFrame )
   
   -- making nil to empty string
   if collectionCategory == nil then collectionCategory = '' end
   if widthPositionMap == nil then widthPositionMap = '' end  
   if localPositionMap == nil then localPositionMap = '' end
   if imageMap == nil then imageMap = '' end

   -- setting standard values
   if widthPositionMap == '' then widthPositionMap = gip.quickbarSettings.widthP242 end

   -- getting the specific class for the item
   local trClass = gip.P242.cssClass

   -- final content to be returned
   local wikidataCategories = ''
   local wikidataID = 'null'
   local finalMap = ''

   -- image from Wikidata
   if localPositionMap == '' then

      -- getting the link with the base function above
      local wikidataImages = {}
      wikidataImages, wikidataID, wikidataCategories = getImagesAsTable ( id, 'P242', 'all' )

      -- no image? -> no row
      if #wikidataImages.images == 0 then
         return wikidataCategories
      end

      -- trying to find the position map that ispart of the requested collection
      -- if not, take the first one
      local filePositionMap = ''

      -- are collections available at all?
      if getTableLength ( wikidataImages.collections ) > 0 then

         -- is a collection requested and availabe?
         if ( collectionCategory ~= '' ) and ( wikidataImages.collections[collectionCategory] ~= nil ) then

            filePositionMap = wikidataImages.collections[collectionCategory]

         -- if not requested, trying to find one of the standard collections, given in Modul:GetImage/Properties
         else

            -- running reversly trough the standard requests
            for i = 1, #gip.P242.collections do
               if wikidataImages.collections[gip.P242.collections[#gip.P242.collections + 1 - i]] ~= nil then
                  filePositionMap = wikidataImages.collections[gip.P242.collections[#gip.P242.collections + 1 - i]]
               end 
            end

            -- nothing found? > take the first one
            if filePositionMap == '' then
               filePositionMap = wikidataImages.images[1].image
            end

         end

      else
         -- if not, just take the first position map
         filePositionMap = wikidataImages.images[1].image
      end
      
      finalMap = 'File:' .. filePositionMap

   -- or local image
   else

      finalMap = localPositionMap

   end

   -- Imagemap wanted?
   if imageMap ~= '' then

      local displayImagemapText = ''
      local imagemapText = mw.loadData ( 'Modul:Imagemap/' .. imageMap )

      for k,v in pairs ( imagemapText ) do
         displayImagemapText = displayImagemapText .. v .. '\n'
      end

      finalMap = qbFrame:extensionTag( 'imagemap', finalMap .. '|' .. widthPositionMap .. '\n' .. displayImagemapText )

   else

      finalMap = '[[' .. finalMap .. '|' .. widthPositionMap .. '|class=notpageimage|Lagekarte]]'

   end

   -- creating the table row with the image
   local tr = mw.html.create ( 'tr' )
   tr:addClass('voy-qb-item ' .. trClass )
      :tag('td')
      :attr('colspan','2')
         :attr('data-wikidata-id',wikidataID)
         :tag('div')
         :addClass('voy-qb-item-image')
         :wikitext(finalMap)

   -- returning the row
   return tostring ( tr ) .. wikidataCategories

end

-- Providing template access
local p = {}

function p.getImage( frame )
   return GetImage.getImage( frame.args[ 1 ], frame.args[ 2 ] ) or ""
end

function p.getMainImageQuickbar( frame )
   return GetImage.getMainImageQuickbar ( frame.args[ 1 ], frame.args[ 2 ] ) or ""
end

function p.getEmblemsQuickbar( frame )
   return GetImage.getEmblemsQuickbar ( frame.args[ 1 ], frame.args[ 2 ] ) or ""
end

function p.getPositionMapQuickbar( frame )
   return GetImage.getPositionMapQuickbar ( frame.args[ 1 ], frame.args[ 2 ], frame.args[ 3 ], frame.args[ 4 ], frame.args[ 5 ], frame ) or ""
end

-- for usage in other modules
-- using it the following way:
--
-- local getImage = require( 'Module:GetImage' )
-- foo = getImage.GetImage().xxx( id, lang )
function p.GetImage()
   return GetImage
end

return p