Skip to content

Instantly share code, notes, and snippets.

@mopsicus

mopsicus/turbo_aegea.php

Last active Mar 24, 2021
Embed
What would you like to do?
Generate RSS channel file Yandex turbo for Aegea blogengine
<?
$db_user = ''; // db user name
$db_password = ''; // db pass
$db_database = ''; // db name
$db_host = 'localhost'; // db url
$db = new mysqli($db_host, $db_user, $db_password, $db_database);
$result = $db->query("SELECT * FROM `notes` WHERE IsPublished = 1 AND isvisible = 1 ORDER BY `Stamp` DESC LIMIT 1");
$row = $result->fetch_assoc();
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= "\n";
$xml .= '<rss xmlns:yandex="http://news.yandex.ru" xmlns:media="http://search.yahoo.com/mrss/" xmlns:turbo="http://turbo.yandex.ru" version="2.0">';
$xml .= "\n";
$xml .= '<channel>';
$xml .= "\n";
$xml .= '<title>#mopsicus.ru – про Unity, игры и разработку</title>';
$xml .= "\n";
$xml .= '<link>https://mopsicus.ru</link>';
$xml .= "\n";
$xml .= '<description>Блог о Unity, разработке и личном опыте</description>';
$xml .= "\n";
$xml .= '<language>ru</language>';
$xml .= "\n";
$xml .= '<turbo:analytics type="Yandex" id="36189005"></turbo:analytics>';
$xml .= "\n";
$xml .= '<item turbo="true">';
$xml .= "\n";
$xml .= "<link>https://$domain</link>";
$xml .= "\n";
$xml .= "<pubDate>".date('r', $row['LastModified'])."</pubDate>";
$xml .= "\n";
$xml .= '<author>Игорь Лопатин</author>';
$xml .= "\n";
$xml .= '<turbo:content>';
$xml .= "\n";
$xml .= '<![CDATA[';
$xml .= "\n";
$xml .= "<header>";
$xml .= "\n";
$xml .= "<h1>#mopsicus.ru</h1>";
$xml .= "\n";
$xml .= "<h2>Блог об играх, разработке на Unity и личном опыте</h2>";
$xml .= "\n";
$xml .= "<figure>";
$xml .= "\n";
$xml .= "<img src=\"https://$domain/user/userpic@2x.jpg\"/>";
$xml .= "\n";
$xml .= "</figure>";
$xml .= "\n";
$xml .= "<menu>";
$xml .= "\n";
$xml .= "<a href=\"https://$domain/selected/\">Избранное</a>";
$xml .= "\n";
$xml .= "<a href=\"https://$domain/all/\">Все заметки</a>";
$xml .= "\n";
$xml .= "<a href=\"https://$domain/all/monthly-newsletter-unity/\">Рассылка</a>";
$xml .= "\n";
$xml .= "<a href=\"http://github.com/mopsicus\">Мой Github</a>";
$xml .= "\n";
$xml .= "<a href=\"https://$domain/tags/projects/\">Проекты</a>";
$xml .= "\n";
$xml .= "<a href=\"https://$domain/all/about/\">Обо мне</a>";
$xml .= "\n";
$xml .= "</menu>";
$xml .= "\n";
$xml .= "</header>";
$xml .= "\n";
$result = $db->query("SELECT * FROM `notes` WHERE IsPublished = 1 AND isvisible = 1 ORDER BY `Stamp` DESC LIMIT 10");
while ($row = $result->fetch_assoc()) {
$body = iconv('windows-1251', 'UTF-8', $row['Text']);
$title = htmlspecialchars (iconv('windows-1251', 'UTF-8', $row['Title']), ENT_NOQUOTES);
$xml .= "<h3>$title</h3>";
$body = nl2br($body, false);
$body = '<p>' . preg_replace('#(<br>[\r\n\s]+){2}#', "</p>\n\n<p>", $body) . '</p>';
$xml .= $body;
$xml .= "<br /><br />\n";
}
$xml .= "\n";
$xml .= ']]>';
$xml .= "\n";
$xml .= '</turbo:content>';
$xml .= "\n";
$xml .= '</item>';
$xml .= "\n\n";
$result = $db->query("SELECT * FROM `notes` WHERE IsPublished = 1 AND isvisible = 1");
while ($row = $result->fetch_assoc()) {
$page = $row['OriginalAlias'];
$title = htmlspecialchars (iconv('windows-1251', 'UTF-8', $row['Title']), ENT_NOQUOTES);
$body = iconv('windows-1251', 'UTF-8', $row['Text']);
$xml .= '<item turbo="true">';
$xml .= "\n";
$xml .= "<link>https://$domain/all/$page</link>";
$xml .= "\n";
$xml .= "<pubDate>".date('r', $row['LastModified'])."</pubDate>";
$xml .= "\n";
$xml .= '<author>Игорь Лопатин</author>';
$xml .= "\n";
$xml .= '<turbo:content>';
$xml .= "\n";
$xml .= '<![CDATA[';
$xml .= "\n";
$xml .= "<header>";
$xml .= "\n";
$xml .= "<h1>$title</h1>";
$xml .= "\n";
$xml .= "</header>";
$xml .= "\n";
$body = nl2br($body, false);
// $body = '<p>' . preg_replace('#(<br>[\r\n\s]+){2}#', "</p>\n\n<p>", $body) . '</p>';
$body .= "<p>\n";
$body = preg_replace('#(<br>[\r\n\s]+){2}#', "\n</p>\n\n<p>\n", $body);
$body .= "</p>";
$body = addFullPathes($body);
$xml .= $body;
$xml .= "\n";
$xml .= ']]>';
$xml .= "\n";
$xml .= '</turbo:content>';
$xml .= "\n";
$xml .= '</item>';
$xml .= "\n\n";
}
$xml .= "\n";
$xml .= '</channel>';
$xml .= "\n";
$xml .= '</rss>';
$db->close();
$fp = fopen('/home/users/m/mops/domains/mopsicus.ru/turbo.xml', 'w');
fwrite($fp, $xml);
fclose($fp);
echo $xml;
function addFullPathes($text) {
$pattern = '/([0-9a-zA-Z\-]+\.)(jpg|png|gif|jpeg) (.*)/';
$suffix = 'https://mopsicus.ru/pictures/';
$result = preg_replace_callback($pattern, function ($matches) use ($suffix) {
return "<figure>\n<img src=\"".$suffix.$matches[1].$matches[2]."\" />\n<figcaption>".$matches[3]."</figcaption>\n</figure>\n";
}, $text);
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment