Module:Favilink Utility

From Caves of Qud Wiki
Revision as of 15:44, 28 October 2019 by Teamtoto (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

function p.main(frame, searchtype, fieldsin)

local name
local fields = fieldsin or '_pageName=Page, DisplayName, Image, DisplayChar, PlainName'

frame=mw.getCurrentFrame()

searchtype = searchtype or 'PlainName'
 
if frame.args ~= nil and frame.args ~= '' then
  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 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='_pageNamespace="0"' .. wherestr, default=name, limit='1'} )
  if next(result) then 
    resulttrue = 1
  end
else
  resulttrue = 1
end

if resulttrue == 1 then
  return result
else
  return nil
end

end

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

function p.IDtoname(frame)
  local result = p.main(frame, 'ObjectID', 'PlainName')
  if not next(result) then
    return name .. '(no results)'
  else 
    return result[1]['PlainName']
  end
end

function p.IDtopage(frame)
  local result = p.main(frame, 'ObjectID', '_pageName=Page')
  if not next(result) then
    return name .. '(no results)'
  else 
    return result[1]['Page']
  end
end

return p