Módulo:AutRules

A documentação para este módulo pode ser criada em Módulo:AutRules/doc

local p = {}

function p.Rules(frame)
  -- Define o alvo das regras
  local target = frame:getParent().args[1] or frame.args[1]
  if target == "" or target == nil then
    target = mw.uri.decode(frame:callParserFunction("#urlget", "fullpagename"))
  end
  if target == "" or target == nil then
    target = mw.title.getCurrentTitle().prefixedText
  end
  -- Semantic Scribunto foi carregado?
  if not mw.smw then
    return "mw.smw module not found"
  end
  -- Define variáveis básicas
  local info, a, b
  local infos = {}
  -- Funções básicas
  local function ucfirst(str)
    return (str:gsub("^%l", string.upper))
  end
  -- Record status
  local recordStatus =
    frame:expandTemplate {
    title = "Field data string",
    args = {"000", "5", "1", property = "Leader", fullpagename = target}
  } or false
  if recordStatus == "n" then
    a = "Segundo o Líder, este é um registro <b>novo</b> (não um alterado ou revisado, expandido, excluído, etc.)"
    info = string.format("* %s.", a)
    table.insert(infos, info)
  elseif recordStatus == "c" then
    a = "Segundo o Líder, este é um registro <b>alterado ou revisado</b> (não um novo, expandido, excluído, etc.)"
    info = string.format("* %s.", a)
    table.insert(infos, info)
  elseif recordStatus == "a" or recordStatus == "d" then
    a = "Segundo o Líder, este é um registro <b>expandido</b> ou <b>excluído</b> (não novo)"
    info = string.format("* %s.", a)
    table.insert(infos, info)
  end
  -- Encoding level
  local encodingLevel =
    frame:expandTemplate {
    title = "Field data string",
    args = {"000", "17", "1", property = "Leader", fullpagename = target}
  } or false
  if encodingLevel == "n" then
    a = "Segundo o Líder, este registro de autoridade está <b>completo</b>"
    info = string.format("* %s.", a)
    table.insert(infos, info)
  elseif encodingLevel == "o" then
    a = "Segundo o Líder, este registro de autoridade está <b>incompleto</b>"
    info = string.format("* %s.", a)
    table.insert(infos, info)
  end
  -- Explica a sintaxe '(Biblioteca)NúmeroDeControle'
  local f001 =
    frame:expandTemplate {
    title = "Field data string",
    args = {"001", "0", "", property = "Control field data", fullpagename = target} or ""
  }
  local f003 =
    frame:expandTemplate {
    title = "Field data string",
    args = {"003", "0", "", property = "Control field data", fullpagename = target} or ""
  }
  a = "Internamente, este registro é identificado pelo código"
  b = "para sistemas externos, o código é"
  info = string.format('* %s "%s"; %s "(%s)%s".', a, f001, b, f003, f001)
  table.insert(infos, info)
  -- Campo 005
  local creationDate =
    mw.smw.ask("[[" .. target .. "]]|?Creation date#-F[l, d \\d\\e F \\d\\e Y]|headers=hide|mainlabel=-") or ""
  if type(creationDate) == "table" then
    for _, row in pairs(creationDate) do
      for _, data in pairs(row) do
        creationDate = data
      end
    end
  end
  local modificationDate =
    mw.smw.ask("[[" .. target .. "]]|?Modification date#-F[l, d \\d\\e F \\d\\e Y]|headers=hide|mainlabel=-") or ""
  if type(modificationDate) == "table" then
    for _, row in pairs(modificationDate) do
      for _, data in pairs(row) do
        modificationDate = data
      end
    end
  end
  a = "O registro foi criado em:"
  b = "Sua última transação foi em:"
  info = string.format("* %s %s. %s %s.", a, creationDate, b, modificationDate)
  table.insert(infos, info)

  local accessPointPresence =
    frame:callParserFunction {
    name = "#ask:[[" ..
      target ..
        "#100||" ..
          target .. "#110||" .. target .. "#111||" .. target .. "#130||" .. target .. "#150||" .. target .. "#151]]",
    args = {"?Field data", format = "count", mainlabel = "-"}
  } or 0
  accessPointPresence = tonumber(accessPointPresence)

  -- Testa (conta) se existe mais de um ponto de acesso principal
  local f100existance = frame:expandTemplate {title = "Exist", args = {"100", target}} or ""
  if accessPointPresence > 1 or f100existance == "multiple" then
    a = "Foi registrado <b>mais de um ponto de acesso principal</b>. Isso não é permitido"
    info = string.format('* <strong class="error">%s.</strong>', a)
    table.insert(infos, info)
  end
  local f100 =
    mw.text.trim(
    frame:expandTemplate {title = "Field data string", args = {"100", "0", "0", fullpagename = target}} or ""
  )
  if f100 ~= "" then
    info = "A autoridade é do tipo <b>pessoa</b>"
    if f100:sub(1, 1) == "0" then
      a = "e a entrada começa pelo seu <b>primeiro nome</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f100:sub(1, 1) == "1" then
      a = "e a entrada começa pelo seu <b>sobrenome</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f100:sub(1, 1) == "3" then
      a = "e a entrada começa pelo seu <b>nome de família</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f100:sub(1, 1) == "#" then
      a = "o primeiro indicador não foi preenchido"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    else
      a = "o primeiro indicador foi preenchido incorretamente"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    end
    if f100:sub(2, 2) ~= "#" then
      a = "O segundo indicador do campo 100 foi preenchido, porém, ele não deve ser preenchido"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
    -- Testa se existe ', ' no subcampo $a (Nome, Sobrenome)
    local f100a = frame:expandTemplate {title = "Select", args = {"100", "a", target}} or ""
    if f100a ~= "" then
      if not string.match(f100a, ".-,.") then
        a = "Em pontos de acesso, o nome pessoal deve ser invertido (Nome, Sobrenome). Verifique o campo 100"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
      if string.match(f100a, "[a-zÀ-ü],[A-ZÀ-ü]") then
        a = "Falta espaço após a vírgula. Verifique o campo 100"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
    end
    local f100q = frame:expandTemplate {title = "Select", args = {"100", "q", target}} or ""
    if f100q ~= "" then
      if not string.match(f100q, "^%(") then
        a = "O subcampo $q do campo 100 necessita parênteses"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
    end
    local f100d = frame:expandTemplate {title = "Select", args = {"100", "d", target}} or ""
    if f100d ~= "" then
      if string.match(f100, "%$d %d%d%d%d$") then
        a = "O subcampo $d do campo 100 necessita hífen"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
      if not string.match(f100, ",%s?%$d") then
        a = "Inclua uma vírgula antes do subcampo $d do campo 100"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
    end
  end
  -- Testa se existe um ponto de acesso principal 110
  local f110 =
    mw.text.trim(
    frame:expandTemplate {title = "Field data string", args = {"110", "0", "0", fullpagename = target}} or ""
  )
  if f110 ~= "" then
    info = "A autoridade é do tipo <b>entidade coletiva</b>"
    if f110:sub(1, 1) == "0" then
      a = "e a entrada começa por um <b>nome invertido</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f110:sub(1, 1) == "1" then
      a = "e a entrada começa por um <b>nome de jurisdição</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f110:sub(1, 1) == "2" then
      a = "e a entrada começa por um <b>nome em ordem direta</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f110:sub(1, 1) == "#" then
      a = "o primeiro indicador não foi preenchido"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    else
      a = "o primeiro indicador foi preenchido incorretamente"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    end
    if f110:sub(2, 2) ~= "#" then
      a = "O segundo indicador do campo 110 foi preenchido, porém, ele não deve ser preenchido"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
    local f110b = frame:expandTemplate {title = "Select", args = {"110", "b", target}} or ""
    if f110b ~= "" then
      if not string.match(f110, ".%s?%$b") then
        a = "Inclua um ponto antes do subcampo $b do campo 110"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
    end
  end
  -- Testa se existe um ponto de acesso principal 111
  local f111 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"111", "0", "0", fullpagename = target}}) or
    ""
  if f111 ~= "" then
    info = "A autoridade é do tipo <b>evento</b>"
    if f111:sub(1, 1) == "0" then
      a = "e a entrada começa por um <b>nome invertido</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f111:sub(1, 1) == "1" then
      a = "e a entrada começa por um <b>nome de jurisdição</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f111:sub(1, 1) == "2" then
      a = "e a entrada começa por um <b>nome em ordem direta</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f111:sub(1, 1) == "#" then
      a = "o primeiro indicador não foi preenchido"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    else
      a = "o primeiro indicador foi preenchido incorretamente"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    end
    if f111:sub(2, 2) ~= "#" then
      a = "O segundo indicador do campo 111 foi preenchido, porém, ele não deve ser preenchido"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
  end
  -- Testa se existe um ponto de acesso principal 130
  local f130 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"130", "0", "0", fullpagename = target}}) or
    ""
  if f130 ~= "" then
    info = "A autoridade é do tipo <b>título uniforme</b>"
    if f130:sub(1, 1) ~= "#" then
      a = "o primeiro indicador não deve ser preenchido"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    end
    if f130:sub(2, 2) == "#" then
      a = "Está faltando o segundo indicador do campo 130"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    else
      local f130a = frame:expandTemplate {title = "Select", args = {"130", "a", target}} or ""
      a = ucfirst(f130a:sub(f130:sub(2, 2)))
      info = string.format('* %s, ele será alfabetado como "<b>%s</b>".', info, a)
      table.insert(infos, info)
    end
  end
  local f150 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"150", "0", "0", fullpagename = target}}) or
    ""
  if f150 ~= "" then
    if f150:sub(1, 1) ~= "#" then
      a = "O primeiro indicador do campo 150 foi preenchido, porém, ele não deve ser preenchido"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
    if f150:sub(2, 2) ~= "#" then
      a = "O segundo indicador do campo 150 foi preenchido, porém, ele não deve ser preenchido"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
  end
  local f400 =
    mw.text.trim(
    frame:expandTemplate {title = "Field data string", args = {"400", "0", "0", fullpagename = target}} or ""
  )
  if f400 ~= "" then
    info = "A remissiva Ver é do tipo <b>pessoa</b>"
    if f400:sub(1, 1) == "0" then
      a = "e a entrada começa pelo seu <b>primeiro nome</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f400:sub(1, 1) == "1" then
      a = "e a entrada começa pelo seu <b>sobrenome</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f400:sub(1, 1) == "3" then
      a = "e a entrada começa pelo seu <b>nome de família</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f400:sub(1, 1) == "#" then
      a = "o primeiro indicador não foi preenchido"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    else
      a = "o primeiro indicador foi preenchido incorretamente"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    end
    if f400:sub(2, 2) ~= "#" then
      a = "O segundo indicador do campo 400 foi preenchido, porém, ele não deve ser preenchido"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
    -- Testa se existe ', ' no subcampo $a (Nome, Sobrenome)
    local f400a = frame:expandTemplate {title = "Select", args = {"400", "a", target}} or ""
    if f400a ~= "" then
      if not string.match(f400a, ".-,.") then
        a = "Em pontos de acesso, o nome pessoal deve ser invertido (Nome, Sobrenome). Verifique o campo 400"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
      if string.match(f400a, "[a-zÀ-ü],[A-ZÀ-ü]") then
        a = "Falta espaço após a vírgula. Verifique o campo 400"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
    end
    local f400q = frame:expandTemplate {title = "Select", args = {"400", "q", target}} or ""
    if f400q ~= "" then
      if not string.match(f400q, "^%(") then
        a = "O subcampo $q do campo 400 necessita parênteses"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
    end
    local f400d = frame:expandTemplate {title = "Select", args = {"400", "d", target}} or ""
    if f400d ~= "" then
      if string.match(f400, "%$d %d%d%d%d$") then
        a = "O subcampo $d do campo 400 necessita hífen"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
      if not string.match(f400, ",%s?%$d") then
        a = "Inclua uma vírgula antes do subcampo $d do campo 400"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
    end
  end
  -- Testa se existe um ponto de acesso principal 110
  local f410 =
    mw.text.trim(
    frame:expandTemplate {title = "Field data string", args = {"410", "0", "0", fullpagename = target}} or ""
  )
  if f410 ~= "" then
    info = "A remissiva Ver é do tipo <b>entidade coletiva</b>"
    if f410:sub(1, 1) == "0" then
      a = "e a entrada começa por um <b>nome invertido</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f410:sub(1, 1) == "1" then
      a = "e a entrada começa por um <b>nome de jurisdição</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f410:sub(1, 1) == "2" then
      a = "e a entrada começa por um <b>nome em ordem direta</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f410:sub(1, 1) == "#" then
      a = "o primeiro indicador não foi preenchido"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    else
      a = "o primeiro indicador foi preenchido incorretamente"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    end
    if f410:sub(2, 2) ~= "#" then
      a = "O segundo indicador do campo 410 foi preenchido, porém, ele não deve ser preenchido"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
    local f410b = frame:expandTemplate {title = "Select", args = {"410", "b", target}} or ""
    if f410b ~= "" then
      if not string.match(f410, ".%s?%$b") then
        a = "Inclua um ponto antes do subcampo $b do campo 410"
        info = string.format('* <strong class="error">%s.</strong>', a)
        table.insert(infos, info)
      end
    end
  end
  -- Testa se existe um ponto de acesso principal 111
  local f411 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"411", "0", "0", fullpagename = target}}) or
    ""
  if f411 ~= "" then
    info = "A remissiva Ver é do tipo <b>evento</b>"
    if f411:sub(1, 1) == "0" then
      a = "e a entrada começa por um <b>nome invertido</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f411:sub(1, 1) == "1" then
      a = "e a entrada começa por um <b>nome de jurisdição</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f411:sub(1, 1) == "2" then
      a = "e a entrada começa por um <b>nome em ordem direta</b>"
      info = string.format("* %s %s.", info, a)
      table.insert(infos, info)
    elseif f411:sub(1, 1) == "#" then
      a = "o primeiro indicador não foi preenchido"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    else
      a = "o primeiro indicador foi preenchido incorretamente"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    end
    if f411:sub(2, 2) ~= "#" then
      a = "O segundo indicador do campo 411 foi preenchido, porém, ele não deve ser preenchido"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
  end
  -- Testa se existe um ponto de acesso principal 130
  local f430 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"430", "0", "0", fullpagename = target}}) or
    ""
  if f430 ~= "" then
    info = "A remissiva Ver é do tipo <b>título uniforme</b>"
    if f430:sub(1, 1) ~= "#" then
      a = "o primeiro indicador não deve ser preenchido"
      info = string.format('* %s, porém, <strong class="error">%s.</strong>', info, a)
      table.insert(infos, info)
    end
    if f430:sub(2, 2) == "#" then
      a = "Está faltando o segundo indicador do campo 430"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    else
      local f430a = frame:expandTemplate {title = "Select", args = {"430", "a", target}} or ""
      a = ucfirst(f430a:sub(f430:sub(2, 2)))
      info = string.format('* %s, ele será alfabetado como "<b>%s</b>".', info, a)
      table.insert(infos, info)
    end
  end
  local f450 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"450", "0", "0", fullpagename = target}}) or
    ""
  if f450 ~= "" then
    if f450:sub(1, 1) ~= "#" or f450:sub(2, 2) ~= "#" then
      a = "Os indicadores do campo 450 não devem ser preenchidos"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
  end
  local f643 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"643", "0", "0", fullpagename = target}}) or
    ""
  if f643 ~= "" then
    if f643:sub(1, 1) ~= "#" or f643:sub(2, 2) ~= "#" then
      a = "Os indicadores do campo 643 não devem ser preenchidos"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
  end
  local f670 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"670", "0", "0", fullpagename = target}}) or
    ""
  if f670 ~= "" then
    if f670:sub(1, 1) ~= "#" or f670:sub(2, 2) ~= "#" then
      a = "Os indicadores do campo 670 não devem ser preenchidos"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
  end
  local f675 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"675", "0", "0", fullpagename = target}}) or
    ""
  if f675 ~= "" then
    if f675:sub(1, 1) ~= "#" or f675:sub(2, 2) ~= "#" then
      a = "Os indicadores do campo 675 não devem ser preenchidos"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
  end
  local f856 =
    mw.text.trim(frame:expandTemplate {title = "Field data string", args = {"856", "0", "0", fullpagename = target}}) or
    ""
  if f856 ~= "" then
    if f856:sub(1, 1) ~= "4" then
      a = 'O primeiro indicador do campo 856 precisa ter o valor "4"'
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
    if f856:sub(2, 2) ~= "#" then
      a = "O segundo indicador do campo 856 foi preenchido, porém, ele não deve ser preenchido"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
    if string.match(f856, "%$a%s") then
      a = "Normalmente, o subcampo utilizado no 856 é o $u, não o $a"
      info = string.format('* <strong class="error">%s.</strong>', a)
      table.insert(infos, info)
    end
  end
  -- Testa por inteiro o registro de modo a acusar: "," ou o "." precedido de espaço; designadores de subcampos errados
  local isoRecord =
    frame:callParserFunction {
    name = "#ask:[[-Has subobject::" .. target .. "]][[Field data::+]]",
    args = {
      "?Field data",
      format = "plainlist",
      link = "none",
      headers = "hide",
      mainlabel = "-",
      outro = "",
      sep = "",
      valuesep = "",
      propsep = ""
    }
  } or ""
  if string.match(isoRecord, "%s[.,][^.]") then
    a = "Pontos finais ou vírgulas nunca devem ser precedidos por um espaço"
    info = string.format('* <strong class="error">%s.</strong>', a)
    table.insert(infos, info)
  end
  if string.match(isoRecord, "%$[A-Z]") then
    a = "Não são permitidos designadores de subcampos com letras maiúsculas"
    info = string.format('* <strong class="error">%s.</strong>', a)
    table.insert(infos, info)
  end

  return table.concat(infos, "\n")
end

return p