Skip to content

Commit

Permalink
T9814: ensure matomo script isn't added more than once (miraheze#91)
Browse files Browse the repository at this point in the history
Since 1.38 it has been possible that the `SkinAfterBottomScripts` hook could be ran twice. Once is in `OutputPage::tailElement()`, once in `OutputPage::getBottomScripts()`. Since `OutputPage::tailElement()` also runs `OutputPage::getBottomScripts()`, if `OutputPage::tailElement()` is ran, the hook is ran twice and the script here gets added twice. This should prevent that.
  • Loading branch information
Universal-Omega authored Jan 29, 2023
1 parent 5fa48ec commit 819eb41
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## ChangeLog for MatomoAnalytics

### 1.1.0 (29-01-2023)
* Ensure matomo script isn't added more than once
* Require MediaWiki 1.39.0

### 1.0.9 (12-01-2023)
* Replace deprecated wfGetDB()

Expand Down
4 changes: 2 additions & 2 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"Universal Omega"
],
"url": "https://github.com/miraheze/MatomoAnalytics",
"version": "1.0.9",
"version": "1.1.0",
"descriptionmsg": "matomoanalytics-desc",
"license-name": "GPL-3.0-or-later",
"type": "specialpage",
"requires": {
"MediaWiki": ">= 1.38.0"
"MediaWiki": ">= 1.39.0"
},
"AvailableRights": [
"noanalytics"
Expand Down
14 changes: 9 additions & 5 deletions includes/MatomoAnalyticsHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public static function wikiRename( $dbw, $old, $new ) {
public static function matomoScript( $skin, &$text = '' ) {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'matomoanalytics' );

// Check if JS tracking is disabled and bow out early
if ( $config->get( 'MatomoAnalyticsDisableJS' ) === true ) {
static $alreadyDone = false;

// Check if JS tracking is disabled or if the script has already been added and bow out early
if ( $alreadyDone || $config->get( 'MatomoAnalyticsDisableJS' ) ) {
return true;
}

$user = RequestContext::getMain()->getUser();
$user = $skin->getUser();
$mAId = MatomoAnalytics::getSiteID( $config->get( 'DBname' ) );

$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
Expand All @@ -56,7 +58,7 @@ public static function matomoScript( $skin, &$text = '' ) {
$jstitle = Xml::encodeJsVar( $title->getPrefixedText() );
$dbname = Xml::encodeJsVar( $config->get( 'DBname' ) );
$urltitle = $title->getPrefixedURL();
$userType = $user->isRegistered() ? "User" : "Anonymous";
$userType = $user->isRegistered() ? 'User' : 'Anonymous';
$cookieDisable = (int)$config->get( 'MatomoAnalyticsDisableCookie' );
$forceGetRequest = (int)$config->get( 'MatomoAnalyticsForceGetRequest' );
$text .= <<<SCRIPT
Expand Down Expand Up @@ -84,9 +86,11 @@ public static function matomoScript( $skin, &$text = '' ) {
})();
</script>
<noscript><p><img src="{$serverurl}matomo.php?idsite={$id}&amp;rec=1&amp;action_name={$urltitle}" style="border:0;" alt="" /></p></noscript>
SCRIPT;
SCRIPT;
}

$alreadyDone = true;

return true;
}
}

0 comments on commit 819eb41

Please sign in to comment.