Lompat ke isi

Modul:AutoRelated: Perbedaan antara revisi

Dari Wikisporta, ensiklopedia olahraga
k Melindungi "Modul:AutoRelated" ([Sunting=Hanya untuk pengurus] (selamanya) [Pindahkan=Hanya untuk pengurus] (selamanya))
Tanda: Suntingan perangkat seluler Suntingan peramban seluler
Tidak ada ringkasan suntingan
Baris 13: Baris 13:
         link = mw.text.trim(link)
         link = mw.text.trim(link)
         if link ~= title.text and not link:find(":") then
         if link ~= title.text and not link:find(":") then
            -- Cek apakah halaman target benar-benar ada (bukan redlink)
             local targetTitle = mw.title.new(link)
             local targetTitle = mw.title.new(link)
             if targetTitle and targetTitle.exists and not seen[link] then
           
            -- Validasi ketat:
            -- 1. Halaman harus ada (exists)
            -- 2. Bukan halaman pengalihan (not isRedirect) -> Mencegah redirect page terpilih!
            -- 3. Belum pernah dimasukkan (not seen)
             if targetTitle and targetTitle.exists and not targetTitle.isRedirect and not seen[link] then
                 seen[link] = true
                 seen[link] = true
                 table.insert(validLinks, link)
                 table.insert(validLinks, link)
Baris 27: Baris 31:
     end
     end
      
      
     -- Acak (shuffle) array secara pseudo-random yang stabil
     -- Acak (shuffle) array secara stabil berdasarkan ID halaman
     math.randomseed(os.time() + title.id)
     math.randomseed(os.time() + title.id)
     for i = #validLinks, 2, -1 do
     for i = #validLinks, 2, -1 do
Baris 38: Baris 42:
     local count = math.min(3, #validLinks)
     local count = math.min(3, #validLinks)
     for i = 1, count do
     for i = 1, count do
        -- Di sinilah kita panggil tag asli ekstensi related secara murni!
         result = result .. "{{#related:" .. validLinks[i] .. "}}\n"
         result = result .. "{{#related:" .. validLinks[i] .. "}}\n"
     end
     end

Revisi per 27 Oktober 2026 14.11

Dokumentasi untuk modul ini dapat dibuat di Modul:AutoRelated/doc

local p = {}

function p.main(frame)
    local title = mw.title.getCurrentTitle()
    local wikitext = title:getContent() or ""
    
    local validLinks = {}
    local seen = {}
    
    -- Cari semua pola wikilink [[Judul Artikel]] di dalam wikitext
    for link in wikitext:gmatch("%[%[([^%]|#]+)%|?.-%]%]") do
        -- Bersihkan spasi dan pastikan bukan halaman spesial atau halaman diri sendiri
        link = mw.text.trim(link)
        if link ~= title.text and not link:find(":") then
            local targetTitle = mw.title.new(link)
            
            -- Validasi ketat:
            -- 1. Halaman harus ada (exists)
            -- 2. Bukan halaman pengalihan (not isRedirect) -> Mencegah redirect page terpilih!
            -- 3. Belum pernah dimasukkan (not seen)
            if targetTitle and targetTitle.exists and not targetTitle.isRedirect and not seen[link] then
                seen[link] = true
                table.insert(validLinks, link)
            end
        end
    end
    
    -- Jika tidak ada link valid, kembalikan kosong
    if #validLinks == 0 then
        return ""
    end
    
    -- Acak (shuffle) array secara stabil berdasarkan ID halaman
    math.randomseed(os.time() + title.id)
    for i = #validLinks, 2, -1 do
        local j = math.random(i)
        validLinks[i], validLinks[j] = validLinks[j], validLinks[i]
    end
    
    -- Ambil maksimal 3 link
    local result = ""
    local count = math.min(3, #validLinks)
    for i = 1, count do
        result = result .. "{{#related:" .. validLinks[i] .. "}}\n"
    end
    
    return frame:preprocess(result)
end

return p