Console.php
2 months ago
Csv.php
1 month ago
Html.php
2 months ago
Json.php
2 weeks ago
Rss.php
2 weeks ago
Tsv.php
2 months ago
Xml.php
2 weeks ago
Rss.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\DataTable\Renderer; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\Archive; |
| 13 | use Piwik\Common; |
| 14 | use Piwik\DataTable\Renderer; |
| 15 | use Piwik\DataTable; |
| 16 | use Piwik\Date; |
| 17 | use Piwik\SettingsPiwik; |
| 18 | /** |
| 19 | * RSS Feed. |
| 20 | * The RSS renderer can be used only on Set that are arrays of DataTable. |
| 21 | * A RSS feed contains one dataTable per element in the Set. |
| 22 | * |
| 23 | */ |
| 24 | class Rss extends Renderer |
| 25 | { |
| 26 | /** |
| 27 | * Computes the dataTable output and returns the string/binary |
| 28 | */ |
| 29 | public function render() : string |
| 30 | { |
| 31 | return $this->renderTable($this->table); |
| 32 | } |
| 33 | /** |
| 34 | * Computes the output for the given data table |
| 35 | * |
| 36 | * @param DataTable\Map $table Must be keyed by 'date'. |
| 37 | */ |
| 38 | protected function renderTable($table) : string |
| 39 | { |
| 40 | if (!$table instanceof DataTable\Map || $table->getKeyName() != 'date') { |
| 41 | throw new Exception("RSS feeds can be generated for one specific website &idSite=X." . "\nPlease specify only one idSite or consider using &format=XML instead."); |
| 42 | } |
| 43 | $idSite = Common::getRequestVar('idSite', 1, 'int'); |
| 44 | $period = Common::getRequestVar('period'); |
| 45 | $piwikUrl = SettingsPiwik::getPiwikUrl() . "?module=CoreHome&action=index&idSite=" . $idSite . "&period=" . $period; |
| 46 | $out = ""; |
| 47 | $moreRecentFirst = array_reverse($table->getDataTables(), \true); |
| 48 | foreach ($moreRecentFirst as $date => $subtable) { |
| 49 | /** @var DataTable $subtable */ |
| 50 | $timestamp = $subtable->getMetadata(Archive\DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getDateStart()->getTimestamp(); |
| 51 | $site = $subtable->getMetadata(Archive\DataTableFactory::TABLE_METADATA_SITE_INDEX); |
| 52 | $pudDate = date('r', $timestamp); |
| 53 | $dateInSiteTimezone = Date::factory($timestamp); |
| 54 | if ($site) { |
| 55 | $dateInSiteTimezone = $dateInSiteTimezone->setTimezone($site->getTimezone()); |
| 56 | } |
| 57 | $dateInSiteTimezone = $dateInSiteTimezone->toString('Y-m-d'); |
| 58 | $thisPiwikUrl = Common::sanitizeInputValue($piwikUrl . "&date={$dateInSiteTimezone}"); |
| 59 | $siteName = $site ? $site->getName() : ''; |
| 60 | $title = self::formatValueXml($siteName . " on " . $date); |
| 61 | $out .= "\t<item>\n\t\t<pubDate>{$pudDate}</pubDate>\n\t\t<guid>{$thisPiwikUrl}</guid>\n\t\t<link>{$thisPiwikUrl}</link>\n\t\t<title>{$title}</title>\n\t\t<author>https://matomo.org</author>\n\t\t<description>"; |
| 62 | $out .= Common::sanitizeInputValue($this->renderDataTable($subtable)); |
| 63 | $out .= "</description>\n\t</item>\n"; |
| 64 | } |
| 65 | $header = $this->getRssHeader(); |
| 66 | $footer = $this->getRssFooter(); |
| 67 | return $header . $out . $footer; |
| 68 | } |
| 69 | /** |
| 70 | * Returns the RSS file footer |
| 71 | */ |
| 72 | protected function getRssFooter() : string |
| 73 | { |
| 74 | return "\t</channel>\n</rss>"; |
| 75 | } |
| 76 | /** |
| 77 | * Returns the RSS file header |
| 78 | */ |
| 79 | protected function getRssHeader() : string |
| 80 | { |
| 81 | $generationDate = date('r', Date::getNowTimestamp()); |
| 82 | $header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rss version=\"2.0\">\n <channel>\n <title>matomo statistics - RSS</title>\n <link>https://matomo.org</link>\n <description>Matomo RSS feed</description>\n <pubDate>{$generationDate}</pubDate>\n <generator>matomo</generator>\n <language>en</language>\n <lastBuildDate>{$generationDate}</lastBuildDate>\n"; |
| 83 | return $header; |
| 84 | } |
| 85 | /** |
| 86 | * @param DataTable $table |
| 87 | * |
| 88 | * @return string |
| 89 | */ |
| 90 | protected function renderDataTable($table) |
| 91 | { |
| 92 | if ($table->getRowsCount() == 0) { |
| 93 | return "<strong><em>Empty table</em></strong><br />\n"; |
| 94 | } |
| 95 | $i = 1; |
| 96 | $tableStructure = array(); |
| 97 | /* |
| 98 | * table = array |
| 99 | * ROW1 = col1 | col2 | col3 | metadata | idSubTable |
| 100 | * ROW2 = col1 | col2 (no value but appears) | col3 | metadata | idSubTable |
| 101 | * subtable here |
| 102 | */ |
| 103 | $allColumns = array(); |
| 104 | foreach ($table->getRows() as $row) { |
| 105 | foreach ($row->getColumns() as $column => $value) { |
| 106 | // for example, goals data is array: not supported in export RSS |
| 107 | // in the future we shall reuse ViewDataTable for html exports in RSS anyway |
| 108 | if (is_array($value) || is_object($value)) { |
| 109 | continue; |
| 110 | } |
| 111 | $allColumns[$column] = \true; |
| 112 | $tableStructure[$i][$column] = $value; |
| 113 | } |
| 114 | $i++; |
| 115 | } |
| 116 | $html = "\n"; |
| 117 | $html .= "<table border=1 width=70%>"; |
| 118 | $html .= "\n<tr>"; |
| 119 | foreach ($allColumns as $name => $toDisplay) { |
| 120 | if ($toDisplay !== \false) { |
| 121 | if ($this->translateColumnNames) { |
| 122 | $name = $this->translateColumnName($name); |
| 123 | } |
| 124 | $html .= "\n\t<td><strong>" . self::formatValueXml($name) . "</strong></td>"; |
| 125 | } |
| 126 | } |
| 127 | $html .= "\n</tr>"; |
| 128 | foreach ($tableStructure as $row) { |
| 129 | $html .= "\n\n<tr>"; |
| 130 | foreach ($allColumns as $columnName => $toDisplay) { |
| 131 | if ($toDisplay !== \false) { |
| 132 | $value = "-"; |
| 133 | if (isset($row[$columnName])) { |
| 134 | $value = self::formatValueXml(urldecode($row[$columnName])); |
| 135 | } |
| 136 | $html .= "\n\t<td>{$value}</td>"; |
| 137 | } |
| 138 | } |
| 139 | $html .= "</tr>"; |
| 140 | } |
| 141 | $html .= "\n\n</table>"; |
| 142 | return $html; |
| 143 | } |
| 144 | } |
| 145 |