Search code examples
mediawikimediawiki-templates

Get only the page name from a subpage url in MediaWiki


I installed Lua support and the Module:String module in MediaWiki and I'm trying to get only the second half of a link in a template.

template: Icon

{{Icon|Icon.png|mypage/Mysubpage}} would return a link to the subpage while only showing "Mysubpage" and not the complete link "mypage/mysubpage"

The normal template looks like this:

[[File:{{{1}}}|{{{size|64px}}}|link={{{2}}}|{{{2}}}]]<br>{{#ifeq:{{#pos:{{lc:{{{2|}}}}}|http}}|0|[{{{2}}}]|[[{{{2}}}]]}}

But now that I'm adding subpages, the icons show the complete link path next to them.

I tried to replace {{{2}}} with:

{{#invoke:String|sub|{{{2}}}| {{#invoke:String|find|{{{2}}}|/|1}}+1 |{{#invoke:String|len|{{{2}}} }} }}

but it doesn't seem to work.

How can I achieve this?

Thanks!


Solution

  • If you have the ParserFunctions extension installed, the {{#titleparts}} magic word might be helpful.

    The docs say:

    This function separates a page title into segments based on slashes, then returns some of those segments as output.

    {{#titleparts: pagename | number of segments to return | first segment to return }}
    

    If the number of segments to return parameter is not specified, it defaults to "0", which returns all the segments from the first segment to return (included).

    Negative values are accepted for both values… Negative values for the first segment to return translates to "start with this segment counting from the right":

    {{#titleparts: Talk:Foo/bar/baz/quok | | -1 }}quok Returns last segment.

    Since you are looking for only the last segment, you want {{#titleparts: {{{2}}} | | -1 }}. This will work whether or not the template is used on a subpage.