---+ TWiki CGI Scripts
TWiki uses a set of scripts in the =bin= directory. This topic describes the interfaces to some of those scripts.

All the scripts can be called from the CGI environment or from the command-line.

%TOC%

---++ 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 cd'd to the =twiki/bin= directory to run the scripts from the command line.

Parameters are passed using '-name' - for example,
<verbatim>
$ cd /usr/local/twiki/bin
$ save -topic MyWeb.MyTopic -user admin -action save -text "New text of the topic"
</verbatim>
All parameters require a value.

---+++ Common parameters
All the scripts accept a number of common parameters. The first two components of the URL after the script name are taken as the web and the topic, respectively. Standard URL parameters are:

| *Parameter* | *Description* | *Default* |
| =topic= | 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.<nop>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. |
| =skin= | Overrides the default skin path (see TWikiSkins) |
| =cover= | Specifies temporary skin path to prepend to the skin path for this script only (see TWikiSkins) |

---++ =save=
The =save= script performs a range of save-related functions, as selected by the =action= parameter.

| *Parameter* | *Description* | *Default* |
| =action_save=1= | *default*; save, return to view, dontnotify is OFF |
| =action_quietsave=1= | save, and return to view, =dontnotify= is ON |
| =action_checkpoint= | save and redirect to the edit script, =dontnotify= is ON |
| =action_cancel= | exit without save, return to view |
| =action_preview= | preview edited text |
| =action_addform= | Redirect to the "change form" page. |
| =action_replaceform...= | Redirect to the "change form" page. |
| =action_delRev= | *Administrators only* delete the most recent revision of the topic - all other parameters are ignored |
| =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. |
| =onlynewtopic= | If set, error if topic already exists |
| =onlywikiname= | If set, error if topic name is not a WikiWord |
| =dontnotify= | if defined, suppress change notification |
| =templatetopic= | Name of a topic to use as a template for the text and form |
| =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 |
| =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. |

Any errors will cause a redirect to an =oops= page.

The parameters are interpreted in according to the following rules.

	1 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.
	1 When the action is =save=, =checkpoint=, =quietsave=, or =preview=:
		1 The new text is taken from the =text= parameter, if it is defined,
			* otherwise it is taken from the =templatetopic=, if it is defined,
			* otherwise it is taken from the previous version of the topic, if it exists,
			* otherwise it is assumed to be empty, and =forcenewrevision= is set.
		1 The name of the new form is taken from the =formtemplate=, if defined
			* otherwise it is taken from the =templatetopic=, if defined,
			* otherwise it is taken from the previous version of the topic, if any,
			* otherwise no form is attached.
		1 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,
			* 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 &gt; 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=.

---++ =oops=
This script is mainly used for rendering pages containing error messages, though it is also used for some functional actions such as manage pages (move topic etc).

=oops= templates are used with the =oops= script to generate system messages. This is done to make internationalisation or other local customisations simple. 

The =oops= script supports the following parameters:

| *Parameter* | *Description* | *Default* |
| =template= | Name of the template file to display |
| =def= | 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 %<nop>PARAM1% etc. |

---++ =edit=
The =edit= scipt understands the following parameters, typically supplied by HTML input fields:

| *Parameter* | *Description* | *Default* |
| =action= | Optional. Use the <code>edit<i>action</i></code> template instead of the standard =edit=, and if =action=text=, then hide the form |
| =onlynewtopic= | If set, error if topic already exists |
| =onlywikiname= | If set, error if topic name is not a WikiWord |
| =templatetopic= | The name of the template topic, copied to get the initial content |
| =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. |
| =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 =%<nop>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. |

Form field values are passed in parameters named 'field' - for example, if I have a field =Status= the parameter name is =Status=.
	1 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.

---++ =view=
Used for viewing topics.

| *Parameter* | *Description* | *Default* |
| =raw=on= | Shows the text of the topic in a scrollable textarea |
| =raw=debug= | 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). This is useful when you want to extract the source of a topic to a local file on disc. |
| =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 [[%SCRIPTURL%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%?template=edit][%SCRIPTURL%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%?template=edit]]. This is mainly useful when you have specialised templates for a TWiki Application. |
%W% For historical reasons, the view script has a special interpretation of the =text= skin. If the =skin=text= parameter is given, it is used like this:
http<nop>://.../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*, and this hack will be removed in a future release. 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.

| *Parameter* | *Description* | *Default* |
| =filename= | name of attachment |
| =rev= | Revision to view |

---++ =preview=
This script is _deprecated_. Its functions are covered by the =save= script.


---++ =rest=
This script can be invoked via http in a similar way as the view script (see *Invocation Examples*, bellow) to execute a plugin method. It'll print the result directly to the stream unless the =endPoint= parameter is specified, in which case the control is redirected to the given topic.

The =rest= script can receive one parameter:

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

Any additional parameter is passed directly to the method (i.e: The method can get any other parameter using the $query object)

---+++ Invocation Examples:

=http://my.host/bin/rest/EmptyPlugin/testRest=

Will invoke =TWiki::Plugin::EmptyPlugin::testRest=, and print the result directly to the stream.

=http://my.host/bin/rest/EmptyPlugin/testRest?endPoint=SomeWeb.SomeTopic=

Will invoke =TWiki::Plugin::EmptyPlugin::testRest=, and redirect the control to <nop>SomeWeb.SomeTopic

---++ =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=.

---++ =rdiff=
Renders the differences between version of a TwikiTopic

| *Parameter* | *Description* | *Default* |
| rev1 | the higher revision |
| rev2 | the lower revision |
| render | the rendering style {sequential, sidebyside, raw, debug} | DIFFRENDERSTYLE, =sequential= |
| type | history, diff, last} history diff, version to version, last version to previous | =diff= |
| context | number of lines of context |
TODO:
	* add a {word} render style

---++ =rename=
Used for renaming topics.

| *Parameter* | *Description* | *Default* |
| =skin= | skin(s) to use |
| =newweb= | new web name |
| =newtopic= | new topic name |
| =breaklock= | |
| =attachment= | |
| =confirm= | if defined, requires a second level of confirmation |
| =currentwebonly= | if defined, searches current web only for links to this topic |
| =nonwikiword= | if defined, a non-wikiword is acceptable for the new topic name |

---++ =attach=
Attach a file to a topic. CGI parameters are:

| *Parameter* | *Description* | *Default* |
| =filename= | Name of attachment |

---++ =upload=

| *Parameter* | *Description* | *Default* |
| =hidefile= | if defined, will not show file in attachment table |
| =filepath= | |
| =filename= | |
| =filecomment= | Comment to associate with file in attachment table |
| =createlink= | if defined, will create a link to file at end of topic |
| =changeproperties= | |

---++ =search=
CGI gateway to the =%<nop>SEARCH%= functionality driven by the following CGI parameters:

| *Parameter:* | *Description:* | *Default:* |
| ="text"= | Search term. Is a keyword search, literal search or regular expression search, depending on the =type= parameter. SearchHelp has more | required |
| =search="text"= | (Alternative to above) | N/A |
| =web="Name"= <br /> =web="%MAINWEB%, Know"= <br /> =web="all"= | Comma-separated list of webs to search. The special word =all= means all webs that doe *not* have the =NOSEARCHALL= variable set to =on= in their %WEBPREFSTOPIC%. You can specifically *exclude* webs from an =all= search using a minus sign - for example, =web="all,-Secretweb"=. | Current web |
| =topic="%WEBPREFSTOPIC%"= <br /> =topic="*Bug"= | Limit search to topics: A topic, a topic with asterisk wildcards, or a list of topics separated by comma. | All topics in a web |
| =excludetopic="Web*"= <br /> =excludetopic="%HOMETOPIC%, <nop>WebChanges"= | Exclude topics from search: A topic, a topic with asterisk wildcards, or a list of topics separated by comma. | None |
| =type="keyword"= <br /> =type="literal"= <br /> =type="regex"= | Do a keyword search like =soap "web service" -shampoo=; a literal search like =web service=; or RegularExpression search like =soap;web service;!shampoo= | =%<nop>SEARCHVAR- DEFAULTTYPE%= [[TWikiPreferences][preferences]] setting (%SEARCHVARDEFAULTTYPE%) |
| =scope="topic"= <br /> =scope="text"= <br /> =scope="all"= | Search topic name (title); the text (body) of topic; or all (both) | ="text"= |
| =order="topic"= <br /> =order="created"= <br />  =order="modified"= <br /> =order="editby"= <br /> =order=<br />&nbsp;"formfield(name)"= | 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 [[FormattedSearch][formatted]] table and sort it with TablePlugin's initsort | Sort by topic name |
| =limit="all"= <br /> =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 TimeInterval.  | All results |
| =reverse="on"= | Reverse the direction of the search | Ascending search |
| =casesensitive="on"= | Case sensitive search | Ignore case |
| =bookview="on"= | BookView search, e.g. show complete topic text | Show topic summary |
| =nonoise="on"= | Shorthand for =nosummary="on" nosearch="on" nototal="on" zeroresults="off" noheader="on" noempty="on"= | Off |
| =nosummary="on"= | Show topic title only | Show topic summary |
| =nosearch="on"= | Suppress search string | Show search string |
| =noheader="on"= | Suppress search header <br /> <span style='background: #FFB0B0;'> *Topics: Changed: By:* </span> | Show search header |
| =nototal="on"= | Do not show number of topics found | Show number |
| =zeroresults="off"= | Suppress all output if there are no hits | =zeroresults="on"=, displays: "Number of topics: 0" |
| =noempty="on"= | Suppress results for webs that have no hits. | Show webs with no hits |
| =header="..."= <br /> =format="..."= | Custom format results: see *[[FormattedSearch]]* for usage, variables &amp; examples | Results in table |
| =expandvariables="on"= | 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 =%<nop>CALC{}%= instead of the formula | Raw text |
| =multiple="on"= | Multiple hits per topic. Each hit can be [[FormattedSearch][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. | =off= |
| =separator=", "= | Line separator between hits | Newline ="$n"= |

---++ =changes=
Shows all the changes in the given web. 

The =changes= script can receive one parameter:

| *Parameter* | *Description* | *Default* |
| =minor= | If 0, show only major changes. If 1, show all the changes (both minor and major) | 0 |

The main difference between invoking this script or using WebChanges is that WebChanges is based on a =%<nop>SEARCH%=, while this script reads the =changes= file in each web, making it a faster alternative.

*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.

---++ =manage=
Performs a range of management functions.
| *Parameter* | *Description* | *Default* |
| =action= | One of =createweb=, =changePassword=, =bulkRegister=, =deleteUserAccount=, =editSettings= or =saveSettings= | none |

---+++ =action=createweb=
| *Parameter* | *Description* | *Default* |
| =webbgcolor= | Background colour for the web | '' |
| =sitemapwhat= | ? | '' |
| =sitemapuseto= | What the web is used for | '' |
| =nosearchall= | Value for NOSEARCHALL in the new web | '' |
| =newweb= | Name of the new web | '' |
| =baseweb= | Name of the web to copy to create the new web | '' |
| =newtopic= | ? | '' |

---+++ =action=changePassword=
| *Parameter* | *Description* | *Default* |
| =username= | Username | |
| =oldpassword= | Existing password (plain text) | |
| =password= | New password (plain text) | |
| =passwordA= | New password confirmation (plain text) | |
| =TopicName= | ? | |

---+++ =action=bulkRegister=
| *Parameter* | *Description* | *Default* |
| =OverwriteHomeTopics= | ? | false |
| =EmailUsersWithDetails= | ? | false |
| =LogTopic= | ? | Same as topic name, with 'Result' appended. |

---+++ =action=deleteUserAccount=
| *Parameter* | *Description* | *Default* |
| =password= | ? | none |

---+++ =action=editSettings=
No parameters

---+++ =action=saveSettings=
| *Parameter* | *Description* | *Default* |
| =text= | Text of the topic | '' |
| =originalrev= | Revision that the edit started on | Most recent revision |
All other parameters may be interpreted as form fields, depending on the current form definition in the topic.

---++ =passwd=

| *Parameter* | *Description* | *Default* |
| =action= | one of =changePassword= or =resetPassword= | none |
Other parameters as described under =manage=, =action=changePassword=.

---++ =register=

| *Parameter* | *Description* | *Default* |
| =action= | =register= or =verify= or =resetPassword= or =approve= | |

---++ =resetpasswd=

| *Parameter* | *Description* | *Default* |
| =LoginName= | *list* of usernames to reset | none |
| =Introduction= | ? | '' |

__Related Topics:__ AdminDocumentationCategory, DeveloperDocumentationCategory
<!-- Do _not_ attempt to edit this topic; it is auto-generated. Please add comments/questions/remarks to the Dev topic instead. -->
