Module:Favilink Utility: Difference between revisions

(Created page with "local p = {} local cargo= mw.ext.cargo function p.main(frame, searchtype, fieldsin) local name local fields = fieldsin or '_pageName=Page, DisplayName, Image, DisplayChar, P...")
 
m (Reverted edits by Egocarib (talk) to last revision by Teamtoto)
Tag: Rollback
 
(27 intermediate revisions by 2 users not shown)
Line 3: Line 3:


function p.main(frame, searchtype, fieldsin)
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")'
searchtype = searchtype or 'PlainName'
name = mw.text.trim(frame)
local wherestr = ''


local name
if (searchtype == 'PlainName') then
local fields = fieldsin or '_pageName=Page, DisplayName, Image, DisplayChar, PlainName'
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


frame=mw.getCurrentFrame()
local result = cargo.query('GeneralData', fields,  
 
  {where=namespaces .. wherestr,
searchtype = searchtype or 'PlainName'
default=name,
orderBy='_pageNamespace ASC'} )
if frame.args ~= nil and frame.args ~= '' then
local resulttrue = 0
  if frame.args[1] ~= nil and frame.args[1] ~= '' then
  name = mw.text.trim(frame:preprocess(frame.args[1]))
  modifier = mw.text.trim(frame:preprocess(frame.args[2]))
  else
    error "There is no argument specified! [[Category:pages with favilink errors]]"
  end
end
 
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='_pageNamespace="0"' .. wherestr, default=name, limit='1'} )
local resulttrue = 0


--[If there was no result, search by alias instead]
--[If there was no result, search by alias instead]
if not next(result) then
if not next(result) then
  if searchtype == 'PlainName' then
if searchtype == 'PlainName' then
    wherestr = ' AND Aliases HOLDS "' .. name:gsub("'", "\'") .. '"'
    wherestr = ' AND Aliases HOLDS "' .. name:gsub("'", "\'") .. '"'
  elseif searchtype == 'ObjectID' then
elseif searchtype == 'ObjectID' then
    wherestr = ' AND IDAliases HOLDS "' .. name:gsub("'", "\'") .. '"'
    wherestr = ' AND IDAliases HOLDS "' .. name:gsub("'", "\'") .. '"'
  end  
end  
 
result = cargo.query('GeneralData', fields,
  result = cargo.query('GeneralData', fields, {where='_pageNamespace="0"' .. wherestr, default=name, limit='1'} )
{where=namespaces .. wherestr,
  if next(result) then  
  default=name,
    resulttrue = 1
  orderBy='_pageNamespace ASC'} )
  end
if next(result) then  
else
    resulttrue = 1
  resulttrue = 1
end
end
else
 
resulttrue = 1
if resulttrue == 1 then
end
  return result
-- If there was a result, check if there were more than two found objects.
else
if resulttrue == 1 then
  return nil
if #result > 1 and searchtype == 'ObjectID' then
end
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
end


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


function p.IDtoname(frame)
function p.IDtoname(frame)
  local result = p.main(frame, 'ObjectID', 'PlainName')
local name = frame
  if not next(result) then
if frame.args ~= nil then
    return name .. '(no results)'
    name = frame.args[1]
  else  
end
    return result[1]['PlainName']
local result = p.main(name, 'ObjectID', 'PlainName')
  end
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
end


function p.IDtopage(frame)
function p.IDtopage(frame)
  local result = p.main(frame, 'ObjectID', '_pageName=Page')
local name = frame
  if not next(result) then
if frame.args ~= nil then
    return name .. '(no results)'
    name = frame.args[1]
  else  
    overridelink = frame.args[2] and frame.args[2] == "overridelink" or False
    return result[1]['Page']
end
  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
end


return p
return p

Latest revision as of 14:49, 27 April 2022


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")'
	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