From a5ffa070e22e5ab8f3bb3c8b29d81dc3bb3a6e2b Mon Sep 17 00:00:00 2001 From: CosmicAlpha Date: Sat, 4 Feb 2023 14:37:17 -0700 Subject: [PATCH] maintenance: add requireExtension() and use $this->getConfig() (#95) Also cleanup CleanupMatomos.php, and ModifyMatomo.php. For ModifyMatomo, it basically rewrites it, and use MatomoAnalytics directly instead of going through MatomoAnalyticsHooks. --- CHANGELOG.md | 6 ++++++ extension.json | 2 +- maintenance/addMissingMatomos.php | 19 +++++-------------- maintenance/cleanupMatomos.php | 25 +++++++++---------------- maintenance/modifyMatomo.php | 15 ++++++++------- 5 files changed, 29 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02479c2..b05ea4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ ## ChangeLog for MatomoAnalytics +### 1.1.2 (04-02-2023) +* Add a CleanupMatomos maintenance script +* Add requireExtension() to all maintenance scripts +* Cleanup the ModifyMatomo maintenance script, and use MatomoAnalytics + directly rather than going through MatomoAnalyticsHooks + ### 1.1.1 (29-01-2023) * Don't override $text within MatomoAnalyticsHooks::matomoScript diff --git a/extension.json b/extension.json index eaa92cd..c1844ed 100644 --- a/extension.json +++ b/extension.json @@ -6,7 +6,7 @@ "Universal Omega" ], "url": "https://github.com/miraheze/MatomoAnalytics", - "version": "1.1.1", + "version": "1.1.2", "descriptionmsg": "matomoanalytics-desc", "license-name": "GPL-3.0-or-later", "type": "specialpage", diff --git a/maintenance/addMissingMatomos.php b/maintenance/addMissingMatomos.php index 41e8df9..421976f 100644 --- a/maintenance/addMissingMatomos.php +++ b/maintenance/addMissingMatomos.php @@ -4,23 +4,21 @@ if ( $IP === false ) { $IP = __DIR__ . '/../../..'; } -require_once "$IP/maintenance/Maintenance.php"; -use MediaWiki\MediaWikiServices; +require_once "$IP/maintenance/Maintenance.php"; class AddMissingMatomos extends Maintenance { public function __construct() { parent::__construct(); $this->addDescription( 'Add missing matomo ids.' ); + + $this->requireExtension( 'CreateWiki' ); + $this->requireExtension( 'MatomoAnalytics' ); } public function execute() { - $config = MediaWikiServices::getInstance() - ->getConfigFactory() - ->makeConfig( 'matomoanalytics' ); - - $dbw = $this->getDB( DB_PRIMARY, [], $config->get( 'CreateWikiDatabase' ) ); + $dbw = $this->getDB( DB_PRIMARY, [], $this->getConfig()->get( 'CreateWikiDatabase' ) ); $res = $dbw->select( 'cw_wikis', @@ -36,10 +34,6 @@ public function execute() { foreach ( $res as $row ) { $DBname = $row->wiki_dbname; - if ( $DBname === 'default' ) { - continue; - } - $id = $dbw->selectField( 'matomo', 'matomo_id', @@ -49,10 +43,7 @@ public function execute() { if ( !isset( $id ) || !$id ) { $this->output( "Add matomo id to {$DBname}\n" ); - MatomoAnalytics::addSite( $DBname ); - } else { - continue; } } } diff --git a/maintenance/cleanupMatomos.php b/maintenance/cleanupMatomos.php index f5bfd84..7dfbbac 100644 --- a/maintenance/cleanupMatomos.php +++ b/maintenance/cleanupMatomos.php @@ -4,9 +4,8 @@ if ( $IP === false ) { $IP = __DIR__ . '/../../..'; } -require_once "$IP/maintenance/Maintenance.php"; -use MediaWiki\MediaWikiServices; +require_once "$IP/maintenance/Maintenance.php"; class CleanupMatomos extends Maintenance { public function __construct() { @@ -14,14 +13,13 @@ public function __construct() { $this->addDescription( 'Cleanup matomo ids that don\'t have corresponding cw_wikis entries.' ); $this->addOption( 'dry-run', 'Perform a dry run and do not actually remove any matomo ids.' ); + + $this->requireExtension( 'CreateWiki' ); + $this->requireExtension( 'MatomoAnalytics' ); } public function execute() { - $config = MediaWikiServices::getInstance() - ->getConfigFactory() - ->makeConfig( 'matomoanalytics' ); - - $dbw = $this->getDB( DB_PRIMARY, [], $config->get( 'CreateWikiDatabase' ) ); + $dbw = $this->getDB( DB_PRIMARY, [], $this->getConfig()->get( 'CreateWikiDatabase' ) ); $res = $dbw->select( 'matomo', @@ -37,10 +35,6 @@ public function execute() { foreach ( $res as $row ) { $DBname = $row->matomo_wiki; - if ( $DBname === 'default' ) { - continue; - } - $wiki = $dbw->selectField( 'cw_wikis', 'wiki_dbname', @@ -49,14 +43,13 @@ public function execute() { ); if ( !isset( $wiki ) || !$wiki ) { - if ( !$this->getOption( 'dry-run', false ) ) { - $this->output( "Remove matomo id from {$DBname}\n" ); - MatomoAnalytics::deleteSite( $DBname ); - + if ( $this->getOption( 'dry-run', false ) ) { + $this->output( "[DRY RUN] Would remove matomo id from {$DBname}\n" ); continue; } - $this->output( "[DRY RUN] Would remove matomo id from {$DBname}\n" ); + $this->output( "Remove matomo id from {$DBname}\n" ); + MatomoAnalytics::deleteSite( $DBname ); } } } diff --git a/maintenance/modifyMatomo.php b/maintenance/modifyMatomo.php index 8573fe1..cc30389 100644 --- a/maintenance/modifyMatomo.php +++ b/maintenance/modifyMatomo.php @@ -4,6 +4,7 @@ if ( $IP === false ) { $IP = __DIR__ . '/../../..'; } + require_once "$IP/maintenance/Maintenance.php"; class ModifyMatomo extends Maintenance { @@ -11,17 +12,17 @@ public function __construct() { parent::__construct(); $this->addDescription( 'Add or remove a wiki from matomo.' ); - $this->addOption( 'remove', 'Remove wiki from matomo', false, false ); + $this->addOption( 'remove', 'Remove wiki from matomo' ); + + $this->requireExtension( 'MatomoAnalytics' ); } public function execute() { - global $wgDBname; + $DBname = $this->getConfig()->get( 'DBname' ); - if ( $this->getOption( 'remove' ) ) { - MatomoAnalyticsHooks::wikiDeletion( null, $wgDBname ); - } else { - MatomoAnalyticsHooks::wikiCreation( $wgDBname ); - } + $this->getOption( 'remove', false ) ? + MatomoAnalytics::deleteSite( $DBname ) : + MatomoAnalytics::addSite( $DBname ); } }