Generating dynamic content with MediaWiki
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?
editA very succint view of "now" can be seen using the {{CURRENTTIMESTAMP}} variable:
Current timestamp = 20241203170008
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
editThink: 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
editEdit this section by adding a pound sign and five tildes (# ~~~~~) to the bottom of the list below.
- 18:05, 23 May 2008 (UTC)
- 20:28, 24 May 2007 (UTC)
- 20:47, 24 May 2007 (UTC)
- 02:30, 21 July 2007 (UTC)
- 01:44, 7 August 2007 (UTC)
- 15:09, 6 October 2007 (UTC)
- 03:27, 9 October 2007 (UTC)
- 00:17, 11 October 2007 (UTC)
- 00:38, 10 December 2007 (UTC)
- 03:05, 7 January 2008 (UTC)
- 20:35, 29 February 2008 (UTC)
- 19:56, 15 May 2008 (UTC)
- 10:00, 23 May 2008 (UTC)
- 04:14, 10 June 2008 (UTC)
- 14:45, 4 July 2008 (UTC)
- 15:09, 14 July 2008 (UTC)
- 18:17, 21 July 2008 (UTC)
- 20:32, 28 July 2008 (UTC)
- 12:45, 27 August 2008 (UTC)
- 17:18, 2 September 2008 (UTC)
- 21:26, 4 October 2008 (UTC)
- 21:28, 4 October 2008 (UTC)
- 23:27, 21 January 2010 (UTC)
- 09:41, 7 November 2008 (UTC)
- 14:49, 16 November 2008 (UTC)
- 17:31, 14 December 2008 (UTC)
- 22:14, 5 January 2009 (UTC)
- 00:58, 22 January 2009 (UTC)
- 20:55, 27 May 2009 (UTC)
- 13:09, 1 April 2009 (UTC)
- 23:51, 13 June 2009 (UTC)
- 22:42, 7 July 2009 (UTC)
- 22:40, 3 August 2009 (UTC)
- 20:27, 4 August 2009 (UTC)
- 22:06, 5 September 2009 (UTC)
- 09:57, 16 September 2009 (UTC)
- 13:45, 30 September 2009 (UTC)
- 20:45, 8 October 2009 (UTC)
- 06:48, 27 October 2009 (UTC)
- 00:46, 5 November 2009 (UTC)
- 17:47, 10 November 2009 (UTC)
- 20:16, 7 December 2009 (UTC)
- 20:16, 7 December 2009 (UTC)
- 22:09, 1 January 2010 (UTC)
- 23:57, 12 January 2010 (UTC)
- 23:28, 21 January 2010 (UTC)
- 23:28, 21 January 2010
- 14:42, 20 April 2010 (UTC)
- 15:57, 23 June 2010 (UTC)
- 14:31, 30 June 2010 (UTC)
- 20:02, 1 July 2010 (UTC)
- 20:20, 29 July 2010 (UTC)
- 08:30, 31 August 2010 (UTC)
- 23:27, 28 October 2010 (UTC)
- 16:54, 3 November 2010 (UTC)
- 15:17, 17 November 2010 (UTC)
- 23:36, 25 January 2011 (UTC)
- 14:41, 28 February 2011 (UTC)
- 21:19, 20 March 2011 (UTC)
- 14:58, 9 May 2011 (UTC)
- 20:21, 6 June 2011 (UTC)
- 15:51, 3 August 2011 (UTC)
- 11:49, 13 October 2011 (UTC)
- 08:13, 26 October 2011 (UTC)
- 17:13, 14 November 2011 (UTC)
- 12:30, 27 February 2012 (UTC)
- 18:30, 8 March 2012 (UTC)
- 18:30, 8 March 2012 (UTC)
- 22:01, 5 April 2012 (UTC)
- 18:20, 13 April 2012 (UTC)
- 23:09, 2 August 2012 (UTC)
- 08:54, 18 September 2012 (UTC)
- 17:20, 22 October 2012 (UTC)
- 23:57, 3 November 2012 (UTC)
- 13:14, 2 January 2013 (UTC)
- 13:45, 25 February 2013 (UTC)
- 10:44, 29 March 2013 (UTC)
- 12:07, 3 May 2013 (UTC)
- 19:26, 14 June 2013 (UTC)
- 05:27, 25 June 2013 (UTC)
- 06:56, 21 August 2013 (UTC)
- 10:58, 5 September 2013 (UTC)
- 09:29, 25 September 2013 (UTC)
- 08:13, 23 December 2020 (UTC)
- 23:12, 9 January 2014 (UTC)
- 23:12, 9 January 2014 (UTC)
- 01:37, 21 February 2014 (UTC)
- 21:38, 12 August 2014 (UTC)
- 22:34, 1 October 2014 (UTC)
- 15:53, 14 November 2014 (UTC)
- 10:55, 7 January 2015 (UTC)
- 06:49, 24 January 2015 (UTC)
- 21:41, 26 March 2015 (UTC)
- 15:39, 16 April 2015 (UTC)
- 09:24, 30 November 2015 (UTC)
- 19:23, 11 July 2016 (UTC)
- 18:44, 27 July 2016 (UTC)
- 16:20, 20 September 2016 (UTC)
- 15:20, 10 December 2016 (UTC)
- 22:28, 23 February 2017 (UTC)
- 13:29, 17 October 2017 (UTC)
- 07:13, 6 November 2017 (UTC)
- 21:44, 2 December 2017 (UTC)
- 20:43, 21 December 2017 (UTC)
- 05:35, 31 December 2017 (UTC)
- 18:46, 8 February 2018 (UTC)
- 20:51, 14 February 2018 (UTC)
- 11:03, 11 May 2018 (UTC)
- 08:00, 25 September 2018 (UTC)
- 12:52, 15 November 2018 (UTC)
- 09:42, 4 January 2019 (UTC)
- 14:11, 1 June 2020 (UTC)
- 09:09, 26 August 2020 (UTC)
- 02:53, 2 June 2022 (UTC)
# 15:47, 6 May 2021 (UTC)
Exercise 2
editSubtract 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.
- 9307
- 1031678
- 1033630
- 3833531
- 197851905
- 487830979
- 9706032432
- 9999928932
- 20184053526
- 20382050115
- 20485033333
- 20590020007
- 20684030582
- 29589064715
- 29589828810
- 29795009988
- 29896973022
- 30206030935
- 30307911823
- 30505061587
- 39985978899
- 40481911404
- 49703951979
- 50601941335
- 50601941705
- 50601941819
- 70590984215
- 79803042990
- 80077932390
- 80606921308
- 89791928783
- 100493961791
- 100582900223
- 100698033532
- 130302919820
- 150078854621
parserFunction #expr:
# {{subst:#expr: {{subst:CURRENTTIMESTAMP}} - 20070523171222}}
Exercise 2.5 Advanced
editBecause 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?
- 5590.0453587963 days (I have used {{subst:#expr: ({{subst:CURRENTTIMESTAMP}} - 20070523171222)/60/60/24}})
- #110924.298692
- 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}})}})
- 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}})}} 553304866 - this module's age in seconds.
Have you lost your... (fanbelt)(alias)(user) <*>--Q^#o
Names and numbers
editNaming 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
editAccording 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
editAdd other candidates for POTM by editing the list below:
- Image:4-Stroke-Engine.gif
- ..edit me..
Picture of the Month
editWe 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 - December |
Weekly maintenance
editAn 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:
last week | this week | next week |
---|---|---|
/2024/W48 | /2024/W49 | /2024/W50 |
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
editWhen 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
editThink 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
editStart 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
editIncrease 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
editCurrent hour: 5 PM (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
editThink 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
editAfter reading and participating at Main page design changes, come up with some dynamics (monthly, weekly, daily, hourly) on a new Main Page/Draft version:
More efficient dynamics
editThe #time parser function uses only the CURRENTTIMESTAMP variable, but gives us several different ways of making it Human-readable:
- Raw {{CURRENTTIMESTAMP}} is 20241203170008
- 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 - 12
- {{#time:M|{{CURRENTTIMESTAMP}}}} for abreviated - Dec
- {{#time:F|{{CURRENTTIMESTAMP}}}} for "Friendly" - December.
- Using {{#time:j F Y|{{CURRENTTIMESTAMP}}}} displays - 3 December 2024
- Subpage names can be in the /yyyy/Www/d weekly format using {{#time:Y/"W"W/N|{{CURRENTTIMESTAMP}}}} to get 2024/W49/2
- Or in a monthly format using Friendly month names {{#time:Y/F/j|{{CURRENTTIMESTAMP}}}} 2024/December/3
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: 3 December 2024 while creating a subpage structure : Generating dynamic content with MediaWiki/2024/W49/2
Exersize 37: Go ahead! Leave a note for today!
See Generating dynamic content with MediaWiki/2024/W49 to join a really dynamic course
Many of these functions can now be implemented more efficiently using Lua modules.
See also
edit- Help:Dynamic page list
- JavaScript Challenges
- Threaded discussions with NavFrames
- List of learning resources related to Wikipedia - ordered by type, including external links
- Lua