Wiki source code of Scripting

Version 35.4 by Eduard Moraru on 2012/03/14

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
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.
6
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:
8
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)
14
15 = XWiki Scripting API =
16
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.
18
19 == [[Bindings>>extensions:Extension.Script Macro#HBindings]] ==
20
21 These objects are available to you in scripting languages.
22
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##**
28 * [[The XWiki utils>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Util.html]]: **##util##**
29
30 == [[XWiki Component>>extensions:Extension.Component Module]] Access ==
31
32 You can also gain direct access to XWiki components using the following code snippet (also see: [[Accessing components from Groovy>>DevGuide.WritingComponents#HAccessingacomponentfromgroovy]]):
33
34 {{info}}
35 This snippet is written in Groovy and will have to be converted to your scripting language.
36 {{/info}}
37
38 {{code language="java"}}
39 {{groovy}}
40 def greeter = com.xpn.xwiki.web.Utils.getComponent(org.xwiki.component.HelloWorld.class);
41 println greeter.sayHello();
42 {{/groovy}}
43 {{/code}}
44
45 == XWiki Core Access ==
46
47 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.
48
49 {{code language="java"}}
50 {{groovy}}
51 def xc = xcontext.getContext();
52 def wiki = xc.getWiki();
53 def xdoc = doc.getDocument();
54 {{/groovy}}
55 {{/code}}
56
57 After using this snippet, you will have 3 new objects:
58
59 * [[The underlying XWikiContext behind the Context object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/XWikiContext.html]]: **##xc##**
60 * [[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##**
61 * [[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##**
62
63 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##**.
64
65 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.
66
67 == Querying XWiki's Model ==
68
69 From your script you can query the full XWiki's Model. Check the [[Query Guide>>QueryGuide]] for more information.
70
71 {{id name="velocity"/}}
72
73 = Velocity Specific Information =
74
75 Velocity is 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.
76
77 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).
78
79 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)
80
81 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]].
82
83 The following Velocity tools are also available in addition to the bindings.
84
85 * [[List Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/ListTool.html]]: **##$listtool##**
86 * [[Number Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/NumberTool.html]]: **##$numbertool##**
87 * [[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##**
88 * [[Math Tool>>http://velocity.apache.org/tools/releases/1.4/generic/MathTool.html]]: **##$mathtool##**
89 * [[Escape Tool>>http://velocity.apache.org/tools/releases/1.4/generic/EscapeTool.html]]: **##$escapetool##**
90 * [[Sort Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/SortTool.html]]: **##$sorttool##**
91 * 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]].
92 * [[String Tool>>http://commons.apache.org/lang/api/org/apache/commons/lang3/StringUtils.html]]: **##$stringtool##** (since 3.4M1)
93
94 {{info}}
95 If you wish to add new Velocity tools you'll need to edit your ##xwiki.properties## file and follow the instructions in there.
96 {{/info}}
97
98 To include Velocity scripts in other Velocity scripts, see [[How to include a velocity page into another page>>DevGuide.IncludeInVelocity]].
99
100 == Other Velocity Variables ==
101
102 {{warning}}
103 These variables can be used but are subject to change in the future.
104 {{/warning}}
105
106 {{id name="HControllingwhethertodisplayCommentsHistoryAttachmentInformationsectionsornot"/}}
107
108 === Controlling Which Sections to Display ===
109
110 You can control whether to display Comments/History/Attachment/Information sections or not by setting some velocity variable to "no":
111
112 {{code language="velocity"}}
113 #set ($showcomments = "no")
114 #set ($showattachments = "no")
115 #set ($showhistory = "no")
116 #set ($showinformation = "no")
117 {{/code}}
118
119 To remove them all you can set:
120
121 {{code language="velocity"}}
122 #set($docextras = [])
123 {{/code}}
124
125 === Information about the current user ===
126
127 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**:
128
129 * ##$isGuest##: checks if the current user is ##XWiki.XWikiGuest##
130 * ##$isSuperAdmin##: checks if the current user is the special user ##superadmin##
131
132 * ##$hasComment##: checks comment rights on the current document
133 * ##$hasEdit##: checks edit rights on the current document
134 * ##$hasWatch##: checks if the user is authenticated and the watch service is available
135
136 * ##$hasAdmin##: checks admin rights on the current document
137 * ##$hasSpaceAdmin##: checks admin rights on the ##XWikiPreferences## document of the current space
138 * ##$hasGlobalAdmin##: checks admin rights on ##XWiki.XWikiPreferences##
139
140 * ##$hasCreateSpace##: checks edit rights on that page that does not exist, in a space that doesn't exist
141 * ##$hasCreatePage##: checks edit rights on that page that does not exist, in the current space
142
143 * ##$hasProgramming##: checks if the current user has programming rights
144
145 * ##$isAdvancedUser##: advanced users: ##superadmin##, users with the ##usertype## property set to "Advanced", guest users with admin rights
146
147 Example:
148
149 {{code language="velocity"}}
150 {{velocity}}
151 #if ($hasAdmin)
152 ## This link will only be visible to users that have admin rights on this document
153 [[Do some admin action>>Some.Document]]
154 #end
155 {{/velocity}}
156 {{/code}}
157
158 === Information about the current wiki ===
159
160 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**:
161
162 * ##$isReadOnly##
163 * ##$isInServletMode##
164 * ##$isInPortletMode##
165
166 = Groovy Specific Information =
167
168 {{info}}
169 Currently all non Velocity scripting languages are only allowed users having Programming Rights.
170 {{/info}}
171
172 * See Groovy snippets in the [[Extensions wiki>>extensions:Main.WebHome]] (click on the "Groovy" tag in the Tag Cloud)
173 * [[Groovy web site>>http://groovy.codehaus.org/]]
174
175 == Groovy Example ==
176
177 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##.
178
179 **Using XWiki Syntax 2.0:**
180
181 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.
182
183 {{code language="velocity"}}
184 {{velocity}}
185 #set($hostname = "www.xwiki.org")
186 Host Name: $hostname
187 $xcontext.put("hostname", $hostname)
188 {{/velocity}}
189 {{groovy}}
190 import java.net.InetAddress;
191 host = xcontext.get("hostname");
192 InetAddress addr = InetAddress.getByName(host);
193 String address = addr.getHostAddress();
194 xcontext.put("address", address);
195 {{/groovy}}
196 {{velocity}}
197 IP Address: $xcontext.get("address")
198 {{/velocity}}
199 {{/code}}
200
201 **Using XWiki Syntax 1.0:**
202
203 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.
204
205 {{code language="velocity"}}
206 #set ($hostname = "www.xwiki.org")
207 Host Name: $hostname
208 <%
209 import java.net.InetAddress;
210 vcontext = context.get("vcontext");
211 host = vcontext.get("hostname");
212 InetAddress addr = InetAddress.getByName(host);
213 String address = addr.getHostAddress();
214 %>
215 IP Address: $address
216 {{/code}}
217
218 = Python Specific Information =
219
220 You can run Python code in XWiki just like Velocity or Groovy.
221
222 {{code language="python"}}
223 {{python}}
224 print "The full name of this document is " + doc.getFullName()
225 {{/python}}
226 {{/code}}
227
228 {{warning}}
229 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]]
230 {{/warning}}
231
232 = Scripting In XWiki Syntax 1.0 =
233
234 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.
235
236 * The only scripting languages available to you are Velocity and Groovy.
237 * In Groovy, the context is known as: **##context##** not **##xcontext##**
238 * The beginning and end of Groovy scripts are denoted by <% and %> rather than through the [[extensions:Extension.Groovy Macro]] (using ~{~{groovy}} and ~{~{/groovy}})
239 * Velocity is parsed in a page no matter what (there is no need to invoke the [[extensions:Extension.Velocity Macro]] using ~{~{velocity}} and ~{~{/velocity}})
240
241 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