Add "canonical" link to every html page
authorChristian Weiske <cweiske@cweiske.de>
Thu, 10 Mar 2022 11:13:45 +0000 (12:13 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 10 Mar 2022 11:13:45 +0000 (12:13 +0100)
bin/build-html.php
data/templates/allgames.tpl.php
data/templates/discover.tpl.php
data/templates/game.tpl.php

index 1f6c87cdbf4248a28d0569ad53761171089559dd..5f601bf57cb892fd736e68111255bc84ee3c419c 100755 (executable)
@@ -31,7 +31,7 @@ foreach (glob($gameDetailsDir . '*.json') as $gameDataFile) {
     $htmlFile = basename($gameDataFile, '.json') . '.htm';
     file_put_contents(
         $wwwGameDir . $htmlFile,
-        renderGameFile($gameDataFile)
+        renderGameFile($gameDataFile, 'game/' . $htmlFile)
     );
 }
 
@@ -42,17 +42,17 @@ foreach (glob($discoverDir . '*.json') as $discoverFile) {
     }
     file_put_contents(
         $wwwDiscoverDir . $htmlFile,
-        renderDiscoverFile($discoverFile)
+        renderDiscoverFile($discoverFile, 'discover/' . $htmlFile)
     );
 }
 
 file_put_contents(
     $wwwDiscoverDir . 'allgames.htm',
-    renderAllGamesList(glob($gameDetailsDir . '*.json'))
+    renderAllGamesList(glob($gameDetailsDir . '*.json'), 'discover/allgames.htm')
 );
 
 
-function renderAllGamesList($detailsFiles)
+function renderAllGamesList($detailsFiles, $path)
 {
     $games = [];
     foreach ($detailsFiles as $gameDataFile) {
@@ -77,6 +77,7 @@ function renderAllGamesList($detailsFiles)
     $navLinks = [
         './' => 'back',
     ];
+    $canonicalUrl = $GLOBALS['baseUrl'] . $path;
 
     $allGamesTemplate = __DIR__ . '/../data/templates/allgames.tpl.php';
     ob_start();
@@ -87,7 +88,7 @@ function renderAllGamesList($detailsFiles)
     return $html;
 }
 
-function renderDiscoverFile($discoverFile)
+function renderDiscoverFile($discoverFile, $path)
 {
     $json = json_decode(file_get_contents($discoverFile));
 
@@ -140,6 +141,11 @@ function renderDiscoverFile($discoverFile)
         $navLinks['./'] = 'discover';
     }
 
+    if ($path === 'discover/index.htm') {
+        $path = 'discover/';
+    }
+    $canonicalUrl = $GLOBALS['baseUrl'] . $path;
+
     $discoverTemplate = __DIR__ . '/../data/templates/discover.tpl.php';
     ob_start();
     include $discoverTemplate;
@@ -149,7 +155,7 @@ function renderDiscoverFile($discoverFile)
     return $html;
 }
 
-function renderGameFile($gameDataFile)
+function renderGameFile($gameDataFile, $path)
 {
     $json = json_decode(file_get_contents($gameDataFile));
 
@@ -180,6 +186,7 @@ function renderGameFile($gameDataFile)
 
     $internetArchiveUrl = $json->stouyapi->{'internet-archive'} ?? null;
     $developerUrl       = $json->stouyapi->{'developer-url'} ?? null;
+    $canonicalUrl       = $GLOBALS['baseUrl'] . $path;
 
     $pushUrl = $GLOBALS['pushToMyOuyaUrl']
         . '?game=' . urlencode($json->apk->package);
index 0b838162dd0663378300ab5c80dd51d69d97ba3b..1685a37e54cdfeb7f21fbc2a4d8d7591ed11c02e 100644 (file)
@@ -8,6 +8,7 @@
   <link rel="stylesheet" type="text/css" href="../datatables/jquery.dataTables.yadcf.css"/>
   <link rel="stylesheet" type="text/css" href="../ouya-allgames.css"/>
   <link rel="icon" href="../favicon.ico"/>
+  <link rel="canonical" href="<?= htmlspecialchars($canonicalUrl) ?>"/>
  </head>
  <body class="allgames">
   <header>
index 7184837012ea0553f27df1a06cab6bc4872748a0..a13918b485cf50b2590718df1846babdcfea44ad 100644 (file)
@@ -6,6 +6,7 @@
   <meta name="generator" content="stouyapi"/>
   <link rel="stylesheet" type="text/css" href="../ouya-discover.css"/>
   <link rel="icon" href="../favicon.ico"/>
+  <link rel="canonical" href="<?= htmlspecialchars($canonicalUrl) ?>"/>
  </head>
  <body class="discover">
   <header>
index ee41ebe67010a5d78c81243a1faa11b324348167..04b6317c6f8a56df3cc7724dd63f8630176278ab 100644 (file)
@@ -7,6 +7,7 @@
   <meta name="author" content="<?= htmlspecialchars($json->developer->name) ?>"/>
   <link rel="stylesheet" type="text/css" href="../ouya-game.css"/>
   <link rel="icon" href="../favicon.ico"/>
+  <link rel="canonical" href="<?= htmlspecialchars($canonicalUrl) ?>"/>
   <meta name="twitter:card" content="summary_large_image"/>
   <meta property="og:title" content="<?= htmlspecialchars($json->title); ?>" />
   <meta property="og:description" content="<?= htmlspecialchars(substr(strtok($json->description, '.!'), 0, 200)); ?>." />