Skip to content

Instantly share code, notes, and snippets.

@mfaux
Last active April 3, 2018 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfaux/481d53c80408334cbdc825f0f510b977 to your computer and use it in GitHub Desktop.
Save mfaux/481d53c80408334cbdc825f0f510b977 to your computer and use it in GitHub Desktop.
param(
[string]$old,
[string]$new
)
$domain = "configit.atlassian.net"
$infile = ".\entities.xml"
$outfile = ".\new-entities.xml"
$oldLower = $old.ToLower()
$newLower = $new.ToLower()
$batch = 1000
Get-Content $infile -ReadCount $batch |
ForEach {
$_ = $_.Replace( "[CDATA[$($old)]", "[CDATA[$($new)]" )
$_ = $_.Replace( "[CDATA[$($oldLower)]", "[CDATA[$($newLower)]" )
$_ = $_.Replace( "spaceKey=$($old)", "spaceKey=$($new)" )
$_ = $_.Replace( "[$($old):", "spaceKey=[$($new):" )
$_ = $_.Replace( "key=$($old)]", "key=$($new)]" )
$_ = $_.Replace( "<spaceKey>$($old)</spaceKey>", "<spaceKey>$($new)</spaceKey>" )
$_ = $_.Replace( "ri:space-key=`"$($old)`"", "ri:space-key=`"$($new)`"" )
$_ = $_.Replace( "ri:space-key=$($old)", "ri:space-key=$($new)" )
$_ = $_.Replace( "<ac:parameter ac:name=`"spaces`">$($old)</ac:parameter>", "<ac:parameter ac:name=`"spaces`">$($new)</ac:parameter>" )
$_ = $_.Replace( "<ac:parameter ac:name=`"spaceKey`">$($old)</ac:parameter>", "<ac:parameter ac:name=`"spaceKey`">$($new)</ac:parameter>" )
# web links
$_ = $_.Replace( "//$($domain)/wiki/display/$($old)/", "//$($domain)/wiki/display/$($new)/" )
$_ = $_.Replace( "//$($domain)/wiki/display/$($oldLower)/", "//$($domain)/wiki/display/$($newLower)/" )
$_ = $_.Replace( "//$($domain)/wiki/spaces/$($old)/", "//$($domain)/wiki/display/$($new)/" )
$_ = $_.Replace( "//$($domain)/wiki/spaces/$($oldLower)/", "//$($domain)/wiki/display/$($newLower)/" )
Add-Content $outfile $_
}
$infile = ".\exportDescriptor.properties"
$outfile = ".\new-exportDescriptor.properties"
(Get-Content $infile).Replace( "spaceKey=$($old)", "spaceKey=$($new)" ) | Set-Content $outfile
@mfaux
Copy link
Author

mfaux commented Oct 19, 2017

rename-space-keys.ps1

This script is intended to make it easier to copy a space in Confluence Cloud (for example, when versioning product docs).

This script renames the keys in Confluence Cloud space export following the directions in Solution 2: Manually Clone Or Rename A Space, an unsupported solution provided by Atlassian.

This script is suitable for large files, and processes 1000 lines of text in memory at a time.

Usage

  1. In the script, replace the value of $domain with your Confluence Cloud domain.

  2. In PowerShell, go to the folder of your unzipped XML space export. Run rename-space.ps1, passing the old space key in -old and the new one in -new. For example:

 .\rename-space.ps1 -old OLDKEY -new NEWKEY

The script outputs two files:

  • new-entities.xml
  • new-exportDescriptor.properties

The original files remain untouched.

You will need to remove the 'new-' prefixes when zipping up your content prior to importing the new space into Confluence.

Limitations

This script does not rename space names. After running the script, you can edit new-entities.xml, and

  • replace [CDATA[Old space name] with [CDATA[New space name]
  • replace [CDATA[old space name] (lower case) with [CDATA[new space name]

Performance

Lines of text Script execution time
1.5 million 16 minutes, 15 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment