Changeset 1311
- Timestamp:
- 12/13/08 13:43:55 (3 years ago)
- File:
-
- 1 edited
-
trunk/core/data/System/DevelopingPlugins.txt (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/core/data/System/DevelopingPlugins.txt
r1221 r1311 1 1 ---+ Developing Plugins 2 3 Foswiki has a large number of internal (perl code) interfaces. However in general it's a bad idea to use these interfaces to extend Foswiki, because that would result in your code breaking every time the core changes. 4 5 To address this problem Foswiki provides a number of Application Program Interfaces (APIs) that allow you to extend Foswiki in a robust way. 6 7 The usual way Foswiki is extended is by writing a _Plugin_. Plugins extend Foswiki by providing functions that 'listen' to events in the Foswiki core, and handling these events. These functions are called "Plugin Handlers" and they are described in depth in %SYSTEMWEB%.EmptyPlugin and =lib/Foswiki/Plugins/EmptyPlugin.pm=. 8 2 %TOC% 3 The usual way Foswiki is extended is by writing a _Plugin_. Plugins extend Foswiki by providing functions that 'listen' to events in the Foswiki core, and handling these events. These functions are called "Plugin Handlers" and they are described in depth in %SYSTEMWEB%.EmptyPlugin ( =lib/Foswiki/Plugins/EmptyPlugin.pm= ). 4 5 ---++ APIs available to Extensions 9 6 To be robust extensions must avoid using any unpublished functionality from the Foswiki core. The following perl packages give access to features for extension authors. These APIs are not just for Plugins, they can be used in any type of extension. Click on the name of the package to see the full documentation. 10 * =[[%SCRIPTURL{view}%/%SYSTEMWEB%/PerlDoc?module=Foswiki::Plugins::EmptyPlugin][Foswiki::Plugins::EmptyPlugin]]= - template plugin for you to use as a starting point for your own plugins.11 7 * =[[%SCRIPTURL{view}%/%SYSTEMWEB%/PerlDoc?module=Foswiki::Func][Foswiki::Func]]= - bridge to core functions. This is the package you will use most. 12 8 * =[[%SCRIPTURL{view}%/%SYSTEMWEB%/PerlDoc?module=Foswiki::Meta][Foswiki::Meta]]= - topic meta-data … … 15 11 * =[[%SCRIPTURL{view}%/%SYSTEMWEB%/PerlDoc?module=Foswiki::Attrs][Foswiki::Attrs]]= - parser and storage object for macro parameters 16 12 * =[[%SCRIPTURL{view}%/%SYSTEMWEB%/PerlDoc?module=Foswiki::Time][Foswiki::Time]]= - time parsing and formatting 13 * =[[%SCRIPTURL{view}%/%SYSTEMWEB%/PerlDoc?module=Foswiki::Sandbox][Foswiki::Sandbox]]= - safe server-side program execution 17 14 In addition the following global variables may be referred to: 18 15 * =$Foswiki::Plugins::VERSION= - plugin handler API version number 19 16 * =$Foswiki::Plugins::SESSION= - reference to =Foswiki= singleton object 20 17 * =$Foswiki::cfg= - reference to configuration hash 21 * =$Foswiki::regex - see 'Standard Regular Expressions', below18 * =$Foswiki::regex= - see [[#StReEx][Standard Regular Expressions]], below 22 19 * =$Foswiki::sandbox= - reference to the static sandbox object (type =Foswiki::Sandbox=), used for calling external programs. 23 20 %I% Foswiki:Development.GettingStarted is the starting point for more comprehensive documentation on developing for Foswiki. … … 28 25 use the standard initialisation sequence. 29 26 27 #StReEx 30 28 ---+++ Standard Regular Expressions 31 29 A number of standard regular expressions are available for use in extensions, in the =$Foswiki::regex= hash. these regular expressions are precompiled in an … … 62 60 | tagNameRegex | Standard macro names e.g. %<nop>THIS_BIT% (THIS_BIT only) | RE | 63 61 64 ---+++ Predefined Hooks for Plugins 65 66 Plugins 'listen' to events happening in the core by registering an interest in those events. They do this by declaring 'plugin handlers'. These are simply functions with a particular name that, if they exist in your plugin, will be called by the core. 67 68 Foswiki:Development.StepByStepRenderingOrder helps you decide which rendering handler to use. See [[EmptyPlugin]] for a full list of the handlers that are defined. 62 #CreatePlugins 63 ---++ Creating New Plugins 64 65 With a reasonable knowledge of the Perl scripting language, you can create new plugins or modify and extend existing ones. 66 67 ---+++ Anatomy of a Plugin 68 69 A (very) basic Foswiki plugin consists of two files: 70 71 * a Perl module, e.g. =lib/Foswiki/Plugins/MyFirstPlugin.pm= 72 * a documentation topic, e.g. =MyFirstPlugin.txt= 73 74 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. 75 76 The plugin API handles the details of connecting your Perl module with the Foswiki core. 77 78 _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_. 79 80 ---+++ Creating the Perl Module 81 82 Copy file =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. 83 84 #CreatePluginTopic 85 ---+++ Writing the Documentation Topic 86 87 The plugin documentation topic contains usage instructions and version details. (The doc topic is also included _in_ the [[#CreatePluginPackage][distribution package]].) To create a documentation topic: 88 89 1. *Copy* the plugin topic template from [[System.EmptyPlugin?raw=on][EmptyPlugin]] 90 1. *Customize* your plugin topic. 91 * Important: In case you plan to publish your plugin on Foswiki.org, use Interwiki names for author names and links to Foswiki.org topics, such as Foswiki:Main/%WIKINAME%. This is important because links should work properly in a plugin topic installed on any Foswiki, not just on Foswiki.org. 92 1. *Save* your topic, for use in [[#CreatePluginPackage][packaging]] and [[#PublishPlugin][publishing]] your plugin. 93 94 <blockquote style="background-color:#f5f5f5"> 95 *OUTLINE: Doc Topic Contents* <br /> 96 Check the plugins web on Foswiki.org for the latest plugin doc topic template. Here's a quick overview of what's covered: 97 98 *Syntax Rules:* <<i>Describe any special text formatting that will be rendered.</i>>" 99 100 *Example:* <<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>>" 101 102 *Plugin Settings:* <<i>Description and settings for custom plugin settings, and those required by Foswiki.</i>>" 103 104 * *Plugins Preferences* <<i>If user settings are needed, link to [[%SYSTEMWEB%.PreferenceSettings][preference settings]] and explain the role of the plugin name prefix</i> 105 106 *Plugin Installation Instructions:* <<i>Step-by-step set-up guide, user help, whatever it takes to install and run, goes here.</i>>" 107 108 *Plugin Info:* <<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 Foswiki:Extensions web.</i>> 109 </blockquote> 110 111 #CreatePluginPackage 112 ---+++ Packaging for Distribution 113 114 The Foswiki:Extensions.BuildContrib is a powerful build environment that is used by the Foswiki project to build Foswiki itself, as well as many of the plugins. You don't *have* to use it, but it is highly recommended! 115 116 If you don't want to (or can't) use the !BuildContrib, then 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=). 117 118 1. Distribute the plugin files in a directory structure that mirrors Foswiki. If your plugin uses additional files, include them all: 119 * =lib/Foswiki/Plugins/MyFirstPlugin.pm= 120 * =data/Foswiki/MyFirstPlugin.txt= 121 * =pub/Foswiki/MyFirstPlugin/uparrow.gif= [a required graphic] 122 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: 123 * =lib/Foswiki/Plugins/MyFirstPlugin.pm= 124 * =data/Foswiki/MyFirstPlugin.txt= 125 * =pub/Foswiki/MyFirstPlugin/uparrow.gif= 126 127 #PublishPlugin 128 ---+++ Publishing for Public Use 129 130 You can release your tested, packaged plugin to the Foswiki community through the Foswiki:Extensions web. All plugins submitted to Foswiki.org are available for public download and further development. 131 132 Publish your plugin by following these steps: 133 1. *Post* the plugin documentation topic to the Foswiki:Extensions web 134 1. *Attach* the distribution zip file(s) to the topic, eg: 135 =MyFirstPlugin.zip= 136 1. Add a user support hub by visiting Foswiki:Support.CreateNewSupportHub 137 1. Optionally, check in the sources to the Foswiki subversion repository 138 (see Foswiki:Development.HowToStartExtensionDevelopmentInSubversion) 139 140 %N% Once you have done the above steps once, you can use the !BuildContrib to upload updates to your plugin. 141 142 Thank you very much for sharing your plugin with the Foswiki community :-) 69 143 70 144 #FastPluginHints 71 ---++ +Hints on Writing Fast Plugins145 ---++ Hints on Writing Fast Plugins 72 146 73 147 * Delay initialization as late as possible. For example, if your plugin is a simple syntax processor, you might delay loading extra Perl modules until you actually see the syntax in the text. … … 79 153 * Use registered tag handlers 80 154 81 ---+++ Version Detection 82 83 To eliminate the incompatibility problems that are bound to arise from active open plugin development, a plugin versioning system is provided for automatic compatibility checking. 84 85 * All plugin packages require a =$VERSION= variable. This should be an integer, or a subversion version id. 86 * The =initPlugin= handler should check all dependencies and return 1 if the initialization is OK or 0 if something went wrong. 87 * The plugin initialization code does not register a plugin that returns 0 (or that has no =initPlugin= handler). 88 * =$Foswiki::Plugins::VERSION= in the =Foswiki::Plugins= module contains the Foswiki plugin API version, currently *%PLUGINVERSION{}%*. 89 * You can also use the =[[VarPLUGINVERSION][%<nop>PLUGINVERSION{}%]]= macro to query the plugin API version or the version of installed plugins. 90 91 ---+++ Security 155 ---++ Security 92 156 93 157 * Badly written plugins can open security holes in Foswiki. This is especially true if care isn't taken to prevent execution of arbitrary commands on the server. … … 99 163 * Avoid =eval=, and if you must use it make sure you sanitise parameters 100 164 * Always use the Foswiki::sandbox to execute commands. Never use backtick or qx//. 165 * Use =Foswiki::Func::checkAccessPermission= to check the access rights of the current user. 101 166 * Always audit the plugins you install, and make sure you are happy with the level of security provided. While every effort is made to monitor plugin authors activities, at the end of the day they are uncontrolled user contributions. 102 103 #CreatePlugins104 ---++ Creating Plugins105 106 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][Foswiki Plugin API]] provides the programming interface for Foswiki.107 108 ---+++ Anatomy of a Plugin109 110 A (very) basic Foswiki plugin consists of two files:111 112 * a Perl module, e.g. =MyFirstPlugin.pm=113 * a documentation topic, e.g. =MyFirstPlugin.txt=114 115 The Perl module can be a block of code that talks to with Foswiki alone, or it can include other elements, like other Perl modules (including other plugins), graphics, Foswiki templates, external applications (ex: a Java applet), or just about anything else it can call.116 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/Foswiki/Plugins/MyFirstPlugin/= directory.117 118 The plugin API handles the details of connecting your Perl module with main Foswiki code. When you're familiar with the [[#PluginAPI][Plugin API]], you're ready to develop plugins.119 120 _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_.121 122 ---+++ Creating the Perl Module123 124 Copy file =lib/Foswiki/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 [[#PluginAPI][Plugin API]] specs for more information.125 126 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:127 <pre>128 use Foswiki::Plugins::MyFirstPlugin::Attrs;129 $var = MyFirstPlugin::Attrs->new();130 </pre>131 132 #CreatePluginTopic133 ---+++ Writing the Documentation Topic134 135 The plugin documentation topic contains usage instructions and version details. It serves the plugin files as %SYSTEMWEB%.FileAttachments for downloading. (The doc topic is also included _in_ the [[#CreatePluginPackage][distribution package]].) To create a documentation topic:136 137 1. *Copy* the plugin topic template from Foswiki.org. To copy the text, go to Foswiki:Plugins/PluginPackage and:138 * enter the plugin name in the "How to Create a Plugin" section139 * click Create140 * select all in the Edit box & copy141 * Cancel the edit142 * go back to your site to the %SYSTEMWEB% web143 * In the GoBox enter your plugin name, for example =MyFirstPlugin=, press enter and create the new topic144 * paste & save new plugin topic on your site145 1. *Customize* your plugin topic.146 * Important: In case you plan to publish your plugin on Foswiki.org, use Interwiki names for author names and links to Foswiki.org topics, such as Foswiki:Main/%WIKINAME%. This is important because links should work properly in a plugin topic installed on any Foswiki, not just on Foswiki.org.147 1. *Save* your topic, for use in [[#CreatePluginPackage][packaging]] and [[#PublishPlugin][publishing]] your plugin.148 149 <blockquote style="background-color:#f5f5f5">150 *OUTLINE: Doc Topic Contents* <br />151 Check the plugins web on Foswiki.org for the latest plugin doc topic template. Here's a quick overview of what's covered:152 153 *Syntax Rules:* <<i>Describe any special text formatting that will be rendered.</i>>"154 155 *Example:* <<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>>"156 157 *Plugin Settings:* <<i>Description and settings for custom plugin settings, and those required by Foswiki.</i>>"158 159 * *Plugins Preferences* <<i>If user settings are needed, link to [[%SYSTEMWEB%.PreferenceSettings][preference settings]] and explain the role of the plugin name prefix</i>160 161 *Plugin Installation Instructions:* <<i>Step-by-step set-up guide, user help, whatever it takes to install and run, goes here.</i>>"162 163 *Plugin Info:* <<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 Foswiki:Plugins web.</i>>164 </blockquote>165 166 #CreatePluginPackage167 ---+++ Packaging for Distribution168 169 The Foswiki:Extensions.BuildContrib is a powerful build environment that is used by the Foswiki project to build Foswiki itself, as well as many of the plugins. You don't *have* to use it, but it is highly recommended!170 171 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 in =Plugin=, ex: =MyFirstPlugin.pm=, and a documentation page with the same name(=MyFirstPlugin.txt=).172 173 1. Distribute the plugin files in a directory structure that mirrors Foswiki. If your plugin uses additional files, include them all:174 * =lib/Foswiki/Plugins/MyFirstPlugin.pm=175 * =data/Foswiki/MyFirstPlugin.txt=176 * =pub/Foswiki/MyFirstPlugin/uparrow.gif= [a required graphic]177 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:178 * =lib/Foswiki/Plugins/MyFirstPlugin.pm=179 * =data/Foswiki/MyFirstPlugin.txt=180 * =pub/Foswiki/MyFirstPlugin/uparrow.gif=181 182 #PublishPlugin183 ---+++ Publishing for Public Use184 185 You can release your tested, packaged plugin to the Foswiki community through the Foswiki:Plugins web. All plugins submitted to Foswiki.org are available for download and further development in Foswiki:Plugins/PluginPackage.186 187 Publish your plugin by following these steps:188 1. *Post* the plugin documentation topic in the Foswiki:Plugins/PluginPackage:189 * enter the plugin name in the "How to Create a Plugin" section, for example =MyFirstPlugin=190 * paste in the topic text from [[#CreatePluginTopic][Writing the Documentation Topic]] and save191 1. *Attach* the distribution zip file to the topic, ex: =MyFirstPlugin.zip=192 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 Foswiki:Support.)193 1. *Put* the plugin into the SVN repository, see Foswiki:Plugins/ReadmeFirst (optional)194 195 %N% Once you have done the above steps once, you can use the !BuildContrib to upload updates to your plugin.196 197 Thank you very much for sharing your plugin with the Foswiki community :-)198 167 199 168 #RecommendedStorageOfPluginData … … 207 176 208 177 ---+++ Web Accessible Data 178 179 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. 209 180 210 181 __Topic-specific data__ such as generated images can be stored in the topic's attachment area, which is web accessible. Use the =Foswiki::Func::saveAttachment()= function to store the data. … … 215 186 * Use only alphanumeric characters, underscores, dashes and periods to avoid platform dependency issues and URL issues 216 187 * Example: =_GaugePlugin_img123.gif= 217 218 __Web specific data__ can be stored in the plugin's attachment area, which is web accessible. Use the =Foswiki::Func::saveAttachment()= function to store the data. 219 220 Recommendation for file names in plugin attachment area: 221 * Prefix the filename with an underscore 222 * Include the name of the web in the filename 223 * Use only alphanumeric characters, underscores, dashes and periods to avoid platform dependency issues and URL issues 224 * Example: =_Main_roundedge-ul.gif= 188 Such auto-generated attachments han be hidden from users by setting the 'h' attribute in the attachment attributes. 189 190 __Web specific data__ should be stored in the attachment area of a topic in the web that you specify for the purpose, e.g. Web.<nop>BathPlugPictures. Use the =Foswiki::Func::saveAttachment()= function to store the data in this topic. 225 191 226 192 #ConfigSpec … … 258 224 </verbatim> 259 225 260 The config.specfile is read by configure, and =configure= then writes =LocalSite.cfg= with the values chosen by the local site admin.226 The =Config.spec= file is read by configure, and =configure= then writes =LocalSite.cfg= with the values chosen by the local site admin. 261 227 262 228 A range of types are available for use in =Config.spec= files: 263 229 264 | BOOLEAN| A true/false value, represented as a checkbox |265 | COMMAND_length_ | A shell command |266 | LANGUAGE| A language (selected from ={LocalesDir}= |267 | NUMBER| A number |268 | OCTAL| An octal number |269 | PASSWORD_length_ | A password (input is hidden) |270 | PATH_length_ | A file path |271 | PERL | A perl structure, consisting of arrays and hashes |272 | REGEX_length_ | A perl regular expression |273 | SELECT_choices_ | Pick one of a range of choices |274 | SELECTCLASS _root_ | Select a perl package (class)|275 | STRING_length_ | A string |276 | URL_length_ | A url |277 | URLPATH_length_ | A relative URL path |230 | =BOOLEAN= | A true/false value, represented as a checkbox | 231 | =COMMAND= _length_ | A shell command | 232 | =LANGUAGE= | A language (selected from ={LocalesDir}= | 233 | =NUMBER= | A number | 234 | =OCTAL= | An octal number | 235 | =PASSWORD= _length_ | A password (input is hidden) | 236 | =PATH= _length_ | A file path | 237 | =PERL= | A simplified perl data structure, consisting of arrays, hashes and scalar values | 238 | =REGEX= _length_ | A perl regular expression | 239 | =SELECT= _choices_ | Pick one of a range of choices | 240 | <code>SELECTCLASS</code> <em>package-specifier</em> | 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. | 241 | =STRING= _length_ | A string | 242 | =URL= _length_ | A url | 243 | =URLPATH= _length_ | A relative URL path | 278 244 279 245 All types can be followed by a comma-separated list of _attributes_. 280 | EXPERT| means this an expert option |281 | M| means the setting is mandatory (may not be empty) |282 | H| means the option is not visible in =configure= |246 | =EXPERT= | means this an expert option | 247 | =M= | means the setting is mandatory (may not be empty) | 248 | =H= | means the option is not visible in =configure= | 283 249 284 250 See =lib/Foswiki.spec= for many more examples. 285 251 286 252 =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. 287 288 Foswiki:Foswiki/SpecifyingConfigurationItemsForExtensions has supplemental documentation on configure settings.289 253 290 254 #MaintainPlugins … … 293 257 ---+++ Discussions and Feedback on Plugins 294 258 295 Each published plugin has a plugin development topic on Foswiki.org. Plugin development topics are named after your plugin and end in =Dev=, such as =MyFirstPluginDev=. The plugin development topic is a great resource to discuss feature enhancements and to get feedback from the Foswiki community.259 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. 296 260 297 261 ---+++ Maintaining Compatibility with Earlier Foswiki Versions … … 328 292 </verbatim> 329 293 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. 294 295 ---+++ TWiki<sup>®</sup> Plugins 296 Most plugins written for TWiki can also be run in Foswiki, by installing the !TWikiCompatibilityPlugin. See Foswiki:Extensions.TWikiCompatibilityPlugin for more information.
Note: See TracChangeset
for help on using the changeset viewer.
