比较提交
20 次代码提交
作者 | SHA1 | 提交日期 |
---|---|---|
WaitSpring | 6bdf708da1 | |
WaitSpring | 59690acb29 | |
WaitSpring | f1c1cb7afd | |
WaitSpring | 37350e420a | |
WaitSpring | eab7846e72 | |
WaitSpring | 9d934d0a06 | |
WaitSpring | 011f55d958 | |
WaitSpring | 498cce658e | |
WaitSpring | 14ad72682e | |
WaitSpring | bed7b88ad9 | |
WaitSpring | 44e640a5df | |
WaitSpring | e7a5bbad7c | |
WaitSpring | 664099ab45 | |
WaitSpring | 0eca329181 | |
WaitSpring | 8ab3991682 | |
WaitSpring | 5e9b70e8da | |
WaitSpring | 4d4d893b83 | |
WaitSpring | bb92e0bf83 | |
WaitSpring | 3fd501d864 | |
WaitSpring | 35e517c057 |
|
@ -13,7 +13,7 @@ jobs:
|
|||
|
||||
strategy:
|
||||
matrix:
|
||||
php: [ '7.3', '7.4', '8.0' ]
|
||||
php: [ '7.3', '7.4', '8.0', '8.1' ]
|
||||
|
||||
runs-on: 'ubuntu-latest'
|
||||
|
||||
|
|
73
README.md
73
README.md
|
@ -1,73 +0,0 @@
|
|||
Mamoto extension for MediaWiki
|
||||
==============================
|
||||
Version 4.0.1
|
||||
- Last update: 28 February 2019
|
||||
|
||||
This the Mamoto (ex-Piwik) integration extension for MediaWiki
|
||||
software. The extension is only useful if you've got a MediaWiki
|
||||
installation; it can only be installed by the administrator of the site.
|
||||
|
||||
Minimum requirements
|
||||
--------------------------------
|
||||
|
||||
1. MediaWiki 1.25+
|
||||
|
||||
2. A Matomo (0.4+) installation with the site configured
|
||||
|
||||
Installation instructions
|
||||
---------------------------------
|
||||
Please, read them carefully. They're not very difficult to understand,
|
||||
but **ALL** steps are necessary:
|
||||
|
||||
1. Create a folder called "Matomo" in your extensions directory
|
||||
|
||||
2. Upload extension.json and Matomo.hooks.php in the "Matomo" folder you've just created
|
||||
|
||||
3. Edit your LocalSettings.php and, at the end of the file, add the
|
||||
following:
|
||||
|
||||
wfLoadExtension( 'Matomo' );
|
||||
|
||||
|
||||
4. Configure the Matomo URL and site-id. To do so; edit the LocalSettings and set up the following variables:
|
||||
> $wgMatomoURL = "matomo-host.tld/dir/";
|
||||
|
||||
> $wgMatomoIDSite = "matomo_idsite";
|
||||
|
||||
**IMPORTANT** Do not define the protocol of the $wgMatomoURL
|
||||
|
||||
Note: Change the value of $wgMatomoURL with the URL, without the protocol
|
||||
but including the domain name, where you installed Matomo.
|
||||
Remember to add the trailing slash!
|
||||
|
||||
5. Enjoy our extension!
|
||||
> Note: to check if the extension has succesfully installed; go to your wiki and check if the Matomo extension is present on the bottom of the Wiki source code.
|
||||
|
||||
|
||||
Custom variables
|
||||
------------------------
|
||||
* Disable cookies by setting the ```$wgMatomoDisableCookies``` variable to ```false```.
|
||||
> For example: $wgMatomoDisableCookies = false;
|
||||
|
||||
* Ignore regular editors: set ```$wgMatomoIgnoreEditors``` to ```true```
|
||||
* Ignore Bots: set ```$wgMatomoIgnoreBots``` to ```true```
|
||||
* Ignore sysop users: set ```$wgMatomoIgnoreSysops``` to ```true```
|
||||
|
||||
* To define custom javascript tags in the Matomo javascript code, its possible to define the $wgMatomoCustomJS variable. For example if you have a single setting to insert; use the following code:
|
||||
> ```$wgMatomoCustomJS = "_paq.push(['trackGoal', '1']);"```
|
||||
|
||||
If you have multiple variables to define; use an array. For example:
|
||||
> `` $wgMatomoCustomJS = array(
|
||||
"_paq.push(['setCustomVariable', '1','environment','production']);",
|
||||
"_paq.push(['setCustomVariable', '1','is_user','yes']);"
|
||||
);``
|
||||
|
||||
* If you want to change the title of your pages inside the Matomo tracker,
|
||||
you can set ```$wgMatomoActionName``` inside your LocalSettings.php file.
|
||||
|
||||
* In case you want to include the title as, for example,
|
||||
"wiki/Title of the page", you can set ```$wgMatomoUsePageTitle``` to
|
||||
```true``` and set ```$wgMatomoActionName``` to ```wiki/```. The extension will print matomo_action_name = 'wiki/Title of the page';
|
||||
|
||||
* If you want to track the username of the visitor with the Matomo feature User ID (needs Matomo >= 2.7.0)
|
||||
set the ```$wgMatomoTrackUsernames``` to true in LocalSettings.php.
|
134
composer.json
134
composer.json
|
@ -1,67 +1,71 @@
|
|||
{
|
||||
"name": "mediawiki/matomo",
|
||||
"type": "mediawiki-extension",
|
||||
"description": "The Matomo (ex-Piwik) extension inserts your Matomo tracking code to the bottom of every page.",
|
||||
"keywords": [
|
||||
"Piwik",
|
||||
"Matomo",
|
||||
"MediaWiki"
|
||||
],
|
||||
"homepage": "https://www.mediawiki.org/wiki/Extension:Matomo",
|
||||
"license": "GPL-2.0-or-later",
|
||||
"support": {
|
||||
"issues": "https://github.com/DaSchTour/piwik-mediawiki-extension/issues",
|
||||
"source": "https://github.com/DaSchTour/piwik-mediawiki-extension"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Isb1009",
|
||||
"email": "isb1009@gmail.com",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "DaSch",
|
||||
"email": "dasch@daschmedia.de",
|
||||
"homepage": "http://www.daschmedia.de",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "Seb35",
|
||||
"homepage": "https://www.seb35.fr",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "Youri van den Bogert",
|
||||
"email": "youri@vandenbogert.eu",
|
||||
"homepage": "https://github.com/YOUR1",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.0",
|
||||
"composer/installers": "1.*,>=1.0.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"MediaWiki\\Extension\\Matomo\\": "src/"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"mediawiki/mediawiki-codesniffer": "38.0.0",
|
||||
"mediawiki/minus-x": "1.1.1",
|
||||
"php-parallel-lint/php-console-highlighter": "0.5.0",
|
||||
"php-parallel-lint/php-parallel-lint": "1.3.1"
|
||||
},
|
||||
"scripts": {
|
||||
"test": [
|
||||
"composer validate",
|
||||
"parallel-lint . --exclude vendor --exclude node_modules",
|
||||
"phpcs -p -s",
|
||||
"minus-x check ."
|
||||
],
|
||||
"fix": [
|
||||
"minus-x fix .",
|
||||
"phpcbf"
|
||||
]
|
||||
}
|
||||
"name": "mediawiki/matomo-new",
|
||||
"type": "mediawiki-extension",
|
||||
"description": "Adding Matomo Tracking Code",
|
||||
"keywords": [
|
||||
"Piwik",
|
||||
"Matomo",
|
||||
"MediaWiki"
|
||||
],
|
||||
"homepage": "https://git.qiuwen.net.cn/Fork/mediawiki-extensions-MatomoNew",
|
||||
"license": "GPL-2.0-or-later",
|
||||
"support": {
|
||||
"source": "https://git.qiuwen.net.cn/Fork/mediawiki-extensions-MatomoNew"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Isb1009",
|
||||
"email": "isb1009@gmail.com",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "DaSch",
|
||||
"email": "dasch@daschmedia.de",
|
||||
"homepage": "http://www.daschmedia.de",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "Seb35",
|
||||
"homepage": "https://www.seb35.fr",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "Youri van den Bogert",
|
||||
"email": "youri@vandenbogert.eu",
|
||||
"homepage": "https://github.com/YOUR1",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "WaitSpring",
|
||||
"email": "me@waitspring.com",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.0",
|
||||
"composer/installers": "1.*,>=1.0.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"MediaWiki\\Extension\\MatomoNew\\": "src/"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"mediawiki/mediawiki-codesniffer": "38.0.0",
|
||||
"mediawiki/minus-x": "1.1.1",
|
||||
"php-parallel-lint/php-console-highlighter": "0.5.0",
|
||||
"php-parallel-lint/php-parallel-lint": "1.3.1"
|
||||
},
|
||||
"scripts": {
|
||||
"test": [
|
||||
"composer validate",
|
||||
"parallel-lint . --exclude vendor --exclude node_modules",
|
||||
"phpcs -p -s",
|
||||
"minus-x check ."
|
||||
],
|
||||
"fix": [
|
||||
"minus-x fix .",
|
||||
"phpcbf"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,46 +1,44 @@
|
|||
{
|
||||
"name": "Matomo",
|
||||
"version": "4.0.1",
|
||||
"author": [
|
||||
"Isb1009",
|
||||
"[http://www.daschmedia.de DaSch]",
|
||||
"[https://www.mediawiki.org/wiki/User:Seb35 Seb35]",
|
||||
"[https://github.com/YOUR1 Youri van den Bogert]"
|
||||
],
|
||||
"url": "https://www.mediawiki.org/wiki/Extension:Matomo",
|
||||
"description": "Adding Matomo Tracking Code",
|
||||
"license-name": "GPL-2.0-or-later",
|
||||
"type": "other",
|
||||
"requires": {
|
||||
"MediaWiki": ">= 1.25.0"
|
||||
},
|
||||
"config": {
|
||||
"MatomoIDSite": "",
|
||||
"MatomoURL": "",
|
||||
"MatomoIgnoreSysops": true,
|
||||
"MatomoIgnoreBots": true,
|
||||
"MatomoCustomJS": "",
|
||||
"MatomoUsePageTitle": false,
|
||||
"MatomoActionName": "",
|
||||
"MatomoDisableCookies": false,
|
||||
"MatomoProtocol": "auto",
|
||||
"MatomoUsernameCustomVariable": [],
|
||||
"MatomoTrackUsernames": false,
|
||||
"MatomoJSFileURL": null
|
||||
},
|
||||
"Hooks": {
|
||||
"SkinAfterBottomScripts": [
|
||||
"MediaWiki\\Extension\\Matomo\\Hooks::MatomoSetup"
|
||||
],
|
||||
"SpecialSearchResults": [
|
||||
"MediaWiki\\Extension\\Matomo\\Hooks::onSpecialSearchResults"
|
||||
],
|
||||
"SpecialSearchSetupEngine": [
|
||||
"MediaWiki\\Extension\\Matomo\\Hooks::onSpecialSearchSetupEngine"
|
||||
]
|
||||
},
|
||||
"AutoloadClasses": {
|
||||
"MediaWiki\\Extension\\Matomo\\Hooks": "src/Hooks.php"
|
||||
},
|
||||
"manifest_version": 1
|
||||
}
|
||||
{
|
||||
"name": "MatomoNew",
|
||||
"version": "4.0.2",
|
||||
"author": [
|
||||
"Isb1009",
|
||||
"[http://www.daschmedia.de DaSch]",
|
||||
"[https://www.mediawiki.org/wiki/User:Seb35 Seb35]",
|
||||
"[https://github.com/YOUR1 Youri van den Bogert]",
|
||||
"WaitSpring"
|
||||
],
|
||||
"url": "https://git.qiuwen.net.cn/Fork/mediawiki-extensions-MatomoNew",
|
||||
"description": "Adding Matomo Tracking Code",
|
||||
"license-name": "GPL-2.0-or-later",
|
||||
"type": "other",
|
||||
"requires": {
|
||||
"MediaWiki": ">= 1.25.0"
|
||||
},
|
||||
"config": {
|
||||
"MatomoIDSite": "",
|
||||
"MatomoProtocol": "auto",
|
||||
"MatomoURL": "",
|
||||
"MatomoEndpoint": "matomo.php",
|
||||
"MatomoIgnoreBots": true,
|
||||
"MatomoUsePageTitle": true,
|
||||
"MatomoActionName": "",
|
||||
"MatomoDisableCookies": false,
|
||||
"MatomoTrackUsernames": false
|
||||
},
|
||||
"Hooks": {
|
||||
"SkinAfterBottomScripts": [
|
||||
"MediaWiki\\Extension\\MatomoNew\\Hooks::MatomoSetup"
|
||||
],
|
||||
"SpecialSearchResults": [
|
||||
"MediaWiki\\Extension\\MatomoNew\\Hooks::onSpecialSearchResults"
|
||||
],
|
||||
"SpecialSearchSetupEngine": [
|
||||
"MediaWiki\\Extension\\MatomoNew\\Hooks::onSpecialSearchSetupEngine"
|
||||
]
|
||||
},
|
||||
"AutoloadClasses": {
|
||||
"MediaWiki\\Extension\\MatomoNew\\Hooks": "src/Hooks.php"
|
||||
},
|
||||
"manifest_version": 1
|
||||
}
|
||||
|
|
378
src/Hooks.php
378
src/Hooks.php
|
@ -1,213 +1,165 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\Matomo;
|
||||
|
||||
use RequestContext;
|
||||
use Xml;
|
||||
|
||||
class Hooks {
|
||||
|
||||
/** @var string|null Searched term in Special:Search. */
|
||||
public static $searchTerm = null;
|
||||
|
||||
/** @var string|null Search profile in Special:Search (search category in Piwik vocabulary). */
|
||||
public static $searchProfile = null;
|
||||
|
||||
/** @var int|null Number of results in Special:Search. */
|
||||
public static $searchCount = null;
|
||||
|
||||
/**
|
||||
* Initialize the Matomo hook
|
||||
*
|
||||
* @param string $skin
|
||||
* @param string &$text
|
||||
* @return bool
|
||||
*/
|
||||
public static function MatomoSetup( $skin, &$text ) {
|
||||
$text = self::addMatomo( $skin->getTitle() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parameter with either the new prefix $wgMatomo or the old $wgPiwik.
|
||||
*
|
||||
* @param string $name Parameter name without any prefix.
|
||||
* @return mixed|null Parameter value.
|
||||
*/
|
||||
public static function getParameter( $name ) {
|
||||
$config = \MediaWiki\MediaWikiServices::getInstance()->getMainConfig();
|
||||
if ( $config->has( "Piwik$name" ) ) {
|
||||
return $config->get( "Piwik$name" );
|
||||
} elseif ( $config->has( "Matomo$name" ) ) {
|
||||
return $config->get( "Matomo$name" );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to save some data in Special:Search.
|
||||
*
|
||||
* @param string $term Searched term.
|
||||
* @param SearchResultSet|null $titleMatches Results in the titles.
|
||||
* @param SearchResultSet|null $textMatches Results in the fulltext.
|
||||
* @return true
|
||||
*/
|
||||
public static function onSpecialSearchResults( $term, $titleMatches, $textMatches ) {
|
||||
self::$searchTerm = $term;
|
||||
self::$searchCount = 0;
|
||||
if ( $titleMatches instanceof SearchResultSet ) {
|
||||
self::$searchCount += (int)$titleMatches->numRows();
|
||||
}
|
||||
if ( $textMatches instanceof SearchResultSet ) {
|
||||
self::$searchCount += (int)$textMatches->numRows();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to save some data in Special:Search.
|
||||
*
|
||||
* @param SpecialSearch $search Special page.
|
||||
* @param string|null $profile Search profile.
|
||||
* @param SearchEngine $engine Search engine.
|
||||
* @return true
|
||||
*/
|
||||
public static function onSpecialSearchSetupEngine( $search, $profile, $engine ) {
|
||||
self::$searchProfile = $profile;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Matomo script
|
||||
* @param string $title
|
||||
* @return string
|
||||
*/
|
||||
public static function addMatomo( $title ) {
|
||||
$user = RequestContext::getMain()->getUser();
|
||||
// Is Matomo disabled for bots?
|
||||
if ( $user->isAllowed( 'bot' ) && self::getParameter( 'IgnoreBots' ) ) {
|
||||
return '<!-- Matomo extension is disabled for bots -->';
|
||||
}
|
||||
|
||||
// Ignore Wiki System Operators
|
||||
if ( $user->isAllowed( 'protect' ) && self::getParameter( 'IgnoreSysops' ) ) {
|
||||
return '<!-- Matomo tracking is disabled for users with \'protect\' rights (i.e., sysops) -->';
|
||||
}
|
||||
|
||||
// Ignore Wiki Editors
|
||||
if ( $user->isAllowed( 'edit' ) && self::getParameter( 'IgnoreEditors' ) ) {
|
||||
return "<!-- Matomo tracking is disabled for users with 'edit' rights -->";
|
||||
}
|
||||
|
||||
$idSite = self::getParameter( 'IDSite' );
|
||||
$matomoURL = self::getParameter( 'URL' );
|
||||
$protocol = self::getParameter( 'Protocol' );
|
||||
$customJS = self::getParameter( 'CustomJS' );
|
||||
$jsFileURL = self::getParameter( 'JSFileURL' );
|
||||
|
||||
// Missing configuration parameters
|
||||
if ( empty( $idSite ) || empty( $matomoURL ) ) {
|
||||
return '<!-- You need to set the settings for Matomo -->';
|
||||
}
|
||||
|
||||
$finalActionName = self::getParameter( 'ActionName' );
|
||||
if ( self::getParameter( 'UsePageTitle' ) ) {
|
||||
$finalActionName .= $title->getPrefixedText();
|
||||
}
|
||||
|
||||
// Check if disablecookies flag
|
||||
if ( self::getParameter( 'DisableCookies' ) ) {
|
||||
$disableCookiesStr = PHP_EOL . '_paq.push(["disableCookies"]);';
|
||||
} else { $disableCookiesStr = null;
|
||||
}
|
||||
|
||||
// Check if we have custom JS
|
||||
if ( !empty( $customJS ) ) {
|
||||
|
||||
// Check if array is given
|
||||
// If yes we have multiple lines/variables to declare
|
||||
if ( is_array( $customJS ) ) {
|
||||
|
||||
// Make empty string with a new line
|
||||
$customJs = PHP_EOL;
|
||||
|
||||
// Store the lines in the $customJs line
|
||||
foreach ( $customJS as $customJsLine ) {
|
||||
$customJs .= $customJsLine;
|
||||
}
|
||||
|
||||
// CustomJs is string
|
||||
} else { $customJs = PHP_EOL . $customJS;
|
||||
}
|
||||
|
||||
// Contents are empty
|
||||
} else { $customJs = null;
|
||||
}
|
||||
|
||||
// Track search results
|
||||
$trackingType = 'trackPageView';
|
||||
$jsTrackingSearch = '';
|
||||
$urlTrackingSearch = '';
|
||||
if ( self::$searchTerm !== null ) {
|
||||
// JavaScript
|
||||
$trackingType = 'trackSiteSearch';
|
||||
$jsTerm = Xml::encodeJsVar( self::$searchTerm );
|
||||
$jsCategory = self::$searchProfile === null ? 'false' : Xml::encodeJsVar( self::$searchProfile );
|
||||
$jsResultsCount = self::$searchCount === null ? 'false' : self::$searchCount;
|
||||
$jsTrackingSearch = ",$jsTerm,$jsCategory,$jsResultsCount";
|
||||
|
||||
// URL
|
||||
$urlTrackingSearch = [ 'search' => self::$searchTerm ];
|
||||
if ( self::$searchProfile !== null ) {
|
||||
$urlTrackingSearch += [ 'search_cat' => self::$searchProfile ];
|
||||
}
|
||||
if ( self::$searchCount !== null ) {
|
||||
$urlTrackingSearch += [ 'search_count' => self::$searchCount ];
|
||||
}
|
||||
$urlTrackingSearch = '&' . wfArrayToCgi( $urlTrackingSearch );
|
||||
}
|
||||
|
||||
// Track username based on https://matomo.org/docs/user-id/ The user
|
||||
// name for anonymous visitors is their IP address which Matomo already
|
||||
// records.
|
||||
if ( self::getParameter( 'TrackUsernames' ) && $user->isRegistered() ) {
|
||||
$username = Xml::encodeJsVar( $user->getName() );
|
||||
$customJs .= PHP_EOL . "_paq.push([\"setUserId\",{$username}]);";
|
||||
}
|
||||
|
||||
// Check if server uses https
|
||||
if ( $protocol == 'auto' ) {
|
||||
|
||||
if ( isset( $_SERVER['HTTPS'] ) && ( $_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1 ) || isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'
|
||||
) {
|
||||
$protocol = 'https';
|
||||
} else {
|
||||
$protocol = 'http';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Prevent XSS
|
||||
$finalActionName = Xml::encodeJsVar( $finalActionName );
|
||||
|
||||
// If $wgMatomoJSFileURL is null the locations are $wgMatomoURL/piwik.php and $wgMatomoURL/piwik.js
|
||||
// Else they are $wgMatomoURL/piwik.php and $wgMatomoJSFileURL
|
||||
$jsMatomoURL = '';
|
||||
$jsMatomoURLCommon = '';
|
||||
if ( $jsFileURL === null ) {
|
||||
$jsFileURL = 'piwik.js';
|
||||
$jsMatomoURLCommon = '+' . Xml::encodeJsVar( $matomoURL . '/' );
|
||||
} else {
|
||||
$jsMatomoURL = '+' . Xml::encodeJsVar( $matomoURL . '/' );
|
||||
}
|
||||
$jsMatomoJSFileURL = Xml::encodeJsVar( $jsFileURL );
|
||||
|
||||
// Matomo script
|
||||
$script = <<<MATOMO
|
||||
<script type="text/javascript">var _paq=_paq||[];{$disableCookiesStr}{$customJs}_paq.push(["{$trackingType}"{$jsTrackingSearch}]);_paq.push(["enableLinkTracking"]);(function(){var u=(("https:"==document.location.protocol)?"https":"http")+"://"{$jsMatomoURLCommon};_paq.push(["setTrackerUrl",u{$jsMatomoURL}+"piwik.php"]);_paq.push(["setSiteId","{$idSite}"]);var d=document,g=d.createElement("script"),s=d.getElementsByTagName("script")[0];g.type="text/javascript";g.defer=true;g.async=true;g.src=u+{$jsMatomoJSFileURL};s.parentNode.insertBefore(g,s)})();</script>
|
||||
<noscript><img src="{$protocol}://{$matomoURL}/piwik.php?idsite={$idSite}&rec=1{$urlTrackingSearch}" width="1" height="1" alt="" /></noscript>
|
||||
MATOMO;
|
||||
|
||||
return $script;
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\MatomoNew;
|
||||
|
||||
use RequestContext;
|
||||
|
||||
class Hooks
|
||||
{
|
||||
|
||||
/** @var string|null Searched term in Special:Search. */
|
||||
public static $searchTerm = null;
|
||||
|
||||
/** @var string|null Search profile in Special:Search (search category in Piwik vocabulary). */
|
||||
public static $searchProfile = null;
|
||||
|
||||
/** @var int|null Number of results in Special:Search. */
|
||||
public static $searchCount = null;
|
||||
|
||||
/**
|
||||
* Initialize the Matomo hook
|
||||
*
|
||||
* @param object $skin
|
||||
* @param string &$text
|
||||
* @return bool
|
||||
*/
|
||||
public static function MatomoSetup($skin, &$text)
|
||||
{
|
||||
$text = self::addMatomo($skin->getTitle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parameter with either the new prefix $wgMatomo or the old $wgPiwik.
|
||||
*
|
||||
* @param string $name Parameter name without any prefix.
|
||||
* @return mixed|null Parameter value.
|
||||
*/
|
||||
public static function getParameter($name)
|
||||
{
|
||||
$config = \MediaWiki\MediaWikiServices::getInstance()->getMainConfig();
|
||||
if ($config->has("Piwik$name")) {
|
||||
return $config->get("Piwik$name");
|
||||
} elseif ($config->has("Matomo$name")) {
|
||||
return $config->get("Matomo$name");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to save some data in Special:Search.
|
||||
*
|
||||
* @param string $term Searched term.
|
||||
* @param SearchResultSet|null $titleMatches Results in the titles.
|
||||
* @param SearchResultSet|null $textMatches Results in the fulltext.
|
||||
* @return true
|
||||
*/
|
||||
public static function onSpecialSearchResults($term, $titleMatches, $textMatches)
|
||||
{
|
||||
self::$searchTerm = $term;
|
||||
self::$searchCount = 0;
|
||||
if ($titleMatches instanceof SearchResultSet) {
|
||||
self::$searchCount += (int)$titleMatches->numRows();
|
||||
}
|
||||
if ($textMatches instanceof SearchResultSet) {
|
||||
self::$searchCount += (int)$textMatches->numRows();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to save some data in Special:Search.
|
||||
*
|
||||
* @param SpecialSearch $search Special page.
|
||||
* @param string|null $profile Search profile.
|
||||
* @param SearchEngine $engine Search engine.
|
||||
* @return true
|
||||
*/
|
||||
public static function onSpecialSearchSetupEngine($search, $profile, $engine)
|
||||
{
|
||||
self::$searchProfile = $profile;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Matomo script
|
||||
* @param object $title
|
||||
* @return string
|
||||
*/
|
||||
public static function addMatomo($title)
|
||||
{
|
||||
$user = RequestContext::getMain()->getUser();
|
||||
// Is Matomo disabled for bots?
|
||||
if ($user->isAllowed('bot') && self::getParameter('IgnoreBots')) {
|
||||
return '<!-- Matomo extension is disabled for bots -->';
|
||||
}
|
||||
|
||||
$idSite = self::getParameter('IDSite');
|
||||
$matomoURL = self::getParameter('URL');
|
||||
$protocol = self::getParameter('Protocol');
|
||||
$endpoint = self::getParameter('Endpoint');
|
||||
|
||||
// Missing configuration parameters
|
||||
if (empty($idSite) || empty($matomoURL)) {
|
||||
return '<!-- You need to set the settings for Matomo -->';
|
||||
}
|
||||
|
||||
$finalActionName = self::getParameter('ActionName');
|
||||
if (self::getParameter('UsePageTitle')) {
|
||||
$finalActionName .= $title->getPrefixedText();
|
||||
}
|
||||
|
||||
// Track search results
|
||||
$urlTrackingSearch = '';
|
||||
if (self::$searchTerm !== null) {
|
||||
// URL
|
||||
$urlTrackingSearch = ['search' => self::$searchTerm];
|
||||
if (self::$searchProfile !== null) {
|
||||
$urlTrackingSearch += ['search_cat' => self::$searchProfile];
|
||||
}
|
||||
if (self::$searchCount !== null) {
|
||||
$urlTrackingSearch += ['search_count' => self::$searchCount];
|
||||
}
|
||||
$urlTrackingSearch = '&' . wfArrayToCgi($urlTrackingSearch);
|
||||
}
|
||||
|
||||
// Track username based on https://matomo.org/docs/user-id/ The user
|
||||
// name for anonymous visitors is their IP address which Matomo already
|
||||
// records.
|
||||
$finalUsername = '';
|
||||
if (self::getParameter('TrackUsernames') && $user->isRegistered()) {
|
||||
$username = $user->getName();
|
||||
$finalUsername = '&uid=' . urlencode($username);
|
||||
}
|
||||
|
||||
// Check if server uses https
|
||||
if ($protocol == 'auto') {
|
||||
if (
|
||||
isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'
|
||||
) {
|
||||
$protocol = 'https';
|
||||
} else {
|
||||
$protocol = 'http';
|
||||
}
|
||||
}
|
||||
|
||||
// Check HTTP Referer
|
||||
$finalURLRef = '';
|
||||
if (isset($_SERVER['HTTP_REFERER'])) {
|
||||
$urlref = $_SERVER['HTTP_REFERER'];
|
||||
$finalURLRef = '&urlref=' . urlencode($urlref);
|
||||
}
|
||||
|
||||
// add URL encode
|
||||
$finalActionName = '&action_name=' . urlencode($finalActionName);
|
||||
$finalURL = '&url=' . urlencode($protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
|
||||
$finalRand = '&rand=' . urlencode(mt_rand(100, 100000));
|
||||
|
||||
// Matomo script
|
||||
$script = <<<MATOMO
|
||||
<script>(function(){var a=new XMLHttpRequest;a.open("post","{$protocol}://{$matomoURL}/{$endpoint}?idsite={$idSite}&rec=1&send_image=1{$finalURL}{$finalActionName}{$finalUsername}{$finalURLRef}{$urlTrackingSearch}{$finalRand}");a.send()})();</script>
|
||||
<noscript><img src="{$protocol}://{$matomoURL}/{$endpoint}?idsite={$idSite}&rec=1&send_image=1{$finalURL}{$finalActionName}{$finalUsername}{$finalURLRef}{$urlTrackingSearch}{$finalRand}" width="1" height="1" alt="" /></noscript>
|
||||
MATOMO;
|
||||
|
||||
return $script;
|
||||
}
|
||||
}
|
||||
|
|
正在加载...
在新工单中引用