前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >xwiki开发者指南-脚本API指南

xwiki开发者指南-脚本API指南

作者头像
lovelife110
发布2021-01-14 11:51:27
1.5K0
发布2021-01-14 11:51:27
举报
文章被收录于专栏:爱生活爱编程

本次指南覆盖main XWiki,可以在wiki页面通过脚本使用的API。这并不意味着全面。对于其他你需要查看XWiki参考API页面

请注意,虽然大多数的例子都用Velocity编写,但你可以使用任何其他脚本语言来访问相同的API。

查询文档

查看查询模块了解有关如何在wiki使用脚本语言执行查询的例子。

创建一个新文档

Velocity的例子:

## Note that getDocument() creates a Document if it doesn't exist. This can be checked with $newDoc.isNew()

#set ($newDoc = $xwiki.getDocument('MySpace.MyPage'))

## Set its content (for example)

#set ($discard = $newDoc.setContent("new content"))

## The second parameter to save() indicates whether the save is a minor edit or not

#set ($discard = $newDoc.save("some comment explaining the save", true)

访问请求

你可以通过request脚本变量访问com.xpn.xwiki.web.XWikiServletRequest对象来访问HTTP请求。

例如,在Velocity,访问请求中传递的action HTTP参数,可以这样写:

$request.action

请注意,这有一个快捷方式:

$request.get("action")

获取外部内容

你可以使用下面的API来获取位于外部URL的内容:

public String getURLContent(String surl, String username, String password) throws IOException

public String getURLContent(String surl) throws IOException

public String getURLContent(String surl, String username, String password, int timeout) throws IOException

public String getURLContent(String surl, int timeout) throws IOException

public byte[] getURLContentAsBytes(String surl, String username, String password)

public byte[] getURLContentAsBytes(String surl) throws IOException

Velocity例子:

$xwiki.getURLContent("http://google.com")

添加对象到页面

这里是Velocity脚本来显示如何在一个页面存储新的对象:

Create an object #set(obj = doc.newObject("XWiki.SomeClass")) obj.set("field1",$``$obj.set("field2",

"XWiki.SomeClass"类必须创建(通过类编辑):field1和field2是类是属性。目前,该代码只有在当前登录的用户中有该页面的编辑权限时可以正常生效,否则Document.save()将无效。

访问一个页面的对象

这里是Velocity脚本来显示它是如何访问附在页面的对象,并读取其字段:

## Retrieve the first object (index 0) among all objects attached to this page and of a certain class 

#set($obj = $doc.getObject('SomeSpace.SomeClass'))

## Retrieve the raw value of the propertty "field1" for this object, provided

## a property called "field1" is actually defined in the class of this object

#set($rawValue = $obj.getProperty('field1').value)

SomeSpace.SomeClass0 : field1 = "$rawValue"

## return the property type: String, TextAreaDate, Boolean, ...

$obj.get('field1').classType

你也可以在不知道它们各自的名字的情况下获得对象的所有属性。当你使用类向导创建一个类时,下面是默认Class Sheet的方式:

#set(class = obj.xWikiClass) ## access the class object representing SomeSpace.SomeClass #foreach(prop in class.properties) ## go through all properties <dt> *{prop.prettyName}* </dt> <dd>

实际上$doc.display(propertyName) 可以显示属性值或者生成页面输入字段,映射到其名称作为参数传递(当inline模式编辑页面)的属性。如果你有一个Velocity脚本,使用display(propertyName)方法来访问包含页面的一个对象的属性,你想在其他地方include,你必须使用includeForm() Velocity宏:

#includeForm("spacename.docname")

查看includeForm()宏了解更多信息。

从任何页面访问对象和在相同类遍历所有对象

这里是Velocity脚本来显示它是如何从另一个页面访问页面中的对象,并读取其字段:

(类似于先前的代码,除了你需要在$xwiki.getDocument之前"call"页面)

get the document which has the object (only one here) - this is the page where I can see things in the object editor ## Retrieve the first object (index 0) among all objects attached to the page MySpace.MyDocWithMyClassObjects and of a certain class MySpace.MyClass #set( MyDoc = xwiki.getDocument("MySpace.MyDocWithMyClassObjects")) ## get the document wich contains the class definition: this page has entries in the class editor #set( class = xwiki.getClass("MySpace.MyClass")) #foreach(prop in class.properties) ## go through all properties * {prop.prettyName} : MyDoc.display(

如果MySpace.MyDocWithMyClassObjects有许多MySpace.MyClass类的对象(不一样的值)

## if you have more than one object on a page, you will have to loop over them and use "$doc.use"

#set($MyDoc = $xwiki.getDocument("MySpace.MyDocWithMyClassObjects"))

#set($class = $xwiki.getClass("MySpace.MyClass"))

## loop over all objects

#foreach($obj in $MyDoc.getObjects("MySpace.MyClass"))

  * Object number $velocityCount

 #set($discard = $MyDoc.use($obj))

 #foreach($prop in $class.properties) ## go through all properties

    ** ${prop.prettyName} : $MyDoc.display($prop.getName())

 #end

#end

在其他Velocity页面引用一个Velocity页面

查看Velocity引用教程

重定向到另一个页面

例如:当一个页面已经被删除,你要让旧的页面重定向到新的页面。

例子:

response.sendRedirect( xwiki.getURL("Space.Page"))

有关更多示例,请参阅重定向Snippet.

为页面添加附件

Velocity例子:

{{velocity}}

#set($content = "This is some small arbitrary content")

#set($discard = $doc.addAttachment("myfile.txt", $content.getBytes()))

#set($discard = $doc.save("some comment"))

{{/velocity}}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 查询文档
  • 创建一个新文档
  • 访问请求
  • 获取外部内容
  • 添加对象到页面
    • Create an object #set(obj = doc.newObject("XWiki.SomeClass")) obj.set("field1",$``$obj.set("field2",
    • 访问一个页面的对象
    • 从任何页面访问对象和在相同类遍历所有对象
      • get the document which has the object (only one here) - this is the page where I can see things in the object editor ## Retrieve the first object (index 0) among all objects attached to the page MySpace.MyDocWithMyClassObjects and of a certain class MySpace.MyClass #set( MyDoc = xwiki.getDocument("MySpace.MyDocWithMyClassObjects")) ## get the document wich contains the class definition: this page has entries in the class editor #set( class = xwiki.getClass("MySpace.MyClass")) #foreach(prop in class.properties) ## go through all properties * {prop.prettyName} : MyDoc.display(
      • 在其他Velocity页面引用一个Velocity页面
      • 重定向到另一个页面
      • 为页面添加附件
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档