وحدة:اخر مقالة

من موسوعة جياي كورمنج
مراجعة ٢٠:٠٣، ١٥ ديسمبر ٢٠٢٣ بواسطة Iwings (نقاش | مساهمات)

يمكن إنشاء صفحة توثيق الوحدة في وحدة:اخر مقالة/شرح

-- Module code: وحدة:اخر_مقالة

local p = {}

function p.getLastArticleFromCategory(frame)
    local wiki_api_endpoint = "https://ar.ciyaye-kurmenc.com/api.php"
    local category_name = "قرى_منطقة_عفرين"

    local params = {
        action = "query",
        format = "json",
        list = "categorymembers",
        cmtitle = "Category:" .. category_name,
        cmlimit = 1,
        cmprop = "title",
        cmtype = "page",
        cmsort = "timestamp",
        cmdir = "desc",
    }

    local url = wiki_api_endpoint .. "?" .. mw.text.urlEncode(mw.text.arrayToQueryString(params))
    local response = mw.http.get(url)

    if response then
        local data = mw.text.jsonDecode(response)
        
        if data and data.query and data.query.categorymembers and #data.query.categorymembers > 0 then
            local last_article_title = data.query.categorymembers[1].title
            return "<h2>Last Article in " .. category_name .. " Category:</h2><p>" .. last_article_title .. "</p>"
        else
            return "<p>No articles found in the " .. category_name .. " category.</p>"
        end
    else
        return "<p>Error fetching data from the API.</p>"
    end
end

return p