Module:DéfiStats

La bibliothèque libre.

La documentation pour ce module peut être créée à Module:DéfiStats/Documentation

local p = {}
local args = {}
local proofreadPage = require('mw.ext.proofreadPage')

function p.Corr(frame)
	return frame:preprocess(CorrVal(frame, 'corr'))
end

function p.Val(frame)
	return frame:preprocess(CorrVal(frame, 'val'))
end

function CorrValOld(frame, operation) --, index, operation)
	args = frame.args
	if args[1] == nil then
		args = frame:getParent().args
	end
	local index = args[1] --'Livre:Vivien - Une femme m’apparut, 1904.djvu'
	local statPages = {}
	statPages = p.searchForReport(index)
	local pagesSum = '??'	
	local pagesRest = '??'
	local perCent = '??'
    	--return mw.dumpObject(statPages) end
 --function rien()
	for i,v in ipairs(statPages) do
		statPage = 'Utilisateur:BookwormBot/reports/' .. v
		titre = mw.title.new(statPage)		
		local textContent = titre.getContent(titre)  

		local values = mw.ustring.match(textContent, escape(index) .. "%|(.-)%}%}")

			if values ~= nil then
				local pagesQuality = mw.text.split( values, '%|')
				pagesSum = pagesQuality[1] + pagesQuality[2] + pagesQuality[3] + 
					pagesQuality[4] + pagesQuality[5] + pagesQuality[6] 
				local pagesOp = pagesQuality[1] + pagesQuality[5] 
				if operation == 'corr' then pagesOp = pagesOp + pagesQuality[4] end
				perCent = math.ceil(100 * pagesOp / pagesSum)
				pagesRest = pagesSum - pagesOp
				break
			end
		--end
	end
	local res =	'{{tm|{{bleu|' .. pagesRest .. '}} {{t|de|80}}|3.2}}'
	res = res .. '{{tm|' .. pagesSum .. '|2.1|80}}{{tm|(' .. perCent .. '%)|80|3.6}}'
	res = res .. ' — [[' .. index .. ']]'
	return res --.. mw.text.nowiki(frame:expandTemplate{ title = ':' .. args[1] })
end

function escape(s)
    return (s:gsub('[%-%.%+%[%]%(%)%$%^%%%?%*]','%%%1'))
end

function p.searchForReport(bookName)
	local titre = mw.title.new( bookName )
	local content = ''
	local indexBot = ''
	local statPages = {}
	if titre ~= nil then 
		content = titre:getContent()
		content = mw.ustring.gsub(content,'%{%{IA%|','[[Catégorie:Facsimilés issus d’Internet Archive]]')
		content = mw.ustring.gsub(content,'%{%{[Gg]allica%|','[[Catégorie:Facsimilés issus de Gallica]]')
		content = mw.ustring.gsub(content,'%{%{[Gg]oogle%|','[[Catégorie:Facsimilés issus de Google Books]]')
		content = mw.ustring.gsub(content,'%{%{[Bb][Aa]nQ%|','[[Catégorie:Ouvrages issus du partenariat avec BAnQ]]')
		content = mw.ustring.gsub(content,'%{%{[Uu]CA|%|','[[Catégorie:Facsimilés issus de l’UCA]]')
		content = mw.ustring.gsub(content,'%{%{[Kk]br|%|','[[Catégorie:Facsimilés issus de la Bibliothèque Royale de Belgique]]')
		content = mw.ustring.gsub(content,'%{%{[Aa]ML|%|','[[Catégorie:Facsimilés provenant des AML]]')
		content = mw.ustring.gsub(content,'%{%{PQC%}%}','[[Catégorie:Projet Québec/Canada]]')		
		--mw.log(content)
	end
	titre = mw.title.new( 'BookwormBot/config', 'Utilisateur' )
	if titre ~= nil then 
		indexBot = titre:getContent()
	end
	for catBot in mw.ustring.gmatch(indexBot, 'Category:(.-)%c') do 
		for catBook in mw.ustring.gmatch(content, 'Catégorie:(.-)%]%]') do 
			--if catBot == catBook then return catBot:gsub("%/", " ") end
			if catBot == catBook then 
				element = catBot:gsub("%/", " ")
				table.insert(statPages, element ) 
			end 
		end
	end
	return statPages
	
end

--Nouvelle approche suite à la non-disponibilité de BookwormBot 

function CorrVal(frame, operation) --, index, operation)
	args = frame.args
	if args[1] == nil then
		args = frame:getParent().args
	end
	local index = args[1] --'Livre:Vivien - Une femme m’apparut, 1904.djvu'
	local pagesSum = '??'	
	local pagesRest = '??'
	local perCent = '??'

	local pagesQuality = Qualities(index)
	pagesSum = pagesQuality[1] + pagesQuality[2] + pagesQuality[3] + 
		pagesQuality[4] + pagesQuality[5] + pagesQuality[6] 
	local pagesOp = pagesQuality[1] + pagesQuality[5] 
	if operation == 'corr' then pagesOp = pagesOp + pagesQuality[4] end
	perCent = math.ceil(100 * pagesOp / pagesSum)
	pagesRest = pagesSum - pagesOp

		--end
	local res =	'{{tm|{{bleu|' .. pagesRest .. '}} {{t|de|80}}|3.2}}'
	res = res .. '{{tm|' .. pagesSum .. '|2.1|80}}{{tm|(' .. perCent .. '%)|80|3.6}}'
	if args[5] ~= nil then 
		res = res .. '{{tm|[[File:Flag of ' .. args[5] .. '.svg|12px]]|1.2}}'
	end
	if args[4] ~= nil then 
		maj = string.char( 39 ) .. string.char( 39 ) .. string.char( 39 )
		maj = frame:expandTemplate{ title = 'vert', args = { maj .. args[4] .. maj } }
		res = res .. ' ' .. frame:expandTemplate{ title = 'tm', args = { maj, '1.4' } }
	end
	res = res .. ' — [[' .. index .. ']]'
	if args[3] ~= nil then 
		res = res .. ' {{t|{{vert|(' .. args[3] .. ')}}|80}}'
	end
	return res --.. mw.text.nowiki(frame:expandTemplate{ title = ':' .. args[1] })
end

function Qualities(livre)
	local Q = {}
	livre = mw.ustring.gsub(livre, 'Livre:', '')
	local index = proofreadPage.newIndex( livre )
	Q[1] = index:pagesWithLevel( 0 ) --mw.ext.proofreadPage.QualityLevel.WITHOUT_TEXT
	Q[2] = index:pagesWithLevel( 1 ) --mw.ext.proofreadPage.QualityLevel.NOT_PROOFREAD
	Q[3] = index:pagesWithLevel( 2 ) --mw.ext.proofreadPage.QualityLevel.PROBLEMATIC
	Q[4] = index:pagesWithLevel( 3 ) --mw.ext.proofreadPage.QualityLevel.PROOFREAD
	Q[5] = index:pagesWithLevel( 4 ) --mw.ext.proofreadPage.QualityLevel.VALIDATED
	Q[6] = index.pageCount - (Q[1] + Q[2] + Q[3] + Q[4] + Q[5])
	return Q
end

function p.beginStats(frame)
	local args = frame.args
	args['mode'] = args['mode'] or ''
	local sCode = '{{User:BookwormBot/reportstart}}'
	if args['mode'] == 'wikicode'  then 
		sCode = mw.text.nowiki(sCode)
	end
	if args[1] == nil then
		args = frame:getParent().args
	end
	
	for i, v in ipairs(args) do
		v = mw.ustring.gsub(v,"[\r\n]", "") --élimination des sauts de ligne
		--v = mw.ustring.gsub(v,"\\", "")
		if args['mode'] == 'wikicode'  then
			sCode = sCode .. '<br />' .. mw.text.nowiki(p.getStats(v))			
		else
			sCode = sCode .. '\n' .. p.getStats(v)
		end
	end	
	if args['mode'] == 'wikicode'  then 
		sCode = sCode .. '<br />' .. mw.text.nowiki('{{User:BookwormBot/reportend}}')
	else
		sCode = sCode .. '\n{{User:BookwormBot/reportend}}'		
	end

	if args['mode'] == 'wikicode'  then
		--sCode = '<nowiki>' .. mw.ustring.gsub( sCode, '\n', '</nowiki><br /><nowiki>' ) .. '</nowiki>'
		--return sCode
	end
	
	return frame:preprocess(sCode)

end

function p.getStats(index)
	local pagesQuality = Qualities(index)
	local pagesSum = '??'	
	pagesSum = pagesQuality[1] + pagesQuality[2] + pagesQuality[3] + 
		pagesQuality[4] + pagesQuality[5] + pagesQuality[6] 
	local pagesOpVal = pagesQuality[1] + pagesQuality[5] 
	local pagesOpCor = pagesOpVal + pagesQuality[4]
	res = '{{User:BookwormBot/reportline|' .. index .. '|'
	res = res .. pagesQuality[1] .. '|'
	res = res .. pagesQuality[2] .. '|'
	res = res .. pagesQuality[3] .. '|'
	res = res .. pagesQuality[4] .. '|'
	res = res .. pagesQuality[5] .. '|'
	res = res .. pagesQuality[6] .. '}}'
	return res
end

return p