Skip to content

Commit

Permalink
Merge pull request miraheze#94 from miraheze/add-CleanupMatomos
Browse files Browse the repository at this point in the history
Add CleanupMatomos maintenance script
  • Loading branch information
Reception123 authored Feb 4, 2023
2 parents a366b8d + edaadbc commit 87b19fa
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions maintenance/cleanupMatomos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";

use MediaWiki\MediaWikiServices;

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.' );
}

public function execute() {
$config = MediaWikiServices::getInstance()
->getConfigFactory()
->makeConfig( 'matomoanalytics' );

$dbw = $this->getDB( DB_PRIMARY, [], $config->get( 'CreateWikiDatabase' ) );

$res = $dbw->select(
'matomo',
'*',
[],
__METHOD__
);

if ( !$res || !is_object( $res ) ) {
throw new MWException( '$res was not set to a valid array.' );
}

foreach ( $res as $row ) {
$DBname = $row->matomo_wiki;

if ( $DBname === 'default' ) {
continue;
}

$wiki = $dbw->selectField(
'cw_wikis',
'wiki_dbname',
[ 'wiki_dbname' => $DBname ],
__METHOD__
);

if ( !isset( $wiki ) || !$wiki ) {
if ( !$this->getOption( 'dry-run', false ) ) {
$this->output( "Remove matomo id from {$DBname}\n" );
MatomoAnalytics::deleteSite( $DBname );

continue;
}

$this->output( "[DRY RUN] Would remove matomo id from {$DBname}\n" );
}
}
}
}

$maintClass = CleanupMatomos::class;
require_once RUN_MAINTENANCE_IF_MAIN;

0 comments on commit 87b19fa

Please sign in to comment.