mediawiki-skins-Vector/includes/FeatureManagement/Requirements/LimitedWidthContentRequirement.php
Jon Robson f28f5850c5 Separate generic ConfigHelper class from LimitedWidthContentRequirement
In preparation for a decision around how we share code, the generic code in
LimitedWidthContentrequirement is moved to ConfigHelper class. A fix
recently applied to Minerva (Ied3ed927202dd9356ebeb7e404230f571a1d910d)
is also incorporated into the new class.

The expectation is this code will be either:
1) be replaced by a submodule
OR
2) Used directly by Vector
after a decision tomorrow.

Bug: T359607
in I3967803bd75cf78d27c1a7c08e62730e46678a68 or removed

Change-Id: I04066acc86594e721e071e9096e1b12e1255b54b
2024-03-20 00:23:51 +00:00

103 lines
3.2 KiB
PHP

<?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
*/
namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements;
use MediaWiki\Config\Config;
use MediaWiki\Request\WebRequest;
use MediaWiki\Skins\Vector\ConfigHelper;
use MediaWiki\Skins\Vector\Constants;
use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
use MediaWiki\Title\Title;
/**
* The `MaxWidthRequirement` for content.
* @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
*/
final class LimitedWidthContentRequirement implements Requirement {
private Config $config;
private WebRequest $request;
private ?Title $title;
/**
* This constructor accepts all dependencies needed to determine whether
* the overridable config is enabled for the current user and request.
*
* @param Config $config
* @param WebRequest $request
* @param Title|null $title can be null in testing environment
*/
public function __construct(
Config $config,
WebRequest $request,
Title $title = null
) {
$this->config = $config;
$this->title = $title;
$this->request = $request;
}
/**
* @inheritDoc
*/
public function getName(): string {
return Constants::REQUIREMENT_LIMITED_WIDTH_CONTENT;
}
/**
* Per the $options configuration (for use with $wgVectorMaxWidthOptions)
* determine whether max-width should be disabled on the page.
* For the main page: Check the value of $options['exclude']['mainpage']
* For all other pages, the following will happen:
* - the array $options['include'] of canonical page names will be checked
* against the current page. If a page has been listed there, function will return false
* (max-width will not be disabled)
* Max width is disabled if:
* 1) The current namespace is listed in array $options['exclude']['namespaces']
* OR
* 2) A query string parameter matches one of the regex patterns in $exclusions['querystring'].
*
* @internal only for use inside tests.
* @param array $options
* @param Title $title
* @param WebRequest $request
* @return bool
*/
private static function shouldDisableMaxWidth( array $options, Title $title, WebRequest $request ): bool {
return ConfigHelper::shouldDisable( $options, $request, $title );
}
/**
* Check query parameter to override config or not.
* Then check for AB test value.
* Fallback to config value.
*
* @inheritDoc
*/
public function isMet(): bool {
return $this->title && !self::shouldDisableMaxWidth(
$this->config->get( 'VectorMaxWidthOptions' ),
$this->title,
$this->request
);
}
}