Modul:Pagina principală
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 isNilOrEmpty = require('Module:StringUtils').isNilOrEmpty
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]*)[Ii]nfocaset([aă])(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%5\'\'\'')
section = mw.ustring.gsub(section, '%{%{([\r\n]*)[Ii]nfobox(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
section = mw.ustring.gsub(section, '%{%{([\r\n]*)[Cc]utie(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
section = mw.ustring.gsub(section, '%{%{([\r\n]*)[Tt]axobox(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
section = mw.ustring.gsub(section, '%{%{([\r\n]*)[Oo]rdine_Religioase(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
section = mw.ustring.gsub(section, '%{%{([\r\n]*)[Rr]ăzboi(.*)%}%}([ \t\r\n]+)(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
section = mw.ustring.gsub(section, '%{%{([\r\n]*)[Mm]ultiple image(.*)%}%}(%s*[\r\n]+%s*)([^\t\r\n\']-)\'\'\'','%4\'\'\'')
section = mw.ustring.gsub(section, '%{%{([\r\n]*)[Qq]uote box(.*)%}%}(%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, '%({{[dD]ate biografice}}%)', '') end
if death then death = '—d. ' .. death else death = '' end
biodata = 'n. ' .. (wikidata.findOneValueNoRef('P569', qid) or '?') .. (death)
return mw.ustring.gsub(section, '{{[Dd]ate 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, "([aA])ltesensuri")
description = removeTemplate(description, "([aA])ltepersoane")
description = removeTemplate(description, "([dD])ezvoltă")
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, "[Dd]iscriminare")
description = removeTemplate(description, "[Cc]ultura României")
description = removeTemplate(description, "[Rr]edirect")
description = removeTemplate(description, "[Uu]niuni același sex Europa")
description = removeTemplate(description, "[Aa]vizmedical")
description = removeTemplate(description, "[Ii]ncrease")
description = removeTemplate(description, "[Dd]ecrease")
description = removeTemplate(description, "[Ll]egendă")
description = removeTemplate(description, "[Bb]ară laterală LGBT")
description = removeTemplate(description, "Participanți la Consiliul de Coroană din 17%-19 februarie 1918")
--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', '')
mw.logObject(description, "Before 2nd try for images")
description = mw.ustring.gsub(description, '%[%[Fișier:(.-)%]%]', '')
description = mw.ustring.gsub(description, '%[%[Imagine:(.-)%]%]', '')
description = mw.ustring.gsub(description, '%[%[Media:(.-)%]%]', '')
description = mw.ustring.gsub(description, '%[%[Fişier:(.-)%]%]', '')
description = mw.ustring.gsub(description, '%[%[File:(.-)%]%]', '')
description = mw.ustring.gsub(description, '%[%[Image:(.-)%]%]', '')
--remove infobox
description = removeInfobox(description)
description = removeTemplate(description, "Infocaseta") -- if the previous failed, retry
--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+', '')
--parameterize wikidata-enabled templates
if qid then
description = mw.ustring.gsub(description, '%{%{([\r\n]*)[Dd]ate biografice(%s*)%}%}', '{{Date biografice|q=' .. qid .. '}}')
description = mw.ustring.gsub(description, '%{%{([\r\n]*)[Pp]ronunție(%s*)%}%}', '{{Pronunție|q=' .. qid .. '}}')
description = mw.ustring.gsub(description, '%{%{([\r\n]*)[Ee]ste un fotbalist(%s*)%}%}', '{{este un fotbalist|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, data, compact)
ret = readMore(articol)
a1 = data.keys[math.random(1,data.len)]
a2 = a1
a3 = a1
while a2 == a1 do
a2 = data.keys[math.random(1,data.len)]
end
while a3 == a1 or a3 == a2 do
a3 = data.keys[math.random(1,data.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(data.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), 'seed')
math.randomseed(math.floor(os.time() / 10))
local type = 'all'
if frame.args then type = frame.args[1] end
local falist = {}
if fadata[type] then
falist = fadata[type]
else
falist = fadata.all
end
mw.logObject(falist, "falist")
local keys, len = {}, 0
for k,v in pairs(falist) do
len = len + 1
keys[len] = k
end
local index = math.random(1,len)
local data = {
keys = keys,
len = len,
list = falist
}
t = keys[index]
-- t = "Jurământul de fidelitate în teritoriile aflate sub administrația Consiliului Dirigent (1918-1920)"
q = falist[t]
return articleText(t, q) .. pp.articolBunJos(t, data, false)
end
function pp.getRandomIntroduction(frame)
local args = {}
qid = nil
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 ''
if isNilOrEmpty(args["arată_imagine"]) == false then
qid = mw.wikibase.getEntityIdForTitle(articol)
end
return articleText(articol, qid) .. readMore(articol)
end
return pp