获取 MediaWiki 中页面的名称
Get only the page name from a subpage url in MediaWiki
我在 MediaWiki 中安装了 Lua 支持和 Module:String 模块,我试图在模板中仅获取 link 的后半部分。
模板:图标
{{Icon|Icon.png|mypage/Mysubpage}}
会 return 一个 link 到子页面,同时只显示 "Mysubpage" 而不是完整的 link "mypage/mysubpage"
普通模板如下所示:
[[File:{{{1}}}|{{{size|64px}}}|link={{{2}}}|{{{2}}}]]<br>{{#ifeq:{{#pos:{{lc:{{{2|}}}}}|http}}|0|[{{{2}}}]|[[{{{2}}}]]}}
但现在我添加了子页面,图标旁边显示了完整的 link 路径。
我尝试将 {{{2}}} 替换为:
{{#invoke:String|sub|{{{2}}}| {{#invoke:String|find|{{{2}}}|/|1}}+1 |{{#invoke:String|len|{{{2}}} }} }}
但是好像不行。
我怎样才能做到这一点?
谢谢!
如果您有 ParserFunctions extension installed, the {{#titleparts}}
魔语可能会有帮助。
docs 说:
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.
由于您只查找最后一段,因此需要 {{#titleparts: {{{2}}} | | -1 }}
。无论模板是否用于子页面,这都有效。
我在 MediaWiki 中安装了 Lua 支持和 Module:String 模块,我试图在模板中仅获取 link 的后半部分。
模板:图标
{{Icon|Icon.png|mypage/Mysubpage}}
会 return 一个 link 到子页面,同时只显示 "Mysubpage" 而不是完整的 link "mypage/mysubpage"
普通模板如下所示:
[[File:{{{1}}}|{{{size|64px}}}|link={{{2}}}|{{{2}}}]]<br>{{#ifeq:{{#pos:{{lc:{{{2|}}}}}|http}}|0|[{{{2}}}]|[[{{{2}}}]]}}
但现在我添加了子页面,图标旁边显示了完整的 link 路径。
我尝试将 {{{2}}} 替换为:
{{#invoke:String|sub|{{{2}}}| {{#invoke:String|find|{{{2}}}|/|1}}+1 |{{#invoke:String|len|{{{2}}} }} }}
但是好像不行。
我怎样才能做到这一点?
谢谢!
如果您有 ParserFunctions extension installed, the {{#titleparts}}
魔语可能会有帮助。
docs 说:
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.
由于您只查找最后一段,因此需要 {{#titleparts: {{{2}}} | | -1 }}
。无论模板是否用于子页面,这都有效。