MediaWiki Parser Functions

From binaryoption
Jump to navigation Jump to search
Баннер1
  1. MediaWiki Parser Functions: A Beginner's Guide

Parser functions are a powerful feature of MediaWiki that allow you to perform calculations, manipulate strings, and control the display of content within wiki pages. They are essentially functions that are processed *during* the parsing of the wiki text, before the page is rendered to the user. This makes them incredibly versatile for creating dynamic content, templates, and complex layouts. This article will serve as a comprehensive introduction to MediaWiki parser functions for beginners, covering the basics, common functions, and practical examples.

    1. What are Parser Functions?

At their core, parser functions take input (arguments) and return output. This output is then substituted into the wiki text before the page is displayed. They are written using a specific syntax: `Template:Function name:arguments`. The `Template:` and `` delimiters tell the parser to evaluate the function.

Think of them as mini-programs that run within your wiki pages. Unlike Lua scripting (available in newer MediaWiki versions), parser functions are relatively simple and are limited in their capabilities. However, they are widely supported and offer a significant degree of flexibility for common tasks. They do *not* allow for loops or complex conditional logic like a full programming language.

It's important to understand that parser functions are evaluated when the page is *parsed*, not when it is *viewed*. This means that if a parser function uses a variable that changes frequently (like the current date), the function will only be evaluated once when the page is saved. To update dynamic values, you might need to use extensions like Extension:DynamicPage or rely on external data sources.

    1. Basic Syntax

The general syntax for using a parser function is:

`Template:Function name:argument1`

  • `function name`: The name of the parser function you want to use (e.g., `if`, `len`, `time`).
  • `argument1`, `argument2`, etc.: The inputs to the function. Arguments are separated by the pipe symbol (`|`).

You can also pass arguments using named parameters:

`Template:Function name`

This can make your code more readable, especially when dealing with functions that have many arguments.

    1. Common Parser Functions

Here's a detailed look at some of the most commonly used parser functions:

      1. 1. `if`

The `if` function is a conditional statement. It checks if a condition is true and returns different results based on the outcome.

Syntax: `Template:If:condition`

  • `condition`: An expression that evaluates to true or false. This can be a string comparison, a check for an empty string, or a numerical comparison.
  • `then`: The output if the condition is true.
  • `else`: The output if the condition is false (optional). If omitted, nothing is displayed if the condition is false.

Example:

`{{if:Template:Len:Some text > 10|Text is long|Text is short}}` This will output "Text is long" because the length of "Some text" is less than 10.

      1. 2. `len`

The `len` function returns the length of a string.

Syntax: `Template:Len:string`

  • `string`: The string whose length you want to determine.

Example:

`Template:Len:Hello World` will output `11`.

      1. 3. `time`

The `time` function formats a timestamp.

Syntax: `Template:Time:format`

  • `format`: The format string that specifies how the timestamp should be displayed. See Help:Magic words#Dates and times for a complete list of format options.
  • `timestamp`: The timestamp to format. If omitted, the current time is used. Timestamps can be in various formats, including epoch seconds, ISO 8601, or a string like "now".

Example:

`Template:Time:Y-m-d` will output the current date in the format YYYY-MM-DD. `Template:Time:H:i:s` will output the current time in the format HH:MM:SS. `Template:Time:Y-m-d` will output the date corresponding to the Unix timestamp 1678886400.

      1. 4. `formatnum`

The `formatnum` function formats a number with a specified number of decimal places and a thousands separator.

Syntax: `number`

  • `number`: The number to format.
  • `decimalplaces`: The number of decimal places to display.
  • `thousandsseparator`: The character to use as a thousands separator (optional).

Example:

`1,234,567.89` will output `1,234,567.89`.

      1. 5. `stringindex`

The `stringindex` function finds the position of a substring within a string.

Syntax: `Template:Stringindex:string`

  • `string`: The string to search within.
  • `substring`: The substring to find.
  • `fromindex`: The starting position for the search (optional).

Example:

`Template:Stringindex:Hello World` will output `7` (the starting position of "World" in "Hello World"). `Template:Stringindex:Hello World` will also output `7`.

      1. 6. `subst`

The `subst` function substitutes variables and parser functions within a string. This is crucial for creating reusable templates.

Syntax: `{{subst:string}}`

  • `string`: The string containing variables and parser functions to substitute.

Example:

If you have a variable `Template:MyVariable` with the value "Example", then `{{subst:Template:MyVariable}}` will output "Example". Without `subst`, it would just output the variable name.

      1. 7. `titletext`

The `titletext` function retrieves the text of a page title. This is useful for displaying page titles dynamically.

Syntax: `Template:Titletext:title`

  • `title`: The title of the page to retrieve the text from.

Example:

If you have a page titled "My Page", `Template:Titletext:My Page` will output "My Page".

      1. 8. `#property`

The `#property` magic word allows you to access properties of a page, such as its modification date or its size.

Syntax: `{{#property:page title|property name}}`

  • `page title`: The title of the page whose property you want to access.
  • `property name`: The name of the property (e.g., `size`, `lastmodified`).

Example:

`{{#property:Main Page|lastmodified}}` will output the last modification date of the Main Page.

    1. Practical Examples & Advanced Usage

These functions can be combined to create more complex functionality. Here are a few examples:

  • **Displaying a conditional message based on the page title:**

```wiki {{if:Template:Titletext:MediaWiki Parser Functions = My Special Page|This is a special page!|This is a regular page.}} ```

  • **Creating a dynamic date display:**

```wiki The current date is: Template:Time:Y-m-d ```

  • **Formatting a number with a thousands separator and two decimal places:**

```wiki The price is: 12,345.678 ```

  • **Calculating a percentage change (requires a template to store initial values):**

Imagine a template `Template:StockData` which stores the previous closing price in a variable called `PreviousClose`. The following code calculates the percentage change from the previous close to the current price (assumed to be stored in a variable `CurrentPrice`)

```wiki {{#vardefine:Change|{{expr:Template:CurrentPrice - Template:PreviousClose}}} {{#vardefine:PercentageChange|{{expr: (Template:Change / Template:PreviousClose) * 100}}}} Percentage Change: Template:PercentageChange ```

This requires Extension:Variables to be enabled.

    1. Limitations and Considerations
  • **Performance:** Parser functions can be computationally expensive, especially when used extensively on frequently viewed pages. Avoid using complex functions in high-traffic areas.
  • **Recursion:** Parser functions do *not* support recursion. You cannot call a parser function from within itself.
  • **Complexity:** While powerful, parser functions can become difficult to read and maintain as they grow in complexity. Consider using Lua scripting if you need more advanced functionality.
  • **Error Handling:** Parser functions have limited error handling capabilities. If an error occurs, the function may simply return nothing or an error message.
  • **Security:** Be cautious when using parser functions with user-provided input, as they could potentially be exploited for malicious purposes.
    1. Resources for Further Learning
  • Help:ParserFunctions: The official MediaWiki documentation on parser functions.
  • Help:Magic words: Information on magic words, which can be used in conjunction with parser functions.
  • Help:Templates: Learn how to create and use templates with parser functions.
  • Help:Variables: Learn how to use variables within parser functions (requires extension).
  • **External Resources:**
   *   [Investopedia - Technical Analysis](https://www.investopedia.com/terms/t/technicalanalysis.asp)
   *   [Babypips - Forex Trading Strategies](https://www.babypips.com/learn-forex/forex-strategies)
   *   [TradingView - Charting and Analysis](https://www.tradingview.com/)
   *   [StockCharts.com - Technical Indicators](https://stockcharts.com/education/technical-indicators/)
   *   [Corporate Finance Institute - Financial Modeling](https://corporatefinanceinstitute.com/resources/knowledge/modeling/financial-modeling/)
   *   [Trend Following](https://trendfollowing.com/)
   *   [Elliott Wave Theory](https://www.elliottwave.com/)
   *   [Fibonacci Retracements](https://www.investopedia.com/terms/f/fibonacciretracement.asp)
   *   [Moving Averages](https://www.investopedia.com/terms/m/movingaverage.asp)
   *   [Relative Strength Index (RSI)](https://www.investopedia.com/terms/r/rsi.asp)
   *   [MACD](https://www.investopedia.com/terms/m/macd.asp)
   *   [Bollinger Bands](https://www.investopedia.com/terms/b/bollingerbands.asp)
   *   [Candlestick Patterns](https://www.investopedia.com/terms/c/candlestick.asp)
   *   [Support and Resistance Levels](https://www.investopedia.com/terms/s/supportandresistance.asp)
   *   [Breakout Trading](https://www.investopedia.com/terms/b/breakout.asp)
   *   [Swing Trading](https://www.investopedia.com/terms/s/swingtrading.asp)
   *   [Day Trading](https://www.investopedia.com/terms/d/daytrading.asp)
   *   [Scalping](https://www.investopedia.com/terms/s/scalping.asp)
   *   [Position Trading](https://www.investopedia.com/terms/p/positiontrading.asp)
   *   [Head and Shoulders Pattern](https://www.investopedia.com/terms/h/headandshoulders.asp)
   *   [Double Top/Bottom](https://www.investopedia.com/terms/d/doubletop.asp)
   *   [Gap Analysis](https://www.investopedia.com/terms/g/gapanalysis.asp)
   *   [Volume Analysis](https://www.investopedia.com/terms/v/volume.asp)
   *   [Divergence (Technical Analysis)](https://www.investopedia.com/terms/d/divergence.asp)
   *   [Ichimoku Cloud](https://www.investopedia.com/terms/i/ichimoku-cloud.asp)



    1. Conclusion

Parser functions are a valuable tool for any MediaWiki user who wants to create dynamic and engaging content. While they have limitations, they offer a powerful way to manipulate text, perform calculations, and control the display of information within your wiki pages. By understanding the basics and exploring the available functions, you can significantly enhance the functionality of your wiki.

MediaWiki Templates Help:Magic words Help:Variables Extension:Lua ParserFunctions Help:Categories Help:Page Help:Editing Help:Formatting


Start Trading Now

Sign up at IQ Option (Minimum deposit $10) Open an account at Pocket Option (Minimum deposit $5)

Join Our Community

Subscribe to our Telegram channel @strategybin to receive: ✓ Daily trading signals ✓ Exclusive strategy analysis ✓ Market trend alerts ✓ Educational materials for beginners

Баннер