Fandom

Wikia Polska

Moduł:Slider

645stron na
tej wiki
Dodaj nową stronę
Dyskusja0

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

--<source lang="lua">
local RandomSlider = {}
 
RandomSlider.modulo = nil
 
function RandomSlider.timeMod()
    return 86400 -- Co 1 dzień
    -- return 300 -- Co 5 minut
end
function RandomSlider.spread(frame)
    local slides = slides(frame.args)
    local tab = mw.html.create('table'):addClass('article-table sortable'):css('border', '1px solid #CCC')
 
    local counts = {100, 1000, 10000, 100000}
 
    local tr = tab:tag('tr')
        :tag('th')
            :wikitext('Nr')
            :done()
        :tag('th')
            :addClass('unsortable')
            :done()
    for _,v in ipairs(counts) do
        tr:tag('th'):wikitext('w ' .. v)
    end
 
    local res = {}
    for _,field in pairs(slides.fields) do
        local i = tostring(field.index)
        res[i] = {}
        for _,v in ipairs(counts) do
            res[i][tostring(v)] = 0
        end
    end
    local seed = getSeed()
    for count = 1, counts[#counts] do
        local list = getFourSlides(slides, seed)
        local ids = {list[1], list[2], list[3], list[4]}
        for _,i in ipairs(ids) do
            i = tostring(i)
            for _,j in ipairs(counts) do
                if count <= j then
                    j = tostring(j)
                    res[i][j] = res[i][j] + 1
                end
            end
        end
        seed = seed + 1
    end
    for _,field in pairs(slides.fields) do
        local i = tostring(field.index)
        local tr = tab:tag('tr'):css('text-align', 'right')
        tr
            :tag('td')
                :css('text-align', 'center')
                :wikitext(i)
                :done()
            :tag('td')
                :css('text-align', 'center')
                :css('padding', '1px')
                :wikitext('[[File:' .. field['Obraz'] .. '|x32px|link=' .. field['Link'] .. '|' .. field['Tytuł'] .. ']]')
                :done()
        for _,v in ipairs(counts) do
            tr:tag('td'):wikitext(res[i][tostring(v)])
        end
    end
    return tab
end
function RandomSlider.seeds(frame)
    local slides = slides(frame.args)
    local tab = mw.html.create('table'):addClass('article-table'):css('border', '1px solid #CCC')
 
    tab
        :tag('tr')
            :tag('th')
                :wikitext('Ziarno')
                :done()
            :tag('th')
                :wikitext('Slajd #1')
                :done()
            :tag('th')
                :wikitext('#2')
                :done()
            :tag('th')
                :wikitext('#3')
                :done()
            :tag('th')
                :wikitext('#4')
                :done()
 
    local first = true
    local seed = getSeed()
    local startSeed = seed - (seed % RandomSlider.modulo)
    local endSeed = startSeed - 1 + RandomSlider.modulo * 2
 
    for i = startSeed, endSeed do
        local list = getFourSlides(slides, i)
 
        if i > startSeed and i % RandomSlider.modulo == 0 then
            tab:tag('tr'):tag('th'):attr('colspan', '5'):css('text-align', 'center'):wikitext('Losowanie')
        end
        local timestamp = i*RandomSlider.timeMod()
        local tr = tab:tag('tr')
            :tag('td')
                :css('line-height', '100%')
                :wikitext(tostring(i) .. '<div style="font-size:80%">' .. mw.getCurrentFrame():preprocess('{{#time:H:i:s|@' .. tostring(timestamp) .. '}}') .. '</div>')
                :done()
        if i == seed then tr:addClass('accent') end
 
        for _,i in ipairs(list) do
            local field = slides.fields[i]
            tr
                :tag('td')
                    :css('padding', '2px')
                    :wikitext('[[File:' .. field['Obraz'] .. '|100px|link=' .. field['Link'] .. '|#' .. field.index .. ' – ' .. field['Tytuł'] .. ']]')
                    :done()
        end
    end
    return tab
end
function RandomSlider.slider(frame)
    local slides = slides(frame.args)
    local list = getFourSlides(slides, getSeed())
    local lines = {}
    for _,i in ipairs(list) do
        local field = slides.fields[i]
        table.insert(lines, mw.ustring.format( 'File:%s|%s|link=%s|linktext=%s', field['Obraz'], field['Tytuł'], field['Link'], field['Opis'] ))
    end
    return mw.getCurrentFrame():preprocess('<gallery data-seed="' .. tostring(seed) .. '" type="slider">\n' .. table.concat(lines, '\n') .. '\n</gallery>')
end
function RandomSlider.list(frame)
    local slides = slides(frame.args, false)
    local tab = mw.html.create('table'):addClass('article-table'):css('width', '100%'):css('border', '1px solid #CCC')
    tab
        :tag('tr')
            :tag('th')
                :wikitext('Nr')
                :css('width', '25px')
                :done()
            :tag('th')
                :wikitext('Obraz')
                :done()
            :tag('th')
                :wikitext('Szczegóły')
                :attr('colspan', '2')
                :done()
 
    local count, count_invalid, count_inactive = 0, 0, 0
    local first = true
    for _,i in ipairs(slides.indexes) do
        field = slides.fields[i]
        if first then
            first = false
        else
            tab
                :tag('tr')
                    :tag('td')
                        :attr('colspan', '4')
                        :css('padding', '2px')
        end
        local tr1 = tab
            :tag('tr')
                :tag('td'):wikitext(tostring(i))
                    :attr('rowspan', '3')
                    :css('text-align', 'center')
                    :done()
                :tag('td'):attr('rowspan', '3')
                    :css('padding', '1px')
                    :addClass('cs-slider-image')
                    :wikitext('[[File:' .. field['Obraz'] .. '|x108px]]')
                    :css('width', '201px')
                    :css('height', '108px')
                    :css('border', '1px solid #CCC')
                    :done()
                :tag('td'):wikitext('\'\'\'Tytuł:\'\'\'')
                    :css('width', '1px')
                    :css('white-space', 'nowrap')
                    :done()
                :tag('td'):wikitext(field['Tytuł'])
                    :done()
        local tr2 = tab
            :tag('tr')
                :tag('td'):wikitext('\'\'\'Opis:\'\'\'')
                    :done()
                :tag('td'):wikitext(field['Opis'])
                    :done()
        local tr3 = tab
            :tag('tr')
                :tag('td'):wikitext('\'\'\'Link:\'\'\'')
                    :done()
                :tag('td'):wikitext(field['Link'])
                    :done()
 
        if not isValid(field) then
            tr1:css('color', 'red'):attr('title', 'Błędny slajd - wymagane są wszystkie 4 parametry')
            tr2:css('color', 'red'):attr('title', 'Błędny slajd - wymagane są wszystkie 4 parametry')
            tr3:css('color', 'red'):attr('title', 'Błędny slajd - wymagane są wszystkie 4 parametry')
            count_invalid = count_invalid+1
        elseif not isActive(field) then
            tr1:css('opacity', '.3'):attr('title', 'Slajd wyłączony w związku z brakiem aktywności')
            tr2:css('opacity', '.3'):attr('title', 'Slajd wyłączony w związku z brakiem aktywności')
            tr3:css('opacity', '.3'):attr('title', 'Slajd wyłączony w związku z brakiem aktywności')
            count_inactive = count_inactive+1
        else
            count = count+1
        end
    end
 
    local infotab = mw.html.create('table'):addClass('article-table'):css('border', '1px solid #CCC')
    infotab:tag('tr')
        :tag('td'):wikitext('\'\'\'Ilość poprawnych:\'\'\'')
            :done()
        :tag('td'):wikitext(count)
            :done()
    if count_invalid > 0 then
        infotab:tag('tr')
            :tag('td'):wikitext('\'\'\'Ilość niepoprawnych:\'\'\'')
                :done()
            :tag('td'):wikitext(count_invalid)
            :done()
    end
    if count_inactive > 0 then
        infotab:tag('tr')
            :tag('td'):wikitext('\'\'\'Ilość wyłączonych:\'\'\'')
                :done()
            :tag('td'):wikitext(count_inactive)
                :done()
    end
    infotab:tag('tr')
        :tag('td'):wikitext('\'\'\'Ilość zestawów:\'\'\'')
            :done()
        :tag('td'):wikitext(math.floor(count/4))
            :done()
    local timeMod = RandomSlider.timeMod()
    local timeTD = infotab:tag('tr')
            :tag('td'):wikitext('\'\'\'Nowy slider co:\'\'\'')
                :done()
            :tag('td')
    local h,m,s = timeFromSec(RandomSlider.timeMod())
    if h>0 then timeTD:wikitext(h .. 'h ') end
    if m>0 then timeTD:wikitext(m .. 'm ') end
    if s>0 then timeTD:wikitext(s .. 's ') end
 
    return infotab, tab
end
function timeFromSec(seconds)
    local hours, minutes = 0, 0
    minutes = math.floor(seconds/60)
    hours = math.floor(minutes/60)
    seconds = seconds - minutes*60
    minutes = minutes - hours*60
    return hours, minutes, math.floor(seconds)
end
function getSeed()
    return math.floor(RandomSlider.now/RandomSlider.timeMod())
end
function table.shuffle(tab)
    local n, order, res = #tab, {}, {}
    for i=1,n do order[i] = { rnd = math.random(), idx = i } end
    table.sort(order, function(a,b) return a.rnd < b.rnd end)
    for i=1,n do res[i] = tab[order[i].idx] end
    return res
end
function table.split(tab)
    local n, res = #tab, {}
    local pivot = math.random(2,n)
    for i=pivot,n do
        table.insert(res, tab[i])
    end
    for i=1,pivot-1 do
        table.insert(res, tab[i])
    end
    return res
end
function table.contains(tab, element)
    for _, value in pairs(tab) do
        if value == element then
            return true
        end
    end
    return false
end
function getFourSlides(slides, seed)
    seed = tonumber(seed)
    local indexes = randomizeSlides(slides, seed)
    local offset, list = (seed % RandomSlider.modulo)*4, {}
    for i=1+offset,4+offset do
        table.insert(list, indexes[i])
    end
    return list
end
function randomizeSlides(slides, seed)
    if RandomSlider.lists == nil then RandomSlider.lists = {} end
    seed = seed - (seed % RandomSlider.modulo)
    if RandomSlider.lists[tostring(seed)] ~= nil then return RandomSlider.lists[tostring(seed)] end
    math.randomseed(seed)
    local list = table.shuffle(table.split(table.shuffle(table.shuffle(slides.indexes))))
    RandomSlider.lists[tostring(seed)] = list
    return list
end
function isValid(field)
    return field['Link'] ~= nil and field['Tytuł'] ~= nil and field['Opis'] ~= nil and field['Obraz']
end
function isActive(field)
    return field['Wyłączony'] == nil
end
function slides(args, filter)
    if filter == nil then filter = true end
    local args = arguments(args)
 
    local fields = {}
    local indexes = {}
 
    for k, v in pairs(args) do
        local keySplit = mw.text.split(tostring(k) or '', ':')
        local index  = tonumber(mw.text.trim(tostring(keySplit[1])))
        local key    = mw.text.trim(tostring(keySplit[2]))
        local value  = mw.text.trim(tostring(v))
 
        if keySplit[2] ~= nil then
            if type(index) == 'number' and index > 0 then
                if fields[index] == nil then
                    table.insert(indexes, index)
                    fields[index] = {index=index}
                end
                fields[index][key] = value
            end
        else
            if not value then
                Skins.vars[mw.text.trim(tostring(k))] = value
            end
        end
    end
 
    if filter then
        indexes = {}
        for k, v in pairs(fields) do
            if not isValid(fields[k]) or not isActive(fields[k]) then
                fields[k] = nil
            else
                table.insert(indexes, k)
            end
        end
    end
    table.sort(indexes)
 
    if RandomSlider.modulo == nil then
        RandomSlider.modulo = math.floor(#indexes/4)
    else
        RandomSlider.modulo = math.min(math.floor(#indexes/4), RandomSlider.modulo)
    end
 
 
    RandomSlider.tz = tonumber(mw.getCurrentFrame():preprocess('{{#ifexpr:{{#time:U}}>={{#time:U|last Sun of Mar}} and {{#time:U|last Sun of Oct}}>{{#time:U}}|7200|3600}}')) -- +2h w letnim, +1h w zimowym - na bazie: http://spolecznosc.wikia.com/wiki/Blog_u%C5%BCytkownika:SH3VEK/Snippety:_Czas
    RandomSlider.now = os.time(os.date('!*t')) + RandomSlider.tz
 
    return {
        fields = fields,
        indexes = indexes,
    }
end
function arguments(origArgs)
    local args = {}
    for k, v in pairs(origArgs) do
        if type(v) == 'string' then v = mw.text.trim(v) end
        if v ~= '' then args[k] = v end
    end
    return args
end
 
return RandomSlider
--</source>

Więcej w Fandom

Losowa wiki