Console.php
1 year ago
Csv.php
1 year ago
Html.php
1 year ago
Json.php
1 year ago
Rss.php
1 year ago
Tsv.php
2 years ago
Xml.php
1 year ago
Rss.php
153 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 | * @return string |
| 30 | */ |
| 31 | public function render() |
| 32 | { |
| 33 | return $this->renderTable($this->table); |
| 34 | } |
| 35 | /** |
| 36 | * Computes the output for the given data table |
| 37 | * |
| 38 | * @param DataTable $table |
| 39 | * @return string |
| 40 | * @throws Exception |
| 41 | */ |
| 42 | protected function renderTable($table) |
| 43 | { |
| 44 | if (!$table instanceof DataTable\Map || $table->getKeyName() != 'date') { |
| 45 | 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."); |
| 46 | } |
| 47 | $idSite = Common::getRequestVar('idSite', 1, 'int'); |
| 48 | $period = Common::getRequestVar('period'); |
| 49 | $piwikUrl = SettingsPiwik::getPiwikUrl() . "?module=CoreHome&action=index&idSite=" . $idSite . "&period=" . $period; |
| 50 | $out = ""; |
| 51 | $moreRecentFirst = array_reverse($table->getDataTables(), \true); |
| 52 | foreach ($moreRecentFirst as $date => $subtable) { |
| 53 | /** @var DataTable $subtable */ |
| 54 | $timestamp = $subtable->getMetadata(Archive\DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getDateStart()->getTimestamp(); |
| 55 | $site = $subtable->getMetadata(Archive\DataTableFactory::TABLE_METADATA_SITE_INDEX); |
| 56 | $pudDate = date('r', $timestamp); |
| 57 | $dateInSiteTimezone = Date::factory($timestamp); |
| 58 | if ($site) { |
| 59 | $dateInSiteTimezone = $dateInSiteTimezone->setTimezone($site->getTimezone()); |
| 60 | } |
| 61 | $dateInSiteTimezone = $dateInSiteTimezone->toString('Y-m-d'); |
| 62 | $thisPiwikUrl = Common::sanitizeInputValue($piwikUrl . "&date={$dateInSiteTimezone}"); |
| 63 | $siteName = $site ? $site->getName() : ''; |
| 64 | $title = $siteName . " on " . $date; |
| 65 | $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>"; |
| 66 | $out .= Common::sanitizeInputValue($this->renderDataTable($subtable)); |
| 67 | $out .= "</description>\n\t</item>\n"; |
| 68 | } |
| 69 | $header = $this->getRssHeader(); |
| 70 | $footer = $this->getRssFooter(); |
| 71 | return $header . $out . $footer; |
| 72 | } |
| 73 | /** |
| 74 | * Returns the RSS file footer |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | protected function getRssFooter() |
| 79 | { |
| 80 | return "\t</channel>\n</rss>"; |
| 81 | } |
| 82 | /** |
| 83 | * Returns the RSS file header |
| 84 | * |
| 85 | * @return string |
| 86 | */ |
| 87 | protected function getRssHeader() |
| 88 | { |
| 89 | $generationDate = date('r', Date::getNowTimestamp()); |
| 90 | $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"; |
| 91 | return $header; |
| 92 | } |
| 93 | /** |
| 94 | * @param DataTable $table |
| 95 | * |
| 96 | * @return string |
| 97 | */ |
| 98 | protected function renderDataTable($table) |
| 99 | { |
| 100 | if ($table->getRowsCount() == 0) { |
| 101 | return "<strong><em>Empty table</em></strong><br />\n"; |
| 102 | } |
| 103 | $i = 1; |
| 104 | $tableStructure = array(); |
| 105 | /* |
| 106 | * table = array |
| 107 | * ROW1 = col1 | col2 | col3 | metadata | idSubTable |
| 108 | * ROW2 = col1 | col2 (no value but appears) | col3 | metadata | idSubTable |
| 109 | * subtable here |
| 110 | */ |
| 111 | $allColumns = array(); |
| 112 | foreach ($table->getRows() as $row) { |
| 113 | foreach ($row->getColumns() as $column => $value) { |
| 114 | // for example, goals data is array: not supported in export RSS |
| 115 | // in the future we shall reuse ViewDataTable for html exports in RSS anyway |
| 116 | if (is_array($value) || is_object($value)) { |
| 117 | continue; |
| 118 | } |
| 119 | $allColumns[$column] = \true; |
| 120 | $tableStructure[$i][$column] = $value; |
| 121 | } |
| 122 | $i++; |
| 123 | } |
| 124 | $html = "\n"; |
| 125 | $html .= "<table border=1 width=70%>"; |
| 126 | $html .= "\n<tr>"; |
| 127 | foreach ($allColumns as $name => $toDisplay) { |
| 128 | if ($toDisplay !== \false) { |
| 129 | if ($this->translateColumnNames) { |
| 130 | $name = $this->translateColumnName($name); |
| 131 | } |
| 132 | $html .= "\n\t<td><strong>{$name}</strong></td>"; |
| 133 | } |
| 134 | } |
| 135 | $html .= "\n</tr>"; |
| 136 | foreach ($tableStructure as $row) { |
| 137 | $html .= "\n\n<tr>"; |
| 138 | foreach ($allColumns as $columnName => $toDisplay) { |
| 139 | if ($toDisplay !== \false) { |
| 140 | $value = "-"; |
| 141 | if (isset($row[$columnName])) { |
| 142 | $value = urldecode($row[$columnName]); |
| 143 | } |
| 144 | $html .= "\n\t<td>{$value}</td>"; |
| 145 | } |
| 146 | } |
| 147 | $html .= "</tr>"; |
| 148 | } |
| 149 | $html .= "\n\n</table>"; |
| 150 | return $html; |
| 151 | } |
| 152 | } |
| 153 |