Wiki source code of Scripting

Version 38.1 by Vincent Massol on 2012/05/18

Hide last authors
OCTAGRAM 24.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
Caleb James DeLisle 23.1 4
Vincent Massol 20.4 5 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.
Vincent Massol 1.1 6
Vincent Massol 20.4 7 XWiki integrates [[jsr-223>>http://scripting.dev.java.net/]] scripting. You can script using several available languages by using one of the following macros:
OCTAGRAM 24.1 8
Vincent Massol 30.3 9 * [[Velocity Macro>>extensions:Extension.Velocity Macro]] (installed by default in XWiki Enterprise)
10 * [[Groovy Macro>>extensions:Extension.Groovy Macro]] (installed by default in XWiki Enterprise)
11 * [[Python Macro>>extensions:Extension.Python Macro]] (installed by default in XWiki Enterprise)
12 * [[Ruby Macro>>extensions:Extension.Ruby Macro]] (not installed by default in XWiki Enterprise)
13 * [[PHP Macro>>extensions:Extension.PHP Macro]] (not installed by default in XWiki Enterprise)
Vincent Massol 20.4 14
Vincent Massol 38.1 15 = Choosing a Scripting language =
16
17 Since XWiki supports several scripting languages you might be wondering which one to use.
18
19 The first thing to know is that Velocity is different from the other scripting languages on 2 aspects:
20 * It's a templating language rather than a pure scripting language, which means that its content is actually wiki markup interspersed with Velocity directives whereas pure scripting languages are written in that language and they need to output wiki markup. For example:(((
21 Velocity:
22
23 {{code}}
24 {{velocity}}
25 Your username is $xcontext.getUser(), welcome to the site.
26 {{/velocity}}
27 {{/code}}
28
29 Groovy:
30
31 {{code}}
32 {{groovy}}
33 println("Your username is " + xcontext.getUser() + " welcome to the site.");
34 {{/groovy}}
35 {{/code}}
36 )))
37 * It doesn't require special permissions since it runs in a Sandbox. Other scripting language require the user to have Programming Rights to execute them. Note that starting with XWiki 4.1 we've introduced a [[Sandbox for Groovy>>platform:AdminGuide.Configuration#HSecuringGroovyScripts]] too but it's still in an early stage.
38
39 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!
40
OCTAGRAM 24.1 41 = XWiki Scripting API =
Caleb James DeLisle 20.1 42
Vincent Massol 30.3 43 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 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>>extensions:Main.WebHome]] area to see how others have figured out how to achieve a variety of results.
Caleb James DeLisle 20.1 44
Vincent Massol 30.3 45 == [[Bindings>>extensions:Extension.Script Macro#HBindings]] ==
Caleb James DeLisle 20.1 46
47 These objects are available to you in scripting languages.
OCTAGRAM 24.1 48
Caleb James DeLisle 20.1 49 * [[The current Document>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Document.html]]: **##doc##**
50 * [[The Context of the request>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Context.html]]: **##xcontext##**
51 * [[The Request object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiRequest.html]]: **##request##**
52 * [[The Response object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiResponse.html]]: **##response##**
53 * [[The XWiki object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/XWiki.html]]: **##xwiki##**
Thomas Mortagne 32.1 54 * [[The XWiki utils>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Util.html]]: **##util##**
Caleb James DeLisle 20.1 55
Vincent Massol 30.3 56 == [[XWiki Component>>extensions:Extension.Component Module]] Access ==
Caleb James DeLisle 20.1 57
Vincent Massol 34.1 58 You can also gain direct access to XWiki components using the following code snippet (also see: [[Accessing components from Groovy>>DevGuide.WritingComponents#HAccessingacomponentfromgroovy]]):
Caleb James DeLisle 30.1 59
Eduard Moraru 35.4 60 {{info}}
61 This snippet is written in Groovy and will have to be converted to your scripting language.
62 {{/info}}
Vincent Massol 34.1 63
coffeemug13 31.1 64 {{code language="java"}}
65 {{groovy}}
Caleb James DeLisle 20.1 66 def greeter = com.xpn.xwiki.web.Utils.getComponent(org.xwiki.component.HelloWorld.class);
67 println greeter.sayHello();
coffeemug13 31.1 68 {{/groovy}}
69 {{/code}}
Caleb James DeLisle 20.1 70
Vincent Massol 20.3 71 == XWiki Core Access ==
Caleb James DeLisle 20.1 72
Vincent Massol 34.1 73 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.
Caleb James DeLisle 30.1 74
coffeemug13 31.1 75 {{code language="java"}}
76 {{groovy}}
Caleb James DeLisle 20.1 77 def xc = xcontext.getContext();
78 def wiki = xc.getWiki();
79 def xdoc = doc.getDocument();
coffeemug13 31.1 80 {{/groovy}}
81 {{/code}}
Caleb James DeLisle 30.1 82
Caleb James DeLisle 20.1 83 After using this snippet, you will have 3 new objects:
OCTAGRAM 24.1 84
Caleb James DeLisle 20.1 85 * [[The underlying XWikiContext behind the Context object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/XWikiContext.html]]: **##xc##**
86 * [[The underlying XWiki object which backs the **##xwiki##** object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/XWiki.html]]: **##wiki##**
87 * [[The underlying XWikiDocument behind the current Document>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/doc/XWikiDocument.html]]: **##xdoc##**
88
Caleb James DeLisle 22.2 89 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##**.
90
Caleb James DeLisle 22.1 91 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.
Caleb James DeLisle 20.1 92
Vincent Massol 23.2 93 == Querying XWiki's Model ==
94
Jean-Vincent Drean 37.1 95 From your script you can query the full XWiki's Model. Check the [[Query Module>>extensions:Extension.Query Module]] for more information.
Vincent Massol 23.2 96
OCTAGRAM 24.1 97 {{id name="velocity"/}}
98
Vincent Massol 20.3 99 = Velocity Specific Information =
Vincent Massol 1.1 100
Vincent Massol 38.1 101 Velocity is currently the only scripting language which can be used without Programming [[AdminGuide.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.
Vincent Massol 1.1 102
Vincent Massol 34.1 103 You can [[gain access to the XWiki core>>#HXWikiCoreAccess]] 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).
104
Caleb James DeLisle 20.1 105 In Velocity you can't import classes and as such you cannot gain direct access to XWiki components as shown [[above>>#HXWikiComponentAccess]]. This leaves you with the provided [[bindings>>#HBindings]] (NOTE: In Velocity, these bindings all start with **##$##** as with all other Velocity variables)
Vincent Massol 1.1 106
Caleb James DeLisle 20.1 107 For more information about programming in the Velocity language, you can refer to the [[Velocity User Guide>>http://velocity.apache.org/engine/releases/velocity-1.6.2/user-guide.html]].
Vincent Massol 1.1 108
Vincent Massol 36.2 109 For more details on using Velocity check the [[Velocity Module Documentation>>extensions:Extension.Velocity Module]] which also contains the full list of Velocity Tools that you can use in your scripts.
OCTAGRAM 24.1 110
111 {{info}}
112 If you wish to add new Velocity tools you'll need to edit your ##xwiki.properties## file and follow the instructions in there.
113 {{/info}}
Vincent Massol 9.2 114
Caleb James DeLisle 16.1 115 To include Velocity scripts in other Velocity scripts, see [[How to include a velocity page into another page>>DevGuide.IncludeInVelocity]].
jeanvivienmaurice 1.20 116
Vincent Massol 20.3 117 == Other Velocity Variables ==
Vincent Massol 13.1 118
Marta Girdea 28.1 119 {{warning}}
OCTAGRAM 24.1 120 These variables can be used but are subject to change in the future.
Marta Girdea 28.1 121 {{/warning}}
Vincent Massol 13.1 122
OCTAGRAM 24.1 123 {{id name="HControllingwhethertodisplayCommentsHistoryAttachmentInformationsectionsornot"/}}
124
Vincent Massol 20.3 125 === Controlling Which Sections to Display ===
Vincent Massol 13.1 126
Caleb James DeLisle 20.1 127 You can control whether to display Comments/History/Attachment/Information sections or not by setting some velocity variable to "no":
Vincent Massol 13.1 128
Thomas Mortagne 33.1 129 {{code language="velocity"}}
Vincent Massol 13.1 130 #set ($showcomments = "no")
131 #set ($showattachments = "no")
132 #set ($showhistory = "no")
133 #set ($showinformation = "no")
Caleb James DeLisle 16.1 134 {{/code}}
Vincent Massol 13.1 135
136 To remove them all you can set:
137
Thomas Mortagne 33.1 138 {{code language="velocity"}}
Vincent Massol 13.1 139 #set($docextras = [])
Caleb James DeLisle 16.1 140 {{/code}}
Vincent Massol 13.1 141
Marta Girdea 28.2 142 === Information about the current user ===
Marta Girdea 28.1 143
Sergiu Dumitriu 35.2 144 The following variables (set in the {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/templates/xwikivars.vm"}}xwikivars.vm{{/scm}} template) are shortcuts for checking various information **for the current user**:
Marta Girdea 28.1 145
146 * ##$isGuest##: checks if the current user is ##XWiki.XWikiGuest##
147 * ##$isSuperAdmin##: checks if the current user is the special user ##superadmin##
148
149 * ##$hasComment##: checks comment rights on the current document
150 * ##$hasEdit##: checks edit rights on the current document
151 * ##$hasWatch##: checks if the user is authenticated and the watch service is available
152
153 * ##$hasAdmin##: checks admin rights on the current document
154 * ##$hasSpaceAdmin##: checks admin rights on the ##XWikiPreferences## document of the current space
155 * ##$hasGlobalAdmin##: checks admin rights on ##XWiki.XWikiPreferences##
156
157 * ##$hasCreateSpace##: checks edit rights on that page that does not exist, in a space that doesn't exist
158 * ##$hasCreatePage##: checks edit rights on that page that does not exist, in the current space
159
160 * ##$hasProgramming##: checks if the current user has programming rights
161
162 * ##$isAdvancedUser##: advanced users: ##superadmin##, users with the ##usertype## property set to "Advanced", guest users with admin rights
163
Marta Girdea 28.2 164 Example:
Caleb James DeLisle 30.1 165
Thomas Mortagne 33.1 166 {{code language="velocity"}}
coffeemug13 31.1 167 {{velocity}}
Marta Girdea 28.2 168 #if ($hasAdmin)
169 ## This link will only be visible to users that have admin rights on this document
170 [[Do some admin action>>Some.Document]]
171 #end
coffeemug13 31.1 172 {{/velocity}}
173 {{/code}}
Marta Girdea 28.2 174
Marta Girdea 28.1 175 === Information about the current wiki ===
176
Sergiu Dumitriu 35.2 177 The following variables (set in the {{scm path="xwiki-platform-core/xwiki-platform-web/src/main/webapp/templates/xwikivars.vm"}}xwikivars.vm{{/scm}} template) are shortcuts for checking various information **about the current wiki**:
Marta Girdea 28.1 178
179 * ##$isReadOnly##
180 * ##$isInServletMode##
181 * ##$isInPortletMode##
182
Vincent Massol 20.3 183 = Groovy Specific Information =
Vincent Massol 1.1 184
Eduard Moraru 35.4 185 {{info}}
186 Currently all non Velocity scripting languages are only allowed users having Programming Rights.
187 {{/info}}
Vincent Massol 1.1 188
Vincent Massol 35.1 189 * See Groovy snippets in the [[Extensions wiki>>extensions:Main.WebHome]] (click on the "Groovy" tag in the Tag Cloud)
190 * [[Groovy web site>>http://groovy.codehaus.org/]]
Vincent Massol 1.1 191
Vincent Massol 20.3 192 == Groovy Example ==
gfiorentino 1.17 193
Caleb James DeLisle 18.1 194 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##.
Vincent Massol 8.2 195
Vincent Massol 34.1 196 **Using XWiki Syntax 2.0:**
197
Caleb James DeLisle 19.1 198 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.
Caleb James DeLisle 30.1 199
Thomas Mortagne 33.1 200 {{code language="velocity"}}
coffeemug13 31.1 201 {{velocity}}
Caleb James DeLisle 19.1 202 #set($hostname = "www.xwiki.org")
203 Host Name: $hostname
204 $xcontext.put("hostname", $hostname)
205 {{/velocity}}
206 {{groovy}}
207 import java.net.InetAddress;
208 host = xcontext.get("hostname");
209 InetAddress addr = InetAddress.getByName(host);
210 String address = addr.getHostAddress();
211 xcontext.put("address", address);
212 {{/groovy}}
213 {{velocity}}
214 IP Address: $xcontext.get("address")
coffeemug13 31.1 215 {{/velocity}}
216 {{/code}}
Caleb James DeLisle 19.1 217
Vincent Massol 34.1 218 **Using XWiki Syntax 1.0:**
219
Caleb James DeLisle 19.1 220 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.
Caleb James DeLisle 30.1 221
Thomas Mortagne 33.1 222 {{code language="velocity"}}
coffeemug13 31.1 223 #set ($hostname = "www.xwiki.org")
Caleb James DeLisle 18.1 224 Host Name: $hostname
Vincent Massol 8.2 225 <%
226 import java.net.InetAddress;
227 vcontext = context.get("vcontext");
Caleb James DeLisle 18.1 228 host = vcontext.get("hostname");
229 InetAddress addr = InetAddress.getByName(host);
230 String address = addr.getHostAddress();
Vincent Massol 8.2 231 %>
coffeemug13 31.1 232 IP Address: $address
233 {{/code}}
Caleb James DeLisle 20.1 234
Caleb James DeLisle 26.1 235 = Python Specific Information =
Marta Girdea 28.1 236
Vincent Massol 34.1 237 You can run Python code in XWiki just like Velocity or Groovy.
Caleb James DeLisle 30.1 238
coffeemug13 31.1 239 {{code language="python"}}
240 {{python}}
Caleb James DeLisle 20.1 241 print "The full name of this document is " + doc.getFullName()
coffeemug13 31.1 242 {{/python}}
243 {{/code}}
Caleb James DeLisle 20.1 244
Caleb James DeLisle 26.1 245 {{warning}}
Vincent Massol 30.3 246 Versions prior to [[XWiki Enterprise 2.4>>xwiki:ReleaseNotes.ReleaseNotesXWikiEnterprise24]] have a bug which prevents you from having access to the default objects (doc, xcontext, request, etc.) a [[workaround is available in the Extensions wiki>>extensions:Extension.Access To Bindings In Python]]
Caleb James DeLisle 26.1 247 {{/warning}}
248
Vincent Massol 20.3 249 = Scripting In XWiki Syntax 1.0 =
Caleb James DeLisle 20.1 250
Vincent Massol 20.5 251 XWiki Syntax 1.0 is rendered by an old rendering engine which 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.
Caleb James DeLisle 20.1 252
253 * The only scripting languages available to you are Velocity and Groovy.
254 * In Groovy, the context is known as: **##context##** not **##xcontext##**
Vincent Massol 30.3 255 * The beginning and end of Groovy scripts are denoted by <% and %> rather than through the [[extensions:Extension.Groovy Macro]] (using ~{~{groovy}} and ~{~{/groovy}})
256 * Velocity is parsed in a page no matter what (there is no need to invoke the [[extensions:Extension.Velocity Macro]] using ~{~{velocity}} and ~{~{/velocity}})
Caleb James DeLisle 20.1 257
258 The last part is important because it means you need to be careful of using $ and # in your document. This is still true inside of <% and %> so you have to be careful writing Groovy.

Get Connected