-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrites.php
56 lines (42 loc) · 1.44 KB
/
rewrites.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
<?php
include('functions/csv.php');
$input_args = getopt('s::f:');
if (!$input_args['f']) {
die("No file selected, use -f filename.csv\n");
}
$input_filename = $input_args['f'];
$output_filename = "rewrites_" . $input_filename;
$data = getCsvAsAssArray($input_filename);
$export = array(array(
'Redirect from',
'Redirect to'
));
$redirects = array();
foreach ($data as $row) {
$redirect_from = $row['Parent Product Url'];
$url = explode('/', $row['Parent Product Url']);
$handle = trim( end( $url ) );
$redirect_to = "/products/" . $handle;
$categories = explode(',', $row['Categories']);
$categories_parts = explode('>', reset( $categories ));
array_pop($url);
$last_uri = null;
foreach ($categories_parts as $index => $category) {
if (isset($url[$index + 1])) {
$redirect_from_category = $last_uri . "/" . $url[$index + 1];
$redirects[$redirect_from_category] = "/collections/" . preg_replace("/[^\w]+/", "-", strtolower(trim($category)));
$last_uri = $redirect_from_category;
}
}
if (!isset($redirects[$redirect_from])) {
$redirects[$redirect_from] = $redirect_to;
}
}
foreach ($redirects as $redirect_from => $redirect_to) {
$export[] = array(
$redirect_from, // Redirct from
$redirect_to, // Redirect to
);
}
writeDataToCsv($output_filename, $export);
echo "File " . $output_filename . " written." . PHP_EOL;