Script Component

Last modified by Admin on 2024/07/08 00:01

brick_addVery easily register fast components defined using scripts
Recommended
TypeJAR
Category
Developed by

Thomas Mortagne

Active Installs23
Rating
1 Votes
LicenseGNU Lesser General Public License 2.1

Installable with the Extension Manager

Description

This tool allows registering components in a wiki page by defining a class in script and standard XWiki component annotations like you would in Java.

To do that you need to:

  • add a XWiki.ScriptComponentClass object to a page
  • select the script language you are going to use (default choices currently are Groovy or Python)
  • choose where it's going to be registered (for the current whole wiki, only for the author, for the whole farm)
  • write the component and indicate the role, hint and dependency using the Java annotations (@Component, @Named, @Inject)
  • save and your component is automatically registered

Note that the author of the script needs programming right to use this feature.

object.png

Sample code:

package org.xwiki.testcomponent.script;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.script.service.ScriptService;

@Component
@Named("testcomponent")
@Singleton
public class TestComponentScriptService implements ScriptService
{
 public String get()
 {
   return "toto";
 }
}

That you can then call from a Velocity script using:

{{velocity}}
$services.testcomponent.get()
{{/velocity}}

See Component Module for more details about XWiki Components. See also WritingComponents for more information about script services.

Note that this approach differs from Wiki Components as it lets you describe and implement the whole component in a single script and use standard Java annotations to describe it instead of objects. This also makes the component a lot faster because it’s compiled in Java while wiki components need to execute wiki content every time a method of the component is called.

Use it in an extension

If you plan to use this feature in an extension you should add the module in your extension dependencies.

In Maven, you can add the following to your pom.xml file:

    <dependency>
     <groupId>org.xwiki.contrib</groupId>
     <artifactId>scriptcomponent</artifactId>
     <version>1.1.1</version>
   </dependency>

Examples

Event listener in Groovy

In order to write an event listener as a script component, just write your event listener in the same way in which you would write it in Java (see more examples and information in Local Observation Module).

For example, for a listener that would update the title of all pages on save, you can use this code:

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.xwiki.bridge.event.DocumentCreatingEvent;
import org.xwiki.bridge.event.DocumentUpdatingEvent;
import org.xwiki.component.annotation.Component;
import org.xwiki.observation.AbstractEventListener;
import org.xwiki.observation.event.Event;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;

@Component
@Named(TitleScriptListener.LISTENER_NAME)
@Singleton
public class TitleScriptListener extends AbstractEventListener
{
   final static String LISTENER_NAME = "titleScript";

   @Inject
   protected Logger logger;

   public TitleScriptListener()
   {
       super(LISTENER_NAME, new DocumentUpdatingEvent(), new DocumentCreatingEvent());
   }

   @Override
   public void onEvent(Event event, Object source, Object data)
   {
        logger.debug("Event: [{}] - Source: [{}]", LISTENER_NAME, event, source);

        XWikiContext xcontext = (XWikiContext) data;
        XWiki xwiki = xcontext.getWiki();
        XWikiDocument doc = (XWikiDocument) source;
        logger.debug("Title of the modified document is: " + doc.getTitle());
       // modify the title of the page before saving
       doc.setTitle(doc.getTitle() + " - updated");
   }
}

REST API endpoint in Groovy

TODO:

Prerequisites & Installation Instructions

We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager).

You can also use the manual method which involves dropping the JAR file and all its dependencies into the WEB-INF/lib folder and restarting XWiki.

Release Notes

v1.1.1

v1.1

v1.0

First version

Dependencies

Dependencies for this extension (org.xwiki.contrib:scriptcomponent 1.1.1):

Get Connected