Magic words & parser functions

From Redazione
Jump to navigation Jump to search
 
(47 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[https://www.mediawiki.org/wiki/Help:Magic_words Magic words] and [https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions Parser functions] are a way to empower the wiki text (the specific language through which pages of all wikis are written, thus one can also use Visual editor where available) with some programmatic feature, for instance the ability to add the current date to a page (which will update during subsequent access) or to trigger whatever other function available in the wiki on the server side. They are both completely comparable to [[How it works/Templates|templates]], with the difference that the second ones are always called with a parameter, and that the logics behind them is not placed within a template or module (so that it can be accessed on the wiki) but is coded server side.
  
Currently this platform offers the additional parser functions.
+
Here is a list of the additional magic words provided by our platform (as usual they are prefixed with the string "CI_" to distinguish them easily from other Mediawiki [https://www.mediawiki.org/wiki/Help:Magic_words magic words] and [https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions parser functions])
They are prefixed with the prefix "ci_" to distinguish them easily from the standard mediawiki [https://www.mediawiki.org/wiki/Help:Magic_words magic words] and [https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions parser functions].
+
 
 +
{| class="wikitable"
 +
|+
 +
!name
 +
!what it does
 +
|-
 +
|CI_userAffiliatedTo
 +
|shows the organization to which the logged in user is affiliated
 +
|-
 +
|CI_userRealName
 +
|shows the real name of logged in user (by contrast to the username)
 +
|-
 +
|CI_isSysop
 +
|returns 1 if the logged in user is an admin ("sysop")
 +
|-
 +
|CI_loggedIn
 +
|returns 1 if the page's visitor is logged in
 +
|-
 +
|CI_visitorIsoCode
 +
|returns the iso code of country of the visitor
 +
|-
 +
|CI_visitorCountry
 +
|returns the country name of the visitor
 +
|}
 +
 
 +
<noinclude>
 +
and parser functions:
 +
 
 +
{| class="wikitable"
 +
|+
 +
!name
 +
!what it does
 +
|-
 +
|CI_editors_realnames
 +
|returns the real names from a list of usernames
 +
|-
 +
|CI_editors_attribution
 +
|taking an [https://www.semantic-mediawiki.org/wiki/Help:Inline_queries ask query] returning the editors of a page as input, returns the page creator or editors of a given page, and the the affiliated organization for each of them, passing to the function a pattern like "created by [name], affiliated to [organization]"
 +
|-
 +
|CI_subpages
 +
|returns the list of sub-pages ("kids" only) of a given page within only 1 depth level, without duplicates
 +
|-
 +
|CI_format_title
 +
|returns the actual page title given a path; e.g. "Presepe /1" from "Centro Italiano Barcellona/Posts/Presepe /1"
 +
|-
 +
|CI_noparse_isHTML
 +
|prevents Mediawiki from html encoding output
 +
|}
 +
 
 +
 
 +
==Instructions of use==
 +
 
 +
 
 +
===Magic words===
 
{| class="wikitable"
 
{| class="wikitable"
 
|+
 
|+
!function name
+
!name
 
!return value
 
!return value
 
!example
 
!example
 
!result
 
!result
 
|-
 
|-
|ci_userAffiliatedTo
+
|CI_userAffiliatedTo
 
|logged in user affiliation
 
|logged in user affiliation
|<nowiki>{{#ci_userAffiliatedTo:}}</nowiki>
+
|<nowiki>{{CI_userAffiliatedTo}}</nowiki>
|{{#ci_userAffiliatedTo:}}
+
|{{CI_userAffiliatedTo}}
 
|-
 
|-
|ci_userRealName
+
|CI_userRealName
 
|logged in user real name
 
|logged in user real name
|<nowiki>{{#ci_userRealName:}}</nowiki>
+
|<nowiki>{{CI_userRealName}}</nowiki>
|{{#ci_userRealName:}}
+
|{{CI_userRealName}}
 
|-
 
|-
|ci_isSysop
+
|CI_isSysop
 
|whether the logged in user is an administrator or not
 
|whether the logged in user is an administrator or not
|<nowiki>{{#ci_isSysop:}}</nowiki>
+
|<nowiki>{{CI_isSysop}}</nowiki>
|{{#ci_isSysop:}}
+
|{{CI_isSysop}}
 
|-
 
|-
|ci_loggedIn
+
|CI_loggedIn
 
|whether the user is logged in or not
 
|whether the user is logged in or not
|<nowiki>{{#ci_loggedIn:}}</nowiki>
+
|<nowiki>{{CI_loggedIn}}</nowiki>
|{{#ci_loggedIn:}}
+
|{{CI_loggedIn}}
 
|-
 
|-
|ci_visitorIsoCode
+
|CI_visitorIsoCode
 
|country code of user (based on the ip)
 
|country code of user (based on the ip)
|<nowiki>{{#ci_visitorIsoCode:}}</nowiki>
+
|<nowiki>{{CI_visitorIsoCode}}</nowiki>
|{{#ci_visitorIsoCode:}}
+
|{{CI_visitorIsoCode}}
 
|-
 
|-
|ci_visitorCountry
+
|CI_visitorCountry
 
|country of user (based on the ip)
 
|country of user (based on the ip)
|<nowiki>{{#ci_visitorCountry:}}</nowiki>
+
|<nowiki>{{CI_visitorCountry}}</nowiki>
|{{#ci_visitorCountry:}}
+
|{{CI_visitorCountry}}
 
|}
 
|}
  
Line 46: Line 100:
 
-->
 
-->
  
<noinclude>
+
===Parser functions===
 
 
'''More'''
 
 
{| class="wikitable"
 
{| class="wikitable"
 
|+
 
|+
Line 55: Line 107:
 
!parameter
 
!parameter
 
|-
 
|-
|ci_HidePathFromPageList
+
|CI_editors_realnames
|<nowiki>wikicode with links, in the form [[complete path|page name]]</nowiki>
 
|list, delimiter, kind of output (ul or comma separated values)
 
|-
 
|ci_editors_realnames
 
 
|comma separated real names
 
|comma separated real names
 
|list
 
|list
 
|-
 
|-
|ci_pageTitlesAttribution
+
|CI_editors_attribution
|<nowiki>wikicode with links, in the form [[complete path|page name]] followed by a string in the form "created by [editor real name] affiliated to [editor affiliation]</nowiki>
+
|<nowiki>wiki-text unordered list with links, in the form [[complete path|page name]] followed by the real name of page creator or editor, and the affiliated organization</nowiki>
|list, delimiter, kind of output (ul or comma separated values)
+
|list, delimiter, kind of output (ul or comma separated values), pattern with editor and related organization, and "fallback" pattern (optionally)
 +
|-
 +
|CI_subpages
 +
|wiki-text unordered list with links, in the form <nowiki>[[/kid page title]]</nowiki>
 +
|none
 +
|-
 +
|CI_format_title
 +
|raw text
 +
|complete page title
 
|}
 
|}
<br />
 
  
'''ci_HidePathFromPageList'''
+
===Examples===
  
example:
 
  
<pre class="language-wiki">
+
'''CI_editors_attribution'''
{{#ci_HidePathFromPageList: path/1/2/a#path/1/2/b#path/1/2/c|#|ul}}
 
</pre>
 
  
will create the following rendered list:
 
 
*[[path/1/2/a|a]]
 
*[[path/1/2/b|b]]
 
*[[path/1/2/b|c]]
 
 
(that is the path of the pages a, b and c will be removed from the list, thus
 
preserving the link paths).
 
 
Of course the same function can be used in combination with a semantic
 
query ("ask" function, form semantic mediawiki extension), as follows:
 
 
<pre class="language-wiki">
 
{{#ci_HidePathFromPageList: {{#ask:
 
|[[~Centro italiano Barcellona*]]
 
[[Category:Language_courses]]
 
|format=array
 
|sep=##
 
}}|##|ul}}
 
</pre>
 
 
The ask query will return a list of pages (with paths)
 
separated by '##' and the parser function 'ci_HidePathFromPageList'
 
will take that list, will split it using '##' as delimiter
 
(2nd argument) and will return a 'ul' (unordered list), for
 
instance as follows:
 
 
*[[Centro italiano Barcellona/Courses/Hablamos italiano a.a. 2018-2019|Hablamos italiano a.a. 2018-2019]]
 
*[[Centro italiano Barcellona/Courses/Hablamos italiano a.a. 2019-2020|Hablamos italiano a.a. 2019-2020]]
 
*[[Centro italiano Barcellona/Courses/Hablamos italiano a.a. 2020-2021|Hablamos italiano a.a. 2020-2021]]
 
 
 
'''ci_pageTitlesAttribution'''
 
 
example:
 
 
<pre class="language-wiki">
 
<pre class="language-wiki">
{{#ci_pageTitlesAttribution: {{#ask:
+
{{#CI_editors_attribution: {{#ask:
 
[[Category:Reading suggestions]]
 
[[Category:Reading suggestions]]
 
|format=array
 
|format=array
 
|?Page creator
 
|?Page creator
|?Page author
+
|?Creation date
 +
|limit=5
 
|sep=##
 
|sep=##
}}|##|ul}}
+
}}|##|ul|created by [name], affiliated to [[[organization]]]| by [name] }}
 
</pre>
 
</pre>
  
 
will create the following list:
 
will create the following list:
  
{{#ci_pageTitlesAttribution: {{#ask:
+
{{#CI_editors_attribution: {{#ask:
 
[[Category:Reading suggestions]]
 
[[Category:Reading suggestions]]
 
|format=array
 
|format=array
 
|?Page creator
 
|?Page creator
|?Page author
+
|?Creation date
 +
|limit=5
 
|sep=##
 
|sep=##
}}|##|ul}}
+
}}|##|ul|created by [name], affiliated to [[[organization]]]| by [name]}}
  
to notice that the output returned by the ask query above will be as follows:
+
Where it can be noted the following:
  
 +
*both "Page creator" and "Creation date" properties are required, because the parser function checks the affiliation of the page creator within a given time-frame
 +
*the used separator symbol (## in the example, but could be something else) of the items returned by the ask query shall be provided to the CI_editors_attribution function as 2nd parameter, in order to separate items using the same symbol
 +
*the last 2 parameters of CI_editors_attribution function are the pattern to be used to format results and, the "fallback" pattern": the fallback pattern will be used if a value within squared brackets cannot be found/replaced.
 +
*the replacement value in the pattern is wrapped by single square brackets (i.e. [name] or [organization], at present), in the query [organization] is wrapped with 2 additional brackets in order to create a (wiki) link
 +
 +
<!--
 +
Also, the output produced by the ask query will be a list of page titles separated by the character '##' with the requested properties separated by the '<PROP>' key-word, and the "CI_editors_attribution" parser function will then format it in an unordered list using the same string '##' as divider.
 +
-->
 +
 +
<!--
 
<pre class="language-xxxx">
 
<pre class="language-xxxx">
 
Announcements/European Commission - Supporting the cultural and creative sectors in the ACP countries<PROP>User:Luca.speziale<PROP>User:Admin<MANY>User:Luca.speziale##Announcements/General Secretariat of the Council - Traineeships at the General Secretariat of the Council<PROP>User:Luca.speziale<PROP>User:Admin<MANY>User:Luca.speziale##Announcements/Language school "Praticamente" - Lavoro per un insegnante di inglese in Kazakistan per 3 mesi presso la Scuola di lingua Italiana “Praticamente”<PROP>User:Luca.speziale<PROP>User:Admin<MANY>User:Luca.speziale
 
Announcements/European Commission - Supporting the cultural and creative sectors in the ACP countries<PROP>User:Luca.speziale<PROP>User:Admin<MANY>User:Luca.speziale##Announcements/General Secretariat of the Council - Traineeships at the General Secretariat of the Council<PROP>User:Luca.speziale<PROP>User:Admin<MANY>User:Luca.speziale##Announcements/Language school "Praticamente" - Lavoro per un insegnante di inglese in Kazakistan per 3 mesi presso la Scuola di lingua Italiana “Praticamente”<PROP>User:Luca.speziale<PROP>User:Admin<MANY>User:Luca.speziale
 
 
</pre>
 
</pre>
  
therefore the function '''ci_pageTitlesAttribution''' will parse the output using the standard Semantic Mediawiki &lt;PROP&gt; and &lt;MANY&gt; delimiters and will retrieve the real name and the user affiliation from the username: then, it will return the output as the '''ci_HidePathFromPageList''' function.
+
therefore the function '''CI_editors_attribution''' will parse the output using the standard Semantic Mediawiki &lt;PROP&gt; and &lt;MANY&gt; delimiters and will retrieve the real name and the user affiliation from the username: then, it will return an unordered list as above.
 +
-->
  
  
'''ci_editors_realnames'''
+
'''CI_editors_realnames'''
  
example:
 
 
<pre class="language-wiki">
 
<pre class="language-wiki">
{{#ci_editors_realnames: {{#ask:  
+
{{#CI_editors_realnames: {{#ask:  
 
[[{{PAGENAME}}]]  
 
[[{{PAGENAME}}]]  
 
|?Page creator
 
|?Page creator
Line 157: Line 185:
 
</pre>
 
</pre>
  
{{#ci_editors_realnames: {{#ask:  
+
will produce the following output:
 +
 
 +
{{#CI_editors_realnames: {{#ask:  
 
[[{{PAGENAME}}]]  
 
[[{{PAGENAME}}]]  
 
|?Page creator
 
|?Page creator
Line 167: Line 197:
 
}}
 
}}
  
 +
<!--
 +
As above, the output produced by the ask query, will be similar to the following:
 +
 +
<pre class="language-xxxx">
 
User:MediaWiki default<PROP>User:Admin<MANY>User:Luca.speziale<MANY>User:MediaWiki default
 
User:MediaWiki default<PROP>User:Admin<MANY>User:Luca.speziale<MANY>User:MediaWiki default
 +
</pre>
 +
 +
and applies as above with the difference that the output produced by the '''CI_editors_realnames''' function will be a comma separated list of real names of provided usernames.
 +
 +
-->
 +
 +
Likewise as above, the output produced by the ask query, will be a list of usernames separated by the key-word '<MANY>', with properties separated by the '<PROP>' key-word, and the "CI_editors_attribution" parser function will retrieve the related real names and will concatenate them by commas.
  
 
</noinclude>
 
</noinclude>

Latest revision as of 21:08, 23 November 2020

Magic words and Parser functions are a way to empower the wiki text (the specific language through which pages of all wikis are written, thus one can also use Visual editor where available) with some programmatic feature, for instance the ability to add the current date to a page (which will update during subsequent access) or to trigger whatever other function available in the wiki on the server side. They are both completely comparable to templates, with the difference that the second ones are always called with a parameter, and that the logics behind them is not placed within a template or module (so that it can be accessed on the wiki) but is coded server side.

Here is a list of the additional magic words provided by our platform (as usual they are prefixed with the string "CI_" to distinguish them easily from other Mediawiki magic words and parser functions)

name what it does
CI_userAffiliatedTo shows the organization to which the logged in user is affiliated
CI_userRealName shows the real name of logged in user (by contrast to the username)
CI_isSysop returns 1 if the logged in user is an admin ("sysop")
CI_loggedIn returns 1 if the page's visitor is logged in
CI_visitorIsoCode returns the iso code of country of the visitor
CI_visitorCountry returns the country name of the visitor


and parser functions:

name what it does
CI_editors_realnames returns the real names from a list of usernames
CI_editors_attribution taking an ask query returning the editors of a page as input, returns the page creator or editors of a given page, and the the affiliated organization for each of them, passing to the function a pattern like "created by [name], affiliated to [organization]"
CI_subpages returns the list of sub-pages ("kids" only) of a given page within only 1 depth level, without duplicates
CI_format_title returns the actual page title given a path; e.g. "Presepe /1" from "Centro Italiano Barcellona/Posts/Presepe /1"
CI_noparse_isHTML prevents Mediawiki from html encoding output


Instructions of use

Magic words

name return value example result
CI_userAffiliatedTo logged in user affiliation {{CI_userAffiliatedTo}}
CI_userRealName logged in user real name {{CI_userRealName}}
CI_isSysop whether the logged in user is an administrator or not {{CI_isSysop}}
CI_loggedIn whether the user is logged in or not {{CI_loggedIn}}
CI_visitorIsoCode country code of user (based on the ip) {{CI_visitorIsoCode}} US
CI_visitorCountry country of user (based on the ip) {{CI_visitorCountry}} United States


Parser functions

function return value parameter
CI_editors_realnames comma separated real names list
CI_editors_attribution wiki-text unordered list with links, in the form [[complete path|page name]] followed by the real name of page creator or editor, and the affiliated organization list, delimiter, kind of output (ul or comma separated values), pattern with editor and related organization, and "fallback" pattern (optionally)
CI_subpages wiki-text unordered list with links, in the form [[/kid page title]] none
CI_format_title raw text complete page title

Examples

CI_editors_attribution

{{#CI_editors_attribution: {{#ask:
[[Category:Reading suggestions]]
|format=array
|?Page creator
|?Creation date
|limit=5
|sep=##
}}|##|ul|created by [name], affiliated to [[[organization]]]| by [name] }}

will create the following list:

Where it can be noted the following:

  • both "Page creator" and "Creation date" properties are required, because the parser function checks the affiliation of the page creator within a given time-frame
  • the used separator symbol (## in the example, but could be something else) of the items returned by the ask query shall be provided to the CI_editors_attribution function as 2nd parameter, in order to separate items using the same symbol
  • the last 2 parameters of CI_editors_attribution function are the pattern to be used to format results and, the "fallback" pattern": the fallback pattern will be used if a value within squared brackets cannot be found/replaced.
  • the replacement value in the pattern is wrapped by single square brackets (i.e. [name] or [organization], at present), in the query [organization] is wrapped with 2 additional brackets in order to create a (wiki) link



CI_editors_realnames

{{#CI_editors_realnames: {{#ask: 
[[{{PAGENAME}}]] 
|?Page creator
|?Page author
|format=array
|mainlabel=-
|sep=##
}}
}}

will produce the following output:



Likewise as above, the output produced by the ask query, will be a list of usernames separated by the key-word '<MANY>', with properties separated by the '<PROP>' key-word, and the "CI_editors_attribution" parser function will retrieve the related real names and will concatenate them by commas.