Skip to content

Commit

Permalink
Add some more logging and also disable cache when deleting/rename a w…
Browse files Browse the repository at this point in the history
…iki (#42)
  • Loading branch information
paladox committed Mar 14, 2021
1 parent ec3deed commit 411142e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
@@ -1,5 +1,8 @@
== ChangeLog for MatomoAnalytics ==

=== 1.0.5.7 (14-03-2021) ===
* Add some more logging and also disable cache when deleting/rename a wik

=== 1.0.5.6 (12-03-2021) ===
* add license-name

Expand Down
2 changes: 1 addition & 1 deletion extension.json
Expand Up @@ -5,7 +5,7 @@
"Southparkfan"
],
"url": "https://github.com/miraheze/MatomoAnalytics",
"version": "1.0.5.6",
"version": "1.0.5.7",
"descriptionmsg": "matomoanalytics-desc",
"license-name": "GPL-3.0-or-later",
"type": "specialpage",
Expand Down
62 changes: 46 additions & 16 deletions includes/MatomoAnalytics.php
Expand Up @@ -3,8 +3,20 @@
use MediaWiki\MediaWikiServices;

class MatomoAnalytics {
private static function getConfig() {
return MediaWikiServices::getInstance()
->getConfigFactory()
->makeConfig( 'matomoanalytics' );
}

private static function getLogger() {
return \MediaWiki\Logger\LoggerFactory::getInstance( 'MatomoAnalytics' );
}

public static function addSite( $dbname ) {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'matomoanalytics' );
$config = static::getConfig();

$logger = static::getLogger();

$siteReply = MediaWikiServices::getInstance()->getHttpRequestFactory()->get(
wfAppendQuery(
Expand All @@ -23,13 +35,19 @@ public static function addSite( $dbname ) {

$siteJson = FormatJson::decode( $siteReply, true );

if ( !$siteJson ) {
$logger->error( "Could not create id for {$dbname}." );
return;
}

$siteId = $siteJson['value'];
if ( $config->get( 'MatomoAnalyticsUseDB' ) ) {
$dbw = wfGetDB( DB_MASTER, [], $config->get( 'MatomoAnalyticsDatabase' ) );
try {
$dbw->insert(
'matomo',
[
'matomo_id' => $siteJson['value'],
'matomo_id' => $siteId,
'matomo_wiki' => $dbname,
],
__METHOD__
Expand All @@ -39,16 +57,18 @@ public static function addSite( $dbname ) {
}
}

return $siteJson['value'];
$logger->debug( "Successfully created {$dbname} with id {$siteId}." );
}

public static function deleteSite( $dbname ) {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'matomoanalytics' );
$config = static::getConfig();

$siteId = static::getSiteID( $dbname, true );

$siteId = static::getSiteID( $dbname );
$logger = static::getLogger();

if ( $config->get( 'MatomoAnalyticsUseDB' ) &&
(int)$siteId === (int)$config->get( 'MatomoAnalyticsSiteID' )
(string)$siteId === (string)$config->get( 'MatomoAnalyticsSiteID' )
) {
return;
}
Expand Down Expand Up @@ -81,17 +101,21 @@ public static function deleteSite( $dbname ) {
$key = $cache->makeKey( 'matomo', 'id' );
$cache->delete( $key );
}

$logger->debug( "Successfully deleted {$dbname} with id {$siteId}." );

return true;
}

public static function renameSite( $old, $new ) {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'matomoanalytics' );
public static function renameSite( $oldDb, $newDb ) {
$config = static::getConfig();

$siteId = static::getSiteID( $oldDb, true );

$siteId = static::getSiteID( $old );
$logger = static::getLogger();

if ( $config->get( 'MatomoAnalyticsUseDB' ) &&
(int)$siteId === (int)$config->get( 'MatomoAnalyticsSiteID' )
(string)$siteId === (string)$config->get( 'MatomoAnalyticsSiteID' )
) {
return;
}
Expand All @@ -118,7 +142,7 @@ public static function renameSite( $old, $new ) {

$dbw->update(
'matomo',
[ 'matomo_wiki' => $new ],
[ 'matomo_wiki' => $newDb ],
[ 'matomo_id' => $siteId ],
__METHOD__
);
Expand All @@ -128,21 +152,27 @@ public static function renameSite( $old, $new ) {
$cache->delete( $key );
}

if ( $siteId === static::getSiteID( $new ) ) {
if ( (string)$siteId === (string)static::getSiteID( $newDb ) ) {
$logger->debug( "Successfully renamed {$oldDb} to {$newDb} with id {$siteId}." );

return true;
} else {
$logger->error( "Failed to rename {$oldDb} to {$newDb} with id {$siteId}." );

throw new MWException( 'Error in renaming Matomo references' );
}
}

public static function getSiteID( $dbname ) {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'matomoanalytics' );
public static function getSiteID( string $dbname, bool $disableCache = false ) {
$config = static::getConfig();

$logger = static::getLogger();

if ( $config->get( 'MatomoAnalyticsUseDB' ) ) {
$cache = ObjectCache::getLocalClusterInstance();
$key = $cache->makeKey( 'matomo', 'id' );
$cacheId = $cache->get( $key );
if ( $cacheId ) {
if ( $cacheId && !$disableCache ) {
return $cacheId;
}

Expand All @@ -155,7 +185,7 @@ public static function getSiteID( $dbname ) {
);

if ( !isset( $id ) || !$id ) {
wfDebugLog( 'MatomoAnalytics', "could not find {$dbname} in matomo table" );
$logger->error( "Could not find {$dbname} in matomo table." );

// Because the site is not found in the matomo table,
// we default to a value set in 'MatomoAnalyticsSiteID' which is 1.
Expand Down

0 comments on commit 411142e

Please sign in to comment.