gnosygnu / xowa

xowa offline wiki application
Other
378 stars 40 forks source link

Wikibase: Return language for GetLabel, GetDesription (Module:Cycling race:4290 attempt to concatenate nil and string) #407

Closed desb42 closed 5 years ago

desb42 commented 5 years ago

An example of this error can be found in fr.wikipedia.org/wiki/Boels_Ladies_Tour_2016

invoke failed: Boels_Ladies_Tour_2016 {{#invoke:Cycling race|generalclassification|{{{1}}}|{{{references}}}}}
 [err 0] <gplx> =Module:Cycling race:4290 attempt to concatenate nil and string
  stack traceback:
  Module:Cycling race:4166: in function <Module:Cycling race:4165>
  mw.lua:531: in function <mw.lua:530>
  [Java]: in function '__index'
  g:/xowa/bin/any/xowa/xtns/Scribunto/engines/Luaj/MWServer.lua:75:
   in function <g:/xowa/bin/any/xowa/xtns/Scribunto/engines/Luaj/MWServer.lua:74>
  [Java]: in ?: ttl=Boels_Ladies_Tour_2016 excerpt={{#invoke:Cycling race|generalclassification|{{{1}}}|{{{references}}}}}
  [trace]:
    gplx.xowa.xtns.scribunto.Scrib_core.Handle_error(Scrib_core.java:216)
    gplx.xowa.xtns.scribunto.engines.luaj.Luaj_engine.Dispatch_as_kv_ary(Luaj_engine.java:80)
    gplx.xowa.xtns.scribunto.engines.luaj.Luaj_engine.CallFunction(Luaj_engine.java:59)
    gplx.xowa.xtns.scribunto.Scrib_core.Invoke(Scrib_core.java:182)
    gplx.xowa.xtns.scribunto.Scrib_invoke_func.Func_evaluate(Scrib_invoke_func.java:69)
    gplx.xowa.parsers.tmpls.Xot_invk_tkn_.Eval_func(Xot_invk_tkn_.java:30)
    gplx.xowa.parsers.tmpls.Xot_invk_tkn.Tmpl_evaluate(Xot_invk_tkn.java:258)
    gplx.xowa.parsers.tmpls.Xot_defn_tmpl.Tmpl_evaluate(Xot_defn_tmpl.java:86)
    gplx.xowa.parsers.tmpls.Xot_invk_tkn.Tmpl_evaluate(Xot_invk_tkn.java:288)
    gplx.xowa.parsers.tmpls.Xot_tmpl_wtr.Write_tkn(Xot_tmpl_wtr.java:103)
    gplx.xowa.parsers.tmpls.Xot_tmpl_wtr.Write_tkn(Xot_tmpl_wtr.java:35)
    gplx.xowa.parsers.tmpls.Xot_tmpl_wtr.Write_all(Xot_tmpl_wtr.java:23)
    gplx.xowa.parsers.Xop_parser.Expand_tmpl(Xop_parser.java:50)
    gplx.xowa.parsers.Xop_parser.Expand_tmpl(Xop_parser.java:44)
    gplx.xowa.parsers.Xop_parser.Parse_text_to_wdom(Xop_parser.java:102)
    gplx.xowa.parsers.Xow_parser_mgr.Parse(Xow_parser_mgr.java:99)
    gplx.xowa.addons.bldrs.mass_parses.parses.wkrs.Xomp_parse_wkr.Exec(Xomp_parse_wkr.java:131)
    gplx.xowa.addons.bldrs.mass_parses.parses.wkrs.Xomp_parse_wkr.Invk(Xomp_parse_wkr.java:190)
    gplx.Gfo_invk_.Invk_by_msg(Gfo_invk_.java:34)
    gplx.core.threads.Thread_adp.run(Thread_adp.java:33)
    java.lang.Thread.run(Thread.java:748)

Looking at lines 4272 - 4288

if countryID then
    flagLink = flag(countryID, timeOfRace)
    if available_list and country then
        if type(translations.list) == "function" then
            countryName = translations.list(countryID)
        end
        if countryName == '' then
            local label, lang = mw.wikibase.getLabelWithLang(countryID)
            --[[ Uses standard language fallback. Should not be nil, as all countries have English labels. ]]
            if lang == wikilang then
                countryName = label
            else
                countryName = label .. ' (' .. lang .. ')'
            end
        end
    end
end

This particular error seems to be triggered by line 4279 (the comment following is interesting) A countryID of Q212 (Ukraine) causes the problem

I think this is partly to do with the fact that the language id is `UA' (not UK?)

desb42 commented 5 years ago

in my frwiki logs there are 2271 error of this type

desb42 commented 5 years ago

I have just been reviewing this issue and think the problem may be due to the wrong number of values returned by the call from lua php.getLabel( id )

mw.wikibase.lua contains the following code

-- Get the label, label language for the given entity id, if specified,
-- or of the connected entity, if exists.
--
-- @param {string} [id]
wikibase.getLabelWithLang = function( id )
    checkTypeMulti( 'getLabelWithLang', 1, id, { 'string', 'nil' } )

    id = getIdOfConnectedItemIfNil( id )

    if id == nil then
        return nil, nil
    end

    return php.getLabel( id )
end

As far as I can tell the function GetLabel in 400_xowa\src\gplx\xowa\xtns\scribunto\libs\Scrib_lib_wikibase.java is returning one value, from the above code it should be returning two.

By the same token, php.getDescription( id ) which is GetDescription in the java file should also return two values

desb42 commented 5 years ago

On yet further examination, it seems that the GetLabel code

public boolean GetLabel(Scrib_proc_args args, Scrib_proc_rslt rslt) {            
    Wdata_doc wdoc = Get_wdoc_or_null(args, core, "GetLabel", true); 
    if (wdoc == null) 
        return rslt.Init_ary_empty();
    else
        return rslt.Init_obj(wdoc.Label_list__get_or_fallback(core.Lang()));
}

should be using the wiki language not the core language My proposed change is

public boolean GetLabel(Scrib_proc_args args, Scrib_proc_rslt rslt) {            
    Wdata_doc wdoc = Get_wdoc_or_null(args, core, "GetLabel", true); 
    if (wdoc == null) 
        return rslt.Init_ary_empty();
    else {
        Xowe_wiki wiki = core.Wiki();
        return rslt.Init_many_objs(wdoc.Label_list__get_or_fallback(wiki.Lang()), wiki.Lang().Key_bry());
    }
}

Not sure about GetDescription

desb42 commented 5 years ago

On further reflection the proposed change is not quite good enough

The function Label_list__get_or_fallback if it should fallback, the returned result should be that language (I assume 'en')

This function needs to return two values, the text and the actual language code This could be done by changing Wdata_doc.java to return the Wdata_langtext_itm instead of just the Text()

gnosygnu commented 5 years ago

As far as I can tell the function GetLabel in 400_xowa\src\gplx\xowa\xtns\scribunto\libs\Scrib_lib_wikibase.java is returning one value, from the above code it should be returning two.

Yeah, found the code line here: https://github.com/wikimedia/mediawiki-extensions-Wikibase/blob/6932770754e834dcc0a103221f7cc5205b5084b2/client/includes/DataAccess/Scribunto/WikibaseLanguageDependentLuaBindings.php#L73

Let me try to get it done this weekend

gnosygnu commented 5 years ago

As far as I can tell the function GetLabel in 400_xowa\src\gplx\xowa\xtns\scribunto\libs\Scrib_lib_wikibase.java is returning one value, from the above code it should be returning two.

By the way, forgot to say it, but thanks for identifying the issue!

Pushed with the commit above. Modified 20 files because I was changing the return value (from byte[] to Wdata_langtext_itm) and wanted to rename the methods to catch all callers. Also did a small bit of refactoring.

Regarding your comments on language:

should be using the wiki language not the core language

Yeah, this is confusing, but they are the same. core.lang gets seeded with wiki.lang. I have that indirection in place because I wanted to allow for language to be changed (similar to page.lang). It shouldn't make any difference, but let me know if I missed something

The function Label_list__get_or_fallback if it should fallback, the returned result should be that language (I assume 'en')

Poorly named method on my side. I cleaned it up a bit. But in general, the logic should be get the language for the wiki, or else, get one of its fallbacks. This is necessary for non-English languages (particularly Chinese) where the language has a fallback chain which terminates in English. For example:

This logic should work, but let me know if I'm missing something

I'm going to mark this item resolved and moving it to done. Let me know if there's anything else, otherwise will be closing out in a week or two.

Thanks!