Module:Sandbox/Matroc2

Documentation for this module may be created at Module:Sandbox/Matroc2/doc

-- Beta functions getname2 and getinternal links

local p = {}

local function loadwikinames()
    local wikinames = {}	
	wikinames[1] = "wikisource"
	wikinames[2] = "wikibooks"
	wikinames[3] = "wikinews"
	wikinames[4] = "wikiquote"
	wikinames[5] = "wikivoyage"
	wikinames[6] = "wiki"
	return wikinames
end  

function p.getnamedesc(frame)
	local name=frame.args[1]
	local id=mw.wikibase.getEntityIdForTitle(name)
	local desc=mw.wikibase.getDescription( id )
	if desc == "" or desc==nil then desc = "No description!" end
	if id == "" or id == nil then return "" end
	return name .. " - " .. id .. " - " .. desc
end

function p.getname(frame)
	local name=mw.wikibase.getEntityIdForTitle(frame.args[1])
	if name == "" or name == nil then return "" end
	return name
	end

function p.getname2(frame)
	local names=frame.args[1]
	local name = ""
	local image = ""
	local items = {}
	local index = 1
    local id = ""
	local latitude = ''
	local longitude = ''
	local marker = ''
    local entity = ''
    local data = ""
    local separator = '\n'
    local lang = ''
    local wiki = ''
    local wikiname = ''
    
 for str in string.gmatch(names,"([^"..separator.."]+)") do
 	if str ~= nil and str ~= "" then
 		str = string.gsub(str,"^%s+","")
     	str = string.gsub(str,"%s+$","")
     	if str ~= "" then
	       items[index] = str
	       index = index + 1
	       end
	    end
	end    

 for i=1,#items do
 	name = items[i]
 	id = ""
    wikiname = '' 	
	id=mw.wikibase.getEntityIdForTitle(items[i])
    if id == nil then id = '' end
    
if id ~= nil and id ~= '' then
    entity = mw.wikibase.getEntityObject(id)	

    if entity == nil then
		latitude = ""
	    longitude = ""
	    end
	    
	local claims = entity.claims
	if claims == nil then
		latitude = ""
		longitude = ""
	    end	
--	if claims.P625 ~= nil then
if pcall(function () t =claims.P625 end ) then
 		if pcall(function () t =entity.claims.P625[1].mainsnak.datavalue.value.latitude end ) then		
		latitude = entity.claims.P625[1].mainsnak.datavalue.value.latitude
		else latitude = "" end
 		if pcall(function () t =entity.claims.P625[1].mainsnak.datavalue.value.longitude end ) then		
		longitude = entity.claims.P625[1].mainsnak.datavalue.value.longitude
		else longitude = "" end
end

--	if claims.P18 ~= nil then
--		image = entity.claims.P18[1].mainsnak.datavalue.value
--		if image == nil then
--			image = ""
--
-- Replace above due to error of getting a nil value
-- Lua error in line 3210: attempt to index field 'datavalue' (a nil value).
image=""
if pcall(function () t =claims.P625 end ) then
 		if pcall(function () t =entity.claims.P18[1].mainsnak.datavalue.value end ) then		
		      image = entity.claims.P18[1].mainsnak.datavalue.value
		    else
		    	image = ""
			end		   

end

		
	end

    wikiname = ""	
	lang = mw.language.getContentLanguage(id).code
	wiki = lang .. "wiki"
	if pcall(function()t1 = entity:getSitelink("wiki") end) then	
        wikiname = entity:getSitelink(wiki) or ""
    else
    	wikiname = ""
    end

if id == "" then wikiname = "" end
if id ~= nil and id ~= "" then
			name = "[[" .. name .. "]]"
end
    marker = '* {{see | name=' .. name .. " | lat=" .. latitude .. " | long=" .. longitude .. " | image=" .. image .. " | wikipedia=" .. wikiname .. " | wikidata=" .. id .. "}}"
    data = data .. marker .. "\n"
    marker = ''
    name = ''
    latitude = ''
    longitude = ''
    image= ''
    id = ''
    wikiname = ''
	
end
    
    return data
    end

-- Load a page and scan it for internal links
-- change process to do match pairs -- is much simpler -- FIX

function p.getinternallinks(frame)
		local page = frame.args[1] or "Main Page"
        local title = mw.title.new(page)
        local displayactual = frame.args['option'] or "yes"
        if title == nil then return end

        if title.id == 0 then
            return "Page does not exist!"
        end

        local data = title:getContent()
		
		if data == nil or data == "" then
			return
		end

		local tt = {}
		local count = 0
		local output = ""
		local separator = "@@@@@"
		local newstr = ""		
			

		data = string.gsub(data,"\n","@@@@@")								--change \n to separator @@@@@
		data = string.gsub(data,"@","BULLET")								--because I use @@@@@ as a separator changing @ to BULLET
		data = string.gsub(data,"%s+"," ")									--change multiple spaces to a space
		data = string.gsub(data,"%[%[","@@@@@[[")							--change [[ to separator @@@@@[[
		data = string.gsub(data,"%]%]","]]@@@@@")							--change ]] to separator ]]@@@@@
		data = string.gsub(data,"@@@@@@@@@@","@@@@@")						--change ]] to separator ]]@@@@@

		for str in string.gmatch(data,"([^"..separator.."]+)") do

			if str ~= nil and str ~= "" then

-- Get rid of File,file,Image,image
 str = string.gsub(str,"%[%[[fF]ile.*",'')
 str = string.gsub(str,"%[%[[iI]mage.*",'')

				str = string.gsub(str,'%s*$','')       					-- drop ending space
				str = string.gsub(str,'^%s+','')
					if string.find(str, '^%[%[') == 1 then			    -- Key is to have [[ at beginning
					    str = string.gsub(str,'%_'," ")
						str = string.gsub(str,'%%2C',",")
						str = string.gsub(str,'%s+%|%s+','|')
						str = string.gsub(str,'%[%[%#','[[' .. page .. '#')
						
-- If plain [[something]] then change it into [[something|something]]
	if string.find(str, '^%[%[.*%|.*%]%]') == 0 then
       str = string.gsub(str,"(%[%[)(.*)(%]%])","%1%2|%2%3")   -- Change [[something]] to  [[something|something]] 
  end						
						
-- Display only actual link make it an option
if displayactual == "yes" then
	str = string.gsub(str,"%|.*%]%]","]]")
end
					    count = count + 1
--					    localstr = str.gsub(str,'^.*|','')
                        localstr = str.gsub(str,'|.*','')
                        localstr = string.upper (localstr)

						tt[count] = localstr .. "@-@" .. str
						end
				end
		end

		table.sort(tt)
		local previous = ""

		for key,value in pairs(tt) do
value = string.gsub(value,'^.*@-@','')
			if value ~= previous then
                output = output .. value .. " -- "
			end
			    previous = value
		end		

		output = string.gsub(output,"BULLET","@")
		output = string.gsub(output," -- $","")
	return output
end


return p