2023-04-11 22:12:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @since 1.35
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements;
|
|
|
|
|
2024-01-02 19:54:08 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2023-04-11 22:12:52 +00:00
|
|
|
use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
|
2024-01-02 19:54:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-04-11 22:12:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class ABRequirement implements Requirement {
|
2024-02-05 00:15:55 +00:00
|
|
|
private Config $config;
|
2023-04-11 22:12:52 +00:00
|
|
|
|
2024-02-05 00:15:55 +00:00
|
|
|
private User $user;
|
2023-04-11 22:12:52 +00:00
|
|
|
|
|
|
|
/**
|
2024-02-05 00:15:55 +00:00
|
|
|
* The name of the experiment
|
2023-04-11 22:12:52 +00:00
|
|
|
*/
|
2024-02-05 00:15:55 +00:00
|
|
|
private string $experimentName;
|
2023-04-11 22:12:52 +00:00
|
|
|
|
|
|
|
/**
|
2024-02-05 00:15:55 +00:00
|
|
|
* The name of the requirement
|
2023-04-11 22:12:52 +00:00
|
|
|
*/
|
2024-02-05 00:15:55 +00:00
|
|
|
private string $name;
|
2023-04-11 22:12:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Config $config
|
|
|
|
* @param User $user
|
|
|
|
* @param string $experimentName The name of the experiment
|
2023-05-22 17:54:41 +00:00
|
|
|
* @param string|null $name The name of the requirement
|
2023-04-11 22:12:52 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Config $config,
|
|
|
|
User $user,
|
2023-05-22 17:54:41 +00:00
|
|
|
string $experimentName,
|
|
|
|
?string $name = null
|
2023-04-11 22:12:52 +00:00
|
|
|
) {
|
|
|
|
$this->config = $config;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->experimentName = $experimentName;
|
2023-05-22 17:54:41 +00:00
|
|
|
$this->name = $name ?? '';
|
2023-04-11 22:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getName(): string {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the user is logged-in and false otherwise.
|
|
|
|
*
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isMet(): bool {
|
|
|
|
// Get the experiment configuration from the config object.
|
|
|
|
$experiment = $this->config->get( 'VectorWebABTestEnrollment' );
|
|
|
|
|
2023-05-22 17:54:41 +00:00
|
|
|
// Use the local user ID directly
|
|
|
|
$id = $this->user->getId();
|
2023-04-19 22:55:18 +00:00
|
|
|
|
2023-04-11 22:12:52 +00:00
|
|
|
// Check if the experiment is not enabled or does not match the specified name.
|
|
|
|
if ( !$experiment['enabled'] || $experiment['name'] !== $this->experimentName ) {
|
|
|
|
// If the experiment is not enabled or does not match the specified name,
|
|
|
|
// return true, indicating that the metric is "met"
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// If the experiment is enabled and matches the specified name,
|
2023-05-22 17:54:41 +00:00
|
|
|
// calculate the user's variant based on their user ID
|
2023-04-19 22:55:18 +00:00
|
|
|
$variant = $id % 2;
|
2023-04-11 22:12:52 +00:00
|
|
|
|
|
|
|
// Cast the variant value to a boolean and return it, indicating whether
|
|
|
|
// the user is in the "control" or "test" group.
|
|
|
|
return (bool)$variant;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|