Added config to disable lazyload (#121)

This commit is contained in:
alistair3149 2020-06-09 01:28:50 -04:00 committed by GitHub
parent ffb0916c6d
commit 9d6f97cae6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 29 deletions

View file

@ -29,6 +29,12 @@ Name | Description | Values | Default
`$wgCitizenSearchDescriptionSource` | Source of description text on search suggestions | `wikidata` - Use description provided by [WikibaseLib](Extension:WikibaseLib) or [ShortDescription](https://www.mediawiki.org/wiki/Extension:ShortDescription); `textextracts` - Use description provided by [TextExtracts](https://www.mediawiki.org/wiki/Extension:TextExtracts); `pagedescription` - Use description provided by [Description2](https://www.mediawiki.org/wiki/Extension:Description2) or any other extension that sets the `description` page property | `textextracts`
`$wgCitizenMaxSearchResults` | Max number of search suggestions | Integer > 0 | `6`
### Image lazyload
Name | Description | Values | Default
:--- | :--- | :--- | :---
`$wgCitizenEnableLazyload` | Enable or disable image lazyloading | `true` - enable; `false` - disable | `true`
`$wgCitizenThumbnailSize` | Thumbnail size in pixels to use for lazy-loading placeholder | Integer > 0 | `10`
### Security-related
#### Content Security Policy (CSP)
Name | Description | Values | Default

View file

@ -41,7 +41,16 @@ class CitizenHooks {
* @return bool
*/
public static function onBeforePageDisplay( $out, $skin ) {
$out->addModules( 'skins.citizen.scripts.lazyload' );
try {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'Citizen' );
$lazyloadEnabled = $config->get( 'CitizenEnableLazyload' );
} catch ( Exception $e ) {
return false;
}
if ( $lazyloadEnabled === true ) {
$out->addModules( 'skins.citizen.scripts.lazyload' );
}
return true;
}
@ -90,6 +99,7 @@ class CitizenHooks {
public static function onThumbnailBeforeProduceHTML( $thumb, &$attribs, &$linkAttribs ) {
try {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'Citizen' );
$lazyloadEnabled = $config->get( 'CitizenEnableLazyload' );
$thumbSize = $config->get( 'CitizenThumbnailSize' );
} catch ( ConfigException $e ) {
wfLogWarning( sprintf( 'Could not get config for "$wgThumbnailSize". Defaulting to "10". %s',
@ -97,43 +107,46 @@ class CitizenHooks {
$thumbSize = 10;
}
$file = $thumb->getFile();
// Replace thumbnail if lazyload is enabled
if ( $lazyloadEnabled === true ) {
$file = $thumb->getFile();
if ( $file !== null ) {
$request = RequestContext::getMain()->getRequest();
if ( $file !== null ) {
$request = RequestContext::getMain()->getRequest();
if ( defined( 'MW_API' ) && $request->getVal( 'action' ) === 'parse' ) {
return true;
}
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';
}
// Set lazy class for the img
if ( isset( $attribs['class'] ) ) {
$attribs['class'] .= ' lazy';
} else {
$attribs['class'] = 'lazy';
}
// Native API
$attribs['loading'] = 'lazy';
// Native API
$attribs['loading'] = 'lazy';
$attribs['data-src'] = $attribs['src'];
$attribs['data-width'] = $attribs['width'];
$attribs['data-height'] = $attribs['height'];
$attribs['data-src'] = $attribs['src'];
$attribs['data-width'] = $attribs['width'];
$attribs['data-height'] = $attribs['height'];
// Replace src with small size image
$attribs['src'] =
preg_replace( '#/\d+px-#', sprintf( '/%upx-', $thumbSize ), $attribs['src'] );
// Replace src with small size image
$attribs['src'] =
preg_replace( '#/\d+px-#', sprintf( '/%upx-', $thumbSize ), $attribs['src'] );
// So that the 10px thumbnail is enlarged to the right size
$attribs['width'] = $attribs['data-width'];
$attribs['height'] = $attribs['data-height'];
// So that the 10px thumbnail is enlarged to the right size
$attribs['width'] = $attribs['data-width'];
$attribs['height'] = $attribs['data-height'];
// Clean up
unset( $attribs['data-width'], $attribs['data-height'] );
// Clean up
unset( $attribs['data-width'], $attribs['data-height'] );
if ( isset( $attribs['srcset'] ) ) {
$attribs['data-srcset'] = $attribs['srcset'];
unset( $attribs['srcset'] );
if ( isset( $attribs['srcset'] ) ) {
$attribs['data-srcset'] = $attribs['srcset'];
unset( $attribs['srcset'] );
}
}
}

View file

@ -147,6 +147,12 @@
"descriptionmsg": "citizen-config-showpagetools",
"public": true
},
"EnableLazyload": {
"value": true,
"description": "Enable or disable image lazyloading",
"descriptionmsg": "citizen-config-enablelazyload",
"public": true
},
"ThumbnailSize": {
"value": 10,
"description": "Thumbnail size to use for lazy-loading",