TWiki CGI and Command Line Scripts

Programs on the TWiki server performing actions such as rendering, saving and renaming topics.

The TWiki scripts are located in the twiki/bin and twiki/tools directories. This topic describes the interfaces to some of those scripts. All scripts in the twiki/bin directory can be called from the CGI (Common Gateway Interface) environment or from the command line. The scripts in the twiki/tools directory can only be called from the command line.

CGI Scripts

Details on CGI scripts located in the twiki/bin directory.

General Information

CGI environment

In the CGI environment parameters are passed to the scripts via the URL and URL parameters. Environment variables are also used to determine the user performing the action. If the environment is not set up, the default TWiki user is used (usually guest).

Command-line

You must be have the twiki/bin directory on the perl path to run the scripts from the command line. To avoid issues with file permissions, run the scripts as the web server user such as nobody or www.

Parameters are passed on the command line using '-name' - for example,

If this is set to a URL, TWiki will immediately redirect to that URL. Otherwise it overrides the URL and is taken as the topic name (you can pass Web.TopicName)

user

Command-line only; set the name of the user performing the action. Note: this usage is inherently insecure, as it bypasses webserver login constraints. For this reason only authorised users should be allowed to execute scripts from the command line.

Specifies temporary skin path to prepend to the skin path for this script only (see TWikiSkins)

attach

Despite the name, this script doesn't actually attach a file to a topic - for that, use upload. This script is part of the transactions sequence executed when a file is uploaded from the browser. it just generates the "new attachment" page for a topic.

changes

If 0, show only major changes. If 1, show all the changes (both minor and major)

0

The main difference between invoking this script and using WebChanges is that WebChanges is based on a %SEARCH%, while this script reads the changes file in each web, making it much faster.

Note: The result from changes script and the topic WebChanges can be different, if the changes file is deleted from a web. In particular, in new installations the changes script will return no results while the WebChanges topic will.

configure

configure is the browser script used for inspection and configuration of the TWiki configuration. None of the parameters to this script are useable for any purpose except configure. See configure.

edit

The edit script understands the following parameters, typically supplied by HTML input fields:

Optional. Use the editaction template instead of the standard edit. If action=text, then hide the form. If action=form hide the normal text area and only edit the form. You can change the Edit/Edit Raw buttons to always append the action parameter in skins like Pattern and Classic by setting the topic or preference variable EDITACTION to the value text or form. To edit the topic once the EDITACTION is defined as form simply remove the action=form from the browser URL of the edit script and reload the edit window

The name of the template topic, copied to get the initial content (new topic only)

text

Initial text for the topic

topicparent

The parent topic

formtemplate

Name of the form to instantiate in the topic. Overrides the form set in the templatetopic if defined. (will remove the form is set to 'none')

contenttype

Optional parameter that defines the application type to write into the CGI header. Defaults to text/html. May be used to invoke alternative client applications

anyname

Any parameter can passed to the new topic; if the template topic contains %URLPARAM{"anyname"}%, it will be replaced by its value

breaklock

If set, any lease conflicts will be ignored, and the edit will proceed even if someone is already editing the topic.

redirectto

If the user continues from edit to save, and if the save (or cancels the edit) process is successful, save will redirect to this topic or URL. The parameter value can be a TopicName, a Web.TopicName, or a URL.Note: Redirect to a URL only works if it is enabled in configure (Miscellaneous {AllowRedirectUrl}).

t

Provide a unique URL each time a topic is edited, typically specifying parameter t=%SERVERTIME{$epoch}% in an edit link. This is done to prevent browsers from caching an edit session, which could result in editing outdated content. The parameter name and value is arbitrary, but must be unique each time.

Form field values are passed in parameters named 'field' - for example, if I have a field Status the parameter name is Status.

The first sequence of ten or more X characters in the topic name will be converted on save to a number such that the resulting topic name is unique in the target web.

Note: Most skins support the definition of EDIT_SKIN, which is used as the value of the cover parameter in edit URLs. This allows you to override the default edit skin on a web, topic or user basis.

login

Used for logging in when TWiki login is being used (e.g TemplateLoginManager).

URL that was being accessed when an access violation occurred. the login process will redirect to this URL if it is successful

none

username

username of user logging in

none

password

password of user logging in

none

logon

Used for logging in when Web Server authentication is being used (e.g. ApacheLoginManager). The script does nothing; it is purely a placeholder for triggering the login process. The webserver will be set up to require a valid user to access this script, thus triggering the webserver login process.

action=editSettings

action=saveSettings

If the savesettings process is successful, save will redirect to this topic or URL. The parameter value can be a TopicName, a Web.TopicName, or a URL.Note: Redirect to a URL only works if it is enabled in configure (Miscellaneous {AllowRedirectUrl}).

All other parameters may be interpreted as form fields, depending on the current form definition in the topic.

BulkRegistration provides the means to create multiple accounts but it does not announce those accounts to the users who own them. BulkResetPassword is used to assign the passwords, the Introduction is used to explain why they are receiving the mail.

Optional, can be set to the name of a single definition within template. This definition will be instantiated in the template wherever %INSTANTIATE% is seen. This lets you use a single template file for many messages. For an example, see oopsmanagebad.tmpl.

paramN

Where N is an integer from 1 upwards. These values will be substituted into template for %PARAM1% etc.

preview

This script is deprecated. Its functions are covered by the save script.

If the rename process is successful, rename will redirect to this topic or URL. The parameter value can be a TopicName, a Web.TopicName, or a URL.Note: Redirect to a URL only works if it is enabled in configure (Miscellaneous {AllowRedirectUrl}).

Note: The rename script can only be called via http POST method, not GET. Make sure you specify method="post" if you call the rename script via a form action.

rest

This REST (Representational State Transfer) script can be invoked via http in the same way as the other TWiki scripts (see Invocation Examples, below) to execute a function that is associated to a "subject" and a "verb" (see below). These functions are usually registered by plugins using the TWiki::Func::registerRESTHandler method. The rest script will print the result directly to the browser unless the endPoint parameter is specified, in which case it will output a redirect to the given topic.

The rest script supports the following parameters:

username

If TemplateLogin, or a similar login manager not embedded in the web server, is used, then you need to pass a username and password to the server. The username and password parameters are used for this purpose.

password

See username

topic

If defined as the full name (including web) of a topic, then when the script starts up plugins will be passed this as the "current" topic. If not defined, then Main.WebHome will be passed to plugins.

endPoint

Where to redirect the response once the request is served, in the form "Web.Topic"

The function is free to use any other query parameters for its own purposes.

Note: The rest script should always require authentication in any TWiki that has logins. Otherwise there is a risk of opening up major security holes. So make sure you add it to the list of authenticated scripts if you are using ApacheLogin.

Invocation Examples

The rest script assumes that it will be called with URL in the form:

http://my.host/bin/rest/<subject>/<verb>

where <subject> must be the WikiWord name of one of the installed TWikiPlugins, and the <verb> is the alias for the function registered using the TWiki::Func::registerRESTHandler method. The <subject> and <verb> are then used to lookup and call the registered function.

<subject> and <verb> are checked for illegal characters exactly in the same way as the web and topic names.

As an example, the EmptyPlugin has registered a function to be used with the rest script under the subject EmptyPlugin and the verb example. Click below to see the rest script in action (run as TWikiGuest).

Administrators only delete the most recent revision of the topic - all other parameters are ignored. You have to be an administrator to use this, and not all store implementations will support it.

action_repRev

Administrators only replace the text of the most recent revision of the topic with the text in the text parameter. text must included embedded meta-data tags. All other parameters are ignored. You have to be an administrator to use this, and not all store implementations will support it.

Name of a topic to use as a template for the text and form (new topic only)

text

New text of the topic

forcenewrevision

if set, forces a revision even if TWiki thinks one isn't needed

topicparent

If 'none' remove any current topic parent. If the name of a topic, set the topic parent to this.

formtemplate

if defined, use the named template for the form (will remove the form is set to 'none')

editaction

When action is checkpoint, add form or replace form..., this is used as the action parameter to the edit script that is redirected to after the save is complete.

originalrev

Revision on which the edit started.

editparams

The parameter string to use to edit the topic

redirectto

The save process will redirect to this topic or URL if it is successful. (Typically this would be the URL that was being viewed when edit was invoked). The parameter value can be a TopicName, a Web.TopicName, or a URL.Note: Redirect to a URL only works if it is enabled in configure (Miscellaneous {AllowRedirectUrl}).

view topic being edited

Any errors will cause a redirect to an oops page.

The parameters are interpreted in according to the following rules.

The first sequence of ten or more X characters in the topic name will be converted to a number such that the resulting topic name is unique in the target web.

When the action is save, checkpoint, quietsave, or preview:

The new text is taken from the text parameter, if it is defined,

otherwise it is taken from the templatetopic, if it is defined, (new topic only)

otherwise it is taken from the previous version of the topic, if any,

The name of the new form is taken from the formtemplate, if defined

otherwise it is taken from the templatetopic, if defined, (new topic only)

otherwise it is taken from the previous version of the topic, if any,

otherwise no form is attached.

The value for each field in the form is taken from the query, if it is defined

otherwise it is taken from the templatetopic, if defined, (new topic only)

otherwise it is taken from the previous version of the topic, if any,

otherwise it defaults to the empty string.

Merging is only enabled if the topic text comes from text and originalrev is > 0 and is not the same as the revision number of the most recent revision. If merging is enabled both the topic and the meta-data are merged.

Form field values are passed in parameters named 'field' - for example, if I have a field Status the parameter name is Status.

Note: The save script can only be called via http POST method, not GET. Make sure to specify the "post" method if you call the save script via a form action. Example:

Sort the results of search by the topic names, topic creation time, last modified time, last editor, or named field of TWikiForms. The sorting is done web by web; in case you want to sort across webs, create a formatted table and sort it with TablePlugin's initsort

Sort by topic name

limit="all"limit="16"

Limit the number of results returned. This is done after sorting if order is specified

All results

date="..."

limits the results to those pages with latest edit time in the given time interval.

Expand variables before applying a FormattedSearch on a search hit. Useful to show the expanded text, e.g. to show the result of a SpreadSheetPlugin%CALC{}% instead of the formula

Raw text

multiple="on"

Multiple hits per topic. Each hit can be formatted. The last token is used in case of a regular expression ";" and search

Only one hit per topic

nofinalnewline="on"

If on, the search variable does not end in a line by itself. Any text continuing immediately after the search tag on the same line will be rendered as part of the table generated by the search, if appropriate.

view

As raw=on, but also shows the metadata (forms etc) associated with the topic.

raw=text

Shows only the source of the topic, as plain text (Content-type: text/plain). Only shows the body text, not the form or other meta-data.

raw=all

Shows only the source of the topic, as plain text (Content-type: text/plain), with embedded meta-data. This may be useful if you want to extract the source of a topic to a local file on disc.

section

Allows to view only a part of the topic delimited by a named section (see VarSTARTSECTION). If the given section is not present, no topic content is displayed.

contenttype

Allows you to specify a different Content-Type: (e.g. contenttype=text/plain)

rev

Revision to view (e.g. rev=45)

template

Allows you to specify a different skin template, overriding the 'view' template the view script would normally use. The default template is view. For example, you could specify /do/view/TWiki/TWikiScripts?template=edit. This is mainly useful when you have specialised templates for a TWiki Application.

topic

redirects (at the beging of the cgi script running) to show the spcified Web.Topic, or, redirects to a URL, if allowed by {AllowRedirectUrl} and {PermittedRedirectHostUrls} configure settings

For historical reasons, the view script has a special interpretation of the text skin. In earlier TWiki versions the skin=text parameter was used like this:
http://.../view/MyWeb/MyTopic?skin=text&contenttype=text/plain&raw=on
which shows the topic as plain text; useful for those who want to download plain text for the topic.
Using skin=text this way is DEPRECATED, use raw=text instead.

viewfile

Used for viewing attachments. Normally, a site will publish the attachments (pub) directory using a URL. However if it contains sensitive information, you will want to protect attachments using TWikiAccessControls. In this case, you can use the viewfile script to give access to attachments while still checking access controls.

Instead of using the filename parameter, you can append the attachment name
to the end of the URL path (after the topic) e.g. https://twiki.math.cornell.edu/do/viewfile/Webname/TopicName/Attachment.gif

Command Line Scripts

Details on command line scripts located in the twiki/tools directory.

geturl.pl

This is a very simple script to get the content of a web site. It is marked as deprecated and might be removed (or enhanced) in a future TWiki release. Its functions are covered by the standard wget and curl commands.

Usage: geturl.pl <host> <path> [<port> [<header>]]

Example: geturl.pl some.domain /some/dir/file.html 80

Will get: http://some.domain:80/some/dir/file.html

rewriteshebang.pl

Simple script to rewrite the #!/usr/bin/perl shebang lines specific to your local Perl installation. It will rewrite the first line of all your TWiki cgi scripts so they use a different shebang line. Use it if your perl is in a non-standard location, or you want to use a different interpreter (such as 'speedy').

tick_twiki.pl

This script executes a number of non-essential regular administration tasks that will help keep your TWiki healthy and happy, such as removing expired sessions and lease files.

It is intended to be run as a cron job or a scheduled task once a week. Example crontab entry:0 0 * * 0 cd /usr/twiki/bin && perl ../tools/tick_twiki.pl

Note: The script has to be run by a user who can write files created by the webserver user.