Wiki source code of Scripting

Version 32.1 by Thomas Mortagne on 2011/03/24

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
OCTAGRAM 24.1 15 = XWiki Scripting API =
Caleb James DeLisle 20.1 16
Vincent Massol 30.3 17 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 18
Vincent Massol 30.3 19 == [[Bindings>>extensions:Extension.Script Macro#HBindings]] ==
Caleb James DeLisle 20.1 20
21 These objects are available to you in scripting languages.
OCTAGRAM 24.1 22
Caleb James DeLisle 20.1 23 * [[The current Document>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Document.html]]: **##doc##**
24 * [[The Context of the request>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Context.html]]: **##xcontext##**
25 * [[The Request object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiRequest.html]]: **##request##**
26 * [[The Response object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiResponse.html]]: **##response##**
27 * [[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 28 * [[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 29
Vincent Massol 30.3 30 == [[XWiki Component>>extensions:Extension.Component Module]] Access ==
Caleb James DeLisle 20.1 31
32 You can also gain direct access to XWiki components using the following code snippet:
33 Also see: [[Accessing components from Groovy>>DevGuide.WritingComponents#HAccessingacomponentfromgroovy]]
34 Note: This snippet is written in Groovy and will have to be converted to your scripting language.
Caleb James DeLisle 30.1 35
coffeemug13 31.1 36 {{code language="java"}}
37 {{groovy}}
Caleb James DeLisle 20.1 38 def greeter = com.xpn.xwiki.web.Utils.getComponent(org.xwiki.component.HelloWorld.class);
39 println greeter.sayHello();
coffeemug13 31.1 40 {{/groovy}}
41 {{/code}}
Caleb James DeLisle 20.1 42
Vincent Massol 20.3 43 == XWiki Core Access ==
Caleb James DeLisle 20.1 44
Caleb James DeLisle 22.2 45 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 run. Using the core should be avoided if at all possible.
Caleb James DeLisle 30.1 46
coffeemug13 31.1 47 {{code language="java"}}
48 {{groovy}}
Caleb James DeLisle 20.1 49 def xc = xcontext.getContext();
50 def wiki = xc.getWiki();
51 def xdoc = doc.getDocument();
coffeemug13 31.1 52 {{/groovy}}
53 {{/code}}
Caleb James DeLisle 30.1 54
Caleb James DeLisle 20.1 55 After using this snippet, you will have 3 new objects:
OCTAGRAM 24.1 56
Caleb James DeLisle 20.1 57 * [[The underlying XWikiContext behind the Context object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/XWikiContext.html]]: **##xc##**
58 * [[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##**
59 * [[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##**
60
Caleb James DeLisle 22.2 61 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##**.
62
Caleb James DeLisle 22.1 63 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 64
Vincent Massol 23.2 65 == Querying XWiki's Model ==
66
67 From your script you can query the full XWiki's Model. Check the [[Query Guide>>QueryGuide]] for more information.
68
OCTAGRAM 24.1 69 {{id name="velocity"/}}
70
Vincent Massol 20.3 71 = Velocity Specific Information =
Vincent Massol 1.1 72
Caleb James DeLisle 20.1 73 Velocity is the only scripting language which can be used without Administration or Programming [[AdminGuide.Access Rights]]. This means you can save velocity scripts using a username with less permission and an exploit of your script is less of a security breach. Also if you are administrating a [[virtual wiki>>AdminGuide.Virtualization]] and you don't have programming rights, you can still do scripting in Velocity.
OCTAGRAM 24.1 74 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 has programming rights.
Vincent Massol 1.1 75
Caleb James DeLisle 20.1 76 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 77
Caleb James DeLisle 20.1 78 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 79
Caleb James DeLisle 20.1 80 The following Velocity tools are also available in addition to the bindings.
OCTAGRAM 24.1 81
Caleb James DeLisle 16.1 82 * [[List Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/ListTool.html]]: **##$listtool##**
83 * [[Number Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/NumberTool.html]]: **##$numbertool##**
Vincent Massol 29.3 84 * [[Comparison Date Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/ComparisonDateTool.html]] (extends [[Date Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/DateTool.html]]): **##$datetool##**
Caleb James DeLisle 16.1 85 * [[Math Tool>>http://velocity.apache.org/tools/releases/1.4/generic/MathTool.html]]: **##$mathtool##**
86 * [[Escape Tool>>http://velocity.apache.org/tools/releases/1.4/generic/EscapeTool.html]]: **##$escapetool##**
87 * [[Sort Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/SortTool.html]]: **##$sorttool##**
Vincent Massol 21.2 88 * Message Tool: **##$msg##** This tool is used to provide internationalized messages based on keys. For more details see the [[How to Write Internationalized Applications tutorial>>DevGuide.InternationalizingApplications]].
Vincent Massol 9.1 89
OCTAGRAM 24.1 90 {{info}}
91 If you wish to add new Velocity tools you'll need to edit your ##xwiki.properties## file and follow the instructions in there.
92 {{/info}}
Vincent Massol 9.2 93
Caleb James DeLisle 16.1 94 To include Velocity scripts in other Velocity scripts, see [[How to include a velocity page into another page>>DevGuide.IncludeInVelocity]].
jeanvivienmaurice 1.20 95
Vincent Massol 20.3 96 == Other Velocity Variables ==
Vincent Massol 13.1 97
Marta Girdea 28.1 98 {{warning}}
OCTAGRAM 24.1 99 These variables can be used but are subject to change in the future.
Marta Girdea 28.1 100 {{/warning}}
Vincent Massol 13.1 101
OCTAGRAM 24.1 102 {{id name="HControllingwhethertodisplayCommentsHistoryAttachmentInformationsectionsornot"/}}
103
Vincent Massol 20.3 104 === Controlling Which Sections to Display ===
Vincent Massol 13.1 105
Caleb James DeLisle 20.1 106 You can control whether to display Comments/History/Attachment/Information sections or not by setting some velocity variable to "no":
Vincent Massol 13.1 107
Caleb James DeLisle 16.1 108 {{code}}
Vincent Massol 13.1 109 #set ($showcomments = "no")
110 #set ($showattachments = "no")
111 #set ($showhistory = "no")
112 #set ($showinformation = "no")
Caleb James DeLisle 16.1 113 {{/code}}
Vincent Massol 13.1 114
115 To remove them all you can set:
116
Caleb James DeLisle 16.1 117 {{code}}
Vincent Massol 13.1 118 #set($docextras = [])
Caleb James DeLisle 16.1 119 {{/code}}
Vincent Massol 13.1 120
Marta Girdea 28.2 121 === Information about the current user ===
Marta Girdea 28.1 122
Marta Girdea 28.2 123 The following variables (set in the [[xwikivars.vm>>http://svn.xwiki.org/svnroot/xwiki/platform/web/trunk/standard/src/main/webapp/templates/xwikivars.vm]] template) are shortcuts for checking various information **for the current user**:
Marta Girdea 28.1 124
125 * ##$isGuest##: checks if the current user is ##XWiki.XWikiGuest##
126 * ##$isSuperAdmin##: checks if the current user is the special user ##superadmin##
127
128 * ##$hasComment##: checks comment rights on the current document
129 * ##$hasEdit##: checks edit rights on the current document
130 * ##$hasWatch##: checks if the user is authenticated and the watch service is available
131
132 * ##$hasAdmin##: checks admin rights on the current document
133 * ##$hasSpaceAdmin##: checks admin rights on the ##XWikiPreferences## document of the current space
134 * ##$hasGlobalAdmin##: checks admin rights on ##XWiki.XWikiPreferences##
135
136 * ##$hasCreateSpace##: checks edit rights on that page that does not exist, in a space that doesn't exist
137 * ##$hasCreatePage##: checks edit rights on that page that does not exist, in the current space
138
139 * ##$hasProgramming##: checks if the current user has programming rights
140
141 * ##$isAdvancedUser##: advanced users: ##superadmin##, users with the ##usertype## property set to "Advanced", guest users with admin rights
142
Marta Girdea 28.2 143 Example:
Caleb James DeLisle 30.1 144
coffeemug13 31.1 145 {{code}}
146 {{velocity}}
Marta Girdea 28.2 147 #if ($hasAdmin)
148 ## This link will only be visible to users that have admin rights on this document
149 [[Do some admin action>>Some.Document]]
150 #end
coffeemug13 31.1 151 {{/velocity}}
152 {{/code}}
Marta Girdea 28.2 153
154
Marta Girdea 28.1 155 === Information about the current wiki ===
156
Marta Girdea 28.2 157 The following variables (set in the [[xwikivars.vm>>http://svn.xwiki.org/svnroot/xwiki/platform/web/trunk/standard/src/main/webapp/templates/xwikivars.vm]] template) are shortcuts for checking various information **about the current wiki**:
Marta Girdea 28.1 158
159 * ##$isReadOnly##
160 * ##$isInServletMode##
161 * ##$isInPortletMode##
162
Vincent Massol 20.3 163 = Groovy Specific Information =
Vincent Massol 1.1 164
Caleb James DeLisle 20.1 165 Currently all non Velocity scripting languages are only allowed for administrators of a wiki (or users having the 'programming' right).
Vincent Massol 1.1 166
Vincent Massol 30.3 167 * See Groovy examples in the [[Extensions wiki>>extensions:Main.WebHome]]
Caleb James DeLisle 16.1 168 * [[Feeling Groovy>>http://www-128.ibm.com/developerworks/java/library/j-alj08034.html]]
169 * [[MVC programming with Groovy templates>>http://www-128.ibm.com/developerworks/java/library/j-pg02155/]]
Vincent Massol 1.1 170
Vincent Massol 20.3 171 == Groovy Example ==
gfiorentino 1.17 172
Caleb James DeLisle 18.1 173 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 174
Caleb James DeLisle 19.1 175 Using XWiki Syntax 2.0:
176 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 177
coffeemug13 31.1 178 {{code}}
179 {{velocity}}
Caleb James DeLisle 19.1 180 #set($hostname = "www.xwiki.org")
181 Host Name: $hostname
182 $xcontext.put("hostname", $hostname)
183 {{/velocity}}
184 {{groovy}}
185 import java.net.InetAddress;
186 host = xcontext.get("hostname");
187 InetAddress addr = InetAddress.getByName(host);
188 String address = addr.getHostAddress();
189 xcontext.put("address", address);
190 {{/groovy}}
191 {{velocity}}
192 IP Address: $xcontext.get("address")
coffeemug13 31.1 193 {{/velocity}}
194 {{/code}}
Caleb James DeLisle 19.1 195
Caleb James DeLisle 18.1 196 Using XWiki Syntax 1.0:
Caleb James DeLisle 19.1 197 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 198
coffeemug13 31.1 199 {{code}}
200 #set ($hostname = "www.xwiki.org")
Caleb James DeLisle 18.1 201 Host Name: $hostname
Vincent Massol 8.2 202 <%
203 import java.net.InetAddress;
204 vcontext = context.get("vcontext");
Caleb James DeLisle 18.1 205 host = vcontext.get("hostname");
206 InetAddress addr = InetAddress.getByName(host);
207 String address = addr.getHostAddress();
Vincent Massol 8.2 208 %>
coffeemug13 31.1 209 IP Address: $address
210 {{/code}}
Caleb James DeLisle 20.1 211
Caleb James DeLisle 26.1 212 = Python Specific Information =
Marta Girdea 28.1 213
Caleb James DeLisle 26.1 214 You can run python code in XWiki just like velocity or groovy.
Caleb James DeLisle 30.1 215
coffeemug13 31.1 216 {{code language="python"}}
217 {{python}}
Caleb James DeLisle 20.1 218 print "The full name of this document is " + doc.getFullName()
coffeemug13 31.1 219 {{/python}}
220 {{/code}}
Caleb James DeLisle 20.1 221
Caleb James DeLisle 26.1 222 {{warning}}
Vincent Massol 30.3 223 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 224 {{/warning}}
225
Vincent Massol 20.3 226 = Scripting In XWiki Syntax 1.0 =
Caleb James DeLisle 20.1 227
Vincent Massol 20.5 228 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 229
230 * The only scripting languages available to you are Velocity and Groovy.
231 * In Groovy, the context is known as: **##context##** not **##xcontext##**
Vincent Massol 30.3 232 * The beginning and end of Groovy scripts are denoted by <% and %> rather than through the [[extensions:Extension.Groovy Macro]] (using ~{~{groovy}} and ~{~{/groovy}})
233 * 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 234
235 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