Wiki-Quellcode von RenameApplication
Zuletzt geändert von admin am 2025/02/23 10:22
Zeige letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity output="false"}} | ||
| 2 | #macro (renameApp $oldAppReference $newAppReference) | ||
| 3 | #set ($oldApp = { | ||
| 4 | 'name': $oldAppReference.name, | ||
| 5 | 'reference': $oldAppReference | ||
| 6 | }) | ||
| 7 | #set ($newApp = { | ||
| 8 | 'name': $newAppReference.name, | ||
| 9 | 'reference': $newAppReference | ||
| 10 | }) | ||
| 11 | #getAppCodeReference($oldApp $newApp) | ||
| 12 | #if (!$newApp.codeReference.equals($oldApp.codeReference)) | ||
| 13 | ## Some application code pages have to be renamed separately because they have the application name in their name. | ||
| 14 | #renameAppCodePages($oldApp $newApp) | ||
| 15 | #if (!$oldApp.codeReference.equals($oldApp.reference) && !$oldApp.codeReference.hasParent($oldApp.reference)) | ||
| 16 | ## The application code pages are not children of the application page. Rename the remaining code pages. | ||
| 17 | #renameAppPage($oldApp.codeReference $newApp.codeReference true) | ||
| 18 | #maybeFixAppPreferences($oldApp.codePreferencesReference $newApp.codePreferencesReference) | ||
| 19 | #end | ||
| 20 | #end | ||
| 21 | #renameAppPage($oldAppReference $newAppReference true) | ||
| 22 | #maybeFixAppPreferences($oldApp.preferencesReference $newApp.preferencesReference) | ||
| 23 | #end | ||
| 24 | |||
| 25 | #macro (getAppCodeReference $oldApp $newApp) | ||
| 26 | #set ($oldApp.codeReference = $services.model.createSpaceReference('Code', $oldApp.reference)) | ||
| 27 | #set ($oldClassReference = $xwiki.getDocument($oldApp.reference).getValue('class')) | ||
| 28 | #if ($oldClassReference) | ||
| 29 | #set ($oldClassReference = $services.model.resolveDocument($oldClassReference, $oldApp.reference)) | ||
| 30 | #set ($oldApp.codeReference = $oldClassReference.parent) | ||
| 31 | #end | ||
| 32 | #if ($oldApp.codeReference.hasParent($oldApp.reference)) | ||
| 33 | ## The code space is nested inside the application space. | ||
| 34 | #set ($newApp.codeReference = $oldApp.codeReference.replaceParent($oldApp.reference, $newApp.reference)) | ||
| 35 | #elseif ($oldApp.codeReference.equals($oldApp.reference)) | ||
| 36 | ## The code space is the same as the application space. | ||
| 37 | #set ($newApp.codeReference = $newApp.reference) | ||
| 38 | #elseif ($oldApp.codeReference.parent.equals($oldApp.reference.parent) | ||
| 39 | && $oldApp.codeReference.name == "${oldApp.name}Code") | ||
| 40 | ## The code space is a sibling of the application space. | ||
| 41 | #set ($newApp.codeReference = $services.model.createSpaceReference("${newApp.name}Code", | ||
| 42 | $newApp.reference.parent)) | ||
| 43 | #else | ||
| 44 | #set ($newApp.codeReference = $oldApp.codeReference) | ||
| 45 | #end | ||
| 46 | #end | ||
| 47 | |||
| 48 | #macro (getAppCodePages $app) | ||
| 49 | #set ($app.homePageReference = $services.model.resolveDocument('', 'default', $app.reference)) | ||
| 50 | #set ($app.preferencesReference = $services.model.createDocumentReference('WebPreferences', $app.reference)) | ||
| 51 | #set ($app.codePreferencesReference = $services.model.createDocumentReference('WebPreferences', $app.codeReference)) | ||
| 52 | #set ($app.codePages = {}) | ||
| 53 | #foreach ($codePage in ['class', 'sheet', 'template', 'templateProvider', 'translations']) | ||
| 54 | #set ($discard = $app.codePages.put("${codePage}Reference", $services.model.createDocumentReference( | ||
| 55 | "${app.name}$stringtool.capitalize($codePage)", $app.codeReference))) | ||
| 56 | #end | ||
| 57 | #end | ||
| 58 | |||
| 59 | #macro (renameAppCodePages $oldApp $newApp) | ||
| 60 | #getAppCodePages($oldApp) | ||
| 61 | #getAppCodePages($newApp) | ||
| 62 | #foreach ($entry in $oldApp.codePages.entrySet()) | ||
| 63 | #renameAppPage($entry.value $newApp.codePages.get($entry.key) true) | ||
| 64 | #end | ||
| 65 | #updateAppClass($oldApp $newApp) | ||
| 66 | #updateAppSheet($oldApp $newApp) | ||
| 67 | #updateAppTemplateProvider($oldApp $newApp) | ||
| 68 | #updateAppTranslations($oldApp $newApp) | ||
| 69 | #end | ||
| 70 | |||
| 71 | #macro (renameAppPage $oldReference $newReference $checkRights) | ||
| 72 | #set ($renameRequest = $services.refactoring.requestFactory.createRenameRequest($oldReference, $newReference)) | ||
| 73 | #set ($discard = $renameRequest.setInteractive(false)) | ||
| 74 | #set ($discard = $renameRequest.setAutoRedirect(false)) | ||
| 75 | #set ($discard = $renameRequest.setCheckRights($checkRights)) | ||
| 76 | #set ($renameJob = $services.refactoring.rename($renameRequest)) | ||
| 77 | #set ($discard = $renameJob.join()) | ||
| 78 | #end | ||
| 79 | |||
| 80 | #macro (updateAppClass $oldApp $newApp) | ||
| 81 | #if ($xwiki.exists($newApp.codePages.classReference) && | ||
| 82 | $services.security.authorization.hasAccess('edit', $newApp.codePages.classReference)) | ||
| 83 | #set ($newClass = $xwiki.getDocument($newApp.codePages.classReference)) | ||
| 84 | ## Update the sheet. | ||
| 85 | #set ($newSheet = $services.model.serialize($newApp.codePages.sheetReference, 'local')) | ||
| 86 | #set ($discard = $newClass.getObject('XWiki.ClassSheetBinding').set('sheet', $newSheet)) | ||
| 87 | ## Update the data space used by old applications. | ||
| 88 | #set ($metaData = $newClass.getObject('AppWithinMinutes.MetadataClass')) | ||
| 89 | #if ($metaData.getValue('dataSpaceName') == $services.model.serialize($oldApp.reference, 'local')) | ||
| 90 | ## Between 6.3M2 (XWIKI-11249) and 7.3RC1 (XWIKI-12741) | ||
| 91 | #set ($discard = $metaData.set('dataSpaceName', $services.model.serialize($newApp.reference, 'local'))) | ||
| 92 | #end | ||
| 93 | #set ($discard = $newClass.save('Update class after renaming app')) | ||
| 94 | #end | ||
| 95 | #end | ||
| 96 | |||
| 97 | #macro (updateAppSheet $oldApp $newApp) | ||
| 98 | #if ($xwiki.exists($newApp.codePages.sheetReference) && | ||
| 99 | $services.security.authorization.hasAccess('edit', $newApp.codePages.sheetReference)) | ||
| 100 | #set ($newSheet = $xwiki.getDocument($newApp.codePages.sheetReference)) | ||
| 101 | ## Update the class reference used within the sheet content. | ||
| 102 | #set ($oldClassReference = $services.model.serialize($oldApp.codePages.classReference, 'local')) | ||
| 103 | #set ($newClassReference = $services.model.serialize($newApp.codePages.classReference, 'local')) | ||
| 104 | #set ($discard = $newSheet.setContent($newSheet.content.replace($oldClassReference, $newClassReference))) | ||
| 105 | #set ($discard = $newSheet.save('Update sheet after renaming app')) | ||
| 106 | #end | ||
| 107 | #end | ||
| 108 | |||
| 109 | #macro (updateAppTemplateProvider $oldApp $newApp) | ||
| 110 | #if ($xwiki.exists($newApp.codePages.templateProviderReference) && | ||
| 111 | $services.security.authorization.hasAccess('edit', $newApp.codePages.templateProviderReference)) | ||
| 112 | #set ($newTemplateProvider = $xwiki.getDocument($newApp.codePages.templateProviderReference)) | ||
| 113 | #set ($newTemplateProviderObj = $newTemplateProvider.getObject('XWiki.TemplateProviderClass')) | ||
| 114 | ## Update the name. | ||
| 115 | #set ($name = $newTemplateProviderObj.getValue('name')) | ||
| 116 | #if ($name.startsWith($oldApp.name.toLowerCase())) | ||
| 117 | #set ($discard = $newTemplateProviderObj.set('name', | ||
| 118 | "$newApp.name.toLowerCase()$name.substring($oldApp.name.length())")) | ||
| 119 | #end | ||
| 120 | ## Update the template. | ||
| 121 | #set ($discard = $newTemplateProviderObj.set('template', | ||
| 122 | $services.model.serialize($newApp.codePages.templateReference, 'local'))) | ||
| 123 | ## Update creation restrictions. | ||
| 124 | #set ($creationRestrictions = []) | ||
| 125 | #foreach ($creationRestriction in $newTemplateProviderObj.getValue('creationRestrictions')) | ||
| 126 | #set ($creationRestrictionReference = $services.model.resolveSpace($creationRestriction, $oldApp.reference)) | ||
| 127 | #if ($creationRestrictionReference.equals($oldApp.reference)) | ||
| 128 | #set ($creationRestrictionReference = $newApp.reference) | ||
| 129 | #else | ||
| 130 | #set ($creationRestrictionReference = $creationRestrictionReference.replaceParent( | ||
| 131 | $oldApp.reference, $newApp.reference)) | ||
| 132 | #end | ||
| 133 | #set ($discard = $creationRestrictions.add($services.model.serialize($creationRestrictionReference, 'local'))) | ||
| 134 | #end | ||
| 135 | #set ($discard = $newTemplateProviderObj.set('creationRestrictions', $creationRestrictions)) | ||
| 136 | #set ($discard = $newTemplateProvider.save('Update template provider after renaming app')) | ||
| 137 | #end | ||
| 138 | #end | ||
| 139 | |||
| 140 | #macro (updateAppTranslations $oldApp $newApp) | ||
| 141 | #if ($xwiki.exists($newApp.codePages.translationsReference) && | ||
| 142 | $services.security.authorization.hasAccess('edit', $newApp.codePages.translationsReference)) | ||
| 143 | #set ($newTranslations = $xwiki.getDocument($newApp.codePages.translationsReference)) | ||
| 144 | ## Update the default translation. | ||
| 145 | #updateAppTranslation($newTranslations $oldApp $newApp) | ||
| 146 | ## Update all the available translations. | ||
| 147 | #foreach ($locale in $newTranslations.translationLocales) | ||
| 148 | #set ($newTranslationsForLocale = $newTranslations.getTranslatedDocument($locale)) | ||
| 149 | #updateAppTranslation($newTranslationsForLocale $oldApp $newApp) | ||
| 150 | #end | ||
| 151 | #end | ||
| 152 | #end | ||
| 153 | |||
| 154 | ## Special characters that need to be escaped in a translation key. | ||
| 155 | #set ($translationKeySpecialCharsPattern = $regextool.compile('([=: \\])')) | ||
| 156 | |||
| 157 | #macro (updateAppTranslation $translation $oldApp $newApp) | ||
| 158 | ## Update the translation keys that are prefixed with the application name. | ||
| 159 | #set ($oldAppKeyPrefix = $translationKeySpecialCharsPattern.matcher($oldApp.name.toLowerCase()).replaceAll('\\$1')) | ||
| 160 | #set ($newAppKeyPrefix = $translationKeySpecialCharsPattern.matcher($newApp.name.toLowerCase()).replaceAll('\\$1')) | ||
| 161 | #set ($content = $translation.content.replaceAll("(?m)(^$regextool.quote($oldAppKeyPrefix))", | ||
| 162 | $regextool.quoteReplacement($newAppKeyPrefix))) | ||
| 163 | ## Update the translation keys that are prefixed with the class reference. | ||
| 164 | #set ($oldClassKeyPrefix = $services.model.serialize($oldApp.codePages.classReference, 'local')) | ||
| 165 | #set ($oldClassKeyPrefix = $translationKeySpecialCharsPattern.matcher($oldClassKeyPrefix).replaceAll('\\$1')) | ||
| 166 | #set ($newClassKeyPrefix = $services.model.serialize($newApp.codePages.classReference, 'local')) | ||
| 167 | #set ($newClassKeyPrefix = $translationKeySpecialCharsPattern.matcher($newClassKeyPrefix).replaceAll('\\$1')) | ||
| 168 | #set ($content = $content.replaceAll("(?m)(^$regextool.quote($oldClassKeyPrefix))", | ||
| 169 | $regextool.quoteReplacement($newClassKeyPrefix))) | ||
| 170 | #set ($discard = $translation.setContent($content)) | ||
| 171 | #set ($discard = $translation.save('Update translations after renaming app')) | ||
| 172 | #end | ||
| 173 | |||
| 174 | #** | ||
| 175 | * Simple users don't have the right to administer the pages they create so the application wizard creates the | ||
| 176 | * WebPreferences page for them (using programming rights). When renaming the applications they have created, simple | ||
| 177 | * users don't have the right to move the WebPreferences page to the new location so we need to do this for them (again, | ||
| 178 | * relying on programming rights). | ||
| 179 | *# | ||
| 180 | #macro (maybeFixAppPreferences $oldPrefsReference $newPrefsReference) | ||
| 181 | ## Fix the preferences page if: | ||
| 182 | ## * the old preferences page exists (wasn't moved / renamed) | ||
| 183 | ## * and the current user has administration right on it | ||
| 184 | ## * the new preferences page doesn't exist | ||
| 185 | ## * and the current user has the right to delete its home page. | ||
| 186 | #set ($newPrefsHomeReference = $services.model.resolveDocument('', 'default', $newPrefsReference.parent)) | ||
| 187 | #if ($xwiki.exists($oldPrefsReference) && $services.security.authorization.hasAccess('admin', $oldPrefsReference) | ||
| 188 | && !$xwiki.exists($newPrefsReference) | ||
| 189 | && $services.security.authorization.hasAccess('delete', $newPrefsHomeReference)) | ||
| 190 | ## This requires programming rights! | ||
| 191 | #renameAppPage($oldPrefsReference $newPrefsReference false) | ||
| 192 | #end | ||
| 193 | #end | ||
| 194 | {{/velocity}} | ||
| 195 | |||
| 196 | {{velocity wiki="false"}} | ||
| 197 | #if ($request.oldAppReference && $request.newAppReference) | ||
| 198 | #if ($services.csrf.isTokenValid($request.form_token)) | ||
| 199 | #set ($oldAppReference = $services.model.resolveSpace($request.oldAppReference)) | ||
| 200 | #set ($newAppReference = $services.model.resolveSpace($request.newAppReference)) | ||
| 201 | #if (!$services.security.authorization.hasAccess('delete', $oldAppReference) | ||
| 202 | || !$services.security.authorization.hasAccess('edit', $newAppReference)) | ||
| 203 | #set ($discard = $response.sendError(403)) | ||
| 204 | #elseif (!$newAppReference.equals($oldAppReference)) | ||
| 205 | #renameApp($oldAppReference $newAppReference) | ||
| 206 | #end | ||
| 207 | #else | ||
| 208 | #set ($discard = $response.sendError(401, 'Bad CSRF Token')) | ||
| 209 | #end | ||
| 210 | #end | ||
| 211 | {{/velocity}} |