Package =TWiki::Func<-- STARTINCLUDE required for huge TWikiDocumentation topic -->Official list of stable TWiki functions for Plugin developers This module defines official functions that Plugins can use to interact with the TWiki engine and content. Refer to EmptyPlugin and lib/TWiki/Plugins/EmptyPlugin.pm for a template Plugin and documentation on how to write a Plugin. Plugins should only use functions published in this module. If you use functions in other TWiki libraries you might create a security hole and you will probably need to change your Plugin when you upgrade TWiki. Deprecated functions will still work in older code, though they should not be called in new Plugins and should be replaced in older Plugins as soon as possible. The version of the TWiki::Func module is defined by the VERSION number of the TWiki::Plugins module, currently 6.10. This can be shown by the %PLUGINVERSION% variable. The 'Since' field in the function
documentation refers to the VERSION number and the date that the function
was addded.
Note: Beware! These methods should only ever be called
from the context of a TWiki Plugin. They require a Plugins SESSION context to be
established before they are called, and will not work if simply called from
another TWiki module. For example,
use TWiki; print TWiki::Func::getSkin(),"\n";will fail with Can't call method "getSkin" on an undefined value at TWiki/Func.pm line 83 .
If you want to call the methods outside the context of a plugin, you can create a Plugins SESSION object. For example,
the script:
use TWiki: $TWiki::Plugins::SESSION = new TWiki(); print TWiki::Func::getSkin(),"\n";will work happily. On this page:
EnvironmentgetSkin( ) -> $skinGet the skin path, set by theSKIN and COVER preferences variables or the skin and cover CGI parameters
Return: $skin Comma-separated list of skins, e.g. 'gnu,tartan' . Empty string if none.
Since: TWiki::Plugins::VERSION 1.000 (29 Jul 2001)
getUrlHost( ) -> $hostGet protocol, domain and optional port of script URL Return:$host URL host, e.g. "http://example.com:80"
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getScriptUrl( $web, $topic, $script, ... ) -> $urlCompose fully qualified URL
$url URL, e.g. "http://example.com:80/cgi-bin/view.pl/Main/WebNotify"
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getViewUrl( $web, $topic ) -> $urlCompose fully qualified view URL
$url URL, e.g. "http://example.com:80/cgi-bin/view.pl/Main/WebNotify"
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getOopsUrl( $web, $topic, $template, $param1, $param2, $param3, $param4 ) -> $urlCompose fully qualified 'oops' dialog URL
$url URL, e.g. "http://example.com:80/cgi-bin/oops.pl/ Main/WebNotify?template=oopslocked¶m1=joe"
This might be used like this:
my $url = TWiki::Func::getOopsUrl($web, $topic, 'oopsmistake', 'I made a boo-boo'); TWiki::Func::redirectCgiQuery( undef, $url ); return 0;Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002) Since TWiki::Plugins::VERSION 1.1, the recommended approach is to throw an oops exception. use Error qw( :try ); throw TWiki::OopsException($web, $topic, undef, 0, [ 'I made a boo-boo' ]);and let TWiki handle the cleanup. getPubUrlPath( ) -> $pathGet pub URL path Return:$path URL path of pub directory, e.g. "/pub"
Since: TWiki::Plugins::VERSION 1.000 (14 Jul 2001)
getCgiQuery( ) -> $queryGet CGI query object. Important: Plugins cannot assume that scripts run under CGI, Plugins must always test if the CGI query object is set Return:$query CGI query object; or 0 if script is called as a shell script
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getSessionValue( $key ) -> $valueGet a session value from the client session module
$value Value associated with key; empty string if not set
Since: TWiki::Plugins::VERSION 1.000 (27 Feb 200)
setSessionValue( $key, $value ) -> $booleanSet a session value via the client session module
clearSessionValue( $key ) -> $booleanClear a session value via the client session module
getContext() -> \%hashGet a hash of context identifiers representing the currently active context. The context is a set of identifiers that are set during specific phases of TWiki processing. For example, each of the standard scripts in the 'bin' directory each has a context identifier - the view script has 'view', the edit script has 'edit' etc. So you can easily tell what 'type' of script your Plugin is being called within. The core context identifiers are listed in the TWikiTemplates topic. Please be careful not to overwrite any of these identifiers! Context identifiers can be used to communicate between Plugins, and between Plugins and templates. For example, in FirstPlugin.pm, you might write:sub initPlugin { TWiki::Func::getContext()->{'MyID'} = 1; ...This can be used in SecondPlugin.pm like this: sub initPlugin { if( TWiki::Func::getContext()->{'MyID'} ) { ... } ...or in a template, like this: %TMPL:DEF{"ON"}% Not off %TMPL:END% %TMPL:DEF{"OFF"}% Not on %TMPL:END% %TMPL:P{context="MyID" then="ON" else="OFF"}%or in a topic: %IF{"context MyID" then="MyID is ON" else="MyID is OFF"}%Note: all plugins have an automatically generated context identifier if they are installed and initialised. For example, if the FirstPlugin is working, the context ID 'FirstPlugin' will be set. Since: TWiki::Plugins::VERSION 1.1 PreferencesgetPreferencesValue( $key, $web ) -> $valueGet a preferences value from TWiki or from a Plugin
$value Preferences value; empty string if not set
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getPluginPreferencesValue( $key ) -> $valueGet a preferences value from your Plugin
$value Preferences value; empty string if not set
Note: This function will will only work when called from the Plugin.pm file itself. it will not work if called from a sub-package (e.g. TWiki::Plugins::MyPlugin::MyModule)
Since: TWiki::Plugins::VERSION 1.021 (27 Mar 2004)
getPreferencesFlag( $key, $web ) -> $valueGet a preferences flag from TWiki or from a Plugin
$value Preferences flag '1' (if set), or "0" (for preferences values "off" , "no" and "0" )
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getPluginPreferencesFlag( $key ) -> $booleanGet a preferences flag from your Plugin
"off" , "no" and "0" , or values not set at all. True otherwise.
Note: This function will will only work when called from the Plugin.pm file itself. it will not work if called from a sub-package (e.g. TWiki::Plugins::MyPlugin::MyModule)
Since: TWiki::Plugins::VERSION 1.021 (27 Mar 2004)
getWikiToolName( ) -> $nameGet toolname as defined in TWiki.cfg Return:$name Name of tool, e.g. 'TWiki' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Deleted: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | Synonymous with TWiki::Func::getPreferencesValue('WIKITOOLNAME'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Since: TWiki::Plugins::VERSION 1.000 (27 Feb 2001)
getMainWebname( ) -> $nameGet name of Main web as defined in TWiki.cfg Return:$name Name, e.g. 'Main' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Deleted: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | Synonymous with TWiki::Func::getPreferencesValue('MAINWEB'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Since: TWiki::Plugins::VERSION 1.000 (27 Feb 2001)
getTwikiWebname( ) -> $nameGet name of TWiki documentation web as defined in TWiki.cfg Return:$name Name, e.g. 'TWiki' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Deleted: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | Synonymous with TWiki::Func::getPreferencesValue('TWIKIWEB'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Since: TWiki::Plugins::VERSION 1.000 (27 Feb 2001)
User Handling and Access ControlgetDefaultUserName( ) -> $loginNameGet default user name as defined in the configuration asDefaultUserLogin
Return: $loginName Default user name, e.g. 'guest'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getWikiName( ) -> $wikiNameGet Wiki name of logged in user Return:$wikiName Wiki Name, e.g. 'JohnDoe'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getWikiUserName( ) -> $wikiNameGet Wiki name of logged in user with web prefix Return:$wikiName Wiki Name, e.g. "Main.JohnDoe"
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
wikiToUserName( $wikiName ) -> $loginNameTranslate a Wiki name to a login name based on Main.TWikiUsers topic
$loginName Login name of user, e.g. 'jdoe'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
userToWikiName( $loginName, $dontAddWeb ) -> $wikiNameTranslate a login name to a Wiki name based on Main.TWikiUsers topic
$wikiName Wiki name of user, e.g. 'Main.JohnDoe' or 'JohnDoe'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
isGuest( ) -> $booleanTest if logged in user is a guest (TWikiGuest) Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)permissionsSet( $web ) -> $booleanTest if any access restrictions are set for this web, ignoring settings on individual pages
checkAccessPermission( $type, $wikiName, $text, $topic, $web ) -> $booleanCheck access permission for a topic based on the TWiki.TWikiAccessControl rules
Webs, Topics and Attachments@web"> getListOfWebs( $filter ) -> @webs
$filter may also contain the word 'public' which will further filter
out webs that have NOSEARCHALL set on them.
'allowed' filters out webs the current user can't read.
For example, the deprecated getPublicWebList function can be duplicated
as follows:
my @webs = TWiki::Func::getListOfWebs( "user,public" );Since: TWiki::Plugins::VERSION 1.1 webExists( $web ) -> $booleanTest if web exists
createWeb( $newWeb, $baseWeb, $opts )
use Error qw( :try ); use TWiki::AccessControlException; try { TWiki::Func::createWeb( "Newweb" ); } catch Error::Simple with { my $e = shift; # see documentation on Error::Simple } catch TWiki::AccessControlException with { my $e = shift; # see documentation on TWiki::AccessControlException } otherwise { ... };Since: TWiki::Plugins::VERSION 1.1 moveWeb( $oldName, $newName )Move (rename) a web.use Error qw( :try ); use TWiki::AccessControlException; try { TWiki::Func::moveWeb( "Oldweb", "Newweb" ); } catch Error::Simple with { my $e = shift; # see documentation on Error::Simple } catch TWiki::AccessControlException with { my $e = shift; # see documentation on TWiki::AccessControlException } otherwise { ... };To delete a web, move it to a subweb of Trash
TWiki::Func::moveWeb( "Deadweb", "Trash.Deadweb" );Since: TWiki::Plugins::VERSION 1.1 @topics"> getTopicList( $web ) -> @topicsGet list of all topics in a web
@topics Topic list, e.g. ( 'WebChanges', 'WebHome', 'WebIndex', 'WebNotify' )
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
topicExists( $web, $topic ) -> $booleanTest if topic exists
normalizeWebTopicName .
Since: TWiki::Plugins::VERSION 1.000 (14 Jul 2001)
checkTopicEditLock( $web, $topic ) -> ( $oopsUrl, $loginName, $unlockTime )Check if a lease has been taken by some other user.
( $oopsUrl, $loginName, $unlockTime ) - The $oopsUrl for calling redirectCgiQuery(), user's $loginName , and estimated $unlockTime in minutes, or ( '', '', 0 ) if no lease exists.
Since: TWiki::Plugins::VERSION 1.010 (31 Dec 2002)
setTopicEditLock( $web, $topic, $lock )
edit script
always takes out a lease.
It is impossible to fully lock a topic. Concurrent changes will be
merged.
Since: TWiki::Plugins::VERSION 1.010 (31 Dec 2002)
saveTopic( $web, $topic, $meta, $text, $options ) -> $error
my( $meta, $text ) = TWiki::Func::readTopic( $web, $topic ) $text =~ s/APPLE/ORANGE/g; TWiki::Func::saveTopic( $web, $topic, $meta, $text, { comment => 'refruited' } );Note: Plugins handlers ( e.g. beforeSaveHandler ) will be called as
appropriate.
saveTopicText( $web, $topic, $text, $ignorePermissions, $dontNotify ) -> $oopsUrlSave topic text, typically obtained by readTopicText(). Topic data usually includes meta data; the file attachment meta data is replaced by the meta data from the topic file if it exists.
$oopsUrl Empty string if OK; the $oopsUrl for calling redirectCgiQuery() in case of error
This method is a lot less efficient and much more dangerous than saveTopic .
Since: TWiki::Plugins::VERSION 1.010 (31 Dec 2002)
my $text = TWiki::Func::readTopicText( $web, $topic ); # check for oops URL in case of error: if( $text =~ /^http.*?\/oops/ ) { TWiki::Func::redirectCgiQuery( $query, $text ); return; } # do topic text manipulation like: $text =~ s/old/new/g; # do meta data manipulation like: $text =~ s/(META\:FIELD.*?name\=\"TopicClassification\".*?value\=\")[^\"]*/$1BugResolved/; $oopsUrl = TWiki::Func::saveTopicText( $web, $topic, $text ); # save topic text moveTopic( $web, $topic, $newWeb, $newTopic )
use Error qw( :try ); try { moveTopic( "Work", "TokyoOffice", "Trash", "ClosedOffice" ); } catch Error::Simple with { my $e = shift; # see documentation on Error::Simple } catch TWiki::AccessControlException with { my $e = shift; # see documentation on TWiki::AccessControlException } otherwise { ... }; getRevisionInfo($web, $topic, $rev, $attachment ) -> ( $date, $user, $rev, $comment )Get revision info of a topic or attachment
( $date, $user, $rev, $comment ) List with: ( last update date, login name of last user, minor part of top revision number ), e.g. ( 1234561, 'phoeny', "5" )
$meta->getRevisionInfo instead if you can - it is significantly
more efficient, and returns a user object that contains other user
information.
NOTE: prior versions of TWiki may under some circumstances have returned
the login name of the user rather than the wiki name; the code documentation
was totally unclear, and we have been unable to establish the intent.
However the wikiname is obviously more useful, so that is what is returned.
Since: TWiki::Plugins::VERSION 1.000 (29 Jul 2001)
getRevisionAtTime( $web, $topic, $time ) -> $revGet the revision number of a topic at a specific time.
readTopic( $web, $topic, $rev ) -> ( $meta, $text )Read topic text and meta data, regardless of access permissions.
( $meta, $text ) Meta data object and topic text
$meta is a perl 'object' of class TWiki::Meta . This class is
fully documented in the source code documentation shipped with the
release, or can be inspected in the lib/TWiki/Meta.pm file.
This method ignores topic access permissions. You should be careful to use checkAccessPermissions to ensure the current user has read access to the topic.
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
readTopicText( $web, $topic, $rev, $ignorePermissions ) -> $textRead topic text, including meta data
$text Topic text with embedded meta data; an oops URL for calling redirectCgiQuery() is returned in case of an error
This method is more efficient than readTopic , but returns meta-data embedded in the text. Plugins authors must be very careful to avoid damaging meta-data. You are recommended to use readTopic instead, which is a lot safer..
Since: TWiki::Plugins::VERSION 1.010 (31 Dec 2002)
attachmentExists( $web, $topic, $attachment ) -> $booleanTest if attachment exists
normalizeWebTopicName .
Since: TWiki::Plugins::VERSION 1.1
readAttachment( $web, $topic, $name, $rev ) -> $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | Read an attachment from the store for a topic, and return it as a string. The names of attachments on a topic can be recovered from the meta-data returned by readTopic . If the attachment does not exist, or cannot be read, undef will be returned. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > | Read an attachment from the store for a topic, and return it as a string. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Added: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > | names of attachments on a topic can be recovered from the meta-data returned
by readTopic . If the attachment does not exist, or cannot be read, undef
will be returned. If the revision is not specified, the latest version will
be returned. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
View permission on the topic is required for the read to be successful. Access control violations are flagged by a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | TWiki::AccessControlException. Permissions are checked for the user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > | TWiki::AccessControlException. Permissions are checked for the current user. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Deleted: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | passed in. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
my( $meta, $text ) = TWiki::Func::readTopic( $web, $topic ); my @attachments = $meta->find( 'FILEATTACHMENT' ); foreach my $a ( @attachments ) { try { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
< < | my $data = TWiki::Func::readAttachment( $meta, $a->{name} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > | my $data = TWiki::Func::readAttachment( $web, $topic, $a->{name} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
...
} catch TWiki::AccessControlException with {
};
}
Since: TWiki::Plugins::VERSION 1.1
saveAttachment( $web, $topic, $attachment, $opts )
$opts may include:
try { TWiki::Func::saveAttachment( $web, $topic, 'image.gif', { file => 'image.gif', comment => 'Picture of Health', hide => 1 } ); } catch Error::Simple with { # see documentation on Error } otherwise { ... };Since: TWiki::Plugins::VERSION 1.1 moveAttachment( $web, $topic, $attachment, $newWeb, $newTopic, $newAttachment )
use Error qw( :try ); try { # move attachment between topics moveAttachment( "Countries", "Germany", "AlsaceLorraine.dat", "Countries", "France" ); # Note destination attachment name is defaulted to the same as source } catch TWiki::AccessControlException with { my $e = shift; # see documentation on TWiki::AccessControlException } catch Error::Simple with { my $e = shift; # see documentation on Error::Simple };Since: TWiki::Plugins::VERSION 1.1 Assembling PagesreadTemplate( $name, $skin ) -> $textRead a template or skin. Embedded template directives get expanded
$text Template text
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
loadTemplate ( $name, $skin, $web ) -> $text
expandTemplate( $def ) -> $stringDo a , only expanding the template (not expanding any variables other than %TMPL)
writeHeader( $query, $contentLength )Prints a basic content-type HTML header for text/html to standard out
redirectCgiQuery( $query, $url )Redirect to URL
addToHEAD( $id, $header )Adds$header to the HTML header (the tag).
This is useful for Plugins that want to include some javascript custom css.
$header will be expanded before being inserted into the section.
Note that this is not the same as the HTTP header, which is modified through the Plugins modifyHeaderHandler .
Since: TWiki::Plugins::VERSION 1.1
example:
TWiki::Func::addToHEAD('PATTERN_STYLE','<link id="twikiLayoutCss" rel="stylesheet" type="text/css" href="%PUBURL%/TWiki/PatternSkin/layout.css" media="all" />') expandCommonVariables( $text, $topic, $web ) -> $textExpand all common%VARIABLES%
$text Expanded text, e.g. 'Current user is TWikiGuest'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
See also: expandVariablesOnTopicCreation
renderText( $text, $web ) -> $textRender text from TWiki markup into XHTML as defined in TWiki.TextFormattingRules
$text XHTML text, e.g. '<b>bold</b> and <code>fixed font</code>'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
internalLink( $pre, $web, $topic, $label, $anchor, $createLink ) -> $textRender topic name and link label into an XHTML link. Normally you do not need to call this funtion, it is called internally byrenderText()
$text XHTML anchor, e.g. '<a href='/cgi-bin/view/Main/WebNotify#Jump'>notify</a>'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
sendEmail ( $text, $retries ) -> $error
To: liz@windsor.gov.uk From: serf@hovel.net CC: george@whitehouse.gov Subject: Revolution Dear Liz, Please abolish the monarchy (with King George's permission, of course) Thanks, A. PeasantLeave a blank line between the last header field and the message body. Since: TWiki::Plugins::VERSION 1.1 wikiToEmail( $wikiName ) -> $email
Creating New TopicsexpandVariablesOnTopicCreation ( $text ) -> $textExpand the limited set of variables that are always expanded during topic creation
Special handlersSpecial handlers can be defined to make functions in plugins behave as if they were built-in to TWiki.registerTagHandler( $var, \&fn, $syntax )Should only be called from initPlugin. Register a function to handle a simple variable. Handles both %VAR% and %VAR{...}%. Registered variables are treated the same as TWiki internal variables, and are expanded at the same time. This is a lot more efficient than using thecommonTagsHandler .
sub handler(\%session, \%params, $topic, $web)where:
sub initPlugin{ TWiki::Func::registerTagHandler('EXEC', \&boo); } sub boo { my( $session, $params, $topic, $web ) = @_; my $cmd = $params->{_DEFAULT}; return "NO COMMAND SPECIFIED" unless $cmd; my $result = `$cmd 2>&1`; return $params->{silent} ? '' : $result; } }would let you do this: %EXEC{"ps -Af" silent="on"}%
registerRESTHandler( $alias, \&fn, )Should only be called from initPlugin. Adds a function to the dispatch table of the REST interface
sub handler(\%session)where:
The EmptyPlugin has the following call in the initPlugin handler: TWiki::Func::registerRESTHandler('example', \&restExample);This adds the restExample function to the REST dispatch table
for the EmptyPlugin under the 'example' alias, and allows it
to be invoked using the URL
http://server:port/bin/rest/EmptyPlugin/example
note that the URL
http://server:port/bin/rest/EmptyPlugin/restExample
(ie, with the name of the function instead of the alias) will not work.
SearchingsearchInWebContent($searchString, $web, \@topics, \%options ) -> \%mapSearch for a string in the content of a web. The search is over all content, including meta-data. Meta-data matches will be returned as formatted lines within the topic content (meta-data matches are returned as lines of the format %META:\w+{.*}%)
\%options hash may contain the following options:
my $result = TWiki::Func::searchInWebContent( "Slimy Toad", $web, \@topics, { casesensitive => 0, files_without_match => 0 } ); foreach my $topic (keys %$result ) { foreach my $matching_line ( @{$result->{$topic}} ) { ...etcSince: TWiki::Plugins::VERSION 1.1 Plugin-specific file handlinggetWorkArea( $pluginName ) -> $directorypathGets a private directory for Plugin use. The Plugin is entirely responsible for managing this directory; TWiki will not read from it, or write to it. The directory is guaranteed to exist, and to be writable by the webserver user. By default it will not be web accessible. The directory and it's contents are permanent, so Plugins must be careful to keep their areas tidy. Since: TWiki::Plugins::VERSION 1.1 (Dec 2005)readFile( $filename ) -> $textRead file, low level. Used for Plugin workarea.
$text Content of file, empty if not found
NOTE: Use this function only for the Plugin workarea, not for topics and attachments. Use the appropriate functions to manipulate topics and attachments.
Since: TWiki::Plugins::VERSION 1.000 (07 Dec 2002)
saveFile( $filename, $text )Save file, low level. Used for Plugin workarea.
General UtilitiesgetRegularExpression( $name ) -> $exprRetrieves a TWiki predefined regular expression or character class.
my $upper = TWiki::Func::getRegularExpression('upperAlpha'); my $alpha = TWiki::Func::getRegularExpression('mixedAlpha'); my $capitalized = qr/[$upper][$alpha]+/;Those expressions marked type 'RE' are precompiled regular expressions that can be used outside square brackets. For example: my $webRE = TWiki::Func::getRegularExpression('webNameRegex'); my $isWebName = ( $s =~ m/$webRE/ );
normalizeWebTopicName($web, $topic) -> ($web, $topic)Parse a web and topic name, supplying defaults as appropriate.
Main and TWiki are the web names set in $cfg{UsersWebName} and $cfg{SystemWebName} respectively.
writeWarning( $text )Log Warning that may require admin intervention to data/warning.txt
writeDebug( $text )Log debug message to data/debug.txt
formatTime( $time, $format, $timezone ) -> $textFormat the time in seconds into the desired time string
$text Formatted time string
isValidWikiWord ( $text ) -> $booleanCheck for a valid WikiWord or WikiName
extractParameters($attr ) -> %paramsExtract all parameters from a variable string and returns a hash of parameters
%params Hash containing all parameters. The nameless parameter is stored in key _DEFAULT
Since: TWiki::Plugins::VERSION 1.025 (26 Aug 2004)
extractNameValuePair( $attr, $name ) -> $valueExtract a named or unnamed value from a variable parameter string - Note: | Function TWiki::Func::extractParameters is more efficient for extracting several parameters
$value Extracted value
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
Deprecated functionsFrom time-to-time, the TWiki developers will add new functions to the interface (either to TWikiFuncDotPm, or new handlers). Sometimes these improvements mean that old functions have to be deprecated to keep the code manageable. When this happens, the deprecated functions will be supported in the interface for at least one more TWiki release, and probably longer, though this cannot be guaranteed. Updated plugins may still need to define deprecated handlers for compatibility with old TWiki 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 theTWiki::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.
The following functions are retained for compatibility only. You should
stop using them as soon as possible.
getScriptUrlPath( ) -> $pathGet script URL path DEPRECATED since 1.1 - usegetScriptUrl instead.
Return: $path URL path of TWiki scripts, e.g. "/cgi-bin"
WARNING: you are strongly recommended not to use this function, as the
{ScriptUrlPaths} URL rewriting rules will not apply to urls generated
using it.
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
@webs"> getPublicWebList( ) -> @websDEPRECATED since 1.1 - usegetListOfWebs instead.
Get list of all public webs, e.g. all webs that do not have the NOSEARCHALL flag set in the WebPreferences
Return: @webs List of all public webs, e.g. ( 'Main', 'Know', 'TWiki' )
Since: TWiki::Plugins::VERSION 1.000 (07 Dec 2002)
formatGmTime( $time, $format ) -> $textDEPRECATED since 1.1 - useformatTime instead.
Format the time to GM time
$text Formatted time string
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getDataDir( ) -> $dirDEPRECATED since 1.1 - use the content handling functions to manipulate topics instead Get data directory (topic file root) Return:$dir Data directory, e.g. '/twiki/data'
This function violates store encapsulation and is therefore deprecated.
Since: TWiki::Plugins::VERSION 1.000 (07 Dec 2002)
getPubDir( ) -> $dirDEPRECATED since 1.1 - use the content handling functions to manipulateattachments instead Get pub directory (file attachment root). Attachments are in$dir/Web/TopicName
Return: $dir Pub directory, e.g. '/htdocs/twiki/pub'
This function violates store encapsulation and is therefore deprecated.
Use readAttachment and saveAttachment instead.
Since: TWiki::Plugins::VERSION 1.000 (07 Dec 2002)
checkDependencies( $moduleName, $dependenciesRef ) -> $errorDEPRECATED since 1.1 - use TWiki:Plugins.BuildContrib and define DEPENDENCIES that can be statically evaluated at install time instead. It is a lot more efficient. Since: TWiki::Plugins::VERSION 1.025 (01 Aug 2004) |
Package =TWiki::Func<-- STARTINCLUDE required for huge TWikiDocumentation topic -->Official list of stable TWiki functions for Plugin developers This module defines official functions that Plugins can use to interact with the TWiki engine and content. Refer to EmptyPlugin and lib/TWiki/Plugins/EmptyPlugin.pm for a template Plugin and documentation on how to write a Plugin. Plugins should only use functions published in this module. If you use functions in other TWiki libraries you might create a security hole and you will probably need to change your Plugin when you upgrade TWiki. Deprecated functions will still work in older code, though they should not be called in new Plugins and should be replaced in older Plugins as soon as possible. The version of the TWiki::Func module is defined by the VERSION number of the TWiki::Plugins module, currently 6.10. This can be shown by the %PLUGINVERSION% variable. The 'Since' field in the function
documentation refers to the VERSION number and the date that the function
was addded.
Note: Beware! These methods should only ever be called
from the context of a TWiki Plugin. They require a Plugins SESSION context to be
established before they are called, and will not work if simply called from
another TWiki module. For example,
use TWiki; print TWiki::Func::getSkin(),"\n";will fail with Can't call method "getSkin" on an undefined value at TWiki/Func.pm line 83 .
If you want to call the methods outside the context of a plugin, you can create a Plugins SESSION object. For example,
the script:
use TWiki: $TWiki::Plugins::SESSION = new TWiki(); print TWiki::Func::getSkin(),"\n";will work happily. On this page:
EnvironmentgetSkin( ) -> $skinGet the skin path, set by theSKIN and COVER preferences variables or the skin and cover CGI parameters
Return: $skin Comma-separated list of skins, e.g. 'gnu,tartan' . Empty string if none.
Since: TWiki::Plugins::VERSION 1.000 (29 Jul 2001)
getUrlHost( ) -> $hostGet protocol, domain and optional port of script URL Return:$host URL host, e.g. "http://example.com:80"
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getScriptUrl( $web, $topic, $script, ... ) -> $urlCompose fully qualified URL
$url URL, e.g. "http://example.com:80/cgi-bin/view.pl/Main/WebNotify"
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getViewUrl( $web, $topic ) -> $urlCompose fully qualified view URL
$url URL, e.g. "http://example.com:80/cgi-bin/view.pl/Main/WebNotify"
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getOopsUrl( $web, $topic, $template, $param1, $param2, $param3, $param4 ) -> $urlCompose fully qualified 'oops' dialog URL
$url URL, e.g. "http://example.com:80/cgi-bin/oops.pl/ Main/WebNotify?template=oopslocked¶m1=joe"
This might be used like this:
my $url = TWiki::Func::getOopsUrl($web, $topic, 'oopsmistake', 'I made a boo-boo'); TWiki::Func::redirectCgiQuery( undef, $url ); return 0;Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002) Since TWiki::Plugins::VERSION 1.1, the recommended approach is to throw an oops exception. use Error qw( :try ); throw TWiki::OopsException($web, $topic, undef, 0, [ 'I made a boo-boo' ]);and let TWiki handle the cleanup. getPubUrlPath( ) -> $pathGet pub URL path Return:$path URL path of pub directory, e.g. "/pub"
Since: TWiki::Plugins::VERSION 1.000 (14 Jul 2001)
getCgiQuery( ) -> $queryGet CGI query object. Important: Plugins cannot assume that scripts run under CGI, Plugins must always test if the CGI query object is set Return:$query CGI query object; or 0 if script is called as a shell script
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getSessionValue( $key ) -> $valueGet a session value from the client session module
$value Value associated with key; empty string if not set
Since: TWiki::Plugins::VERSION 1.000 (27 Feb 200)
setSessionValue( $key, $value ) -> $booleanSet a session value via the client session module
clearSessionValue( $key ) -> $booleanClear a session value via the client session module
getContext() -> \%hashGet a hash of context identifiers representing the currently active context. The context is a set of identifiers that are set during specific phases of TWiki processing. For example, each of the standard scripts in the 'bin' directory each has a context identifier - the view script has 'view', the edit script has 'edit' etc. So you can easily tell what 'type' of script your Plugin is being called within. The core context identifiers are listed in the TWikiTemplates topic. Please be careful not to overwrite any of these identifiers! Context identifiers can be used to communicate between Plugins, and between Plugins and templates. For example, in FirstPlugin.pm, you might write:sub initPlugin { TWiki::Func::getContext()->{'MyID'} = 1; ...This can be used in SecondPlugin.pm like this: sub initPlugin { if( TWiki::Func::getContext()->{'MyID'} ) { ... } ...or in a template, like this: %TMPL:DEF{"ON"}% Not off %TMPL:END% %TMPL:DEF{"OFF"}% Not on %TMPL:END% %TMPL:P{context="MyID" then="ON" else="OFF"}%or in a topic: %IF{"context MyID" then="MyID is ON" else="MyID is OFF"}%Note: all plugins have an automatically generated context identifier if they are installed and initialised. For example, if the FirstPlugin is working, the context ID 'FirstPlugin' will be set. Since: TWiki::Plugins::VERSION 1.1 PreferencesgetPreferencesValue( $key, $web ) -> $valueGet a preferences value from TWiki or from a Plugin
$value Preferences value; empty string if not set
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getPluginPreferencesValue( $key ) -> $valueGet a preferences value from your Plugin
$value Preferences value; empty string if not set
Note: This function will will only work when called from the Plugin.pm file itself. it will not work if called from a sub-package (e.g. TWiki::Plugins::MyPlugin::MyModule)
Since: TWiki::Plugins::VERSION 1.021 (27 Mar 2004)
getPreferencesFlag( $key, $web ) -> $valueGet a preferences flag from TWiki or from a Plugin
$value Preferences flag '1' (if set), or "0" (for preferences values "off" , "no" and "0" )
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getPluginPreferencesFlag( $key ) -> $booleanGet a preferences flag from your Plugin
"off" , "no" and "0" , or values not set at all. True otherwise.
Note: This function will will only work when called from the Plugin.pm file itself. it will not work if called from a sub-package (e.g. TWiki::Plugins::MyPlugin::MyModule)
Since: TWiki::Plugins::VERSION 1.021 (27 Mar 2004)
getWikiToolName( ) -> $nameGet toolname as defined in TWiki.cfg Return:$name Name of tool, e.g. 'TWiki'
Synonymous with TWiki::Func::getPreferencesValue('WIKITOOLNAME');
Since: TWiki::Plugins::VERSION 1.000 (27 Feb 2001)
getMainWebname( ) -> $nameGet name of Main web as defined in TWiki.cfg Return:$name Name, e.g. 'Main'
Synonymous with TWiki::Func::getPreferencesValue('MAINWEB');
Since: TWiki::Plugins::VERSION 1.000 (27 Feb 2001)
getTwikiWebname( ) -> $nameGet name of TWiki documentation web as defined in TWiki.cfg Return:$name Name, e.g. 'TWiki'
Synonymous with TWiki::Func::getPreferencesValue('TWIKIWEB');
Since: TWiki::Plugins::VERSION 1.000 (27 Feb 2001)
User Handling and Access ControlgetDefaultUserName( ) -> $loginNameGet default user name as defined in the configuration asDefaultUserLogin
Return: $loginName Default user name, e.g. 'guest'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getWikiName( ) -> $wikiNameGet Wiki name of logged in user Return:$wikiName Wiki Name, e.g. 'JohnDoe'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getWikiUserName( ) -> $wikiNameGet Wiki name of logged in user with web prefix Return:$wikiName Wiki Name, e.g. "Main.JohnDoe"
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
wikiToUserName( $wikiName ) -> $loginNameTranslate a Wiki name to a login name based on Main.TWikiUsers topic
$loginName Login name of user, e.g. 'jdoe'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
userToWikiName( $loginName, $dontAddWeb ) -> $wikiNameTranslate a login name to a Wiki name based on Main.TWikiUsers topic
$wikiName Wiki name of user, e.g. 'Main.JohnDoe' or 'JohnDoe'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
isGuest( ) -> $booleanTest if logged in user is a guest (TWikiGuest) Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)permissionsSet( $web ) -> $booleanTest if any access restrictions are set for this web, ignoring settings on individual pages
checkAccessPermission( $type, $wikiName, $text, $topic, $web ) -> $booleanCheck access permission for a topic based on the TWiki.TWikiAccessControl rules
Webs, Topics and Attachments@web"> getListOfWebs( $filter ) -> @webs
$filter may also contain the word 'public' which will further filter
out webs that have NOSEARCHALL set on them.
'allowed' filters out webs the current user can't read.
For example, the deprecated getPublicWebList function can be duplicated
as follows:
my @webs = TWiki::Func::getListOfWebs( "user,public" );Since: TWiki::Plugins::VERSION 1.1 webExists( $web ) -> $booleanTest if web exists
createWeb( $newWeb, $baseWeb, $opts )
use Error qw( :try ); use TWiki::AccessControlException; try { TWiki::Func::createWeb( "Newweb" ); } catch Error::Simple with { my $e = shift; # see documentation on Error::Simple } catch TWiki::AccessControlException with { my $e = shift; # see documentation on TWiki::AccessControlException } otherwise { ... };Since: TWiki::Plugins::VERSION 1.1 moveWeb( $oldName, $newName )Move (rename) a web.use Error qw( :try ); use TWiki::AccessControlException; try { TWiki::Func::moveWeb( "Oldweb", "Newweb" ); } catch Error::Simple with { my $e = shift; # see documentation on Error::Simple } catch TWiki::AccessControlException with { my $e = shift; # see documentation on TWiki::AccessControlException } otherwise { ... };To delete a web, move it to a subweb of Trash
TWiki::Func::moveWeb( "Deadweb", "Trash.Deadweb" );Since: TWiki::Plugins::VERSION 1.1 @topics"> getTopicList( $web ) -> @topicsGet list of all topics in a web
@topics Topic list, e.g. ( 'WebChanges', 'WebHome', 'WebIndex', 'WebNotify' )
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
topicExists( $web, $topic ) -> $booleanTest if topic exists
normalizeWebTopicName .
Since: TWiki::Plugins::VERSION 1.000 (14 Jul 2001)
checkTopicEditLock( $web, $topic ) -> ( $oopsUrl, $loginName, $unlockTime )Check if a lease has been taken by some other user.
( $oopsUrl, $loginName, $unlockTime ) - The $oopsUrl for calling redirectCgiQuery(), user's $loginName , and estimated $unlockTime in minutes, or ( '', '', 0 ) if no lease exists.
Since: TWiki::Plugins::VERSION 1.010 (31 Dec 2002)
setTopicEditLock( $web, $topic, $lock )
edit script
always takes out a lease.
It is impossible to fully lock a topic. Concurrent changes will be
merged.
Since: TWiki::Plugins::VERSION 1.010 (31 Dec 2002)
saveTopic( $web, $topic, $meta, $text, $options ) -> $error
my( $meta, $text ) = TWiki::Func::readTopic( $web, $topic ) $text =~ s/APPLE/ORANGE/g; TWiki::Func::saveTopic( $web, $topic, $meta, $text, { comment => 'refruited' } );Note: Plugins handlers ( e.g. beforeSaveHandler ) will be called as
appropriate.
saveTopicText( $web, $topic, $text, $ignorePermissions, $dontNotify ) -> $oopsUrlSave topic text, typically obtained by readTopicText(). Topic data usually includes meta data; the file attachment meta data is replaced by the meta data from the topic file if it exists.
$oopsUrl Empty string if OK; the $oopsUrl for calling redirectCgiQuery() in case of error
This method is a lot less efficient and much more dangerous than saveTopic .
Since: TWiki::Plugins::VERSION 1.010 (31 Dec 2002)
my $text = TWiki::Func::readTopicText( $web, $topic ); # check for oops URL in case of error: if( $text =~ /^http.*?\/oops/ ) { TWiki::Func::redirectCgiQuery( $query, $text ); return; } # do topic text manipulation like: $text =~ s/old/new/g; # do meta data manipulation like: $text =~ s/(META\:FIELD.*?name\=\"TopicClassification\".*?value\=\")[^\"]*/$1BugResolved/; $oopsUrl = TWiki::Func::saveTopicText( $web, $topic, $text ); # save topic text moveTopic( $web, $topic, $newWeb, $newTopic )
use Error qw( :try ); try { moveTopic( "Work", "TokyoOffice", "Trash", "ClosedOffice" ); } catch Error::Simple with { my $e = shift; # see documentation on Error::Simple } catch TWiki::AccessControlException with { my $e = shift; # see documentation on TWiki::AccessControlException } otherwise { ... }; getRevisionInfo($web, $topic, $rev, $attachment ) -> ( $date, $user, $rev, $comment )Get revision info of a topic or attachment
( $date, $user, $rev, $comment ) List with: ( last update date, login name of last user, minor part of top revision number ), e.g. ( 1234561, 'phoeny', "5" )
$meta->getRevisionInfo instead if you can - it is significantly
more efficient, and returns a user object that contains other user
information.
NOTE: prior versions of TWiki may under some circumstances have returned
the login name of the user rather than the wiki name; the code documentation
was totally unclear, and we have been unable to establish the intent.
However the wikiname is obviously more useful, so that is what is returned.
Since: TWiki::Plugins::VERSION 1.000 (29 Jul 2001)
getRevisionAtTime( $web, $topic, $time ) -> $revGet the revision number of a topic at a specific time.
readTopic( $web, $topic, $rev ) -> ( $meta, $text )Read topic text and meta data, regardless of access permissions.
( $meta, $text ) Meta data object and topic text
$meta is a perl 'object' of class TWiki::Meta . This class is
fully documented in the source code documentation shipped with the
release, or can be inspected in the lib/TWiki/Meta.pm file.
This method ignores topic access permissions. You should be careful to use checkAccessPermissions to ensure the current user has read access to the topic.
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
readTopicText( $web, $topic, $rev, $ignorePermissions ) -> $textRead topic text, including meta data
$text Topic text with embedded meta data; an oops URL for calling redirectCgiQuery() is returned in case of an error
This method is more efficient than readTopic , but returns meta-data embedded in the text. Plugins authors must be very careful to avoid damaging meta-data. You are recommended to use readTopic instead, which is a lot safer..
Since: TWiki::Plugins::VERSION 1.010 (31 Dec 2002)
attachmentExists( $web, $topic, $attachment ) -> $booleanTest if attachment exists
normalizeWebTopicName .
Since: TWiki::Plugins::VERSION 1.1
readAttachment( $web, $topic, $name, $rev ) -> $data
readTopic . If the attachment does not exist, or cannot be read, undef will be returned.
View permission on the topic is required for the
read to be successful. Access control violations are flagged by a
TWiki::AccessControlException. Permissions are checked for the user
passed in.
my( $meta, $text ) = TWiki::Func::readTopic( $web, $topic ); my @attachments = $meta->find( 'FILEATTACHMENT' ); foreach my $a ( @attachments ) { try { my $data = TWiki::Func::readAttachment( $meta, $a->{name} ); ... } catch TWiki::AccessControlException with { }; }Since: TWiki::Plugins::VERSION 1.1 saveAttachment( $web, $topic, $attachment, $opts )
$opts may include:
try { TWiki::Func::saveAttachment( $web, $topic, 'image.gif', { file => 'image.gif', comment => 'Picture of Health', hide => 1 } ); } catch Error::Simple with { # see documentation on Error } otherwise { ... };Since: TWiki::Plugins::VERSION 1.1 moveAttachment( $web, $topic, $attachment, $newWeb, $newTopic, $newAttachment )
use Error qw( :try ); try { # move attachment between topics moveAttachment( "Countries", "Germany", "AlsaceLorraine.dat", "Countries", "France" ); # Note destination attachment name is defaulted to the same as source } catch TWiki::AccessControlException with { my $e = shift; # see documentation on TWiki::AccessControlException } catch Error::Simple with { my $e = shift; # see documentation on Error::Simple };Since: TWiki::Plugins::VERSION 1.1 Assembling PagesreadTemplate( $name, $skin ) -> $textRead a template or skin. Embedded template directives get expanded
$text Template text
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
loadTemplate ( $name, $skin, $web ) -> $text
expandTemplate( $def ) -> $stringDo a , only expanding the template (not expanding any variables other than %TMPL)
writeHeader( $query, $contentLength )Prints a basic content-type HTML header for text/html to standard out
redirectCgiQuery( $query, $url )Redirect to URL
addToHEAD( $id, $header )Adds$header to the HTML header (the tag).
This is useful for Plugins that want to include some javascript custom css.
$header will be expanded before being inserted into the section.
Note that this is not the same as the HTTP header, which is modified through the Plugins modifyHeaderHandler .
Since: TWiki::Plugins::VERSION 1.1
example:
TWiki::Func::addToHEAD('PATTERN_STYLE','<link id="twikiLayoutCss" rel="stylesheet" type="text/css" href="%PUBURL%/TWiki/PatternSkin/layout.css" media="all" />') expandCommonVariables( $text, $topic, $web ) -> $textExpand all common%VARIABLES%
$text Expanded text, e.g. 'Current user is TWikiGuest'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
See also: expandVariablesOnTopicCreation
renderText( $text, $web ) -> $textRender text from TWiki markup into XHTML as defined in TWiki.TextFormattingRules
$text XHTML text, e.g. '<b>bold</b> and <code>fixed font</code>'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
internalLink( $pre, $web, $topic, $label, $anchor, $createLink ) -> $textRender topic name and link label into an XHTML link. Normally you do not need to call this funtion, it is called internally byrenderText()
$text XHTML anchor, e.g. '<a href='/cgi-bin/view/Main/WebNotify#Jump'>notify</a>'
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
sendEmail ( $text, $retries ) -> $error
To: liz@windsor.gov.uk From: serf@hovel.net CC: george@whitehouse.gov Subject: Revolution Dear Liz, Please abolish the monarchy (with King George's permission, of course) Thanks, A. PeasantLeave a blank line between the last header field and the message body. Since: TWiki::Plugins::VERSION 1.1 wikiToEmail( $wikiName ) -> $email
Creating New TopicsexpandVariablesOnTopicCreation ( $text ) -> $textExpand the limited set of variables that are always expanded during topic creation
Special handlersSpecial handlers can be defined to make functions in plugins behave as if they were built-in to TWiki.registerTagHandler( $var, \&fn, $syntax )Should only be called from initPlugin. Register a function to handle a simple variable. Handles both %VAR% and %VAR{...}%. Registered variables are treated the same as TWiki internal variables, and are expanded at the same time. This is a lot more efficient than using thecommonTagsHandler .
sub handler(\%session, \%params, $topic, $web)where:
sub initPlugin{ TWiki::Func::registerTagHandler('EXEC', \&boo); } sub boo { my( $session, $params, $topic, $web ) = @_; my $cmd = $params->{_DEFAULT}; return "NO COMMAND SPECIFIED" unless $cmd; my $result = `$cmd 2>&1`; return $params->{silent} ? '' : $result; } }would let you do this: %EXEC{"ps -Af" silent="on"}%
registerRESTHandler( $alias, \&fn, )Should only be called from initPlugin. Adds a function to the dispatch table of the REST interface
sub handler(\%session)where:
The EmptyPlugin has the following call in the initPlugin handler: TWiki::Func::registerRESTHandler('example', \&restExample);This adds the restExample function to the REST dispatch table
for the EmptyPlugin under the 'example' alias, and allows it
to be invoked using the URL
http://server:port/bin/rest/EmptyPlugin/example
note that the URL
http://server:port/bin/rest/EmptyPlugin/restExample
(ie, with the name of the function instead of the alias) will not work.
SearchingsearchInWebContent($searchString, $web, \@topics, \%options ) -> \%mapSearch for a string in the content of a web. The search is over all content, including meta-data. Meta-data matches will be returned as formatted lines within the topic content (meta-data matches are returned as lines of the format %META:\w+{.*}%)
\%options hash may contain the following options:
my $result = TWiki::Func::searchInWebContent( "Slimy Toad", $web, \@topics, { casesensitive => 0, files_without_match => 0 } ); foreach my $topic (keys %$result ) { foreach my $matching_line ( @{$result->{$topic}} ) { ...etcSince: TWiki::Plugins::VERSION 1.1 Plugin-specific file handlinggetWorkArea( $pluginName ) -> $directorypathGets a private directory for Plugin use. The Plugin is entirely responsible for managing this directory; TWiki will not read from it, or write to it. The directory is guaranteed to exist, and to be writable by the webserver user. By default it will not be web accessible. The directory and it's contents are permanent, so Plugins must be careful to keep their areas tidy. Since: TWiki::Plugins::VERSION 1.1 (Dec 2005)readFile( $filename ) -> $textRead file, low level. Used for Plugin workarea.
$text Content of file, empty if not found
NOTE: Use this function only for the Plugin workarea, not for topics and attachments. Use the appropriate functions to manipulate topics and attachments.
Since: TWiki::Plugins::VERSION 1.000 (07 Dec 2002)
saveFile( $filename, $text )Save file, low level. Used for Plugin workarea.
General UtilitiesgetRegularExpression( $name ) -> $exprRetrieves a TWiki predefined regular expression or character class.
my $upper = TWiki::Func::getRegularExpression('upperAlpha'); my $alpha = TWiki::Func::getRegularExpression('mixedAlpha'); my $capitalized = qr/[$upper][$alpha]+/;Those expressions marked type 'RE' are precompiled regular expressions that can be used outside square brackets. For example: my $webRE = TWiki::Func::getRegularExpression('webNameRegex'); my $isWebName = ( $s =~ m/$webRE/ );
normalizeWebTopicName($web, $topic) -> ($web, $topic)Parse a web and topic name, supplying defaults as appropriate.
Main and TWiki are the web names set in $cfg{UsersWebName} and $cfg{SystemWebName} respectively.
writeWarning( $text )Log Warning that may require admin intervention to data/warning.txt
writeDebug( $text )Log debug message to data/debug.txt
formatTime( $time, $format, $timezone ) -> $textFormat the time in seconds into the desired time string
$text Formatted time string
isValidWikiWord ( $text ) -> $booleanCheck for a valid WikiWord or WikiName
extractParameters($attr ) -> %paramsExtract all parameters from a variable string and returns a hash of parameters
%params Hash containing all parameters. The nameless parameter is stored in key _DEFAULT
Since: TWiki::Plugins::VERSION 1.025 (26 Aug 2004)
extractNameValuePair( $attr, $name ) -> $valueExtract a named or unnamed value from a variable parameter string - Note: | Function TWiki::Func::extractParameters is more efficient for extracting several parameters
$value Extracted value
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
Deprecated functionsFrom time-to-time, the TWiki developers will add new functions to the interface (either to TWikiFuncDotPm, or new handlers). Sometimes these improvements mean that old functions have to be deprecated to keep the code manageable. When this happens, the deprecated functions will be supported in the interface for at least one more TWiki release, and probably longer, though this cannot be guaranteed. Updated plugins may still need to define deprecated handlers for compatibility with old TWiki 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 theTWiki::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.
The following functions are retained for compatibility only. You should
stop using them as soon as possible.
getScriptUrlPath( ) -> $pathGet script URL path DEPRECATED since 1.1 - usegetScriptUrl instead.
Return: $path URL path of TWiki scripts, e.g. "/cgi-bin"
WARNING: you are strongly recommended not to use this function, as the
{ScriptUrlPaths} URL rewriting rules will not apply to urls generated
using it.
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
@webs"> getPublicWebList( ) -> @websDEPRECATED since 1.1 - usegetListOfWebs instead.
Get list of all public webs, e.g. all webs that do not have the NOSEARCHALL flag set in the WebPreferences
Return: @webs List of all public webs, e.g. ( 'Main', 'Know', 'TWiki' )
Since: TWiki::Plugins::VERSION 1.000 (07 Dec 2002)
formatGmTime( $time, $format ) -> $textDEPRECATED since 1.1 - useformatTime instead.
Format the time to GM time
$text Formatted time string
Since: TWiki::Plugins::VERSION 1.000 (7 Dec 2002)
getDataDir( ) -> $dirDEPRECATED since 1.1 - use the content handling functions to manipulate topics instead Get data directory (topic file root) Return:$dir Data directory, e.g. '/twiki/data'
This function violates store encapsulation and is therefore deprecated.
Since: TWiki::Plugins::VERSION 1.000 (07 Dec 2002)
getPubDir( ) -> $dirDEPRECATED since 1.1 - use the content handling functions to manipulateattachments instead Get pub directory (file attachment root). Attachments are in$dir/Web/TopicName
Return: $dir Pub directory, e.g. '/htdocs/twiki/pub'
This function violates store encapsulation and is therefore deprecated.
Use readAttachment and saveAttachment instead.
Since: TWiki::Plugins::VERSION 1.000 (07 Dec 2002)
checkDependencies( $moduleName, $dependenciesRef ) -> $errorDEPRECATED since 1.1 - use TWiki:Plugins.BuildContrib and define DEPENDENCIES that can be statically evaluated at install time instead. It is a lot more efficient. Since: TWiki::Plugins::VERSION 1.025 (01 Aug 2004) |