mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-11-28 00:01:05 +00:00
feat: Convert hooks to HookHandler interface
This commit is contained in:
parent
c1086c9295
commit
24a2a38b4b
|
@ -1,219 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
* Citizen - A responsive skin developed for the Star Citizen Wiki
|
|
||||||
*
|
|
||||||
* This file is part of Citizen.
|
|
||||||
*
|
|
||||||
* Citizen 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 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* Citizen 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 Citizen. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* @file
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Citizen;
|
|
||||||
|
|
||||||
use ConfigException;
|
|
||||||
use MediaWiki\MediaWikiServices;
|
|
||||||
use RequestContext;
|
|
||||||
use ResourceLoaderContext;
|
|
||||||
use ThumbnailImage;
|
|
||||||
use User;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hook handlers for Citizen skin.
|
|
||||||
*
|
|
||||||
* Hook handler method names should be in the form of:
|
|
||||||
* on<HookName>()
|
|
||||||
*/
|
|
||||||
class CitizenHooks {
|
|
||||||
/**
|
|
||||||
* ResourceLoaderGetConfigVars hook handler for setting a config variable
|
|
||||||
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
|
|
||||||
*
|
|
||||||
* @param array &$vars Array of variables to be added into the output of the startup module.
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function onResourceLoaderGetConfigVars( &$vars ) {
|
|
||||||
try {
|
|
||||||
$vars['wgCitizenSearchDescriptionSource'] = self::getSkinConfig( 'CitizenSearchDescriptionSource' );
|
|
||||||
} catch ( ConfigException $e ) {
|
|
||||||
// Should not happen
|
|
||||||
$vars['wgCitizenSearchDescriptionSource'] = 'textextracts';
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$vars['wgCitizenMaxSearchResults'] = self::getSkinConfig( 'CitizenMaxSearchResults' );
|
|
||||||
} catch ( ConfigException $e ) {
|
|
||||||
// Should not happen
|
|
||||||
$vars['wgCitizenMaxSearchResults'] = 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$vars['wgCitizenEnableSearch'] = self::getSkinConfig( 'CitizenEnableSearch' );
|
|
||||||
} catch ( ConfigException $e ) {
|
|
||||||
// Should not happen
|
|
||||||
$vars['wgCitizenEnableSearch'] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SkinPageReadyConfig hook handler
|
|
||||||
*
|
|
||||||
* Replace searchModule provided by skin.
|
|
||||||
*
|
|
||||||
* @since 1.35
|
|
||||||
* @param ResourceLoaderContext $context
|
|
||||||
* @param mixed[] &$config Associative array of configurable options
|
|
||||||
* @return void This hook must not abort, it must return no value
|
|
||||||
*/
|
|
||||||
public static function onSkinPageReadyConfig(
|
|
||||||
ResourceLoaderContext $context,
|
|
||||||
array &$config
|
|
||||||
) {
|
|
||||||
// It's better to exit before any additional check
|
|
||||||
if ( $context->getSkin() !== 'citizen' ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tell the `mediawiki.page.ready` module not to wire up search.
|
|
||||||
$config['search'] = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Lazyload images
|
|
||||||
* Modified from the Lazyload extension
|
|
||||||
* Looks for thumbnail and swap src to data-src
|
|
||||||
*
|
|
||||||
* @param ThumbnailImage $thumbnail
|
|
||||||
* @param array &$attribs
|
|
||||||
* @param array &$linkAttribs
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function onThumbnailBeforeProduceHTML( $thumbnail, &$attribs, &$linkAttribs ) {
|
|
||||||
try {
|
|
||||||
$lazyloadEnabled = self::getSkinConfig( 'CitizenEnableLazyload' );
|
|
||||||
} catch ( ConfigException $e ) {
|
|
||||||
$lazyloadEnabled = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace thumbnail if lazyload is enabled
|
|
||||||
if ( $lazyloadEnabled === true ) {
|
|
||||||
$file = $thumbnail->getFile();
|
|
||||||
|
|
||||||
if ( $file !== null ) {
|
|
||||||
$request = RequestContext::getMain()->getRequest();
|
|
||||||
|
|
||||||
if ( defined( 'MW_API' ) && $request->getVal( 'action' ) === 'parse' ) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set lazy class for the img
|
|
||||||
if ( isset( $attribs['class'] ) ) {
|
|
||||||
$attribs['class'] .= ' lazy';
|
|
||||||
} else {
|
|
||||||
$attribs['class'] = 'lazy';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Native API
|
|
||||||
$attribs['loading'] = 'lazy';
|
|
||||||
|
|
||||||
$attribs['data-src'] = $attribs['src'];
|
|
||||||
$attribs['src'] = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D';
|
|
||||||
|
|
||||||
if ( isset( $attribs['srcset'] ) ) {
|
|
||||||
$attribs['data-srcset'] = $attribs['srcset'];
|
|
||||||
$attribs['srcset'] = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a skin configuration variable.
|
|
||||||
*
|
|
||||||
* @param string $name Name of configuration option.
|
|
||||||
* @return mixed Value configured.
|
|
||||||
* @throws \ConfigException
|
|
||||||
*/
|
|
||||||
private static function getSkinConfig( $name ) {
|
|
||||||
return MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'Citizen' )->get( $name );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add Citizen preferences to the user's Special:Preferences page directly underneath skins.
|
|
||||||
* Based on Vector's implementation
|
|
||||||
*
|
|
||||||
* @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, &$prefs ) {
|
|
||||||
// Preferences to add.
|
|
||||||
$citizenPrefs = [
|
|
||||||
'CitizenThemeUser' => [
|
|
||||||
'type' => 'select',
|
|
||||||
// Droptown title
|
|
||||||
'label-message' => 'prefs-citizen-theme-label',
|
|
||||||
// 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',
|
|
||||||
'options' => [
|
|
||||||
wfMessage( 'prefs-citizen-theme-option-auto' )->escaped() => 'auto',
|
|
||||||
wfMessage( 'prefs-citizen-theme-option-light' )->escaped() => 'light',
|
|
||||||
wfMessage( 'prefs-citizen-theme-option-dark' )->escaped() => 'dark',
|
|
||||||
],
|
|
||||||
'default' => MediaWikiServices::getInstance()->getUserOptionsLookup()->getOption(
|
|
||||||
$user,
|
|
||||||
'CitizenThemeUser'
|
|
||||||
) ?? 'auto',
|
|
||||||
// Only show this section when the Citizen skin is checked. The JavaScript client also uses
|
|
||||||
// this state to determine whether to show or hide the whole section.
|
|
||||||
'hide-if' => [ '!==', 'wpskin', 'citizen' ],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
// Seek the skin preference section to add Citizen preferences just below it.
|
|
||||||
$skinSectionIndex = array_search( 'skin', array_keys( $prefs ) );
|
|
||||||
if ( $skinSectionIndex !== false ) {
|
|
||||||
// Skin preference section found. Inject Citizen skin-specific preferences just below it.
|
|
||||||
// This pattern can be found in Popups too. See T246162.
|
|
||||||
$citizenSectionIndex = $skinSectionIndex + 1;
|
|
||||||
$prefs = array_slice( $prefs, 0, $citizenSectionIndex, true )
|
|
||||||
+ $citizenPrefs
|
|
||||||
+ array_slice( $prefs, $citizenSectionIndex, null, true );
|
|
||||||
} else {
|
|
||||||
// Skin preference section not found. Just append Citizen skin-specific preferences.
|
|
||||||
$prefs += $citizenPrefs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete the override cookie if the theme was changed through the user preferences
|
|
||||||
*
|
|
||||||
* @param array $formData Array of user submitted data
|
|
||||||
* @param \HTMLForm $form HTMLForm object, also a ContextSource
|
|
||||||
* @param User $user User with preferences to be saved
|
|
||||||
* @param bool &$result Boolean indicating success
|
|
||||||
* @param array $oldUserOptions Array with user's old options (before save)
|
|
||||||
* @return bool|void True or no return value to continue or false to abort
|
|
||||||
*/
|
|
||||||
public static function onPreferencesFormPreSave( $formData, $form, $user, &$result, $oldUserOptions ) {
|
|
||||||
if ( isset( $formData['CitizenThemeUser'] ) && $formData['CitizenThemeUser'] !== 'auto' ) {
|
|
||||||
// Reset override cookie from theme toggle
|
|
||||||
$form->getOutput()->getRequest()->response()->setCookie( 'skin-citizen-theme-override', null );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
102
includes/Hooks/PreferenceHooks.php
Normal file
102
includes/Hooks/PreferenceHooks.php
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Citizen - A responsive skin developed for the Star Citizen Wiki
|
||||||
|
*
|
||||||
|
* This file is part of Citizen.
|
||||||
|
*
|
||||||
|
* Citizen 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 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Citizen 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 Citizen. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @file
|
||||||
|
* @ingroup Skins
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare( strict_types=1 );
|
||||||
|
|
||||||
|
namespace Citizen\Hooks;
|
||||||
|
|
||||||
|
use HTMLForm;
|
||||||
|
use MediaWiki\MediaWikiServices;
|
||||||
|
use MediaWiki\Preferences\Hook\GetPreferencesHook;
|
||||||
|
use MediaWiki\Preferences\Hook\PreferencesFormPreSaveHook;
|
||||||
|
use User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hooks to run relating to user preferences
|
||||||
|
*/
|
||||||
|
class PreferenceHooks implements PreferencesFormPreSaveHook, GetPreferencesHook {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete the override cookie if the theme was changed through the user preferences
|
||||||
|
*
|
||||||
|
* @param array $formData Array of user submitted data
|
||||||
|
* @param HTMLForm $form HTMLForm object, also a ContextSource
|
||||||
|
* @param User $user User with preferences to be saved
|
||||||
|
* @param bool &$result Boolean indicating success
|
||||||
|
* @param array $oldUserOptions Array with user's old options (before save)
|
||||||
|
* @return bool|void True or no return value to continue or false to abort
|
||||||
|
*/
|
||||||
|
public function onPreferencesFormPreSave( $formData, $form, $user, &$result, $oldUserOptions ) {
|
||||||
|
if ( isset( $formData['CitizenThemeUser'] ) && $formData['CitizenThemeUser'] !== 'auto' ) {
|
||||||
|
// Reset override cookie from theme toggle
|
||||||
|
$form->getOutput()->getRequest()->response()->setCookie( 'skin-citizen-theme-override', null );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add Citizen preferences to the user's Special:Preferences page directly underneath skins.
|
||||||
|
* Based on Vector's implementation
|
||||||
|
*
|
||||||
|
* @param User $user User whose preferences are being modified.
|
||||||
|
* @param array[] &$preferences Preferences description array, to be fed to a HTMLForm object.
|
||||||
|
*/
|
||||||
|
public function onGetPreferences( $user, &$preferences ) {
|
||||||
|
// Preferences to add.
|
||||||
|
$citizenPrefs = [
|
||||||
|
'CitizenThemeUser' => [
|
||||||
|
'type' => 'select',
|
||||||
|
// Droptown title
|
||||||
|
'label-message' => 'prefs-citizen-theme-label',
|
||||||
|
// 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',
|
||||||
|
'options' => [
|
||||||
|
wfMessage( 'prefs-citizen-theme-option-auto' )->escaped() => 'auto',
|
||||||
|
wfMessage( 'prefs-citizen-theme-option-light' )->escaped() => 'light',
|
||||||
|
wfMessage( 'prefs-citizen-theme-option-dark' )->escaped() => 'dark',
|
||||||
|
],
|
||||||
|
'default' => MediaWikiServices::getInstance()->getUserOptionsLookup()->getOption(
|
||||||
|
$user,
|
||||||
|
'CitizenThemeUser'
|
||||||
|
) ?? 'auto',
|
||||||
|
// Only show this section when the Citizen skin is checked. The JavaScript client also uses
|
||||||
|
// this state to determine whether to show or hide the whole section.
|
||||||
|
'hide-if' => [ '!==', 'wpskin', 'citizen' ],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
// Seek the skin preference section to add Citizen preferences just below it.
|
||||||
|
$skinSectionIndex = array_search( 'skin', array_keys( $preferences ) );
|
||||||
|
if ( $skinSectionIndex !== false ) {
|
||||||
|
// Skin preference section found. Inject Citizen skin-specific preferences just below it.
|
||||||
|
// This pattern can be found in Popups too. See T246162.
|
||||||
|
$citizenSectionIndex = $skinSectionIndex + 1;
|
||||||
|
$preferences = array_slice( $preferences, 0, $citizenSectionIndex, true )
|
||||||
|
+ $citizenPrefs
|
||||||
|
+ array_slice( $preferences, $citizenSectionIndex, null, true );
|
||||||
|
} else {
|
||||||
|
// Skin preference section not found. Just append Citizen skin-specific preferences.
|
||||||
|
$preferences += $citizenPrefs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
79
includes/Hooks/ResourceLoaderHooks.php
Normal file
79
includes/Hooks/ResourceLoaderHooks.php
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Citizen - A responsive skin developed for the Star Citizen Wiki
|
||||||
|
*
|
||||||
|
* This file is part of Citizen.
|
||||||
|
*
|
||||||
|
* Citizen 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 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Citizen 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 Citizen. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @file
|
||||||
|
* @ingroup Skins
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare( strict_types=1 );
|
||||||
|
|
||||||
|
namespace Citizen\Hooks;
|
||||||
|
|
||||||
|
use Config;
|
||||||
|
use ConfigException;
|
||||||
|
use MediaWiki\MediaWikiServices;
|
||||||
|
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
|
||||||
|
use Skin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hooks to run relating to the resource loader
|
||||||
|
*/
|
||||||
|
class ResourceLoaderHooks implements ResourceLoaderGetConfigVarsHook {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ResourceLoaderGetConfigVars hook handler for setting a config variable
|
||||||
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
|
||||||
|
* @param array &$vars
|
||||||
|
* @param Skin $skin
|
||||||
|
* @param Config $config
|
||||||
|
*/
|
||||||
|
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
|
||||||
|
try {
|
||||||
|
$vars['wgCitizenSearchDescriptionSource'] = self::getSkinConfig( 'CitizenSearchDescriptionSource' );
|
||||||
|
} catch ( ConfigException $e ) {
|
||||||
|
// Should not happen
|
||||||
|
$vars['wgCitizenSearchDescriptionSource'] = 'textextracts';
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$vars['wgCitizenMaxSearchResults'] = self::getSkinConfig( 'CitizenMaxSearchResults' );
|
||||||
|
} catch ( ConfigException $e ) {
|
||||||
|
// Should not happen
|
||||||
|
$vars['wgCitizenMaxSearchResults'] = 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$vars['wgCitizenEnableSearch'] = self::getSkinConfig( 'CitizenEnableSearch' );
|
||||||
|
} catch ( ConfigException $e ) {
|
||||||
|
// Should not happen
|
||||||
|
$vars['wgCitizenEnableSearch'] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a skin configuration variable.
|
||||||
|
*
|
||||||
|
* @param string $name Name of configuration option.
|
||||||
|
* @return mixed Value configured.
|
||||||
|
* @throws ConfigException
|
||||||
|
*/
|
||||||
|
private static function getSkinConfig( $name ) {
|
||||||
|
return MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'Citizen' )->get( $name );
|
||||||
|
}
|
||||||
|
}
|
55
includes/Hooks/SkinHooks.php
Normal file
55
includes/Hooks/SkinHooks.php
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Citizen - A responsive skin developed for the Star Citizen Wiki
|
||||||
|
*
|
||||||
|
* This file is part of Citizen.
|
||||||
|
*
|
||||||
|
* Citizen 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 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Citizen 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 Citizen. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @file
|
||||||
|
* @ingroup Skins
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare( strict_types=1 );
|
||||||
|
|
||||||
|
namespace Citizen\Hooks;
|
||||||
|
|
||||||
|
use MediaWiki\Skins\Hook\SkinPageReadyConfigHook;
|
||||||
|
use ResourceLoaderContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hooks to run relating the skin
|
||||||
|
*/
|
||||||
|
class SkinHooks implements SkinPageReadyConfigHook {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SkinPageReadyConfig hook handler
|
||||||
|
*
|
||||||
|
* Replace searchModule provided by skin.
|
||||||
|
*
|
||||||
|
* @since 1.35
|
||||||
|
* @param ResourceLoaderContext $context
|
||||||
|
* @param mixed[] &$config Associative array of configurable options
|
||||||
|
* @return void This hook must not abort, it must return no value
|
||||||
|
*/
|
||||||
|
public function onSkinPageReadyConfig( ResourceLoaderContext $context, array &$config ): void {
|
||||||
|
// It's better to exit before any additional check
|
||||||
|
if ( $context->getSkin() !== 'citizen' ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tell the `mediawiki.page.ready` module not to wire up search.
|
||||||
|
$config['search'] = false;
|
||||||
|
}
|
||||||
|
}
|
100
includes/Hooks/ThumbnailHooks.php
Normal file
100
includes/Hooks/ThumbnailHooks.php
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Citizen - A responsive skin developed for the Star Citizen Wiki
|
||||||
|
*
|
||||||
|
* This file is part of Citizen.
|
||||||
|
*
|
||||||
|
* Citizen 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 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Citizen 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 Citizen. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @file
|
||||||
|
* @ingroup Skins
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare( strict_types=1 );
|
||||||
|
|
||||||
|
namespace Citizen\Hooks;
|
||||||
|
|
||||||
|
use ConfigException;
|
||||||
|
use MediaWiki\Hook\ThumbnailBeforeProduceHTMLHook;
|
||||||
|
use MediaWiki\MediaWikiServices;
|
||||||
|
use RequestContext;
|
||||||
|
use ThumbnailImage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hooks to tun relating thumbnails
|
||||||
|
*/
|
||||||
|
class ThumbnailHooks implements ThumbnailBeforeProduceHTMLHook {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lazyload images
|
||||||
|
* Modified from the Lazyload extension
|
||||||
|
* Looks for thumbnail and swap src to data-src
|
||||||
|
*
|
||||||
|
* @param ThumbnailImage $thumbnail
|
||||||
|
* @param array &$attribs
|
||||||
|
* @param array &$linkAttribs
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function onThumbnailBeforeProduceHTML( $thumbnail, &$attribs, &$linkAttribs ) {
|
||||||
|
try {
|
||||||
|
$lazyloadEnabled = self::getSkinConfig( 'CitizenEnableLazyload' );
|
||||||
|
} catch ( ConfigException $e ) {
|
||||||
|
$lazyloadEnabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace thumbnail if lazyload is enabled
|
||||||
|
if ( $lazyloadEnabled === true ) {
|
||||||
|
$file = $thumbnail->getFile();
|
||||||
|
|
||||||
|
if ( $file !== null ) {
|
||||||
|
$request = RequestContext::getMain()->getRequest();
|
||||||
|
|
||||||
|
if ( defined( 'MW_API' ) && $request->getVal( 'action' ) === 'parse' ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set lazy class for the img
|
||||||
|
if ( isset( $attribs['class'] ) ) {
|
||||||
|
$attribs['class'] .= ' lazy';
|
||||||
|
} else {
|
||||||
|
$attribs['class'] = 'lazy';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Native API
|
||||||
|
$attribs['loading'] = 'lazy';
|
||||||
|
|
||||||
|
$attribs['data-src'] = $attribs['src'];
|
||||||
|
$attribs['src'] = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D';
|
||||||
|
|
||||||
|
if ( isset( $attribs['srcset'] ) ) {
|
||||||
|
$attribs['data-srcset'] = $attribs['srcset'];
|
||||||
|
$attribs['srcset'] = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a skin configuration variable.
|
||||||
|
*
|
||||||
|
* @param string $name Name of configuration option.
|
||||||
|
* @return mixed Value configured.
|
||||||
|
* @throws ConfigException
|
||||||
|
*/
|
||||||
|
private static function getSkinConfig( $name ) {
|
||||||
|
return MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'Citizen' )->get( $name );
|
||||||
|
}
|
||||||
|
}
|
25
skin.json
25
skin.json
|
@ -50,7 +50,6 @@
|
||||||
},
|
},
|
||||||
"AutoloadClasses": {
|
"AutoloadClasses": {
|
||||||
"SkinCitizen": "includes/SkinCitizen.php",
|
"SkinCitizen": "includes/SkinCitizen.php",
|
||||||
"Citizen\\CitizenHooks": "includes/CitizenHooks.php",
|
|
||||||
"Citizen\\ApiWebappManifest": "includes/api/ApiWebappManifest.php"
|
"Citizen\\ApiWebappManifest": "includes/api/ApiWebappManifest.php"
|
||||||
},
|
},
|
||||||
"AutoloadNamespaces": {
|
"AutoloadNamespaces": {
|
||||||
|
@ -64,12 +63,26 @@
|
||||||
"class": "Citizen\\ApiWebappManifest"
|
"class": "Citizen\\ApiWebappManifest"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"HookHandlers": {
|
||||||
|
"PreferenceHooks": {
|
||||||
|
"class": "Citizen\\Hooks\\PreferenceHooks"
|
||||||
|
},
|
||||||
|
"ResourceLoaderHooks": {
|
||||||
|
"class": "Citizen\\Hooks\\ResourceLoaderHooks"
|
||||||
|
},
|
||||||
|
"SkinHooks": {
|
||||||
|
"class": "Citizen\\Hooks\\SkinHooks"
|
||||||
|
},
|
||||||
|
"ThumbnailHooks": {
|
||||||
|
"class": "Citizen\\Hooks\\ThumbnailHooks"
|
||||||
|
}
|
||||||
|
},
|
||||||
"Hooks": {
|
"Hooks": {
|
||||||
"ResourceLoaderGetConfigVars": "Citizen\\CitizenHooks::onResourceLoaderGetConfigVars",
|
"ResourceLoaderGetConfigVars": "ResourceLoaderHooks",
|
||||||
"SkinPageReadyConfig": "Citizen\\CitizenHooks::onSkinPageReadyConfig",
|
"SkinPageReadyConfig": "SkinHooks",
|
||||||
"ThumbnailBeforeProduceHTML": "Citizen\\CitizenHooks::onThumbnailBeforeProduceHTML",
|
"ThumbnailBeforeProduceHTML": "ThumbnailHooks",
|
||||||
"GetPreferences": "Citizen\\CitizenHooks::onGetPreferences",
|
"GetPreferences": "PreferenceHooks",
|
||||||
"PreferencesFormPreSave": "Citizen\\CitizenHooks::onPreferencesFormPreSave"
|
"PreferencesFormPreSave": "PreferenceHooks"
|
||||||
},
|
},
|
||||||
"ResourceModules": {
|
"ResourceModules": {
|
||||||
"skins.citizen.styles": {
|
"skins.citizen.styles": {
|
||||||
|
|
Loading…
Reference in a new issue