Page MenuHomePhabricator

mw.ext.UnlinkedWikibase should return 1-indexed arrays
Closed, ResolvedPublicBUG REPORT

Description

The below is an example claims clause from the result of a mw.ext.UnlinkedWikibase.getEntity() call:

table#1 {
    [0] = table#2 {
        ["id"] = "Q28700152$a0680b11-4a48-2781-25ff-e9ef637db19d",
        ["mainsnak"] = table#3 {
            ["datatype"] = "quantity",
            ["datavalue"] = table#4 {
                ["type"] = "quantity",
                ["value"] = table#5 {
                    ["amount"] = "+19194",
                    ["unit"] = "1",
                },
            },
            ["hash"] = "d0917f8b8043f0fd178d1d42c3fabb213a41aa9d",
            ["property"] = "P1104",
            ["snaktype"] = "value",
        },
        ["qualifiers"] = table#6 {
            ["P518"] = table#7 {
                [0] = table#8 {
                    ["datatype"] = "wikibase-item",
                    ["datavalue"] = table#9 {
                        ["type"] = "wikibase-entityid",
                        ["value"] = table#10 {
                            ["entity-type"] = "item",
                            ["id"] = "Q15138389",
                            ["numeric-id"] = 15138389,
                        },
                    },
                    ["hash"] = "ca3d21d2d7076c6cea1eb2d2e001c3510b423808",
                    ["property"] = "P518",
                    ["snaktype"] = "value",
                },
            },
            ["P585"] = table#11 {
                [0] = table#12 {
                    ["datatype"] = "time",
                    ["datavalue"] = table#13 {
                        ["type"] = "time",
                        ["value"] = table#14 {
                            ["after"] = 0,
                            ["before"] = 0,
                            ["calendarmodel"] = "http://www.wikidata.org/entity/Q1985727",
                            ["precision"] = 11,
                            ["time"] = "+2023-01-07T00:00:00Z",
                            ["timezone"] = 0,
                        },
                    },
                    ["hash"] = "204a7a442fb66a4f00181a368dae5cf42aed4456",
                    ["property"] = "P585",
                    ["snaktype"] = "value",
                },
            },
        },
        ["qualifiers-order"] = table#15 {
            "P518",
            [0] = "P585",
        },
        ["rank"] = "normal",
        ["type"] = "statement",
    },
}

while an example claims clause from the result of a mw.wikibase.getEntity() call:

table#1 {
    table#2 {
        ["id"] = "Q1$c363f1e2-4306-f551-7322-6340b09df4d6",
        ["mainsnak"] = table#3 {
            ["datatype"] = "wikibase-item",
            ["datavalue"] = table#4 {
                ["type"] = "wikibase-entityid",
                ["value"] = table#5 {
                    ["entity-type"] = "item",
                    ["id"] = "Q22",
                    ["numeric-id"] = 22,
                },
            },
            ["property"] = "P22",
            ["snaktype"] = "value",
        },
        ["rank"] = "normal",
        ["type"] = "statement",
    },
    table#6 {
        ["id"] = "Q1$896be192-4888-7c7e-8b76-8aa9dc8e4330",
        ["mainsnak"] = table#7 {
            ["datatype"] = "wikibase-item",
            ["datavalue"] = table#8 {
                ["type"] = "wikibase-entityid",
                ["value"] = table#9 {
                    ["entity-type"] = "item",
                    ["id"] = "Q23",
                    ["numeric-id"] = 23,
                },
            },
            ["property"] = "P22",
            ["snaktype"] = "value",
        },
        ["rank"] = "normal",
        ["type"] = "statement",
    },
}

What should have happened instead?:

Array should be 1-indexed in Lua.

Software version (skip for WMF-hosted wikis like Wikipedia):

REL1_39

Other information (browser name/version, screenshots, etc.):

Event Timeline

For any wiki administrators:
This issue make the ipairs() do not work. I've had to apply the following function to the output to work around it:

local function convertTo1IndexedArray(array)
	local newArray = {}

	local zeroIndexed = array[0] ~= nil
	for k, v in pairs(array) do
		if type(v) == 'table' then
			v = convertTo1IndexedArray(v)
		end
		local newK = zeroIndexed and type(k) == 'number' and k+1 or k
		newArray[newK] = v
	end
	return newArray
end

Change 894174 had a related patch set uploaded (by Samwilson; author: Samwilson):

[mediawiki/extensions/UnlinkedWikibase@master] Convert arrays to 1-indexed for Lua

https://gerrit.wikimedia.org/r/894174

@Lens0021 thanks for pointing this out, and making the fix! I've moved the array-fixing to be in PHP in the above patch, although I'm not sure which side it makes most sense to put it (it's perhaps easier to write tests for PHP in the current set-up, which is why I put it there).

Change 894175 had a related patch set uploaded (by Samwilson; author: Samwilson):

[integration/config@master] Add UnlinkedWikibase extension dependency on Scribunto

https://gerrit.wikimedia.org/r/894175

Change 894175 merged by jenkins-bot:

[integration/config@master] Add UnlinkedWikibase extension dependency on Scribunto

https://gerrit.wikimedia.org/r/894175

Change 894174 merged by jenkins-bot:

[mediawiki/extensions/UnlinkedWikibase@master] Convert arrays to 1-indexed for Lua

https://gerrit.wikimedia.org/r/894174

Samwilson claimed this task.

Merged and released in 3.0.0.

Change 961914 had a related patch set uploaded (by Samwilson; author: Samwilson):

[mediawiki/extensions/UnlinkedWikibase@REL1_40] Convert arrays to 1-indexed for Lua

https://gerrit.wikimedia.org/r/961914

Change 961914 merged by jenkins-bot:

[mediawiki/extensions/UnlinkedWikibase@REL1_40] Convert arrays to 1-indexed for Lua

https://gerrit.wikimedia.org/r/961914