2019-08-15 17:40:13 +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
|
|
|
|
*/
|
|
|
|
|
2019-12-25 23:13:59 +00:00
|
|
|
namespace Citizen;
|
|
|
|
|
|
|
|
use ConfigException;
|
|
|
|
use Exception;
|
2019-12-12 03:48:55 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-12-25 23:13:59 +00:00
|
|
|
use OutputPage;
|
|
|
|
use RequestContext;
|
|
|
|
use Skin;
|
|
|
|
use ThumbnailImage;
|
2019-08-15 17:40:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook handlers for Citizen skin.
|
|
|
|
*
|
|
|
|
* Hook handler method names should be in the form of:
|
2019-12-25 23:13:59 +00:00
|
|
|
* on<HookName>()
|
2019-08-15 17:40:13 +00:00
|
|
|
*/
|
|
|
|
class CitizenHooks {
|
2019-12-12 03:38:55 +00:00
|
|
|
/**
|
|
|
|
* 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 ) {
|
2019-12-25 23:13:59 +00:00
|
|
|
try {
|
|
|
|
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'Citizen' );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-12 03:48:55 +00:00
|
|
|
|
2019-12-25 23:13:59 +00:00
|
|
|
try {
|
2020-01-01 22:40:56 +00:00
|
|
|
$vars['wgCitizenSearchDescriptionSource'] = $config->get( 'CitizenSearchDescriptionSource' );
|
2019-12-25 23:13:59 +00:00
|
|
|
} catch ( ConfigException $e ) {
|
|
|
|
// Should not happen
|
2020-01-01 22:40:56 +00:00
|
|
|
$vars['wgCitizenSearchDescriptionSource'] = 'textextracts';
|
2019-12-25 23:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-01-01 22:40:56 +00:00
|
|
|
$vars['wgCitizenMaxSearchResults'] = $config->get( 'CitizenMaxSearchResults' );
|
2019-12-25 23:13:59 +00:00
|
|
|
} catch ( ConfigException $e ) {
|
|
|
|
// Should not happen
|
2020-01-01 22:40:56 +00:00
|
|
|
$vars['wgCitizenMaxSearchResults'] = 6;
|
2019-12-25 23:13:59 +00:00
|
|
|
}
|
2019-12-12 03:38:55 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
/**
|
|
|
|
* Lazyload images
|
|
|
|
* Modified from the Lazyload extension
|
2019-08-15 22:31:54 +00:00
|
|
|
* Looks for thumbnail and swap src to data-src
|
2019-12-25 23:13:59 +00:00
|
|
|
*
|
|
|
|
* @param ThumbnailImage $thumb
|
|
|
|
* @param array &$attribs
|
|
|
|
* @param array &$linkAttribs
|
|
|
|
* @return bool
|
2019-08-15 17:40:13 +00:00
|
|
|
*/
|
2019-12-25 23:13:59 +00:00
|
|
|
public static function onThumbnailBeforeProduceHTML( $thumb, &$attribs, &$linkAttribs ) {
|
2020-01-11 20:27:39 +00:00
|
|
|
try {
|
|
|
|
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'Citizen' );
|
2020-06-09 05:28:50 +00:00
|
|
|
$lazyloadEnabled = $config->get( 'CitizenEnableLazyload' );
|
2020-01-11 20:32:32 +00:00
|
|
|
$thumbSize = $config->get( 'CitizenThumbnailSize' );
|
2020-01-11 20:27:39 +00:00
|
|
|
} catch ( ConfigException $e ) {
|
|
|
|
wfLogWarning( sprintf( 'Could not get config for "$wgThumbnailSize". Defaulting to "10". %s',
|
|
|
|
$e->getMessage() ) );
|
|
|
|
$thumbSize = 10;
|
|
|
|
}
|
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
// Replace thumbnail if lazyload is enabled
|
|
|
|
if ( $lazyloadEnabled === true ) {
|
|
|
|
$file = $thumb->getFile();
|
2019-08-15 22:31:54 +00:00
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
if ( $file !== null ) {
|
|
|
|
$request = RequestContext::getMain()->getRequest();
|
2019-12-25 23:13:59 +00:00
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
if ( defined( 'MW_API' ) && $request->getVal( 'action' ) === 'parse' ) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-15 22:31:54 +00:00
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
// Set lazy class for the img
|
|
|
|
if ( isset( $attribs['class'] ) ) {
|
|
|
|
$attribs['class'] .= ' lazy';
|
|
|
|
} else {
|
|
|
|
$attribs['class'] = 'lazy';
|
|
|
|
}
|
2019-08-16 02:48:10 +00:00
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
// Native API
|
|
|
|
$attribs['loading'] = 'lazy';
|
2019-08-16 00:39:40 +00:00
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
$attribs['data-src'] = $attribs['src'];
|
|
|
|
$attribs['data-width'] = $attribs['width'];
|
|
|
|
$attribs['data-height'] = $attribs['height'];
|
2019-08-15 22:31:54 +00:00
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
// Replace src with small size image
|
|
|
|
$attribs['src'] =
|
|
|
|
preg_replace( '#/\d+px-#', sprintf( '/%upx-', $thumbSize ), $attribs['src'] );
|
2019-08-16 02:48:10 +00:00
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
// So that the 10px thumbnail is enlarged to the right size
|
|
|
|
$attribs['width'] = $attribs['data-width'];
|
|
|
|
$attribs['height'] = $attribs['data-height'];
|
2019-08-21 19:57:17 +00:00
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
// Clean up
|
|
|
|
unset( $attribs['data-width'], $attribs['data-height'] );
|
2019-08-21 19:57:17 +00:00
|
|
|
|
2020-06-09 05:28:50 +00:00
|
|
|
if ( isset( $attribs['srcset'] ) ) {
|
|
|
|
$attribs['data-srcset'] = $attribs['srcset'];
|
|
|
|
unset( $attribs['srcset'] );
|
|
|
|
}
|
2019-08-15 22:31:54 +00:00
|
|
|
}
|
2019-08-15 17:40:13 +00:00
|
|
|
}
|
2019-12-25 23:13:59 +00:00
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|