Jump to content

Generating dynamic content with MediaWiki

From Wikiversity

Active communities that use MediaWiki such as Wikipedia and Wikiversity often use MediaWiki's variables, parser functions and other tools to add dynamic features to pages like portals and current events. MediaWiki has several ways to deal with the passage of time and displaying dynamic content. This learning resource will explore how this is done and hopefully develop or improve some actual dynamic content for use here at Wikiversity.

When is now?

[edit | edit source]

A very succint view of "now" can be seen using the {{CURRENTTIMESTAMP}} variable:

Current timestamp = 20241109001948

Although the output is a bit "cryptic", it is quite accurate to the second your browser loaded this page. What you see is a "big endian" view of the current time, that is, the output of the variable shows from left to right the most to least significant information in the format:

YYYYMMDDHHMMSS example: 20070523171222

In our example, a "snapshot" of the time when this page was being edited, it was 2007 05 23 17:12:22, where the year is a standard four-digit year, 2007, the month is shown as a zero-padded numeric depiction of "May", the day of the month is the 23rd, the hour is the 17th hour on a 24-hour clock and it's 12 minutes and 22 seconds from the top of that hour. This method of displaying the current time is fairly universally recognized and conforms to an international standard called ISO 8601. This learning project will attempt to remain within the guidelines of the standard. We will also look at how MediaWiki keeps track of time and converts timestamps to more human-friendly views.

Activities

[edit | edit source]

Think: Click the "History" tab for this page and compare the "snapshot" of {{CURRENTTIMESTAMP}} above against the date displayed on the first edit to this article. What is the diffence between how the time is displayed? How does this conform with or differ from ISO 8601?

Exercise 1

[edit | edit source]

Edit this section by adding a pound sign and five tildes (# ~~~~~) to the bottom of the list below.

  1. 18:05, 23 May 2008 (UTC)
  2. 20:28, 24 May 2007 (UTC)
  3. 20:47, 24 May 2007 (UTC)
  4. 02:30, 21 July 2007 (UTC)
  5. 01:44, 7 August 2007 (UTC)
  6. 15:09, 6 October 2007 (UTC)
  7. 03:27, 9 October 2007 (UTC)
  8. 00:17, 11 October 2007 (UTC)
  9. 00:38, 10 December 2007 (UTC)
  10. 03:05, 7 January 2008 (UTC)
  11. 20:35, 29 February 2008 (UTC)
  12. 19:56, 15 May 2008 (UTC)
  13. 10:00, 23 May 2008 (UTC)
  14. 04:14, 10 June 2008 (UTC)
  15. 14:45, 4 July 2008 (UTC)
  16. 15:09, 14 July 2008 (UTC)
  17. 18:17, 21 July 2008 (UTC)
  18. 20:32, 28 July 2008 (UTC)
  19. 12:45, 27 August 2008 (UTC)
  20. 17:18, 2 September 2008 (UTC)
  21. 21:26, 4 October 2008 (UTC)
  22. 21:28, 4 October 2008 (UTC)
  23. 23:27, 21 January 2010 (UTC)
  24. 09:41, 7 November 2008 (UTC)
  25. 14:49, 16 November 2008 (UTC)
  26. 17:31, 14 December 2008 (UTC)
  27. 22:14, 5 January 2009 (UTC)
  28. 00:58, 22 January 2009 (UTC)
  29. 20:55, 27 May 2009 (UTC)
  30. 13:09, 1 April 2009 (UTC)
  31. 23:51, 13 June 2009 (UTC)
  32. 22:42, 7 July 2009 (UTC)
  33. 22:40, 3 August 2009 (UTC)
  34. 20:27, 4 August 2009 (UTC)
  35. 22:06, 5 September 2009 (UTC)
  36. 09:57, 16 September 2009 (UTC)
  37. 13:45, 30 September 2009 (UTC)
  38. 20:45, 8 October 2009 (UTC)
  39. 06:48, 27 October 2009 (UTC)
  40. 00:46, 5 November 2009 (UTC)
  41. 17:47, 10 November 2009 (UTC)
  42. 20:16, 7 December 2009 (UTC)
  43. 20:16, 7 December 2009 (UTC)
  44. 22:09, 1 January 2010 (UTC)
  45. 23:57, 12 January 2010 (UTC)
  46. 23:28, 21 January 2010 (UTC)
  47. 23:28, 21 January 2010
  48. 14:42, 20 April 2010 (UTC)
  49. 15:57, 23 June 2010 (UTC)
  50. 14:31, 30 June 2010 (UTC)
  51. 20:02, 1 July 2010 (UTC)
  52. 20:20, 29 July 2010 (UTC)
  53. 08:30, 31 August 2010 (UTC)
  54. 23:27, 28 October 2010 (UTC)
  55. 16:54, 3 November 2010 (UTC)
  56. 15:17, 17 November 2010 (UTC)
  57. 23:36, 25 January 2011 (UTC)
  58. 14:41, 28 February 2011 (UTC)
  59. 21:19, 20 March 2011 (UTC)
  60. 14:58, 9 May 2011 (UTC)
  61. 20:21, 6 June 2011 (UTC)
  62. 15:51, 3 August 2011 (UTC)
  63. 11:49, 13 October 2011 (UTC)
  64. 08:13, 26 October 2011 (UTC)
  65. 17:13, 14 November 2011 (UTC)
  66. 12:30, 27 February 2012 (UTC)
  67. 18:30, 8 March 2012 (UTC)
  68. 18:30, 8 March 2012 (UTC)
  69. 22:01, 5 April 2012 (UTC)
  70. 18:20, 13 April 2012 (UTC)
  71. 23:09, 2 August 2012 (UTC)
  72. 08:54, 18 September 2012 (UTC)
  73. 17:20, 22 October 2012 (UTC)
  74. 23:57, 3 November 2012 (UTC)
  75. 13:14, 2 January 2013 (UTC)
  76. 13:45, 25 February 2013 (UTC)
  77. 10:44, 29 March 2013 (UTC)
  78. 12:07, 3 May 2013 (UTC)
  79. 19:26, 14 June 2013 (UTC)
  80. 05:27, 25 June 2013 (UTC)
  81. 06:56, 21 August 2013 (UTC)
  82. 10:58, 5 September 2013 (UTC)
  83. 09:29, 25 September 2013 (UTC)
  84. 08:13, 23 December 2020 (UTC)
  85. 23:12, 9 January 2014 (UTC)
  86. 23:12, 9 January 2014 (UTC)
  87. 01:37, 21 February 2014 (UTC)
  88. 21:38, 12 August 2014 (UTC)
  89. 22:34, 1 October 2014 (UTC)
  90. 15:53, 14 November 2014 (UTC)
  91. 10:55, 7 January 2015 (UTC)
  92. 06:49, 24 January 2015 (UTC)
  93. 21:41, 26 March 2015 (UTC)
  94. 15:39, 16 April 2015 (UTC)
  95. 09:24, 30 November 2015 (UTC)
  96. 19:23, 11 July 2016 (UTC)
  97. 18:44, 27 July 2016 (UTC)
  98. 16:20, 20 September 2016 (UTC)
  99. 15:20, 10 December 2016 (UTC)
  100. 22:28, 23 February 2017 (UTC)
  101. 13:29, 17 October 2017 (UTC)
  102. 07:13, 6 November 2017 (UTC)
  103. 21:44, 2 December 2017 (UTC)
  104. 20:43, 21 December 2017 (UTC)
  105. 05:35, 31 December 2017 (UTC)
  106. 18:46, 8 February 2018 (UTC)
  107. 20:51, 14 February 2018 (UTC)
  108. 11:03, 11 May 2018 (UTC)
  109. 08:00, 25 September 2018 (UTC)
  110. 12:52, 15 November 2018 (UTC)
  111. 09:42, 4 January 2019 (UTC)
  112. 14:11, 1 June 2020 (UTC)
  113. 09:09, 26 August 2020 (UTC)
  114. 02:53, 2 June 2022 (UTC)

# 15:47, 6 May 2021 (UTC)

Exercise 2

[edit | edit source]

Subtract the "snapshot" value (20070523171222) from the current timestamp using the parserFunction #expr:

# {{subst:#expr: {{subst:CURRENTTIMESTAMP}} - 20070523171222}}

How old is this article? Add your result to the list below by simply copying the code above and pasting it to the bottom of the list.

  1. 9307
  2. 1031678
  3. 1033630
  4. 3833531
  5. 197851905
  6. 487830979
  7. 9706032432
  8. 9999928932
  9. 20184053526
  10. 20382050115
  11. 20485033333
  12. 20590020007
  13. 20684030582
  14. 29589064715
  15. 29589828810
  16. 29795009988
  17. 29896973022
  18. 30206030935
  19. 30307911823
  20. 30505061587
  21. 39985978899
  22. 40481911404
  23. 49703951979
  24. 50601941335
  25. 50601941705
  26. 50601941819
  27. 70590984215
  28. 79803042990
  29. 80077932390
  30. 80606921308
  31. 89791928783
  32. 100493961791
  33. 100582900223
  34. 100698033532
  35. 130302919820
  36. 150078854621

parserFunction #expr:

# {{subst:#expr: {{subst:CURRENTTIMESTAMP}} - 20070523171222}}

Exercise 2.5 Advanced

[edit | edit source]

Because a day has 86400 seconds (not 100000 seconds), the result of the expression isn't particularly useful at determining how old the article actually is in terms of years, months, days, hours, minutes and seconds. Can you write an expression that is accurate and human readable?

  1. 5590.0453587963 days (I have used {{subst:#expr: ({{subst:CURRENTTIMESTAMP}} - 20070523171222)/60/60/24}})
  2. #110924.298692
  3. 40161428403119 or you mean 40161428403119 umm, looks it will be difficult (I used {{subst:#expr:(20070523180500+{{subst:CURRENTTIMESTAMP}})}} # {{subst:#expr: ({{subst:CURRENTTIMESTAMP}}-20070523171222)/60/60/24/365)}} years old this wiki is and {{subst:#expr:(20070523180500+{{subst:REVISIONTIMESTAMP}})}})
  4. 2 Y 4 M * D …
  • Juan go to bad, look at his parser:
 {{subst:#ifexpr: {{subst:CURRENTMONTH}} < 5| {{subst:#expr:{{subst:#expr:{{subst:CURRENTYEAR}}-2007}}-1}}}} 
{{#ifexpr: ({{subst:CURRENTMONTH}} = 5) | 
| {{subst:#expr:{{subst:CURRENTYEAR}}-2007}}}} {{subst:{{#ifexpr: ({{subst:CURRENTMONTH}} > 5) | 
| {{subst:#expr:{{subst:CURRENTYEAR}}-2007}}}} Y {{subst:{{#ifexpr: ({{subst:CURRENTMONTH}} < 5)
| 
{{subst:#expr: Charley looked at this code and... &... catbad. bad form.. no pattern..

{{subst:CURRENTMONTH}}-5}}}} {{subst:#ifexpr: {{subst:CURRENTMONTH}} = 5| 0 }} {{subst:#ifexpr: {{subst:CURRENTMONTH}} < 5| {{subst:CURRENTMONTH}} }} M      
  • hint: The snapshot, 20070523171222 can be broken down with:
* {{#time: Y|20070523171222}},
* {{#time: m|20070523171222}},
* {{#time: d|20070523171222}},
* {{#time: H|20070523171222}},
* {{#time: i|20070523171222}}
& {{#time: s|20070523171222}}

>

to give us 2007, 05, 23, 17, 12 and 22

and what about {{#expr: ({{#time: U|{{CURRENTTIMESTAMP}}}}-{{#time: U|20070523171222}})}} 551171246 - this module's age in seconds.

Have you lost your... (fanbelt)(alias)(user) <*>--Q^#o

Names and numbers

[edit | edit source]

Naming things that deal with chronology and the passage of time are important to Humans. "May" is the current month as this article is being written. Its numeric value is 05 of course. The difference between using a name or numeric value is mostly dependant upon whether one is working on calculations or displaying things for human readers. Generating dynamic content usually involves some sort of CURRENT variable and a parser function or two. In the previous section, we worked some with the CURRENTTIMESTAMP variable and the #expr: parser function. Next, we shall demonstrate the use of the CURRENTMONTH variable and the #switch parser function to make a twelve-month rolling "queue" for featured content. Wikiversity:Picture of the Month (shortcut: WV:POTM).

Activities

[edit | edit source]

According to Wikiversity:Top100, our most popular picture is Image:4-Stroke-Engine.gif. We need to come up with a set of candidates for future POTM use and work on {{POTM}}.

Exercise 3

[edit | edit source]

Add other candidates for POTM by editing the list below:

  1. Image:4-Stroke-Engine.gif
  2. ..edit me..

Picture of the Month

[edit | edit source]

We have a template named {{POTM}} and here is the code:

{| style="text-align:center;background-color: LightGray;"
|-
'''Picture of the Month - {{CURRENTMONTHNAME}}'''
|{{#switch:{{CURRENTMONTH}}
|00=[[Image:4-Stroke-Engine.gif|thumb|center]]
|01=[[Image:Torus.png|thumb|center]]
|02=[[Image:ScienceOlympiad.jpg|thumb|center]]
|03=[[Image:BesselY plot.svg|thumb|center]]
|04=[[Image:BernoullisLawDerivationDiagram.png|thumb|center]]
|05=[[Image:Koeln Hohenzollernbruecke.jpg|thumb|center]]
|06=[[Image:Missione del Guaricano-bimbi a scuola.jpg|thumb|center]]
|07=[[Image:Learning bongos.jpg|thumb|center]]
|08=[[Image:US presidential inauguration 2005.jpg|thumb|center]]
|09=[[Image:Crystal Clear action loopnone.png|thumb|center]]
|10=[[Image:My-computer.svg|thumb|center]]
|11=[[Image:Pythagorean.svg|thumb|center]]
|12=[[Image:Mersenne Prime table.png|thumb|center]]
|}}
|}
Picture of the Month - November




Weekly maintenance

[edit | edit source]

An example of a weekly schema for planning and logging Wikiversity:Maintenance activiities can be found at Wikiversity:Categories.

Back in 2007, we ran a six-week series, Wikiversity:Categories/2007 using this code:

'''2007:''' [[Wikiversity:Categories/2007/Week{{#expr:({{CURRENTWEEK}}-1)}}|last week]] 
| [[Wikiversity:Categories/2007/Week{{CURRENTWEEK}}|this week]] 
| [[Wikiversity:Categories/2007/Week{{#expr:({{CURRENTWEEK}}+1)}}|next week]]

Here, the Category Taskforce generated subpages for planning and logging weekly category maintenance activities. Each week, the "queue" shifts placing the CURRENTWEEK at the "this week" position on the exposed queue, thus creating a red "next week" link at the right for planning next week's work. The Wikiversity:Categories/2007 page lists all the weeks from 20 to 25 leaving a log of category-related activities while the initiative lasted.

Here's a little table for weekly logging and planning:

{| class="prettytable"
|+ Weekly chores [[/{{CURRENTYEAR}}]]
!last week !! this week !! next week
|-
|[[/{{CURRENTYEAR}}/W{{#expr:({{CURRENTWEEK}}-1)}}]] 
|[[/{{CURRENTYEAR}}/W{{CURRENTWEEK}}]] 
|[[/{{CURRENTYEAR}}/W{{#expr:({{CURRENTWEEK}}+1)}}]]
|}

Which makes this:

Weekly chores /2024
last week this week next week
/2024/W44 /2024/W45 /2024/W46

Activities: What other ongoing Wikiversity:Maintenance tasks can use dynamic content to plan and log activities on a weekly basis? Can we come up with an overall schema for helping the custodians? discuss

Daily dynamics

[edit | edit source]

When dealing with daily changes, one needs to consider how large a content base is available and how often it will require updating. As with monthly and weekly dynamic content, daily changes need to operate from some type of queue that can use a CURRENT variable. Active Portals with a good-sized staff of maintainers might consider daily features such as a "Quote of the Day" or "Today's Top Participant" to make their presence more interesting.

Activities

[edit | edit source]

Think and discuss: What kind of content is well-suited for daily rotation? Can something like "Quote of the Day" be dynamically linked from Wikiquote? How can participants be acknowledged fairly for outstanding contributions? It there a way to automatically feature activitly-edited learning resources statistically from recent changes for something like "Today's most active learning project"?

Exercise 6

[edit | edit source]

Start a collection of your favorite quotes. Copy the Code below to your userpage and replacing the examples with your own. Provide a link at QOTD.

{{#switch: {{CURRENTDOW}}
 |0="When I examine myself and my methods of thought, I come to the conclusion that the gift of fantasy has meant more to me than my talent for absorbing positive knowledge." -[[w:Albert Einstein|]]
 |1="I stand on a notch between two eternities." - [[w:Henry David Thoreau|]]
 |2="It's nice to be important, but it's more important to be nice." -[[w:John Lennon|]]
 |3="More powerful than the might of all the armies on Earth is an idea whose time has come." -[[w:Victor Hugo]]
 |4="The bulk of the world’s knowledge is an imaginary construction." -[[w:Helen Keller|]]
 |5="Freedom of expression is the matrix, the indispensable condition, of nearly every other form of freedom." -[[w:Benjamin N. Cardozo]]
 |6="The second handers offer substitutes for competence such as love, charm, kindness - easy substitutes - and there is no substitute for creation." -[[w:Ayn Rand]]
 "When I examine myself and my methods of thought, I come to the conclusion that the gift of fantasy has meant more to me than my talent for absorbing positive knowledge." -Albert Einstein
}}

Exercise 7

[edit | edit source]

Increase the collection of quotes to 31. Then edit the #switch conditional on {{QOTD}} from CURRENTDOW (Day of the Week) to CURRENTDAY (Day of the Month).

{{#switch: {{CURRENTDAY}}
 |0="When I examine myself and my methods of thought, I come to the conclusion that the gift of fantasy has meant more to me than my talent for absorbing positive knowledge." -[[w:Albert Einstein|]]
 |1="I stand on a notch between two eternities." - [[w:Henry David Thoreau|]]
 |2="It's nice to be important, but it's more important to be nice." -[[w:John Lennon|]]
 |3="More powerful than the might of all the armies on Earth is an idea whose time has come." -[[w:Victor Hugo]]
 ...
 |n= Many more quotes
 ...
 |29="The bulk of the world’s knowledge is an imaginary construction." -[[w:Helen Keller|]]
 |30="Freedom of expression is the matrix, the indispensable condition, of nearly every other form of freedom." -[[w:Benjamin N. Cardozo]]
 |31="The second handers offer substitutes for competence such as love, charm, kindness - easy substitutes - and there is no substitute for creation." -[[w:Ayn Rand]]
 "When I examine myself and my methods of thought, I come to the conclusion that the gift of fantasy has meant more to me than my talent for absorbing positive knowledge." -Albert Einstein
}}

Build a Main Page section for Quote of the Day. See if there is interest in building School or topic-specific QOTD collections and expanding {{QOTD}} to include categorized collections of quotes.

..in progress 16:38, 24 May 2007 (UTC)..

A 24-hour clock

[edit | edit source]

Current hour: Midnight (UTC)

The "current hour" shown above is simply a more user-friendly representation of the two-digit value for the server's {{CURRENTHOUR}} variable parsed by the #switch: function:

'''Current hour:''' {{#switch:{{CURRENTHOUR}}
|00=Midnight (UTC)
|01=1 AM (UTC)
|02=2 AM (UTC)
|03=3 AM (UTC)
|04=4 AM (UTC)
|05=5 AM (UTC)
|06=6 AM (UTC)
|07=7 AM (UTC)
|08=8 AM (UTC)
|09=9 AM (UTC)
|10=10 AM (UTC)
|11=11 AM (UTC)
|12=Noon (UTC)
|13=1 PM (UTC)
|14=2 PM (UTC)
|15=3 PM (UTC)
|16=4 PM (UTC)
|17=5 PM (UTC)
|18=6 PM (UTC)
|19=7 PM (UTC)
|20=8 PM (UTC)
|21=9 PM (UTC)
|22=10 PM (UTC)
|23=11 PM (UTC)
|}}

One can scroll through any set of 24 values displaying a different result each hour. See CQ's pick for Person of the hour for a simple application.

Activities

[edit | edit source]

Think and discuss: What are some other uses for "- of the hour" dynamic content? What kinds of things at the Main Page should change each hour? How can this be implemented? Participate at Main page design changes

Exercise 8

[edit | edit source]

After reading and participating at Main page design changes, come up with some dynamics (monthly, weekly, daily, hourly) on a new Main Page/Draft version:

  1. Main Page/Draft version 0.1
  2. Main Page/Draft version 0.2
  3. Main Page/Draft version 0.3
  4. ...

More efficient dynamics

[edit | edit source]

The #time parser function uses only the CURRENTTIMESTAMP variable, but gives us several different ways of making it Human-readable:

  • Raw {{CURRENTTIMESTAMP}} is 20241109001948
  • If we wanted to automagically generate a subpage just for this year, we can use /{{#time:Y|{{CURRENTTIMESTAMP}}}} to get /2024.
  • The current month can be:
    • {{#time:m|{{CURRENTTIMESTAMP}}}} for a numeric - 11
    • {{#time:M|{{CURRENTTIMESTAMP}}}} for abreviated - Nov
    • {{#time:F|{{CURRENTTIMESTAMP}}}} for "Friendly" - November.
  • Using {{#time:j F Y|{{CURRENTTIMESTAMP}}}} displays - 9 November 2024
  • Subpage names can be in the /yyyy/Www/d weekly format using {{#time:Y/"W"W/N|{{CURRENTTIMESTAMP}}}} to get 2024/W45/6
  • Or in a monthly format using Friendly month names {{#time:Y/F/j|{{CURRENTTIMESTAMP}}}} 2024/November/9

One can even mix and match to make subpages in one format displayed in another:

[[{{FULLPAGENAME}}/{{#time:Y/"W"W/N|{{CURRENTTIMESTAMP}}}}|{{#time:j F Y|{{CURRENTTIMESTAMP}}}}]]

Which renders: 9 November 2024 while creating a subpage structure : Generating dynamic content with MediaWiki/2024/W45/6

Exersize 37: Go ahead! Leave a note for today!

See Generating dynamic content with MediaWiki/2024/W45 to join a really dynamic course

Many of these functions can now be implemented more efficiently using Lua modules.

See also

[edit | edit source]