2015-12-30 12:41:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PageImages\Hooks;
|
|
|
|
|
|
|
|
use File;
|
|
|
|
use ImageGalleryBase;
|
|
|
|
use Parser;
|
|
|
|
use Title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the "ParserMakeImageParams" and "AfterParserFetchFileAndTitle" hooks.
|
|
|
|
*
|
|
|
|
* @license WTFPL 2.0
|
|
|
|
* @author Max Semenik
|
|
|
|
* @author Thiemo Mättig
|
|
|
|
*/
|
2016-02-14 11:21:00 +00:00
|
|
|
class ParserFileProcessingHookHandlers {
|
2015-12-30 12:41:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ParserMakeImageParams hook handler, saves extended information about images used on page
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserMakeImageParams
|
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @param File|bool $file
|
2016-03-07 08:47:51 +00:00
|
|
|
* @param array[] &$params
|
2015-12-30 12:41:37 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
*/
|
|
|
|
public static function onParserMakeImageParams(
|
|
|
|
Title $title,
|
|
|
|
$file,
|
|
|
|
array &$params,
|
|
|
|
Parser $parser
|
|
|
|
) {
|
|
|
|
$handler = new self();
|
2016-03-07 08:47:51 +00:00
|
|
|
$handler->doParserMakeImageParams( $title, $file, $params, $parser );
|
2015-12-30 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AfterParserFetchFileAndTitle hook handler, saves information about gallery images
|
|
|
|
*
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param ImageGalleryBase $gallery
|
|
|
|
*/
|
2016-11-21 23:29:28 +00:00
|
|
|
public static function onAfterParserFetchFileAndTitle(
|
|
|
|
Parser $parser, ImageGalleryBase $gallery
|
|
|
|
) {
|
2015-12-30 12:41:37 +00:00
|
|
|
$handler = new self();
|
2016-03-07 08:47:51 +00:00
|
|
|
$handler->doAfterParserFetchFileAndTitle( $parser, $gallery );
|
2015-12-30 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Title $title
|
|
|
|
* @param File|bool $file
|
2016-03-07 08:47:51 +00:00
|
|
|
* @param array[] &$params
|
2015-12-30 12:41:37 +00:00
|
|
|
* @param Parser $parser
|
|
|
|
*/
|
|
|
|
public function doParserMakeImageParams(
|
|
|
|
Title $title,
|
|
|
|
$file,
|
|
|
|
array &$params,
|
|
|
|
Parser $parser
|
|
|
|
) {
|
|
|
|
$this->processFile( $parser, $file, $params );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param ImageGalleryBase $gallery
|
|
|
|
*/
|
|
|
|
public function doAfterParserFetchFileAndTitle( Parser $parser, ImageGalleryBase $gallery ) {
|
|
|
|
foreach ( $gallery->getImages() as $image ) {
|
|
|
|
$this->processFile( $parser, $image[0], null );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param File|Title|null $file
|
2016-03-07 08:47:51 +00:00
|
|
|
* @param array[]|null $handlerParams
|
2015-12-30 12:41:37 +00:00
|
|
|
*/
|
|
|
|
private function processFile( Parser $parser, $file, $handlerParams ) {
|
|
|
|
if ( !$file || !$this->processThisTitle( $parser->getTitle() ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !( $file instanceof File ) ) {
|
|
|
|
$file = wfFindFile( $file );
|
|
|
|
if ( !$file ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_array( $handlerParams ) ) {
|
|
|
|
$myParams = $handlerParams;
|
|
|
|
$this->calcWidth( $myParams, $file );
|
|
|
|
} else {
|
2016-11-21 23:29:28 +00:00
|
|
|
$myParams = [];
|
2015-12-30 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$myParams['filename'] = $file->getTitle()->getDBkey();
|
|
|
|
$myParams['fullwidth'] = $file->getWidth();
|
|
|
|
$myParams['fullheight'] = $file->getHeight();
|
|
|
|
|
|
|
|
$out = $parser->getOutput();
|
2016-11-21 23:29:28 +00:00
|
|
|
$pageImages = $out->getExtensionData( 'pageImages' ) ?: [];
|
2015-12-30 12:41:37 +00:00
|
|
|
$pageImages[] = $myParams;
|
|
|
|
$out->setExtensionData( 'pageImages', $pageImages );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if data for this title should be saved
|
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function processThisTitle( Title $title ) {
|
|
|
|
global $wgPageImagesNamespaces;
|
|
|
|
static $flipped = false;
|
|
|
|
|
|
|
|
if ( $flipped === false ) {
|
|
|
|
$flipped = array_flip( $wgPageImagesNamespaces );
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset( $flipped[$title->getNamespace()] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Estimates image size as displayed if not explicitly provided. We don't follow the core size
|
|
|
|
* calculation algorithm precisely because it's not required and editor's intentions are more
|
|
|
|
* important than the precise number.
|
|
|
|
*
|
2016-03-07 08:47:51 +00:00
|
|
|
* @param array[] &$params
|
2015-12-30 12:41:37 +00:00
|
|
|
* @param File $file
|
|
|
|
*/
|
|
|
|
private function calcWidth( array &$params, File $file ) {
|
|
|
|
global $wgThumbLimits, $wgDefaultUserOptions;
|
|
|
|
|
|
|
|
if ( isset( $params['handler']['width'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $params['handler']['height'] ) && $file->getHeight() > 0 ) {
|
|
|
|
$params['handler']['width'] =
|
|
|
|
$file->getWidth() * ( $params['handler']['height'] / $file->getHeight() );
|
|
|
|
} elseif ( isset( $params['frame']['thumbnail'] )
|
|
|
|
|| isset( $params['frame']['thumb'] )
|
2017-06-20 07:17:38 +00:00
|
|
|
|| isset( $params['frame']['frameless'] )
|
|
|
|
) {
|
2015-12-30 12:41:37 +00:00
|
|
|
$params['handler']['width'] = isset( $wgThumbLimits[$wgDefaultUserOptions['thumbsize']] )
|
|
|
|
? $wgThumbLimits[$wgDefaultUserOptions['thumbsize']]
|
|
|
|
: 250;
|
|
|
|
} else {
|
|
|
|
$params['handler']['width'] = $file->getWidth();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|