Module:Lua json: Difference between revisions

From Visual Data Wiki
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 1: Line 1:
local p = {}
local p = require('Module:Http')
local json = require('Module:Json')
 
local function queryPages(titleParam, titleValue)
    local url = mw.uri.fullUrl('api.php', {action = 'query', format = 'json', list = 'allpages', apnamespace = 486})
    local response = p.get(url)
    local data = json.decode(response)
 
    if not data or not data.query or not data.query.allpages then
        return {}
    end
 
    local pages = {}
    for _, page in ipairs(data.query.allpages) do
        table.insert(pages, page.title)
    end
 
    return pages
end


function p.getDataByTitle(titleParam, titleValue, targetParam)
function p.getDataByTitle(titleParam, titleValue, targetParam)
     local pages = mw.site.indexPages("Data:")
     local pages = queryPages(titleParam, titleValue)
     local results = {}
     local results = {}


     for _, page in ipairs(pages) do
     for _, pageTitle in ipairs(pages) do
         local jsonPage = mw.title.new(page)
         local jsonPage = mw.title.new(pageTitle)
         if jsonPage and jsonPage.exists then
         if jsonPage and jsonPage.exists and jsonPage.contentModel == 'json' then
             local content = jsonPage:getContent()
             local content = jsonPage:getContent()
             local jsonData = mw.text.jsonDecode(content)
             local jsonData = json.decode(content)
             if jsonData and jsonData.schemas and jsonData.schemas.Project and jsonData.schemas.Project[titleParam] == titleValue then
             if jsonData and jsonData[titleParam] == titleValue then
                 table.insert(results, jsonData.schemas.Project[targetParam])
                 table.insert(results, jsonData[targetParam])
             end
             end
         end
         end

Revision as of 16:06, 16 March 2024

Documentation for this module may be created at Module:Lua json/doc

local p = require('Module:Http')
local json = require('Module:Json')

local function queryPages(titleParam, titleValue)
    local url = mw.uri.fullUrl('api.php', {action = 'query', format = 'json', list = 'allpages', apnamespace = 486})
    local response = p.get(url)
    local data = json.decode(response)

    if not data or not data.query or not data.query.allpages then
        return {}
    end

    local pages = {}
    for _, page in ipairs(data.query.allpages) do
        table.insert(pages, page.title)
    end

    return pages
end

function p.getDataByTitle(titleParam, titleValue, targetParam)
    local pages = queryPages(titleParam, titleValue)
    local results = {}

    for _, pageTitle in ipairs(pages) do
        local jsonPage = mw.title.new(pageTitle)
        if jsonPage and jsonPage.exists and jsonPage.contentModel == 'json' then
            local content = jsonPage:getContent()
            local jsonData = json.decode(content)
            if jsonData and jsonData[titleParam] == titleValue then
                table.insert(results, jsonData[targetParam])
            end
        end
    end

    return results
end

return p