setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Start building the HTML string
$html = '';
$html .= '';
$html .= '
';
$html .= 'YOURLS HTML Sitemap';
$html .= '';
$html .= '';
$html .= 'YOURLS HTML Sitemap
';
$html .= '';
// Check if the 'private' column exists in the YOURLS database table
$table_name = YOURLS_DB_TABLE_URL;
$stmt = $pdo->query("DESCRIBE `$table_name`");
$private_column_exists = false;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($row['Field'] === 'private') {
$private_column_exists = true;
break;
}
}
if ($private_column_exists) {
// Retrieve all public short URLs
$stmt = $pdo->prepare("SELECT `keyword`, `url`, `timestamp` FROM `$table_name` WHERE `private` = '0'");
$stmt->execute();
} else {
// Retrieve all short URLs (assuming all are public)
$stmt = $pdo->prepare("SELECT `keyword`, `url`, `timestamp` FROM `$table_name`");
$stmt->execute();
}
// Loop through each link and add it to the HTML sitemap
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$shorturl = yourls_link($row['keyword']);
$longurl = htmlspecialchars($row['url']);
$timestamp = date('c', strtotime($row['timestamp']));
$html .= '- ';
$html .= '' . $shorturl . '';
$html .= ' - ' . $longurl . ' - Last Modified: ' . $timestamp;
$html .= '
';
}
// Close the HTML
$html .= '
';
$html .= '';
$html .= '';
// Save the HTML to sitemap.html file
file_put_contents(YOURLS_ABSPATH . '/sitemap.html', $html);
} catch (PDOException $e) {
// Handle PDO exceptions
yourls_die('Error: ' . $e->getMessage());
}
}