Generating dynamic content with MediaWiki

From Wikiversity

Jump to: navigation, search

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.

Contents

[edit] When is now?

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

Current timestamp = 20081202074539

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.

[edit] 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?

[edit] Exercise 1

Edit this section by adding five tildes (~~~~~) to the numbered list below.

  1. 18:05, 23 May 2007 (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. 09:41, 7 November 2008 (UTC)
  24. 14:49, 16 November 2008 (UTC)

[edit] Exercise 2

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. 5590.0453587963 days (I have used {{subst:#expr: ({{subst:CURRENTTIMESTAMP}} - 20070523171222)/60/60/24}})
  7. 487830979
  8. 110924.298692
  9. 9706032432
  10. 9999928932
  11. edit me

NOTE: 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?

[edit] 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).

[edit] 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}}.

[edit] Exercise 3

Add other candidates for POTM by editing the list below:

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

[edit] Picture of the Month

What we shall do is go ahead and create a template named {{POTM}} and assign the 4-Stroke-Engine.gif as the POTM for May of 2007 and display it here using the code:

{{POTM}}

Image:4-Stroke-Engine.gif


Our code at the outset is fairly raw, for example there are no attributes in the image tag and we only have one image for all twelve months:

{{#switch: {{CURRENTMONTH}}
 |01=[[Image:4-Stroke-Engine.gif]]
 |02=[[Image:4-Stroke-Engine.gif]]
 |03=[[Image:4-Stroke-Engine.gif]]
 |04=[[Image:4-Stroke-Engine.gif]]
 |05=[[Image:4-Stroke-Engine.gif]]
 |06=[[Image:4-Stroke-Engine.gif]]
 |07=[[Image:4-Stroke-Engine.gif]]
 |08=[[Image:4-Stroke-Engine.gif]]
 |09=[[Image:4-Stroke-Engine.gif]]
 |10=[[Image:4-Stroke-Engine.gif]]
 |11=[[Image:4-Stroke-Engine.gif]]
 |12=[[Image:4-Stroke-Engine.gif]]
}}

[edit] Activities

Think and discuss: Where will the POTM be diplayed? On the Wikiversity Main Page? On an appropriate Portal? Maybe there can be other things like a Portal of the Week or Wikiversitan of the Hour. How can we make dynamic content more consensus-driven? Is the Top100 listing a good place to get candidates for featured content? Is the WikiChart system dynamic enough? ...Let's talk!

[edit] Exercise 4

Build your own Picture of the Month collection from your favorite pictures on your userpage for practice. You may get some ideas from Gallery of new files or the current version of this WikiChart. Fill in entries for each month using the code below. Add attributes to the image tags providing captions containing learning resources that link to each image. If you are satisfied with your choices, go ahead and be bold with {{POTM}}.

{{#switch: {{CURRENTMONTH}}
 |01=
 |02=
 |03=
 |04=
 |05=
 |06=
 |07=
 |08=
 |09=
 |10=
 |11=
 |12=
 [[Image:4-Stroke-Engine.gif]]
}}

Note that the empty template still works using its default Image:4-Stroke-Engine.gif, So don't worry about blank values. The sections below show how to increase the frequency of the dynamics for larger faster-moving collections.

[edit] Weekly maintenance

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

2007: last week | this week | next week

'''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 is generating 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 1 to 53 as an index for keeping a running log of category-related activities.

[edit] Activities

Think and discuss: 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 overal schema for helping the custodians?

[edit] Exercise 5

Find an application for planning and logging activity on a monthly or weekly basis. Learn how the FULLPAGENAME variable works. You can also change CURRENTWEEK to CURRENTMONTH if you need a slower dynamic. The code below will create red links to subpages if you add it to your userpage. You will need to manually create a ../2007 (or current year) "index" page using something like

[[{{FULLPAGENAME}}/2007]]

... to create the main link on your selected project or user page.

Next open the new index page and use:

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

Next create pages for last week | this week | next week with things you did last week, are doing this week, and plan to do next week. Now you can return to the ../2007 index page and add future and past weeks as needed: ../2007/Week1 through ..2007/Week53.

Would you like some help? See other ways to do things? talk to me!

[edit] 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.

[edit] 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"?

[edit] 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.

{{#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
}}

[edit] 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).

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

[edit] A 24-hour clock

Current hour: 7 AM (UTC)

The "current hour" shown above is simply a more user-frienly 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.

[edit] 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

[edit] 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:

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

[edit] See also

Personal tools