The TWiki Plugin APIThe Application Programming Interface (API) for TWiki Plugins provides the specifications for hooking into the core TWiki code from your external Perl Plugin module.Available Core FunctionsThe TWikiFuncDotPm module (lib/TWiki/Func.pm) describes all the interfaces available to Plugins. Plugins should only use the interfaces described in this module.
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 HooksIn addition to TWiki core functions, Plugins can use predefined hooks, or callbacks, as described in thelib/TWiki/Plugins/EmptyPlugin.pm module.
Hints on Writing Fast Plugins
Version DetectionTo eliminate the incompatibility problems that are bound to arise from active open Plugin development, a Plugin versioning system is provided for automatic compatibility checking.
Security
Creating PluginsWith 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 TWiki Plugin API Plugins by providing a programming interface for TWiki.Anatomy of a PluginA (very) basic TWiki Plugin consists of two files:
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 Plugin API, you're ready to develop Plugins.
The TWiki::Plugins.BuildContrib module provides a lot of support for plugins development, including a plugin creator, automatic publishing support, and automatic installation script writer. If you plan on writing more than one plugin, you probably need it.
Creating the Perl ModuleCopy filelib/TWiki/Plugins/EmptyPlugin.pm to <name>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 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 MyFirstPlugin::Attrs; instead of just Package Attrs;. Then call it using:
use TWiki::Plugins::MyFirstPlugin::Attrs; $var = MyFirstPlugin::Attrs->new(); Writing the Documentation TopicThe Plugin documentation topic contains usage instructions and version details. It serves the Plugin files as FileAttachments for downloading. (The doc topic is also included in the distribution package.) To create a documentation topic:
OUTLINE: Doc Topic Contents Packaging for DistributionThe TWiki:Plugins.BuildContrib is a powerful build environment that is used by the TWiki project to build TWiki itself, as well as many of the Plugins. You don't have to use it, but it is highly recommended! If you don't want (or can't) use the BuildContrib, then a minimum Plugin release consists of a Perl module with a WikiName that ends inPlugin, ex: MyFirstPlugin.pm, and a documentation page with the same name(MyFirstPlugin.txt).
Measuring and Improving the Plugin PerformanceA high quality Plugin performs well. You can use the TWiki:Plugins.PluginBenchmarkAddOn to measure your TWiki:Plugins.PluginBenchmarks. The data is needed as part of the Documentation Topic. See also Hints on Writing Fast Plugins.Publishing for Public UseYou 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 by following these steps:
Recommended Storage of Plugin Specific DataPlugins sometimes need to store data. This can be Plugin internal data such as cache data, or data generated for browser consumption such as images. Plugins should store data using TWikiFuncDotPm functions that support saving and loading of topics and attachments.Plugin Internal DataYou can create a Plugin "work area" using theTWiki::Func::getWorkArea() function, which gives you a persistent directory where you can store data files. By default they will not be web accessible. The directory is guaranteed to exist, and to be writable by the webserver user. For convenience, TWiki::Func::storeFile() and TWiki::Func::readFile() are provided to persistently store and retrieve simple data in this area.
Web Accessible DataTopic-specific data such as generated images can be stored in the topic's attachment area, which is web accessible. Use theTWiki::Func::saveAttachment() function to store the data.
Recommendation for file name:
TWiki::Func::saveAttachment() function to store the data.
Recommendation for file names in Plugin attachment area:
Integrating with
Some TWiki extensions have setup requirements that are best integrated into |
| BOOLEAN | A true/false value, represented as a checkbox |
| COMMAND length | A shell command |
| LANGUAGE | A language (selected from {LocalesDir} |
| NUMBER | A number |
| OCTAL | An octal number |
| PASSWORD length | A password (input is hidden) |
| PATH length | A file path |
| PERL | A perl structure, consisting of arrays and hashes |
| REGEX length | A perl regular expression |
| SELECT choices | Pick one of a range of choices |
| SELECTCLASS root | Select a perl package (class) |
| STRING length | A string |
| URL length | A url |
| URLPATH length | A relative URL path |
| EXPERT | means this an expert option |
| M | means the setting is mandatory (may not be empty) |
| H | means the option is not visible in configure |
lib/TWiki.spec for many more examples.
Config.spec files are also used for other (non-plugin) extensions. in this case they are stored under the Contrib directory instead of the Plugins directory.
Dev, such as MyFirstPluginDev. The Plugin development topic is a great resource to discuss feature enhancements and to get feedback from the TWiki community.
TWiki::Plugins version in which the handler was first deprecated. For example, if we need to define the endRenderingHandler for compatibility with TWiki::Plugins versions before 1.1, we would add this to the plugin:
package TWiki::Plugins::SinkPlugin;
use vars qw( %TWikiCompatibility );
$TWikiCompatibility{endRenderingHandler} = 1.1;
If the currently-running TWiki version is 1.1 or later, then the handler will not be called and the warning will not be issued. TWiki with versions of TWiki::Plugins before 1.1 will still call the handler as required.
Related Topics: DeveloperDocumentationCategory, AdminDocumentationCategory, TWiki:TWiki.TWikiPluginsSupplement