%META:TOPICINFO{author="PeterThoeny" date="1071473016" format="1.0" version="1.26"}%
%TOC%
%STARTINCLUDE%
---# TWiki Plugins

_Plug-in enhanced feature add-ons, with a Plugin API for developers_

---++ Overview

You can add Plugins to extend TWiki's functionality, without altering the core program code. A plug-in approach lets you:

	* add virtually unlimited features while keeping the main TWiki code compact and efficient;
	* heavily customize an installation and still do clean updates to new versions of TWiki;
	* rapidly develop new TWiki functions in Perl using the Plugin API.

Everything to do with TWiki Plugins - demos, new releases, downloads, development, general discussion - is available at TWiki.org, in the <b> TWiki:Plugins </b> web.

---++ Preinstalled Plugins

TWiki comes with a set of Plugins as part of the standard installation.

	* DefaultPlugin: Optionally handles some legacy variables from older versions of TWiki. You can control this option from %TWIKIWEB%.TWikiPreferences. (Perl programmers can also [[#DefaultPluginAlt][add rules]] for simple custom processing.)
	* EmptyPlugin: Is a fully functional module, minus active code; it does nothing and serves as a template for new Plugin development. 
	* InterwikiPlugin: Use it for shorthand linking to remote sites, ex: =TWiki:Plugins= expands to TWiki:Plugins on TWiki.org. You can edit the predefined set of of Wiki-related sites, and add your own.
	* EditTablePlugin: %EDITTABLEPLUGIN_SHORTDESCRIPTION%
	* RenderListPlugin: %RENDERLISTPLUGIN_SHORTDESCRIPTION%
	* SlideShowPlugin: %SLIDESHOWPLUGIN_SHORTDESCRIPTION%
	* SmiliesPlugin: %SMILIESPLUGIN_SHORTDESCRIPTION%
	* SpreadSheetPlugin: %SPREADSHEETPLUGIN_SHORTDESCRIPTION%
	* TablePlugin: %TABLEPLUGIN_SHORTDESCRIPTION%

#InstallPlugin
---++ Installing Plugins

Each <nop>TWikiPlugin comes with full documentation: step-by-step installation instructions, a detailed description of any special requirements, version details, and a working example for testing. 

Most Plugins can be installed in three easy steps, with no programming skills required:
	1 *Download* the zip file containing the Plugin, documentation, and any other required files, from TWiki:Plugins. 
	1 *Distribute* the files to their proper locations - unzip the zip archive in your TWiki installation directory - if have a standard TWiki installation, this will distribute automatically. Otherwise, place the files according to the directory paths listed on the Plugin top in TWiki:Plugins.
	1 *Check* the demo example on the Plugin topic: if it's working, the installation was fine!

*Special Requests:* Some Plugins need certain Perl modules to be preinstalled on the host system. Plugins may also use other resources, like graphics, other modules, applications, templates. In these cases, detailed instructions are in the Plugin documentation.

Each Plugin has a standard release page, located in the TWiki:Plugins web at TWiki.org. In addition to the documentation topic (=SomePlugin=), there's a separate development page.
	* *Doc page:* Read all available info about the Plugin; download the attached distribution files.
	* *Dev page:* Post feature requests, bug reports and general dev comments; topic title ends in =Dev= (=SomePluginDev=).
	* *User support:* Post installation, how to use type questions (and answers, if you have them) in the TWiki:Support web.

#PreTesting
---+++ On-Site Pretesting

To test new Plugins on your installation before making them public, you may want to use one of these two approaches:

	* *Method 1:* Safely test on-the-fly by creating separate Production and Test branches in your live TWiki installation. 
		* *Duplicate* the =twiki/bin= and =twiki/lib= directories for the Test version, and adjust the paths in the new =lib/TWiki.cfg=.  The following directories are shared: =twiki/data=, =twiki/templates= and =twiki/pub=.
		* *Test* Plugins and other new features in the Test installation until you're satisfied.
			* %X% If you modify topics using the new features, live users will likely see unfamiliar new META tags showing up on their pages - to avoid this, create and edit test-only topics to try out new features.
		* *Copy* the modified files to the Production installation. You can update a TWiki installation live and users won't even notice.

	* *Method 2:* List the Plugin being tested in the =DISABLEDPLUGINS= variable in %TWIKIWEB%.TWikiPreferences. Redefine the =DISABLEDPLUGINS= variable in the =Sandbox= web and do the testing there.

---+++ A Note on Plugin Performance

The performance of the system depends on the number of Plugins installed and on the Plugin implementation. Some Plugins impose no measurable performance decrease, some do. For example, =outsidePREHandler= is an expensive callback function, or a Plugin might use many Perl libraries that need to be initialized with each page view (unless you run mod_perl). It is recommended to measure the performance with and without a new Plugin. Example for Unix:%BR% =time wget -qO /dev/null <nop>%SCRIPTURL%/view%SCRIPTSUFFIX%/%TWIKIWEB%/AbcPlugin=

In case you need to install an "expensive" Plugin and you need its functionality only in one web you can place the Plugin topic into that web. TWiki will initialize the Plugin only if the Plugin topic is found (which won't be the case for other webs.)

---++ Managing Plugins

When you finish [[#InstallPlugin][installing a Plugin]], you should be able to read the user instructions and go. In fact, some Plugins require additional settings or offer extra options that you have to select. Also, you may want to make a Plugin available only in certain webs, or temporarily disable it. And may want to list all available Plugins in certain topics. You can handle all of these management tasks with simple procedures.

---+++ Setting Preferences

Installed Plugins can be *toggled on or off*, site-wide or by web, through <nop>TWikiPreferences and individual <nop>WebPreferences:

	* All Plugin modules present in the =lib/TWiki/Plugins= directory are activated automatically unless disabled by the =DISABLEDPLUGINS= Preferences variable in <nop>TWikiPreferences. You can optionally list the installed Plugins in the =INSTALLEDPLUGINS= Preferences variable. This is useful to define the sequence of Plugin execution, or to specify other webs than the %WIKITOOLNAME% web for the Plugin topics. Settings in %TWIKIWEB%.TWikiPreferences are:
		* =Set INSTALLEDPLUGINS = <nop>DefaultPlugin, ...= 
		* =Set DISABLEDPLUGINS = <nop>EmptyPlugin, ...=

*Plugin execution order* in TWiki is determined by searching Plugin topics in a specific sequence: First, full =web.topicname= name, if specified in =INSTALLEDPLUGINS=; next, the %TWIKIWEB% web is searched; and finally, the current web.

*Plugin-specific settings* are done in individual Plugin topics. Two settings are standard for each Plugin:
	1 One line description, used to form the bullets describing the Plugins in the %TWIKIWEB%.TextFormattingRules topic:
		* =Set SHORTDESCRIPTION = Blah blah woof woof.=
	1 Debug Plugin, output can be seen in =data/debug.txt=. Set to 0=off or 1=on:
		* =Set DEBUG = 0=
	* The settings can be retrieved as Preferences variables like =%&lt;pluginname&gt;_&lt;var&gt;%=, ex: =%<nop>DEFAULTPLUGIN_SHORTDESCRIPTION%= shows the description of the <nop>DefaultPlugin.

---+++ Listing Active Plugins

Plugin status variables let you list all active Plugins wherever needed. There are two list formats:
	* The =%<nop>ACTIVATEDPLUGINS%= variable lists activated Plugins by name. (This variable is displayed in %TWIKIWEB%.TWikiPreferences for debugging use.)
	* The =%<nop>PLUGINDESCRIPTIONS%= variable displays a bullet list with a one-line description of each active Plugins. This variable is based on the =%&lt;plugin&gt;_SHORTDESCRIPTION%= Preferences variables of individual topics and is shown in %TWIKIWEB%.TextFormattingRules.

<blockquote style="background-color:#f5f5f5">
*DEMO: Automatically List Active Plugins Using Variables*

Using =%<nop>ACTIVATEDPLUGINS%=:<br />
On this TWiki site, the active Plugins are: %ACTIVATEDPLUGINS%.

Using =%<no>PLUGINDESCRIPTIONS%=:<br />
You can use any of these active TWiki Plugins:
%PLUGINDESCRIPTIONS%
</blockquote>

#PluginAPI
---++ The TWiki Plugin API

The Application Programming Interface (API) for <nop>TWikiPlugins provides the specifications for hooking into the core TWiki code from your external Perl Plugin module. The Plugin API is new to the Production version of TWiki with the 01-Sep-2001 release.

---+++ Available Core Functions

The TWikiFuncModule (=lib/TWiki/Func.pm=) implements ALL official Plugin functions. Plugins should ONLY use functions published in this module.

%X% If you use functions not in =Func.pm=, you run the risk of creating security holes. Also, your Plugin will likely break and require updating when you upgrade to a new version of TWiki.

---+++ Predefined Hooks

In addition to TWiki core functions, Plugins can use *predefined hooks*, or *call backs*, listed in the =lib/TWiki/Plugins/EmptyPlugin.pm= module.

	* All but the initPlugin are disabled. To enable a call back, remove =DISABLE_= from the function name.
	* For best performance, enable only the functions you really need. NOTE: =outsidePREHandler= and =insidePREHandler= are particularly expensive. 

Most Plugins use either the =commonTagsHandler= or =startRenderingHandler= for rendering tasks:
	* =commonTagsHandler:= Use it to expand =%<nop>XYZPLUGIN%= and =%<nop>XYZPLUGIN{...}%= variables
	* =startRenderingHandler:= Use it for your own rendering rules or to overload TWiki's internal rendering like =[<nop>[links]]=

TWiki:Codev/StepByStepRenderingOrder helps you decide which rendering handler to use.

---+++ Hints on Writing Fast Plugins

	* Delay the Plugin initialization to the actual function which is handling the tag. This way all the expensive initialization is done only when needed.
	* For example, use an =eval= block like:%BR%
	  =eval { require IPC::Run }= %BR%
	  =return "&lt;font color=\"red\"&gt;SamplePlugin: Can't load required modules ($@)&lt;/font&gt;" if $@;=
	* You could return errors as strings to show what happened
	* You can use a flag to avoid running the initialization twice

---+++ Plugin Version Detection

To eliminate the incompatibility problems bound to arise from active open Plugin development, a Plugin versioning system and an API =GetVersion= detection routine are provided for automatic compatibility checking.

	* All modules require a =$VERSION='0.000'= variable, beginning at =1.000=.

	* The =initPlugin= handler should check all dependencies and return TRUE if the initialization is OK or FALSE if something went wrong.
		* The Plugin initialization code does not register a Plugin that returns FALSE (or that has no =initPlugin= handler).

	* =$VERSION='1.000'= is the current setting in =TWiki::Plugins.pm= and in the preinstalled system Plugins (DefaultPlugin, EmptyPlugin, InterwikiPlugin).

#CreatePlugins
---++ Creating Plugins

With a reasonable knowledge of the Perl scripting language, you can create new Plugins or modify and extend existing ones. Basic plug-in architecture uses an Application Programming Interface (API), a set of software instructions that allow external code to interact with the main program. The [[#PluginAPI][TWiki Plugin API]] Plugins by providing a programming interface for TWiki.

#DefaultPluginAlt
---+++ The <nop>DefaultPlugin Alternative

	* *<nop>DefaultPlugin* can handle some outdated TWiki variables, found, for example, in sites recently updated from an old version. Settings are in DefaultPlugin topic. You can also add your own simple custom processing rules here, though in all but very simple cases, writing a new Plugin is preferable.

---+++ Anatomy of a Plugin

A basic TWiki Plugin consists of two elements:

	* a Perl module, ex: =MyFirstPlugin.pm=
	* a documentation topic, ex: =MyFirstPlugin.txt=

The Perl module can be a block of code that connects with TWiki alone, or it can include other elements, like other Perl modules (including other Plugins), graphics, TWiki templates, external applications (ex: a Java applet), or just about anything else it can call.
In particular, files that should be web-accessible (graphics, Java applets ...) are best placed as attachments of the =MyFirstPlugin= topic. Other needed Perl code is best placed in a =lib/TWiki/Plugins/MyFirstPlugin/= directory.

The Plugin API handles the details of connecting your Perl module with main TWiki code. When you're familiar with the [[#PluginAPI][Plugin API]], you're ready to develop Plugins.

---+++ Creating the Perl Module 

Copy file =lib/TWiki/Plugins/EmptyPlugin.pm= to =&lt;name&gt;Plugin.pm=. The =EmptyPlugin.pm= module contains mostly empty functions, so it does nothing, but it's ready to be used. Customize it. Refer to the [[#PluginAPI][Plugin API]] specs for more information.

If your Plugin uses its own modules and objects, you must include the name of the Plugin in the package name. For example, write =Package <nop>MyFirstPlugin::Attrs;= instead of just =Package Attrs;=. Then call it using:
<pre>
  use TWiki::Plugins::MyFirstPlugin::Attrs;
  $var = MyFirstPlugin::Attrs->new();
</pre>

#CreatePluginTopic
---+++ Writing the Documentation Topic

The Plugin documentation topic contains usage instructions and version details. It serves the Plugin files as %TWIKIWEB%.FileAttachments for downloading. (The doc topic is also included _in_ the [[#CreatePluginPackage][distribution package]].) To create a documentation topic:

	1. *Copy* the Plugin topic template from TWiki.org. To copy the text, go to TWiki:Plugins/PluginPackage and:
		* enter the Plugin name in the "How to Create a Plugin" section
		* click Create
		* select all in the Edit box &amp; copy 
		* Cancel the edit
		* go back to your site to the %TWIKIWEB% web
		* In the GoBox enter your Plugin name, for example =MyFirstPlugin=, press enter and create the new topic
		* paste &amp; save new Plugin topic on your site
	2. *Customize* your Plugin topic.
		* In case you plan to publish your Plugin at TWiki.org, use Interwiki names for author names, like TWiki:Main/%WIKINAME%. 
	3. *Save* your topic, for use in [[#CreatePluginPackage][packaging]] and [[#PublishPlugin][publishing]] your Plugin.

<blockquote style="background-color:#f5f5f5">
*OUTLINE: Doc Topic Contents* <br />
Check the Plugins web on TWiki.org for the latest Plugin doc topic template. Here's a quick overview of what's covered:

*Syntax Rules:* &lt;<i>Describe any special text formatting that will be rendered.</i>&gt;" 

*Example:* &lt;<i>Include an example of the Plugin in action. Possibly include a static HTML version of the example to compare if the installation was a success!</i>&gt;"

*Plugin Settings:* &lt;<i>Description and settings for custom Plugin %<nop>VARIABLES%, and those required by TWiki.</i>&gt;"

	* *Plugins Preferences* &lt;<i>If user settings are needed, explain... Entering values works exactly like %TWIKIWEB%.TWikiPreferences and WebPreferences: six (6) spaces and then:</i>&gt;"
		* *Set &lt;<i>EXAMPLE = value added</i>&gt;*

*Plugin Installation Instructions:* &lt;<i>Step-by-step set-up guide, user help, whatever it takes to install and run, goes here.</i>&gt;"

*Plugin Info:* &lt;<i>Version, credits, history, requirements - entered in a form, displayed as a table. Both are automatically generated when you create or edit a page in the TWiki:Plugins web.</i>&gt;"
</blockquote>

#CreatePluginPackage
---+++ Packaging for Distribution

A minimum Plugin release consists of a Perl module with a WikiName that ends in =Plugin=, ex: =MyFirstPlugin.pm=, and a documentation page with the same name(=MyFirstPlugin.txt=).

	1. Distribute the Plugin files in a directory structure that mirrors TWiki. If your Plugin uses additional files, include them ALL:
		* =lib/TWiki/Plugins/MyFirstPlugin.pm=
		* =data/TWiki/MyFirstPlugin.txt=
		* =pub/TWiki/MyFirstPlugin/uparrow.gif= [a required graphic]
	2. Create a zip archive with the Plugin name (=MyFirstPlugin.zip=) and add the entire directory structure from Step 1. The archive should look like this:
		* =lib/TWiki/Plugins/MyFirstPlugin.pm=
		* =data/TWiki/MyFirstPlugin.txt=
		* =pub/TWiki/MyFirstPlugin/uparrow.gif=

#PublishPlugin
---+++ Publishing for Public Use

You can release your tested, packaged Plugin to the TWiki community through the TWiki:Plugins web. All Plugins submitted to TWiki.org are available for download and further development in TWiki:Plugins/PluginPackage. Publish your Plugin in these steps:

	1. *Post* the Plugin documentation topic in the TWiki:Plugins/PluginPackage:
		* enter the Plugin name in the "How to Create a Plugin" section, for example =MyFirstPlugin=
		* paste in the topic text from [[#CreatePluginTopic][Creating Plugin Documentation]] and save
	1. *Attach* the distribution zip file to the topic, ex: =MyFirstPlugin.zip=
	1. *Link* from the doc page to a new, blank page named after the Plugin, and ending in =Dev=, ex: =MyFirstPluginDev=. This is the discussion page for future development. (User support for Plugins is handled in TWiki:Support.)
	1. *Put* the Plugin into the CVS repository, see TWiki:Plugins/ReadmeFirst (optional)

Thank you very much for sharing your Plugin with the TWiki community :-)

#RecommendedStorageOfPluginData
---++ Recommended Storage of Plugin Data

Plugins sometimes need to store data. This can be Plugin internal data like cache data, or generated data for the browser like images. The following is a recommendation where to store the data.

---+++ Where to store Plugin Internal Data

In case the Plugin generates data just for internal use, or data which is not specific to a topic, store it in the Plugin's attachment directory.

	* The Plugin's attachment directory is =pubdir/Installweb/FooBarPlugin=
		* =Installweb= refers to the name of the web where the Plugin is installed
	* The Plugin's attachment URL is =%<nop>PUBURL%/Installweb/FooBarPlugin=
	* The filename should start with an underscore, followed by an identifier, e.g. =_any_name.ext=
		* The leading underscore avoids a nameclash with files attached to the Plugin topic
		* Use only alphanumeric characters, underscores and periods to avoid platform dependency issues and URL issues
		* Do not use subdirectories (rename and delete would fail)
	* Use Plugin API functions documented in TWikiFuncModule to ensure portability:
		* Use =getPubDir()= to get the attachment root directory
		* Use =getUrlHost()= and =getPubUrlPath()= to build the URL in case you create content for the browser
		* Use =$installWeb= to get the name of the web where the Plugin is installed
		* Create the web directory and topic attachment directory if needed
	* Hint: Package the Plugin at least with one file attachment. This ensures that the attachment directory already exists

---+++ Where to Store Data for Topics using the Plugin

In case the Plugin generates data which is specific to a topic, store it in the topic's attachment directory.

	* The topic's attachment directory is =pubdir/Webname/TopicName=
	* The topic's attachment URL is =%<nop>PUBURL%/Webname/TopicName=
	* The filename should start with an underscore, followed by the Plugin name, an underscore and an identifier, e.g. =_FooBarPlugin_any_name.ext=
		* The leading underscore avoids a nameclash with files attached to the same topic
		* Use only alphanumeric characters, underscores and periods to avoid platform dependency issues and URL issues
		* Do not use subdirectories (rename and delete would fail)
	* Use Plugin API functions documented in TWiki.TWikiFuncModule to ensure portability:
		* Use =getPubDir()= to get the attachment root directory
		* Use =getUrlHost()= and =getPubUrlPath()= to build the URL in case you create content for the browser

Example code to build the file name:
<verbatim>
sub _make_filename
{
	 my ( $web, $topic, $name ) = @_;

	 # Create web directory "pub/$web" if needed
	 my $dir = TWiki::Func::getPubDir() . "/$web";
	 unless( -e "$dir" ) {
		  umask( 002 );
		  mkdir( $dir, 0775 );
	 }
	 # Create topic directory "pub/$web/$topic" if needed
	 $dir .= "/$topic";
	 unless( -e "$dir" ) {
		  umask( 002 );
		  mkdir( $dir, 0775 );
	 }
	 return "$dir/_FooBarPlugin_$name";
}
</verbatim>

-- TWiki:Main/PeterThoeny - 11 Dec 2003 <br />
-- TWiki:Main/AndreaSterbini - 29 May 2001 <br />
-- TWiki:Main/MikeMannix - 03 Dec 2001	
