-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmove-posts.php
executable file
·107 lines (81 loc) · 2.74 KB
/
move-posts.php
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
#!/usr/bin/env php
<?php
/**
* Wordpress to Confluence migration utility: move blog posts to new parent space
*
* @author Mike Sollanych <[email protected]>
* @package visioncritical/wordpress-to-confluence
*/
require("vendor/autoload.php");
require("includes/helpers.class.php");
use Httpful\Request;
// Handle command line arguments
require("includes/conf-commandline.inc.php");
$args = getArguments();
// REST base URL
$base = $args["confluence-url"] . 'rest/api';
$source = $args["source-space"];
$target = $args["target-space"];
// Create the template
$template = Request::init()
->expectsJson()
->authenticateWith($args["confluence-username"], $args["confluence-password"]);
// Set it as a template
Request::ini($template);
// Begin
print ("Confluence blog post move running...");
print("\nGetting space details...");
$spacetest = Request::get($base."/space")->send();
if ($spacetest->code != "200") {
die(k($spacetest->raw_headers));
}
// Get ID of target space
$target_id = false;
foreach($spacetest->body->results as $res) {
if ($res->key == $target) $target_id = $res->id;
}
if (!$target_id) die("\nCould not find target space!");
// Go and get a list of pages for this space
print ("\nGetting a list of pages for space $source...");
$contentremains = true;
$limit = 20;
$start = 0;
$templimit = 0;
$tempcount = 0;
while ($contentremains) {
print ("\nGetting records from $start to ".($start + $limit)."...");
$content = Request::get("$base/space/$source/content?limit=$limit&start=$start")->send();
if ($content->body->blogpost->size < $content->body->blogpost->limit)
$contentremains = false;
else
$start = $start + $limit;
// Now for each of these entries, go get the actual blog post
foreach ($content->body->blogpost->results as $result) {
// Save Post ID
$id = $result->id;
print("\nMoving post: ".$result->title);
// Get that entire post
$post = Request::get("$base/content/$id?expand=body.storage,version")->send();
// Create the PUT to update the content
$putdata = [
"version" => ["number" => ($post->body->version->number + 1)],
"type" => "page",
"ancestors" => [["id" => $target_id]]
];
// Actually go and PUT that to the page
$putresponse = \Httpful\Request::put("$base/content/$id")
->sendsJson() // tell it we're sending (Content-Type) JSON...
->body(json_encode($putdata)) // attach a body/payload...
->send();
if ($putresponse->code == 200) {
print("\nSuccessfully moved post!\n\n\n");
}
else {
print ("\nError moving post: ");
k($putresponse);
}
// Early bailout for now
$tempcount++;
if ($tempcount > $templimit) break 2;
}
}