לדלג לתוכן

יחידה:דיווח מצב

מתוך חב"דפדיה, אנציקלופדיה חב"דית חופשית

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:דיווח מצב/תיעוד

--[[ To generate the statusOrder list, I:
     1. copied the list from under "suggested values" in the
        templatedata at the bottom of תבנית:מצב/צבע/קבוצות
     2. Moved the "suggestion" category options
        to be after the first list item ("new"),
        and moved the "under discussion" option
        to be after the "suggestion" options
     3. Ran the invertTable function (below)
        and copied its output back here
     4. Added manually statusTextAll and statusTextNone to the top.
     (It's possible to directly read and parse the template wikicode,
      I was just lazy. At some point it's better to do that,
      to avoid double code and the need to synchronize)

function invertTable (t)
	res = "local statusOrder = {<br/>"
	for i,a in ipairs(t) do
		res = res .. '    ["' .. a ..'"] = ' .. i
		if i < #t then
			res = res .. ','
		end
		res = res .. '<br/>'
	end
	res = res .. '}'
	return res
end
]]
local newline ='\r\n'
local statusTextAll = 'סה"כ דיונים'
local statusTextNone = "ללא תבנית מצב"
local statusOrder = {
	[statusTextAll] = -1,
	[statusTextNone] = 0,
	["חדש"] = 1,
	["בעבודה"] = 2,
	["הצעה"] = 3,
	["הצעות"] = 4,
	["הצעה לשיפור"] = 5,
	["הצעות לשיפור"] = 6,
	["הצעת שיפור"] = 7,
	["הצעות שיפור"] = 8,
	["בדיון"] = 9,
	["בטיפול"] = 10,
	["הוכלל בדף השיחה"] = 11,
	["בדף השיחה"] = 12,
	["במעקב"] = 13,
	["דיון פתוח"] = 14,
	["טופל"] = 15,
	["תוקן"] = 16,
	["{{תוקן}}"] = 17,
	["נפתר"] = 18,
	["בוצע"] = 19,
	["{{בוצע}}"] = 20,
	["הועבר"] = 21,
	["נענה"] = 22,
	["דיווח שגוי"] = 23,
	["שגוי"] = 24,
	["לא טעות"] = 25,
	["לא שגיאה"] = 26,
	["מופיע בערך"] = 27,
	["כבר מופיע בערך"] = 28,
	["לא תקלה"] = 29,
	["לא בוצע"] = 30,
	["{{לא בוצע}}"] = 31,
	["שם שגוי"] = 32,
	["מגבלה טכנית"] = 33,
	["לא יבוצע"] = 34,
	["לא ערך"] = 35,
	["פרסום"] = 36,
	["פרסומי"] = 37,
	["פרסומת"] = 38,
	["לא יקרה"] = 39,
	["לא הועבר"] = 40,
	["נמחק"] = 41,
	["לא יועבר"] = 42,
	["כבר דווח"] = 43,
	["כבר נענה"] = 44,
	["בקשה חוזרת"] = 45,
	["כפילות"] = 46,
	["כפל"] = 47,
	["ישן"] = 48,
	["לא נענה"] = 49,
	["לא טופל"] = 50,
	["ישן, לא נענה"] = 51,
	["דיווח חוזר"] = 52,
	["ישן, לא טופל"] = 53,
	["לא ניתן לטפל"] = 54,
	["ללא תוכן"] = 55,
	["ללא מענה"] = 56,
	["בלי תוכן"] = 57,
	["דרוש מקור"] = 58,
	["{{מקור}}"] = 59,
	["אין מקור"] = 60,
	["לא ברור"] = 61,
	["{{הבהרה}}"] = 62,
	["לא בשל"] = 63,
	["לא מוכן"] = 64,
	["חסר מידע"] = 65,
	["אחר"] = 66,
	["דיון סגור"] = 67,
	["דיון להעברה"] = 68,
	["להעברה"] = 69
}

function tableRow (statusList, func, first)
	local text
	local cellFirst
	local cellSep
	if first then
		cellFirst = '|+'
		cellSep = '!'
	else
		cellFirst = '|-'
		cellSep = '|'
	end
	text = cellFirst .. newline
	for i,s in ipairs(statusList) do
		if i == 1 then
			text = text .. '!'
		else
			text = text .. cellSep
		end
		text = text .. func(s, statusList[1][2][1]) .. newline -- sending the total number to all of the rows. it is ignored by most, anď used only for the calculation in row 3
	end
	return text
end

function tableRow1 (s)
	return s[1]
end

function tableRow2 (s)
	return s[2][1]
end

function tableRow3 (s, total)
	return math.floor(0.5 + 100*s[2][1]/total) .. '%'
end

function tableRow4 (s)
	if not s[2][2] then
		return 'פירוט'
	end
	text = 'style="vertical-align: top" | <div class = "mw-collapsible mw-collapsed" style="text-align:right"><br/>'
	for i, headerUser in ipairs(s[2][2]) do
		text = text .. newline .. "*" .. headerUser[1]
		if headerUser[2] then
			text = text .. " (" .. headerUser[2] .. ")"
		end
	end
	text = text .. "</div>"
	return text
end

function formatTable (statusList, showPercent, showLinks)
	-- table start, title row, first cell
    local tableWikiText = '{| class="wikitable" style="text-align: center"\r\n'
    tableWikiText = tableWikiText .. tableRow (statusList, tableRow1, true)
    tableWikiText = tableWikiText .. tableRow (statusList, tableRow2, false)
    if showPercent then
    	tableWikiText = tableWikiText .. tableRow (statusList, tableRow3, false)
    end
    if showLinks then
        tableWikiText = tableWikiText .. tableRow (statusList, tableRow4, false)
    end
    tableWikiText = tableWikiText .. '|}'
    return tableWikiText
end

function processSectionName ( sectionName, titleText )
	local tav    = ' *תב *'
	local tavnit = ' *תבנית *'
	local tm = '&lcub;&lcub;%1&rcub;&rcub;'
	local sectionName1 = mw.text.trim(sectionName)
	local sectionName2 = string.gsub( sectionName1, '{{'..tav   ..'|'..tav   ..': *([^}]*) *}}',  tm) -- usage of 'tav'    template: keep content and surround by double curly braces, drop template prefix
    local sectionName3 = string.gsub( sectionName2, '{{'..tav   ..'|'..tavnit..': *([^}]*) *}}',  tm) -- usage of 'tav'    template: keep content and surround by double curly braces, drop template prefix
    local sectionName4 = string.gsub( sectionName3, '{{'..tav   ..'|'..         ' *([^}]*) *}}',  tm) -- usage of 'tav'    template: keep content and surround by double curly braces
    local sectionName5 = string.gsub( sectionName4, '{{'..tavnit..'|'..tav   ..': *([^}]*) *}}',  tm) -- usage of 'tavnit' template: keep content and surround by double curly braces, drop template prefix
    local sectionName6 = string.gsub( sectionName5, '{{'..tavnit..'|'..tavnit..': *([^}]*) *}}',  tm) -- usage of 'tavnit' template: keep content and surround by double curly braces, drop template prefix
    local sectionName7 = string.gsub( sectionName6, '{{'..tavnit..'|'..         ' *([^}]*) *}}',  tm) -- usage of 'tavnit' template: keep content and surround by double curly braces
    local sectionName8 = string.gsub( sectionName7, '%[%[[^]|]*|([^]|]*)%]%]', '%1') -- wikilinks with display text: keep only the display text
    local sectionName9 = string.gsub( sectionName8, '%[%[:?([^]]*)%]%]', '%1') -- wikilinks with display text: keep the content, dropping the brackets and optionally also a leading colon
    local sectionName10= "[[" .. titleText .. "#" .. sectionName9 .. "|" .. sectionName9 .. "]]"
    return sectionName10
end

function getSections ( wikiText, titleText )
	local sectionHeaderPattern = '%c==([^=][^%c=]*)=='
	local sectionTable = {}
	local offset
	local offsetStart
	local offsetStartPrev
	local sectionName
	offsetStart, offset, sectionName = string.find( wikiText, sectionHeaderPattern )
	if not sectionName then
		return nil -- no H2 sections found on page
	end
	while sectionName do
		offsetStartPrev = offsetStart
		sectionNamePrev = processSectionName(sectionName, titleText) -- remove leading and trailing whitespace and interlinks, convert into a link to the header
		offsetStart, offset, sectionName = string.find( wikiText, sectionHeaderPattern, offset )
		if sectionName then
			sectionText = string.sub(wikiText, offsetStartPrev, offsetStart)
			table.insert(sectionTable, {sectionNamePrev, sectionText})
		end
	end
	sectionText = string.sub(wikiText, offsetStartPrev)
	table.insert(sectionTable, {sectionNamePrev, sectionText})
	return sectionTable
end

function statusSort (s1, s2)
	return (statusOrder[s1[1]] or 999) < (statusOrder[s2[1]] or 999)
end

function sectionTableToStatusTable ( sectionTable, showEditors )
	local statusType
	local argsMatcher
	local argList
	local matzav = "מצב"
	local fullPattern = '{{ *'..matzav..' *| *([^}]*})}' -- including thd first curly brace, to delimit the last argument
	
	statusTable = {}
	for i, section in ipairs(sectionTable) do
		statusTemplateArguments = string.match (section[2], fullPattern)
		if not statusTemplateArguments then
			statusType = statusTextNone
			user = nil
		else
		    argsMatcher = string.gmatch (statusTemplateArguments, "([^|}]*) *[|}]")
		    argList = {}
		    for arg in argsMatcher do
		    	table.insert(argList, mw.text.trim(arg))
		    end
		    statusType = argList[1]
		    if showEditors then
		        user = argList[2] -- might be nil. also, I'm ignoring the display text (argList[3]) if it exists
		    else
		    	user = nil
		    end
		end
		if not statusTable[statusType] then
			statusTable[statusType] = {1, {{section[1],user}}}
		else
			statusTable[statusType][1] = statusTable[statusType][1] + 1
			table.insert(statusTable[statusType][2], {section[1],user})
		end
	end
	statusTable[statusTextAll] = {#sectionTable}
	-- now that the table is built, convert it into a sequence and sort it
    local statusList = {}
    for k, v in pairs(statusTable) do
    	table.insert(statusList, {k, v})
    end
    table.sort(statusList, statusSort)
    return statusList
end

function getStatusReport ( title, showPercent, showLinks, showEditors )
	local wikiText = title:getContent()
	local sectionTable = getSections(wikiText, title.prefixedText) -- the page title text is used to convert the section header into a link 
	if not sectionTable then
		return ''
	end
	local statusList = sectionTableToStatusTable(sectionTable, showEditors)
    return formatTable (statusList, showPercent, showLinks)
end

function emptyParam (param)
	return (not param) or (param == "")
end

function statusReport ( frame )
	local yes = 'כן'
	local no = 'לא'
	local title
	local showPercent
	local showLinks
	local showEditors
	if emptyParam(frame.args['title']) then
    	title = mw.title.getCurrentTitle()
    else
    	title = mw.title.new(frame.args['title'])
    end
	showPercent = emptyParam(frame.args['percent']) or (frame.args['percent'] == yes)
	showLinks   = emptyParam(frame.args['links'])   or (frame.args['link']    == yes)
	showEditors = emptyParam(frame.args['editors']) or (frame.args['editors'] == yes)
	
	return getStatusReport (title, showPercent, showLinks, showEditors)
end

return {
	statusReport = statusReport
}