Modul:Pagina principală

De la Wikipedia, enciclopedia liberă
Jump to navigation Jump to search

Documentația acestui modul poate fi creată la Modul:Pagina principală/doc

local pp = {}
local wikidata = require('Modul:Wikidata')
local removeRedLinks = require('Modul:RedLinkRemover').fromString
local imgutils = require('Modul:ImageUtils')
local fadata= mw.loadData('Modul:Pagina principală/articole')
local bda = require('Modul:BirthDateAndAge')
local getArgs = require('Module:Arguments').getArgs

local function removeTemplate(wikitext, template)
	--TODO: smarter regex that can skip imbricated templates
	local regex = '{{' .. template .. '(.-)}}'
	wikitext = mw.ustring.gsub(wikitext, regex, '')
	return wikitext
end

local function removeInfobox(section)
	--TODO: merge regular expressions
	mw.logObject(section, "before removeInfobox")
	section = mw.ustring.gsub(section, '%{%{([\r\n]*)Infocaseta(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
	section = mw.ustring.gsub(section, '%{%{([\r\n]*)Infobox(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
	section = mw.ustring.gsub(section, '%{%{([\r\n]*)Cutie(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
	section = mw.ustring.gsub(section, '%{%{([\r\n]*)Taxobox(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
	section = mw.ustring.gsub(section, '%{%{([\r\n]*)Ordine_Religioase(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
	section = mw.ustring.gsub(section, '%{%{([\r\n]*)Război(.*)%}%}([ \t\r\n]+)(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
	section = mw.ustring.gsub(section, '%{%{([\r\n]*)multiple image(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
	mw.logObject(section, "after removeInfobox")
	return section
end

local function firstSection(text)
	local section = mw.ustring.sub(text, 1, mw.ustring.find(text, '\n=='))
	section = mw.text.trim(section)
	--mw.logObject(section)
	return section
end

local function addBacklink(section, title)
	mw.logObject(description, "Before backlink")
	section = mw.ustring.gsub(section, '\'\'\'([^%[%{\']-)\'\'\'', '\'\'\'\[\[' .. title .. '\|%1\]\]\'\'\'', 1)
	mw.logObject(description, "After backlink")
	return section
end

local function replaceBiodata(section, qid)
	death = wikidata.findOneValueNoRef('P570', qid)
	if qid == nil then return mw.ustring.gsub(section, '%({{Date biografice}}%)', '') end
	if death then death = '—d. ' .. death else death = '' end
	biodata = 'n. ' .. (wikidata.findOneValueNoRef('P569', qid) or '?') .. (death)
	return mw.ustring.gsub(section, '{{Date biografice}}', biodata)
end

local function readMore(article)
	return "\n<div align=right style=\"font-size:90%;\">'''''[[" .. article .. "|continuare...]]'''''</div>\n"
end

local function articleText(title, qid)
	local title = mw.title.makeTitle( 0, title, 'firstHeading');
	if title.isRedirect then title = title.redirectTarget end
	description = firstSection(title:getContent());
	image, caption = wikidata.findImageAndCaption(qid)

	-- remove comments
	description = mw.ustring.gsub(description, '(<!\-\-(.-)\-\->\n?)', '')
	description = mw.ustring.gsub(description, '(</?onlyinclude>\n?)', '')
	description = mw.ustring.gsub(description, '__TOC__', '')
	description = replaceBiodata(description, qid)
	mw.logObject(description, "Before Refs")
	-- remove refs
	description = mw.ustring.gsub(description, '(<(ref)([^>]-)/(\s-)>)', '')
	--mw.logObject(description)
	description = mw.ustring.gsub(description, '(<(ref)(.-)>(.-)<\/%2>)', '')
	mw.logObject(description, "Before templates")
	-- remove unwanted templates
	description = removeTemplate(description, "Rp")
	description = removeTemplate(description, "Actualizare")
	description = removeTemplate(description, "Titlu cursiv")
	description = removeTemplate(description, "Formula 1")
	description = removeTemplate(description, "([pP])entru")
	description = removeTemplate(description, "([tT])rimite")
	description = removeTemplate(description, "acesta")
	description = removeTemplate(description, "about")
	description = removeTemplate(description, "distinge")
	description = removeTemplate(description, "([dD])espre")
	description = removeTemplate(description, "([aA])ltesensuri")
	description = removeTemplate(description, "ortografie")
	description = removeTemplate(description, "diacritice")
	description = removeTemplate(description, "TOC limit")
	description = removeTemplate(description, "efn")
	description = removeTemplate(description, "sfn")
	description = removeTemplate(description, "PAC")
	description = removeTemplate(description, "RAC")
	description = removeTemplate(description, "([dD])eznotă")
	description = removeTemplate(description, "dezambig")
	description = removeTemplate(description, "articol bun")
	description = removeTemplate(description, "pp-") -- protected pages
	description = removeTemplate(description, "Căsătorie homosexuală")
	description = removeTemplate(description, "Cutie Racovita, Sibiu")
	description = removeTemplate(description, "Membru Academia Română")
	description = removeTemplate(description, "Relativitate generală")
	description = removeTemplate(description, "Statalitatea poloneză")
	description = removeTemplate(description, "Collapsible list")
	description = removeTemplate(description, ".. icon")
	description = removeTemplate(description, "[Ff]ix")
	description = removeTemplate(description, "Însemnele României")
	description = removeTemplate(description, "[Cc]oord")
	description = removeTemplate(description, "Mitropolie Ortodoxă")
	description = removeTemplate(description, "[iI]magine")
	description = removeTemplate(description, "Istoria")
	description = removeTemplate(description, "[iI]talic title")
	description = removeTemplate(description, "[Tt]itlu")
	description = removeTemplate(description, "CitareText")
	description = removeTemplate(description, "Budism")
	description = removeTemplate(description, "Decesrecent")
	description = removeTemplate(description, "rquote")
	description = removeTemplate(description, "sanitas")
	description = removeTemplate(description, "[Pp]p-semi-vandalism")
	description = removeTemplate(description, "ref label")
	description = removeTemplate(description, "[eE]cartamente")
	description = removeTemplate(description, "#tag")
	description = removeTemplate(description, "[Cc]utie")
	description = removeTemplate(description, "[Cc]omentariu")
	description = removeTemplate(description, "Literatura")
	description = removeTemplate(description, "[Mm]onarhism")
	description = removeTemplate(description, "Discriminare")
	description = removeTemplate(description, "Cultura României")
	description = removeTemplate(description, "[Rr]edirect")
        description = removeTemplate(description, "Uniuni același sex Europa")
        description = removeTemplate(description, "Avizmedical") 
	--remove images
	mw.logObject(description, "Before images")
	description = mw.ustring.gsub(description, '%[%[Fișier:([^\r\n]*)%]%]\n', '')
	description = mw.ustring.gsub(description, '%[%[Imagine:([^\r\n]*)%]%]\n', '')
	description = mw.ustring.gsub(description, '%[%[Media:([^\r\n]*)%]%]\n', '')
	description = mw.ustring.gsub(description, '%[%[Fişier:([^\r\n]*)%]%]\n', '')
	description = mw.ustring.gsub(description, '%[%[File:([^\r\n]*)%]%]\n', '')
	description = mw.ustring.gsub(description, '%[%[Image:([^\r\n]*)%]%]\n', '')
	--remove infobox
	description = removeInfobox(description)
	--remove images at the end of the introduction
	description = mw.ustring.gsub(description, '%[%[Fișier:([^\r\n]*)%]%]', '')
	description = mw.ustring.gsub(description, '%[%[Imagine:([^\r\n]*)%]%]', '')
	description = mw.ustring.gsub(description, '%[%[Media:([^\r\n]*)%]%]', '')
	description = mw.ustring.gsub(description, '%[%[Fişier:([^\r\n]*)%]%]', '')
	description = mw.ustring.gsub(description, '%[%[File:([^\r\n]*)%]%]', '')
	description = mw.ustring.gsub(description, '%[%[Image:([^\r\n]*)%]%]', '')
	mw.logObject(description, "After Files")
	--remove first empty line
	description = mw.ustring.gsub(description, '^\n+', '')
	--parametrize biodata
	if qid then
		description = mw.ustring.gsub(description, '%{%{([\r\n]*)[Dd]ate biografice(%s*)%}%}', '{{Date biografice|q=' .. qid .. '}}')
	end
	--remove red links
	description = removeRedLinks(description)
	--add a link to the article itself
	description = addBacklink(description, title.prefixedText)
	
	-- add image
	-- TODO: allow local image
	if image ~= nil then
		if caption == nil then caption = "" else caption = "|" .. caption end
		if imgutils.isLandscape({f=image}) then size = '150px' else size = '100px' end
		description = '{{Wikipedia:Articol de calitate/Sus|[[Fișier:' .. image .. '|border|' .. size .. caption .. ']]}}\n\n' .. description
	end

	preprocessed = tostring( mw.getCurrentFrame():preprocess(description) )
	mw.logObject(preprocessed, "Preprocessed")
	nomarkers = mw.text.killMarkers(preprocessed)
	mw.logObject(nomarkers, "Final version")
	return nomarkers
end

function pp.articolBunJos(articol, compact)
	ret = readMore(articol)
	a1 = fadata.keys[math.random(1,fadata.len)]
	a2 = a1
	a3 = a1
	while a2 == a1 do
		a2 = fadata.keys[math.random(1,fadata.len)]
	end
	while a3 == a1 or a3 == a2 do
		a3 = fadata.keys[math.random(1,fadata.len)]
	end
	mw.logObject(a1, "a1")
	mw.logObject(a2, "a2")
	mw.logObject(a3, "a3")
	if compact == true then
		ret = ret .. "<div style=\"font-size:90%;\">Mai mult [[Wikipedia:Articole de calitate|conținut de calitate]]: '''[[" .. a1 .. "]]''' – '''[[" .. a2 .."]]''' – '''[[" .. a3 .."]]'''</div>"
	else
		local plural = require('Modul:Plural')
		no = plural.build_plural(fadata.len, "pagină", nil, nil)
		ret = ret .. "<div style=\"font-size:90%;\">\nAlte sugestii dintre [[Wikipedia:Articole de calitate|cele ".. no .. "  recomandate]]:\n*'''[[" .. a1 .. "]]'''\n*'''[[" .. a2 .. "]]'''\n*'''[[" .. a3 .. "]]'''\n</div>"
	end
    return ret
end

function pp.articoleBune(frame)
	mw.logObject(math.floor(os.time() / 10))
	math.randomseed(math.floor(os.time() / 10))
	t = fadata.keys[math.random(1,fadata.len)]
	--t = "Pictura bisericească a lui Nicolae Grigorescu"
	q = fadata.featuredPages[t]
	return articleText(t, q) .. pp.articolBunJos(t, false)
end

function pp.getRandomIntroduction(frame)
	local args = {}
    for name, value in frame:argumentPairs() do
    	if value ~= nil and #value > 0 then
        	args[name] = value
        end
    end
	if #args == 0 then return end
	duration = 10
	if args["durată"] == 'h' then 
		duration = 60 * 60
	elseif args["durată"] == 'z' then 
		duration = 60 * 60 * 24
	elseif args["durată"] == 'l' then 
		duration = 60 * 60 * 24 * 30
	elseif args["durată"] == 'a' then 
		duration = 60 * 60 * 24 * 365 
	else
		duration = tonumber(args["durată"]) or 10
	end
	math.randomseed(math.floor(os.time() / duration))
	articol = args[math.random(1,#args)]
	articol = articol:match('^%s*(.*%S)') or ''
	mw.logObject(articol)
	return articleText(articol, nil) .. readMore(articol)
end

return pp