Page MenuHomeMiraheze
Paste P401

fixMatomo.php
ActivePublic

Authored by Paladox on Apr 5 2021, 21:57.
Tags
None
Referenced Files
F1420112: raw-paste-data.txt
Apr 5 2021, 21:58
F1420111: raw-paste-data.txt
Apr 5 2021, 21:57
Subscribers
Tokens
"Like" token, awarded by Dmehus.
<?php
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
use MediaWiki\MediaWikiServices;
class addMissingMatomos extends Maintenance {
public function __construct() {
parent::__construct();
$this->addOption( 'default', 'Add missing matomo ids' );
}
public function execute() {
$config = MediaWikiServices::getInstance()
->getConfigFactory()
->makeConfig( 'matomoanalytics' );
$dbw = wfGetDB( DB_MASTER, [], $config->get( 'CreateWikiDatabase' ) );
$res = $dbw->select(
'cw_wikis',
'*',
[],
__METHOD__
);
if ( !$res || !is_object( $res ) ) {
throw new MWException( '$res was not set to a valid array.' );
}
foreach ( $res as $row ) {
$DBname = $row->wiki_dbname;
if ( $DBname === 'default' ) {
continue;
}
$id = $dbw->selectField(
'matomo',
'matomo_id',
[ 'matomo_wiki' => $DBname ],
__METHOD__
);
if ( !isset( $id ) || !$id ) {
$this->output( "Add matomo id to {$DBname}");
MatomoAnalytics::addSite( $DBname );
} else {
continue;
}
}
}
}
$maintClass = 'addMissingMatomos';
require_once RUN_MAINTENANCE_IF_MAIN;

Event Timeline

Paladox edited the content of this paste. (Show Details)
RhinosF1 changed the title of this paste from untitled to fixMatomo.php.Apr 5 2021, 22:00
RhinosF1 updated the paste's language from autodetect to php.
Dmehus subscribed.

Wow, cool, Phabricator lets you have syntax highlighting templates, and change the syntax highlighting on each Paste. Good work on this code, @Paladox