Skip to content

Commit

Permalink
fixes for Matomo 5 (#20)
Browse files Browse the repository at this point in the history
added phpstan + phpcs QA. See TESTS.md
  • Loading branch information
Mikael Schirén authored Nov 16, 2023
1 parent 79d63c1 commit 5651276
Show file tree
Hide file tree
Showing 15 changed files with 2,137 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
37 changes: 20 additions & 17 deletions API.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Piwik\Plugins\RerIntranetSubnetwork;

use Piwik\DataTable\Row;

use Piwik\Archive;
use Piwik\DataTable;
use Piwik\Metrics;
Expand All @@ -21,14 +20,13 @@
*/
class API extends \Piwik\Plugin\API
{

public function getIntranetSubnetwork($idSite, $period, $date, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
$dataTable = $archive->getDataTable(Archiver::INTRANETSUBNETWORK_RECORD_NAME);
$dataTable->filter('Sort', array(Metrics::INDEX_NB_VISITS));
$dataTable->queueFilter('ColumnCallbackReplace', ['label', __NAMESPACE__.'\getSubnetName']);
$dataTable->queueFilter('ColumnCallbackReplace', ['label', __NAMESPACE__ . '\getSubnetName']);
$dataTable->queueFilter('ReplaceColumnNames');

$column = 'nb_visits';
Expand All @@ -42,16 +40,13 @@ public function getIntranetSubnetwork($idSite, $period, $date, $segment = false)
$tableArray = $dataTable->getDataTables();
$visitSumsArray = $visitsSums->getDataTables();
} else {
$tableArray = Array($dataTable);
$visitSumsArray = Array($visitsSums);
$tableArray = array($dataTable);
$visitSumsArray = array($visitsSums);
}
// walk through the results and calculate the percentage
foreach ($tableArray as $key => $table)
{
foreach ($visitSumsArray AS $k => $visits)
{
if ($k == $key)
{
foreach ($tableArray as $key => $table) {
foreach ($visitSumsArray as $k => $visits) {
if ($k == $key) {
$visitsSumTotal = (is_object($visits))
?
(float)$visits->getFirstRow()->getColumn($column)
Expand All @@ -65,13 +60,21 @@ public function getIntranetSubnetwork($idSite, $period, $date, $segment = false)
$columnAggregationOps[$percCol] = 'skip';
$table->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnAggregationOps);


$table->filter('ColumnCallbackAddColumnPercentage', array($percCol, Metrics::INDEX_NB_VISITS, $visitsSumTotal, 1));
// we don't want <0% or >100%:
$table->filter('RangeCheck', array($percCol));
if (isset($visitsSumTotal)) {
$table->filter(
'ColumnCallbackAddColumnPercentage',
[
$percCol,
Metrics::INDEX_NB_VISITS,
$visitsSumTotal,
1
]
);
// we don't want <0% or >100%:
$table->filter('RangeCheck', array($percCol));
}
}

return $dataTable;
}

}
}
1 change: 0 additions & 1 deletion Archiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ public function aggregateDayReport()
$metrics = $this->getLogAggregator()->getMetricsFromVisitByDimension(self::INTRANETSUBNETWORK_FIELD)->asDataTable();
$this->getProcessor()->insertBlobRecord(self::INTRANETSUBNETWORK_RECORD_NAME, $metrics->getSerialized());
}

}
11 changes: 8 additions & 3 deletions Columns/IntranetSubnetwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getName()
*/
public function configureSegments(SegmentsList $segmentsList, DimensionSegmentFactory $dimensionSegmentFactory)
{
$segment = new Segment;
$segment = new Segment();
$segment->setSegment('subnetworkName');
$segment->setCategory('General_Visit');
$segment->setName('RerIntranetSubnetwork_SubnetworkName');
Expand All @@ -68,7 +68,7 @@ public function configureSegments(SegmentsList $segmentsList, DimensionSegmentFa
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
// Load system settings
$settings = new SystemSettings;
$settings = new SystemSettings();

// $logger = StaticContainer::getContainer()->get('Psr\Log\LoggerInterface');
// LoggerInterface->debug('RULE: {rule}', ['rule'=>$rule]);
Expand All @@ -82,7 +82,12 @@ public function onNewVisit(Request $request, Visitor $visitor, $action)

// Check if is a common subnetwork
if (true == $settings->subnetwork_default->getValue()) {
if (preg_match('/^127\.0\.0\.1$|^(192\.168|^172\.(1[6789]|2[0-9]|3[01]))(\.\d{1,3}){2}$|^10(\.\d{1,3}){3}$/', $request->getIpString())) {
if (
preg_match(
'/^127\.0\.0\.1$|^(192\.168|^172\.(1[6789]|2[0-9]|3[01]))(\.\d{1,3}){2}$|^10(\.\d{1,3}){3}$/',
$request->getIpString()
)
) {
return 'Intranet';
}
}
Expand Down
7 changes: 2 additions & 5 deletions Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@

class Controller extends \Piwik\Plugin\Controller
{

public function getIntranetSubnetwork($fetch = false)
{
Piwik::checkUserHasSomeViewAccess();
# $this->checkTokenInUrl();

$view = Factory::build( 'pie', 'RerIntranetSubnetwork.getIntranetSubnetwork', $this->pluginName . '.' . __FUNCTION__ );
$view = Factory::build('pie', 'RerIntranetSubnetwork.getIntranetSubnetwork', $this->pluginName . '.' . __FUNCTION__);
# $view = Factory::build( 'table', 'RerIntranetSubnetwork.getIntranetSubnetwork', $this->pluginName . '.' . __FUNCTION__ );
# $view = Factory::build( $this->pluginName, "RerIntranetSubnetwork.getIntranetSubnetwork", $this->pluginName . '.' . __FUNCTION__ );
$this->setPeriodVariablesView($view);
$column = 'nb_visits';
$percCol = 'nb_visits_percentage';
$percColName = 'General_ColumnPercentageVisits';
if ($view->period == 'day')
{
if ($view->period == 'day') {
$column = 'nb_uniq_visitors';
}
$view->config->columns_to_display = ['label', $percCol, $column ];
Expand All @@ -33,5 +31,4 @@ public function getIntranetSubnetwork($fetch = false)

return $view->render();
}

}
15 changes: 7 additions & 8 deletions Reports/GetIntranetSubnetwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function init()

// If a subcategory is specified, the report will be displayed in the menu under this menu item
// $this->subcategoryId = 'RerIntranetSubnetwork_Title';
$this->subcategoryId = $this->name;
$this->subcategoryId = $this->name;
}

/**
Expand Down Expand Up @@ -103,12 +103,11 @@ public function isEnabled()
*/

/**
public function configureWidgets(WidgetsList $widgetsList, ReportWidgetFactory $factory)
{
// we have to do it manually since it's only done automatically if a subcategoryId is specified,
// we do not set a subcategoryId since this report is not supposed to be shown in the UI
$widgetsList->addWidgetConfig($factory->createWidget());
}
public function configureWidgets(WidgetsList $widgetsList, ReportWidgetFactory $factory)
{
// we have to do it manually since it's only done automatically if a subcategoryId is specified,
// we do not set a subcategoryId since this report is not supposed to be shown in the UI
$widgetsList->addWidgetConfig($factory->createWidget());
}
*/

}
1 change: 0 additions & 1 deletion RerIntranetSubnetwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

class RerIntranetSubnetwork extends \Piwik\Plugin
{

}
20 changes: 11 additions & 9 deletions SystemSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ protected function init()

private function createSubnetworkDefaultSetting()
{
return $this->makeSetting('subnetwork_default',
return $this->makeSetting(
'subnetwork_default',
true,
FieldConfig::TYPE_BOOL,
function (FieldConfig $field) {
Expand All @@ -44,20 +45,21 @@ function (FieldConfig $field) {

private function createSubnetworkRegexSetting()
{
return $this->makeSetting('subnetwork_regex', null,
return $this->makeSetting(
'subnetwork_regex',
null,
FieldConfig::TYPE_STRING,
function (FieldConfig $field) {
$field->title = Piwik::translate('RerIntranetSubnetwork_SettingTitle');
$field->description = Piwik::translate('RerIntranetSubnetwork_SettingDescription');
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
// $field->validate = function ($value, Setting $field) {
// if ( '^' !== substr($value, 0, 2) || '/' !== substr($value, -1) )
// {
// throw new \Exception(Piwik::translate('RerIntranetSubnetwork_SettingException'));
// }
// };
// $field->validate = function ($value, Setting $field) {
// if ( '^' !== substr($value, 0, 2) || '/' !== substr($value, -1) )
// {
// throw new \Exception(Piwik::translate('RerIntranetSubnetwork_SettingException'));
// }
// };
}
);
}

}
29 changes: 29 additions & 0 deletions TESTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Running tests

## PHPStan

Go to RerIntranetSubnetwork plugin dir:

```bash
composer install
```

Go to Matomo root (/var/www/html usually) run:

```bash
/var/www/html/plugins/RerIntranetSubnetwork/vendor/bin/phpstan analyze -c /var/www/html/plugins/RerIntranetSubnetwork/tests/phpstan.neon --level=1 /var/www/html/plugins/RerIntranetSubnetwork
```

## PHPCS

Go to RerIntranetSubnetwork plugin dir:

```bash
composer install
```

Run PHP Codesniffer

```bash
vendor/bin/phpcs --ignore=*/vendor/* --standard=PSR2 .
```
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"require-dev": {
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpunit/phpunit": "^10.4",
"squizlabs/php_codesniffer": "^3.7",
"pheromone/phpcs-security-audit": "^2.0",
"slevomat/coding-standard": "^8.14"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
Loading

0 comments on commit 5651276

Please sign in to comment.