Module:EntityAbstract

-- local util_args = require('Module:ArgsUtil') local util_html = require("Module:HtmlUtil") local util_map = require('Module:MapUtil') local util_table = require("Module:TableUtil") local util_text = require("Module:TextUtil") local util_title = require("Module:TitleUtil") local util_vars = require("Module:VarsUtil") local i18n = require("Module:I18nUtil") local lang = mw.getLanguage('en') local Sprite = require('Module:Sprite').spriteImage local LCS = require('Module:LuaClassSystem') local Entity = LCS.class.abstract

Entity.imagesizes = { default = 25 }

-- the following are mandatory params when subclassed Entity.objectType = nil Entity.defaultlength = 'long' Entity.imagelength = 'link' Entity.imagesizes = { default = 25 } -- should never be modified only overwritten so it's ok to be outside of init

-- the following are all optional params to be defined when subclassed Entity.cssClass = nil Entity.defaultLink = nil Entity.filePattern = nil Entity.imageDisplayLength = nil

function Entity:init(str) if not str then self.is_nil = true return end local lookup = mw.loadData('Module:' .. self.objectType .. 'names') local vars = lookup[str and tostring(str):lower or 'DEFAULT'] if type(vars) == 'string' then vars = lookup[vars] end if not vars then vars = lookup.DEFAULT or {} self.unknown = true end self.vars = util_table.shallowClone(vars) end

function Entity:get(len) if self.is_nil then return nil end return self.vars[len or self.defaultlength] end

function Entity:exists return not self.is_nil end

function Entity:isknown if self.is_nil then return nil end return not self.unknown end

function Entity:tostring if self.is_nil then return nil end if self.vars then return self:get(self.defaultlength) end return 'Attempting to tostring something we cannot' end

function Entity:name(opts) if not opts then opts = {} end return self:get(opts.len or self.defaultlength) end

function Entity:flair(opts) if self.is_nil then return nil end local span = mw.html.create('span') :addClass('markup-object') :addClass(self.cssClass) span:wikitext(self:image(opts)) span:tag('span') :wikitext(self:name(opts)) return tostring(span) end

function Entity:flairlink(opts) if self.is_nil then return nil end if not opts then opts = {} end local span = mw.html.create('span') :addClass('markup-object') :addClass(self.cssClass) span:wikitext(self:image(opts)) :wikitext(self:name(opts)) return self:_link(tostring(span), opts) end

function Entity:image(opts) if self.is_nil then return nil end if not opts then opts = {} end return Sprite{ self:getSpriteKey(opts), type = self.objectType, -- size is needed to calculate position even if nosize is specified size = opts.size or self.imagesizes[opts.len or 'default'] or self.imagesizes.default, nosize = opts.nosize or self.nosize, class = self:getImageClass(opts), skiplookup = true, display = self.vars[self.imageDisplayLength or self.imagelength], } end

function Entity:file(opts) if self.is_nil then return end if not opts then opts = {} end return self:_file(opts, '') end

function Entity:filelink(opts) if self.is_nil then return end if not opts then opts = {} end return self:_file(opts, opts.link or self:get(self.imagelength)) end

function Entity:_file(opts, link, file) return (''):format(		file or self.filePattern:format(self:get(self.imagelength)),		link,		opts.size or self.imagesizes[opts.len or 'default'] or self.imagesizes.default	) end

function Entity:getSpriteKey(opts) return self.vars[self.imagelength] end

function Entity:getImageClass(opts) return nil end

function Entity:imagelink(opts) if not opts then opts = {} end return self:_link(self:image(opts), opts) end

function Entity:link(opts) if not opts then opts = {} end return self:_link(nil, opts) end

function Entity:_link(display, opts) local link = self:_getLinkTarget(link, opts) display = display or self:name(opts) return util_text.intLink(link, display) end

function Entity:_getLinkTarget(link, opts) if link then return link end if opts.link then return opts.link end return util_title.concatSubpage(		self:get(self.defaultlength),		opts.suffix	) end

function Entity:_or(entity) if self.is_nil then return entity end return self end

return Entity --