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: Manual revert
Line 1: Line 1:
local p = {}
local p = {}
local json = require("Module:Json")


-- Funktion zum Durchsuchen der Seiten nach dem angegebenen Titelwert
function p.getProjectData()
function p.getDataByTitle(titleParam, titleValue, targetParam)
     local jsonPage = mw.title.new("Data:Project/41")
     local pages = mw.title.new('Special:PrefixIndex/Data:' .. titleValue)
     if jsonPage and jsonPage.exists then
 
         local content = jsonPage:getContent()
    -- Überprüfen, ob die Seiten existieren und durchsuchbar sind
        local jsonData = mw.text.jsonDecode(content)
     if pages and pages.exists then
        if jsonData and jsonData.schemas and jsonData.schemas.Project then
         local results = {}
            return jsonData.schemas.Project.objective
 
        else
        -- Iteration über die Seiten
             return "Error: Invalid JSON structure"
        for _, page in ipairs(mw.ext.pageinfo.getPagesInCategory(pages.prefixedText)) do
            local content = mw.title.new(page.fullText)
           
            -- Überprüfen, ob die Seite existiert und das Content-Model JSON ist
            if content and content.exists and content.contentModel == "json" then
                local data = mw.text.jsonDecode(content:getContent())
               
                -- Überprüfen, ob der angegebene Parameter existiert und den richtigen Wert hat
                if data and data[titleParam] == titleValue then
                    table.insert(results, data[targetParam])
                end
             end
         end
         end
        -- Rückgabe der Ergebnisse
        return results
     else
     else
        -- Wenn die Seiten nicht existieren oder nicht durchsuchbar sind, wird ein Fehler ausgegeben
         return "Error: JSON page does not exist"
         return "Error: Unable to find pages with the given prefix"
     end
     end
end
end


return p
return p

Revision as of 17:18, 16 March 2024

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

local p = {}

function p.getProjectData()
    local jsonPage = mw.title.new("Data:Project/41")
    if jsonPage and jsonPage.exists then
        local content = jsonPage:getContent()
        local jsonData = mw.text.jsonDecode(content)
        if jsonData and jsonData.schemas and jsonData.schemas.Project then
            return jsonData.schemas.Project.objective
        else
            return "Error: Invalid JSON structure"
        end
    else
        return "Error: JSON page does not exist"
    end
end

return p