XML-RPC/Spaces.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#Requires -Version 3.0
<# Spaces #>
function Get-ConfluenceSpaces {
    #Vector<SpaceSummary> getSpaces(String token) - returns all the summaries that the current user can see.
    #Space getSpace(String token, String spaceKey) - returns a single Space. If the spaceKey does not exist: earlier versions of Confluence will throw an Exception. Later versions (3.0+) will return a null object.
    #String getSpaceStatus(String token, String spaceKey) - returns the status of a space, either CURRENT or ARCHIVED.
    #TODO: Vector getSpacesWithLabel(String token, String labelName) - Returns an array of spaces that have been labelled with labelName.
    #TODO: Vector getSpacesContainingContentWithLabel(String token, String labelName) - Returns all spaces that have content labelled with labelName.
    [CmdletBinding(
        DefaultParameterSetName = "getSpaces"
    )]
    [OutputType(
        [Confluence.SpaceSummary],
        ParameterSetName = ('getSpaces')
    )]
    [OutputType(
        [Confluence.Space],
        ParameterSetName = ('getSpace')
    )]
    [OutputType(
        [string],
        ParameterSetName = ('getSpaceStatus')
    )]
    # [OutputType(
    # [],
    # ParameterSetName = ('getSpacesWithLabel')
    # )]
    param(
        [Parameter(
            Mandatory = $true
        )]
        [string]$apiURi,

        [Parameter(
            Mandatory = $true
        )]
        [string]$Token,

        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "getSpace"
        )]
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "getSpaceStatus"
        )]
        [string[]]$SpaceKey,

        [Parameter(
            Mandatory = $true,
            ParameterSetName = "getSpaceStatus"
        )]
        [switch]$status

        # [Parameter(
        # Mandatory = $true,
        # ParameterSetName = "getSpacesWithLabel"
        # )]
        # [string]$Label,

        # [Parameter(
        # ParameterSetName = "getSpacesWithLabel"
        # )]
        # [switch]$ContainingContent
    )

    Begin
        { Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started" }

    Process {
        Write-Debug "$($MyInvocation.MyCommand.Name):: Given Parameters: $($PsCmdlet | Out-String)"
        switch ($PsCmdlet.ParameterSetName) {
            "getSpaces" {
                Write-Verbose "$($MyInvocation.MyCommand.Name):: Fetching all spaces"
                Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getSpaces" -Params ($token) -OutputType "Confluence.SpaceSummary"
            }
            "getSpaceStatus" {
                foreach ($space in $SpaceKey) {
                    Write-Verbose "$($MyInvocation.MyCommand.Name):: Fetching status of $space space"
                    Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getSpaceStatus" -Params ($token, $space)
                }
            }
            "getSpace" {
                foreach ($space in $SpaceKey) {
                    Write-Verbose "$($MyInvocation.MyCommand.Name):: Fetching $space space"
                    Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getSpace" -Params ($token, $space) -OutputType "Confluence.Space"
                }
            }
            # "getSpacesWithLabel" {
            # if ($ContainingContent) {
            # Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getSpacesContainingContentWithLabel" -Params ($token, $Label) -OutputType "Confluence.Space"
            # }
            # else {
            # Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.getSpacesWithLabel" -Params ($token, $Label) -OutputType "Confluence.Space"
            # }
            # }
        }
    }

    End
        { Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended" }
}
function Export-ConfluenceSpace {
    #String exportSpace(String token, String spaceKey, String exportType) - exports a space and returns a String holding the URL for the download. The export type argument indicates whether or not to export in XML or HTML format - use "TYPE_XML" or "TYPE_HTML" respectively. (Note: In Confluence 3.0, the remote API specification for PDF exports changed. You can no longer use this 'exportSpace' method to export a space to PDF. Please refer to Remote API Specification for PDF Export for current remote API details on this feature.)
    #String exportSpace(String token, String spaceKey, String exportType, boolean exportAll) - exports a space and returns a String holding the URL for the download. In the version 2 API you can set exportAll to true to export the entire contents of the space.
    [CmdletBinding()]
    param(
        [Parameter(
            Position = 0,
            Mandatory = $true
        )]
        [string]$apiURi,

        [Parameter(
            Position = 1,
            Mandatory = $true
        )]
        [string]$Token,

        [Parameter(
            Position = 2,
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [string[]]$SpaceKey,

        [Parameter(
            Position = 3
        )]
        [ValidateSet("XML", "HTML")]
        [Alias("Type", "Format")]
        [string]$exportType = "XML",


        [Alias("All")]
        [switch]$exportAll
    )

    Begin {
        $exportType = $exportType.insert(0, "TYPE_")
    }

    Process {
        foreach ($space in $SpaceKey) {
            Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.exportSpace" -Params ($token, $space, $exportType, $exportAll)
        }
    }

    End { }
}
function Export-ConfluenceSpaceToPDF {
    #public String exportSpace(String token, String spaceKey) - exports the entire space as a PDF. Returns a url to download the exported PDF. Depending on how you have Confluence set up, this URL may require you to authenticate with Confluence. Note that this will be normal Confluence authentication, not the token based authentication that the web service uses.
    [CmdletBinding()]
    param(
        [Parameter(
            Position = 0,
            Mandatory = $true
        )]
        [string]$apiURi,

        [Parameter(
            Position = 1,
            Mandatory = $true
        )]
        [string]$Token,

        [Parameter(
            Position = 2,
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [string[]]$SpaceKey
    )

    Begin { }

    Process {
        foreach ($space in $SpaceKey) {
            Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.pdfexport" -Params ($token, $space)
        }
    }

    End { }
}
function Add-ConfluenceSpace {
    #Space addSpace(String token, Space space) - create a new space, passing in name, key and description.
    #Space addPersonalSpace(String token, Space personalSpace, String userName) - add a new space as a personal space.
    [CmdletBinding(DefaultParameterSetName="addSpace")]
    param(
        [Parameter(
            Position=0,
            Mandatory=$true
        )]
        [string]$apiURi,

        [Parameter(
            Position=1,
            Mandatory=$true
        )]
        [string]$Token,

        [Parameter(
            Position=2,
            Mandatory=$true,
            ValueFromPipeline=$true,
            ParameterSetName="addSpace"
        )]
        $Space,

        [Parameter(
            Position=2,
            Mandatory=$true,
            ParameterSetName="addPersonalSpace"
        )]
        $personalSpace,

        [Parameter(
            Position=3,
            Mandatory=$true,
            ParameterSetName="addPersonalSpace"
        )]
        [Alias("User")]
        [switch]$userName
    )

    Process {
        switch ($PsCmdlet.ParameterSetName) {
            "addSpace" {
                Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.addSpace" -Params ($token,$SpaceKey)
            }
            "addPersonalSpace" {
                Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.addPersonalSpace" -Params ($token,$personalSpace, $userName)
            }
        }
    }
}
function Remove-ConfluenceSpace {
    #Boolean removeSpace(String token, String spaceKey) - remove a space completely.
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(
            Position=0,
            Mandatory=$true
        )]
        [string]$apiURi,

        [Parameter(
            Position=1,
            Mandatory=$true
        )]
        [string]$Token,

        [Parameter(
            Position=2,
            Mandatory=$true,
            ValueFromPipeline=$true
        )]
        [Alias("Space")]
        [string]$SpaceKey
    )

    Process {
        if ($PSCmdlet.ShouldProcess($SpaceKey)) {
            Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.removeSpace" -Params ($token,$SpaceKey)
        }
    }
}
function ConvertTo-ConfluencePersonalSpace {
    #boolean convertToPersonalSpace(String token, String userName, String spaceKey, String newSpaceName, boolean updateLinks) - convert an existing space to a personal space.
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(
            Position=0,
            Mandatory=$true
        )]
        [string]$apiURi,

        [Parameter(
            Position=1,
            Mandatory=$true
        )]
        [string]$Token,

        [Parameter(
            Position=2,
            Mandatory=$true
        )]
        [Alias("User")]
        [switch]$userName,

        [Parameter(
            Position=3,
            Mandatory=$true
        )]
        [Alias("Space")]
        [string]$SpaceKey,

        [Parameter(
            Position=3,
            Mandatory=$true
        )]
        [Alias("NewName")]
        [string]$newSpaceName,

        [switch]$updateLinks
    )

    Process {
        if ($PSCmdlet.ShouldProcess($SpaceKey)) {
            Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.convertToPersonalSpace" -Params ($token,$userName, $spaceKey,$newSpaceName,$updateLinks)
        }
    }
}
function Set-ConfluenceSpace {
    #Space storeSpace(String token, Space space) - create a new space if passing in a name, key and description or update the properties of an existing space. Only name, homepage or space group can be changed.
    ##Boolean setSpaceStatus(String token, String spaceKey, String status) - set a new status for a space. Valid values for status are "CURRENT" and "ARCHIVED".
    [CmdletBinding(DefaultParameterSetName = "storeSpace")]
    param(
        [Parameter(
            Mandatory = $true
        )]
        [string]$apiURi,

        [Parameter(
            Mandatory = $true
        )]
        [string]$Token,

        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ParameterSetName = "storeSpace"
        )]
        [Confluence.Space]$Space,

        [Parameter(
            Mandatory = $true,
            ParameterSetName = "setSpaceStatus"
        )]
        [Alias("Space")]
        [string]$SpaceKey,

        [Parameter(
            Mandatory = $true,
            ParameterSetName = "setSpaceStatus"
        )]
        [ValidateSet("CURRENT", "ARCHIVED")]
        [string]$status
    )

    Process {
        switch ($PsCmdlet.ParameterSetName) {
            "storeSpace" {
                Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.storeSpace" -Params ($token, $Space)
            }
            "setSpaceStatus" {
                Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.setSpaceStatus" -Params ($token, $SpaceKey, $status)
            }
        }
    }
}
<#function Import-ConfluenceConfluenceSpace {
    #boolean importSpace(String token, byte[] zippedImportData) - import a space into Confluence. Note that this uses a lot of memory - about 4 times the size of the upload. The data provided should be a zipped XML backup, the same as exported by Confluence.
}#>

function Get-ConfluenceTrashContent {
    #ContentSummaries getTrashContents(String token, String spaceKey, int offset, int count) - get the contents of the trash for the given space, starting at 'offset' and returning at most 'count' items.
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(
            Mandatory = $true
        )]
        [string]$apiURi,

        [Parameter(
            Mandatory = $true
        )]
        [string]$Token,

        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true
        )]
        [Confluence.Space]$Space
    )

    Process {
        if ($PSCmdlet.ShouldProcess($SpaceKey)) {
            Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.storeSpace" -Params ($token, $Space)
        }
    }
}
function Remove-ConfluenceTrashItem {
    #boolean purgeFromTrash(String token, String spaceKey, long contentId) - remove some content from the trash in the given space, deleting it permanently.
    #boolean emptyTrash(String token, String spaceKey) - remove all content from the trash in the given space, deleting them permanently.
    [CmdletBinding(DefaultParameterSetName = "purgeFromTrash", SupportsShouldProcess = $true)]
    param(
        [Parameter(
            Mandatory = $true
        )]
        [string]$apiURi,

        [Parameter(
            Mandatory = $true
        )]
        [string]$Token,

        [Parameter(
            Mandatory = $true,
            ParameterSetName = "purgeFromTrash"
        )]
        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "emptyTrash"
        )]
        [Alias("Space")]
        [string]$SpaceKey,

        [Parameter(
            Mandatory = $true,
            ParameterSetName = "purgeFromTrash"
        )]
        [long]$contentId,

        [Parameter(
            Mandatory = $true,
            ParameterSetName = "emptyTrash"
        )]
        [Alias("All")]
        [switch]$RemoveAll
    )

    Process {
        switch ($PsCmdlet.ParameterSetName) {
            "purgeFromTrash" {
                if ($PSCmdlet.ShouldProcess($SpaceKey)) {
                    Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.purgeFromTrash" -Params ($token, $SpaceKey, $contentId)
                }
                break
            }
            "emptyTrash" {
                if ($RemoveAll) {
                    if ($PSCmdlet.ShouldProcess($SpaceKey)) {
                        Invoke-ConfluenceCall -Url $apiURi -MethodName "confluence2.emptyTrash" -Params ($token, $SpaceKey)
                    }
                }
                break
            }
        }
    }
}
<# /Spaces #>