Dokumentation för denna modul finns på /dok (redigera), /test


Används av Mall:antal uppslag. Kommer att sluta funka vid 500 språk.

-- only optional named parameters "list=" "report=" "format=" "omit=" "limit="
-- possible values in format string "k","n","N","a" spc/underscore comma cross

-- example syntax:
-- {{#invoke:catsummer|go}}
-- {{#invoke:catsummer|go|report=long}}
-- {{#invoke:catsummer|go|list=en,eo,id,sv|report=long}}
-- {{#invoke:catsummer|go|list=en,eo,id,smi-usm,sv|report=table}}
-- {{#invoke:catsummer|go|report=export|format=k,N,a#|omit=empty}}

local exporttbl = {}
local import_lang = require ("Modul:lang")

local function ucFirst(str)
  return mw.ustring.gsub(str, "^%l", mw.ustring.upper)
end

clang    = "spr" .. string.char(195,165) .. "k"
cucflang = "Spr" .. string.char(195,165) .. "k"
cbadlang = "ok" .. string.char(195,164) .. "nt " .. clang

function exporttbl.go (arxframent) -- main

  local arxsomons = 0

  local temp = 0

  local lng_codes = {} -- from "list=" or "Modul:lang"/"exportList"

  local par_list_codes = ""
  local par_report = ""
  local par_format = ""
  local strlngcode = ""
  local strlngnamelc = ""
  local strlngnameup = ""
  local strcatname = ""
  local stritem = ""
  local strret = ""

  local limit_que = 0
  local num_err = 0 -- 0 OK | 1 ano | 2 li | 3 for | 4 con | 5 bad co | 6 cost
  local panja = 0
  local index = 0
  local posi = 0
  local chx = 0
  local numcodelen = 0
  local count_in_cat = 0
  local sumo = 0

  local booreport_long = false -- from "report=long"
  local booreport_table = false -- from "report=table"
  local booreport_export = false -- from "report=export"
  local booomit_empty = false -- from "omit="
  local booomit_used = false -- from "omit="
  local boosuccesscost = false -- for "pcall"
  local boo_buy_entry = false
  local boo_success_form = false
  local boo_is_last = false

  arxsomons = arxframent.args -- "args" from our own "frame"
  if (arxsomons['caller']=="true") then
    arxsomons = arxframent:getParent().args -- "args" from caller's "frame"
  end--if

  while (true) do -- fake loop

    num_err = 1 -- prepare
    if (arxsomons[1]~=nil) then
      break -- no anonymous parameters tolerated
    end--if

    num_err = 2 -- prepare -- broken "list=" parameter
    temp = arxsomons["list"]
    if (type(temp)=="string") then
      panja = string.len(temp)
      if ((panja<2) or (panja>100000)) then
        break -- broken "list=" parameter
      end--if
      par_list_codes = temp
    end--if

    par_report = arxsomons["report"]
    booreport_long = (par_report=="long")   -- cannot fail
    booreport_table = (par_report=="table") -- cannot fail
    booreport_export = (par_report=="export") -- cannot fail

    par_format = "k," -- default: list of codes only
    num_err = 3 -- prepare
    temp = arxsomons["format"]
    if (type(temp)=="string") then
      panja = string.len(temp)
      if ((panja<2) or (panja>9)) then
        break -- format string
      end--if
      par_format = temp
    end--if

    booomit_empty = arxsomons["omit"]=="empty" -- cannot fail
    booomit_used = arxsomons["omit"]=="used" -- cannot fail

    limit_que = 999999 -- default: no limit
    temp = arxsomons["limit"]
    if (type(temp)=="string") then
      panja = string.len(temp)
      if ((panja>0) and (panja<7)) then -- 0...999'999
        limit_que = tonumber (temp) or 0
      end--if
    end--if

    num_err = 4 -- prepare -- conflicting parameters
    if booomit_used and (not booreport_export) then
      break -- doesn't make sense to omit used from summing or table
    end--if
    if booomit_empty and (not (booreport_export or booreport_table)) then
      break -- doesn't make sense to omit empty from summing
    end--if

    num_err = 0 -- prepare -- OK
    if (par_list_codes=="") then
      lng_codes = import_lang.exportList () -- get them all
    else
      index = 0 -- also needed far below for error complaint
      posi = 0
      panja = string.len(par_list_codes)
      while (true) do -- genuine inner loop over codes
        strlngcode = ""
        numcodelen = 0
        if (posi>=panja) then
          break -- inner loop for now
        end--if
        while (true) do -- genuine deep loop over octets
          if (posi>=panja) then
            break -- deep loop for now
          end--if
          chx = string.byte(par_list_codes,(posi+1),(posi+1))
          posi = posi + 1
          if (chx==44) then -- comma ","
            break -- deep loop for now
          end--if
          strlngcode = strlngcode .. string.char(chx)
          numcodelen = numcodelen + 1
        end--while -- deep loop
        if ((numcodelen<2) or (numcodelen>10)) then
          num_err = 2 -- broken "list=" parameter detected
          break -- inner loop
        end--if
        index = index + 1
        lng_codes[index] = strlngcode
      end--while -- genuine inner loop over codes
    end--if
    if (num_err~=0) then
      break -- error inside above loop
    end--if
    if (#lng_codes==0) then
      num_err = 2 -- broken "list=" parameter detected -- empty
      break
    end--if

    table.sort(lng_codes)

    num_err = 0 -- prepare
    index = 0 -- also needed far below for error complaint
    while (true) do -- genuine inner loop over codes
      strlngcode = lng_codes[index+1]
      if (strlngcode==nil) then
        break -- done
      end--if
      boo_is_last = not lng_codes[index+2]
      strlngnamelc = import_lang.getLanguage(strlngcode)
      if (strlngnamelc==cbadlang) then
        num_err = 5 -- bad code detected -- inner loop for now
        break
      end--if
      strlngnameup = ucFirst(strlngnamelc)
      if (index<limit_que) then
        strcatname = strlngnameup .. "/Alla uppslag"
        boosuccesscost,count_in_cat = pcall ( mw.site.stats.pagesInCategory , strcatname , "all" ) -- expensive and dangerous
        if (not boosuccesscost) then
          num_err = 6 -- cost limit exceeded -- inner loop for now
          break
        end--if
      else
        count_in_cat = 0 -- bad guess
      end--if
      sumo = sumo + count_in_cat
      boo_buy_entry = ((count_in_cat==0) and (not booomit_empty)) or ((count_in_cat~=0) and (not booomit_used))
      if (booreport_table and boo_buy_entry) then
        strret = strret .. '<tr><td>' .. strlngcode .. '</td><td>' .. strlngnamelc .. '</td><td>' .. tostring(count_in_cat) .. '</td></tr>'
      end--if
      if (booreport_export and boo_buy_entry) then
        stritem = "" -- filled in the deep loop
        posi = 0
        panja = string.len(par_format)
        while (true) do -- genuine deep loop over format string
          if (posi>=panja) then
            break -- deep loop for now
          end--if
          chx = string.byte(par_format,(posi+1),(posi+1))
          if (chx==95) then
            chx = 32 -- MediaWiki can trim off spaces
          end--if
          posi = posi + 1
          boo_success_form = false
          if ((chx==32) or (chx==44)) then -- space " " or comma "," copied
            if ((not boo_is_last) or (posi~=panja)) then
              stritem = stritem .. string.char(chx)
            end--if
            boo_success_form = true -- also for skipped separator
          end--if
          if (chx==35) then -- cross "#" translates to double LF EOL due HTML
            stritem = stritem .. "\n\n"
            boo_success_form = true
          end--if
          if (chx==107) then -- "k" -- "sv"
            stritem = stritem .. strlngcode
            boo_success_form = true
          end--if
          if (chx==110) then -- "n" -- "svenska"
            stritem = stritem .. strlngnamelc
            boo_success_form = true
          end--if
          if (chx==78) then -- "N" -- "Svenska"
            stritem = stritem .. strlngnameup
            boo_success_form = true
          end--if
          if (chx==97) then -- "a" -- "999999"
            stritem = stritem .. tostring (count_in_cat)
            boo_success_form = true
          end--if
          if (not boo_success_form) then
            num_err = 3
            break -- format string error detected
          end--if
        end--while -- genuine deep loop
        strret = strret .. stritem
      end--if (booreport_export and boo_buy_entry) then
      if (num_err~=0) then
        break -- error inside above loop
      end--if
      index = index + 1
    end--while -- genuine inner loop over codes

    break -- finally to join mark
  end--while -- fake loop -- join mark

  if (num_err==0) then
    if (booreport_long) then
      strret = 'Totalt ' .. tostring (index) .. ' ' .. clang .. ' och ' .. tostring (sumo) .. ' sidor i kategorierna.'
    end--if
    if (booreport_table) then
      strret = strret .. '<tr><th>Totalt</th><th>' .. tostring (index) .. ' ' .. clang .. '</th><th>' .. tostring(sumo) .. ' huvuduppslag</th></tr>'
      strret = '<table class="wikitable sortable"><tr><th>' .. cucflang .. 'kod</th><th>' .. cucflang .. 'namn</th><th>Antal huvuduppslag</th></tr>' .. strret ..'</table>'
    end--if
    if ((not booreport_long) and (not booreport_table) and (not booreport_export)) then
      strret = tostring (sumo) -- bare number
    end--if
  end--if

  if (num_err==1) then
    strret = 'No anonymous parameters appreciated.'
  end--if
  if (num_err==2) then
    strret = 'CSV syntax error in parameter "list=" at index ' .. tostring(index) .. '.'
  end--if
  if (num_err==3) then
    strret = 'Parameter "format=" is bad.'
  end--if
  if (num_err==4) then
    strret = 'Parameter "omit=" conflicts with parameter "list=".'
  end--if
  if (num_err==5) then
    strret = 'Bad language code "' .. strlngcode .. '".'
  end--if
  if (num_err==6) then
    strret = 'Cost limit exceeded with language code "' .. strlngcode .. '" at index ' .. tostring(index) .. '.'
  end--if
  if (num_err~=0) then
    strret = "ERROR: " .. strret
  end--if

  return strret

end

return exporttbl