HQL Scripting Examples in Velocity
HQL Query Examples in Velocity
XWiki allows user to access documents and objects with HQL queries in Velocity scripts.
Public API (searchDocuments)
General example showing how to display the first 5 results of a given query:
#set($results = $xwiki.searchDocuments($hql, 5, 0))
#foreach ($item in $results)
* $item
#end
The examples below will show you various HQL queries that you can write.
Simple Query
Displays all documents who have been created by the user XWiki.JohnDoe:
Ordered Query
Displays all documents who have been created by the user XWiki.JohnDoe and sorted by document's last modification date, in ascending order:
Advanced Query (date & time)
Query documents older than 3 days
The following snippet deletes all documents in the space TempDocs that are older than 3 days.
For the computation of the relative age, a calendar object created by the $datetool is used and passed to the hibernate query. Tested with MySQL only.
#set($cal = $datetool.systemCalendar)$cal.add(7,-3)
#set($olderTempDocs=$xwiki.search($hql,5000, 0, ["TempdDocs",$datetool.toDate($cal)]))
#foreach($x in $olderTempDocs)$xwiki.getDocument($x).deleteWithProgrammingRights()#end
Other examples:
- Listing all documents modified during the current day: where year(doc.date) = year(current_date()) and month(doc.date) = month(current_date()) and day(doc.date) > (day(current_date()) - 1) order by doc.date desc
- Listing all documents modified during the current week: where year(doc.date) = year(current_date()) and month(doc.date) = month(current_date()) and day(doc.date) > (day(current_date()) - 7) order by doc.date desc
- Listing all documents modified during the current month: where year(doc.date) = year(current_date()) and month(doc.date) > (month(current_date()) - 1) order by doc.date desc
Privileged API (search)
General example showing how to display the first 5 results of a given query:
#set($results = $xwiki.search($hql, 5, 0))
#foreach ($item in $results)
* $item
#end
The examples below will show you various HQL queries that you can write.
Simple Query
Count Query
## Since $xwiki.search is returning a list, we get its first element
Count : $results.get(0)
Simple Query with multiple fields
#foreach ($row in $results)
#foreach ($col in $row)
#if ($velocityCount == 1)
#set($docName = $col)
#elseif ($velocityCount == 2)
#set($docDate = $col)
#end
#end
$docName : $docDate <br/>
#end
Getting objects of a specific class
Getting objects properties
Getting documents where objects properties equals some value
If property is multiselect Page type, relational storage must be checked, if not, the content is not stored the same way and we cannot do "join prop.list as list"
List users currently editing pages
List attachments of a page
Statistics
- Most Viewed Articles Snippet
- Most Active Contributors In Group Snippet
- Number Of Active Users Per Day And Per Week Snippet
- Number Of Edited Articles Per Day And Per Week Snippet
- Number Of Created Articles Per Day And Per Week Snippet
- Number Of Deleted Articles Per Day And Per Week Snippet
Non-exhaustive list of queryable Object Fields
The full list of available fields can be found in the Hibernate mapping files (*.hbm.xml), which can be found inside WEB-INF/lib/xwiki-core-x.y.jar (where x.y is the XWiki version).
XWikiDocument
- XWikiDocument.fullName : full name, including space and page name. Example value: Main.WebHome
- XWikiDocument.author : last editor. Example value: XWiki.Admin
- XWikiDocument.creator : first editor. Example value: XWiki.Admin
BaseObject
- BaseObject.id : arbitrary unique id of the object. Example value: 123456789
- BaseObject.className : class. Example value: XWiki.XWikiUsers
*Property (StringProperty, IntegerProperty, etc)
- Property.id.id : unique id of the object the property belongs to. Example value: 123456789
- Property.name : name of the property. Example value: first_name
- Property.value : value. Example value: John