Modul:AutoRelated: Perbedaan antara revisi

←Membuat halaman berisi '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 a...'
Tanda: Suntingan perangkat seluler Suntingan peramban seluler
 
Tidak ada ringkasan suntingan
 
(2 revisi perantara oleh pengguna yang sama tidak ditampilkan)
Baris 10: Baris 10:
     -- Cari semua pola wikilink [[Judul Artikel]] di dalam wikitext
     -- Cari semua pola wikilink [[Judul Artikel]] di dalam wikitext
     for link in wikitext:gmatch("%[%[([^%]|#]+)%|?.-%]%]") do
     for link in wikitext:gmatch("%[%[([^%]|#]+)%|?.-%]%]") do
        -- Bersihkan spasi dan pastikan bukan halaman spesial atau halaman diri sendiri
         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: Halaman ada, bukan redirect, dan belum pernah dimasukkan
             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)
               
                -- OPTIMASI UTAMA: Berhenti mencari jika sudah terkumpul 10 link valid.
                -- Ini mencegah error "too many expensive function calls" pada artikel panjang.
                if #validLinks >= 5 then
                    break
                end
             end
             end
         end
         end
Baris 27: Baris 33:
     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 44:
     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