mirror of
https://gerrit.wikimedia.org/r/mediawiki/skins/Vector.git
synced 2024-11-15 11:58:44 +00:00
0ba99a1e97
Sections can be nested. Using rendering/skin as the parent means that Vector's skin preference will always come straight after the skin preference and before Popups. A change in core is needed to update the selector for the element which shows/hides the subsection as well as provide a generic message key to replace the one inside this repository. Note: If the "Vector" specific heading is needed, we can achieve this with a little more work but that is a conversation for another time. Depends-On: Idd06bcfe7935e16732a6a95c1253dbf95c8aca2e Bug: T246162 Change-Id: I4be9764ddca186e5bfd493678afd62d446072e8f
158 lines
5.8 KiB
PHP
158 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace Vector;
|
|
|
|
use HTMLForm;
|
|
use MediaWiki\MediaWikiServices;
|
|
use OutputPage;
|
|
use SkinTemplate;
|
|
use SkinVector;
|
|
use User;
|
|
|
|
/**
|
|
* Presentation hook handlers for Vector skin.
|
|
*
|
|
* Hook handler method names should be in the form of:
|
|
* on<HookName>()
|
|
*/
|
|
class Hooks {
|
|
/**
|
|
* BeforePageDisplayMobile hook handler
|
|
*
|
|
* Make Vector responsive when operating in mobile mode (useformat=mobile)
|
|
*
|
|
* @see https://www.mediawiki.org/wiki/Extension:MobileFrontend/BeforePageDisplayMobile
|
|
* @param OutputPage $out
|
|
* @param SkinTemplate $sk
|
|
*/
|
|
public static function onBeforePageDisplayMobile( OutputPage $out, $sk ) {
|
|
// This makes Vector behave in responsive mode when MobileFrontend is installed
|
|
if ( $sk instanceof SkinVector ) {
|
|
$sk->enableResponsiveMode();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add Vector preferences to the user's Special:Preferences page directly underneath skins.
|
|
*
|
|
* @param User $user User whose preferences are being modified.
|
|
* @param array[] &$prefs Preferences description array, to be fed to a HTMLForm object.
|
|
*/
|
|
public static function onGetPreferences( User $user, array &$prefs ) {
|
|
if ( !self::getConfig( Constants::CONFIG_KEY_SHOW_SKIN_PREFERENCES ) ) {
|
|
// Do not add Vector skin specific preferences.
|
|
return;
|
|
}
|
|
|
|
// Preferences to add.
|
|
$vectorPrefs = [
|
|
Constants::PREF_KEY_SKIN_VERSION => [
|
|
'type' => 'toggle',
|
|
// The checkbox title.
|
|
'label-message' => 'prefs-vector-enable-vector-1-label',
|
|
// Show a little informational snippet underneath the checkbox.
|
|
'help-message' => 'prefs-vector-enable-vector-1-help',
|
|
// The tab location and title of the section to insert the checkbox. The bit after the slash
|
|
// indicates that a prefs-skin-prefs string will be provided.
|
|
'section' => 'rendering/skin/skin-prefs',
|
|
// Convert the preference string to a boolean presentation.
|
|
'default' =>
|
|
$user->getOption( Constants::PREF_KEY_SKIN_VERSION ) === Constants::SKIN_VERSION_LATEST ?
|
|
'0' :
|
|
'1',
|
|
// Only show this section when the Vector skin is checked. The JavaScript client also uses
|
|
// this state to determine whether to show or hide the whole section.
|
|
'hide-if' => [ '!==', 'wpskin', Constants::SKIN_NAME ]
|
|
],
|
|
];
|
|
|
|
// Seek the skin preference section to add Vector preferences just below it.
|
|
$skinSectionIndex = array_search( 'skin', array_keys( $prefs ) );
|
|
if ( $skinSectionIndex !== false ) {
|
|
// Skin preference section found. Inject Vector skin-specific preferences just below it.
|
|
// This pattern can be found in Popups too. See T246162.
|
|
$vectorSectionIndex = $skinSectionIndex + 1;
|
|
$prefs = array_slice( $prefs, 0, $vectorSectionIndex, true )
|
|
+ $vectorPrefs
|
|
+ array_slice( $prefs, $vectorSectionIndex, null, true );
|
|
} else {
|
|
// Skin preference section not found. Just append Vector skin-specific preferences.
|
|
$prefs += $vectorPrefs;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Hook executed on user's Special:Preferences form save. This is used to convert the boolean
|
|
* presentation of skin version to a version string. That is, a single preference change by the
|
|
* user may trigger two writes: a boolean followed by a string.
|
|
*
|
|
* @param array $formData Form data submitted by user
|
|
* @param HTMLForm $form A preferences form
|
|
* @param User $user Logged-in user
|
|
* @param bool &$result Variable defining is form save successful
|
|
* @param array $oldPreferences
|
|
*/
|
|
public static function onPreferencesFormPreSave(
|
|
array $formData,
|
|
HTMLForm $form,
|
|
User $user,
|
|
&$result,
|
|
$oldPreferences
|
|
) {
|
|
$preference = null;
|
|
$isVectorEnabled = ( $formData[ 'skin' ] ?? '' ) === Constants::SKIN_NAME;
|
|
if ( $isVectorEnabled && array_key_exists( Constants::PREF_KEY_SKIN_VERSION, $formData ) ) {
|
|
// A preference was set. However, Special:Preferences converts the result to a boolean when a
|
|
// version name string is wanted instead. Convert the boolean to a version string in case the
|
|
// preference display is changed to a list later (e.g., a "_new_ new Vector" / '3' or
|
|
// 'alpha').
|
|
$preference = $formData[ Constants::PREF_KEY_SKIN_VERSION ] ?
|
|
Constants::SKIN_VERSION_LEGACY :
|
|
Constants::SKIN_VERSION_LATEST;
|
|
} elseif ( array_key_exists( Constants::PREF_KEY_SKIN_VERSION, $oldPreferences ) ) {
|
|
// The setting was cleared. However, this is likely because a different skin was chosen and
|
|
// the skin version preference was hidden.
|
|
$preference = $oldPreferences[ Constants::PREF_KEY_SKIN_VERSION ];
|
|
}
|
|
if ( $preference !== null ) {
|
|
$user->setOption( Constants::PREF_KEY_SKIN_VERSION, $preference );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Called on each pageview to populate preference defaults for existing users.
|
|
*
|
|
* @param array &$defaultPrefs
|
|
*/
|
|
public static function onUserGetDefaultOptions( array &$defaultPrefs ) {
|
|
$default = self::getConfig( Constants::CONFIG_KEY_DEFAULT_SKIN_VERSION_FOR_EXISTING_ACCOUNTS );
|
|
$defaultPrefs[ Constants::PREF_KEY_SKIN_VERSION ] = $default;
|
|
}
|
|
|
|
/**
|
|
* Called one time when initializing a users preferences for a newly created account.
|
|
*
|
|
* @param User $user Newly created user object.
|
|
* @param bool $isAutoCreated
|
|
*/
|
|
public static function onLocalUserCreated( User $user, $isAutoCreated ) {
|
|
$default = self::getConfig( Constants::CONFIG_KEY_DEFAULT_SKIN_VERSION_FOR_NEW_ACCOUNTS );
|
|
// Permanently set the default preference. The user can later change this preference, however,
|
|
// self::onLocalUserCreated() will not be executed for that account again.
|
|
$user->setOption( Constants::PREF_KEY_SKIN_VERSION, $default );
|
|
}
|
|
|
|
/**
|
|
* Get a configuration variable such as `Constants::CONFIG_KEY_SHOW_SKIN_PREFERENCES`.
|
|
*
|
|
* @param string $name Name of configuration option.
|
|
* @return mixed Value configured.
|
|
* @throws \ConfigException
|
|
*/
|
|
private static function getConfig( $name ) {
|
|
/* @var Config */ $service =
|
|
MediaWikiServices::getInstance()->getService( Constants::SERVICE_CONFIG );
|
|
return $service->get( $name );
|
|
}
|
|
}
|