Wiki source code of api
Version 4.1 by Ophion Admin on 2026/10/06 16:02
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | {{velocity}} |
| |
2.1 | 2 | #set ($appSpace = 'api') |
| 3 | #set ($appClass = 'api.Code.apiClass') | ||
| 4 | ## Lowercase letters and digits, single hyphens only between the parts | ||
| 5 | #set ($namePattern = '[a-z0-9]+(-[a-z0-9]+)*') | ||
| 6 | ## Collect all AWM apps: data class name -> app title | ||
| 7 | #set ($apps = {}) | ||
| 8 | #set ($xwql = "select doc.fullName from Document doc, doc.object(AppWithinMinutes.LiveTableClass) as app order by doc.fullName") | ||
| 9 | #foreach ($homeName in $services.query.xwql($xwql).execute()) | ||
| 10 | #set ($home = $xwiki.getDocument($homeName)) | ||
| 11 | #if ($home) | ||
| 12 | #set ($cls = "$!home.getObject('AppWithinMinutes.LiveTableClass').getValue('class')") | ||
| 13 | ## Skip empty entries and the API app itself | ||
| 14 | #if ($cls != '' && $cls != $appClass) | ||
| 15 | #set ($discard = $apps.put($cls, $home.plainTitle)) | ||
| 16 | #end | ||
| 17 | #end | ||
| 18 | #end | ||
| 19 | #set ($errorMsg = '') | ||
| 20 | #if ("$!request.createEntry" == '1') | ||
| 21 | #set ($name = "$!request.name") | ||
| 22 | #set ($sourceClass = "$!request.sourceClass") | ||
| |
4.1 | 23 | #if (!$services.csrf.isTokenValid("$!request.form_token")) |
| 24 | #set ($errorMsg = 'Invalid form token, please try again.') | ||
| 25 | #elseif (!$name.matches($namePattern)) | ||
| |
2.1 | 26 | #set ($errorMsg = 'Only lowercase letters, digits and hyphens are allowed.') |
| 27 | #elseif (!$apps.containsKey($sourceClass)) | ||
| 28 | #set ($errorMsg = 'Invalid source application.') | ||
| 29 | #else | ||
| 30 | #set ($ref = $services.model.createDocumentReference('', [$appSpace, $name], 'WebHome')) | ||
| 31 | #if ($xwiki.exists($ref)) | ||
| 32 | #set ($errorMsg = "An API named '${name}' already exists.") | ||
| 33 | #else | ||
| |
4.1 | 34 | ## Create and save the entry directly, no edit form needed |
| 35 | #set ($newDoc = $xwiki.getDocument($ref)) | ||
| 36 | #set ($discard = $newDoc.setTitle($name)) | ||
| 37 | #set ($discard = $newDoc.setParent("${appSpace}.WebHome")) | ||
| 38 | #set ($newObj = $newDoc.newObject($appClass)) | ||
| 39 | #set ($discard = $newObj.set('sourceClass', $sourceClass)) | ||
| 40 | #set ($discard = $newDoc.save('Created API entry')) | ||
| 41 | $response.sendRedirect($doc.getURL()) | ||
| |
2.1 | 42 | #end |
| 43 | #end | ||
| 44 | #end | ||
| 45 | #if ($errorMsg != '') | ||
| 46 | {{error}}$errorMsg{{/error}} | ||
| 47 | #end | ||
| 48 | {{html clean="false"}} | ||
| 49 | <style> | ||
| 50 | /* Hide the default AWM "Add new entry" button */ | ||
| 51 | ul.actions a.action.add, a.action.add { display: none !important; } | ||
| 52 | </style> | ||
| |
4.1 | 53 | <form action="$doc.getURL()" method="post" class="form-inline"> |
| |
2.1 | 54 | <input type="hidden" name="createEntry" value="1"/> |
| |
4.1 | 55 | <input type="hidden" name="form_token" value="$!services.csrf.token"/> |
| |
2.1 | 56 | <select name="sourceClass" class="form-control"> |
| 57 | #foreach ($app in $apps.entrySet()) | ||
| 58 | <option value="$escapetool.xml($app.key)">$escapetool.xml($app.value)</option> | ||
| 59 | #end | ||
| 60 | </select> | ||
| 61 | <input type="text" name="name" class="form-control" required="required" | ||
| 62 | pattern="$namePattern" title="Lowercase letters, digits and hyphens only"/> | ||
| 63 | <input type="submit" class="btn btn-primary" value="Create API"/> | ||
| 64 | </form> | ||
| 65 | {{/html}} | ||
| 66 | {{/velocity}} | ||
| 67 | |||
| 68 | {{velocity}} | ||
| |
1.1 | 69 | #set ($columnsProperties = { |
| 70 | 'doc.title': {"link":"view"}, | ||
| |
3.1 | 71 | 'sourceClass': {}, |
| |
1.1 | 72 | 'doc.location': {"html":true}, |
| 73 | 'doc.date': {}, | ||
| 74 | 'doc.author': {"link":"author"}, | ||
| 75 | '_actions': {"sortable":false,"filterable":false,"html":true,"actions":["edit","delete"]} | ||
| 76 | }) | ||
| 77 | #set ($options = { | ||
| 78 | 'className': 'api.Code.apiClass', | ||
| 79 | 'translationPrefix': 'api.livetable.', | ||
| 80 | 'tagCloud': true, | ||
| 81 | 'rowCount': 15, | ||
| 82 | 'maxPages': 10, | ||
| 83 | 'selectedColumn': 'doc.title', | ||
| 84 | 'defaultOrder': 'asc' | ||
| 85 | }) | ||
| |
3.1 | 86 | #set ($columns = ['doc.title', 'sourceClass', 'doc.location', 'doc.date', 'doc.author', '_actions']) |
| |
1.1 | 87 | #livetable('api' $columns $columnsProperties $options) |
| 88 | {{/velocity}} |