Jump to content

Module:Football map

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jts1882 (talk | contribs) at 10:43, 12 May 2018 (image handling order of priority: template arg > module data > wikidata). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--require('Module:No globals')
-- All Lua modules on Wikipedia must begin by defining a variable that will hold their
-- externally accessible functions. They can have any name and may also hold data.
local p = {}

local stadiumDatabase = require( "Module:Football map/data" ) -- configuration module
                          
-- Add a function to the variable. These are callable in Wikipedia via the #invoke command.
-- "frame" will contain the data that Wikipedia sends this function when it is called. 
p.hello = function(frame) 

    -- Declare a local variable and assign data to it.
    local str = "Hello World!"  

    -- Quit this function and send the information in "str" back to Wikipedia.
    -- The "print" function is not allowed, so all output is accomplished via 
    -- returning strings in this fashion.
    return str    
end  -- End the function.

-- main function
p.main = function(frame) 
	str = p.getMapframeString()
	return frame:preprocess(str)   -- the mapframe needs to be preprocessed!!!!!
end  -- End the function.

--[[ function to construct mapframe string
	The appropriate string with JSON between mapframe elements is generated correctly (test a copy),
	but it is displaces as the string rather than embedding the map 
	i.e. it is not behaving like normal HTML
]]
p.getMapframeString = function(frame) 

    --mapString = '<mapframe text="London football stadia" width=800 height=650 align=left zoom=11 latitude=51.530 longitude=-0.16 >'
    
    mapWidth = mw.getCurrentFrame():getParent().args['width'] or "400"
    mapHeight = mw.getCurrentFrame():getParent().args['height'] or "300"
    mapLatitude = mw.getCurrentFrame():getParent().args['latitude'] or "51.5"
    mapLongitude = mw.getCurrentFrame():getParent().args['longitude'] or "-0.15"
    mapAlign = mw.getCurrentFrame():getParent().args['align'] or "right"
    mapText = mw.getCurrentFrame():getParent().args['text'] or ""
    mapZoom = mw.getCurrentFrame():getParent().args['zoom'] or "9"
    
    
    mapData = p.getStadiumJSON()
    local mapString = ""
     	
    if mapData ~= "" then

	    mapString = '<mapframe text="' .. mapText ..'" width=' .. mapWidth .. ' height=' .. mapHeight 
	    mapString = mapString .. ' align=' .. mapAlign .. ' zoom=' .. mapZoom
	    mapString =mapString .. ' latitude=' .. mapLatitude .. ' longitude=' .. mapLongitude .. ' >'
	   
	    mapString = mapString .. mapData .. '</mapframe>'
    end
    
    return mapString    

end  -- End the function.

-- function to construct JSON string
p.getStadiumJSON = function(frame) 

    -- now we need to iterate through the stadiumN parameters and get data for the feature markers
    local maxNumber = 200 -- maximum number looked for
    local mapData = ""
    local stadiumName = ""
    local clubName = ""
    
    -- default parameters for marker color, size and symbol (i.e. those without index suffix)
    local markerColor = mw.getCurrentFrame():getParent().args['color'] or  "0050d0"
    local markerSize = mw.getCurrentFrame():getParent().args['size'] or "medium"
    local markerSymbol = mw.getCurrentFrame():getParent().args['symbol'] or "soccer"
    
    local index=0
    while index < maxNumber do 
    	index = index + 1
	    stadiumName = mw.getCurrentFrame():getParent().args['stadium'..tostring(index)] or ""
	    local stadiumID = ""
	    local featureName = ""
	    local featureData = ""
	    local featureAlias =  ""
	    local featureDescription =  ""
	    local featureImage =  ""
	    local wdDescription = ""
	    local wdImage = ""
	    
	    -- if we have retrieved a name from the |stadiumN parameter
	    if stadiumName ~= "" then  

		    -- check the stadium list for name match
		    for _, params in pairs( stadiumDatabase.stadia ) do
		    	if stadiumName == params[1] then -- if we have a match from the list
		    		featureName = params[1]
		    		featureLatitude = params[2]
		    		featureLongitude = params[3]
		    		featureAlias = params[4]
		    		featureDescription = params[5]
		    		featureImage = '[[' .. params[6] .. ']]'
		    		break
		        end
		    end
	    else --end
	    -- if we have no name from the |stadiumN parameter, try the |clubN parameter
	    --if stadiumName == "" then  
	    	clubName = mw.getCurrentFrame():getParent().args['club'..tostring(index)] or ""
	    	
	    	-- let us assume the club name has a wikipedia page with the extact same name
			local wdId = mw.wikibase.getEntityIdForTitle(clubName)
        	if wdId and mw.wikibase.isValidEntityId( wdId ) then -- if valid id
	        	local item = mw.wikibase.getEntity(wdId)
	        	mw.logObject( mw.wikibase.getBestStatements( 'Q18721', 'P115' ) )
	        	--local statements = mw.wikibase.getBestStatements( 'Q18721', 'P115' )[1] 
	        	local statements = item:getBestStatements('P115')[1] --home venue
	            if statements ~= nil then -- check cordinates available
        	    	
					stadiumID = statements.mainsnak.datavalue.value.id
                    -- P115 doesn't seem to have the stadium name (P115 is type "Item") 
                    --- other properties (e.g. stadium name) would need to be got from the ID
                    stadiumName = mw.wikibase.getLabel( stadiumID  )
                    --stadiumName = clubName -- if the marker is to be labelled with club name
	        	end
            end	    	
	    end    
        -- if featureName set, we now have a stadium name match and coordinates from the stadium index list
        -- but what if the name doesn't match? Here we could seek info from wikiData
        
        if stadiumName ~= ""  and featureName == "" then 
        	-- 	have a look in wikidata
        	local WikidataId = mw.wikibase.getEntityIdForTitle(stadiumName)
        	
        	if stadiumID ~= "" then -- use stadium ID obtained for |clubN
        		WikidataId  = stadiumID 
        		-- currently stadiumName holds the club name
        		--stadiumName = stadiumName .. '(' .. mw.wikibase.getLabel( stadiumID  ) .. ')'
        	end 
            
        	if WikidataId and mw.wikibase.isValidEntityId( WikidataId ) then -- valid id
	        	local item = mw.wikibase.getEntity(WikidataId)
	        	--[[ statements of interest (datavalue element)
	        			P635 coordinate location (value.longitude/latitude)
	        			P18 image on commons (value, "File:value")
	        			P466 occupant (value.id) (use )
	        			P571 inception (value), P576 demolished (value)
	        			P1566 GeoNames ID (value, "geonames.org/value")
	        	]]
	        	local statements = item:getBestStatements('P625')[1] --coordinate location 
	            if statements ~= nil then -- check cordinates available
        	    	local coord = statements.mainsnak.datavalue.value
        	    	if type(coord.latitude) == 'number' and type(coord.longitude) == 'number' then 
		        	    -- add coordinate data from wikidata for unindexed stadium
			        	featureName = stadiumName
			        	featureLatitude = coord.latitude
			        	featureLongitude = coord.longitude
		        	end
	        	end
	        	statements = item:getBestStatements('P18')[1] --image
	        	if statements ~= nil then 
	               wdImage = "[[File:" .. statements.mainsnak.datavalue.value .. "]]"
	        	end
	        	wdDescription = " "
            end
        end
        
	    -- use specificed coordinates if provided (overriding above)
	    local latitude = mw.getCurrentFrame():getParent().args['latitude'..tostring(index)] or 0
	    local longitude = mw.getCurrentFrame():getParent().args['longitude'..tostring(index)] or 0
	    if latitude ~= 0 and longitude ~= 0 then -- if both explicitly set 
	    	featureLatitude = latitude
	    	featureLongitude= longitude
	    	featureName = stadiumName -- in case an location without wikipedia presence
	    end
	    	
	   
	    
	    if featureName ~= "" then 
	    	
	    	--check if current feature marker has specified color, size or symbol
	    	local color = mw.getCurrentFrame():getParent().args['color'..tostring(index)] or markerColor
	    	local symbol = mw.getCurrentFrame():getParent().args['symbol'..tostring(index)] or markerSymbol
	    	local size = mw.getCurrentFrame():getParent().args['size'..tostring(index)] or markerSize
	    	
	    	
	    	-- use specified description and image if provided
	    	local argDescription = mw.getCurrentFrame():getParent().args['description'..tostring(index)] or ""
	        local argImage = mw.getCurrentFrame():getParent().args['image'..tostring(index)] or ""
	    	if argDescription ~= "" then 
	    		featureDescription = argDescription 
	        else
	    	    featureDescription = wdDescription
	        end
	        
	    	if argImage ~= "" then 
	    		featureImage = '[[' .. argImage .. ']]'  -- priority for image from template argument
	    	else
	    		if featureImage == "" then -- if not set by data, use wikidata image
	    			featureImage = wdImage
	    	    end
	        end 
	    	
	    	
            featureName = '[[' .. featureName .. ']]'
            if clubName ~= "" then featureName = '[[' .. clubName .. ']] (' .. featureName ..')' end
    		if featureImage ~= "" then featureDescription = featureImage  .. featureDescription end

	    	
	    	--mapData = mapStadium1
	    	featureData = '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": ['
	    	.. featureLongitude .. ',' .. featureLatitude .. '] }, "properties": { "title": "' 
	    	.. featureName 	.. '",  "description": "'..featureDescription
	    	..'", "marker-symbol": "' .. symbol .. '", "marker-size": "' .. size .. '",  "marker-color": "' .. color .. '"   }  } '
	    	
	    	if index > 1 and mapData ~= "" then
	    	    mapData = mapData .. ',' .. featureData
	    	else
	    		mapData = featureData 
	    	end
	    else
	    	--mapData = '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": [-0.066417, 51.60475] }, "properties": { "title": "White Hart Lane (default)",  "description": "Stadium of Tottenham Hotspur F.C.", "marker-symbol": "soccer", "marker-size": "large",  "marker-color": "0050d0"   }  } '
		end
	 end -- end while loop
	 
	 if index > 1 then
	 	mapData = '[' .. mapData .. ']'
	 end
     
     return mapData
     
end -- End the function.

-- function to construct JSON string for WHL in London map
p.getTestJSONstring = function(frame) 
   
    return '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": [-0.066417, 51.60475] }, "properties": { "title": "White Hart Lane",  "description": "Stadium of Tottenham Hotspur F.C.", "marker-symbol": "soccer", "marker-size": "large",  "marker-color": "0050d0"   }  } '

end  -- End the function.

-- function to construct JSON string
p.getJSONstring = function(frame) 

    local stadiumName = mw.getCurrentFrame():getParent().args['stadium1'] or "default name"

    local str=stadiumName
    local jsonString = '{   "type": "Feature", '
    jsonString = jsonString .. ' "geometry": { "type": "Point", "coordinates": [-0.065833, 51.603333] }, '
    jsonString = jsonString .. ' "properties": {     "title": "[[White Hart Lane]]", '
    jsonString = jsonString .. ' "description": "[[File:White Hart Lane Aerial.jpg|150px]]Tottenham Hotspur Football Club (1899-2017)", '
    jsonString = jsonString .. ' "marker-symbol": "-number", "marker-size": "small", "marker-color": "dd50d0"  } } '
    
    --mapString = mapString ..  '</mapframe>'
   jsonString = '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": [-0.066417, 51.60475] }, "properties": { "title": "[[Northumberland Development Project]]",  "description": "[[File:NDProject2015.jpg|100px]]", "marker-symbol": "soccer", "marker-size": "large",  "marker-color": "0050d0"   }  } '
   
   jsonString = '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": [-0.066417, 51.60475] }, "properties": { "title": "title",  "description": "description", "marker-symbol": "soccer", "marker-size": "large",  "marker-color": "0050d0"   }  } '
    
    str = '<nowiki>' .. jsonString .. '</nowiki>'
    --str =  jsonString 

    return str    


end   -- End the function.

-- All modules end by returning the variable containing its functions to Wikipedia.
return p

-- We can now use this module by calling {{#invoke: HelloWorld | hello }}.
-- The #invoke command begins with the module's name, in this case "HelloWorld",
-- then takes the name of one of its functions as an argument, in this case "hello".