Extension:Widgets

The Widgets extension allows the creation of raw HTML pages that can be embedded (similarly to templates) in normal wiki pages. You do this by creating pages in the Widget namespace. They avoid the security problems of raw HTML in editable wiki pages because the privilege to edit in the Widget namespace is managed. The Widgets extension has become the generally-accepted means of embedding raw HTML in MediaWiki, and many pre-written Widgets are available.

This extension is maintained by Sergey Chernyshev, Jeroen De Dauw and Yaron Koren.

Installation
Download the code archive and extract it to the  folder. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installing from tarball
You can find the current version of Widgets on the Google Code download page, as well as a list of legacy downloads.

Configuration
To install this extension, add the following to LocalSettings.php:

Folder permissions
Also, the  folder should be made writable by the web server. See Making a directory writable by the webserver. The compiled templates folder is where Smarty stores pre-compiled templates.

An example of Qnap: Start telnet with port22 find / -name 'compiled_templates' [cr] [result] /share/MD0_DATA/Qweb/mediawiki-1.15.1/extensions/Widgets/compiled_templates chmod a+rw /share/MD0_DATA/Qweb/mediawiki-1.15.1/extensions/Widgets/compiled_templates

Usage
To add a widget to your MediaWiki installation, just create a page in the Widget: namespace and then use the  parser function to include it in the pages of the wiki.

parser function
To add a defined widget to pages, users can use the  parser function. The syntax is as follows:

Where WidgetName is a page name in the Widget namespace (e.g. Widget:WidgetName) and param=value pairs are the settable parameters, defined within the widget code.

Pages in Widget namespace
All widgets in the wiki are defined by creating pages in the special  namespace, which is automatically added to your wiki when you install this extension.

To see all Widgets defined in your system, you can simply go to the page, select "Widget" in the namespace dropdown and click "Go".

For security reasons, these pages are only editable by wiki administrators - see #User rights below for more info.

You can find many pre-defined widgets to install in your wiki at MediaWikiWidgets.org. If you're interested in creating widgets yourself, see the next section.

Widget page syntax
The Widgets extension uses the Smarty PHP templating engine to provide simple templating functionality within widget pages. All parameters passed to a widget are converted into Smarty parameters.

Important: Use escape modifiers on all passed-in parameters to prevent users from passing in raw HTML from normal wiki pages. Failure to protect against this will expose the hosting site to XSS (and other) attacks.

Arrays
If you use the same parameter multiple times, the widget will get an array of values. You can use to go through the array.

Booleans (true/false)
Since version 0.8.5, this extension supports passing boolean parameters by just using a parameter name without a value, like this:

This will set  to   for your widget. Also, in addition to PHP's default handling of boolean conversions, you can use value "false" to set the boolean value  (this is not the case in PHP as string "false" doesn't get converted to a boolean false; see PHP docs on boolean casting).

Dotted notation
Parameter names can have dots, and Smarty will interpret them as associative arrays, so you can use with both   and   attributes to traverse through them, or you can just use the same name with dots if you want to reference the parameter directly.

Validate modifier
In addition to standard Smarty modifiers (like the heavily used ), the Widgets extension implements the   modifier, that uses PHP Data filtering to allow for validating widget parameters.

To make sure the  variable value is a valid URL, you can use following code: Homepage

The following values for the validate are supported (mapping to PHP's validation filters):
 * (FILTER_VALIDATE_URL)
 * (FILTER_VALIDATE_INT)
 * (FILTER_VALIDATE_BOOLEAN)
 * (FILTER_VALIDATE_FLOAT)
 * (FILTER_VALIDATE_EMAIL)
 * (FILTER_VALIDATE_IP)

Refreshing a widget page
If you're using a call to the widget within the widget page itself, then you will not see the updated widget (and no widget at all when you just created a page). This happens because the page contents are not available to the Widgets extension until a page is saved, but the call to the  parser function is made before the page is saved. After the page is saved, it's cached by MediaWiki, so you won't see the result even if you reload the page via the browser. To make the latest edits to the widget code appear, you need to refresh the page in the cache; to do this, you just need to use the  action (see also Purge extension), or wait a certain amount of time (up to 24 hours).

Widgets and ParserFunctions
Using ParserFunctions within templates to display widgets, makes them an invaluable tool for creating complex displays of data with minimal lines of code.

It is particularly helpful if you want to pre-set some parameters of the widget while allowing users to modify others (e.g. video ID for YouTube widget or user handle for Twitter widget).

Simply including a widget in existing template that collects necessary data might be an easy way to enhance existing site without additional content work.

Additional configuration
The steps in this section are optional - the extension should work fine even without these changes, but they will give you more flexibility if you have a complex MediaWiki installation.

Using FlaggedRevs for widget review
As of version 0.9 you can use the FlaggedRevs extension to enable a widget security review process.

This will enable the Widgets extension to use only stable versions of the Widget:* pages.

When you have your FlaggedRevs configured in the way that represents your review process, just set the following variable in your LocalSettings.php:

This will tell the Widgets extension not to protect Widget:* pages using permissions, but rather require changes to be reviewed for security before they can take effect.

To just use FlaggedRevs to review widgets, the following configuration should suffice:

Changing the namespace ID
The default ID for the "Widget:" namespace is 274. This can be changed if you want, but make sure that it's an even number. To set the namespace ID, define a value for NS_WIDGET before you include the extension in LocalSettings.php: The "Widget talk" namespace ID (NS_WIDGET_TALK) will be automatically defined as  + 1, so don't set it yourself.

You can use  and   when configuring other MediaWiki properties.

User rights
This extension adds a namespace called "Widget", but due to potential security implications that can result from using insecure widget code, this namespace is only editable by users who have the  permission (the   group is also created to add users to; see Help:User rights management for more details).

CHANGES

 * 0.9.2 (January 12, 2011) - Release 0.9.2:
 * 0.9.1 (September 19, 2010) - Release 0.9.1: Some code structure changes and fix to a bug 25219.
 * 0.9 (April 16, 2010) - Release 0.9: Added support for FlaggedRev extension controlling Widget review.
 * 0.8.10 (November 27, 2009) - Security Release: Fixed a security hole in error message.
 * 0.8.9 (November 11, 2009) - Bugfix release: base64 wasn't identified properly which caused some widgets not to be displayed.
 * 0.8.8 (November 2, 2009) - HTML is inserted as is now, thanks to Joshua C. Lerner.
 * 0.8.7 (June 19, 2009) - namespaces issues and i18n issues fixed by ialex.
 * 0.8.6 (May 22, 2009) - some important security fixes by Tim Starling.
 * 0.8.5 (March 12, 2009) - Allowing parameters without values to work as bulean "true" and converting test "false" into boolean false (which is not the case in PHP itself).
 * 0.8.4 (March 11, 2009) - Minor security release - compiled Smarty templates can't be accessed directly. Also fixed a problem with compiled_templates being empty in archives and therefore not extractable by some software.
 * 0.8.3 (February 10, 2009) - Added 'validate' modifier that makes sure variable value conforms to validation rules (using http://us.php.net/filter).
 * 0.8.2 (January 27, 2009) - Fixed a bug when widgets were not showing up correctly on old versions of the pages. Thanks to Max Ingleton for a bug report.
 * 0.8.1 (June 25, 2008) - Worked around MediaWiki bug that inserts  in front of widget output.
 * 0.8 (June 10, 2008) - Fixed a bug where variables were carried over from one widget to another on the same page
 * 0.7 (May 23, 2008) - Added support for arrays and Smarty's dotted notation
 * 0.6 (May 9, 2008) - Minor tweaks and bug fixes, release Makefile
 * 0.5 (Feb 11, 2008) - First public release

Contributing Widgets
If you created a widget and would like to share it, feel free to post it to MediaWikiWidgets.org, or just add it to Widget library.

Bugs and feature requests
If you found a problem, would like to contribute a patch or request a new feature, feel free to open a bug in MediaWiki Bugzilla:

https://bugzilla.wikimedia.org/enter_bug.cgi?product=MediaWiki+extensions&component=Widgets

Support
The best way to seek help with this extension is to send your questions to MediaWiki Widgets Google group

http://groups.google.com/group/mediawiki-widgets

The extension maintainer, and active users and contributors, are on this list and will be able to help you.

Troubleshooting
There are a few common problems that users encounter when they start to use Widgets extension - we'll try to document them here:
 * On a widget page, right after you just created it (or copied from MediaWikiWidgets.org)you see the message:

Warning: Smarty error: unable to read resource: "Widget:" /path-to-your-mediawiki/extensions/Widgets/ smarty/Smarty.class.php on line 1095


 * This is most likely caused by the widget not yet existing at the moment when the widget page itself is being processed - to solve this simply purge the page, e.g. add &action=purge (or ?action=purge if you have nice URLs) to the URL.
 * It's also possible that you called the Widget incorrectly. Widget page names are case sensitive and must match the name of the widget you're calling. E.g. don't use when the widget is called "Widget:YouTube", or vice versa.

PHP Fatal error: Smarty error: unable to write to $compile_dir '/path-to-your-mediawiki/extensions/Widgets/compiled_templates'. Be sure $compile_dir is writable by the web server user. in /path-to-your-mediawiki/extensions/Widgets/smarty/Smarty.class.php on line 1095, referer: http://your-wiki.com/Widget:
 * If the page doesn't load and you see the following error message in the log file:
 * Check if you changed permissions for Smarty to store compiled templates in.


 * If your wiki began returning totally white pages or 500 errors when you updated Mediawiki to 1.20 or a later version, try setting the permissions on extensions/Widgets/compiled_templates to 777.

Widget library
MediaWikiWidgets.org contains a full library of ready-made widgets, including support for most of the major video sites. Any widget can be used simply by copying over the contents of the page.

To get the most up-to-date list of widgets, click here.

Extensions that can be replaced with widgets
Let's collect a list of extensions that can be replaced with widgets because all they do is output some HTML/JS/CSS with parameters and simple logic that can be done using Smarty templates.

Maybe someone will come and create a widget for that to simplify deployment. Also, these lists of extensions are a good source for action:
 * Special:WhatLinksHere/Template:XSS_alert
 * Extension_Matrix/parser_function
 * Extension:RawMsg

Sites using this extension

 * see here for sites that use Widgets