List all objects

Last modified by Manuel Leduc on 2023/10/10 16:58

cogList all objects in the current wiki
TypeSnippet
Category
Developed by

Vincent Massol

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

This script displays all objects (a.k.a XObjects) present in the current wiki, using a LiveTable.

objects.png

To use it:

  • Make sure you have Programming Rights
  • Create a page (e.g. Objects.WebHome) with the following content:
    {{velocity}}
    #set($columns = [ "object"  , "page", "date" ])
    #set($columnsProperties = {
        "object" : { "type" : "text"},
        "page" : { "type" : "text"},
        "date" : { "type" : "date"}
    })

    #set($options = {
        "resultPage":"Objects.JSON"
    })

    #livetable("pbjects" $columns $columnsProperties $options)
    {{/velocity}}
  • Create a second page, the page that generates the JSON to feed the livetable (e.g. Objects.JSON - if you use a different name, adjust the reference in the first page):
    {{velocity wiki="false"}}
    #if("$!{request.xpage}" == 'plain')
      $response.setContentType('application/json')
    #end
    ##==============================
    ## Offset = item # at which to start displaying data
    ##==============================
    #set($offset = 0)
    #if ("$!request.offset" != "")
      #set($offset = $mathtool.toInteger($request.get('offset')))
      ## offset starts from 0 in velocity and 1 in javascript
      #set($offset = $offset - 1)
      #if($offset < 0)
        #set($offset = 0)
      #end
    #end
    ##==================
    ## Limit = # of items to display
    ##==================
    #set($limit = $mathtool.toInteger($request.get('limit')))
    ##==========
    ## Sort direction
    ##==========
    #set($order = "$!request.sort")
    #set($sortClause = "")
    #if($order != '')
      #set($orderDirection = "$!{request.get('dir').toLowerCase()}")
      #if("$!orderDirection" != '' && "$!orderDirection" != 'asc')
        #set($orderDirection = 'desc')
      #end
      #if ($order == "object")
        #set ($sortClause = "order by obj.className $orderDirection")
      #elseif ($order == "page")
        #set ($sortClause = "order by obj.name $orderDirection")
      #elseif ($order == "date")
        #set ($sortClause = "order by doc.date $orderDirection")
      #end
    #end
    ## =======
    ## Filter Data
    ## =======
    #set ($objectFilter = "%$!{request.get('object')}%")
    #set ($pageFilter = "%$!{request.get('page')}%")
    ## Handle hidden documents. Note that we cannot use the "hidden" filter since it seems it works only on short form queries or queries on Documents
    #set ($hiddenClause = '')
    #if($xwiki.getDocument($xcontext.user).getValue('displayHiddenDocuments') != 1)
      #set ($hiddenClause = "and (doc.hidden <> true or doc.hidden is null)")
    #end
    ## Compute the total number of entries
    #set ($count = $services.query.xwql("select count(obj.id) from BaseObject as obj, Document as doc where obj.className like :objectFilter and obj.name like :pageFilter and obj.name  = doc.fullName $hiddenClause").bindValue("objectFilter", $objectFilter).bindValue("pageFilter", $pageFilter).execute())
    ## Compute the entries to display
    #set ($result = $services.query.xwql("select obj.className, obj.name, doc.date from BaseObject as obj, Document as doc where obj.className like :objectFilter and obj.name like :pageFilter and obj.name  = doc.fullName $hiddenClause $sortClause").bindValue("objectFilter", $objectFilter).bindValue("pageFilter", $pageFilter).setLimit($limit).setOffset($offset).execute())
    ## =============
    ## Generate the JSON
    ## =============
    {
    "totalrows": $count[0],
    "matchingtags": {},
    "tags" : [],
    "returnedrows":  $result.size(),
    "offset": $offset,
    "reqNo": $mathtool.toInteger($request.reqNo),
    "rows": [
    #foreach($item in $result)
    {
     "doc_viewable" : true,
     "object" : "$item[0]",
     "page" : "$item[1]",
     "date" : "$xwiki.formatDate($item[2])"
    }#if( $foreach.hasNext ),#end
    #end
    ]
    }
    {{/velocity}}

Get Connected