Lua error on MediaWiki
Asked Answered
I

2

8

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.

Inhumanity answered 11/11, 2013 at 8:32 Comment(6)
Including the code around line 302 is going to be helpful.Chromatolysis
The code is all taken from the wikipedia modules, the function that actually throws the error is: function renderMainTable() local tbl = HtmlBuilder.create('table') .attr('cellspacing', 0) .addClass('nowraplinks') .addClass(args.bodyclass) but, if I modify the code to skip that line, then the rest of the module throws the same error. en.wikipedia.org/wiki/Module:NavboxInhumanity
attr is nil, and from what I can tell by reading the Wikipedia documentation, this means that the HtmlBuilder.create('table') call is returning you a Lua table without that field, which can only mean that it isn't setting the builder metatable. Can you provide more information or code, if any has been modified?Jaclyn
The code is imported straight from Wikipedia - HtmlBuilder code is here: en.wikipedia.org/wiki/Module:HtmlBuilderInhumanity
Can you paste (or pastebin) the exact contents of your two module files?Chromatolysis
Navbox HtmlBuilder Thanks.Inhumanity
J
6

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!

Jaclyn answered 19/11, 2013 at 14:30 Comment(2)
Wow. Thanks for that! I can only guess that Wikipedia did something to 'correct' the double underscore when exporting the modules. Thanks again, and enjoy your 50 rep!Inhumanity
@Inhumanity I think you have to actually click on the bounty button to award it. atm it's still showing the bounty as open.Walloper
A
3

Let me say that it is very hard to attempt answering your question. In the original post you're not saying much that is of help to solve the issue. I'll just base upon this line of code you mentioned:

function renderMainTable() local tbl = HtmlBuilder.create('table') .attr('cellspacing', 0) .addClass('nowraplinks') .addClass(args.bodyclass)

I would probably try replacing it with this:

function renderMainTable() local tbl = HtmlBuilder.create('table')
Agbogla answered 17/11, 2013 at 17:15 Comment(1)
Hi, replacing that line moved the error to the next place where tbl is used. I'm sorry if I haven't given enough information, maybe because I've been inside this thing for too long I don't see what it looks like from the outside any more. The code is entirely copied from these two modules: en.wikipedia.org/wiki/Module:Navbox en.wikipedia.org/wiki/Module:HtmlBuilderInhumanity

© 2022 - 2024 — McMap. All rights reserved.