Module:Favilink Utility: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
throw error is more than one result was found
Add Endgame namespace to search
 
(6 intermediate revisions by 2 users not shown)
Line 5: Line 5:
local name
local name
local fields = fieldsin or 'JoinKey=Page, DisplayName, PlainName, COALESCE(Image, DisplayChar)=Display'
local fields = fieldsin or 'JoinKey=Page, DisplayName, PlainName, COALESCE(Image, DisplayChar)=Display'
local namespaces = '(_pageNamespace="0" OR _pageNamespace="10002")'
local namespaces = '(_pageNamespace="0" OR _pageNamespace="10002" OR _pageNamespace="10006")'
searchtype = searchtype or 'PlainName'
searchtype = searchtype or 'PlainName'
name = mw.text.trim(frame)
name = mw.text.trim(frame)
Line 43: Line 43:
-- If there was a result, check if there were more than two found objects.
-- If there was a result, check if there were more than two found objects.
if resulttrue == 1 then
if resulttrue == 1 then
if #result > 1 then
if #result > 1 and searchtype == 'ObjectID' then
error("There was more than one result with that name![[Category:pages with favilink errors]]")
error("There was more than one result with the name " .. name .. "![[Category:pages with favilink errors]]")
end
end
return result[1]
return result[1]
Line 67: Line 67:
     return name .. '(no results)]][[Category:Pages with ID to Name errors'
     return name .. '(no results)]][[Category:Pages with ID to Name errors'
else  
else  
     return result[1]['PlainName']
     return result['PlainName']
end
end
end
end
Line 75: Line 75:
if frame.args ~= nil then
if frame.args ~= nil then
     name = frame.args[1]
     name = frame.args[1]
    overridelink = frame.args[2] and frame.args[2] == "overridelink" or False
end
end
local result = p.main(name, 'ObjectID', '_pageName=Page,PlainName')
local result = p.main(name, 'ObjectID', '_pageName=Page,PlainName')
Line 82: Line 83:
     return name .. '(no results)]][[Category:Pages with ID to Page errors'
     return name .. '(no results)]][[Category:Pages with ID to Page errors'
else  
else  
pagename = result[1]['Page']
pagename = result['Page']
plainname = result[1]['PlainName']
plainname = result['PlainName']
if string.lower(plainname) == string.lower(pagename) then
if string.lower(plainname) == string.lower(pagename) or not overridelink then
return pagename
return pagename
end
end
-- otherwise, use plainname for stuff like cybernetics credit wedge 2¢
-- otherwise, use plainname for stuff like cybernetics credit wedge 2¢
return result[1]['Page'] .. '|' .. result[1]['PlainName']
return result['Page'] .. '|' .. result['PlainName']
end
end
end
end


return p
return p

Latest revision as of 02:08, 8 April 2025


local p = {}
local cargo= mw.ext.cargo

function p.main(frame, searchtype, fieldsin)
	local name
	local fields = fieldsin or 'JoinKey=Page, DisplayName, PlainName, COALESCE(Image, DisplayChar)=Display'
	local namespaces = '(_pageNamespace="0" OR _pageNamespace="10002" OR _pageNamespace="10006")'
	searchtype = searchtype or 'PlainName'
	name = mw.text.trim(frame)
	local wherestr = ''

	if (searchtype == 'PlainName') then
		wherestr = ' AND PlainName="' .. name:gsub("'", "\'") .. '"'
	elseif (searchtype == 'ObjectID') then
		wherestr = ' AND BINARY ObjectID="' .. name:gsub("'", "\'") .. '"'
	elseif (searchtype == '_pageName') then
		wherestr = ' AND _pageName="' .. name:gsub("'", "\'") .. '"'
	end

	local result = cargo.query('GeneralData', fields, 
							   {where=namespaces .. wherestr,
								default=name,
								orderBy='_pageNamespace ASC'} )
	local resulttrue = 0

--[If there was no result, search by alias instead]
	if not next(result) then
		if searchtype == 'PlainName' then
    		wherestr = ' AND Aliases HOLDS "' .. name:gsub("'", "\'") .. '"'
		elseif searchtype == 'ObjectID' then
    		wherestr = ' AND IDAliases HOLDS "' .. name:gsub("'", "\'") .. '"'
		end 
		result = cargo.query('GeneralData', fields,
							 {where=namespaces .. wherestr,
							  default=name,
							  orderBy='_pageNamespace ASC'} )
		if next(result) then 
    		resulttrue = 1
		end
	else
		resulttrue = 1
	end
	-- If there was a result, check if there were more than two found objects.
	if resulttrue == 1 then
		if #result > 1 and searchtype == 'ObjectID' then
			error("There was more than one result with the name " .. name .. "![[Category:pages with favilink errors]]")
		end
		return result[1]
	else
		return nil
	end
end

function p.favilink(frame, searchtype)
	return p.main(frame, searchtype, '_pageName=Page, DisplayName, Image, DisplayChar, PlainName, COALESCE(Image, DisplayChar)=Display')
end

function p.IDtoname(frame)
	local name = frame
	if frame.args ~= nil then
    	name = frame.args[1]
	end
	local result = p.main(name, 'ObjectID', 'PlainName')
	if result == nil then
    	return name .. '(no results)]][[Category:Pages with ID to Name errors'
	elseif not next(result) then
    	return name .. '(no results)]][[Category:Pages with ID to Name errors'
	else 
    	return result['PlainName']
	end
end

function p.IDtopage(frame)
	local name = frame
	if frame.args ~= nil then
    	name = frame.args[1]
    	overridelink = frame.args[2] and frame.args[2] == "overridelink" or False
	end
	local result = p.main(name, 'ObjectID', '_pageName=Page,PlainName')
	if result == nil then
    	return name .. '(no results)]][[Category:Pages with ID to Page errors'
	elseif not next(result) then
    	return name .. '(no results)]][[Category:Pages with ID to Page errors'
	else 
		pagename = result['Page']
		plainname = result['PlainName']
		if string.lower(plainname) == string.lower(pagename) or not overridelink then
			return pagename
		end
		-- otherwise, use plainname for stuff like cybernetics credit wedge 2¢
		return result['Page'] .. '|' .. result['PlainName']
	end
end

return p