Page MenuHomePhabricator

Provide a Lua method mw.templatedata.load()
Open, LowestPublic

Description

We now duplicate the effort done in TemplateData by building a separate Lua table that can be loaded with mw.loadData(). Instead we should load the TemplateData directly and translate it into a Lua structure. This is pretty straight forward. It can even be mimicked by transcluding the doc page, stripping it down to the TemplateData, and then converting the resulting JSON to a Lua structure.

Still, I think it is better to make a native method mw.loadTemplateData() that does the necessary checks and avoids heavy transclusions.

A problem with this function is that it ends up between two different extensions. It is a result of using both Extension:TemplateData and Extension:Scribunto, but it does not really belong in either of them.

Details

Related Gerrit Patches:
mediawiki/extensions/TemplateData : masterLua method for TemplateData

Event Timeline

jeblad created this task.Jul 28 2015, 12:18 AM
jeblad raised the priority of this task from to Needs Triage.
jeblad updated the task description. (Show Details)
jeblad added a subscriber: jeblad.
Restricted Application added a subscriber: Aklapper. · View Herald TranscriptJul 28 2015, 12:18 AM
Aklapper renamed this task from There should be a Lua method mw.loadTemplateData() to Provide a Lua method mw.loadTemplateData().Jul 28 2015, 9:57 AM
Aklapper set Security to None.
Anomie added a subscriber: Anomie.

but it does not really belong in either of them.

Sure it does: it belongs in TemplateData, if reading it from the wikitext really is a problem. Much like how TitleBlacklist, ParserFunctions, FlaggedRevs, and Wikidata extensions provide Scribunto libraries for what they do.

Anomie removed a subscriber: Anomie.Jul 28 2015, 1:13 PM
Jdforrester-WMF triaged this task as Lowest priority.Sep 17 2015, 6:35 PM

as a temporary workaround, see [[he:Module:ReadTd]]. this short module uses
mw.title.makeTitlet()
title:getContnet()
and mw.text.jsonDecode()

to look for <templatedata> tags in one or more pages, and return the first valid templatedata it finds, as a lua table.

this is a kludge, of course, and not a good excuse not to provide this directly, just like it's provided directly through the API.

the actual code is simple enough, so i'm pasting it here:

local docSubPage = 'תיעוד'

function _readTemplateData( templateName ) 
	local title = mw.title.makeTitle( 0, templateName )  
	local templateContent = title and title.exists and title:getContent() -- template's raw content
	local capture =  templateContent and mw.ustring.match( templateContent, '<templatedata%s*>(.*)</templatedata%s*>' ) -- templatedata as text
	if capture then return pcall( mw.text.jsonDecode, capture ) end
	return false
end

function readTemplateData( templateName )
	if type( templateName ) == 'string' then 
		templateName = { templateName, templateName .. '/' .. docSubPage }
	end
	if type( templateName ) == "table" then
		for _, name in ipairs( templateName ) do
			local ok, result = _readTemplateData( name ) 
			if ok then return result end
		end
	end
	return nil
end

peace.

Rical added a subscriber: Rical.Jul 5 2016, 10:56 PM
IKhitron added a subscriber: IKhitron.
Kipod added a comment.Mar 13 2017, 4:25 AM

i do not understand why this was awarded "lowest" priority. reading templatedata from scribuntu is a natural and obvious requirement:
templatedata contains all kinds of "goodies", such as declaring parameters as "required", "obsolete", etc.
in the future' i see more valuable metadata included in TD, such as "if this parameter is not provided, take the value of property X from wikidata", or "this parameter is expected to have one of the following values: a, b, or x" (the last one, e.g., for parameters used with #switch ).
long story short, i think this should be _at least_ "normal priority - for me it's actually "high".

peace.

Izno added a subscriber: Izno.Jun 29 2018, 3:36 AM

T125105: Provide a way to show TemplateData content in custom format in template documentation and this one strike me as the same requirement; MCR seems to enable this use case.

Change 468901 had a related patch set uploaded (by Putnik; owner: Putnik):
[mediawiki/extensions/TemplateData@master] Lua method for TemplateData

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

putnik added a subscriber: putnik.Oct 21 2018, 10:39 PM

I made a patch that adds a method to get TemplateData in Lua. The method is currently named mw.ext.TemplateData.loadTemplateData(), which is long, but according to the conventions available, the prefix for the extension should be mw.ext.TemplateData. Alternatively, it's possible to change the name of the method itself (e.g. to load()). Or, perhaps, someone can indicate the reasons for which the prefix can be removed.

stjn added a subscriber: stjn.Oct 22 2018, 2:37 PM

I made a patch that adds a method to get TemplateData in Lua. The method is currently named mw.ext.TemplateData.loadTemplateData(), which is long, but according to the conventions available, the prefix for the extension should be mw.ext.TemplateData. Alternatively, it's possible to change the name of the method itself (e.g. to load()). Or, perhaps, someone can indicate the reasons for which the prefix can be removed.

If mw.ext.templateData is a required prefix, it’s probably better to opt in for .load or .loadData as a name of the method.

@eranroz, would you like to review the patch?

This lib still has the problem that it only makes sense when both Lua and Templatedata exist in the environment. It is a fundamental flaw for several extensions, they add some functionality together with some more or less hack-ish code to figure out whether they shall run in the current environment.

T107119#4684407: The naming schema used for Lua is pretty weird and should be revised. As it is now it creates way to long names without any real need. Such names are only necessary in a system where code can be loaded at will from external sites. Within a controlled system it is not necessary.

I wonder if anyone at all read this… Probably none.

I wonder if anyone at all read this… Probably none.

I read this and I'm sure there are other people who do.

For what it's worth, I very much support this suggestion, but unfortunately I'm not much of a Scribunto developer.

jeblad renamed this task from Provide a Lua method mw.loadTemplateData() to Provide a Lua method mw.templatedata.load().Jan 27 2019, 12:19 PM
jeblad changed the task status from Open to Stalled.Feb 2 2019, 3:05 AM

Perhaps if someone else does the followup.

jeblad removed a subscriber: jeblad.Feb 2 2019, 4:45 AM
stjn changed the task status from Stalled to Open.EditedFeb 2 2019, 8:44 PM
stjn added a subscriber: jeblad.

@jeblad: can you explain your reasoning? Just because no one finished the patch, it doesn’t mean it should be marked as ‘stalled’ (removing it from the view for everyone who wants to work on TemplateData-related tasks).

(Ah, sorry, didn’t see that you’ve ‘hijacked’ the work here. Still, no reason to push the task aside.)

jeblad removed a subscriber: jeblad.Feb 2 2019, 8:58 PM

Change 468901 had a related patch set uploaded (by Putnik; owner: Putnik):
[mediawiki/extensions/TemplateData@master] Lua method for TemplateData

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

The repo for my work on this is at Github: jeblad/TemplateData. I don't remember if it works as it is, it probably don't.

I've started to mess around with an idea to use TemplateData as a conf object, and make two link objects that pulls in data from either a Frame object or a Wikibase object. Thus it would be possible to access the value itself from the TemplateData object, which is called Root at the repo.

There are probably some code I did not check in when I put this into cold storage, but usually I leave the code at the repo in working order.

The docs folder is generated by ldocs. There is a convenience method at composer.json. There are no documentation in the wiki, everything is generated docs. The current doc can be navigated at jeblad.github.io/TemplateData/mw.templatedata.html.

Rical added a comment.Aug 31 2019, 5:42 AM

In the description of this task we can see:

Instead we should load the TemplateData directly and translate it into a Lua structure.

But, in my understanding, this is a lost of a direct usefull acces to the datas themslves.
Then functions to acces independantly at datas and at the template seems better.

I think the direction in https://gerrit.wikimedia.org/r/#/c/mediawiki/extensions/TemplateData/+/468901/ is good and simple approach.
It doesn't work as is, but require very small change for fix (replacing loadTemplateData to load in the lua module). I like it as it doesn't require too tight and coupled Lua code to the structure of TemplateData (which is usually dealed with JS code and can evolve later).

Utility functions to easily access to specific values (such as getNamesFromSets etc) can be a good addition to the base functionality, but first we should have the base functionality :)

I posted the links, and a description, but I'm not going to invest more work into this.

jeblad added a subscriber: jeblad.Aug 31 2019, 5:59 PM
jeblad removed a subscriber: jeblad.