General Actions:
Scripting allows you to create basic to complex web applications at the XWiki page (or view) layer without the need for compiling code or deploying software components. In other words, you can use scripting syntax in addition to wiki and HTML syntax as the contents of an XWiki page.
XWiki integrates jsr-223 scripting. You can script using several available languages by using one of the following macros:
Since XWiki supports several scripting languages you might be wondering which one to use. Most of the code written by XWiki developers is in Velocity, with a few more complex extensions written in Groovy; these two are thoroughly tried and tested, so they are both a safe bet. The other languages should work just as well, but there are less developers that could help answering any questions.
The first thing to know is that Velocity is different from the other scripting languages on 2 aspects:
Velocity:
Groovy:
Being a templating engine, Velocity doesn't offer many means of structuring code. In fact, there's only one useful directive in this regard, #macro. However, because it is a templating engine, its syntax is much simpler and easier to understand by non-developers, which means that it's accessible to a wider range of users, without a serious background in programming.
Without programming rights, it's impossible to instantiate new objects, except literals and those safely offered by the XWiki APIs. Nevertheless, the XWiki API is powerful enough to allow a wide range of applications to be safely developed, if "the XWiki way" is properly followed.
Velocity is also available in some other parts of XWiki: it is the language in which all the templates that generate the HTML UI of XWiki are written, it can be optionally activated in skin extensions, and it is executed when sending CSS and JavaScript skin resources from the filesystem.
In conclusion, Velocity is suited for projects with small to medium complexity, and which don't require access to other resources except the provided XWiki API and registered script services. It allows very quick and easy development, offers good security and decent performance, and can easily be packaged and distributed as a XAR.
Groovy is a full-fledged scripting language, which supports almost the entire Java syntax, and provides its own syntax delicacies and custom APIs that enhance the Java language even further. While it is recommended that complex code be written in Java as components accessible via script services, Groovy has the advantage that it is written live in the wiki, without requiring compilation, deployment and server restarts, thus enabling faster development.
The XWiki API is available in the context when executing Groovy scripts, but unlike in Velocity, the code isn't limited to this API, and any other classes or objects can be accessed freely. New classes can be defined in Groovy, compatible with Java classes, and this allows more structured code to be written, unlike in Velocity. A particular case of classes is new component roles and component implementations, which allows, for example, new script services or new event listeners to be defined in the wiki. It is possible to load attached jar files into the classpath of an executing script, which means that a wiki document can contain a complex program AND its required libraries not already provided by the platform.
Other than being available as a scripting language for writing custom code, it is also the language in which scheduler tasks are written.
In conclusion, Groovy is suited for complex projects or for custom wiki enhancement through new components, when speedy live development is required. Being written in wiki documents, it can also be easily packaged and distributed as a XAR.
After taking into account these considerations and if requiring Programming Rights isn't an issue for you, you should pick the script language that you're most familiar with!
The API is documented in Javadoc format and can be accessed here: XWiki API Javadoc. If you are not familiar with Java or object oriented programming, you will probably be confused by the API documentation. It is not within the scope of our documentation to teach you all the details about Java, or object oriented programming. You can find all of that information already online. You can also explore the page code found throughout the Extensions wiki area to see how others have figured out how to achieve a variety of results.
These objects are available to you in scripting languages.
Since XWiki 4.1M2+ there's a Script Service to access the Component Manager (see also: Accessing components from Groovy).
For example using Groovy you'd write:
You can also get the ComponentManager with:
Sometimes the XWiki Api doesn't provide the methods which you need for your application. You can gain raw access the core of XWiki but it presents an increased security risk and requires Programming Rights to be able to save the page containing the script (Programming Rights are not required for viewing a page containing a script requiring Programming Rights, rights are only needed at save time). Using the core should be avoided if at all possible.
After using this snippet, you will have 3 new objects:
You will find that many of the methods in wiki and xdoc require an instance of the XWikiContext, this is the underlying xcontext xc not the Api context xcontext.
Again, these methods are only for the rare cases when functionality is not provided by the public Api. We put a lot of effort into preserving the behavior of the public Api and much less into preserving the behavior of core methods so you may find that core methods are deprecated, removed, or their behavior is changed in subsequent versions.
From your script you can query the full XWiki Model. Check the Query Module for more information.
Velocity is currently the only scripting language which can be used without Programming Access Rights. This means you can save velocity scripts using a user with less permission and an exploit of your script is less of a security breach.
You can gain access to the XWiki core from Velocity but this will require Programming Rights. Strictly speaking, protected APIs are only available when the page that contains them was last saved by someone who had Programming Rights (see above).
In Velocity you can't import classes and as such you cannot gain direct access to XWiki components as shown above. This leaves you with the provided bindings (NOTE: In Velocity, these bindings all start with $ as with all other Velocity variables)
For more information about programming in the Velocity language, you can refer to the Velocity User Guide.
For more details on using Velocity check the Velocity Module Documentation which also contains the full list of Velocity Tools that you can use in your scripts.
To include Velocity scripts in other Velocity scripts, see How to include a velocity page into another page.
You can control whether to display Comments/History/Attachment/Information sections or not by setting some velocity variables to "no":
To remove them all you can set:
The following variables (set in the xwikivars.vm template) are shortcuts for checking various information for the current user:
Example:
The following variables (set in the xwikivars.vm template) are shortcuts for checking various information about the current wiki:
The following example demonstrates how to use a groovy script to interact with velocity code in your page. This example performs a DNS lookup from the velocity variable $hostname and stores the result in the variable $address.
Using XWiki Syntax 2.0:
Objects can be passed back and forth between scripting languages by storing them in commonly available objects. One such commonly available object which only lasts the length of the request is the context object, known as xcontext.
Using XWiki Syntax 1.0:
Because Groovy and Velocity code are parsed together, variables defined in Groovy can be used directly in velocity without storing in and retrieving from the context.
You can run Python code in XWiki just like Velocity or Groovy.
Most JSR223 based scripting languages reinject the created variable in the current ScriptContext which means you can define a variable in a Groovy script and reuse it in a following Python script for example.
Since 4.1M2 a bridge has been written for Velocity (which is not based on JSR223) to allow it to access the current ScriptContext variable so you can do things like:
XWiki Syntax 1.0 is rendered by an old rendering engine which is still supported but for which no further development is planned (it will eventually be removed). Syntax 1.0 has some idiosyncrasies which were solved by syntax 2.0.
The last part is important because it means you need to be careful when using $ and # in your document. This is still true inside of <% and %> so you have to be careful when writing Groovy.