Skip to content

Commit

Permalink
maintenance: add requireExtension() and use $this->getConfig() (mirah…
Browse files Browse the repository at this point in the history
…eze#95)

Also cleanup CleanupMatomos.php, and ModifyMatomo.php. For ModifyMatomo, it basically rewrites it, and use MatomoAnalytics directly instead of going through MatomoAnalyticsHooks.
  • Loading branch information
Universal-Omega authored Feb 4, 2023
1 parent 87b19fa commit a5ffa07
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 5 additions & 14 deletions maintenance/addMissingMatomos.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -49,10 +43,7 @@ public function execute() {

if ( !isset( $id ) || !$id ) {
$this->output( "Add matomo id to {$DBname}\n" );

MatomoAnalytics::addSite( $DBname );
} else {
continue;
}
}
}
Expand Down
25 changes: 9 additions & 16 deletions maintenance/cleanupMatomos.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@
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() {
parent::__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',
Expand All @@ -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',
Expand All @@ -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 );
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions maintenance/modifyMatomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}

require_once "$IP/maintenance/Maintenance.php";

class ModifyMatomo extends Maintenance {
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 );
}
}

Expand Down

0 comments on commit a5ffa07

Please sign in to comment.