Modul:Pivot

Aus KoschWiki
Version vom 18. März 2019, 18:16 Uhr von SyncBot (D | B) (Schützte „Modul:Pivot“: Automatisch gesperrt. ([Verschieben=Nur Administratoren erlauben] (unbeschränkt) [Bearbeiten=Nur Administratoren erlauben] (unbeschränkt)))
Zur Navigation springen Zur Suche springen
Dokumentation und Testfälle unter Modul:Pivot/Doku.
local p = {}

function countItems(thedata,tbl,spl,flt)
 local txt=string.gsub(thedata,"%[%[SMW::on%]%]","")
 txt=string.gsub(txt,"%[%[SMW::off%]%]","")
 local lines=mw.text.split(txt,spl)
 for key, value in pairs(lines) do
 	local v=mw.text.trim(value)
 	if (flt==nil) or (string.sub(v,1,string.len(flt))==flt) then
 		if v~="" then
 			local a=tbl[v];
 			if a==nil then a=0 end
 			tbl[v]=a+1
 		end
 	end
 end
 return tbl
end

function getCategory(link,cats)
 local catbp=string.gsub(link,"(%[%[)([^%|]*)(%|.*)","%2")
 if catbp==nil then
 	return ""
 end
 local title=mw.title.new(catbp)
 if title==nil then
 	return ""
 end
 local titletext=title.text
 return "[[Kategorie:"..titletext.."|"..cats.."]]"
end

function pivotSub(data,data2,spl,srt,flt,bld,cat)
 local result=""
 local cats=""
 local tbl={ }
 tbl=countItems(data,tbl,spl,flt)
 tbl=countItems(data2,tbl,spl,flt)
 
 local tbls={ }
 for key, value in pairs(tbl) do
 	table.insert(tbls,{key,value})
 end
 function compareC(a,b)
 	if a[2]~=b[2] then return a[2] > b[2] end
 	return a[1] < b[1]
 end
 function compareT(a,b)
 	if a[1]~=b[1] then return a[1] < b[1] end
 	return a[2] < b[2]
 end
 if srt=="C" then 
 	table.sort(tbls,compareC)
 else
 	table.sort(tbls,compareT)
 end

 for key, value in pairs(tbls) do
 	local b=""
 	if tonumber(value[2])>=bld then b="'''" end
 	result=result..b..value[1].." ("..value[2].."x)"..b..", "
 	cats=cats..getCategory(value[1],cat)
 end

 local l=string.len(result)
 if l>=2 then
 	result=string.sub(result,1,l-2)
 end

 if cat~="" then 
 	result=result..cats
 end

 return result   
 
end

function p.countAndSort(frame)
 local data=frame.args[1] --data
 if data==nil then data="" end
 local spl=frame.args[2] --Split character
 if spl==nil then spl="\n" end
 local srt=frame.args[3] --Sort ("C"ount or "T"ype)
 if srt==nil then srt="C"
 	elseif srt~="C" then srt="T" end
 local flt=frame.args[4] --Filter by first character
 local bld=frame.args[5] --Number to apear bold
 if bld==nil then bld=5 else bld=tonumber(bld) end
 local data2=frame.args[6] --additional data (maximum string size)
 if data2==nil then data2="" end
 local cat=frame.args[7] --Categorize?
 if cat==nil then cat="" end
 return pivotSub(data,data2,spl,srt,flt,bld,cat)
end

return p