Jump to content

Module:Web2Cit/results

From Meta, a Wikimedia project coordination wiki
Module documentation
--[[
=p.domain(
   mw.getCurrentFrame():newChild{
      title="Template:Web2Cit/results/domain",
      args={
         ["domain"]="www.example.com",
         ["path1"]="/path/1/",
         ["score1"]="100",
         ["path1_fieldname1"]="itemType",
         ["path1_expected1"]="* expected",
         ["path1_actual1"]="* actual",
         ["path1_score1"]="100",
      }
   }:newChild{
      title="Module:Web2Cit/results",
      args={}
   }
)
]]

local TNT = require('Module:TNT')
local tab = "I18n/Web2Cit.tab"  -- https://commons.wikimedia.org/wiki/Data:I18n/Web2Cit.tab
local lang

local WEB2CIT = "https://web2cit.toolforge.org/"
local ERRORCAT = "Category:Web2Cit Monitor Results with errors"
local MISSCAT = "Category:Web2Cit Monitor Results with missing tests"
local ROOTCAT = "Category:Web2Cit Monitor Results with root path"
local NOPATHS = "Category:Web2Cit Monitor Results without paths"

local function testsTable(args)
	local out = {}

    table.insert(out, '{| class="wikitable"')
    table.insert(out, "|-")
    table.insert(out,
		"! " .. TNT.formatInLanguage(lang, tab, "monitor.results.domain.tests.table.path")
		.. "!! " .. TNT.formatInLanguage(lang, tab, "monitor.results.domain.tests.table.score")
		.. "!! " .. TNT.formatInLanguage(lang, tab, "monitor.results.domain.tests.table.coverage")
	)

    local i = 1

    while args["path" .. i] do
		table.insert(out, "|-")
        
		local path = args["path" .. i]
		table.insert(out, "| [[#" .. path .. "|" .. path .. "]]")

		local error = args["error" .. i]
		if error then
			table.insert(out,
				'| colspan="2" | '
				.. TNT.formatInLanguage(lang, tab, "monitor.results.domain.tests.table.error")
			)
		else
			local score = args["score" .. i]
			local coverage = args["coverage" .. i]
			table.insert(out, "| " .. score)
			table.insert(out,
				'| data-sort-value="' .. coverage .. '" | ' .. coverage .. "%"
			)
		end
		
        i = i + 1
    end

    table.insert(out, "|}")

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

local function pathTable(args, pathIndex)
    local out = {}

    table.insert(out, '{| class="wikitable"')
    table.insert(out, "|-")
	table.insert(out,
		"! " .. TNT.formatInLanguage(lang, tab, "monitor.results.path.table.fieldname")
		.. " !! " .. TNT.formatInLanguage(lang, tab, "monitor.results.path.table.actual")
		.. " !! " .. TNT.formatInLanguage(lang, tab, "monitor.results.path.table.expected")
		.. " !! " .. TNT.formatInLanguage(lang, tab, "monitor.results.path.table.score")
	)

	local error = args["error" .. pathIndex]
	if error then
		table.insert(out, "|-")
		table.insert(out,
			'| colspan="4" | '
			.. TNT.formatInLanguage(lang, tab, "monitor.results.path.error", error)
		)
		table.insert(out, "[[" .. ERRORCAT .. "]]")
	else
		local prefix = "path" .. pathIndex .. "_"
	    local j = 1
		
	    while args[prefix .. "fieldname" .. j] do
	        local fieldname = args[prefix .. "fieldname" .. j]
	        local expected = args[prefix .. "expected" .. j]
	        local actual = args[prefix .. "actual" .. j]
	        local score = args[prefix .. "score" .. j]
	
	        table.insert(out, "|-")
	        table.insert(out, "| " .. fieldname)
			table.insert(out, "|\n" .. actual)
			if score then
				table.insert(out, "|\n" .. expected)
				table.insert(out, "| " .. score)
			else
				table.insert(out,
					'| colspan="2" style="text-align:center;" | '
					.. TNT.formatInLanguage(lang, tab, "monitor.results.path.table.field.missing")
					.. "[[" .. MISSCAT .. "]]"
				)
			end
	
	        j = j + 1
	    end
	end

    table.insert(out, "|}")

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

local p = {}

function p.domain(frame)
    local args = frame:getParent().args
	lang = frame:preprocess("{{USERLANGUAGE}}")

    local out = {}

    table.insert(out, "__NOTOC__")

	-- intro
    table.insert(out,
        "= "
		.. TNT.formatInLanguage(lang, tab, "monitor.results.domain.intro.head", args.domain)
		.. " ="
	)

    table.insert(out,
		TNT.formatInLanguage(lang, tab, "monitor.results.domain.intro.body", args.domain)
	)

	-- checks
	table.insert(out,
		"== "
		.. TNT.formatInLanguage(lang, tab, "monitor.results.domain.checks.head")
		.. " =="
	)

	table.insert(out, '{| class="wikitable"')
    table.insert(out, frame:expandTemplate{ title="Web2Cit/log/header" });
    table.insert(out, "|-")
    table.insert(out, frame:preprocess("{{../log}}"))
    table.insert(out, "|}")

	table.insert(out,
		"''([[../log|"
		.. TNT.formatInLanguage(lang, tab, "monitor.results.domain.checks.all")
		.. "]])''"
	)

	-- tests
	table.insert(out,
		"== "
		.. TNT.formatInLanguage(lang, tab, "monitor.results.domain.tests.head")
		.. " =="
	)

	if not args.path1 then
		table.insert(out,
			frame:expandTemplate{
				title="Warning message",
				args = {
					msg = TNT.formatInLanguage(lang, tab,
						"monitor.results.domain.tests.empty"
					)
				}
			}
		)
		table.insert(out,
			"[[" .. NOPATHS .. "]]"
		)
	end

	table.insert(out,
		TNT.formatInLanguage(lang, tab, "monitor.results.domain.tests.body")
		.. frame:extensionTag( "ref", TNT.formatInLanguage(lang, tab,
				"monitor.results.domain.tests.body.note"
		))
	)

	table.insert(out, testsTable(args))

	table.insert(out,
		"''([" .. WEB2CIT .. "translate?tests=true&domain=" .. args.domain .. " "
		.. TNT.formatInLanguage(lang, tab, "monitor.results.domain.tests.open")
		.. "])''"
	)

	-- paths
	local i = 1

    while args["path" .. i] do
        local path = args["path" .. i]

        table.insert(out,
			"=== " .. path .. " ==="
		)

		-- root path warning and categorization
		if path == "/" then
			table.insert(out,
				frame:expandTemplate{
					title="Ombox",
					args = {
						text = TNT.formatInLanguage(lang, tab,
							"monitor.results.path.root",
							"<code>/</code>"
						),
						small = "yes"
					}
				}
			)
			table.insert(out,
				"[[" .. ROOTCAT .. "]]"
			)
		end

        table.insert(out, pathTable(args, i))

		-- To support URL fragments, fragment delimiter # must be encoded (T430885).
		local encodedPath = string.gsub(path, "#", "%%23")

		table.insert(out,
			"''([" .. WEB2CIT .. "https://" .. args.domain .. encodedPath .. " "
			.. TNT.formatInLanguage(lang, tab, "monitor.results.path.open")
			.. "])''"
		)

        i = i + 1
    end

	-- table
	table.insert(out,
		"== "
		.. TNT.formatInLanguage(lang, tab, "monitor.results.domain.notes")
		.. " =="
	)

	table.insert(out, frame:preprocess("<references/>"))


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

return p