diff --git a/.github/workflows/phpcs.yaml b/.github/workflows/phpcs.yaml index 7e8ba9b..881ad94 100644 --- a/.github/workflows/phpcs.yaml +++ b/.github/workflows/phpcs.yaml @@ -32,5 +32,4 @@ jobs: php-version: '8.1' tools: cs2pr - name: Run phpcs - run: ./vendor/bin/phpcs . --standard=PSR12 -q --report=checkstyle --ignore=*/vendor/*,Updates/*,tests/* | cs2pr - + run: ./vendor/bin/phpcs . --standard=tests/ruleset.xml -q --report=checkstyle --ignore=*/vendor/*,Updates/*,tests/* | cs2pr diff --git a/.gitignore b/.gitignore index b63e503..d227213 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ vendor tests/.phpunit.result.cache +docs/docs.md +composer.lock +usage diff --git a/API.php b/API.php index b5194b4..83b2b68 100644 --- a/API.php +++ b/API.php @@ -3,41 +3,174 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ namespace Piwik\Plugins\BotTracker; -use Piwik\Container\StaticContainer; -use Piwik\Db; +use Exception; use Piwik\Common; +use Piwik\Container\StaticContainer; use Piwik\DataTable; -use Piwik\Site; use Piwik\Date; -use Piwik\Piwik; +use Piwik\Db; use Piwik\Period; +use Piwik\Piwik; +use Piwik\Plugin\API as PiwikAPI; use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph\Evolution; +use Piwik\Site; /** - * @package Piwik_BotTracker + * @package Matomo_BotTracker */ -class API extends \Piwik\Plugin\API +class API extends PiwikAPI { + private static $instance = null; - private static function getDb() + public function deleteBot($botId) { - return Db::get(); + Piwik::checkUserHasSuperUserAccess(); + try { + $db = self::getDb(); + $db->query( + "DELETE FROM `" . + Common::prefixTable('bot_db') . + "` WHERE `botId` = ?", + [$botId] + ); + return true; + } catch (Exception $e) { + throw $e; + return false; + } } /** - * @return DataTable + * Get Data for the Report "Top10" + * + * @param int $idSite + * @param string $period + * @param string $date + * @param bool|string $segment + * @return \Piwik\DataTable */ - private static function getDataTable($rows) + public function getTop10($idSite, $period, $date, $segment = false) { - return DataTable::makeFromIndexedArray($rows); + Piwik::checkUserHasSomeViewAccess(); + return $this->getAllBotDataPie($idSite); + } + + /** + * Get Data for the Report "BotTracker" + * + * @param int $idSite + * @param string $period + * @param string $date + * @param bool|string $segment + * @return \Piwik\DataTable + */ + public function getBotTracker($idSite, $period, $date, $segment = false) + { + Piwik::checkUserHasSomeViewAccess(); + return $this->getAllBotData($idSite); + } + + /** + * Get Data for the Report "BotTrackerReport" + * + * @param int $idSite + * @param string $period + * @param string $date + * @param bool|string $segment + */ + public function getBotTrackerReport($idSite, $period, $date, $segment = false) + { + Piwik::checkUserHasSomeViewAccess(); + return $this->getBotTrackerReportDataTable($idSite, $period, $date, $segment = false); + } + + /** + * Get Data for the Report "BotStatsReport" + * + * @param int $idSite + * @param string $period + * @param string $date + * @param bool|string $segment + */ + public function getOtherBots($idSite, $period, $date, $segment = false) + { + Piwik::checkUserHasSomeViewAccess(); + return $this->getOtherBotsDataTable($idSite, $period, $date, $segment = false); + } + + /** + * Get Data for the Report "BotStatsReport" + * + * @param int $idSite + * @param string $period + * @param string $date + * @param bool|string $segment + */ + public function getStatsReport($idSite, $period, $date, $segment = false) + { + Piwik::checkUserHasSomeViewAccess(); + return $this->getStatsReportDataTable($idSite, $period, $date, $segment = false); } + + /** + * Get Data for the Report "BotTrackerReport" + * + * @param int $idSite + * @param string $period + * @param string $date + * @param bool|string $segment + * @return \Piwik\DataTable + */ + public function getBotTrackerTopTenReport($idSite, $period, $date, $segment = false) + { + Piwik::checkUserHasSomeViewAccess(); + return $this->getBotTrackerTopTenReportPieDataTable($idSite, $period, $date, $segment = false); + } + + /** + * Get Data for Dashboard-Widget + * + * @param int $idSite + * @param string $period + * @param string $date + * @param bool|string $segment + * @return \Piwik\DataTable + */ + public function getBotTrackerAnzeige($idSite, $period, $date, $segment = false) + { + Piwik::checkUserHasSomeViewAccess(); + return $this->getActiveBotData($idSite); + } + + public function getBotTypes() + { + Piwik::checkUserHasSomeViewAccess(); + $db = self::getDb(); + return $db->fetchAll("SELECT `name`, `id` FROM " . Common::prefixTable('bot_type') . " ORDER BY `name`"); + } + + public function addBotType($type) + { + Piwik::checkUserHasSuperUserAccess(); + try { + $db = self::getDb(); + $sql = sprintf( + 'INSERT INTO ' . Common::prefixTable('bot_type') . ' (`name`) VALUES (?)' + ); + $db->query($sql, [$type]); + } catch (Exception $e) { + throw $e; + } + } + /** * @return \Piwik\Plugins\BotTracker\API * @throws \Exception @@ -47,10 +180,10 @@ public static function getInstance() try { $instance = StaticContainer::get('BotTracker_API'); if (!($instance instanceof API)) { - throw new \Exception('BotTracker_API must inherit API'); + throw new Exception('BotTracker_API must inherit API'); } self::$instance = $instance; - } catch (\Exception $e) { + } catch (Exception $e) { self::$instance = StaticContainer::get('Piwik\Plugins\BotTracker\API'); StaticContainer::getContainer()->set('BotTracker_API', self::$instance); } @@ -72,7 +205,7 @@ public static function getAllBotData($idSite) } /** - * @return DataTable + * @return \Piwik\DataTable */ public static function getBotTrackerReportDataTable($idSite, $period, $date, $segment) { @@ -134,9 +267,9 @@ public static function getBotTrackerTopTenReportPieData($idSite, $period, $date, // For some reason, we are off by one, add an dummy bot in end. // @todo: Look into why this is needed. $dummy = [ - 'botName' => 'dummy', - 'botId' => 0, 'botCount' => 0, + 'botId' => 0, + 'botName' => 'dummy', ]; array_push($rows, $dummy); // Get totals of a bot number. @@ -162,15 +295,13 @@ public static function getBotTrackerTopTenReportPieData($idSite, $period, $date, // Remove zero results, and limit to ten. $pie = array_diff($pie, [0]); arsort($pie); - $rows = array_slice($pie, 0, 10); - - return $rows; + return array_slice($pie, 0, 10); } public static function getAllBotDataForConfig($idsite) { Piwik::checkUserHasSomeViewAccess(); - $rows = self::getDb()->fetchAll( + return self::getDb()->fetchAll( "SELECT `idsite`, `botId`, `botName`, `botActive`, `botKeyword`, `extra_stats`, @@ -179,11 +310,8 @@ public static function getAllBotDataForConfig($idsite) " WHERE `idsite` = ? ORDER BY `botId`", [$idsite] ); - - return $rows; } - public static function getActiveBotData($idSite) { Piwik::checkUserHasSomeViewAccess(); @@ -263,72 +391,71 @@ public static function insertBot($idSite, $botName, $botActive, $botKeyword, $ex $botType] ); return true; - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; return false; } } - public static function defaultBots() { $botList = []; - $botList[] = ['Amazonbot','Amazonbot']; - $botList[] = ['Qualys','Qualys']; - $botList[] = ['bingbot','bingbot']; - $botList[] = ['YandexBot','YandexBot']; - $botList[] = ['AhrefsBot','AhrefsBot']; - $botList[] = ['Ahrefs','Ahrefs']; - $botList[] = ['Scrapy','Scrapy']; - $botList[] = ['Googlebot-Image','Google-Image']; - $botList[] = ['Googlebot-News','Googlebot-News']; - $botList[] = ['Googlebot-Video','Googlebot-Video']; - $botList[] = ['Storebot-Google','Storebot-Google']; - $botList[] = ['Google-InspectionTool','Google-InspectionTool']; - $botList[] = ['Google-Extended','Google-Extended']; - $botList[] = ['GoogleOther','GoogleOther']; - $botList[] = ['APIs-Google','APIs-Google']; - $botList[] = ['AdsBot-Google-Mobile','AdsBot-Google-Mobile']; - $botList[] = ['AdsBot-Google','AdsBot-Google']; - $botList[] = ['Mediapartners-Google','Google AdSense']; - $botList[] = ['Google-Safety','Google-Safety']; - $botList[] = ['Googlebot','Googlebot']; - $botList[] = ['Google-Read-Aloud','Google-Read-Aloud']; - $botList[] = ['Google-Site-Verification','Google-Site-Verification']; - $botList[] = ['AdIdxBot','AdIdxBot']; - $botList[] = ['NewRelic','NewRelic']; - $botList[] = ['Detectify','Detectify']; - $botList[] = ['UptimeRobot','UptimeRobot']; - $botList[] = ['SendGrid','SendGrid']; - $botList[] = ['Applebot','Applebot']; - $botList[] = ['PinterestBot','PinterestBot']; - $botList[] = ['Pingdom','Pingdom']; - $botList[] = ['Barkrowler','Barkrowler']; - $botList[] = ['SEMrush','SEMrush']; - $botList[] = ['GPTBot','GPTBot']; - $botList[] = ['ChatGPT-User','ChatGPT-User']; - $botList[] = ['Bytespider','Bytespider']; - $botList[] = ['CCBot','CCBot']; - $botList[] = ['FacebookBot','FacebookBot']; - $botList[] = ['Google-Extended','Google-Extended']; - $botList[] = ['Site24x7','Site24x7']; - $botList[] = ['Stripe','Stripe']; - $botList[] = ['Slackbot','Slackbot']; - $botList[] = ['Proximic','Proximic']; - $botList[] = ['okhttp','okhttp']; - $botList[] = ['Python','Python']; - $botList[] = ['SemrushBot','SemrushBot']; - $botList[] = ['Chrome-Lighthouse','Chrome-Lighthouse']; - $botList[] = ['Axios','Axios']; - $botList[] = ['PetalBot','PetalBot']; - $botList[] = ['CriteoBot','CriteoBot']; - $botList[] = ['Baidu','Baidu']; - $botList[] = ['ContentKing','ContentKing']; - $botList[] = ['IAS crawler','IAS crawler']; - $botList[] = ['Sucuri','Sucuri']; - $botList[] = ['Seekport','Seekport']; - $botList[] = ['Sogou','Sogou']; - $botList[] = ['YahooMailProxy','YahooMailProxy']; + $botList[] = ['Amazonbot', 'Amazonbot']; + $botList[] = ['Qualys', 'Qualys']; + $botList[] = ['bingbot', 'bingbot']; + $botList[] = ['YandexBot', 'YandexBot']; + $botList[] = ['AhrefsBot', 'AhrefsBot']; + $botList[] = ['Ahrefs', 'Ahrefs']; + $botList[] = ['Scrapy', 'Scrapy']; + $botList[] = ['Googlebot-Image', 'Google-Image']; + $botList[] = ['Googlebot-News', 'Googlebot-News']; + $botList[] = ['Googlebot-Video', 'Googlebot-Video']; + $botList[] = ['Storebot-Google', 'Storebot-Google']; + $botList[] = ['Google-InspectionTool', 'Google-InspectionTool']; + $botList[] = ['Google-Extended', 'Google-Extended']; + $botList[] = ['GoogleOther', 'GoogleOther']; + $botList[] = ['APIs-Google', 'APIs-Google']; + $botList[] = ['AdsBot-Google-Mobile', 'AdsBot-Google-Mobile']; + $botList[] = ['AdsBot-Google', 'AdsBot-Google']; + $botList[] = ['Mediapartners-Google', 'Google AdSense']; + $botList[] = ['Google-Safety', 'Google-Safety']; + $botList[] = ['Googlebot', 'Googlebot']; + $botList[] = ['Google-Read-Aloud', 'Google-Read-Aloud']; + $botList[] = ['Google-Site-Verification', 'Google-Site-Verification']; + $botList[] = ['AdIdxBot', 'AdIdxBot']; + $botList[] = ['NewRelic', 'NewRelic']; + $botList[] = ['Detectify', 'Detectify']; + $botList[] = ['UptimeRobot', 'UptimeRobot']; + $botList[] = ['SendGrid', 'SendGrid']; + $botList[] = ['Applebot', 'Applebot']; + $botList[] = ['PinterestBot', 'PinterestBot']; + $botList[] = ['Pingdom', 'Pingdom']; + $botList[] = ['Barkrowler', 'Barkrowler']; + $botList[] = ['SEMrush', 'SEMrush']; + $botList[] = ['GPTBot', 'GPTBot']; + $botList[] = ['ChatGPT-User', 'ChatGPT-User']; + $botList[] = ['Bytespider', 'Bytespider']; + $botList[] = ['CCBot', 'CCBot']; + $botList[] = ['FacebookBot', 'FacebookBot']; + $botList[] = ['Google-Extended', 'Google-Extended']; + $botList[] = ['Site24x7', 'Site24x7']; + $botList[] = ['Stripe', 'Stripe']; + $botList[] = ['Slackbot', 'Slackbot']; + $botList[] = ['Proximic', 'Proximic']; + $botList[] = ['okhttp', 'okhttp']; + $botList[] = ['Python', 'Python']; + $botList[] = ['SemrushBot', 'SemrushBot']; + $botList[] = ['Chrome-Lighthouse', 'Chrome-Lighthouse']; + $botList[] = ['Axios', 'Axios']; + $botList[] = ['PetalBot', 'PetalBot']; + $botList[] = ['CriteoBot', 'CriteoBot']; + $botList[] = ['Baidu', 'Baidu']; + $botList[] = ['ContentKing', 'ContentKing']; + $botList[] = ['IAS crawler', 'IAS crawler']; + $botList[] = ['Sucuri', 'Sucuri']; + $botList[] = ['Seekport', 'Seekport']; + $botList[] = ['Sogou', 'Sogou']; + $botList[] = ['YahooMailProxy', 'YahooMailProxy']; return $botList; } @@ -351,24 +478,6 @@ public static function insertDefaultBots($idsite = 0) return $i; } - public function deleteBot($botId) - { - Piwik::checkUserHasSuperUserAccess(); - try { - $db = self::getDb(); - $query = $db->query( - "DELETE FROM `" . - Common::prefixTable('bot_db') . - "` WHERE `botId` = ?", - [$botId] - ); - return true; - } catch (\Exception $e) { - throw $e; - return false; - } - } - public static function getBotByName($idSite, $botName) { Piwik::checkUserHasSomeViewAccess(); @@ -382,92 +491,8 @@ public static function getBotByName($idSite, $botName) return $rows; } - private static function convertBotLastVisitToLocalTime($rows, $idSite) - { - // convert lastVisit to localtime - $timezone = Site::getTimezoneFor($idSite); - - try { - foreach ($rows as &$row) { - if ($row['botLastVisit'] == '0000-00-00 00:00:00') { - $row['botLastVisit'] = " - "; - } elseif ($row['botLastVisit'] == '2000-01-01 00:00:00') { - $row['botLastVisit'] = " - "; - } else { - $botLastVisit = Date::adjustForTimezone(strtotime($row['botLastVisit']), $timezone); - $row['botLastVisit'] = date('Y-m-d H:i:s', $botLastVisit); - } - } - } catch (\Exception $e) { - throw $e; - return false; - } - return $rows; - } - private static function htmlentities2utf8($string) - { - $output = preg_replace_callback("/(&#[0-9]+;)/", function ($m) { - return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); - }, $string); - return html_entity_decode($output); - } - - /** - * Get Data for the Report "Top10" - * @param int $idSite - * @param string $period - * @param string $date - * @param bool|string $segment - * @return DataTable - */ - public function getTop10($idSite, $period, $date, $segment = false) - { - Piwik::checkUserHasSomeViewAccess(); - return $this->getAllBotDataPie($idSite); - } - /** - * Get Data for the Report "BotTracker" - * @param int $idSite - * @param string $period - * @param string $date - * @param bool|string $segment - * @return DataTable - */ - public function getBotTracker($idSite, $period, $date, $segment = false) - { - Piwik::checkUserHasSomeViewAccess(); - return $this->getAllBotData($idSite); - } - - /** - * Get Data for the Report "BotTrackerReport" - * @param int $idSite - * @param string $period - * @param string $date - * @param bool|string $segment - */ - public function getBotTrackerReport($idSite, $period, $date, $segment = false) - { - Piwik::checkUserHasSomeViewAccess(); - return $this->getBotTrackerReportDataTable($idSite, $period, $date, $segment = false); - } - - /** - * Get Data for the Report "BotStatsReport" - * @param int $idSite - * @param string $period - * @param string $date - * @param bool|string $segment - */ - public function getOtherBots($idSite, $period, $date, $segment = false) - { - Piwik::checkUserHasSomeViewAccess(); - return $this->getOtherBotsDataTable($idSite, $period, $date, $segment = false); - } - - /** - * @return DataTable + * @return \Piwik\DataTable */ public static function getOtherBotsDataTable($idSite, $period, $date, $segment) { @@ -484,32 +509,16 @@ public static function getOtherBotsData($idSite, $period, $date, $segment) list($startDate, $endDate) = self::getDateRangeForPeriod($date, $period, false); $startDate = $startDate->toString(); $endDate = $endDate->toString(); - $rows = self::getDb()->fetchAll( + return self::getDb()->fetchAll( "SELECT useragent, COUNT(*) as total FROM " . Common::prefixTable('bot_device_detector_bots') . " WHERE idSite= ? AND date(date) between ? AND ? GROUP BY `useragent` ORDER BY `useragent`", - [$idSite, $startDate, $endDate ] + [$idSite, $startDate, $endDate] ); - // // @todo: convert visit_timestamp to site - return $rows; } - /** - * Get Data for the Report "BotStatsReport" - * @param int $idSite - * @param string $period - * @param string $date - * @param bool|string $segment - */ - public function getStatsReport($idSite, $period, $date, $segment = false) - { - Piwik::checkUserHasSomeViewAccess(); - return $this->getStatsReportDataTable($idSite, $period, $date, $segment = false); - } - - /** - * @return DataTable + * @return \Piwik\DataTable */ public static function getStatsReportDataTable($idSite, $period, $date, $segment) { @@ -530,7 +539,7 @@ public static function getStatsReportData($idSite, $period, $date, $segment) "SELECT * FROM " . Common::prefixTable('bot_db_stat') . " WHERE idSite= ? AND date(visit_timestamp) between ? AND ? ORDER BY `botId`", - [$idSite, $startDate, $endDate ] + [$idSite, $startDate, $endDate] ); foreach ($rows as &$row) { $name = self::getDb()->fetchRow( @@ -545,55 +554,6 @@ public static function getStatsReportData($idSite, $period, $date, $segment) return $rows; } - /** - * Get Data for the Report "BotTrackerReport" - * @param int $idSite - * @param string $period - * @param string $date - * @param bool|string $segment - * @return DataTable - */ - public function getBotTrackerTopTenReport($idSite, $period, $date, $segment = false) - { - Piwik::checkUserHasSomeViewAccess(); - return $this->getBotTrackerTopTenReportPieDataTable($idSite, $period, $date, $segment = false); - } - - /** - * Get Data for Dashboard-Widget - * @param int $idSite - * @param string $period - * @param string $date - * @param bool|string $segment - * @return DataTable - */ - public function getBotTrackerAnzeige($idSite, $period, $date, $segment = false) - { - Piwik::checkUserHasSomeViewAccess(); - return $this->getActiveBotData($idSite); - } - - public function getBotTypes() - { - Piwik::checkUserHasSomeViewAccess(); - $db = self::getDb(); - $rows = $db->fetchAll("SELECT `name`, `id` FROM " . Common::prefixTable('bot_type') . " ORDER BY `name`"); - return $rows; - } - public function addBotType($type) - { - Piwik::checkUserHasSuperUserAccess(); - try { - $db = self::getDb(); - $sql = sprintf( - 'INSERT INTO ' . Common::prefixTable('bot_type') . ' (`name`) VALUES (?)' - ); - $db->query($sql, [$type]); - } catch (\Exception $e) { - throw $e; - } - } - public static function getDateRangeForPeriod($date, $period, $lastN = false) { $lastN = false; @@ -612,7 +572,8 @@ public static function getDateRangeForPeriod($date, $period, $lastN = false) $oPeriod = Period\Factory::build($period, Date::factory($date)); $startDate = $oPeriod->getDateStart(); $endDate = $oPeriod->getDateEnd(); - } else { // if the range includes the last N periods or is a multiple period + } else { + // if the range includes the last N periods or is a multiple period if (!$isMultiplePeriod) { list($date, $lastN) = Evolution::getDateRangeAndLastN($period, $date, $lastN); } @@ -623,4 +584,49 @@ public static function getDateRangeForPeriod($date, $period, $lastN = false) } return [$startDate, $endDate]; } + + private static function getDb() + { + return Db::get(); + } + + /** + * @return \Piwik\DataTable + */ + private static function getDataTable($rows) + { + return DataTable::makeFromIndexedArray($rows); + } + + private static function convertBotLastVisitToLocalTime($rows, $idSite) + { + // convert lastVisit to localtime + $timezone = Site::getTimezoneFor($idSite); + + try { + foreach ($rows as &$row) { + if ($row['botLastVisit'] == '0000-00-00 00:00:00') { + $row['botLastVisit'] = " - "; + } elseif ($row['botLastVisit'] == '2000-01-01 00:00:00') { + $row['botLastVisit'] = " - "; + } else { + $botLastVisit = Date::adjustForTimezone(strtotime($row['botLastVisit']), $timezone); + $row['botLastVisit'] = date('Y-m-d H:i:s', $botLastVisit); + } + } + } catch (Exception $e) { + throw $e; + return false; + } + return $rows; + } + + private static function htmlentities2utf8($string) + { + $output = preg_replace_callback("/(&#[0-9]+;)/", function ($m) { + return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); + }, $string); + return html_entity_decode($output); + } + } diff --git a/BotTracker.php b/BotTracker.php index 7df9822..36160c6 100644 --- a/BotTracker.php +++ b/BotTracker.php @@ -3,34 +3,35 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ namespace Piwik\Plugins\BotTracker; +use Exception; use Piwik\Common; use Piwik\Config; use Piwik\Container\StaticContainer; use Piwik\Db; +use Piwik\DeviceDetector\DeviceDetectorFactory; +use Piwik\Plugin; +use Piwik\Plugins\BotTracker\API as BotTrackerAPI; use Piwik\Plugins\SitesManager\API as APISitesManager; use Piwik\Tracker; -use Piwik\Plugins\BotTracker\API as BotTrackerAPI; -use Piwik\DeviceDetector\DeviceDetectorFactory; -class BotTracker extends \Piwik\Plugin +/** + * @package Matomo_BotTracker + */ +class BotTracker extends Plugin { - private function getDb() - { - return Db::get(); - } public function install() { $tableExists = false; $db = $this->getDb(); - // create new table "bot_db" $query = "CREATE TABLE `" . Common::prefixTable('bot_db') . "` (`botId` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, `idsite` INTEGER(10) UNSIGNED NOT NULL, @@ -47,7 +48,7 @@ public function install() // if the table already exist do not throw error. Could be installed twice... try { $db->exec($query); - } catch (\Exception $e) { + } catch (Exception $e) { $tableExists = true; } @@ -57,8 +58,7 @@ public function install() BotTrackerAPI::insertDefaultBots($site['idsite']); } } - // Create bot_db_stat table. - $query2 = 'CREATE TABLE IF NOT EXISTS `' . Common::prefixTable('bot_db_stat') . '` + $query2 = 'CREATE TABLE IF NOT EXISTS `' . Common::prefixTable('bot_db_stat') . '` ( `visitId` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `botId` INTEGER(10) UNSIGNED NOT NULL, @@ -71,12 +71,11 @@ public function install() ) DEFAULT CHARSET=utf8'; try { $db->exec($query2); - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } - // Create bot_type table - $query3 = 'CREATE TABLE IF NOT EXISTS `' . Common::prefixTable('bot_type') . '` + $query3 = 'CREATE TABLE IF NOT EXISTS `' . Common::prefixTable('bot_type') . '` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(256) NOT NULL, @@ -85,7 +84,7 @@ public function install() ) DEFAULT CHARSET=utf8'; try { $db->exec($query3); - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } @@ -111,11 +110,11 @@ public function install() ); $db->query($sql, [$type]); } - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } - $query4 = 'CREATE TABLE IF NOT EXISTS `' . Common::prefixTable('bot_visits') . '` + $query4 = 'CREATE TABLE IF NOT EXISTS `' . Common::prefixTable('bot_visits') . '` ( `id` BIGINT UNSIGNED AUTO_INCREMENT, `botId` INT UNSIGNED, @@ -125,11 +124,11 @@ public function install() ) DEFAULT CHARSET=utf8'; try { $db->exec($query4); - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } - $query5 = 'CREATE TABLE IF NOT EXISTS `' . Common::prefixTable('bot_device_detector_bots') . '` + $query5 = 'CREATE TABLE IF NOT EXISTS `' . Common::prefixTable('bot_device_detector_bots') . '` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `idsite` INT UNSIGNED, @@ -139,7 +138,7 @@ public function install() ) DEFAULT CHARSET=utf8'; try { $db->exec($query5); - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -162,7 +161,7 @@ public function uninstall() public function registerEvents() { return [ - 'Tracker.isExcludedVisit' => 'checkBot', + 'Tracker.isExcludedVisit' => 'checkBot', 'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys', ]; } @@ -260,7 +259,7 @@ public function checkBot(&$exclude, $request) $query = "INSERT INTO `" . Common::prefixTable('bot_db_stat') . "` (idsite, botid, page, visit_timestamp, useragent) VALUES (?,?,?,?,?)"; // max length of useragent can be 256 Bytes - $params = [$idSite,$botId,$currentUrl,$currentTimestamp,substr($userAgent, 0, 256)]; + $params = [$idSite, $botId, $currentUrl, $currentTimestamp, substr($userAgent, 0, 256)]; $db->query($query, $params); } } else { @@ -288,4 +287,10 @@ public function checkBot(&$exclude, $request) } } } + + private function getDb() + { + return Db::get(); + } + } diff --git a/CHANGELOG.md b/CHANGELOG.md index 0303039..18f0557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -114,7 +114,7 @@ Matomo 5 compatibility fixes. * change order of columns in the BotTracker report (issue #68) -## [2.00] - 2020-12-05 +## [2.00] - unknown2-05 * upgrade to Matomo 4 (issue #66) diff --git a/Commands/AddBot.php b/Commands/AddBot.php index 1479822..0dca19f 100644 --- a/Commands/AddBot.php +++ b/Commands/AddBot.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -10,14 +11,15 @@ namespace Piwik\Plugins\BotTracker\Commands; use Piwik\Plugin\ConsoleCommand; -use Symfony\Component\Console\Input\InputOption; use Piwik\Plugins\BotTracker\API; +use Symfony\Component\Console\Input\InputOption; /** * Add new bot. */ class AddBot extends ConsoleCommand { + protected function configure() { $HelpText = 'The %command.name% command will add a new bot. @@ -83,4 +85,5 @@ protected function doExecute(): int $output->write("New bot added, $name\n"); return self::SUCCESS; } + } diff --git a/Commands/AddBotType.php b/Commands/AddBotType.php index 1251337..855cffc 100644 --- a/Commands/AddBotType.php +++ b/Commands/AddBotType.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -10,14 +11,15 @@ namespace Piwik\Plugins\BotTracker\Commands; use Piwik\Plugin\ConsoleCommand; -use Symfony\Component\Console\Input\InputOption; use Piwik\Plugins\BotTracker\API; +use Symfony\Component\Console\Input\InputOption; /** * Add new bot type. */ class AddBotType extends ConsoleCommand { + protected function configure() { $HelpText = 'The %command.name% command will add a new bot type. @@ -60,4 +62,5 @@ protected function doExecute(): int return self::FAILURE; } } + } diff --git a/Commands/AddDefaultBots.php b/Commands/AddDefaultBots.php index 50111ee..fdbc171 100644 --- a/Commands/AddDefaultBots.php +++ b/Commands/AddDefaultBots.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -10,14 +11,15 @@ namespace Piwik\Plugins\BotTracker\Commands; use Piwik\Plugin\ConsoleCommand; -use Symfony\Component\Console\Input\InputOption; use Piwik\Plugins\BotTracker\API; +use Symfony\Component\Console\Input\InputOption; /** * Add default bots. */ class AddDefaultBots extends ConsoleCommand { + protected function configure() { $HelpText = 'The %command.name% will add the default bots included. @@ -35,7 +37,7 @@ protected function configure() InputOption::VALUE_OPTIONAL, 'SiteId', null - ) + ), ] ); } @@ -60,4 +62,5 @@ protected function doExecute(): int return self::FAILURE; } } + } diff --git a/Commands/DeleteBot.php b/Commands/DeleteBot.php index 1eee9bc..a94246b 100644 --- a/Commands/DeleteBot.php +++ b/Commands/DeleteBot.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -10,14 +11,15 @@ namespace Piwik\Plugins\BotTracker\Commands; use Piwik\Plugin\ConsoleCommand; -use Symfony\Component\Console\Input\InputOption; use Piwik\Plugins\BotTracker\API; +use Symfony\Component\Console\Input\InputOption; /** * Add new bot. */ class DeleteBot extends ConsoleCommand { + protected function configure() { $HelpText = 'The %command.name% command will add a new bot. @@ -35,7 +37,7 @@ protected function configure() InputOption::VALUE_REQUIRED, 'Id of bot', null - ) + ), ] ); } @@ -59,4 +61,5 @@ protected function doExecute(): int $output->write("Bot with id $botId deleted.\n"); return self::SUCCESS; } + } diff --git a/Commands/ListBotTypes.php b/Commands/ListBotTypes.php index 0fc72e4..6b009de 100644 --- a/Commands/ListBotTypes.php +++ b/Commands/ListBotTypes.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -17,6 +18,7 @@ */ class ListBotTypes extends ConsoleCommand { + protected function configure() { $HelpText = 'The %command.name% will add the default bots included. @@ -50,4 +52,5 @@ protected function doExecute(): int } return self::SUCCESS; } + } diff --git a/Commands/ListBots.php b/Commands/ListBots.php index 68a1c21..311e32b 100644 --- a/Commands/ListBots.php +++ b/Commands/ListBots.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -10,14 +11,15 @@ namespace Piwik\Plugins\BotTracker\Commands; use Piwik\Plugin\ConsoleCommand; -use Symfony\Component\Console\Input\InputOption; use Piwik\Plugins\BotTracker\API; +use Symfony\Component\Console\Input\InputOption; /** * Add new bot. */ class ListBots extends ConsoleCommand { + protected function configure() { $HelpText = 'The %command.name% command will list bots for site id. @@ -35,7 +37,7 @@ protected function configure() InputOption::VALUE_REQUIRED, 'SiteId', null - ) + ), ] ); } @@ -69,4 +71,5 @@ protected function doExecute(): int } return self::SUCCESS; } + } diff --git a/Controller.php b/Controller.php index 2f041cc..37e8ca3 100644 --- a/Controller.php +++ b/Controller.php @@ -3,23 +3,29 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ namespace Piwik\Plugins\BotTracker; +use Exception; use Piwik\Nonce; use Piwik\Notification\Manager as NotificationManager; use Piwik\Piwik; -use Piwik\View; -use Piwik\Plugins\SitesManager\API as APISitesManager; +use Piwik\Plugin\ControllerAdmin; use Piwik\Plugins\BotTracker\API as APIBotTracker; +use Piwik\Plugins\SitesManager\API as APISitesManager; use Piwik\Request; -use Piwik\Plugin\ControllerAdmin; +use Piwik\View; +/** + * @package Matomo_BotTracker + */ class Controller extends ControllerAdmin { + public string $nonce; /** * @property string $nonce @@ -55,7 +61,7 @@ public function docs() Piwik::checkUserHasSomeViewAccess(); $info = "Bot Tracker Docs"; return $this->renderTemplate('docs', array( - 'info' => $info + 'info' => $info, )); } @@ -142,12 +148,11 @@ public function saveConfig() } } $this->index($siteID, $errorList); - } catch (\Exception $e) { + } catch (Exception $e) { echo $e; } } - public function addNew() { try { @@ -165,7 +170,7 @@ public function addNew() $botActive = trim(Request::fromRequest()->getStringParameter('new_botActive', '0')); $extraStats = trim(Request::fromRequest()->getStringParameter('new_extraStats', '0')); if ( - $botName != '' || + $botName != '' || $botKeyword != '' ) { if (empty($botName)) { @@ -181,7 +186,7 @@ public function addNew() $errorList[] = 'Bot ' . $botName . ' ' . Piwik::translate('BotTracker_Added'); $this->index($siteID, $errorList); - } catch (\Exception $e) { + } catch (Exception $e) { echo $e; } } @@ -201,7 +206,7 @@ public function deleteBotEntry() $errorList[] = 'Bot ' . $botId . ' ' . Piwik::translate('BotTracker_Message_deleted'); $this->index($siteID, $errorList); - } catch (\Exception $e) { + } catch (Exception $e) { echo $e; } } @@ -218,8 +223,9 @@ public function configInsertDb() $errorList[] = $i . " " . Piwik::translate('BotTracker_Message_bot_inserted'); $this->index($siteID, $errorList); - } catch (\Exception $e) { + } catch (Exception $e) { echo $e; } } + } diff --git a/Menu.php b/Menu.php index 3a327c0..addcea3 100644 --- a/Menu.php +++ b/Menu.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -11,14 +12,14 @@ use Piwik\Menu\MenuAdmin; use Piwik\Piwik; +use Piwik\Plugin\Menu as MatomoMenu; /** - * This class allows you to add, remove or rename menu items. - * To configure a menu (such as Admin Menu, Reporting Menu, User Menu...) simply call the corresponding methods as - * described in the API-Reference http://developer.piwik.org/api-reference/Piwik/Menu/MenuAbstract + * @package Matomo_BotTracker */ -class Menu extends \Piwik\Plugin\Menu +class Menu extends MatomoMenu { + public function configureAdminMenu(MenuAdmin $menu) { if (Piwik::isUserHasSomeAdminAccess()) { @@ -33,4 +34,5 @@ public function configureAdminMenu(MenuAdmin $menu) ); } } + } diff --git a/Reports/Base.php b/Reports/Base.php index a845231..a36b33b 100644 --- a/Reports/Base.php +++ b/Reports/Base.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -11,10 +12,15 @@ use Piwik\Plugin\Report; +/** + * @package Matomo_BotTracker + */ abstract class Base extends Report { + protected function init() { $this->categoryId = 'General_Visitors'; } + } diff --git a/Reports/GetBotTracker.php b/Reports/GetBotTracker.php index 22063ad..2fbb3e7 100644 --- a/Reports/GetBotTracker.php +++ b/Reports/GetBotTracker.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later * @deprecated since release 5.2.0 @@ -12,28 +13,21 @@ use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; -use Piwik\Widget\WidgetsList; use Piwik\Report\ReportWidgetFactory; +use Piwik\Widget\WidgetsList; /** * Defines the GetBotTracker report. * * See {@link https://developer.matomo.org/api-reference/Piwik/Plugin/Report} for more information. + * * @deprecated since v5.2.0, will be removed in v5.3.0 + * @package Matomo_BotTracker */ class GetBotTracker extends Base { - protected function init() - { - parent::init(); - - $this->name = Piwik::translate('BotTracker_Bot_Tracker_Total_Over_Time_Deprecated_Report'); - $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); - $this->order = 99; - } /** - * @param ViewDataTable $view */ public function configureView(ViewDataTable $view) { @@ -42,13 +36,13 @@ public function configureView(ViewDataTable $view) $view->config->translations['botKeyword'] = Piwik::translate('BotTracker_BotKeyword'); $view->config->translations['botCount'] = Piwik::translate('BotTracker_BotCount'); $view->config->translations['botLastVisit'] = Piwik::translate('BotTracker_BotLastVisit'); - $view->config->columns_to_display = ['botName','botCount','botLastVisit']; + $view->config->columns_to_display = ['botName', 'botCount', 'botLastVisit']; $view->config->show_search = false; $view->config->show_footer_icons = false; $view->config->show_exclude_low_population = false; $view->config->show_table_all_columns = false; $view->config->show_insights = false; - $view->config->show_related_reports = false; + $view->config->show_related_reports = false; $view->config->show_pivot_by_subtable = false; $view->config->show_table_performance = false; $view->config->show_all_views_icons = false; @@ -75,4 +69,14 @@ public function configureWidgets(WidgetsList $widgetsList, ReportWidgetFactory $ $factory->createWidget()->setIsNotWidgetizable() ); } + + protected function init() + { + parent::init(); + + $this->name = Piwik::translate('BotTracker_Bot_Tracker_Total_Over_Time_Deprecated_Report'); + $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); + $this->order = 99; + } + } diff --git a/Reports/GetBotTrackerAnzeige.php b/Reports/GetBotTrackerAnzeige.php index b159064..19c7480 100644 --- a/Reports/GetBotTrackerAnzeige.php +++ b/Reports/GetBotTrackerAnzeige.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later * @deprecated since release 5.2.0 @@ -12,28 +13,21 @@ use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; -use Piwik\Widget\WidgetsList; use Piwik\Report\ReportWidgetFactory; +use Piwik\Widget\WidgetsList; /** * Defines the GetBotTrackerAnzeige report. * * See {@link https://developer.matomo.org/api-reference/Piwik/Plugin/Report} for more information. + * * @deprecated since v5.2.0, will be removed in v5.3.0 + * @package Matomo_BotTracker */ class GetBotTrackerAnzeige extends Base { - protected function init() - { - parent::init(); - - $this->name = Piwik::translate('BotTracker_DisplayWidget_Deprecated_Report'); - $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); - $this->order = 99; - } /** - * @param ViewDataTable $view */ public function configureView(ViewDataTable $view) { @@ -46,12 +40,12 @@ public function configureView(ViewDataTable $view) $view->config->show_exclude_low_population = false; $view->config->show_table_all_columns = false; $view->config->show_insights = false; - $view->config->show_related_reports = false; + $view->config->show_related_reports = false; $view->config->show_pivot_by_subtable = false; $view->config->show_table_performance = false; $view->config->show_all_views_icons = false; $view->config->show_export = false; - $view->config->columns_to_display = ["botName","botCount","botLastVisit"]; + $view->config->columns_to_display = ["botName", "botCount", "botLastVisit"]; $view->requestConfig->filter_sort_column = 'botCount'; $view->requestConfig->filter_sort_order = 'desc'; $view->requestConfig->filter_limit = 10; @@ -69,4 +63,14 @@ public function configureWidgets(WidgetsList $widgetsList, ReportWidgetFactory $ { $widgetsList->addWidgetConfig($factory->createWidget()); } + + protected function init() + { + parent::init(); + + $this->name = Piwik::translate('BotTracker_DisplayWidget_Deprecated_Report'); + $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); + $this->order = 99; + } + } diff --git a/Reports/GetBotTrackerReport.php b/Reports/GetBotTrackerReport.php index 606c9f3..1a01742 100644 --- a/Reports/GetBotTrackerReport.php +++ b/Reports/GetBotTrackerReport.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -11,40 +12,32 @@ use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; -use Piwik\Widget\WidgetsList; use Piwik\Report\ReportWidgetFactory; +use Piwik\Widget\WidgetsList; /** * Defines the GetBotTrackerReport report. * See {@link https://developer.matomo.org/api-reference/Piwik/Plugin/Report} for more information. + * + * @package Matomo_BotTracker */ class GetBotTrackerReport extends Base { - protected function init() - { - parent::init(); - - $this->name = Piwik::translate('BotTracker_Bot_Tracker_Report'); - $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); - $this->documentation = Piwik::translate('BotTracker_ReportDocumentation'); - $this->order = 98; - } /** - * @param ViewDataTable $view */ public function configureView(ViewDataTable $view) { $view->config->translations['botId'] = Piwik::translate('BotTracker_BotId'); $view->config->translations['botName'] = Piwik::translate('BotTracker_BotName'); $view->config->translations['total'] = Piwik::translate('BotTracker_BotCount'); - $view->config->columns_to_display = ['botName','total']; + $view->config->columns_to_display = ['botName', 'total']; $view->config->show_search = false; $view->config->show_footer_icons = false; $view->config->show_exclude_low_population = false; $view->config->show_table_all_columns = false; $view->config->show_insights = false; - $view->config->show_related_reports = false; + $view->config->show_related_reports = false; $view->config->show_pivot_by_subtable = false; $view->config->show_table_performance = false; $view->config->show_all_views_icons = false; @@ -68,4 +61,15 @@ public function configureWidgets(WidgetsList $widgetsList, ReportWidgetFactory $ $config->setOrder(1); $widgetsList->addWidgetConfig($config); } + + protected function init() + { + parent::init(); + + $this->name = Piwik::translate('BotTracker_Bot_Tracker_Report'); + $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); + $this->documentation = Piwik::translate('BotTracker_ReportDocumentation'); + $this->order = 98; + } + } diff --git a/Reports/GetBotTrackerTopTenReport.php b/Reports/GetBotTrackerTopTenReport.php index b74bc47..1eebf09 100644 --- a/Reports/GetBotTrackerTopTenReport.php +++ b/Reports/GetBotTrackerTopTenReport.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -11,31 +12,21 @@ use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; -use Piwik\Widget\WidgetsList; use Piwik\Report\ReportWidgetFactory; +use Piwik\Widget\WidgetsList; /** * Defines the class GetBotTrackerTopTenReport report. * See {@link https://developer.matomo.org/api-reference/Piwik/Plugin/Report} for more information. + * + * @package Matomo_BotTracker */ class GetBotTrackerTopTenReport extends Base { - protected function init() - { - parent::init(); - - $this->name = Piwik::translate('BotTracker_Top_10_Bots'); - $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); - $this->documentation = Piwik::translate('BotTracker_TopTenDocumentation'); - // This defines in which order your report appears in the mobile app, in the menu and in the list of widgets - $this->order = 98; - } /** * Here you can configure how your report should be displayed. For instance whether your report supports a search * etc. You can also change the default request config. For instance change how many rows are displayed by default. - * - * @param ViewDataTable $view */ public function configureView(ViewDataTable $view) { @@ -43,7 +34,7 @@ public function configureView(ViewDataTable $view) $view->config->show_footer_icons = true; $view->config->show_insights = false; $view->config->selectable_columns = ["value"]; - $view->config->show_related_reports = false; + $view->config->show_related_reports = false; $view->config->show_table_all_columns = false; } @@ -58,11 +49,11 @@ public function getRelatedReports() return []; } - public function getDefaultTypeViewDataTable() { return 'graphPie'; } + public function alwaysUseDefaultViewDataTable() { return true; @@ -74,4 +65,16 @@ public function configureWidgets(WidgetsList $widgetsList, ReportWidgetFactory $ $config->setOrder(2); $widgetsList->addWidgetConfig($config); } + + protected function init() + { + parent::init(); + + $this->name = Piwik::translate('BotTracker_Top_10_Bots'); + $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); + $this->documentation = Piwik::translate('BotTracker_TopTenDocumentation'); + // This defines in which order your report appears in the mobile app, in the menu and in the list of widgets + $this->order = 98; + } + } diff --git a/Reports/GetOtherBots.php b/Reports/GetOtherBots.php index 2c2c5b8..bb71b47 100644 --- a/Reports/GetOtherBots.php +++ b/Reports/GetOtherBots.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -11,27 +12,19 @@ use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; -use Piwik\Widget\WidgetsList; use Piwik\Report\ReportWidgetFactory; +use Piwik\Widget\WidgetsList; /** * Defines the GetBotStatsReport report. * See {@link https://developer.matomo.org/api-reference/Piwik/Plugin/Report} for more information. + * + * @package Matomo_BotTracker */ class GetOtherBots extends Base { - protected function init() - { - parent::init(); - - $this->name = Piwik::translate('BotTracker_Bot_Tracker_OtherBots'); - $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); - $this->documentation = Piwik::translate('BotTracker_Bot_Tracker_OtherBotsDocumentation'); - $this->order = 98; - } /** - * @param ViewDataTable $view */ public function configureView(ViewDataTable $view) { @@ -67,4 +60,15 @@ public function isEnabled() { return true; } + + protected function init() + { + parent::init(); + + $this->name = Piwik::translate('BotTracker_Bot_Tracker_OtherBots'); + $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); + $this->documentation = Piwik::translate('BotTracker_Bot_Tracker_OtherBotsDocumentation'); + $this->order = 98; + } + } diff --git a/Reports/GetStatsReport.php b/Reports/GetStatsReport.php index bb161bc..d9c6afb 100644 --- a/Reports/GetStatsReport.php +++ b/Reports/GetStatsReport.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -11,27 +12,19 @@ use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; -use Piwik\Widget\WidgetsList; use Piwik\Report\ReportWidgetFactory; +use Piwik\Widget\WidgetsList; /** * Defines the GetBotStatsReport report. * See {@link https://developer.matomo.org/api-reference/Piwik/Plugin/Report} for more information. + * + * @package Matomo_BotTracker */ class GetStatsReport extends Base { - protected function init() - { - parent::init(); - - $this->name = Piwik::translate('BotTracker_Bot_Tracker_Report_Stats'); - $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); - $this->documentation = Piwik::translate('BotTracker_ReportDocumentation'); - $this->order = 98; - } /** - * @param ViewDataTable $view */ public function configureView(ViewDataTable $view) { @@ -69,4 +62,15 @@ public function isEnabled() // @todo: Add check if there is any data in db_stats table, do not show if there is none. return true; } + + protected function init() + { + parent::init(); + + $this->name = Piwik::translate('BotTracker_Bot_Tracker_Report_Stats'); + $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); + $this->documentation = Piwik::translate('BotTracker_ReportDocumentation'); + $this->order = 98; + } + } diff --git a/Reports/GetTop10.php b/Reports/GetTop10.php index dcaa817..e9fad53 100644 --- a/Reports/GetTop10.php +++ b/Reports/GetTop10.php @@ -3,6 +3,7 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later * @deprecated since v5.2.0, will be removed in v5.3.0 @@ -17,21 +18,13 @@ * Defines the GetTop10 report. * * See {@link https://developer.matomo.org/api-reference/Piwik/Plugin/Report} for more information. + * + * @package Matomo_BotTracker */ class GetTop10 extends Base { - protected function init() - { - parent::init(); - - $this->name = Piwik::translate('BotTracker_Top_10_Bots_Deprecated_Report'); - $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); - // This defines in which order your report appears in the mobile app, in the menu and in the list of widgets - $this->order = 99; - } /** - * @param ViewDataTable $view */ public function configureView(ViewDataTable $view) { @@ -39,7 +32,7 @@ public function configureView(ViewDataTable $view) $view->config->show_footer_icons = true; $view->config->show_insights = false; $view->config->selectable_columns = ["value"]; - $view->config->show_related_reports = false; + $view->config->show_related_reports = false; $view->config->show_table_all_columns = false; } @@ -55,8 +48,20 @@ public function getDefaultTypeViewDataTable() { return 'graphPie'; } + public function alwaysUseDefaultViewDataTable() { return true; } + + protected function init() + { + parent::init(); + + $this->name = Piwik::translate('BotTracker_Top_10_Bots_Deprecated_Report'); + $this->subcategoryId = Piwik::translate('BotTracker_BotTracker'); + // This defines in which order your report appears in the mobile app, in the menu and in the list of widgets + $this->order = 99; + } + } diff --git a/SystemSettings.php b/SystemSettings.php index 5c802e7..8641d9e 100644 --- a/SystemSettings.php +++ b/SystemSettings.php @@ -3,31 +3,32 @@ /** * BotTracker, a Matomo plugin by Digitalist Open Tech * Based on the work of Thomas--F (https://github.com/Thomas--F) + * * @link https://github.com/digitalist-se/BotTracker * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ namespace Piwik\Plugins\BotTracker; -use Piwik\Settings\Setting; use Piwik\Settings\FieldConfig; +use Piwik\Settings\Plugin\SystemSettings as MatomoSystemSettings; +use Piwik\Settings\Setting; /** - * Defines Settings for BotTracker. + * @package Matomo_BotTracker */ -class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings +class SystemSettings extends MatomoSystemSettings { - /** @var Setting */ - public $trackDeviceDetectorBots; + + public Setting $trackDeviceDetectorBots; protected function init() { $this->trackDeviceDetectorBots = $this->trackDeviceDetectorBots(); } - /** - * @return Setting + * @return \Piwik\Settings\Setting */ private function trackDeviceDetectorBots() { @@ -43,4 +44,5 @@ function (FieldConfig $field) { } ); } + } diff --git a/TESTS.md b/TESTS.md index 0498243..d66859a 100644 --- a/TESTS.md +++ b/TESTS.md @@ -45,7 +45,7 @@ Set up test environment: ```bash ./console development:enable -./console config:set --section=tests --key=http_host --value=web +./console config:set --section=tests --key=http_host --value=matomo.loc ./console config:set --section=tests --key=request_uri --value=/ ./console config:set --section=database_tests --key=host --value=db ./console config:set --section=database_tests --key=username --value=root diff --git a/Widgets/GetDigiInfo.php b/Widgets/GetDigiInfo.php index f06cd2e..6dda120 100644 --- a/Widgets/GetDigiInfo.php +++ b/Widgets/GetDigiInfo.php @@ -5,28 +5,12 @@ use Piwik\Piwik; use Piwik\Widget\Widget; use Piwik\Widget\WidgetConfig; -use Piwik\Translation\Translator; +/** + * @package Matomo_BotTracker + */ class GetDigiInfo extends Widget { - /** - * @var Translator - */ - private $translator; - - public function __construct(Translator $translator) - { - $this->translator = $translator; - } - - public static function configure(WidgetConfig $config) - { - $config->setCategoryId('General_Visitors'); - $config->setSubcategoryId(Piwik::translate('BotTracker_BotTracker')); - $config->setIsWide(); - $config->setOrder(0); - $config->setIsNotWidgetizable(); - } public function render() { @@ -37,7 +21,7 @@ public function render()
- +

Bot Tracker

@@ -48,4 +32,14 @@ public function render()
'; } + + public static function configure(WidgetConfig $config) + { + $config->setCategoryId('General_Visitors'); + $config->setSubcategoryId(Piwik::translate('BotTracker_BotTracker')); + $config->setIsWide(); + $config->setOrder(0); + $config->setIsNotWidgetizable(); + } + } diff --git a/composer.json b/composer.json index 92f4bcc..bd0ce2b 100644 --- a/composer.json +++ b/composer.json @@ -20,10 +20,11 @@ "php-webdriver/webdriver": "^1.15", "squizlabs/php_codesniffer": "^3.7", "phpstan/phpstan": "^1.10.62", - "slevomat/coding-standard": "^8.8", + "slevomat/coding-standard": "^8.15", "spaze/phpstan-disallowed-calls": "^2.12", "psr/container": "1.1.2", - "phpstan/phpstan-deprecation-rules": "^1.1" + "phpstan/phpstan-deprecation-rules": "^1.1", + "sirbrillig/phpcs-import-detection": "^1.3" }, "config": { "allow-plugins": { diff --git a/tests/phpunit.xml b/tests/phpunit.xml index d92adfe..374b7fe 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -4,6 +4,7 @@ ../Commands/ ../Reports/ + ../Widgets/ diff --git a/tests/ruleset.xml b/tests/ruleset.xml new file mode 100644 index 0000000..da03310 --- /dev/null +++ b/tests/ruleset.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file