Search code examples
luamediawikimediawiki-templatesscribunto

Lua error on MediaWiki


I'm trying to set up a MediaWiki, and trying to use the Navbox template. I had everything working fine on my local machine, but when I copied it all to the server I get Lua script errors, specifically:

Lua error at line 302: attempt to call field 'attr' (a nil value).
Backtrace:
(tail call): ?
Module:Navbox:302: in function "renderMainTable"
Module:Navbox:348: in function "renderMainTable"
(tail call): ?
mw.lua:425: ?
(tail call): ?
[C]: in function "xpcall"
MWServer.lua:73: in function "handleCall"
MWServer.lua:266: in function "dispatch"
MWServer.lua:33: in function "execute"
mw_main.lua:7: in main chunk
[C]: ?

If I edit that file then it just gives error for all the other fields.

My server is running MediaWiki 1.20, if that makes a difference. I have tried with Scribunto 1.20, 1.21 and master (making changes to the engines to fit with 1.20).

If anyone can help, that would be great.

Edited modules: Navbox, HtmlBuilder.


Solution

  • Look very, very carefully at your pastebinned code compared to Wikipedia's code. In fact, I'd recommend performing a diff of the two.

    Your code

    metatable._index = function(t, key)
        local ret = rawget(t, key)
        if ret then
            return ret
        end
    
        ret = metatable[key]
        if type(ret) == 'function' then
            return function(...)
                return ret(t, ...)
            end
        else
            return ret
        end
    end
    

    Wikipedia

    metatable.__index = function(t, key)
        local ret = rawget(t, key)
        if ret then
            return ret
        end
    
        ret = metatable[key]
        if type(ret) == 'function' then
            return function(...) 
                return ret(t, ...) 
            end 
        else
            return ret
        end
    end
    

    Do you see the difference? Metamethods in Lua always start with two underscores __, not one. I'm not sure how your code got to the state that it's in, but this would very well explain all the troubles you've been having, even why attr was not accessible. It was due to the metatable's __index field lacking an underscore, which of course means that it would not be recognized at all. I'm surprised I noticed, since it's easy to miss that extra underscore when skimming.

    I would recommend restoring your HtmlBuilder module to its original state first then see if that fixes your issue. You might want to restore NavBox and any others you may have modified, if your modifications aren't too significant, but a diff would definitely tell you what's different between your versions.

    Just be mindful of what you change in the future, but don't be afraid to experiment so long as you have backups!