Webflow::setToken($this->webflow_api_token);
- $path = $this->getMirrorPath();
- if ($force) {
- $path = Files::emptyDir($path);
- }
+ $tmp = Files::tmpdir(null, 'webflow_mirror');
+
$wget = new CommandLine('wget');
$wget->setArg('mirror');
$wget->setArg('convert-links');
$wget->setArg('adjust-extension');
$wget->setArg('page-requisites');
$wget->setArg('user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0');
- $wget->setArg("directory-prefix", $path);
+ $wget->setArg("directory-prefix", $tmp);
$wget->setArg("span-hosts");
$wget->setArg("no-host-directories");
if ($slow) {
$wget->setArg(null, 'https://' . $this->webflow . '.webflow.io');
$wget->execute();
+
+ $path = $this->getMirrorPath();
+ Files::rmdir($path);
+ `mv $tmp $path`;
+
stop_measure('Webflow mirror');
return $wget;
{
$mirror = $this->getMirrorPath();
$path = Files::emptyDir(protected_path('webflow/final/' . $this->id . '/' . $locale));
- $rsync = new CommandLine\Rsync($mirror, $path, true);
+ $rsync = new CommandLine\Rsync($mirror, $path, false);
+ $rsync->addExclude('*.html');
$rsync->execute();
+ $urlmap = $this->getURLMap($locale);
+
file_put_contents(Files::mkdir($path . '/css') . 'custom.css', $this->getCustomCSS());
file_put_contents(Files::mkdir($path . '/js') . 'custom.js', $this->getCustomJS());
$images = $this->getTranslation('images', $locale);
$seo = $this->getTranslation('seo', $locale);
- foreach (Files::getRecursiveDirectoryIterator($path) as $f) {
+ foreach (Files::getRecursiveDirectoryIterator($mirror) as $f) {
/** @var $f \SplFileInfo */
if ($f->isDir() || $f->getExtension() !== 'html') {
continue;
}
- $this->compileHTMLFile($f, $isMainLocale, $locale, $texts, $images, $seo);
+ $relative = '/' . str_replace($mirror, '', $f->getPathname());
+ $this->compileHTMLFile($f, $relative, $path, $isMainLocale, $locale, $texts, $images, $seo, $urlmap);
+ }
+
+ // Redirections
+ $htaccess = '<IfModule mod_rewrite.c>
+ RewriteEngine On
+ RewriteBase /
+';
+ foreach ($this->redirections as $redirection) {
+ foreach ($seo as $s) {
+ if ($s['id'] == $redirection['to']) {
+
+ $htaccess .= " " . 'RewriteRule ^' . $redirection['from'] . '$ ' . ($urlmap[$s['url']] ?? $s['url']) . ' [R=308]' . "\n";
+ break;
+ }
+ }
}
+ $htaccess .= "</IfModule>\n";
+ file_put_contents($path . '/.htaccess', $htaccess);
}
/**
* @param $locale string
* @return void
*/
- protected function compileHTMLFile($f, $isMainLocale, $locale, $texts, $images, $seo)
+ protected function compileHTMLFile($f, $relative, $dest, $isMainLocale, $locale, $texts, $images, $seo, $urlmap)
{
$html = file_get_contents($f->getPathname());
}
$pageId = $m[1];
+ $htmlPage = false;
+ foreach ($seo as $s) {
+ if ($s['id'] === $relative) {
+ $pageId = $s['id'];
+ $htmlPage = true;
+ break;
+ }
+ }
$html = str_replace('</head>', '<link href="/css/custom.css" rel="stylesheet">' . "\n" . '</head>', $html);
$html = str_replace('</body>', '<script src="/js/custom.js"></script>' . "\n" . '</body>', $html);
- $html = preg_replace('/lang=\"[a-zA-Z\-_]{2,6}\"/', 'lang="' . $locale . '"', $html);
-
+ $html = preg_replace('/\s+lang=\"[a-zA-Z\-_]{2,6}\"/', ' lang="' . $locale . '"', $html);
// Texts
foreach ($texts as $text => $translation) {
// Images
+ // Replace URL
+ foreach ($urlmap as $k => $v) {
+ $html = str_replace(ltrim($k, '/'), ltrim($v, '/'), $html);
+ }
+
// SEO
- if (!$isMainLocale) {
+ if (!$isMainLocale || $htmlPage) {
foreach ($seo as $s) {
if ($s['id'] === $pageId) {
$currentPage = $s;
}
}
+
if (isset($currentPage)) {
$og_title = e($currentPage['og_title_copied'] ? $currentPage['seo_title'] : $currentPage['og_title']);
$og_desc = e($currentPage['og_description_copied'] ? $currentPage['seo_description'] : $currentPage['og_description']);
<meta content="' . $og_title . '" property="twitter:title" />
<meta content="' . $og_desc . '" property="twitter:description" />';
$html = preg_replace('/<title>[^<]*<\/title>/', '<title>' . e($currentPage['seo_title']) . '</title>' . $meta, $html);
+
+ }
+ }
+
+ file_put_contents($dest . ($urlmap[$relative] ?? $relative), $html);
+ }
+
+ public function getURLMap($locale)
+ {
+ if ($locale === $this->getMainLocale()) {
+ return [];
+ }
+
+ $seoTranslations = $this->getTranslations('seo');
+ $seo = $seoTranslations[$locale];
+ $main = $seoTranslations[$this->getMainLocale()];
+
+ $res = [];
+ foreach ($seo as $item) {
+ foreach ($main as $m) {
+ if ($item['id'] == $m['id']) {
+ if ($m['url'] !== $item['url']) {
+ $res[$m['url']] = $item['url'];
+ }
+ break;
+ }
}
}
- file_put_contents($f->getPathname(), $html);
+ return $res;
}
protected function getTextTranslationsForCompilation($locale)
if (!isset($pages[$item['id']])) {
continue;
}
+ if (!$item['slug']) {
+ $item['url'] = '/index.html';
+ } else {
+ $e = explode('/', $item['url']);
+ $p = array_pop($e);
+ $p = $item['slug'] . '.html';
+ array_push($e, $p);
+ $item['url'] = implode('/', $e);
+ }
$translation[] = $item;
}
}
return strcmp($a['url'], $b['url']);
});
-
+
$this->setTranslation('seo', $locale, $translation);
}