# 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?

A very succint view of "now" can be seen using the {{CURRENTTIMESTAMP}} variable:
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:
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

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 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

Subtract the "snapshot" value (20070523171222) from the current timestamp using the parserFunction #expr:
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:

#### Exercise 2.5 Advanced

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?
* 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:
* hint: The snapshot, 20070523171222 can be broken down with:
to give us 2007, 05, 23, 17, 12 and 22
and what about {{#expr: ({{#time: U|{{CURRENTTIMESTAMP}}}}-{{#time: U|20070523171222}})}} 548133048 - this module's age in seconds.
Have you lost your... (fanbelt)(alias)(user) <*>--Q^#o

## Names and numbers

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

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

Add other candidates for POTM by editing the list below:
* Image:4-Stroke-Engine.gif
* ..edit me..

## Picture of the Month

We have a template named {{POTM}} and here is the code:
| Picture of the Month - October | File:My-computer.svg |

## Weekly maintenance

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:
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:
Which makes this:
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

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

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

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.

#### Exercise 7

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).
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

Current hour: 8 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:
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

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

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:
* Main Page/Draft version 0.1
* Main Page/Draft version 0.2
* Main Page/Draft version 0.3

## More efficient dynamics

The #time parser function uses only the CURRENTTIMESTAMP variable, but gives us several different ways of making it Human-readable:
* Raw {{CURRENTTIMESTAMP}} is 20241004202310
* 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 - 10
** {{#time:M|{{CURRENTTIMESTAMP}}}} for abreviated - Oct
** {{#time:F|{{CURRENTTIMESTAMP}}}} for "Friendly" - October.
* Using {{#time:j F Y|{{CURRENTTIMESTAMP}}}} displays - 4 October 2024
* Subpage names can be in the /yyyy/Www/d weekly format using {{#time:Y/"W"W/N|{{CURRENTTIMESTAMP}}}} to get 2024/W40/5
* Or in a monthly format using Friendly month names {{#time:Y/F/j|{{CURRENTTIMESTAMP}}}} 2024/October/4
One can even mix and match to make subpages in one format displayed in another:
Which renders: 4 October 2024 while creating a subpage structure : Generating dynamic content with MediaWiki/2024/W40/5
Exersize 37: Go ahead! Leave a note for today!
See Generating dynamic content with MediaWiki/2024/W40 to join a really dynamic course
Many of these functions can now be implemented more efficiently using Lua modules.