Skip to content

Commit

Permalink
Remove PHP response format
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Feb 12, 2020
1 parent 924c9e1 commit 72d6d97
Show file tree
Hide file tree
Showing 29 changed files with 207 additions and 1,184 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -37,6 +37,7 @@ The Product Changelog at **[matomo.org/changelog](https://matomo.org/changelog)*
* The event `CustomPiwikJs.shouldAddTrackerFile` has been renamed to `CustomJsTracker.shouldAddTrackerFile`
* Public API class `Piwik\Plugins\CustomPiwikJs\TrackerUpdater` has been renamed to `Piwik\Plugins\CustomJsTracker\TrackerUpdater`
* API method `CustomPiwikJs.doesIncludePluginTrackersAutomatically` has been renamed to `CustomJsTracker.doesIncludePluginTrackersAutomatically`
* The API response format `php` has been removed.

## Matomo 3.13.1

Expand Down
2 changes: 1 addition & 1 deletion core/CliMulti.php
Expand Up @@ -327,7 +327,7 @@ public function isCommandAlreadyRunning($url)
$posStart = strpos($commandToCheck, 'console climulti');
$posPid = strpos($commandToCheck, '&pid='); // the pid is random each time so we need to ignore it.
$shortendCommand = substr($commandToCheck, $posStart, $posPid - $posStart);
// equals eg console climulti:request -q --matomo-domain= --superuser module=API&method=API.get&idSite=1&period=month&date=2018-04-08,2018-04-30&format=php&trigger=archivephp
// equals eg console climulti:request -q --matomo-domain= --superuser module=API&method=API.get&idSite=1&period=month&date=2018-04-08,2018-04-30&format=json&trigger=archivephp
$shortendCommand = preg_replace("/([&])date=.*?(&|$)/", "", $shortendCommand);
$currentlyRunningJobs = preg_replace("/([&])date=.*?(&|$)/", "", $currentlyRunningJobs);

Expand Down
8 changes: 4 additions & 4 deletions core/CronArchive.php
Expand Up @@ -828,7 +828,7 @@ private function processArchiveForPeriods($idSite, $lastTimestampWebsiteProcesse
*/
private function getVisitsRequestUrl($idSite, $period, $date, $segment = false)
{
$request = "?module=API&method=API.get&idSite=$idSite&period=$period&date=" . $date . "&format=php";
$request = "?module=API&method=API.get&idSite=$idSite&period=$period&date=" . $date . "&format=json";
if ($segment) {
$request .= '&segment=' . urlencode($segment);
}
Expand Down Expand Up @@ -908,7 +908,7 @@ protected function processArchiveDays($idSite, $lastTimestampWebsiteProcessedDay
$this->logArchiveWebsite($idSite, "day", $date);

$content = $this->request($url);
$daysResponse = Common::safe_unserialize($content);
$daysResponse = json_decode($content, true);

if (empty($content)
|| !is_array($daysResponse)
Expand Down Expand Up @@ -1139,7 +1139,7 @@ private function archiveReportsFor($idSite, $period, $date, $archiveSegments, Ti
$success = $success && $this->checkResponse($content, $url);

if ($noSegmentUrl == $url && $success) {
$stats = Common::safe_unserialize($content);
$stats = json_decode($content, true);

if (!is_array($stats)) {
$this->logError("Error unserializing the following response from $url: " . $content);
Expand Down Expand Up @@ -1217,7 +1217,7 @@ private function logNetworkError($url, $response)
}

/**
* Issues a request to $url eg. "?module=API&method=API.getDefaultMetricTranslations&format=original&serialize=1"
* Issues a request to $url eg. "?module=API&method=API.getDefaultMetricTranslations&format=json&serialize=1"
*
* @param string $url
* @return string
Expand Down
153 changes: 151 additions & 2 deletions core/DataTable/Renderer.php
Expand Up @@ -11,6 +11,7 @@
use Exception;
use Piwik\Columns\Dimension;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Piwik;
use Piwik\BaseFactory;
Expand Down Expand Up @@ -143,8 +144,7 @@ public function setTable($table)
'json',
'csv',
'tsv',
'html',
'php'
'html'
);

/**
Expand Down Expand Up @@ -379,4 +379,153 @@ protected static function shouldWrapArrayBeforeRendering(

return $wrap;
}

/**
* Produces a flat php array from the DataTable, putting "columns" and "metadata" on the same level.
*
* For example, when a originalRender() would be
* array( 'columns' => array( 'col1_name' => value1, 'col2_name' => value2 ),
* 'metadata' => array( 'metadata1_name' => value_metadata) )
*
* a flatRender() is
* array( 'col1_name' => value1,
* 'col2_name' => value2,
* 'metadata1_name' => value_metadata )
*
* @param null|DataTable|DataTable\Map|Simple $dataTable
* @return array Php array representing the 'flat' version of the datatable
*/
protected function convertDataTableToArray($dataTable = null)
{
if (is_null($dataTable)) {
$dataTable = $this->table;
}

if (is_array($dataTable)) {
$flatArray = $dataTable;
if (self::shouldWrapArrayBeforeRendering($flatArray)) {
$flatArray = array($flatArray);
}
} elseif ($dataTable instanceof DataTable\Map) {
$flatArray = array();
foreach ($dataTable->getDataTables() as $keyName => $table) {
$flatArray[$keyName] = $this->convertDataTableToArray($table);
}
} elseif ($dataTable instanceof Simple) {
$flatArray = $this->convertSimpleTable($dataTable);

reset($flatArray);
$firstKey = key($flatArray);

// if we return only one numeric value then we print out the result in a simple <result> tag
// keep it simple!
if (count($flatArray) == 1
&& $firstKey !== DataTable\Row::COMPARISONS_METADATA_NAME
) {
$flatArray = current($flatArray);
}
} // A normal DataTable needs to be handled specifically
else {
$array = $this->convertTable($dataTable);
$flatArray = $this->flattenArray($array);
}

return $flatArray;
}

/**
* Converts the given data table to an array
*
* @param DataTable $table
* @return array
*/
protected function convertTable($table)
{
$array = [];

foreach ($table->getRows() as $id => $row) {
$newRow = array(
'columns' => $row->getColumns(),
'metadata' => $row->getMetadata(),
'idsubdatatable' => $row->getIdSubDataTable(),
);

if ($id == DataTable::ID_SUMMARY_ROW) {
$newRow['issummaryrow'] = true;
}

if (isset($newRow['metadata'][DataTable\Row::COMPARISONS_METADATA_NAME])) {
$newRow['metadata'][DataTable\Row::COMPARISONS_METADATA_NAME] = $row->getComparisons();
}

$subTable = $row->getSubtable();
if ($this->isRenderSubtables()
&& $subTable
) {
$subTable = $this->convertTable($subTable);
$newRow['subtable'] = $subTable;
if ($this->hideIdSubDatatable === false
&& isset($newRow['metadata']['idsubdatatable_in_db'])
) {
$newRow['columns']['idsubdatatable'] = $newRow['metadata']['idsubdatatable_in_db'];
}
unset($newRow['metadata']['idsubdatatable_in_db']);
}
if ($this->hideIdSubDatatable !== false) {
unset($newRow['idsubdatatable']);
}

$array[] = $newRow;
}
return $array;
}

/**
* Converts the simple data table to an array
*
* @param Simple $table
* @return array
*/
protected function convertSimpleTable($table)
{
$array = [];

$row = $table->getFirstRow();
if ($row === false) {
return $array;
}
foreach ($row->getColumns() as $columnName => $columnValue) {
$array[$columnName] = $columnValue;
}

$comparisons = $row->getComparisons();
if (!empty($comparisons)) {
$array[DataTable\Row::COMPARISONS_METADATA_NAME] = $comparisons;
}

return $array;
}

/**
*
* @param array $array
* @return array
*/
protected function flattenArray($array)
{
$flatArray = [];
foreach ($array as $row) {
$newRow = $row['columns'] + $row['metadata'];
if (isset($row['idsubdatatable'])
&& $this->hideIdSubDatatable === false
) {
$newRow += array('idsubdatatable' => $row['idsubdatatable']);
}
if (isset($row['subtable'])) {
$newRow += array('subtable' => $this->flattenArray($row['subtable']));
}
$flatArray[] = $newRow;
}
return $flatArray;
}
}
12 changes: 0 additions & 12 deletions core/DataTable/Renderer/Json.php
Expand Up @@ -109,18 +109,6 @@ public static function sendHeaderJSON()
Common::sendHeader('Content-Type: application/json; charset=utf-8');
}

private function convertDataTableToArray($table)
{
$renderer = new Php();
$renderer->setTable($table);
$renderer->setRenderSubTables($this->isRenderSubtables());
$renderer->setSerialize(false);
$renderer->setHideIdSubDatableFromResponse($this->hideIdSubDatatable);
$array = $renderer->flatRender();

return $array;
}

private function convertDataTableColumnMetadataValues(&$table)
{
if (empty($table)) {
Expand Down

0 comments on commit 72d6d97

Please sign in to comment.