Wiki-Quellcode von CommonCode
Version 1.1 von Daniel Herrmann am 2025/06/22 19:09
Zeige letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity output="false"}} | ||
| 2 | |||
| 3 | ## See https://solr.apache.org/guide/solr/latest/query-guide/standard-query-parser.html#escaping-special-characters | ||
| 4 | ## And https://jira.xwiki.org/browse/XCOMMONS-2926 | ||
| 5 | #set ($solrSpecialChars = ['+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/', '\', ' ']) | ||
| 6 | #set ($escapedSolrSpecialChars = ['\+', '\-', '\&&', '\||', '\!', '\(', '\)', '\{', '\}', '\[', '\]', '\^', '\"', '\~', '\*', '\?', '\:', '\/', '\\', '\ ']) | ||
| 7 | |||
| 8 | #macro (escapeSolr $v) | ||
| 9 | $stringtool.replaceEach($v, $solrSpecialChars, $escapedSolrSpecialChars)## | ||
| 10 | #end | ||
| 11 | |||
| 12 | #macro (getSpaceFacet $space) | ||
| 13 | #set ($facetNumber = $space.getReversedReferenceChain().size() - 1) | ||
| 14 | #if ($space.getRoot().getType().toString() == "WIKI") | ||
| 15 | #set ($facetNumber = $facetNumber - 1) | ||
| 16 | #escapeSolr("${facetNumber}/${services.model.serialize($space)}.")## | ||
| 17 | #end | ||
| 18 | #end | ||
| 19 | |||
| 20 | #macro (getCQLMacroResults $wikimacro $results) | ||
| 21 | #set ($results = $NULL) | ||
| 22 | #set ($cql = $wikimacro.params.cql) | ||
| 23 | #if ("$!cql" == "") | ||
| 24 | #if ("$!xcontext.macro.params.operator" == "AND" || "$!xcontext.macro.params.operator" == "and") | ||
| 25 | #set ($operator = "AND") | ||
| 26 | #else | ||
| 27 | #set ($operator = "OR") | ||
| 28 | #end | ||
| 29 | #set ($labels = $wikimacro.params.labels) | ||
| 30 | #if ("$!labels" == "") | ||
| 31 | #set ($labels = $wikimacro.params.label) | ||
| 32 | #end | ||
| 33 | #if ("$!labels" != "") | ||
| 34 | #set ($labelList = $labels.split("\s*,\s*|\s+")) | ||
| 35 | #set ($stringifiedLabels = []) | ||
| 36 | #set ($cql = "") | ||
| 37 | #foreach ($label in $labelList) | ||
| 38 | #if ($foreach.first) | ||
| 39 | #set ($join = "") | ||
| 40 | #else | ||
| 41 | #set ($join = " $operator ") | ||
| 42 | #end | ||
| 43 | #set ($cql = "${cql}${join}label = ${jsontool.serialize($label)}") | ||
| 44 | #end | ||
| 45 | #set ($cql = "(${cql})") | ||
| 46 | #set ($type = $wikimacro.params.type) | ||
| 47 | #if ("$!type" != "") | ||
| 48 | #set ($cql = "$cql AND type = ${jsontool.serialize($type)}") | ||
| 49 | #end | ||
| 50 | #end | ||
| 51 | #end | ||
| 52 | #set ($query = $services.query.createQuery($cql, "cql")) | ||
| 53 | #set ($fq = "") | ||
| 54 | #if ("$!xcontext.macro.params.cql" == "") | ||
| 55 | #set ($spaces = "$!xcontext.macro.params.spaces") | ||
| 56 | #if (!$spaces.isEmpty() && !$spaces.contains("@all")) | ||
| 57 | #foreach ($space in $spaces.split("\s*,\s*|\s+")) | ||
| 58 | #set ($s = $services.confluence.spaces.getSloppySpace($space)) | ||
| 59 | #if ($objecttool.isNotNull($s)) | ||
| 60 | #set ($fq = "$fq space_facet:#getSpaceFacet($s)") | ||
| 61 | #end | ||
| 62 | #end | ||
| 63 | #end | ||
| 64 | #end | ||
| 65 | #if ("$!type" == "") | ||
| 66 | #set ($fq = "$fq type:DOCUMENT") | ||
| 67 | #end | ||
| 68 | #if (!$fq.trim().isEmpty()) | ||
| 69 | #set ($discard = $query.bindValue("fq", $fq.trim())) | ||
| 70 | #end | ||
| 71 | ## Max value. By default, Solr limits to 10 results, | ||
| 72 | ## which doesn't correspond to the behavior of Confluence. | ||
| 73 | ## INT_MAX causes huge performance issues, so we limit to 1000 | ||
| 74 | ## arbritrarily by default | ||
| 75 | #set ($discard = $query.setLimit(1000)) | ||
| 76 | #if ("$!xcontext.macro.params.max" != "") | ||
| 77 | #set ($limit = $numbertool.toNumber($wikimacro.params.max)) | ||
| 78 | #if ($objecttool.isNotNull($limit)) | ||
| 79 | #set ($discard = $query.setLimit($limit)) | ||
| 80 | #end | ||
| 81 | #end | ||
| 82 | #if ("$!xcontext.macro.params.sort" == "modified") | ||
| 83 | #set ($sortField = "date") | ||
| 84 | #elseif ("$!xcontext.macro.params.sort" == "creation") | ||
| 85 | #set ($sortField = "creationdate") | ||
| 86 | #else | ||
| 87 | #set ($sortField = "title_sort") | ||
| 88 | #end | ||
| 89 | #if ("$!xcontext.macro.params.reverse" == "true") | ||
| 90 | #set ($sortAsc = "desc") | ||
| 91 | #else | ||
| 92 | #set ($sortAsc = "asc") | ||
| 93 | #end | ||
| 94 | #set ($discard = $query.bindValue("sort", "$sortField $sortAsc")) | ||
| 95 | #set ($resultList = []) | ||
| 96 | #set ($alreadyListed = $collectiontool.getSet()) | ||
| 97 | #foreach ($r in $query.execute()[0].results) | ||
| 98 | #set ($ref = "$r.wiki:$r.fullname") | ||
| 99 | #if (!$alreadyListed.contains($ref)) | ||
| 100 | #set ($discard = $resultList.add($r)) | ||
| 101 | #set ($discard = $alreadyListed.add($ref)) | ||
| 102 | #end | ||
| 103 | #end | ||
| 104 | #setVariable("$results" $resultList) | ||
| 105 | #end | ||
| 106 | {{/velocity}} |