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.
A very succint view of "now" can be seen using the {{CURRENTTIMESTAMP}} variable:
Current timestamp = 20241121044454
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.
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?
Edit this section by adding a pound sign and five tildes (# ~~~~~) to the bottom of the list below.
# 15:47, 6 May 2021 (UTC)
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.
parserFunction #expr:
# {{subst:#expr: {{subst:CURRENTTIMESTAMP}} - 20070523171222}}
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?
{{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
* {{#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}})}} 552223952 - this module's age in seconds.
Have you lost your... (fanbelt)(alias)(user) <*>--Q^#o
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).
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}}.
Add other candidates for POTM by editing the list below:
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 |
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:
last week | this week | next week |
---|---|---|
/2024/W46 | /2024/W47 | /2024/W48 |
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
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.
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"?
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
}}
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)..
Current hour: 4 AM (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.
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
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:
The #time parser function uses only the CURRENTTIMESTAMP variable, but gives us several different ways of making it Human-readable:
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: 21 November 2024 while creating a subpage structure : Generating dynamic content with MediaWiki/2024/W47/4
Exersize 37: Go ahead! Leave a note for today!
See Generating dynamic content with MediaWiki/2024/W47 to join a really dynamic course
Many of these functions can now be implemented more efficiently using Lua modules.