Module:MwJson: Difference between revisions

Update package: OSW Core
(Update package: OSW Core)
Tag: Reverted
(Update package: OSW Core)
 
(One intermediate revision by the same user not shown)
Line 84: Line 84:
local mode = p.defaultArg(args.mode, p.mode.header)
local mode = p.defaultArg(args.mode, p.mode.header)
--local merged_jsonschema = p.defaultArg(args.merged_jsonschema, {})
--local merged_jsonschema = p.defaultArg(args.merged_jsonschema, {})
local template = p.defaultArg(args.template, nil)
local templates = p.defaultArg(args.templates, {})
local templates = p.defaultArg(args.templates, {})
local recursive = p.defaultArg(args.recursive, true)
local recursive = p.defaultArg(args.recursive, true)
Line 95: Line 96:
if (mode == p.mode.header) then category_template_slot = p.slots.header_template end
if (mode == p.mode.header) then category_template_slot = p.slots.header_template end
if (categories == nil) then categories = p.getCategories({jsonschema=jsonschema, includeNamespace=true}).categories end
if (categories == nil) then categories = p.getCategories({jsonschema=jsonschema, includeNamespace=true, includeSchemas=true}).categories end
if (type(categories) ~= 'table') then categories = {categories} end
if (type(categories) ~= 'table') then categories = {categories} end
if (debug) then msg = msg .. "Supercategories: " .. mw.dumpObject(categories) .. "\n<br>" end
if (debug) then msg = msg .. "Supercategories: " .. mw.dumpObject(categories) .. "\n<br>" end
Line 102: Line 103:
--mw.logObject("Visit " .. category)
--mw.logObject("Visit " .. category)
if (debug) then msg = msg .. "Fetch slot " .. p.slots.jsonschema .. " from page " .. category .. "\n<br>" end
if (debug) then msg = msg .. "Fetch slot " .. p.slots.jsonschema .. " from page " .. category .. "\n<br>" end
local super_jsonschema_str = mw.slots.slotContent( p.slots.jsonschema , category )
local super_jsonschema = nil
if (super_jsonschema_str ~= nil) then
if p.splitString(category, ':')[1] == "JsonSchema" then super_jsonschema = p.loadJson({title=category, slot=p.slots.main}).json
super_jsonschema = mw.text.jsonDecode( super_jsonschema_str )
else super_jsonschema = p.loadJson({title=category, slot=p.slots.jsonschema}).json end
if (super_jsonschema ~= nil) then
if (recursive) then
if (recursive) then
local res = p.walkJsonSchema({jsonschema=super_jsonschema, jsonschemas=jsonschemas, templates=templates, mode=mode, visited=visited, root=false})
local res = p.walkJsonSchema({jsonschema=super_jsonschema, jsonschemas=jsonschemas, templates=templates, mode=mode, visited=visited, root=false})
Line 113: Line 115:
--mw.logObject("Store " .. category)
--mw.logObject("Store " .. category)
table.insert(visited, category)
table.insert(visited, category)
jsonschemas[category] = mw.text.jsonDecode( super_jsonschema_str ) --keep a copy of the schema, super_jsonschema passed by references gets modified
jsonschemas[category] = p.copy( super_jsonschema ) --keep a copy of the schema, super_jsonschema passed by references gets modified
--jsonschema = p.tableMerge(jsonschema, super_jsonschema) --merge superschema is done by the caller
--jsonschema = p.tableMerge(jsonschema, super_jsonschema) --merge superschema is done by the caller
end
end
Line 151: Line 154:
local msg = ""
local msg = ""
local res = p.defaultArg(args.jsondata, "")
local res = p.defaultArg(args.jsondata, "")
local root = p.defaultArg(args.root, true) -- first entry into recursion
for k,v in pairs(jsondata) do
for k,v in pairs(jsondata) do
Line 174: Line 178:
end
end
elseif type(v) == 'table' then  
elseif type(v) == 'table' then  
if (v[1] == nil) then --key value array = object/dict
if (p.tableLength(v) > 0 and v[1] == nil) then --key value array = object/dict
local sub_res = p.expandEmbeddedTemplates({frame=frame, jsondata=v, jsonschema=p.defaultArgPath(jsonschema, {"properties", k}, {}), template=eval_template, mode=mode, stringify_arrays=stringify_arrays})
local sub_res = p.expandEmbeddedTemplates({frame=frame, jsondata=v, jsonschema=p.defaultArgPath(jsonschema, {"properties", k}, {}), template=eval_template, mode=mode, stringify_arrays=stringify_arrays, root=false})
msg = msg .. sub_res.debug_msg
msg = msg .. sub_res.debug_msg
jsondata[k] = sub_res.res
jsondata[k] = sub_res.res
Line 195: Line 199:


if type(e) == 'table' then
if type(e) == 'table' then
local sub_res = p.expandEmbeddedTemplates({frame=frame, jsondata=e, jsonschema=p.defaultArgPath(jsonschema, {"properties", k, "items"}, {}), template=eval_template, mode=mode, stringify_arrays=stringify_arrays})
local sub_res = p.expandEmbeddedTemplates({frame=frame, jsondata=e, jsonschema=p.defaultArgPath(jsonschema, {"properties", k, "items"}, {}), template=eval_template, mode=mode, stringify_arrays=stringify_arrays, root=false})
msg = msg .. sub_res.debug_msg
msg = msg .. sub_res.debug_msg
if (type(sub_res.res) == 'table') then  
if (type(sub_res.res) == 'table') then  
Line 207: Line 211:
--evaluate single array item string as json {"self": "<value>", ".": "<value>"} => does not work since jsondata is an object
--evaluate single array item string as json {"self": "<value>", ".": "<value>"} => does not work since jsondata is an object
--e = p.expandEmbeddedTemplates({frame=frame, jsondata={["self"]=e,["."]=e}, jsonschema=p.defaultArgPath(jsonschema, {"properties", k, "items"}, {}), template=eval_template, mode=mode, stringify_arrays=stringify_arrays})
--e = p.expandEmbeddedTemplates({frame=frame, jsondata={["self"]=e,["."]=e}, jsonschema=p.defaultArgPath(jsonschema, {"properties", k, "items"}, {}), template=eval_template, mode=mode, stringify_arrays=stringify_arrays, root=false})


Line 230: Line 234:
if (template == nil) then  
if (template == nil and root == false) then -- don't stringify root json objects
local templates = jsondata[p.keys.template]
local templates = jsondata[p.keys.template]
if (templates == nil) then templates = p.defaultArg(jsonschema[p.keys.template], {}) end
if (templates == nil) then templates = p.defaultArg(jsonschema[p.keys.template], {}) end
Line 242: Line 246:
end
end
if template ~= nil then
if (template ~= nil and root == false) then -- don't stringify root json objects
if (template.type == "wikitext") then
if (template.type == "wikitext") then
for k,v in pairs(jsondata) do
for k,v in pairs(jsondata) do
Line 294: Line 298:
jsonschema = expand_res.json
jsonschema = expand_res.json
--mw.log(mw.text.jsonEncode(jsonschema))
--mw.log(mw.text.jsonEncode(jsonschema))
 
local display_label = p.defaultArgPath(jsondata, {p.keys.name}, "")
 
if (display_label == "" or (title.nsText ~= "Category" and title.nsText ~= "Property")) then
display_label = p.defaultArgPath(jsondata, {p.keys.label, 1, p.keys.text}, "") --prefere label for all non-category and non-property pages
end
local jsonld = p.copy(jsondata)
local jsonld = p.copy(jsondata)
local json_data_store = p.copy(jsondata)
local json_data_store = p.copy(jsondata)
Line 540: Line 540:
local jsonschema = p.defaultArg(args.jsonschema, {})
local jsonschema = p.defaultArg(args.jsonschema, {})
local includeNamespace = p.defaultArg(args.includeNamespace, false)
local includeNamespace = p.defaultArg(args.includeNamespace, false)
local includeSchemas = p.defaultArg(args.includeSchemas, false)
local categories = {}
local categories = {}
Line 546: Line 547:
--properties['@category'] = {}
--properties['@category'] = {}
for k, entry in pairs(allOf) do
for k, entry in pairs(allOf) do
if type(entry) == 'table' then -- "allOf": [{"$ref": "/wiki/Category:Test?action=raw"}]
local refs = nil
for p, v in pairs(entry) do
if type(entry) == 'table' then refs = entry -- "allOf": [{"$ref": "/wiki/Category:Test?action=raw"}]
if (p == '$ref') then
else refs = {entry} end-- "allOf": {"$ref": "/wiki/Category:Test?action=raw"}
for category in v:gmatch("Category:([^?]+)") do -- e.g. "/wiki/Category:Test?action=raw"
for p, v in pairs(entry) do
if (includeNamespace) then category = "Category:" .. category end
if (p == '$ref') then
    table.insert(categories, category)
for category in v:gmatch("Category:([^?]+)") do -- e.g. "/wiki/Category:Test?action=raw"
if (includeNamespace) then category = "Category:" .. category end
    table.insert(categories, category)
end
if includeSchemas then
for schema in v:gmatch("JsonSchema:([^?]+)") do -- e.g. "/wiki/JsonSchema:Test?action=raw"
if (includeNamespace) then schema = "JsonSchema:" .. schema end
    table.insert(categories, schema)
end
end
end
end
end
end
else -- "allOf": {"$ref": "/wiki/Category:Test?action=raw"}
if (k == '$ref') then
for category in entry:gmatch("Category:([^?]+)") do -- e.g. "/wiki/Category:Test?action=raw"
if (includeNamespace) then category = "Category:" .. category end
table.insert(categories, category)
end
end
end
end
end
end
Line 745: Line 746:
context = p.tableMerge(context, subcontext) -- pull up nested context
context = p.tableMerge(context, subcontext) -- pull up nested context
local values = {}
local values = {}
if (v[1] == nil) then --key value array = object/dict
if (p.tableLength(v) > 0 and v[1] == nil) then --key value array = object/dict => subobject
local subproperties_res = p.getSemanticProperties({jsonschema=schema, jsondata=v, properties=p.copy(subobject_properties), store=true, root=false, debug=debug, context=context, subschema=schema_properties[k], parent_schema_property=property_data[k]})
local subproperties_res = p.getSemanticProperties({jsonschema=schema, jsondata=v, properties=p.copy(subobject_properties), store=true, root=false, debug=debug, context=context, subschema=schema_properties[k], parent_schema_property=property_data[k]})
local id = subproperties_res.id --subobject_id
local id = subproperties_res.id --subobject_id