turbo.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /*
  3. * to do
  4. * https://dzen.ru/help/news/export-content/export.html
  5. * https://yandex.ru/dev/turbo/doc/rss/simple-rss.html
  6. *
  7. */
  8. use yii\helpers\Html;
  9. Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
  10. header('Content-Type: application/rss+xml; charset=utf-8');
  11. ?>
  12. <?php echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL ?>
  13. <rss version="2.0"
  14. xmlns:yandex="http://news.yandex.ru"
  15. xmlns:media="http://search.yahoo.com/mrss/"
  16. xmlns:turbo="http://turbo.yandex.ru"
  17. >
  18. <channel>
  19. <?php if (isset($channel['title'])) : ?>
  20. <title><?= $channel['title']; ?></title>
  21. <?php endif; ?>
  22. <?php if (isset($channel['link'])) : ?>
  23. <link><?= Html::encode($channel['link']); ?></link>
  24. <?php endif; ?>
  25. <?php if (isset($channel['description'])) : ?>
  26. <description><?= $channel['description']; ?></description>
  27. <?php endif; ?>
  28. <?php if (isset($channel['language'])) : ?>
  29. <language><?= $channel['language']; ?></language>
  30. <?php endif; ?>
  31. <?php if (isset($channel['analytics'])) : ?>
  32. <turbo:analytics><?= $channel['analytics']; ?></turbo:analytics>
  33. <?php endif; ?>
  34. <?php if (isset($channel['adnetwork'])) : ?>
  35. <turbo:adNetwork><?= $channel['adnetwork']; ?></turbo:adNetwork>
  36. <?php endif; ?>
  37. <?php foreach ($items as $item): ?>
  38. <?php if ( $item['export_rss'] & 1 ) continue; ?>
  39. <?php if (isset($item['url']) ) : ?>
  40. <item turbo="<?= ($item['status']) ? 'true' : 'false'; ?>">
  41. <link><?= rtrim(Html::encode($channel['link']),'/'); ?><?= Html::encode($item['url']); ?></link>
  42. <?php if (isset($item['title']) ) : ?>
  43. <title><![CDATA[<?= $item['title']; ?>]]></title>
  44. <?php endif; ?>
  45. <?php if (isset($item['description']) ) : ?>
  46. <description>
  47. <![CDATA[
  48. <?= $item['description']; ?>
  49. ]]>
  50. </description>
  51. <?php endif; ?>
  52. <pubDate><?= date( "r", strtotime( $item['updated_at'] ) ); ?></pubDate>
  53. <?php if (isset($item['newscontent']) ) : ?>
  54. <yandex:full-text>
  55. <![CDATA[
  56. <?/*<h1><?= ($item['title']) ? $item['title'] : ''; ?></h1>*/?>
  57. <p><?= ($item['description']) ? $item['description'] : ''; ?></p>
  58. <?= $item['newscontent']; ?>
  59. ]]>
  60. </yandex:full-text>
  61. <?php endif; ?>
  62. <?php if ($item['status'] && isset($item['content'])) : ?>
  63. <turbo:content>
  64. <![CDATA[
  65. <header>
  66. <?/*<h1><?= ($item['title']) ? $item['title'] : ''; ?></h1>*/?>
  67. <p><?= ($item['description']) ? $item['description'] : ''; ?></p>
  68. </header>
  69. <figure data-turbo-ad-id="second_ad_1444026-44"></figure>
  70. <?php if (isset($item['image'])) :?>
  71. <figure>
  72. <img src="<?= $item['image']; ?>" />
  73. <?php if (isset($item['image_title'])) :?>
  74. <figcaption><?= $item['image_title']; ?></figcaption>
  75. <?php endif; ?>
  76. </figure>
  77. <?php endif; ?>
  78. <?= $item['content']; ?>
  79. <figure data-turbo-ad-id="second_ad_1444026-43"></figure>
  80. ]]>
  81. </turbo:content>
  82. <?php endif; ?>
  83. <?php if (isset($item['image'])) :?>
  84. <?
  85. $r = explode(".", $item['image']);
  86. if( is_array($r) ){
  87. if( array_pop($r) == 'webp' ){
  88. $mtype = "image/webp";
  89. }else{
  90. $mtype = "image/jpeg";
  91. }
  92. }
  93. ?>
  94. <enclosure url="<?=$item['image']?>" type="<?=$mtype?>" />
  95. <?php endif; ?>
  96. <?
  97. preg_match_all('/src="([^"]+\.(jpg|png)).*"/i', $item['content'], $ax );
  98. foreach ( $ax[1] as $aa ){
  99. if( !preg_match( "/(\/manager\/|small)/", $aa) ){
  100. $r = explode(".", $aa);
  101. if( is_array($r) ){
  102. if( array_pop($r) == 'png' ){
  103. $mtype = "image/png";
  104. }else{
  105. $mtype = "image/jpeg";
  106. }
  107. }
  108. ?>
  109. <enclosure url="<?=$aa?>" type="<?=$mtype?>" />
  110. <?
  111. }
  112. }
  113. if( count( $item['topics'] ) > 0 ){
  114. ?>
  115. <metrics>
  116. <yandex schema_identifier="<?=$item['uid']?>">
  117. <breadcrumblist>
  118. <?
  119. foreach ( $item['topics'] as $topics ){
  120. ?>
  121. <breadcrumb url="<?=$topics['url']?>" text='<?=str_replace("'",'`',$topics['title'])?>'/>
  122. <?
  123. }
  124. ?>
  125. </breadcrumblist>
  126. </yandex>
  127. </metrics>
  128. <?
  129. }
  130. switch( $item['type'] ){
  131. case 2:
  132. case 6:
  133. // статья
  134. $ganre = "article";
  135. case 7:
  136. case 5:
  137. // статья
  138. $ganre = "article";
  139. default:
  140. $ganre = "message";
  141. break;
  142. }
  143. ?>
  144. <yandex:genre><?=$ganre?></yandex:genre>
  145. <?php if (isset($item['story'])) : ?>
  146. <yandex:theme_tags><?=$item['story'];?></yandex:theme_tags>
  147. <?php endif; ?>
  148. <?php if (isset($item['category'])) : ?>
  149. <category><?=$item['category'];?></category>
  150. <?php endif; ?>
  151. <author><?=$item['author'];?></author>
  152. </item>
  153. <?php endif; ?><?php endforeach; ?>
  154. </channel>
  155. </rss>