-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_redirect.php
286 lines (264 loc) · 6.13 KB
/
migrate_redirect.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
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
<?php
$hostname="localhost";
$username="root";
$password=$argv[1];
$d6_db = "mjd6";
$wp_db = "pantheon_wp";
$d6 = new PDO("mysql:host=$hostname;dbname=$d6_db", $username, $password);
$wp = new PDO("mysql:host=$hostname;dbname=$wp_db", $username, $password);
function fix_dest($url) {
$components = parse_url($url);
if (!empty($components['host']) || preg_match('/^\//', $url)) {
return $url;
}
return '/' . $url;
}
//GET LEGACY REDIRECTS
$legacy_redirects = $d6->prepare('
SELECT
src,
dst
FROM mjd6.legacy_redirect
;'
);
$legacy_redirects->execute();
$redirect_post_insert = $wp->prepare('
INSERT IGNORE INTO pantheon_wp.wp_posts
(post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt,
post_name, to_ping, pinged, post_modified, post_modified_gmt,
post_content_filtered, post_type, `post_status`, post_parent, post_mime_type)
VALUES (
1,
FROM_UNIXTIME("1970-1-1 00:00:00"),
CONVERT_TZ(FROM_UNIXTIME("1970-1-1 00:00:00"), "PST8PDT","UTC"),
"",
:post_title, #from not hashed
:post_excerpt, # to url
:post_name, #from hashed md5
"",
"",
FROM_UNIXTIME("1970-1-1 00:00:00"),
CONVERT_TZ(FROM_UNIXTIME("1970-1-1 00:00:00"), "PST8PDT","UTC"),
"",
"vip-legacy-redirect",
"publish",
:post_parent, # to as post id
""
)
;
');
$redirects = Array();
$wp->beginTransaction();
while ( $redirect = $legacy_redirects->fetch(PDO::FETCH_ASSOC)) {
$src = fix_dest($redirect['src']);
$dst = fix_dest($redirect['dst']);
$redirect_post_insert->execute(Array(
"post_name" => md5($src),
"post_title" => $src,
"post_parent" => null,
"post_excerpt" => $dst,
));
}
$wp->commit();
//GET MANUAL REDIRECTS
$manual_redirects = $d6->prepare('
SELECT source, redirect FROM path_redirect
WHERE redirect NOT LIKE "node%"
AND source != redirect
;'
);
$manual_redirects->execute();
$wp->beginTransaction();
while ( $redirect = $manual_redirects->fetch(PDO::FETCH_ASSOC)) {
$src = fix_dest($redirect['source']);
$dst = fix_dest($redirect['redirect']);
$redirect_post_insert->execute(Array(
"post_name" => md5($src),
"post_title" => $src,
"post_parent" => null,
"post_excerpt" => $dst,
));
}
$wp->commit();
//UPDATE PAGES WITH SLASHES IN THEM
$page_redirects = $d6->prepare('
SELECT DISTINCT
a.dst,
REPLACE(
a.dst,
"/",
"-"
)
FROM mjd6.node n
INNER JOIN mjd6.node_revisions r
USING(vid)
LEFT OUTER JOIN mjd6.url_alias a
ON a.src = CONCAT("node/", n.nid)
WHERE n.type = "page"
AND a.dst NOT LIKE "%about%"
AND a.dst NOT LIKE "%toc%"
AND a.dst LIKE "%/%"
AND n.status = 1
;
');
$page_redirects->execute();
$wp->beginTransaction();
while ( $redirect = $page_redirects->fetch(PDO::FETCH_NUM)) {
$src = fix_dest($redirect[0]);
$dst = fix_dest($redirect[1]);
$redirect_post_insert->execute(Array(
"post_name" => md5($src),
"post_title" => $src,
"post_parent" => null,
"post_excerpt" => $dst,
));
}
$wp->commit();
// UPDATE ABOUT PAGES W/ SUBDIRS
$page_redirects = $d6->prepare('
SELECT DISTINCT
a.dst,
CONCAT("/about/",
REPLACE(
SUBSTR(a.dst,
LOCATE("/", a.dst) + 1
),
"/",
"-"
)
)
FROM mjd6.node n
INNER JOIN mjd6.node_revisions r
USING(vid)
LEFT OUTER JOIN mjd6.url_alias a
ON a.src = CONCAT("node/", n.nid)
WHERE n.type = "page"
AND a.dst LIKE "about/%/%"
AND n.status = 1
;
');
$page_redirects->execute();
$wp->beginTransaction();
while ( $redirect = $page_redirects->fetch(PDO::FETCH_NUM)) {
$src = fix_dest($redirect[0]);
$dst = fix_dest($redirect[1]);
$redirect_post_insert->execute(Array(
"post_name" => md5($src),
"post_title" => $src,
"post_parent" => null,
"post_excerpt" => $dst,
));
}
$wp->commit();
/**
* GET POSTS WITH THE WRONG MONTH IN THE URL
* drupal makes the url w/ the month set by created date, not post date
* So we're gonna make those old urls point to new good urls
*/
$month_redirects = $d6->prepare('
SELECT DISTINCT n.nid,
r.dst
FROM mjd6.url_alias r
JOIN mjd6.node n
ON ( n.nid = REPLACE(r.src, "node/", "") )
JOIN mjd6.publication_date p
ON ( n.nid = p.nid )
WHERE r.src LIKE "node%" AND r.src NOT LIKE "%feed"
AND n.status = 1 AND
(n.type = "article" OR n.type = "blogpost" OR n.type = "full_width_article")
AND
MONTH( FROM_UNIXTIME(p.published_at) )
!=
TRIM( LEADING "0" FROM
SUBSTRING_INDEX(
SUBSTRING_INDEX( r.dst, "/", -2 ),
"/", 1)
)
;'
);
$month_redirects->execute();
$wp->beginTransaction();
while ( $redirect = $month_redirects->fetch(PDO::FETCH_ASSOC)) {
$src = fix_dest($redirect['dst']);
$redirect_post_insert->execute(Array(
"post_name" => md5($src),
"post_title" => $src,
"post_parent" => $redirect['nid'],
"post_excerpt" => null,
));
}
$wp->commit();
// now do the stuff where we need the wildcards
$redirect_post_insert = $wp->prepare('
insert ignore into pantheon_wp.wp_posts
(post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt,
post_name, to_ping, pinged, post_modified, post_modified_gmt,
post_content_filtered, post_type, `post_status`, post_parent, post_mime_type)
values (
1,
from_unixtime("1970-1-1 00:00:00"),
convert_tz(from_unixtime("1970-1-1 00:00:00"), "America/New_York","utc"),
"",
"auto draft", #post title
"",
:post_name, #post name
"",
"",
from_unixtime("1970-1-1 00:00:00"),
convert_tz(from_unixtime("1970-1-1 00:00:00"), "America/New_York","utc"),
"",
"redirect_rule",
"publish",
0, # to as post id
""
)
;
');
$wp->beginTransaction();
$redirect_post_insert->execute(array(
"post_name" => 'node-to-post',
));
$node_to_post = $wp->lastInsertId();
$redirect_post_insert->execute(array(
"post_name" => 'rss-feeds',
));
$rss_feeds = $wp->lastInsertId();
$wp->commit();
$meta_insert = $wp->prepare('
INSERT IGNORE INTO pantheon_wp.wp_postmeta
(post_id, meta_key, meta_value)
VALUES (?, ?, ?)
;
');
$wp->beginTransaction();
$meta_insert->execute(array(
$node_to_post,
'_redirect_rule_from',
'/node/*',
));
$meta_insert->execute(array(
$node_to_post,
'_redirect_rule_to',
'/?p=*',
));
$meta_insert->execute(array(
$node_to_post,
'_redirect_rule_status_code',
'302',
));
$meta_insert->execute(array(
$rss_feeds,
'_redirect_rule_from',
'/rss/sections/*/feed',
));
$meta_insert->execute(array(
$rss_feeds,
'_redirect_rule_to',
'/category/*/feed',
));
$meta_insert->execute(array(
$rss_feeds,
'_redirect_rule_status_code',
'302',
));
$wp->commit();