Wiki source code of Scripting

Version 15.1 by Raluca Stavro on 2009/10/26

Hide last authors
Vincent Massol 8.3 1 #startfloatingbox()
2 *Contents*
Vincent Massol 14.1 3 #toc ("2" "4" "")
Vincent Massol 8.3 4 #endfloatingbox()
5
Vincent Massol 1.1 6 1 Scripting
7
8 XWiki integrates both Velocity and Groovy scripting. Together, these two mechanisms allow 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 Velocity and Groovy script syntax in addition to wiki and HTML syntax as the contents of an XWiki page.
9
Raluca Stavro 15.1 10 <a name="velocity"></a>
Vincent Massol 1.1 11 1.1 XWiki's Velocity API
12
Sergiu Dumitriu 11.2 13 The concept of the 'context' is central to Velocity. The context is a 'carrier' of data between the Java layer of the XWiki engine and the template or page layer. The programmers of the XWiki core have gathered objects of various types and placed them in the Velocity context. These objects, and their methods and properties, are accessible via template elements called references and effectively form an API for XWiki.
Vincent Massol 1.1 14
Vincent Massol 11.3 15 The API is documented in Javadoc format and can be accessed here: [XWiki API Javadoc>DevGuide.API]. 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 Velocity, Java, or object oriented programming. You can find all of that information already online. You should then refer to the [Velocity User Guide>http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html] as an ongoing reference. Finally, you can explore the page code found throughout the [Code Zone>code:Main.WebHome] area to see how others have figured out how to achieve a variety of results.
Vincent Massol 1.1 16
Vincent Massol 9.1 17 You can access in your velocity script to:
Sergiu Dumitriu 11.1 18 * The current document: *<tt>\$doc</tt>*
19 * The Context of the request: *<tt>\$context</tt>*
20 * the request object: *<tt>\$request</tt>*
21 * the response object: *<tt>\$response</tt>*
22 * the XWiki Object: *<tt>\$xwiki</tt>*
Vincent Massol 1.1 23
Vincent Massol 9.1 24 In addition the following Velocity tools are also available in the Velocity context:
Sergiu Dumitriu 11.1 25 * [List Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/ListTool.html]: *<tt>\$listtool</tt>*
26 * [Number Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/NumberTool.html]: *<tt>\$numbertool</tt>*
27 * [Date Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/DateTool.html]: *<tt>\$datetool</tt>*
28 * [Math Tool>http://velocity.apache.org/tools/releases/1.4/generic/MathTool.html]: *<tt>\$mathtool</tt>*
29 * [Escape Tool>http://velocity.apache.org/tools/releases/1.4/generic/EscapeTool.html]: *<tt>\$escapetool</tt>*
30 * [Sort Tool>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/SortTool.html]: *<tt>\$sorttool</tt>*
Vincent Massol 9.1 31
Vincent Massol 12.1 32 #info("If you wish to add new Velocity tools you'll need to edit your <tt>xwiki.properties</tt> file and follow the instructions in there.")
Vincent Massol 9.2 33
Vincent Massol 1.4 34 You can also [use HQL to query the XWiki database>velocityHqlExamples] from your velocity scripts.
Vincent Massol 1.1 35
Vincent Massol 5.1 36 To include Velocity scripts in other Velocity scripts, see [How to include a velocity page into another page>DevGuide.IncludeInVelocity].
jeanvivienmaurice 1.20 37
Vincent Massol 13.1 38 1.1.1 Other Velocity Variables
39
40 #info("These variables can be used but are subject to change in the future.")
41
Vincent Massol 14.2 42 1.1.1.1 Controlling whether to display Comments/History/Attachment/Information sections or not
Vincent Massol 13.1 43
44 It's possible to control whether to display these sections by setting some velocity variable to "no":
45
46 {code:none}
47 #set ($showcomments = "no")
48 #set ($showattachments = "no")
49 #set ($showhistory = "no")
50 #set ($showinformation = "no")
51 {code}
52
53 To remove them all you can set:
54
55 {code:none}
56 #set($docextras = [])
57 {code}
58
Vincent Massol 1.5 59 1.1 XWiki's Groovy API
Vincent Massol 1.1 60
61 Currently Groovy is only allowed for admins of a wiki (or users having the 'programming' right).
62
Vincent Massol 4.1 63 * See Groovy examples in the [Code Zone>code:Main.WebHome], more specifically in the [Code Snippets area>code:Snippets.WebHome]
Vincent Massol 1.1 64 * [Feeling Groovy>http://www-128.ibm.com/developerworks/java/library/j-alj08034.html]
65 * [MVC programming with Groovy templates>http://www-128.ibm.com/developerworks/java/library/j-pg02155/]
Vincent Massol 1.16 66 * [Guillaume Laforge on Groovy, XWiki etc.>http://www.stelligent.com/content/view/44/71/]
Vincent Massol 1.1 67
Vincent Massol 8.2 68 1.1.1 Groovy Example
gfiorentino 1.17 69
Sergiu Dumitriu 11.1 70 The following example demonstrates how to use a groovy script directly inside your page. This example performs a reverse DNS lookup from the velocity variable <tt>\$address</tt> and store the result into the variable <tt>\$hostname</tt>.
Vincent Massol 8.2 71
72 {code}
73 #set ($address = "172.20.12.61")
74
75 IP Address: $address
76
77 <%
78
79 import java.net.InetAddress;
80
81 vcontext = context.get("vcontext");
82 hostname = vcontext.get("address");
83
84 InetAddress addr = InetAddress.getByName(hostname);
85 hostname = addr.getHostName().toLowerCase();
86
87 %>
88
89 Hostname: $hostname
90 {code}
91

Get Connected