Mediawiki: in a link, pass parameter to template mediawiki, without transclusion

2.3k views Asked by At

Here's a wiki syntax challenge!

Since asking Mediawiki: Same section with multiple names (section aliases/synonyms), I've developed a more specific query as I feel like I'm closer to a solution that works the way I want it to:

I want to, in a link, pass a parameter to a template WITHOUT transcluding the full article.

[[ Template:Interrupts Code Examples|pagename={{PAGENAME}} ]]

Except this creates a link to an article named pagename=Name_of_page_you're_on instead of passing a parameter.

Transcluding the article, the parameter is passed successfully, but I just want a link to pass the parameter to the template.

For more context,

Using #ifexists I can generate a "code examples compilation" page that defaults to sections with headings "Example One" and "Example Two" when the names aren't given, but I also want them to be section linkable and show up with the appropriate headings depending on where the comes in from:

"EIMSK" and "Example Two"
"EICRA" and "Example Two"
"Example One" and "PCMSK0"
"Example One" and "PCMSK1"
"Example One" and "PCMSK2"
"Example One" and "PCICR"

Is this too terribly convoluted and I should just manually compile these pages? Is this possible?

My current solution is to have

"PCMSK0"
{{example one code}}
"PCMSK2"
{{example one code}}
"EICRA"
{{example two code}}
etc.

which is far from ideal since then the compilation of code examples page is just one giant long mess.

I am open to writing a MediaWiki extension in PHP if someone can give me some psuedocode to work off of.

1

There are 1 answers

0
Ilmari Karonen On BEST ANSWER

Note: This may be only a partial answer. I'll post it anyway, in case it is of some use.

It you're simply trying to link to another page on your wiki with custom query parameters, use one of the URL data magic words (localurl, fullurl, canonicalurl, etc.), like this:

[{{fullurl:Target page|pagename={{PAGENAMEE}}}} link text]

(Note the use of {{PAGENAMEE}} to automatically URL-encode the source page name.)

Note that the resulting link will be treated by MediaWiki as an external link, and will be formatted as such. If the external link icon bothers you, a simple way to get rid of it is to wrap the link in a <span> or a <div> with class="plainlinks".

It's possible to wrap all this in a template, e.g. like this:

Page Template:Fancylink:

<span class="plainlinks">[{{fullurl:{{{1}}}|pagename={{PAGENAMEE}}}} {{{2|{{{1}}}}}}]</span>

which would allow you to write your links as:

{{fancylink:Target page|link text}}

For an example of a similar template (albeit with extra bells and whistles added on top), see e.g. Template:Edit on Wikipedia, which links to a page with the action=edit parameter added to the URL.

However, this method alone will not cause the URL parameter pagename= you pass in the link to be substituted for {{{pagename}}} on the target page. To make that happen, you'd probably need to write a custom MediaWiki extension. This should be doable, but considerably more involved than just creating the links in the first page.


Alternatively, the "conventional" MediaWiki way of solving this kind of issue, given that the set of valid parameter values seems to be limited, would be to create a separate page for each value that simply transcludes the shared template, something like:

Page Interrupts Code Example PCICR:

{{Interrupts Code Example One|interrupt=PCICR}}

with Template:Interrupts Code Example One containing the actual example code, possibly with {{{interrupt}}} substituted for the interrupt name (or used for things like {{#switch:...}}) in appropriate places. To create the individual pages, if there are many, you could use a bot or the maintenance scripts importTextFile.php or edit.php.