Module:Favilink Utility: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
mNo edit summary |
||
Line 6: | Line 6: | ||
local name | local name | ||
local fields = fieldsin or 'JoinKey=Page, DisplayName, Image, DisplayChar, PlainName' | local fields = fieldsin or 'JoinKey=Page, DisplayName, Image, DisplayChar, PlainName' | ||
local namespaces = '(_pageNamespace="0" OR _pageNamespace="10002")' | |||
searchtype = searchtype or 'PlainName' | searchtype = searchtype or 'PlainName' | ||
Line 21: | Line 21: | ||
end | end | ||
local result = cargo.query('GeneralData', fields, {where= | local result = cargo.query('GeneralData', fields, {where=namespaces .. wherestr, default=name, limit='1'} ) | ||
local resulttrue = 0 | local resulttrue = 0 | ||
Line 32: | Line 32: | ||
end | end | ||
result = cargo.query('GeneralData', fields, {where= | result = cargo.query('GeneralData', fields, {where=namespaces .. wherestr, default=name, limit='1'} ) | ||
if next(result) then | if next(result) then | ||
resulttrue = 1 | resulttrue = 1 |
Revision as of 19:26, 17 July 2020
local p = {}
local cargo= mw.ext.cargo
function p.main(frame, searchtype, fieldsin)
local name
local fields = fieldsin or 'JoinKey=Page, DisplayName, Image, DisplayChar, PlainName'
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 JoinKey="' .. name:gsub("'", "\'") .. '"'
end
local result = cargo.query('GeneralData', fields, {where=namespaces .. 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=namespaces .. 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 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[1]['PlainName']
end
end
function p.IDtopage(frame)
local name = frame
if frame.args ~= nil then
name = frame.args[1]
end
local result = p.main(name, 'ObjectID', '_pageName=Page')
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
return result[1]['Page']
end
end
return p