Wiki source code of Scripting

Version 21.2 by Vincent Massol on 2010/02/15

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