Wiki source code of Scripting

Version 23.3 by Vincent Massol on 2010/06/21

Hide last authors
Caleb James DeLisle 16.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
Caleb James DeLisle 23.1 2
Vincent Massol 20.4 3 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 4
Vincent Massol 20.4 5 XWiki integrates [[jsr-223>>http://scripting.dev.java.net/]] scripting. You can script using several available languages by using one of the following macros:
6 * [[Velocity Macro>>code:Macros.VelocityMacro]] (installed by default in XWiki Enterprise)
7 * [[Groovy Macro>>code:Macros.GroovyMacro]] (installed by default in XWiki Enterprise)
8 * [[Python Macro>>code:Macros.PythonMacro]] (installed by default in XWiki Enterprise)
9 * [[Ruby Macro>>code:Macros.RubyMacro]] (not installed by default in XWiki Enterprise)
10 * [[PHP Macro>>code:Macros.PHPMacro]] (not installed by default in XWiki Enterprise)
11
Vincent Massol 20.3 12 = XWiki Scripting API =
Caleb James DeLisle 20.1 13
14 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 [[Code Zone>>code:Main.WebHome]] area to see how others have figured out how to achieve a variety of results.
15
Vincent Massol 20.3 16 == [[Bindings>>code:Macros.ScriptMacro#HBindings]] ==
Caleb James DeLisle 20.1 17
18 These objects are available to you in scripting languages.
19 * [[The current Document>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Document.html]]: **##doc##**
20 * [[The Context of the request>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Context.html]]: **##xcontext##**
21 * [[The Request object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiRequest.html]]: **##request##**
22 * [[The Response object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/web/XWikiResponse.html]]: **##response##**
23 * [[The XWiki object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/XWiki.html]]: **##xwiki##**
24
Vincent Massol 20.3 25 == [[XWiki Component>>code:Modules.ComponentModule]] Access ==
Caleb James DeLisle 20.1 26
27 You can also gain direct access to XWiki components using the following code snippet:
28 Also see: [[Accessing components from Groovy>>DevGuide.WritingComponents#HAccessingacomponentfromgroovy]]
29 Note: This snippet is written in Groovy and will have to be converted to your scripting language.
30 {{code language=java}}
31 {{groovy}}
32 def greeter = com.xpn.xwiki.web.Utils.getComponent(org.xwiki.component.HelloWorld.class);
33 println greeter.sayHello();
34 {{/groovy}}
35 {{/code}}
36
Vincent Massol 20.3 37 == XWiki Core Access ==
Caleb James DeLisle 20.1 38
Caleb James DeLisle 22.2 39 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 20.1 40 {{code language=java}}
41 {{groovy}}
42 def xc = xcontext.getContext();
43 def wiki = xc.getWiki();
44 def xdoc = doc.getDocument();
45 {{/groovy}}
46 {{/code}}
47 After using this snippet, you will have 3 new objects:
48 * [[The underlying XWikiContext behind the Context object>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/XWikiContext.html]]: **##xc##**
49 * [[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##**
50 * [[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##**
51
Caleb James DeLisle 22.2 52 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##**.
53
Caleb James DeLisle 22.1 54 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 55
Vincent Massol 23.2 56 == Querying XWiki's Model ==
57
58 From your script you can query the full XWiki's Model. Check the [[Query Guide>>QueryGuide]] for more information.
59
Caleb James DeLisle 16.2 60 {{id name=velocity /}}
Vincent Massol 20.3 61 = Velocity Specific Information =
Vincent Massol 1.1 62
Caleb James DeLisle 20.1 63 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.
64 You can [[gain access to the XWiki core>>#HXWikiCoreAccess]] from Velocity but this will require programming rights.
Vincent Massol 1.1 65
Caleb James DeLisle 20.1 66 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 67
Caleb James DeLisle 20.1 68 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 69
Caleb James DeLisle 20.1 70 The following Velocity tools are also available in addition to the bindings.
Caleb James DeLisle 16.1 71 * [[List Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/ListTool.html]]: **##$listtool##**
72 * [[Number Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/NumberTool.html]]: **##$numbertool##**
73 * [[Date Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/DateTool.html]]: **##$datetool##**
74 * [[Math Tool>>http://velocity.apache.org/tools/releases/1.4/generic/MathTool.html]]: **##$mathtool##**
75 * [[Escape Tool>>http://velocity.apache.org/tools/releases/1.4/generic/EscapeTool.html]]: **##$escapetool##**
76 * [[Sort Tool>>http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/SortTool.html]]: **##$sorttool##**
Vincent Massol 21.2 77 * 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 78
Caleb James DeLisle 16.1 79 {{info}}If you wish to add new Velocity tools you'll need to edit your ##xwiki.properties## file and follow the instructions in there.{{/info}}
Vincent Massol 9.2 80
Caleb James DeLisle 16.1 81 To include Velocity scripts in other Velocity scripts, see [[How to include a velocity page into another page>>DevGuide.IncludeInVelocity]].
jeanvivienmaurice 1.20 82
Vincent Massol 20.3 83 == Other Velocity Variables ==
Vincent Massol 13.1 84
Caleb James DeLisle 16.1 85 {{info}}These variables can be used but are subject to change in the future.{{/info}}
Vincent Massol 13.1 86
Caleb James DeLisle 20.1 87 {{id name=HControllingwhethertodisplayCommentsHistoryAttachmentInformationsectionsornot /}}
Vincent Massol 20.3 88 === Controlling Which Sections to Display ===
Vincent Massol 13.1 89
Caleb James DeLisle 20.1 90 You can control whether to display Comments/History/Attachment/Information sections or not by setting some velocity variable to "no":
Vincent Massol 13.1 91
Caleb James DeLisle 16.1 92 {{code}}
Vincent Massol 13.1 93 #set ($showcomments = "no")
94 #set ($showattachments = "no")
95 #set ($showhistory = "no")
96 #set ($showinformation = "no")
Caleb James DeLisle 16.1 97 {{/code}}
Vincent Massol 13.1 98
99 To remove them all you can set:
100
Caleb James DeLisle 16.1 101 {{code}}
Vincent Massol 13.1 102 #set($docextras = [])
Caleb James DeLisle 16.1 103 {{/code}}
Vincent Massol 13.1 104
Vincent Massol 20.3 105 = Groovy Specific Information =
Vincent Massol 1.1 106
Caleb James DeLisle 20.1 107 Currently all non Velocity scripting languages are only allowed for administrators of a wiki (or users having the 'programming' right).
Vincent Massol 1.1 108
Caleb James DeLisle 16.1 109 * See Groovy examples in the [[Code Zone>>code:Main.WebHome]], more specifically in the [[Code Snippets area>>code:Snippets.WebHome]]
110 * [[Feeling Groovy>>http://www-128.ibm.com/developerworks/java/library/j-alj08034.html]]
111 * [[MVC programming with Groovy templates>>http://www-128.ibm.com/developerworks/java/library/j-pg02155/]]
Vincent Massol 1.1 112
Vincent Massol 20.3 113 == Groovy Example ==
gfiorentino 1.17 114
Caleb James DeLisle 18.1 115 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 116
Caleb James DeLisle 19.1 117 Using XWiki Syntax 2.0:
118 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.
119 {{code}}
120 {{velocity}}
121 #set($hostname = "www.xwiki.org")
122 Host Name: $hostname
123 $xcontext.put("hostname", $hostname)
124 {{/velocity}}
125 {{groovy}}
126 import java.net.InetAddress;
127 host = xcontext.get("hostname");
128 InetAddress addr = InetAddress.getByName(host);
129 String address = addr.getHostAddress();
130 xcontext.put("address", address);
131 {{/groovy}}
132 {{velocity}}
133 IP Address: $xcontext.get("address")
134 {{/velocity}}
135 {{/code}}
136
Caleb James DeLisle 18.1 137 Using XWiki Syntax 1.0:
Caleb James DeLisle 19.1 138 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 16.1 139 {{code}}
Caleb James DeLisle 18.1 140 #set ($hostname = "www.xwiki.org")
141 Host Name: $hostname
Vincent Massol 8.2 142 <%
143 import java.net.InetAddress;
144 vcontext = context.get("vcontext");
Caleb James DeLisle 18.1 145 host = vcontext.get("hostname");
146 InetAddress addr = InetAddress.getByName(host);
147 String address = addr.getHostAddress();
Vincent Massol 8.2 148 %>
Caleb James DeLisle 18.1 149 IP Address: $address
Caleb James DeLisle 16.1 150 {{/code}}
Caleb James DeLisle 20.1 151
Vincent Massol 20.3 152 = Python Specific Information =
Caleb James DeLisle 20.1 153
154 In Python, you have all of the powers available in Groovy but the default bindings are not available due to a [[bug in Jython>>http://bugs.jython.org/issue1426]] There is a workaround snippet which manually loads the context, document, wiki object, request and response available [[here>>code:Snippets.AccessToBindingsInPythonSnippet]].
155
156 Example Snippet:
157 {{code language=python}}
158 {{python}}
159 import com.xpn.xwiki.web.Utils as Utils
160 import org.xwiki.context.Execution as Execution
161 import com.xpn.xwiki.api.Context as Context
162 import com.xpn.xwiki.api.Document as Document
163 xcontext = Context(Utils.getComponent(Execution).getContext().getProperty("xwikicontext"))
164 doc = Document(xcontext.getDoc(), xcontext.getContext())
165 print "The full name of this document is " + doc.getFullName()
166 {{/python}}
167 {{/code}}
168
Vincent Massol 20.3 169 = Scripting In XWiki Syntax 1.0 =
Caleb James DeLisle 20.1 170
Vincent Massol 20.5 171 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 172
173 * The only scripting languages available to you are Velocity and Groovy.
174 * In Groovy, the context is known as: **##context##** not **##xcontext##**
175 * The beginning and end of Groovy scripts are denoted by <% and %> rather than through the [[code:Macros.GroovyMacro]] (using ~{{groovy}} and ~{{/groovy}})
176 * Velocity is parsed in a page no matter what (there is no need to invoke the [[code:Macros.VelocityMacro]] using ~{{velocity}} and ~{{/velocity}})
177
178 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