Change book | Close bookmaker |
lib/Foswiki/Plugins/EmptyPlugin.pm
).
bin
directory. Each of the scripts has a specific function, as described in CommandAndCGIScripts.
The scripts are responsible for interpreting the parameters passed in the request, and generating a response that is sent back to the browser, usually in the form of an HTML page.
Foswiki contains three engines that are used by the scripts; the template engine, the macro engine, and the TML engine.Once all the engines have run, the output is sent to the browser.
There are several ways plugins can interact with this process.rest
script to support some form of transaction outside those supported by the standard scripts.
Foswiki::Func
- this is the package you will use most. This package exposes a lot of core functionality in a way that is friendly to extension writers. If you find that there are two ways of doing something - a Foswiki::Func
way, and another call to one of the packages below, then the Foswiki::Func
way is almost always the right way.
Foswiki::Meta
- topic and web meta-data. Certain Foswiki::Func
methods, and some plugin handlers, are passed (or return) objects of this type. Almost all of the methods of Foswiki::Meta
have analagous methods in Foswiki::Func
- in general you should call the Foswiki::Func
methods in preference to calling Foswiki::Meta
methods directly.
Foswiki::OopsException
- special exception for invoking the 'oops' script
Foswiki::AccessControlException
- access control exception
Foswiki::Attrs
- parser and storage object for macro parameters
Foswiki::Time
- time parsing and formatting
Foswiki::Sandbox
- safe server-side program execution, used for calling external programs.
Foswiki::Iterator
specification Foswiki::ListIterator
- utility class for iterator objects that iterate over list contents
Foswiki::LineIterator
- utility class for iterator objects that iterate over lines in a block of text
Foswiki::AggregateIterator
- utility class for iterator objects that aggregate other iterators into a single iteration
$Foswiki::Plugins::VERSION
- plugin handler API version number
$Foswiki::Plugins::SESSION
- reference to Foswiki
singleton object
$Foswiki::cfg
- reference to configuration hash
$Foswiki::regex
- see Standard Regular Expressions, below
Foswiki
singleton object having been created before the APIs can be used.
This will only be a problem if you are writing an extension that doesn't
use the standard initialisation sequence.
$Foswiki::regex
hash. these regular expressions are precompiled in an
I18N-compatible manner. The
following are guaranteed to be present. Others may exist, but their use
is unsupported and they may be removed in future Foswiki versions.
In the table below, the expression marked type 'String' are intended for
use within character classes (i.e. for use within square brackets inside
a regular expression), for example:
my $isCapitalizedWord = ( $s =~ /[$Foswiki::regex{upperAlpha}][$Foswiki::regex{mixedAlpha}]+/ );Those expressions marked type 'RE' are precompiled regular expressions that can be used outside square brackets. For example:
my $isWebName = ( $s =~ m/$Foswiki::regex{webNameRegex}/ );
Name | Matches | Type |
---|---|---|
wikiWordRegex | WikiWords | RE |
webNameRegex | User web names | RE |
topicNameRegex | Topic names | RE |
anchorRegex | #AnchorNames | RE |
abbrevRegex | Abbreviations/Acronyms e.g. GOV, IRS | RE |
emailAddrRegex | email@address.com | RE |
tagNameRegex | Standard macro names e.g. %THIS_BIT% (THIS_BIT only) | RE |
upperAlpha | Upper case characters | String |
upperAlphaNum | Upper case characters and digits | String |
lowerAlpha | Lower case characters | String |
lowerAlphaNum | Lower case characters and digits | String |
numeric | Digits | String |
mixedAlpha | Alphabetic characters | String |
mixedAlphaNum | Alphanumeric characters | String |
With a reasonable knowledge of the Perl scripting language, you can create new plugins or modify and extend existing ones.
A (very) basic Foswiki plugin consists of two files:
lib/Foswiki/Plugins/MyFirstPlugin.pm
MyFirstPlugin.txt
The Perl module can invoke other, non-Foswiki, elements, like other Perl modules (including other plugins), graphics, external applications, or just about anything else that Perl can call.
The plugin API handles the details of connecting your Perl module with the Foswiki core.
The Foswiki:Extensions.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.lib/Foswiki/Plugins/EmptyPlugin.pm
to <name>Plugin.pm
. The EmptyPlugin does nothing, but it contains all the information you need to create you own custom plugin.
Plugin
, ex: MyFirstPlugin.pm
, and a documentation page with the same name(MyFirstPlugin.txt
).
lib/Foswiki/Plugins/MyFirstPlugin.pm
data/Foswiki/MyFirstPlugin.txt
pub/Foswiki/MyFirstPlugin/uparrow.gif
[a required graphic]
MyFirstPlugin.zip
) and add the entire directory structure from Step 1. The archive should look like this: lib/Foswiki/Plugins/MyFirstPlugin.pm
data/Foswiki/MyFirstPlugin.txt
pub/Foswiki/MyFirstPlugin/uparrow.gif
MyFirstPlugin.zip
eval
block like this:eval { require IPC::Run }
return "<font color=\"red\">SamplePlugin: Can't load required modules ($@)</font>" if $@;
lib/Foswiki/Plugins/BathPlugin/
.
$NO_PREFS_IN_TOPIC
if you possibly can, as that will stop Foswiki from reading the plugin topic for every page. Use Config.spec instead.
%Foswiki::cfg
hash for configuration options. Don't ask installers to edit topics in the System web. configure
describes the steps
eval
, and if you must use it make sure you sanitise parameters
Foswiki::Func::checkAccessPermission
to check the access rights of the current user.
Foswiki::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, Foswiki::Func::storeFile()
and Foswiki::Func::readFile()
are provided to persistently store and retrieve simple data in this area.
The internal data area is not normally made web-accessible for security reasons. If yoou want to store web accessible data, for example generated images, then you should use Foswiki's attachment mechanisms.
Topic-specific data such as generated images can be stored in the topic's attachment area, which is web accessible. Use theFoswiki::Func::saveAttachment()
function to store the data.
Recommendation for file name: _GaugePlugin_img123.gif
Foswiki::Func::saveAttachment()
function to store the data in this topic.
configure
configure
rather than trying to use preference settings. These extensions use Config.spec
files to publish their configuration requirements.
Config.spec
files are read during configuration. Once a Config.spec
has defined a configuration item, it is available for edit through the standard configure
interface. Config.spec
files are stored in the 'plugin directory' e.g. lib/Foswiki/Plugins/BathPlugin/Config.spec
.
Config.spec
elements $Foswiki::cfg
hash:
Location | Example | Notes |
---|---|---|
Under the {Extensions} namespace |
$Foswiki::cfg{Extensions}{BathPlugin}{PlugType} |
This is the recommended location, It is recommended for all new extensions, and strongly recommended for non-Plugin type extensions (Contribs, Skins, etc). |
Under the {Plugins} namespace |
$Foswiki::cfg{Plugins}{BathPlugin}{PlugType} |
This is traditionally where foswiki organizes all plugin settings. Foswiki automatically populates two settings for Plugins (and only plugins):
Config.spec file.We no longer recommend using this namespace for custom settings. |
Under the root namespace | $Foswiki::cfg{BathPlugin} |
Not Recommended Historical extensions place settings under the root, it results in a very cluttered configuration. |
{Extensions}
, even if it has a Plugin component.
{Plugins}
space but this is no longer recommended.
$Foswiki::cfg{Plugins}{BathPlugin} = "Take a bath here"; $Foswiki::cfg{Plugins}{BathPlugin}{PlugType} = "rubber stopper";
Config.spec
file Config.spec
file. For full technical details, see How To Write A .spec File. However the information given here should be sufficient for most extension developers.
The Config.spec
file for a plugin starts with a line that declares what section the configuration should appear in. The standard for all extensions is:
# ---+ ExtensionsNext we have a sub-heading for the configuration specific to this extension, and the actual configuration options:
# ----++ BathPlugin # This plugin senses the level of water in your bath, and ensures the plug # is not removed while the water is still warm.This is followed by one or more configuration items. Each configuration item has a type, a description and a default. For example:
# **SELECT Plastic,Rubber,Metal** # Select the plug type $Foswiki::cfg{Plugins}{BathPlugin}{PlugType} = 'Plastic'; # **NUMBER** # Enter the chain length in cm $Foswiki::cfg{Plugins}{BathPlugin}{ChainLength} = '30'; # **BOOLEAN EXPERT** # Turn this option off to disable the water temperature alarm $Foswiki::cfg{BathPlugin}{TempSensorEnabled} = '1';The type (e.g.
**SELECT**
) tells configure
to how to prompt for the value. It also tells configure how to do some basic checking on the value you actually enter. All the comments between the type and the configuration item are taken as part of the description. The configuration item itself defines the default value for the configuration item. The above spec defines the configuration items $Foswiki::cfg{BathPlugin}{PlugType}
, $Foswiki::cfg{BathPlugin}{ChainLength}
, and $Foswiki::cfg{BathPlugin}{TempSensorEnabled}
for use in your plugin. For example,
if( $Foswiki::cfg{Plugins}{BathPlugin}{TempSensorEnabled} && $curTemperature > 50 ) { die "The bathwater is too hot for comfort"; }You can use other
$Foswiki::cfg
values in other settings,
but you must be sure they are only evaluated under program control, and
not when this file is parsed by perl. For example:
$Foswiki::cfg{Plugins}{BathPlugin}{MyBath} = "$Foswiki::cfg{PubDir}/enamel.gif"; # BAD # Perl will interpolate variables in double-quotes, so $Foswiki::cfg{PubDir} # will be evaluated at configuration time, which will make reconfiguration # difficult. $Foswiki::cfg{Plugins}{BathPlugin}{MyBath} = '$Foswiki::cfg{PubDir}/enamel.gif'; # GOOD # The single quotes make sure $Foswiki::cfg{PubDir} will only be evaluated # at run-time.The
Config.spec
file is read by configure, and configure
then writes LocalSite.cfg
with the values chosen by the local site admin.
A range of types are available for use in Config.spec
files:
BOOLEAN |
A true/false value, represented as a checkbox |
BOOLGROUP choices |
A group of related checkboxes to toggle on/off from a list of choices. Stored as a simple list, BOOLGROUP fee,fie,foe,fum selected items stored as 'fee,fie,foe' |
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 simplified perl data structure, consisting of arrays, hashes and scalar values |
REGEX length |
A perl regular expression |
SELECT choices |
Pick one of a range of choices |
SELECTCLASS package-specifier |
Select a perl package (class) e.g. SELECTCLASS Foswiki::Plugins::BathPlugin::*Plug lets the user select between all packages with names ending in Plug , Foswiki::Plugins::BathPlugin::RubberPlug , Foswiki::Plugins::BathPlugin::BrassPlug etc. |
STRING length |
A string |
URL length |
A url |
URLPATH length |
A relative URL path |
DISPLAY_IF |
means this option is conditionally displayed based on some Javascript expression (see below for more) |
ENABLE_IF |
means this option is conditionally enabled based on some Javascript expression (see below for more) |
EXPERT |
means this an expert option |
HIDDEN |
means the option is not visible in configure |
MANDATORY |
means the setting is mandatory (may not be empty) |
MULTIPLE |
Allow multiple select (SELECT type) |
SPELLCHECK |
Enable to request spellchecking in the UI (if available) |
80 |
means "use an 80 column one-line text field" - if the UI wants to support it. |
5x80 |
means "use a 5 row, 80 column textarea". Can be used with any text entry field type, such as STRING, COMMAND, PERL etc. However the UI may not support it. |
NO
to invert the meaning e.g. NOHIDDEN
(however since this is the default state for these options, it's of limited use!)
As of Foswiki 2.0 you can also use the ENHANCE
type to enhance the documentation for an already-existing $Foswiki::cfg
value. For example:
# **ENHANCE {Store}{Implementation}** # Documentation for a new store implementation here
ENHANCE
does not have a corresponding $Foswiki::cfg
value, just a documentation block.
See lib/Foswiki.spec
for many more examples, and How To Write A .spec File for more information about the syntax and attributes.
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.
# **BOOLEAN DISPLAY_IF {Other}{Option}=='RequiredValue'**You can use almost any Javascript condition; any string of the form
{Other}{Option}
will be expanded to the value of that option in the browser. ENABLE_IF
works the same way, except it simply disables the option unless the condition is satisified. The Javascript condition can be terminated by /DISPLAY_IF
/ /ENABLE_IF
if it is not the last option. For example,
# **BOOLEAN DISPLAY_IF {An}{Option}=='RequiredValue' /DISPLAY_IF ENABLE_IF {Another}{Option}=='AnotherValue' **When testing BOOLEAN options, simply stating the name of the option is enough. For example,
**STRING DISPLAY_IF {This}{BOOLEAN}{Option} **
---+ Extensions ---++ BathPlugin $Foswiki::cfg{Plugins}{BathPlugin}{PlugType} = 'rubber'; $Foswiki::cfg{Extensions}{BathPlugin}{ToyType} = 'duckie'; $Foswiki::cfg{foobar} = 'some archaic root-level setting';
configure
(As of Foswiki 2.0, it is no longer possible to directly link into Configure tabs.
Usually published plugins have a support hub in the Support web on Foswiki.org.Support hubs have links to where to discuss feature enhancements and give feedback to the developer and user communities.
Organizations typically do not upgrade to the latest Foswiki for many months. However, many administrators still would like to install the latest versions of a plugin on their older Foswiki installation. This need is fulfilled if plugins are maintained in a compatible manner.
if( $Foswiki::Plugins::VERSION >= 1.1 ) { @webs = Foswiki::Func::getListOfWebs( 'user,public' ); } else { @webs = Foswiki::Func::getPublicWebList( ); }
When a plugin defines deprecated handlers, a warning will be shown in the list generated by %FAILEDPLUGINS%. Admins who see these warnings should check Foswiki.org and if necessary, contact the plugin author, for an updated version of the plugin.
Updated plugins may still need to define deprecated handlers for compatibility with old Foswiki versions. In this case, the plugin package that defines old handlers can suppress the warnings in %FAILEDPLUGINS%.
This is done by defining a map from the handler name to theFoswiki::Plugins
version in which the handler was first deprecated. For example, if we need to define the endRenderingHandler
for compatibility with Foswiki::Plugins
versions before 1.1, we would add this to the plugin:
package Foswiki::Plugins::SinkPlugin; use vars qw( %FoswikiCompatibility ); $FoswikiCompatibility{endRenderingHandler} = 1.1;If the currently-running Foswiki version is 1.1 or later, then the handler will not be called and the warning will not be issued. Foswiki with versions of
Foswiki::Plugins
before 1.1 will still call the handler as required.