Module:GetData: Difference between revisions

From Bodhicitta
((by SublimeText.Mediawiker))
Tag: Manual revert
No edit summary
Line 7: Line 7:
     end
     end


    local pSetData = mw.loadData('Module:GetData/'..frame.args['data'])
local pSetData = mw.loadData('Module:GetData/'..frame.args['data'])
    local parentArgs = frame:getParent().args
local parentArgs = frame:getParent().args
     local props = parentArgs.prop
     local props = parentArgs.prop
     local dataStore = {}
     local dataStore = {}


     for propValue in mw.ustring.gmatch(props, "([^;]+)") do
     for propValue in string.gmatch(props, "([^;]+)") do
        if propValue and pSetData[propValue] then  
    if propValue and pSetData[propValue] then  
             table.insert(dataStore, propValue .. '=' .. pSetData[propValue])
             table.insert(dataStore, propValue .. '=' .. pSetData[propValue])
             if parentArgs.sep then
             if parentArgs.sep then
                table.insert(dataStore, '+sep=' .. parentArgs.sep)
            table.insert(dataStore, '+sep=' .. parentArgs.sep)
             end
             end
         end
         end
     end
     end


    local dataStored = mw.smw.set( dataStore )
local dataStored = mw.smw.set( dataStore )


     if dataStored == true then
     if dataStored == true then
Line 33: Line 33:
function p.main(frame)
function p.main(frame)


     -- defining a couple of tables to be used
     -- bringing in property values from the appropriate set for this item
    local extData = {}
local pData = mw.loadData('Module:GetData/'..frame.args['data'])
    local pData = {}


     -- bringing in the arguments passed to the parent templates
     -- bringing in the arguments passed to the parent templates
    local parentArgs = frame:getParent().args
local parentArgs = frame:getParent().args


     -- bringing in property values from the appropriate set for this item
     -- bringing in the properties being requested and feeding into a table
     local props = parentArgs.prop
     local props = parentArgs.prop
    -- iterating through properties requested and fetching value from DRL
    for property in mw.ustring.gmatch(props, "([^;]+)") do
        local extDataTable = mw.ext.externalData.getExternalData{
            url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=%5B%5B' .. mw.uri.encode( mw.title.getCurrentTitle().subpageText ) .. '%5D%5D&po=' .. property .. '&p%5Bvaluesep%5D=;&p%5Bformat%5D=csv',
            data = {
                propvalue = property
            },
            format = 'CSV with header'
        }
        for _,data in ipairs(extDataTable) do
            if data.propvalue then
                pData[property] = data.propvalue
            end
        end
    end
    -- bringing in the properties being requested and feeding into a table
     local propTable = {}
     local propTable = {}
     for property in mw.ustring.gmatch(props, "([^;]+)") do
     for propValue in string.gmatch(props, "([^;]+)") do
         table.insert(propTable, property)
         table.insert(propTable, propValue)
     end
     end


Line 104: Line 84:
                 if parentArgs.sep then
                 if parentArgs.sep then
                     local sep = parentArgs.sep
                     local sep = parentArgs.sep
                     for value in mw.ustring.gmatch(pdString, "([^" .. sep .. "]+)") do
                     for value in string.gmatch(pdString, "([^" .. sep .. "]+)") do
                         table.insert(pdTable, value)
                         table.insert(pdTable, value)
                     end
                     end
Line 116: Line 96:
                 for k,v in pairs(pdTable) do
                 for k,v in pairs(pdTable) do
                     if v then
                     if v then
                         mapdVal = mw.ustring.gsub(map, "@@@", v)
                         mapdVal = string.gsub(map, "@@@", v)
                         pdString = pdString .. mapdVal
                         pdString = pdString .. mapdVal
                     end
                     end
Line 124: Line 104:
             -- checking if a printsep parameter has been defined and separating each mapped value accordingly
             -- checking if a printsep parameter has been defined and separating each mapped value accordingly
             if parentArgs.printsep then
             if parentArgs.printsep then
                finalString = finalString .. parentArgs.printsep .. frame:preprocess( pdString )
            finalString = finalString .. parentArgs.printsep .. frame:preprocess( pdString )
                if mw.ustring.sub(finalString, 1, 1) == parentArgs.printsep then
            if mw.ustring.sub(finalString, 1, 1) == parentArgs.printsep then
                    finalString = mw.ustring.sub(finalString, 2)
finalString = mw.ustring.sub(finalString, 2)
                end
end
             else
             else
                finalString = finalString .. frame:preprocess( pdString )
            finalString = finalString .. frame:preprocess( pdString )
             end
             end
         end
         end
Line 145: Line 125:
     -- checking if a silent flag has been added, if so and string is empty anyways, return string, otherwise, check for default parent argument and return that.
     -- checking if a silent flag has been added, if so and string is empty anyways, return string, otherwise, check for default parent argument and return that.
     if parentArgs.silent ~= "yes" then
     if parentArgs.silent ~= "yes" then
        if finalString ~= "" and finalString ~= nil then
    if finalString ~= "" and finalString ~= nil then
            return finalString
        return finalString
        else
    else
            if parentArgs.default then
    if parentArgs.default then
                return parentArgs.default
    return parentArgs.default
            end
    end
        end
    end
    end
end
 
end
end


return p
return p

Revision as of 13:49, 2 September 2024

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

local p = {}

function p.set(frame)

    if not mw.smw then
        return "mw.smw module not found"
    end

	local pSetData = mw.loadData('Module:GetData/'..frame.args['data'])
	local parentArgs = frame:getParent().args
    local props = parentArgs.prop
    local dataStore = {}

    for propValue in string.gmatch(props, "([^;]+)") do
    	if propValue and pSetData[propValue] then 
            table.insert(dataStore, propValue .. '=' .. pSetData[propValue])
            if parentArgs.sep then
            	table.insert(dataStore, '+sep=' .. parentArgs.sep)
            end
        end
    end

	local dataStored = mw.smw.set( dataStore )

    if dataStored == true then
        return ''
    else
        return 'An error occurred during the storage process. Message reads ' .. dataStored.error
    end

end

function p.main(frame)

    -- bringing in property values from the appropriate set for this item
	local pData = mw.loadData('Module:GetData/'..frame.args['data'])

    -- bringing in the arguments passed to the parent templates
	local parentArgs = frame:getParent().args

    -- bringing in the properties being requested and feeding into a table
    local props = parentArgs.prop
    local propTable = {}
    for propValue in string.gmatch(props, "([^;]+)") do
        table.insert(propTable, propValue)
    end

    -- checking if a single property called actually has a value, if not return nothing
    if #propTable == 1 then
        local valueCheck = propTable[1]
        if pData[valueCheck] == '' or pData[valueCheck] == nil then 
            if parentArgs.default then
                return parentArgs.default
            else
                return ''
            end
        end
    end

    -- defining our output variable
    local finalString = ""

    -- if an intro string has been defined, adding to output string now
    if parentArgs.intro then
        for k,v in ipairs(propTable) do
            if pData[v] ~= "" then
                finalString = parentArgs.intro
            end
        end
    end

    -- iterating through requested properties
    for i,prop in pairs(propTable) do
        if pData[prop] then

            -- feeding property value into a variable
            local pdString = pData[prop]

            -- checing if a "map" parameter has been defined
            if parentArgs.map then
                local pdTable = {}

                -- checking if a sep parameter has been defined and splitting values accordingly
                if parentArgs.sep then
                    local sep = parentArgs.sep
                    for value in string.gmatch(pdString, "([^" .. sep .. "]+)") do
                        table.insert(pdTable, value)
                    end
                else
                    table.insert(pdTable, pdString)
                end

                -- mapping each value from above
                local map = parentArgs.map
                pdString = ""
                for k,v in pairs(pdTable) do
                    if v then
                        mapdVal = string.gsub(map, "@@@", v)
                        pdString = pdString .. mapdVal
                    end
                end
            end

            -- checking if a printsep parameter has been defined and separating each mapped value accordingly
            if parentArgs.printsep then
            	finalString = finalString .. parentArgs.printsep .. frame:preprocess( pdString )
            	if mw.ustring.sub(finalString, 1, 1) == parentArgs.printsep then
					finalString = mw.ustring.sub(finalString, 2)
				end
            else
            	finalString = finalString .. frame:preprocess( pdString )
            end
        end
    end

    -- if an outro string has been defined, adding to output string now
    if parentArgs.outro then
        for k,v in ipairs(propTable) do
            if pData[v] ~= "" then
                finalString = finalString .. parentArgs.outro
            end
        end
    end

    -- checking if a silent flag has been added, if so and string is empty anyways, return string, otherwise, check for default parent argument and return that.
    if parentArgs.silent ~= "yes" then
    	if finalString ~= "" and finalString ~= nil then
	        return finalString
	    else
	    	if parentArgs.default then
	    		return parentArgs.default
	    	end
	    end
	end
end

return p