Topic on Extension talk:Scribunto

How to get the title of frame:getParent?

8
Pamputt (talkcontribs)

Let us talk about this template that invokes Module:anagrammes to display its content. It is used in page such as Modèle:anagrammes/fr/cehin. Is there a way to retrieve the full page name (i.e. Modèle:anagrammes/fr/cehin in the previous example) in order to used it in "Module:anagramme"? Something like frame:getParentTitle. Thanks in advance for your advice.

FeRDNYC (talkcontribs)

@Pamputt Sure, you can look up the page object, the way en:Module:Namespace detect does:

function p.getPageObject(page)
	-- Get the page object, passing the function through pcall in case of
	-- errors, e.g. being over the expensive function count limit.
	if page then
		local success, pageObject = pcall(mw.title.new, page)
		if success then
			return pageObject
		else
			return nil
		end
	else
		return mw.title.getCurrentTitle()
	end
end

Though, for your purposes, just calling mw.title.getCurrentTitle() alone might be enough.

FeRDNYC (talkcontribs)

@Pamputt I may have misunderstood your question, I'm realizing. But you could do something like this:

local parent_title
local parent = frame:getParent()
if parent ~= nil then
  parent_title = parent:getTitle()
end

...Does that help any?

Pamputt (talkcontribs)

Thank you very much for your replies. Sadly, it does not solve our problem.

Actually, we have implemented a solution that uses a parameter. Our goal was to get the full title (i.e. Modèle:anagrammes/fr/cehin) in order to extract the language code (i.e. "fr" in this case). It means that when Template:anagrammes is called via Module:anagrammes, its full name is retrievable in Module:anagrammes.

Tacsipacsi (talkcontribs)

Only the title of the page directly invoking the module (i.e. Modèle:anagrammes) and the module title can be queried in Scribunto (in addition to the current page title using mw.title.getCurrentTitle()). I’m not even sure if it’s possible to get arbitrary ancestor frames in the MediaWiki parser.

GrandEscogriffe (talkcontribs)

If I understand this correctly, I would also like being able to get arbitrary ancestror frames and I hope the developers implement it at some point.

In my case, there is a template A which calls a template B which calls a module C. I want to access from Module:C the parameters fed to Template:A, just like frame:getParent() accesses the parameters fed to Template:B.

FeRDNYC (talkcontribs)

Like Tacsipacsi, I'm not even sure that's possible. What's more, I think it would actually be a very bad idea to implement anything like that, as it would just encourage brittle code.

Building assumptions about Template:A into Module:C is just too fragile, IMHO. It means that you have to write a lot of extra conditional code in Module:C so that it won't break in cases where it's called from any context other than Template:B inside Template:A, for one thing. Good encapsulation and self-contained, modular code would discourage any sort of assumptions of context — if your Module:C needs some piece of information about the calling context, then that information should be passed in to it as one of its arguments.

If Template:A has three parameters, then Template:B can have five, three of which can be {{{1}}} {{{2}}} and {{{3}}} (or the named equivalents). And then all five (potentially) can be passed in with the invocation of any methods from Module:C. That keeps the code far cleaner than having Module:C root around in its parents' closets for information it wasn't given. Again, purely IMHO.

GrandEscogriffe (talkcontribs)

In practice, Template:B is {{Wikidata}}, french Wikipedia's main template for direct Wikidata calls (the native syntax {{#property:}} is discouraged). Module:C is Interface Wikidata which then calls other modules. Template:A can be plenty of large templates, typically infoboxes, with a large number of calls to {{Wikidata}} (example: {{Infobox Société}}).

The parameter of such templates that I want to access is wikidata, which would be either a Qindex, to customize which Wikidata item the data is imported from (especially useful on test pages), or - to disable the call to Wikidata. This syntax is already in place for other infobox templates which are built by invoking directly a Lua module, where their parameters are accessed via frame:getParent(). {{Wikidata}} has a parameter entity which also works this way.

This means that there is a straightforward solution which is manually adding | entity = {{{wikidata|}}} inside all the calls to {{Wikidata}} of the infobox templates. This is conceptually simple but results in bloated, harder to read code because there are very frequent calls to {{Wikidata}} inside each template. It also takes work to do this in all the wikidata calls of all the templates.

In contrast, accessing the parameter directly at Module:Interface Wikidata would require very little code if it were possible. The module does not need the parameter wikidata (or entity from {{Wikidata}}) to operate: if it is not supplied it just targets the wikidata item which is linked to the current page.

Reply to "How to get the title of frame:getParent?"