forked from miraheze/MatomoAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request miraheze#94 from miraheze/add-CleanupMatomos
Add CleanupMatomos maintenance script
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |