Skip to content

Commit

Permalink
Refs #4125, rename Referers_... record names to Referrers and make su…
Browse files Browse the repository at this point in the history
…re code is backwards compatible.
  • Loading branch information
Benaka Moorthi committed Oct 4, 2013
1 parent cefbc7f commit 0deb2bb
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 18 deletions.
4 changes: 4 additions & 0 deletions core/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ public static function getPluginForReport($report)
}

$plugin = substr($report, 0, strpos($report, '_'));
if ($plugin == 'Referrers') { // TODO: remove when Referers plugin name is changed
$plugin = 'Referers';
}

if (empty($plugin)
|| !\Piwik\PluginsManager::getInstance()->isPluginActivated($plugin)
) {
Expand Down
98 changes: 90 additions & 8 deletions plugins/Referers/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Piwik\Common;
use Piwik\Date;
use Piwik\DataTable;
use Piwik\DataTable\Map;
use Piwik\Plugins\Referers\Archiver;

/**
Expand Down Expand Up @@ -56,11 +57,65 @@ static public function getInstance()
protected function getDataTable($name, $idSite, $period, $date, $segment, $expanded = false, $idSubtable = null)
{
$dataTable = Archive::getDataTableFromArchive($name, $idSite, $period, $date, $segment, $expanded, $idSubtable);

// backwards compatibility: before v2.0, Referrers_... blobs were named Referers_... . here we
// check if we're missing blobs w/ the correct name, and if so, try to use the old name.
$dataMissing = true;
$dataTable->filter(function ($table) use (&$dataMissing) {
if ($table->getRowsCount() > 0) {
$dataMissing = false;
}
});

if ($dataMissing) {
$oldName = $this->getOldReferrersRecordName($name);
$oldData = Archive::getDataTableFromArchive($oldName, $idSite, $period, $date, $segment, $expanded, $idSubtable);

if ($dataTable instanceof DataTable) {
$dataTable = $oldData;
} else {
$this->replaceEmptyDataTablesWith($dataTable, $oldData);
}
}

$dataTable->filter('Sort', array(Metrics::INDEX_NB_VISITS, 'desc', $naturalSort = false, $expanded));
$dataTable->queueFilter('ReplaceColumnNames');
return $dataTable;
}

private function getNumeric($name, $idSite, $period, $date, $segment)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
$dataTable = $archive->getDataTableFromNumeric($name);

// backwards compatibility: before v2.0, Referrers_... metrics were named Referers_... . here we
// check if we're missing metrics w/ the correct name, and if so, try to use the old name.
// NOTE: getDataTableFromNumeric will return a DataTable w/ data even if no metrics are found.
// In this case, there will be one row w/ column values of 0.
$dataMissing = true;
$dataTable->filter(function ($table) use (&$dataMissing, $name) {
if ($table->getRowsCount() > 0
&& $table->getFirstRow()->getColumn($name) != 0
) {
$dataMissing = false;
}
});

if ($dataMissing) {
$oldName = $this->getOldReferrersRecordName($name);
$oldData = $archive->getDataTableFromNumeric($oldName);

if ($dataTable instanceof DataTable) {
$dataTable = $oldData;
} else {
$this->replaceEmptyDataTablesWith($dataTable, $oldData);
}
}

return $dataTable;
}

/**
* Returns a report describing visit information for each possible referrer type. The
* result is a datatable whose subtables are the reports for each parent row's referrer type.
Expand Down Expand Up @@ -416,13 +471,6 @@ public function getNumberOfDistinctWebsitesUrls($idSite, $period, $date, $segmen
return $this->getNumeric(Archiver::METRIC_DISTINCT_URLS_RECORD_NAME, $idSite, $period, $date, $segment);
}

private function getNumeric($name, $idSite, $period, $date, $segment)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
return $archive->getDataTableFromNumeric($name);
}

/**
* Removes idsubdatatable_in_db metadata from a DataTable. Used by Social tables since
* they use fake subtable IDs.
Expand Down Expand Up @@ -534,4 +582,38 @@ private function setGetReferrerTypeSubtables($dataTable, $idSite, $period, $date
}
}
}
}

/**
* Replaces empty DataTables in a DataTable Map with DataTables in another DataTable Map.
* Only replaces DataTables if DataTables in $replaceIn are empty.
*
* @param Map $replaceIn
* @param Map $replaceWith
*/
private function replaceEmptyDataTablesWith(&$replaceIn, $replaceWith)
{
foreach ($replaceWith->getArray() as $label => $replaceWithChildTable) {
if ($replaceWithChildTable instanceof Map) { // recurse
$this->replaceEmptyDataTablesWith($replaceIn->getTable($label), $replaceWithChildTable);
} else {
$replaceInChildTable = $replaceIn->getTable($label);

if (!$this->isDataTableEmpty($replaceWithChildTable)
&& $this->isDataTableEmpty($replaceInChildTable)
) {
$replaceIn->addTable($replaceWithChildTable, $label);
}
}
}
}

private function getOldReferrersRecordName($name)
{
return str_replace('Referrers', 'Referers', $name);
}

private function isDataTableEmpty($table)
{
return empty($table) || $table->getRowsCount() == 0;
}
}
21 changes: 11 additions & 10 deletions plugins/Referers/Archiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@

class Archiver extends PluginsArchiver
{
const SEARCH_ENGINES_RECORD_NAME = 'Referers_keywordBySearchEngine';
const KEYWORDS_RECORD_NAME = 'Referers_searchEngineByKeyword';
const CAMPAIGNS_RECORD_NAME = 'Referers_keywordByCampaign';
const WEBSITES_RECORD_NAME = 'Referers_urlByWebsite';
const REFERRER_TYPE_RECORD_NAME = 'Referers_type';
const METRIC_DISTINCT_SEARCH_ENGINE_RECORD_NAME = 'Referers_distinctSearchEngines';
const METRIC_DISTINCT_KEYWORD_RECORD_NAME = 'Referers_distinctKeywords';
const METRIC_DISTINCT_CAMPAIGN_RECORD_NAME = 'Referers_distinctCampaigns';
const METRIC_DISTINCT_WEBSITE_RECORD_NAME = 'Referers_distinctWebsites';
const METRIC_DISTINCT_URLS_RECORD_NAME = 'Referers_distinctWebsitesUrls';
const SEARCH_ENGINES_RECORD_NAME = 'Referrers_keywordBySearchEngine';
const KEYWORDS_RECORD_NAME = 'Referrers_searchEngineByKeyword';
const CAMPAIGNS_RECORD_NAME = 'Referrers_keywordByCampaign';
const WEBSITES_RECORD_NAME = 'Referrers_urlByWebsite';
const REFERRER_TYPE_RECORD_NAME = 'Referrers_type';
const METRIC_DISTINCT_SEARCH_ENGINE_RECORD_NAME = 'Referrers_distinctSearchEngines';
const METRIC_DISTINCT_KEYWORD_RECORD_NAME = 'Referrers_distinctKeywords';
const METRIC_DISTINCT_CAMPAIGN_RECORD_NAME = 'Referrers_distinctCampaigns';
const METRIC_DISTINCT_WEBSITE_RECORD_NAME = 'Referrers_distinctWebsites';
const METRIC_DISTINCT_URLS_RECORD_NAME = 'Referrers_distinctWebsitesUrls';

protected $columnToSortByBeforeTruncation;
protected $maximumRowsInDataTableLevelZero;
protected $maximumRowsInSubDataTable;
Expand Down

0 comments on commit 0deb2bb

Please sign in to comment.