-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathhugo-export-cli.php
More file actions
149 lines (133 loc) · 5.59 KB
/
hugo-export-cli.php
File metadata and controls
149 lines (133 loc) · 5.59 KB
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
<?php
/*
* Run the exporter from the command line and spit the zipfile to STDOUT.
*
* Usage:
*
* $ php hugo-export-cli.php > my-hugo-files.zip
*
* Usage: (With configured tmp folder if the avaiable space in system /tmp is insufficent for migration)
*
* $ php hugo-export-cli.php /mnt/tmp-folder
*
* Folder-only export (skips zip archive and writes to /mnt/tmp-folder/hugo-export-files)
*
* $ php hugo-export-cli.php /mnt/tmp-folder --no-zip
*
* Must be run in the wordpress-to-hugo-exporter/ directory.
*
*/
include_once "../../../wp-load.php";
include_once "../../../wp-admin/includes/file.php";
require_once "hugo-export.php";
$args = $argv;
array_shift($args);
$tmpFolder = null;
$folderOnly = false;
$incrementalExport = false;
$folderFlags = array('--no-zip', '--folder-only');
$incrementalNoChangesExitCode = 3; // Keep in sync with bin/sync.sh
foreach ($args as $arg) {
if (in_array($arg, $folderFlags, true)) {
$folderOnly = true;
continue;
}
if ('--incremental' === $arg) {
$incrementalExport = true;
continue;
}
if (null === $tmpFolder) {
$tmpFolder = $arg;
continue;
}
}
$je = new Hugo_Export();
$exportStartTime = microtime(true);
if (isset($tmpFolder)) {
if ('null' !== strtolower($tmpFolder) && is_dir($tmpFolder)) {
echo "[INFO] Start to export data to configured folder $tmpFolder\n";
$je->setTempDir($tmpFolder);
} else {
echo "[WARN] Passed TEMP folder $tmpFolder is not a valid folder. Remove the argument or create it as a folder before contiune\n";
exit(1);
}
} else {
echo "[INFO] tmp folder not found, use default. You could invoke php hugo-export-cli.php with an extra argument as the temporary folder path if needful.\n";
}
$lastSyncFile = null;
$incrementalStartTime = null;
if ($incrementalExport && !$folderOnly) {
echo "[ERROR] --incremental requires --no-zip since incremental sync only works with folder exports.\n";
exit(1);
}
$folderExportPath = null;
if ($folderOnly) {
if (empty($tmpFolder)) {
echo "[ERROR] --no-zip mode requires a writable temp folder argument.\n";
exit(1);
}
$folderExportPath = trailingslashit($tmpFolder) . 'hugo-export-files';
$lastSyncFile = trailingslashit($folderExportPath) . '.last_sync';
if ($incrementalExport) {
if (!is_dir($folderExportPath)) {
echo "[ERROR] Incremental export requires an existing folder at $folderExportPath. Run a full sync first.\n";
exit(1);
}
if (file_exists($lastSyncFile)) {
$timestampValue = trim((string)file_get_contents($lastSyncFile));
$parsed = strtotime($timestampValue);
if (false === $parsed) {
echo "[WARN] Unable to parse last sync timestamp in $lastSyncFile. Running full export.\n";
} else {
$minuteInSeconds = defined('MINUTE_IN_SECONDS') ? MINUTE_IN_SECONDS : 60;
$incrementalStartTime = $parsed - (5 * $minuteInSeconds);
$incrementalStartFormatted = gmdate('c', $incrementalStartTime);
echo "[INFO] Incremental export enabled starting from $incrementalStartFormatted (includes 5 minute buffer).\n";
$je->setIncrementalStartTime($incrementalStartFormatted);
}
} else {
echo "[WARN] No last sync marker found at $lastSyncFile. Running full export.\n";
}
}
$shouldCleanExportDir = !$incrementalExport;
$logAction = $shouldCleanExportDir ? "Cleaning and writing" : "Using existing export folder";
echo "[INFO] Folder-only export enabled. $logAction to $folderExportPath\n";
$je->setCustomExportDir($folderExportPath, true, $shouldCleanExportDir);
$je->skipZipCreation(true);
}
if ($incrementalExport && $je->hasIncrementalStartTime()) {
$postsToProcess = $je->get_posts();
$postCount = is_array($postsToProcess) ? count($postsToProcess) : 0;
if (0 === $postCount) {
$startTime = $je->getIncrementalStartTime();
$startDescription = $startTime ? " at or after $startTime" : "";
echo "[INFO] Incremental export detected no posts modified{$startDescription}. Nothing to export.\n";
exit($incrementalNoChangesExitCode);
}
echo "[INFO] Incremental export will process $postCount post(s) modified since {$je->getIncrementalStartTime()}.\n";
}
$je->export();
$elapsedSeconds = microtime(true) - $exportStartTime;
$hourInSeconds = defined('HOUR_IN_SECONDS') ? HOUR_IN_SECONDS : 3600;
$minuteInSeconds = defined('MINUTE_IN_SECONDS') ? MINUTE_IN_SECONDS : 60;
$hours = (int) floor($elapsedSeconds / $hourInSeconds);
$minutes = (int) floor(($elapsedSeconds % $hourInSeconds) / $minuteInSeconds);
$seconds = (int) round($elapsedSeconds - ($hours * $hourInSeconds) - ($minutes * $minuteInSeconds));
$seconds = ($seconds === 60) ? 59 : $seconds;
echo "[INFO] Export completed in {$hours}h{$minutes}m{$seconds}s\n";
if (!$folderOnly) {
echo "[SUGGESTION] Use --no-zip to skip archive creation and speed up future exports.\n";
}
if (!$incrementalExport) {
echo "[SUGGESTION] Use --incremental with --no-zip to only sync recent changes for faster runs.\n";
}
if ($folderExportPath) {
echo "[INFO] Hugo files exported to $folderExportPath\n";
$timestampForMarker = gmdate('c');
$markerTarget = $lastSyncFile ?: trailingslashit($folderExportPath) . '.last_sync';
if (false === file_put_contents($markerTarget, $timestampForMarker)) {
echo "[WARN] Failed to update last sync marker at $markerTarget\n";
} else {
echo "[INFO] Updated last sync marker at $markerTarget\n";
}
}