Jump to content

Module:Bharatpedia ads: Difference between revisions

no edit summary
(Created page with "------------------------------------------------------------------------------- -- Module:Wikipedia ads -- -- This module displays a random banner-style advert for a Wikipedia project, -- page or process. It implements Template:Wikipedia ads. ------------------------------------------------------------------------------- -- Set constants local LIST_MODULE = 'Module:Wikipedia ads/list' local DATA_MODULE = 'Module:Wikipedia ads/data' local p =...")
 
No edit summary
Line 1: Line 1:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
--                        Module:Wikipedia ads
--                        Module:Bharatpedia ads
--
--
-- This module displays a random banner-style advert for a Wikipedia project,
-- This module displays a random banner-style advert for a Bharatpedia project,
-- page or process. It implements [[Template:Wikipedia ads]].
-- page or process. It implements [[Template:Bharatpedia ads]].
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------


-- Set constants
-- Set constants
local LIST_MODULE = 'Module:Wikipedia ads/list'
local LIST_MODULE = 'Module:Bharatpedia ads/list'
local DATA_MODULE = 'Module:Wikipedia ads/data'
local DATA_MODULE = 'Module:Bharatpedia ads/data'


local p = {}
local p = {}
Line 14: Line 14:


local function addWarning(msg)
local function addWarning(msg)
table.insert(warnings, msg)
  table.insert(warnings, msg)
end
end


local function makeWikilink(page, display)
local function makeWikilink(page, display)
return string.format('[[%s|%s]]', page, display)
  return string.format('[[%s|%s]]', page, display)
end
end


local function makeUrlLink(url, display)
local function makeUrlLink(url, display)
url = tostring(url)
  url = tostring(url)
return string.format('[%s %s]', url, display)
  return string.format('[%s %s]', url, display)
end
end


local function colorText(s, color)
local function colorText(s, color)
return string.format('<span style="color:%s">%s</span>', color, s)
  return string.format('<span style="color:%s">%s</span>', color, s)
end
end


local function getImageData(args)
local function getImageData(args)
-- This function gets an image data from the data module. It also tracks
  -- This function gets an image data from the data module. It also tracks
-- whether the image data choice was random.
  -- whether the image data choice was random.
local data = mw.loadData(DATA_MODULE)
  local data = mw.loadData(DATA_MODULE)


local function getSomeImageData(id, param)
  local function getSomeImageData(id, param)
id = tonumber(id) or id
    id = tonumber(id) or id
local someImageData = data.ids[id]
    local someImageData = data.ids[id]
if someImageData then
    if someImageData then
return someImageData
      return someImageData
else
    else
addWarning(string.format("ID '%s' does not exist", tostring(id)))
      addWarning(string.format("ID '%s' does not exist", tostring(id)))
return nil
      return nil
end
    end
end
  end


-- Get the image data of the ad to display.
  -- Get the image data of the ad to display.
local imageData, isRandom
  local imageData, isRandom
if args.ad then
  if args.ad then
imageData = getSomeImageData(args.ad, 'ad')
    imageData = getSomeImageData(args.ad, 'ad')
if not imageData then
    if not imageData then
return nil
      return nil
end
    end
isRandom = false
    isRandom = false
else
  else
local imageDataArray, length
    local imageDataArray, length
if args[1] then
    if args[1] then
imageDataArray = {}
      imageDataArray = {}
for i, id in ipairs(args) do
      for i, id in ipairs(args) do
imageDataArray[#imageDataArray + 1] = getSomeImageData(id, i)
        imageDataArray[#imageDataArray + 1] = getSomeImageData(id, i)
end
      end
length = #imageDataArray
      length = #imageDataArray
if length < 1 then
      if length < 1 then
return nil
        return nil
end
      end
else
    else
imageDataArray = data.list
      imageDataArray = data.list
length = data.noAds
      length = data.noAds
end
    end
assert(length >= 1, string.format(
    assert(length >= 1, string.format(
'no ads were found in [[%s]]',
      'no ads were found in [[%s]]',
DATA_MODULE
      DATA_MODULE
))
    ))
isRandom = length > 1
    isRandom = length > 1
if isRandom then
    if isRandom then
math.randomseed(os.clock() * 1000000000)
      math.randomseed(os.clock() * 1000000000)
imageData = imageDataArray[math.random(length)]
      imageData = imageDataArray[math.random(length)]
else
    else
imageData = imageDataArray[1]
      imageData = imageDataArray[1]
end
    end
end
  end


-- Check that the image data has the required fields. We have already
  -- Check that the image data has the required fields. We have already
-- checked the ID in the data module.
  -- checked the ID in the data module.
for i, field in ipairs{'image', 'link'} do
  for i, field in ipairs{'image', 'link'} do
assert(imageData[field], string.format(
    assert(imageData[field], string.format(
"Invalid image data in [[%s]]; table with ID '%s' has no '%s' field",
      "Invalid image data in [[%s]]; table with ID '%s' has no '%s' field",
LIST_MODULE, tostring(imageData.id), field
      LIST_MODULE, tostring(imageData.id), field
))
    ))
end
  end


return imageData, isRandom
  return imageData, isRandom
end
end


local function renderAd(imageData, args, title, isRandom)
local function renderAd(imageData, args, title, isRandom)
local width = tonumber(args.width) or 468
  local width = tonumber(args.width) or 468
local maxWidth = width + 9
  local maxWidth = width + 9
local linkColor = args.linkcolor or '#002bb8'
  local linkColor = args.linkcolor or '#002bb8'


-- Table root
  -- Table root
local root = mw.html.create('table')
  local root = mw.html.create('table')
root
  root
:addClass('plainlinks qxz-ads')
    :addClass('plainlinks qxz-ads')
:css('color', args.color or '#555555')
    :css('color', args.color or '#555555')
:css('border', 'none')
    :css('border', 'none')
:css('background', args.background)
    :css('background', args.background)
:css('line-height', '1em')
    :css('line-height', '1em')
:css('font-size', '90%')
    :css('font-size', '90%')
:css('display', 'block')
    :css('display', 'block')
:css('overflow', 'auto')
    :css('overflow', 'auto')
:css('max-width', maxWidth .. 'px')
    :css('max-width', maxWidth .. 'px')
if args.float then
  if args.float then
root:css('float', args.float)
    root:css('float', args.float)
root:css('margin', args.margin)
    root:css('margin', args.margin)
else
  else
root:css('margin', args.margin or '0 auto')
    root:css('margin', args.margin or '0 auto')
end
  end
 
-- Image row
  -- Image row
root
  root
:tag('tr')
    :tag('tr')
:tag('td')
      :tag('td')
:attr('colspan', 2)
        :attr('colspan', 2)
:css('border', 'none')
        :css('border', 'none')
:wikitext(string.format(
        :wikitext(string.format(
'',
          '',
imageData.image,
          imageData.image,
width,
          width,
imageData.link,
          imageData.link,
imageData.link
          imageData.link
))
        ))
 
-- Links row
  -- Links row
if not args.nolinks then
  if not args.nolinks then
local linksRow = root:tag('tr')
    local linksRow = root:tag('tr')


-- Wikipedia ads link
    -- Bharatpedia ads link
linksRow
    linksRow
:tag('td')
      :tag('td')
:css('border', 'none')
        :css('border', 'none')
:wikitext(makeWikilink(
        :wikitext(makeWikilink(
'Template:Wikipedia ads',
          'Template:Bharatpedia ads',
colorText('Wikipedia ads', linkColor)
          colorText('Bharatpedia ads', linkColor)
))
        ))


-- File info, purge and ID
    -- File info, purge and ID
local links = {}
    local links = {}
links[#links + 1] = makeWikilink(
    links[#links + 1] = makeWikilink(
':File:' .. imageData.image,
      ':File:' .. imageData.image,
colorText('file info', linkColor)
      colorText('file info', linkColor)
)
    )
if args.showpurge or isRandom then
    if args.showpurge or isRandom then
links[#links + 1] = makeUrlLink(
      links[#links + 1] = makeUrlLink(
title:fullUrl{action = 'purge'},
        title:fullUrl{action = 'purge'},
colorText('show another', linkColor)
        colorText('show another', linkColor)
)
      )
end
    end
links[#links + 1] = '#' .. tostring(imageData.id)
    links[#links + 1] = '#' .. tostring(imageData.id)
linksRow
    linksRow
:tag('td')
      :tag('td')
:css('text-align', 'right')
        :css('text-align', 'right')
:css('border', 'none')
        :css('border', 'none')
:wikitext(table.concat(links, ' – '))
        :wikitext(table.concat(links, ' – '))
end
  end


return tostring(root)
  return tostring(root)
end
end


local function renderWarnings(args, title)
local function renderWarnings(args, title)
if #warnings < 1 then
  if #warnings < 1 then
return nil
    return nil
end
  end


-- Error list
  -- Error list
local root = mw.html.create('div')
  local root = mw.html.create('div')
:css('width', '468px')
    :css('width', '468px')
if args.float then
  if args.float then
root
    root
:css('float', args.float)
      :css('float', args.float)
:css('clear', 'both')
      :css('clear', 'both')
else
  else
root:css('margin', '0 auto')
    root:css('margin', '0 auto')
end
  end
local list = root:tag('ul')
  local list = root:tag('ul')
:addClass('error')
    :addClass('error')
:css('font-size', '90%')
    :css('font-size', '90%')
for _, msg in ipairs(warnings) do
  for _, msg in ipairs(warnings) do
list
    list
:tag('li')
      :tag('li')
:wikitext(string.format(
        :wikitext(string.format(
'Wikipedia ads error: %s ([[Template:Wikipedia ads#Errors|help]]).',
          'Bharatpedia ads error: %s ([[Template:Bharatpedia ads#Errors|help]]).',
msg
          msg
))
        ))
end
  end


-- Category. We use [[Module:Category handler]] for its blacklist.
  -- Category. We use [[Module:Category handler]] for its blacklist.
local mCatHandler = require('Module:Category handler')
  local mCatHandler = require('Module:Category handler')
local category = mCatHandler._main{
  local category = mCatHandler._main{
all = '[[Category:Wikipedia ads templates with errors]]',
    all = '[[Category:Bharatpedia ads templates with errors]]',
nocat = args.nocat,
    nocat = args.nocat,
page = title and title.prefixedText
    page = title and title.prefixedText
}
  }


local ret = tostring(root)
  local ret = tostring(root)
if category then
  if category then
ret = ret .. category
    ret = ret .. category
end
  end
return ret
  return ret
end
end


function p._main(args, title)
function p._main(args, title)
title = title or mw.title.getCurrentTitle()
  title = title or mw.title.getCurrentTitle()
local ret = {}
  local ret = {}
local imageData, isRandom = getImageData(args)
  local imageData, isRandom = getImageData(args)
if imageData then
  if imageData then
ret[#ret + 1] = renderAd(imageData, args, title, isRandom)
    ret[#ret + 1] = renderAd(imageData, args, title, isRandom)
end
  end
ret[#ret + 1] = renderWarnings(args, title)
  ret[#ret + 1] = renderWarnings(args, title)
if #ret > 0 then
  if #ret > 0 then
return table.concat(ret)
    return table.concat(ret)
else
  else
return nil
    return nil
end
  end
end
end


function p.main(frame)
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
  local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Wikipedia ads'
    wrappers = 'Template:Bharatpedia ads'
})
  })
return p._main(args)
  return p._main(args)
end
end


return p
return p