Zum Inhalt springen

Modul:GetItem/Test

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

Verwendung

Das Submodul ist eine Testversion des Moduls GetItem und nicht für den Produktivbetrieb geeignet. Es dient der Weiterentwicklung des Moduls GetItem, 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.

Testaufrufe

  • Bonusprogramm (P4446) der Thai Airways International (es existieren keine Sitelinks, nur eine Bezeichnung):
    • {{#invoke:GetItem/Test|getItems|Q188710|P4446}}
    • Ergebnis: Royal Orchid Plus
  • Allianz (P114) der Thai Airways International (Es existiert ein Link im deutschen Wikivoyage, aber kein Link erwünscht):
    • {{#invoke:GetItem/Test|getItems|Q188710|P159||label}}
    • Ergebnis: Star Alliance
  • „ist Teil von“ (P361) der Stadt Kamakura (Es existiert nur ein Link auf die lokale (japanische) Wikipedia):
    • {{#invoke:GetItem/Test|getItems|Q200267|P361}}
    • Ergebnis: Shōnan

Test in Quickbars

Quickbar Wie auch sonst üblich enthalten die Tags data-, class- und title-Attribute mit zusätzlichen Infos über die Wikidata-Suche bzw den Wikidata-Abgleich.

Thai Airways International
Bonusprogr.Royal Orchid Plus
AllianzStar Alliance
HauptverwaltungChatuchak
Tochterges.Thai Smile,Nok Air


Lufthansa
Bonusprogr.Miles & More
AllianzStar Alliance
HauptverwaltungKöln
TochtergesellschaftenAustrian Airlines,
Tyrolean Airways,
Swiss,
Swiss Global Air Lines,
Edelweiss Air,
Air Dolomiti,
Eurowings,
Lufthansa CityLine,
Germanwings,
Lufthansa Cargo


Dem. Rep. Kongo
Fahrseiterechts
Stecker-TypEurostecker,
Stecker-Typ D,
Stecker-Typ E
Notrufnummer(n)Notruf 113 (Feuerwehr),
114 (Polizei),
Notruf 118 (Polizei)
Hinweise
--[=[ GetItem 2023-02-12
* local GetItemsAsTable (internal use)
* getItems
* getItemsQuickbar
]=]
local GetItem = {}

-- categories and properties for data evaluation
-- its used to determine the official language of an country
local cg = mw.loadData( 'Module:CountryData/Geography' )
local gip = require ( 'Modul:GetItem/Properties' )

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


-- GetItemsAsTable() -> just for internal use. it delivers the requested items as a table
-- item: Wikidata-ID
-- property: requested property

-- it delivers the requested items as a table
-- label: Label of the object
-- sitelink: Sitelink in the following order:
--   - German Wikivoyage
--   - German Wikipedia
--   - local Wikipedia (if the country of an object is known)
-- wiki: code of the wiki ("wikivoyage" for the German Wikivoyage )
-- wikilanguage: language of the wiki ("de" for the German Wikivoyage )

local getItemsAsTable = function ( id, property )

   -- 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 = nilIf ( nilIf ( id, '' ), 'self' ) or mw.wikibase.getEntityIdForCurrentPage() or ''


   -- no Wikidata object
   if localID == '' then
      return {}, '', ''
   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

   -- property unknown in table "gip": category but continue
   if not gip[requestedProperty] then
      maintenanceCategory = maintenanceCategory .. gip.unknownProperty
   end

   -- getting the values
   local wdStatements = mw.wikibase.getBestStatements( localID, requestedProperty )

   -- variables to store the result
   local wdValues = {}
   local wdItem
   local hasEndDate = false

   -- setting the "property used" maintaining category
   if #wdStatements > 0  then
      maintenanceCategory = maintenanceCategory .. '[[Kategorie:Seiten, die die Wikidata-Eigenschaft ' .. requestedProperty .. ' benutzen]]'
   end

   -- no data? > setting the "no data" category
   if #wdStatements == 0  and gip[requestedProperty] ~= nil then
      maintenanceCategory = maintenanceCategory .. gip[requestedProperty].noData
   end

   -- running through the array and store it in a simple table
   for i, entry in pairs ( wdStatements ) do

      wdItem = {}

      -- Prüfen, ob Endzeittpunkt vorhanden ist
      hasEndDate = false
      if entry.qualifiers ~= nil then
         if entry.qualifiers.P582 ~= nil then
            hasEndDate = true
         end
      end

      -- check for wikibase-item
      if entry.mainsnak.datatype == 'wikibase-item' 
            and entry.mainsnak.datavalue ~= nil 
            and not ( hasEndDate )
      then

         -- getting the item
         wdItem.item = entry.mainsnak.datavalue.value.id

         -- getting the label
         wdItem.label = mw.wikibase.getLabel( entry.mainsnak.datavalue.value.id ) or ''

         -- getting the sitelink

         wdItem.wiki = ''
         wdItem.wikiLanguage = ''

         -- trying German Wikivoyage first
         wdItem.siteLink = mw.wikibase.getSitelink( entry.mainsnak.datavalue.value.id, 'dewikivoyage' ) or ''

         if wdItem.siteLink ~= '' then

            -- setting the wiki information
            wdItem.wiki = 'wikivoyage'
            wdItem.wikiLanguage = 'de'

         else

            -- trying the German Wikipedia
            wdItem.siteLink = mw.wikibase.getSitelink( entry.mainsnak.datavalue.value.id, 'dewiki' ) or ''

            if wdItem.siteLink ~= '' then

               -- setting the wiki information
               wdItem.wiki = 'wikipedia'
               wdItem.wikiLanguage = 'de'

            else

               -- trying the local Wikipedia
               -- first getting the country
               local wdCountry = {}
               local wdCountry = mw.wikibase.getBestStatements( localID, 'P17' )
               if #wdCountry > 0 then

                  -- there where empty values
                  if wdCountry[1].mainsnak.datavalue ~= nil then

                     maintenanceCategory = maintenanceCategory .. '[[Kategorie:Seiten, die die Wikidata-Eigenschaft P17 benutzen]]'
                     
                     if cg.countries[wdCountry[1].mainsnak.datavalue.value.id] ~= nil then 

                        local language = cg.countries[wdCountry[1].mainsnak.datavalue.value.id].lang or ''
                        wdItem.siteLink = mw.wikibase.getSitelink( entry.mainsnak.datavalue.value.id, language .. 'wiki' ) or ''

                        -- found a local wikipedia?
                        if wdItem.siteLink ~= '' then

                           -- setting the wiki information
                           wdItem.wiki = 'wikipedia'
                           wdItem.wikiLanguage = language

                        end

                     end -- if #wdIso3166 > 0

                  end -- if wdCountry[1].mainsnak.datavalue ~= nil

               end -- if #wdCountry > 0

            end -- if wdItem.siteLink ~= ''

         end -- if wdItem.siteLink ~= ''

         -- getting the qualifiers
         -- the qualifiers are set up in Modul:GetItem/Properties
         if gip[requestedProperty] ~= nil then 
            if gip[requestedProperty].qualifiers ~= nil then

               wdItem.qualifiers = {}

               -- looping over the rquested qualifiers
               for i, qualifier in ipairs( gip[requestedProperty].qualifiers ) do

                  if entry.qualifiers ~= nil then
                     if entry.qualifiers[qualifier] ~= nil then

                        -- Wikidata-Item
                        if entry.qualifiers[qualifier][1].datatype == 'wikibase-item' then
                           wdItem.qualifiers[i] = {}
                           wdItem.qualifiers[i].qualifier = qualifier
                           wdItem.qualifiers[i].value = mw.wikibase.getLabel ( entry.qualifiers[qualifier][1].datavalue.value.id ) or ''
                        end


                     end -- if entry.qualifiers.qualifier ~= nil
                  end -- if entry.qualifiers ~= nil
                  
               end -- for i, qualifier in ipairs( gip[requestedProperty].qualifiers )

              end -- if gip[requestedProperty].qualifiers ~= nil
           end -- f gip[requestedProperty] ~= nil

         -- adding the capital to the list
         table.insert( wdValues, wdItem )

      end -- if entry.mainsnak.datatype == 'wikibase-item' and entry.mainsnak.datavalue ~= nil and not ( hasEndDate )

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

   -- returning the items
   return wdValues, localID, maintenanceCategory
 
end


-- GetItems() 
-- gets the text of a property including its language
--   first parameters: see internal function
--   values:
--      - single: Only one (first) entry is shown
--      - all (standard): All entries ar shown
--   show:
--      - sitelink (standard): the sitelink with the label as link label
--      - label: just the label (removes tha category as well),
--   delimiter: delimiter between the numbers; standard is comma and breakable space
GetItem.getItems = function ( id, property, values, show, delimiter )

   -- returning String
   local returnString = ''

   -- values
   local requestedValues = coalesce ( nilIf ( values, '' ), 'all' )

   -- WD-Values
   local stringList = {}
   local wikidataID = ''
   local categories = ''
   stringList, wikidataID, categories = getItemsAsTable ( id, property )

   -- linkText
   local linkText = ''

   -- no delimiter in front of the first entry
   local stringDelimiter = ''

   -- qualifiers, if requested
   local stringQualifier = ''

   -- displaying the values
   for key, value in pairs( stringList ) do

      if value.qualifiers ~= nil then
         if #value.qualifiers > 0 then

            stringQualifier = ' ('

            for i = 1, #value.qualifiers do
               if i == 1 then 
                  stringQualifier = stringQualifier .. value.qualifiers[i].value
               else
                  stringQualifier = stringQualifier .. ', ' .. value.qualifiers[i].value
               end
            end

            stringQualifier = stringQualifier .. ')'

         end
      end

      if coalesce ( nilIf ( show, '' ), 'sitelink' ) == 'sitelink' and value.siteLink ~= '' then

         returnString = returnString .. stringDelimiter .. '[['

         if value.wiki == 'wikipedia' then
            returnString = returnString .. 'w:' .. value.wikiLanguage .. ':'
         end

         linkText = coalesce ( nilIf ( value.label, '' ), value.siteLink )

         returnString = returnString .. value.siteLink .. '|' .. linkText .. ']]' .. stringQualifier

      else -- showing just the label
         returnString = returnString .. stringDelimiter .. value.label .. stringQualifier
      end

      -- breaking, 
      if requestedValues ~= 'all' then
         break;
      end

      stringDelimiter = coalesce ( nilIf ( delimiter, '' ), ', ' )

   end

   returnString = returnString

   -- providing the list of capitals
   if coalesce ( show, '' ) == 'label' then
      return returnString, categories
   else
      return returnString .. categories, categories
   end

end


-- Get the string as wiki markup for quickbars (infoboxes)
GetItem.getItemsQuickbar = function ( id, property, wikiValue, label, delimiter )

   -- checking property
   if coalesce ( property, '' ) == '' then
      return ''
   end
   
   -- getting the link with the base function above
   local wikidataID
   local wikidataStrings = {}
   local wikidataCategories
   wikidataStrings, wikidataID, wikidataCategories = getItemsAsTable ( id, property )
   
   -- if it is taken from wikidata, this variable provides an additional class for: MediaWiki:Gadget-Wikidata-Content.css
   -- space is needed becaus its simply added to the existing class string
   local wikidataClass = ' voy-wikidata-content'

   -- comparing a possibly given value with Wikidata
   local category = ''

   -- is Wikidata value not available?
   local wikidataNoData = false
   if wikidataID == '' then
      if gip[property] ~= nil then
         category = gip[property].noData
         wikidataNoData = true
      end
      wikidataClass = ''
   end

   -- creating text for display
   local returnString = ''
   local stringDelimiter = ''
   local stringQualifier = ''

   -- Wikidate comparision tags
   -- is Wikidata available
   local wikidataAvailable = true

   -- is one of the entries equal to the wiki value?
   local wikidataOneIsEqual = false
   local wikidataEqualData = false

   -- are none of the entries equal to the wiki value?
   -- starting with false
   -- later initialised to true, when local wiki value is given
   local wikidataNothingIsEqual = false
   local wikidataNoEqualData = false

   -- processing nil value for wikiValue
   if wikiValue == nil then 
      wikiValue = '' 
   end
   
   -- wikivalues, that can be interpreted as "TRUE" are removed and Wikidata is used
   -- it's used to switch entries on and off in Quickbars
   if wikiValue =='yes'
      or wikiValue == 'y'
      or wikiValue == 'true'
      or wikiValue == 'wahr'
      or wikiValue == 'ja'
      or wikiValue == 'j'
   then
      wikiValue = ''
   end

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

   -- processing wikiValue
   if coalesce ( wikiValue, '' ) ~= '' then
      -- starting with TRUE by default
      -- set to FALSE if one value is equal
      wikidataNothingIsEqual = true
      wikidataNoEqualData = true
   end

   -- displaying the values and compare with Wikidata
   for key, value in pairs( wikidataStrings ) do

      if value.qualifiers ~= nil then
         if #value.qualifiers > 0 then

            stringQualifier = ' ('

            for i = 1, #value.qualifiers do
               if i == 1 then 
                  stringQualifier = stringQualifier .. value.qualifiers[i].value
               else
                  stringQualifier = stringQualifier .. ', ' .. value.qualifiers[i].value
               end
            end

            stringQualifier = stringQualifier .. ')'

         end
      end

      if coalesce ( nilIf ( show, '' ), 'sitelink' ) == 'sitelink' and value.siteLink ~= '' then

         returnString = returnString .. stringDelimiter .. '[['

         if value.wiki == 'wikipedia' then
            returnString = returnString .. 'w:' .. value.wikiLanguage .. ':'
         end

         linkText = coalesce ( nilIf ( value.label, '' ), value.siteLink )

         returnString = returnString .. value.siteLink .. '|' .. linkText .. ']]' .. stringQualifier

      else -- showing just the label
         returnString = returnString .. stringDelimiter .. value.label .. stringQualifier
      end

      -- comparing with Wikidata

      if coalesce ( wikiValue, '' ) ~= '' then
         if mw.ustring.gsub ( wikiValue, '[%[%]]', '' ) == value.siteLink or mw.ustring.gsub ( wikiValue, '[%[%]]', '' ) == value.label then
            wikidataOneIsEqual = true
         end
      end

      -- Setting the delimiter
      stringDelimiter = coalesce ( nilIf ( delimiter, '' ), ',<br />' )

      -- resetting the qualifier
      stringQualifier = ''

   end

   -- saving wikidata value in case of a local string is used
   local wikidataString = returnString

   -- do not state "different values", when no wikidata value is available
   if #wikidataStrings == 0 then
      wikidataNoEqualData = false
      wikidataClass = ''
   end

   -- Setting Wikidata categories
   if coalesce ( wikiValue, '' ) ~= '' then

      -- setting the categories
      if gip[property] ~= nil then

         if wikidataOneIsEqual then 
            category = category .. gip[property].equalData
            wikidataEqualData = true
         end

         if wikidataNothingIsEqual and #wikidataStrings > 0 then 
            category = category .. gip[property].noEqualData
            wikidataNoEqualData = true
         end

      end

      -- displaying the wikiValue
      returnString = wikiValue

      -- removing the WD-Class
      wikidataClass = ''

   end

   -- displaying only, if any data available
   local trClass = ''
   if returnString == '' then
      trClass = ' voy-qb-empty'
   end

   -- creating the table row 
   local tr = mw.html.create ( 'tr' )
   
   -- first table cell (heading)
   local trLabel = coalesce ( nilIf ( label, '' ), '&nbsp;' )
   if gip[property] then
        trClass = ' ' .. gip[property].cssClass .. trClass
        if trLabel == '&nbsp;' then
           trLabel = gip[property].label
        end
   end
   tr:addClass('voy-qb-item' .. trClass )
      :tag('th')
      :addClass('voy-qb-item-key')
      :wikitext(trLabel)
   
   -- second table cell (the requested number)
   if wikidataNoData then
      tr:tag('td')
         :addClass( 'voy-qb-item-value1 voy-qb-item-value-nowikidata' .. wikidataClass )
            :attr('data-wikidata-id',wikidataID)
            :attr('title','Keine Wert auf Wikidata')
         :wikitext( returnString )
   elseif wikidataEqualData then
      tr:tag('td')
            :addClass( 'voy-qb-item-value1 voy-qb-item-value-wikidata-equal' .. wikidataClass )
            :attr('data-wikidata-id',wikidataID)
            :attr('data-wikidata-value',wikidataString)
            :attr('title','Daten identisch - Angabe auf Wikidata: ' .. wikidataString)
            :wikitext( returnString )
   elseif wikidataNoEqualData then
      tr:tag('td')
            :addClass( 'voy-qb-item-value1 voy-qb-item-value-wikidata-noequal' .. wikidataClass )
            :attr('data-wikidata-id',wikidataID)
            :attr('data-wikidata-value',wikidataString)
            :attr('title','Daten unterschiedlich - Angabe auf Wikidata: ' .. wikidataString )
            :wikitext( returnString )
   else
      tr:tag('td')
            :addClass( 'voy-qb-item-value1' .. wikidataClass )
            :attr('data-wikidata-id',wikidataID)
            :attr('data-wikidata-value',wikidataString)
            :wikitext( returnString )
   end

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

-- Providing template access
local p = {}

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

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

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

return p