MediaWiki Content Organizing Features: Templates, Sections, Redirection

Exclusive offer: get 50% off this eBook here
MediaWiki Administrators’ Tutorial Guide

MediaWiki Administrators’ Tutorial Guide — Save 50%

Install, manage, and customize your MediaWiki installation

$23.99    $12.00
by Mizanur Rahman | February 2010 | MySQL Content Management Open Source PHP

In the previous article by Mizanur Rahman, author of MediaWiki Administrators’ Tutorial Guide, we discussed the importance of organizing and managing content for a site. We also studied some of the MediaWiki Content Organizing Features like Namespaces and Categories.

In this article we will see various MediaWiki Content Organizing Features like Templates, Sections, and Redirection.

Template

Consider a case where a Haunted site's users felt that they should make a repository of all haunted movies and share it with others. A movie, however, will have a lot of information to go with it—producers, director, actors, release date, distributors, storyline, etc. It will be very easy to create a page with all the information, but it won't be possible to summarize the movie in a way that anybody can have summary information when they visit any movie's page. There needs to be a common format that will be used by all the movie pages. That is how Wikipedia shows a summary of every James Bond movie at the right side of the page. Since all movies have some common attributes but different values, they use the same format for all the James Bond movie summaries—it's only the attributes' values that change, and not the attributes themselves. Can we use the same thing in our Haunted site?

MediaWiki has the solution, and it is known as a template. A template is a page that can be inserted into another page via a process called transclusion. Templates usually reside in the Template namespace in MediaWiki. Templates are useful for any text for which one wants a copy in two or more pages, and there is no need for each copy to be edited independently, to adapt it to the page it is in. Templates can also be parameterized—we can add parameters to a template in order to show different content based on the parameter. This lets a template act like a subroutine. Looking at it from other angle, a template can be thought of as being like the include file that we use in programming.

Creating our First Template

The syntax for insertion of the page Template:templatename is {{ templatename }}. This is called a template tag. If the page Template:templatename does not exist, then {{ templatename }} works as [[Template:templatename]], a link to a non-existing page, leading to the edit page for the template. Thus, one way of creating a template is putting in the tag first, and then following the link. Let's create our first template using this technique. Write down the following text in the URL section of the browser:

http://haunted.com/index.php?title=Template:Movie_Summary

This will take us to an empty non-existent template page. We can edit the template and save it as our template. Let's make the movie summary information template for our movie section. It will contain the movie name, a poster, screenwriter, cast details, etc. Editing a template page is similar to editing a normal page. There is no difference at all, and so we can use wiki syntax in our template page. Let us add the following content in our template page for a movie named "The Haunting" and save it:

'''The Haunting''' <br>
[[Image:200px-The_Haunting_film.jpg]] <br>
'''The Haunting''' film poster <br>
'''Directed by''' Jan de Bont<br>
'''Produced by''' Donna Roth,<br>
Colin Wilson<br>
'''Written by''' Novel:<br>
Shirley Jackson <br>
'''Screenplay:'''<br>
David Self<br>
'''Starring''' Lili Taylor,<br>
Catherine Zeta-Jones,<br>
Owen Wilson,<br>
Liam Neeson<br>
'''Distributed by''' DreamWorks<br>
'''Release date(s)''' July 20, 1999<br>
'''Running time''' 113 minutes<br>
'''Language''' English<br>
'''Budget''' ~ US$80,000,000<br>

We can now call this template from any of our pages using a pair of double curly braces {{ }} with the name of the template between the braces. Assuming that we are creating a new page where we will show all stories, let's add the template to a story page. Open any of the story pages that we have created so far, and add the following line at the beginning of the edit page:

{{Movie_Summary}}

Now save the page and preview it in the browser. You will see true magic now; the content of the template is shown in the story page as follows:

MediaWiki Content Organizing Features: Templates, Sections, Redirection

We put the template tag at the start of the page, but you can always put it anywhere you want in the content page. We can use templates to create a header, a footer, the copyright information, special messages, etc., for our site. This is a very simple but powerful use of templates. Think about a situation where we have a lot of movie information available. What we did is just for a single movie, but we can use the same template for other movies with the same type of attributes. When we use templates, we don't have to worry about changing the summary attributes in each and every page. We will just change the template and all the pages will be updated, since pages include that template. We can do amazing things using templates. Also, since they are similar to normal pages, we can always create nice-looking templates using tables, images, links, etc.

Templates work on a project basis. So a template in one wiki will not work in another wiki site. In order to use the same template on another wiki site, we have to build the same template in that site. Also when we change a template, we must be careful about the impact of the changes in the pages where the template is actually used.

Parameterizing Templates

We already know that we can add parameters in our template to make it work like a subroutine. I hope all of you know what a subroutine means; if not you could visit the following URL: http://en.wikipedia.org/wiki/Subroutine. Based on its parameters, a subroutine performs some task and shows results. We know templates are not subroutines, but they can be used as subroutines for performing different tasks.

Take the example of our movie summary template. We have hardcoded the name of the movie and other attributes, but we can use the same template for another movie by changing the attributes' values. So it is almost same as adding the content in each page. However, if we can parameterize the template, it will definitely make our task easy.

What we can do is make the movie name, movie poster, writer, actors' names etc., into variables that will be set by parameters and passed from the calling page. All the template parameters are divided into two categories: named parameters and numbered parameters. In order to create a parameterized template, we need to perform the following two tasks every time:

  1. In the template page, declare parameters that will be changed based on the passed values.
  2. Call the template with proper values from the calling page.

Parameters are declared with three pairs of braces with the parameter name inside. {{{myVar}}} declares a parameter name myVar. So in the template, the parameter declaration will done as follows:

{{{parname1|default}}}, {{{parname2|default}}}

and in the tag or calling page, we have to write it as follows:

{{templatename|parname1=parvalue1|parname2=parvalue2}}

The default option in the parameter declaration is totally optional. It can be different for each and every parameter, and applies when no value has been provided for the parameter. Here default stands for the default value of the parameter. This default value will be used if a parameter is not set to any value from the calling pages.

You will see that we are using the parameter name in both template definition and declaration page. This is known as a named parameter. There is another type of parameter as well, called a numbered parameter, which is indicated by the use of ?a number instead of a name. In a numbered parameter option, the declaration looks like this:

{{{1|default}}}, {{{2|default}}}

and in the calling page, we have to write down the tag as follows:

{{templatename|parvalue1|parvalue2}}

Now back to our movie summary example. We want to convert our movie summary template to a named parameterized template. We will use different parameters for different attributes of the template. We will also use a table to make the template look better. Here is the code for the template:

{|style="width:250px; " border="0"
|-
|width=100px|
|width=100px|
|-
| colspan="2" align="center" |'''{{{name}}}'''
|-
| colspan="2" align="center" |[[Image:{{{image}}}
|{{{image_size|200px}}}]]
|-
| colspan="2" align="center" |''{{{caption}}}''
|-
|'''Directed by'''||{{{director}}}
|-
|'''Produced by'''||{{{producer}}}
|-
|'''Written by'''||{{{writer}}}
|-
|'''Screenplay:'''||{{{screenplay}}}
|-
|'''Starring'''||{{{starring}}}
|-
|'''Distributed by''' ||{{{distributor}}}
|-
|'''Release date(s)'''||{{{released}}}
|-
|'''Running time'''|| {{{runtime}}}
|-
|'''Language'''|| {{{language}}}
|-
|'''Budget'''|| {{{budget}}}
|}
MediaWiki Administrators’ Tutorial Guide Install, manage, and customize your MediaWiki installation
Published: March 2007
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

Now save the template and go to the "Haunted Movie" page, where we have included this template. We need to add parameters to the tag, and pass values to the parameters. Write the following tag at the top of the edit box:

{{Movie_Summary |
name = The Haunting |
image = 200px-The_Haunting_film.jpg |
caption = ''The Haunting'' film poster |
writer = '''Novel:'''<br>[[Shirley Jackson]] |
screenplay = [[David Self]] |
starring = [[Lili Taylor]],<BR>[[Catherine
Zeta-Jones]],<br>[[Owen Wilson]],<br>[[Liam Neeson]] |
director = [[Jan de Bont]] |
producer = [[Donna Roth]],<br>[[Colin Wilson]] |
distributor = [[DreamWorks]] |
released = [[July 20]], [[1999]] |
runtime = 113 minutes |
language = English |
budget = ~ US$80,000,000
}}

After saving the page, you will see that the page looks the same with parameterized values. Here is the page we will be shown on the screen:

MediaWiki Content Organizing Features: Templates, Sections, Redirection

Change the values of the parameters, and you will see the difference. We can easily created hundreds and thousands of movie pages with the help of this template. What we need to do is call the template with parameter values, and the template will do the rest.

The same task can be performed with numbered parameters. The numbered parameters will start from 1 and continue until all the parameters are numbered. For numbered parameters, the declaration in the template definition page will be as follows:

{|style="width:250px; " border="0"
|-
|width=100px|
|width=100px|
|-
| colspan="2" align="center" |'''{{{1}}}'''
|-
| colspan="2" align="center" |[[Image:{{{2}}}]]
|-
| colspan="2" align="center" |''{{{3}}}''
|-
|'''Directed by'''||{{{4}}}
|-
|'''Produced by'''||{{{5}}}
|-
|'''Written by'''||{{{6}}}
|-
|'''Screenplay:'''||{{{7}}}
|-
|'''Starring'''||{{{8}}}
|-
|'''Distributed by''' ||{{{9}}}
|-
|'''Release date(s)'''||{{{10}}}
|-
|'''Running time'''|| {{{11}}}
|-
|'''Language'''|| {{{12}}}
|-
|'''Budget'''|| {{{13}}}
|}

and the calling page tag will look like the following, with changed parameters to create a different movie summary with the same template:

{{Movie_Summary
|The Haunting
|200px-The_Haunting_Poster.jpg
|The Haunting DVD cover
|[[Robert Wise]]
|[[Robert Wise]]
|'''Novel:'''<br>[[Shirley Jackson]]
|[[Nelson Gidding]]
|[[Julie Harris]]<br />[[Richard Johnson]]<br />[[Claire Bloom]]
|[[MGM]]
|[[September 18]], [[1963]] ([[USA]])
|112 min.
|[[English language|English]]
|
}}

The output of the page will be:

MediaWiki Content Organizing Features: Templates, Sections, Redirection

So we can see that by changing the attributes' values we can use the same template in any page we want. This helps organize the content more effectively and also give a different identity to different types of content.

MediaWiki Administrators’ Tutorial Guide Install, manage, and customize your MediaWiki installation
Published: March 2007
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

Named versus Numbered Parameters

Even though both named and numbered parameters can be used, there are some places where named parameters are better than numbered parameters, and vice versa. Here are some things to help you choose between named and numbered parameters.

Named parameters are used when you know which parameters exist, and their exact name. So knowing the name of the parameters is a must for named parameters. Also, we can always mix the order of parameters in the parameter list, and so, no particular order is fixed for a named parameter list. We can make one of our own, until we have all the required parameters in the list. Named parameters are also very easy to understand. We can define a meaning and purpose for the parameter.

For numbered parameters, on the other hand, we don't have to write the parameter's name followed by an assignment operator; we just pass the parameter separated by a pipe character. What is important here, however, is that the order of the parameters must be maintained, or else parameters will get wrong values. Numbers are international and they don't need translation for different languages or projects. So unlike named parameters, numbered parameters do not require any translation of the parameter list for using with multilingual sites.

Section

Sections are an efficient way of organizing content inside a page, since they allow us to generate a table of contents automatically, as well as let us edit section contents rather than page content all at once. Each section in an article has a corresponding Edit link on the right side of the section. This link takes us to the article edit page, but with that particular section only. Isn't it amazing? Suppose an article has a 100 sections, and we wanted to edit it. Conventionally, we would have to go through a huge page of 100 sections in the edit box, which will not only look very cumbersome, but will also be very difficult to trace and edit. Sections help us in this matter by ensuring that we get only the relevant section's content while leaving other sections' content untouched, and ensuring that users don't get lost in the huge amount of content. If a page is large, we can also break it into different sub-pages, but that is not always right thing to do. Let's see a comparison between using sections and creating separate pages for a big article.

Comparison between Sections and Separate Pages

Section

Separate pages

Article page contains all the edit histories for the article, not based on section.

Edit histories are stored separately for each page, and can be traced easily.

Article page contains what links here or back links as a whole.

Each page contains the back links, giving more flexibility.

Users can be redirected to a section very easily.

Redirection to a section is not possible.

Loading one large page is more convenient than loading several small ones.

Loading a small page is faster than loading a large page.

Searching within one large page with a local search function is faster, and in some respects better than searching several pages.

Searching separate pages always takes time.

Table of contents is generated providing convenient navigation.

No table of contents is available if we create a separate page for each section.

A single page is always more manageable, since all the contents are in a single place.

Managing many separate pages is difficult, and always needs continuous monitoring for updating.

Creating a Table of Contents Using Sections

We know that sections or headers can be used to create a table of contents for any article. If the article contains more than three sections, then a table of contents is automatically generated. We can also stop the automatic creation of a table of contents by following the methods:

  • Turning it off in the user preference settings
  • In the article edit box, making use of the magic word _NOTOC_.

We can also force the system to show a table of contents even when we have less than three sections in the article. This can be performed by adding the _FORCETOC_ or _TOC_ magic words inside the article. If we use the _FORCETOC_ magic word, then the table of contents is placed before the first header in the article, but if we use the _TOC_ magic word in the article, the table of contents is placed at the position of the _TOC_ word in the article. This gives a great flexibility in moving the table of contents to our desired position, such as to the right, center, or left, or inside a table, and in choosing the number of times we want to show the table of contents in the article.

Redirection

As the name suggest, redirection is the process in which users are redirected to a particular page based on the setup or action defined by someone. Most of us have some familiarity with redirection—when we visit a website online, and that site has been moved, we see a little redirection message, and in a few seconds we are moved to a new page. There are a lot of other places where you would find redirection required. When a page in MediaWiki is moved or renamed, a redirection is created automatically by the system.

A redirect is a page with only the following kind of content:

#REDIRECT [[link in internal link style]]

So far we know that a redirect is used for page movement and renaming in MediaWiki. However, redirects can be used for other purposes too.

  • Finding a page.
  • Conveniently going to a page.
  • Linking indirectly to a page, without the need for a piped link.

In order to create a redirect, we have to create a new page or use an existing page from the site. If the page is new, then add the following line at the beginning of the edit box:

#REDIRECT [[A night in the jungle]]

Suppose the redirect page we have just created is named "Story". Now, whenever someone tries to access the "Story" page, he or she will be redirected to the "A night in the jungle" page. When the user is redirected to the page, he or she will see a small caption at the top of the page, citing details of the page from which they have been redirected:

MediaWiki Content Organizing Features: Templates, Sections, Redirection

Editing a redirect is as simple as creating it. Click the redirect page's name from any of the pages where you see the text Redirected from …. This will take you to the redirect page. Edit the page as you would a normal page, and save it.

We can add additional text after the redirect tag and the link. This can be used as an explanation when we visit the redirect page itself. Extra lines of text are automatically deleted when saving the redirect. The page will not redirect if there is anything on the page before the redirect. Also, there must be no spaces between the # and the REDIRECT. We have to also remember that interwiki redirects and special page redirects are not possible with the current features. Also, redirecting to an anchor is not possible.

Summary

We have seen MediaWiki Content Organizing Features like Templates, Sections, and Redirection.

If you have read this article you may be interested to view :

About the Author :


Mizanur Rahman

Mizanur Rahman from Bangladesh is a Senior Software Engineer at Relisource Technologies (http://www.relisource.com). He loves to work with Java, PHP and other web-based technologies and is a moderator of PHPXperts, the largest PHP user group in Bangladesh.

Contact Mizanur Rahman

Books From Packt

MediaWiki 1.1 Beginner's Guide
MediaWiki 1.1 Beginner's Guide

MediaWiki Skins Design
MediaWiki Skins Design

Elgg Social Networking
Elgg Social Networking

Learning jQuery 1.3
Learning jQuery 1.3

Drupal E-commerce with Ubercart 2.x
Drupal E-commerce with Ubercart 2.x

Expert PHP 5 Tools
Expert PHP 5 Tools

    AJAX and PHP: Building Modern Web Applications 2nd Edition
AJAX and PHP: Building Modern Web Applications 2nd Edition

Joomla! Web Security
Joomla! Web Security

No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
y
w
j
N
h
k
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software